]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar.c
* backend/ev-document-bookmarks.h: * pdf/xpdf/Makefile.am: *
[evince.git] / shell / ev-sidebar.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Red Hat, Inc.
4  *
5  *  Author:
6  *    Jonathan Blandford <jrb@alum.mit.edu>
7  *
8  * Evince is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Evince is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <gtk/gtk.h>
29
30 #include "ev-sidebar.h"
31 #include "ev-document-thumbnails.h"
32 #include "ev-document-bookmarks.h"
33 #include "ev-sidebar-bookmarks.h"
34 #include "ev-sidebar-thumbnails.h"
35
36 typedef struct
37 {
38         char *id;
39         char *title;
40         GtkWidget *main_widget;
41 } EvSidebarPage;
42
43 enum
44 {
45         PAGE_COLUMN_ID,
46         PAGE_COLUMN_TITLE,
47         PAGE_COLUMN_MAIN_WIDGET,
48         PAGE_COLUMN_NOTEBOOK_INDEX,
49         PAGE_COLUMN_NUM_COLS
50 };
51
52 struct _EvSidebarPrivate {
53         GtkWidget *option_menu;
54         GtkWidget *notebook;
55
56         GtkTreeModel *page_model;
57 };
58
59 static void ev_sidebar_omenu_changed_cb (GtkComboBox *combo_box,
60                                          gpointer     user_data);
61
62 G_DEFINE_TYPE (EvSidebar, ev_sidebar, GTK_TYPE_VBOX)
63
64 #define EV_SIDEBAR_GET_PRIVATE(object) \
65         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR, EvSidebarPrivate))
66
67 static void
68 ev_sidebar_class_init (EvSidebarClass *ev_sidebar_class)
69 {
70         GObjectClass *g_object_class;
71
72         g_object_class = G_OBJECT_CLASS (ev_sidebar_class);
73
74         g_type_class_add_private (g_object_class, sizeof (EvSidebarPrivate));
75
76 }
77
78 static void
79 ev_sidebar_init (EvSidebar *ev_sidebar)
80 {
81         GtkWidget *hbox;
82         GtkCellRenderer *renderer;
83         
84         ev_sidebar->priv = EV_SIDEBAR_GET_PRIVATE (ev_sidebar);
85         gtk_box_set_spacing (GTK_BOX (ev_sidebar), 6);
86
87         /* data model */
88         ev_sidebar->priv->page_model = (GtkTreeModel *)
89                 gtk_list_store_new (PAGE_COLUMN_NUM_COLS,
90                                     G_TYPE_STRING,
91                                     G_TYPE_STRING,
92                                     GTK_TYPE_WIDGET,
93                                     G_TYPE_INT);
94
95         /* top option menu */
96         hbox = gtk_hbox_new (FALSE, 6);
97         gtk_box_pack_start (GTK_BOX (ev_sidebar), hbox,
98                             FALSE, FALSE, 0);
99         ev_sidebar->priv->option_menu =
100                 gtk_combo_box_new_with_model (ev_sidebar->priv->page_model);
101         g_signal_connect (ev_sidebar->priv->option_menu, "changed",
102                           G_CALLBACK (ev_sidebar_omenu_changed_cb), ev_sidebar);
103         gtk_box_pack_start (GTK_BOX (hbox),
104                             ev_sidebar->priv->option_menu,
105                             FALSE, FALSE, 0);
106         
107         renderer = gtk_cell_renderer_text_new ();
108         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (ev_sidebar->priv->option_menu),
109                                     renderer, TRUE);
110         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (ev_sidebar->priv->option_menu),
111                                         renderer,
112                                         "text", PAGE_COLUMN_TITLE,
113                                         NULL);
114
115         ev_sidebar->priv->notebook = gtk_notebook_new ();
116         gtk_notebook_set_show_border (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
117         gtk_notebook_set_show_tabs (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
118         gtk_box_pack_start (GTK_BOX (ev_sidebar), ev_sidebar->priv->notebook,
119                             TRUE, TRUE, 0);
120         gtk_widget_show_all (GTK_WIDGET (ev_sidebar));
121 }
122
123 static void
124 ev_sidebar_omenu_changed_cb (GtkComboBox *combo_box,
125                              gpointer     user_data)
126 {
127         GtkTreeIter iter;
128         EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
129
130         if (gtk_combo_box_get_active_iter (combo_box, &iter)) {
131                 gint index;
132
133                 gtk_tree_model_get (ev_sidebar->priv->page_model,
134                                     &iter,
135                                     PAGE_COLUMN_NOTEBOOK_INDEX, &index,
136                                     -1);
137                 gtk_notebook_set_current_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
138                                                index);
139                                                
140         }
141 }
142
143 /* Public functions */
144
145 GtkWidget *
146 ev_sidebar_new (void)
147 {
148         GtkWidget *ev_sidebar;
149
150         ev_sidebar = g_object_new (EV_TYPE_SIDEBAR, NULL);
151
152         return ev_sidebar;
153 }
154
155 void
156 ev_sidebar_add_page (EvSidebar   *ev_sidebar,
157                      const gchar *page_id,
158                      const gchar *title,
159                      GtkWidget   *main_widget)
160 {
161         GtkTreeIter iter;
162         int index;
163
164         g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
165         g_return_if_fail (page_id != NULL);
166         g_return_if_fail (title != NULL);
167         g_return_if_fail (GTK_IS_WIDGET (main_widget));
168
169         index = gtk_notebook_append_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
170                                           main_widget, NULL);
171                                           
172         gtk_list_store_insert_with_values (GTK_LIST_STORE (ev_sidebar->priv->page_model),
173                                            &iter, 0,
174                                            PAGE_COLUMN_ID, page_id,
175                                            PAGE_COLUMN_TITLE, title,
176                                            PAGE_COLUMN_MAIN_WIDGET, main_widget,
177                                            PAGE_COLUMN_NOTEBOOK_INDEX, index,
178                                            -1);
179
180         /* Set the first item added as active */
181         if (gtk_combo_box_get_active (GTK_COMBO_BOX (ev_sidebar->priv->option_menu)))
182                 gtk_combo_box_set_active (GTK_COMBO_BOX (ev_sidebar->priv->option_menu), 1);
183 }
184
185 void
186 ev_sidebar_set_document (EvSidebar   *sidebar,
187                          EvDocument  *document)
188 {
189         EvSidebarPrivate *priv;
190         GtkTreeIter iter;
191         gboolean result;
192
193         g_return_if_fail (EV_IS_SIDEBAR (sidebar));
194         g_return_if_fail (EV_IS_DOCUMENT (document));
195
196         priv = sidebar->priv;
197         
198         /* FIXME: We should prolly make sidebars have an interface.  For now, we
199          * do this bad hack (TM)
200          */
201         for (result = gtk_tree_model_get_iter_first (priv->page_model, &iter);
202              result;
203              result = gtk_tree_model_iter_next (priv->page_model, &iter)) {
204                 GtkWidget *widget;
205
206                 gtk_tree_model_get (priv->page_model, &iter,
207                                     PAGE_COLUMN_MAIN_WIDGET, &widget,
208                                     -1);
209
210                 if (EV_IS_SIDEBAR_BOOKMARKS (widget)
211                     && EV_IS_DOCUMENT_BOOKMARKS (document)
212                     && ev_document_bookmarks_has_document_bookmarks (EV_DOCUMENT_BOOKMARKS (document)))
213                         ev_sidebar_bookmarks_set_document (EV_SIDEBAR_BOOKMARKS (widget),
214                                                            document);
215                 else if (EV_IS_SIDEBAR_THUMBNAILS (widget) &&
216                          EV_IS_DOCUMENT_THUMBNAILS (document))
217                         ev_sidebar_thumbnails_set_document (EV_SIDEBAR_THUMBNAILS (widget),
218                                                             document);
219                                 
220
221                   
222         }
223         
224
225 }
226
227 void
228 ev_sidebar_clear (EvSidebar *ev_sidebar)
229 {
230         g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
231
232         gtk_list_store_clear (GTK_LIST_STORE (ev_sidebar->priv->page_model));
233 }