]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar.c
bookmarks: Add a popup menu to open, rename and remove bookmarks
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31
32 #include "ev-sidebar.h"
33 #include "ev-sidebar-page.h"
34
35 enum
36 {
37         PROP_0,
38         PROP_CURRENT_PAGE
39 };
40
41 enum
42 {
43         PAGE_COLUMN_TITLE,
44         PAGE_COLUMN_MENU_ITEM,
45         PAGE_COLUMN_MAIN_WIDGET,
46         PAGE_COLUMN_NOTEBOOK_INDEX,
47         PAGE_COLUMN_NUM_COLS
48 };
49
50 struct _EvSidebarPrivate {
51         GtkWidget *notebook;
52         GtkWidget *select_button;
53         GtkWidget *menu;
54         GtkWidget *hbox;
55         GtkWidget *label;
56
57         EvDocumentModel *model;
58         GtkTreeModel *page_model;
59 };
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_dispose (GObject *object)
68 {
69         EvSidebar *ev_sidebar = EV_SIDEBAR (object);
70
71         if (ev_sidebar->priv->menu) {
72                 gtk_menu_detach (GTK_MENU (ev_sidebar->priv->menu));
73                 ev_sidebar->priv->menu = NULL;
74         }
75         
76         if (ev_sidebar->priv->page_model) {
77                 g_object_unref (ev_sidebar->priv->page_model);
78                 ev_sidebar->priv->page_model = NULL;
79         }
80                 
81            
82         G_OBJECT_CLASS (ev_sidebar_parent_class)->dispose (object);
83 }
84
85 static void
86 ev_sidebar_select_page (EvSidebar *ev_sidebar,  GtkTreeIter *iter)
87 {
88         char *title;
89         int index;
90
91         gtk_tree_model_get (ev_sidebar->priv->page_model, iter,
92                             PAGE_COLUMN_TITLE, &title, 
93                             PAGE_COLUMN_NOTEBOOK_INDEX, &index,
94                             -1);
95
96         gtk_notebook_set_current_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook), index);
97         gtk_label_set_text (GTK_LABEL (ev_sidebar->priv->label), title);
98
99         g_free (title);
100 }
101
102 void
103 ev_sidebar_set_page (EvSidebar   *ev_sidebar,
104                      GtkWidget   *main_widget)
105 {
106         GtkTreeIter iter;
107         gboolean valid;
108
109         valid = gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
110            
111         while (valid) {
112                 GtkWidget *widget;
113
114                 gtk_tree_model_get (ev_sidebar->priv->page_model, &iter,
115                                     PAGE_COLUMN_MAIN_WIDGET, &widget,
116                                     -1);
117                          
118                 if (widget == main_widget) {
119                         ev_sidebar_select_page (ev_sidebar, &iter);
120                         valid = FALSE;
121                 } else {
122                         valid = gtk_tree_model_iter_next (ev_sidebar->priv->page_model, &iter);
123                 }
124                 g_object_unref (widget);
125         }
126
127         g_object_notify (G_OBJECT (ev_sidebar), "current-page");
128 }
129
130 static void
131 ev_sidebar_set_property (GObject      *object,
132                          guint         prop_id,
133                          const GValue *value,
134                          GParamSpec   *pspec)
135 {
136         EvSidebar *sidebar = EV_SIDEBAR (object);
137
138         switch (prop_id)
139         {
140         case PROP_CURRENT_PAGE:
141                 ev_sidebar_set_page (sidebar, g_value_get_object (value));      
142                 break;
143         default:
144                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145         }
146 }
147
148 static GtkWidget *
149 ev_sidebar_get_current_page (EvSidebar *sidebar)
150 {
151         GtkNotebook *notebook = GTK_NOTEBOOK (sidebar->priv->notebook);
152
153         return gtk_notebook_get_nth_page
154                 (notebook, gtk_notebook_get_current_page (notebook));
155 }
156
157 static void
158 ev_sidebar_get_property (GObject *object,
159                          guint prop_id,
160                          GValue *value,
161                          GParamSpec *pspec)
162 {
163         EvSidebar *sidebar = EV_SIDEBAR (object);
164
165         switch (prop_id)
166         {
167         case PROP_CURRENT_PAGE:
168                 g_value_set_object (value, ev_sidebar_get_current_page (sidebar));
169                 break;
170         default:
171                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172         }
173 }
174
175 static void
176 ev_sidebar_class_init (EvSidebarClass *ev_sidebar_class)
177 {
178         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_sidebar_class);
179
180         g_type_class_add_private (g_object_class, sizeof (EvSidebarPrivate));
181
182         g_object_class->dispose = ev_sidebar_dispose;
183         g_object_class->get_property = ev_sidebar_get_property;
184         g_object_class->set_property = ev_sidebar_set_property;
185
186         g_object_class_install_property (g_object_class,
187                                          PROP_CURRENT_PAGE,
188                                          g_param_spec_object ("current-page",
189                                                               "Current page",
190                                                               "The currently visible page",
191                                                               GTK_TYPE_WIDGET,
192                                                               G_PARAM_READWRITE));
193 }
194
195 static void
196 ev_sidebar_menu_position_under (GtkMenu  *menu,
197                                 int      *x,
198                                 int      *y,
199                                 gboolean *push_in,
200                                 gpointer  user_data)
201 {
202         GtkWidget    *widget;
203         GtkAllocation allocation;
204
205         g_return_if_fail (GTK_IS_BUTTON (user_data));
206         g_return_if_fail (!gtk_widget_get_has_window (GTK_WIDGET (user_data)));
207
208         widget = GTK_WIDGET (user_data);
209            
210         gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
211         gtk_widget_get_allocation (widget, &allocation);
212            
213         *x += allocation.x;
214         *y += allocation.y + allocation.height;
215            
216         *push_in = FALSE;
217 }
218
219 static gboolean
220 ev_sidebar_select_button_press_cb (GtkWidget      *widget,
221                                    GdkEventButton *event,
222                                    gpointer        user_data)
223 {
224         EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
225
226         if (event->button == 1) {
227                 GtkRequisition requisition;
228                 GtkAllocation allocation;
229                 gint width;
230
231                 gtk_widget_get_allocation (widget, &allocation);
232                 width = allocation.width;
233                 gtk_widget_set_size_request (ev_sidebar->priv->menu, -1, -1);
234                 gtk_widget_get_preferred_size (ev_sidebar->priv->menu, &requisition, NULL);
235                 gtk_widget_set_size_request (ev_sidebar->priv->menu,
236                                              MAX (width, requisition.width), -1);
237                 
238                 gtk_widget_grab_focus (widget);
239                          
240                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
241                 gtk_menu_popup (GTK_MENU (ev_sidebar->priv->menu),
242                                 NULL, NULL, ev_sidebar_menu_position_under, widget,
243                                 event->button, event->time);
244
245                 return TRUE;
246         }
247
248         return FALSE;
249 }
250
251 static gboolean
252 ev_sidebar_select_button_key_press_cb (GtkWidget   *widget,
253                                        GdkEventKey *event,
254                                        gpointer     user_data)
255 {
256         EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
257            
258         if (event->keyval == GDK_KEY_space ||
259             event->keyval == GDK_KEY_KP_Space ||
260             event->keyval == GDK_KEY_Return ||
261             event->keyval == GDK_KEY_KP_Enter) {
262                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
263                 gtk_menu_popup (GTK_MENU (ev_sidebar->priv->menu),
264                                 NULL, NULL, ev_sidebar_menu_position_under, widget,
265                                 1, event->time);
266                 return TRUE;
267         }
268
269         return FALSE;
270 }
271
272 static void
273 ev_sidebar_close_clicked_cb (GtkWidget *widget,
274                              gpointer   user_data)
275 {
276         EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
277
278         gtk_widget_hide (GTK_WIDGET (ev_sidebar));
279 }
280
281 static void
282 ev_sidebar_menu_deactivate_cb (GtkWidget *widget,
283                                gpointer   user_data)
284 {
285         GtkWidget *menu_button;
286
287         menu_button = GTK_WIDGET (user_data);
288            
289         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);
290 }
291
292 static void
293 ev_sidebar_menu_detach_cb (GtkWidget *widget,
294                            GtkMenu   *menu)
295 {
296         EvSidebar *ev_sidebar = EV_SIDEBAR (widget);
297            
298         ev_sidebar->priv->menu = NULL;
299 }
300
301 static void
302 ev_sidebar_menu_item_activate_cb (GtkWidget *widget,
303                                   gpointer   user_data)
304 {
305         EvSidebar *ev_sidebar = EV_SIDEBAR (user_data);
306         GtkTreeIter iter;
307         GtkWidget *menu_item, *item;
308         gboolean valid;
309
310         menu_item = gtk_menu_get_active (GTK_MENU (ev_sidebar->priv->menu));
311         valid = gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
312            
313         while (valid) {
314                 gtk_tree_model_get (ev_sidebar->priv->page_model, &iter,
315                                     PAGE_COLUMN_MENU_ITEM, &item,
316                                     -1);
317                          
318                 if (item == menu_item) {
319                         ev_sidebar_select_page (ev_sidebar, &iter);
320                         valid = FALSE;
321                 } else {
322                         valid = gtk_tree_model_iter_next (ev_sidebar->priv->page_model, &iter);
323                 }
324                 g_object_unref (item);
325         }
326
327         g_object_notify (G_OBJECT (ev_sidebar), "current-page");
328 }
329
330 static void
331 ev_sidebar_init (EvSidebar *ev_sidebar)
332 {
333         GtkWidget *hbox;
334         GtkWidget *close_button;
335         GtkWidget *select_hbox;
336         GtkWidget *arrow;
337         GtkWidget *image;
338
339         ev_sidebar->priv = EV_SIDEBAR_GET_PRIVATE (ev_sidebar);
340
341         /* data model */
342         ev_sidebar->priv->page_model = (GtkTreeModel *)
343                         gtk_list_store_new (PAGE_COLUMN_NUM_COLS,
344                                             G_TYPE_STRING,
345                                             GTK_TYPE_WIDGET,
346                                             GTK_TYPE_WIDGET,
347                                             G_TYPE_INT);
348
349         /* top option menu */
350         hbox = gtk_hbox_new (FALSE, 0);
351         ev_sidebar->priv->hbox = hbox;
352         gtk_box_pack_start (GTK_BOX (ev_sidebar), hbox, FALSE, FALSE, 0);
353         gtk_widget_show (hbox);
354
355         ev_sidebar->priv->select_button = gtk_toggle_button_new ();
356         gtk_button_set_relief (GTK_BUTTON (ev_sidebar->priv->select_button), GTK_RELIEF_NONE);
357         g_signal_connect (ev_sidebar->priv->select_button, "button_press_event",
358                           G_CALLBACK (ev_sidebar_select_button_press_cb),
359                           ev_sidebar);
360         g_signal_connect (ev_sidebar->priv->select_button, "key_press_event",
361                           G_CALLBACK (ev_sidebar_select_button_key_press_cb),
362                           ev_sidebar);
363
364         select_hbox = gtk_hbox_new (FALSE, 0);
365
366         ev_sidebar->priv->label = gtk_label_new ("");
367         gtk_box_pack_start (GTK_BOX (select_hbox),
368                             ev_sidebar->priv->label,
369                             FALSE, FALSE, 0);
370         gtk_widget_show (ev_sidebar->priv->label);
371
372         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
373         gtk_box_pack_end (GTK_BOX (select_hbox), arrow, FALSE, FALSE, 0);
374         gtk_widget_show (arrow);
375
376         gtk_container_add (GTK_CONTAINER (ev_sidebar->priv->select_button), select_hbox);
377         gtk_widget_show (select_hbox);
378
379         gtk_box_pack_start (GTK_BOX (hbox), ev_sidebar->priv->select_button, TRUE, TRUE, 0);
380         gtk_widget_show (ev_sidebar->priv->select_button);
381
382         close_button = gtk_button_new ();
383         gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
384         g_signal_connect (close_button, "clicked",
385                           G_CALLBACK (ev_sidebar_close_clicked_cb),
386                           ev_sidebar);
387            
388         image = gtk_image_new_from_stock (GTK_STOCK_CLOSE,
389                                           GTK_ICON_SIZE_MENU);
390         gtk_container_add (GTK_CONTAINER (close_button), image);
391         gtk_widget_show (image);
392    
393         gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
394         gtk_widget_show (close_button);
395    
396         ev_sidebar->priv->menu = gtk_menu_new ();
397         g_signal_connect (ev_sidebar->priv->menu, "deactivate",
398                           G_CALLBACK (ev_sidebar_menu_deactivate_cb),
399                           ev_sidebar->priv->select_button);
400         gtk_menu_attach_to_widget (GTK_MENU (ev_sidebar->priv->menu),
401                                    GTK_WIDGET (ev_sidebar),
402                                    ev_sidebar_menu_detach_cb);
403         gtk_widget_show (ev_sidebar->priv->menu);
404
405         ev_sidebar->priv->notebook = gtk_notebook_new ();
406         gtk_notebook_set_show_border (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
407         gtk_notebook_set_show_tabs (GTK_NOTEBOOK (ev_sidebar->priv->notebook), FALSE);
408         gtk_box_pack_start (GTK_BOX (ev_sidebar), ev_sidebar->priv->notebook,
409                             TRUE, TRUE, 0);
410         gtk_widget_show (ev_sidebar->priv->notebook);
411
412         gtk_widget_set_sensitive (GTK_WIDGET (ev_sidebar->priv->notebook), FALSE);
413         gtk_widget_set_sensitive (GTK_WIDGET (ev_sidebar->priv->select_button), FALSE);
414 }
415
416 /* Public functions */
417
418 GtkWidget *
419 ev_sidebar_new (void)
420 {
421         GtkWidget *ev_sidebar;
422
423         ev_sidebar = g_object_new (EV_TYPE_SIDEBAR, NULL);
424
425         return ev_sidebar;
426 }
427
428 void
429 ev_sidebar_add_page (EvSidebar   *ev_sidebar,
430                      GtkWidget   *main_widget)
431 {
432         GtkTreeIter iter;
433         GtkWidget *menu_item;
434         gchar *label_title;
435         const gchar *title;
436         int index;
437            
438         g_return_if_fail (EV_IS_SIDEBAR (ev_sidebar));
439         g_return_if_fail (EV_IS_SIDEBAR_PAGE (main_widget));
440         g_return_if_fail (GTK_IS_WIDGET (main_widget));
441
442         ev_sidebar_page_set_model (EV_SIDEBAR_PAGE (main_widget),
443                                    ev_sidebar->priv->model);
444         title = ev_sidebar_page_get_label (EV_SIDEBAR_PAGE (main_widget));
445            
446         index = gtk_notebook_append_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
447                                           main_widget, NULL);
448            
449         menu_item = gtk_image_menu_item_new_with_label (title);
450         g_signal_connect (menu_item, "activate",
451                           G_CALLBACK (ev_sidebar_menu_item_activate_cb),
452                           ev_sidebar);
453         gtk_widget_show (menu_item);
454         gtk_menu_shell_append (GTK_MENU_SHELL (ev_sidebar->priv->menu),
455                                menu_item);
456
457         /* Insert and move to end */
458         gtk_list_store_insert_with_values (GTK_LIST_STORE (ev_sidebar->priv->page_model),
459                                            &iter, 0,
460                                            PAGE_COLUMN_TITLE, title,
461                                            PAGE_COLUMN_MENU_ITEM, menu_item,
462                                            PAGE_COLUMN_MAIN_WIDGET, main_widget,
463                                            PAGE_COLUMN_NOTEBOOK_INDEX, index,
464                                            -1);
465         gtk_list_store_move_before(GTK_LIST_STORE(ev_sidebar->priv->page_model),
466                                            &iter, NULL);
467
468
469         /* Set the first item added as active */
470         gtk_tree_model_get_iter_first (ev_sidebar->priv->page_model, &iter);
471         gtk_tree_model_get (ev_sidebar->priv->page_model,
472                             &iter,
473                             PAGE_COLUMN_TITLE, &label_title,
474                             PAGE_COLUMN_NOTEBOOK_INDEX, &index,
475                             -1);
476
477         gtk_menu_set_active (GTK_MENU (ev_sidebar->priv->menu), index);
478         gtk_label_set_text (GTK_LABEL (ev_sidebar->priv->label), label_title);
479         gtk_notebook_set_current_page (GTK_NOTEBOOK (ev_sidebar->priv->notebook),
480                                        index);
481         g_free (label_title);
482 }
483
484 static void
485 ev_sidebar_document_changed_cb (EvDocumentModel *model,
486                                 GParamSpec      *pspec,
487                                 EvSidebar       *sidebar)
488 {
489         EvSidebarPrivate *priv = sidebar->priv;
490         EvDocument *document = ev_document_model_get_document (model);
491         GtkTreeIter iter;
492         gboolean valid;
493         gboolean has_pages = FALSE;
494
495         for (valid = gtk_tree_model_get_iter_first (priv->page_model, &iter);
496              valid;
497              valid = gtk_tree_model_iter_next (priv->page_model, &iter)) {
498                 GtkWidget *widget;
499                 GtkWidget *menu_widget;
500
501                 gtk_tree_model_get (priv->page_model, &iter,
502                                     PAGE_COLUMN_MAIN_WIDGET, &widget,
503                                     PAGE_COLUMN_MENU_ITEM, &menu_widget,
504                                     -1);
505
506                 if (ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (widget), document)) {
507                         has_pages = TRUE;
508                 } else {
509                         gtk_widget_set_sensitive (menu_widget, FALSE);
510                 }
511                 g_object_unref (widget);
512                 g_object_unref (menu_widget);
513         }
514
515         if (!has_pages) {
516                 gtk_widget_hide (GTK_WIDGET (sidebar));
517         } else {
518                 gtk_widget_set_sensitive (GTK_WIDGET (sidebar->priv->notebook), TRUE);
519                 gtk_widget_set_sensitive (GTK_WIDGET (sidebar->priv->select_button), TRUE);
520         }
521 }
522
523 void
524 ev_sidebar_set_model (EvSidebar       *sidebar,
525                       EvDocumentModel *model)
526 {
527         g_return_if_fail (EV_IS_SIDEBAR (sidebar));
528         g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
529
530         if (model == sidebar->priv->model)
531                 return;
532
533         sidebar->priv->model = model;
534         g_signal_connect (model, "notify::document",
535                           G_CALLBACK (ev_sidebar_document_changed_cb),
536                           sidebar);
537 }