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