]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar.c
Add thumbnail support.
[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-sidebar-bookmarks.h"
33 #include "ev-sidebar-thumbnails.h"
34
35 typedef struct
36 {
37         char *id;
38         char *title;
39         GtkWidget *main_widget;
40 } EvSidebarPage;
41
42 enum
43 {
44         PAGE_COLUMN_ID,
45         PAGE_COLUMN_TITLE,
46         PAGE_COLUMN_MAIN_WIDGET,
47         PAGE_COLUMN_NOTEBOOK_INDEX,
48         PAGE_COLUMN_NUM_COLS
49 };
50
51 struct _EvSidebarPrivate {
52         GtkWidget *option_menu;
53         GtkWidget *notebook;
54
55         GtkTreeModel *page_model;
56 };
57
58 static void ev_sidebar_omenu_changed_cb (GtkComboBox *combo_box,
59                                          gpointer     user_data);
60
61 G_DEFINE_TYPE (EvSidebar, ev_sidebar, GTK_TYPE_VBOX)
62
63 #define EV_SIDEBAR_GET_PRIVATE(object) \
64         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR, EvSidebarPrivate))
65
66 static void
67 ev_sidebar_class_init (EvSidebarClass *ev_sidebar_class)
68 {
69         GObjectClass *g_object_class;
70
71         g_object_class = G_OBJECT_CLASS (ev_sidebar_class);
72
73         g_type_class_add_private (g_object_class, sizeof (EvSidebarPrivate));
74
75 }
76
77 static void
78 ev_sidebar_init (EvSidebar *ev_sidebar)
79 {
80         GtkWidget *hbox;
81         GtkCellRenderer *renderer;
82         
83         ev_sidebar->priv = EV_SIDEBAR_GET_PRIVATE (ev_sidebar);
84         gtk_box_set_spacing (GTK_BOX (ev_sidebar), 6);
85
86         /* data model */
87         ev_sidebar->priv->page_model = (GtkTreeModel *)
88                 gtk_list_store_new (PAGE_COLUMN_NUM_COLS,
89                                     G_TYPE_STRING,
90                                     G_TYPE_STRING,
91                                     GTK_TYPE_WIDGET,
92                                     G_TYPE_INT);
93
94         /* top option menu */
95         hbox = gtk_hbox_new (FALSE, 6);
96         gtk_box_pack_start (GTK_BOX (ev_sidebar), hbox,
97                             FALSE, FALSE, 0);
98         ev_sidebar->priv->option_menu =
99                 gtk_combo_box_new_with_model (ev_sidebar->priv->page_model);
100         g_signal_connect (ev_sidebar->priv->option_menu, "changed",
101                           G_CALLBACK (ev_sidebar_omenu_changed_cb), ev_sidebar);
102         gtk_box_pack_start (GTK_BOX (hbox),
103                             ev_sidebar->priv->option_menu,
104                             FALSE, FALSE, 0);
105         
106         renderer = gtk_cell_renderer_text_new ();
107         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (ev_sidebar->priv->option_menu),
108                                     renderer, TRUE);
109         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (ev_sidebar->priv->option_menu),
110                                         renderer,
111                                         "text", PAGE_COLUMN_TITLE,
112                                         NULL);
113
114         ev_sidebar->priv->notebook = gtk_notebook_new ();
115         gtk_notebook_set_show_border (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
116         gtk_notebook_set_show_tabs (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
117         gtk_box_pack_start (GTK_BOX (ev_sidebar), ev_sidebar->priv->notebook,
118                             TRUE, TRUE, 0);
119         gtk_widget_show_all (GTK_WIDGET (ev_sidebar));
120 }
121
122 static void
123 ev_sidebar_omenu_changed_cb (GtkComboBox *combo_box,
124                              gpointer     user_data)
125 {
126         GtkTreeIter iter;
127         EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
128
129         if (gtk_combo_box_get_active_iter (combo_box, &iter)) {
130                 gint index;
131
132                 gtk_tree_model_get (ev_sidebar->priv->page_model,
133                                     &iter,
134                                     PAGE_COLUMN_NOTEBOOK_INDEX, &index,
135                                     -1);
136                 gtk_notebook_set_current_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
137                                                index);
138                                                
139         }
140 }
141
142 /* Public functions */
143
144 GtkWidget *
145 ev_sidebar_new (void)
146 {
147         GtkWidget *ev_sidebar;
148
149         ev_sidebar = g_object_new (EV_TYPE_SIDEBAR, NULL);
150
151         return ev_sidebar;
152 }
153
154 void
155 ev_sidebar_add_page (EvSidebar   *ev_sidebar,
156                      const gchar *page_id,
157                      const gchar *title,
158                      GtkWidget   *main_widget)
159 {
160         GtkTreeIter iter;
161         int index;
162
163         g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
164         g_return_if_fail (page_id != NULL);
165         g_return_if_fail (title != NULL);
166         g_return_if_fail (GTK_IS_WIDGET (main_widget));
167
168         index = gtk_notebook_append_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
169                                           main_widget, NULL);
170                                           
171         gtk_list_store_insert_with_values (GTK_LIST_STORE (ev_sidebar->priv->page_model),
172                                            &iter, 0,
173                                            PAGE_COLUMN_ID, page_id,
174                                            PAGE_COLUMN_TITLE, title,
175                                            PAGE_COLUMN_MAIN_WIDGET, main_widget,
176                                            PAGE_COLUMN_NOTEBOOK_INDEX, index,
177                                            -1);
178
179         /* Set the first item added as active */
180         if (gtk_combo_box_get_active (GTK_COMBO_BOX (ev_sidebar->priv->option_menu)))
181                 gtk_combo_box_set_active (GTK_COMBO_BOX (ev_sidebar->priv->option_menu), 0);
182 }
183
184 void
185 ev_sidebar_set_document (EvSidebar   *sidebar,
186                          EvDocument  *document)
187 {
188         EvSidebarPrivate *priv;
189         GtkTreeIter iter;
190         gboolean result;
191
192         g_return_if_fail (EV_IS_SIDEBAR (sidebar));
193         g_return_if_fail (EV_IS_DOCUMENT (document));
194
195         priv = sidebar->priv;
196         
197         /* FIXME: We should prolly make sidebars have an interface.  For now, we
198          * do this bad hack (TM)
199          */
200         for (result = gtk_tree_model_get_iter_first (priv->page_model, &iter);
201              result;
202              result = gtk_tree_model_iter_next (priv->page_model, &iter)) {
203                 GtkWidget *widget;
204
205                 gtk_tree_model_get (priv->page_model, &iter,
206                                     PAGE_COLUMN_MAIN_WIDGET, &widget,
207                                     -1);
208
209                 if (EV_IS_SIDEBAR_BOOKMARKS (widget))
210                         /* && EV_IS_BOOKMARKS (document)
211                            && ev_bookmarks_has_bookmarks (document)... */
212                         ev_sidebar_bookmarks_set_document (EV_SIDEBAR_BOOKMARKS (widget),
213                                                            document);
214                 else if (EV_IS_SIDEBAR_THUMBNAILS (widget) &&
215                          EV_IS_DOCUMENT_THUMBNAILS (document))
216                         ev_sidebar_thumbnails_set_document (EV_SIDEBAR_THUMBNAILS (widget),
217                                                             document);
218                                 
219
220                   
221         }
222         
223
224 }
225
226 void
227 ev_sidebar_clear (EvSidebar *ev_sidebar)
228 {
229         g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
230
231         gtk_list_store_clear (GTK_LIST_STORE (ev_sidebar->priv->page_model));
232 }