1 /* ev-sidebar-bookmarks.c
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2010 Carlos Garcia Campos  <carlosgc@gnome.org>
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #include "config.h"
22 
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
25 
26 #include "ev-sidebar-bookmarks.h"
27 
28 #include "ev-document.h"
29 #include "ev-document-misc.h"
30 #include "ev-sidebar-page.h"
31 #include "ev-utils.h"
32 
33 enum {
34         PROP_0,
35         PROP_WIDGET
36 };
37 
38 enum {
39         COLUMN_MARKUP,
40         COLUMN_PAGE,
41         N_COLUMNS
42 };
43 
44 enum {
45         ADD_BOOKMARK,
46         N_SIGNALS
47 };
48 
49 struct _EvSidebarBookmarksPrivate {
50         EvDocumentModel *model;
51         EvBookmarks     *bookmarks;
52 
53         GtkWidget       *tree_view;
54         GtkWidget       *del_button;
55         GtkWidget       *add_button;
56 
57         /* Popup menu */
58         GtkWidget       *popup;
59         GtkUIManager    *ui_manager;
60         GtkActionGroup  *action_group;
61 };
62 
63 static void ev_sidebar_bookmarks_page_iface_init (EvSidebarPageInterface *iface);
64 
65 G_DEFINE_TYPE_EXTENDED (EvSidebarBookmarks,
66                         ev_sidebar_bookmarks,
67                         GTK_TYPE_BOX,
68                         0,
69                         G_ADD_PRIVATE (EvSidebarBookmarks)
70                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
71                                                ev_sidebar_bookmarks_page_iface_init))
72 
73 static guint signals[N_SIGNALS];
74 
75 static const gchar popup_menu_ui[] =
76         "<popup name=\"BookmarksPopup\" action=\"BookmarksPopupAction\">\n"
77         "  <menuitem name=\"OpenBookmark\" action=\"OpenBookmark\"/>\n"
78         "  <separator/>\n"
79         "  <menuitem name=\"RenameBookmark\" action=\"RenameBookmark\"/>\n"
80         "  <menuitem name=\"RemoveBookmark\" action=\"RemoveBookmark\"/>\n"
81         "</popup>\n";
82 
83 static gint
ev_sidebar_bookmarks_get_selected_page(EvSidebarBookmarks * sidebar_bookmarks,GtkTreeSelection * selection)84 ev_sidebar_bookmarks_get_selected_page (EvSidebarBookmarks *sidebar_bookmarks,
85                                         GtkTreeSelection   *selection)
86 {
87         GtkTreeModel *model;
88         GtkTreeIter   iter;
89 
90         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
91                 guint page;
92 
93                 gtk_tree_model_get (model, &iter,
94                                     COLUMN_PAGE, &page,
95                                     -1);
96                 return page;
97         }
98 
99         return -1;
100 }
101 
102 static void
ev_bookmarks_popup_cmd_open_bookmark(GtkAction * action,EvSidebarBookmarks * sidebar_bookmarks)103 ev_bookmarks_popup_cmd_open_bookmark (GtkAction          *action,
104                                       EvSidebarBookmarks *sidebar_bookmarks)
105 {
106         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
107         GtkTreeSelection          *selection;
108         gint                       page;
109 
110         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
111         page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
112         ev_document_model_set_page (priv->model, page);
113 }
114 
115 static void
ev_bookmarks_popup_cmd_rename_bookmark(GtkAction * action,EvSidebarBookmarks * sidebar_bookmarks)116 ev_bookmarks_popup_cmd_rename_bookmark (GtkAction          *action,
117                                         EvSidebarBookmarks *sidebar_bookmarks)
118 {
119         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
120         GtkTreeView               *tree_view = GTK_TREE_VIEW (priv->tree_view);
121         GtkTreeSelection          *selection;
122         GtkTreeModel              *model;
123         GtkTreeIter                iter;
124 
125 
126         selection = gtk_tree_view_get_selection (tree_view);
127         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
128                 GtkTreePath *path;
129 
130                 path = gtk_tree_model_get_path (model, &iter);
131                 gtk_tree_view_set_cursor (tree_view, path,
132                                           gtk_tree_view_get_column (tree_view, 0),
133                                           TRUE);
134                 gtk_tree_path_free (path);
135         }
136 }
137 
138 static void
ev_bookmarks_popup_cmd_remove_bookmark(GtkAction * action,EvSidebarBookmarks * sidebar_bookmarks)139 ev_bookmarks_popup_cmd_remove_bookmark (GtkAction          *action,
140                                         EvSidebarBookmarks *sidebar_bookmarks)
141 {
142         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
143         GtkTreeSelection          *selection;
144         gint                       page;
145         EvBookmark                 bm;
146 
147         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
148         page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
149         bm.page = page;
150         bm.title = NULL;
151         ev_bookmarks_delete (priv->bookmarks, &bm);
152 }
153 
154 static const GtkActionEntry popup_entries[] = {
155         { "OpenBookmark", "document-open", N_("_Open Bookmark"), NULL,
156           NULL, G_CALLBACK (ev_bookmarks_popup_cmd_open_bookmark) },
157         { "RenameBookmark", NULL, N_("_Rename Bookmark"), NULL,
158           NULL, G_CALLBACK (ev_bookmarks_popup_cmd_rename_bookmark) },
159         { "RemoveBookmark", NULL, N_("_Remove Bookmark"), NULL,
160           NULL, G_CALLBACK (ev_bookmarks_popup_cmd_remove_bookmark) }
161 };
162 
163 static gint
compare_bookmarks(EvBookmark * a,EvBookmark * b)164 compare_bookmarks (EvBookmark *a,
165                    EvBookmark *b)
166 {
167         if (a->page < b->page)
168                 return -1;
169         if (a->page > b->page)
170                 return 1;
171         return 0;
172 }
173 
174 static void
ev_sidebar_bookmarks_update(EvSidebarBookmarks * sidebar_bookmarks)175 ev_sidebar_bookmarks_update (EvSidebarBookmarks *sidebar_bookmarks)
176 {
177         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
178         GtkListStore              *model;
179         GList                     *items, *l;
180         GtkTreeIter                iter;
181 
182         model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view)));
183         gtk_list_store_clear (model);
184 
185         if (!priv->bookmarks) {
186                 g_object_set (priv->tree_view, "has-tooltip", FALSE, NULL);
187                 return;
188         }
189 
190         items = ev_bookmarks_get_bookmarks (priv->bookmarks);
191         items = g_list_sort (items, (GCompareFunc)compare_bookmarks);
192         for (l = items; l; l = g_list_next (l)) {
193                 EvBookmark *bm = (EvBookmark *)l->data;
194 
195                 gtk_list_store_append (model, &iter);
196                 gtk_list_store_set (model, &iter,
197                                     COLUMN_MARKUP, bm->title,
198                                     COLUMN_PAGE, bm->page,
199                                     -1);
200         }
201         g_list_free (items);
202         g_object_set (priv->tree_view, "has-tooltip", TRUE, NULL);
203 }
204 
205 static void
ev_sidebar_bookmarks_changed(EvBookmarks * bookmarks,EvSidebarBookmarks * sidebar_bookmarks)206 ev_sidebar_bookmarks_changed (EvBookmarks        *bookmarks,
207                               EvSidebarBookmarks *sidebar_bookmarks)
208 {
209         ev_sidebar_bookmarks_update (sidebar_bookmarks);
210 }
211 
212 static void
ev_sidebar_bookmarks_selection_changed(GtkTreeSelection * selection,EvSidebarBookmarks * sidebar_bookmarks)213 ev_sidebar_bookmarks_selection_changed (GtkTreeSelection   *selection,
214                                         EvSidebarBookmarks *sidebar_bookmarks)
215 {
216         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
217         gint                       page;
218 
219         page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
220         if (page >= 0) {
221                 ev_document_model_set_page (priv->model, page);
222                 gtk_widget_set_sensitive (priv->del_button, TRUE);
223         } else {
224                 gtk_widget_set_sensitive (priv->del_button, FALSE);
225         }
226 }
227 
228 static void
ev_sidebar_bookmarks_add_clicked(GtkWidget * button,EvSidebarBookmarks * sidebar_bookmarks)229 ev_sidebar_bookmarks_add_clicked (GtkWidget          *button,
230                                   EvSidebarBookmarks *sidebar_bookmarks)
231 {
232         /* Let the window add the bookmark since
233          * since we don't know the page title
234          */
235         g_signal_emit (sidebar_bookmarks, signals[ADD_BOOKMARK], 0);
236 }
237 
238 static void
ev_sidebar_bookmarks_del_clicked(GtkWidget * button,EvSidebarBookmarks * sidebar_bookmarks)239 ev_sidebar_bookmarks_del_clicked (GtkWidget          *button,
240                                   EvSidebarBookmarks *sidebar_bookmarks)
241 {
242         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
243         GtkTreeSelection          *selection;
244         gint                       page;
245         EvBookmark                 bm;
246 
247         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
248         page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
249         if (page < 0)
250                 return;
251 
252         bm.page = page;
253         bm.title = NULL;
254         ev_bookmarks_delete (priv->bookmarks, &bm);
255 }
256 
257 static void
ev_sidebar_bookmarks_bookmark_renamed(GtkCellRendererText * renderer,const gchar * path_string,const gchar * new_text,EvSidebarBookmarks * sidebar_bookmarks)258 ev_sidebar_bookmarks_bookmark_renamed (GtkCellRendererText *renderer,
259                                        const gchar         *path_string,
260                                        const gchar         *new_text,
261                                        EvSidebarBookmarks  *sidebar_bookmarks)
262 {
263         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
264         GtkTreePath               *path = gtk_tree_path_new_from_string (path_string);
265         GtkTreeModel              *model;
266         GtkTreeIter                iter;
267         guint                      page;
268         EvBookmark                 bm;
269 
270         if (!new_text || new_text[0] == '\0')
271                 return;
272 
273         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view));
274         gtk_tree_model_get_iter (model, &iter, path);
275         gtk_tree_model_get (model, &iter,
276                             COLUMN_PAGE, &page,
277                             -1);
278         gtk_tree_path_free (path);
279 
280         bm.page = page;
281         bm.title = g_strdup (new_text);
282         ev_bookmarks_update (priv->bookmarks, &bm);
283 }
284 
285 static gboolean
ev_sidebar_bookmarks_query_tooltip(GtkWidget * widget,gint x,gint y,gboolean keyboard_tip,GtkTooltip * tooltip,EvSidebarBookmarks * sidebar_bookmarks)286 ev_sidebar_bookmarks_query_tooltip (GtkWidget          *widget,
287                                     gint                x,
288                                     gint                y,
289                                     gboolean            keyboard_tip,
290                                     GtkTooltip         *tooltip,
291                                     EvSidebarBookmarks *sidebar_bookmarks)
292 {
293         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
294         GtkTreeModel              *model;
295         GtkTreeIter                iter;
296         GtkTreePath               *path = NULL;
297         EvDocument                *document;
298         guint                      page;
299         gchar                     *page_label;
300         gchar                     *text;
301 
302         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view));
303         if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (priv->tree_view),
304                                                 &x, &y, keyboard_tip,
305                                                 &model, &path, &iter))
306                 return FALSE;
307 
308         gtk_tree_model_get (model, &iter,
309                             COLUMN_PAGE, &page,
310                             -1);
311 
312         document = ev_document_model_get_document (priv->model);
313         page_label = ev_document_get_page_label (document, page);
314         text = g_strdup_printf (_("Page %s"), page_label);
315         gtk_tooltip_set_text (tooltip, text);
316         g_free (text);
317         g_free (page_label);
318 
319         gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (priv->tree_view),
320                                        tooltip, path);
321         gtk_tree_path_free (path);
322 
323         return TRUE;
324 }
325 
326 static gboolean
ev_sidebar_bookmarks_popup_menu_show(EvSidebarBookmarks * sidebar_bookmarks,gint x,gint y,gboolean keyboard_mode)327 ev_sidebar_bookmarks_popup_menu_show (EvSidebarBookmarks *sidebar_bookmarks,
328                                       gint                x,
329                                       gint                y,
330                                       gboolean            keyboard_mode)
331 {
332         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
333         GtkTreeView               *tree_view = GTK_TREE_VIEW (sidebar_bookmarks->priv->tree_view);
334         GtkTreeSelection          *selection = gtk_tree_view_get_selection (tree_view);
335 
336         if (keyboard_mode) {
337                 if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
338                         return FALSE;
339         } else {
340                 GtkTreePath *path;
341 
342                 if (!gtk_tree_view_get_path_at_pos (tree_view, x, y, &path, NULL, NULL, NULL))
343                         return FALSE;
344 
345                 g_signal_handlers_block_by_func (selection,
346                                                  ev_sidebar_bookmarks_selection_changed,
347                                                  sidebar_bookmarks);
348                 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
349                 g_signal_handlers_unblock_by_func (selection,
350                                                    ev_sidebar_bookmarks_selection_changed,
351                                                    sidebar_bookmarks);
352                 gtk_tree_path_free (path);
353         }
354 
355         if (!priv->popup)
356                 priv->popup = gtk_ui_manager_get_widget (priv->ui_manager, "/BookmarksPopup");
357 
358         gtk_menu_popup_at_pointer (GTK_MENU (priv->popup), NULL);
359         return TRUE;
360 }
361 
362 static gboolean
ev_sidebar_bookmarks_button_press(GtkWidget * widget,GdkEventButton * event,EvSidebarBookmarks * sidebar_bookmarks)363 ev_sidebar_bookmarks_button_press (GtkWidget          *widget,
364                                    GdkEventButton     *event,
365                                    EvSidebarBookmarks *sidebar_bookmarks)
366 {
367         if (event->button != 3)
368                 return FALSE;
369 
370         return ev_sidebar_bookmarks_popup_menu_show (sidebar_bookmarks, event->x, event->y, FALSE);
371 }
372 
373 static gboolean
ev_sidebar_bookmarks_popup_menu(GtkWidget * widget)374 ev_sidebar_bookmarks_popup_menu (GtkWidget *widget)
375 {
376         EvSidebarBookmarks *sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (widget);
377         gint                x, y;
378 
379         ev_document_misc_get_pointer_position (widget, &x, &y);
380         return ev_sidebar_bookmarks_popup_menu_show (sidebar_bookmarks, x, y, TRUE);
381 }
382 
383 static void
ev_sidebar_bookmarks_dispose(GObject * object)384 ev_sidebar_bookmarks_dispose (GObject *object)
385 {
386         EvSidebarBookmarks *sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (object);
387         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
388 
389         if (priv->model) {
390                 g_object_unref (priv->model);
391                 priv->model = NULL;
392         }
393 
394         if (priv->bookmarks) {
395                 g_object_unref (priv->bookmarks);
396                 priv->bookmarks = NULL;
397         }
398 
399         if (priv->action_group) {
400                 g_object_unref (priv->action_group);
401                 priv->action_group = NULL;
402         }
403 
404         if (priv->ui_manager) {
405                 g_object_unref (priv->ui_manager);
406                 priv->ui_manager = NULL;
407         }
408 
409         G_OBJECT_CLASS (ev_sidebar_bookmarks_parent_class)->dispose (object);
410 }
411 
412 static void
ev_sidebar_bookmarks_init(EvSidebarBookmarks * sidebar_bookmarks)413 ev_sidebar_bookmarks_init (EvSidebarBookmarks *sidebar_bookmarks)
414 {
415         EvSidebarBookmarksPrivate *priv;
416         GtkWidget                 *swindow;
417         GtkWidget                 *hbox;
418         GtkListStore              *model;
419         GtkCellRenderer           *renderer;
420         GtkTreeSelection          *selection;
421 
422         sidebar_bookmarks->priv = ev_sidebar_bookmarks_get_instance_private (sidebar_bookmarks);
423         priv = sidebar_bookmarks->priv;
424 
425         gtk_orientable_set_orientation (GTK_ORIENTABLE (sidebar_bookmarks), GTK_ORIENTATION_VERTICAL);
426         gtk_box_set_spacing (GTK_BOX (sidebar_bookmarks), 6);
427 
428         swindow = gtk_scrolled_window_new (NULL, NULL);
429         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
430                                         GTK_POLICY_AUTOMATIC,
431                                         GTK_POLICY_AUTOMATIC);
432         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
433                                              GTK_SHADOW_IN);
434         gtk_box_pack_start (GTK_BOX (sidebar_bookmarks), swindow, TRUE, TRUE, 0);
435         gtk_widget_show (swindow);
436 
437         model = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_UINT);
438         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
439         g_object_unref (model);
440         g_signal_connect (priv->tree_view, "query-tooltip",
441                           G_CALLBACK (ev_sidebar_bookmarks_query_tooltip),
442                           sidebar_bookmarks);
443         g_signal_connect (priv->tree_view,
444                           "button-press-event",
445                           G_CALLBACK (ev_sidebar_bookmarks_button_press),
446                           sidebar_bookmarks);
447         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
448         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
449         g_signal_connect (selection, "changed",
450                           G_CALLBACK (ev_sidebar_bookmarks_selection_changed),
451                           sidebar_bookmarks);
452 
453         renderer = gtk_cell_renderer_text_new ();
454         g_object_set (renderer,
455                       "ellipsize", PANGO_ELLIPSIZE_END,
456                       "editable", TRUE,
457                       NULL);
458         g_signal_connect (renderer, "edited",
459                           G_CALLBACK (ev_sidebar_bookmarks_bookmark_renamed),
460                           sidebar_bookmarks);
461         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view),
462                                                      0, NULL, renderer,
463                                                      "markup", COLUMN_MARKUP,
464                                                      NULL);
465         gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
466         gtk_widget_show (priv->tree_view);
467 
468         hbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
469 
470         priv->add_button = gtk_button_new_with_mnemonic (_("_Add"));
471         gtk_button_set_image (GTK_BUTTON (priv->add_button), gtk_image_new_from_icon_name ("list-add", GTK_ICON_SIZE_BUTTON));
472 
473         g_signal_connect (priv->add_button, "clicked",
474                           G_CALLBACK (ev_sidebar_bookmarks_add_clicked),
475                           sidebar_bookmarks);
476         gtk_widget_set_sensitive (priv->add_button, FALSE);
477         gtk_box_pack_start (GTK_BOX (hbox), priv->add_button, TRUE, TRUE, 6);
478         gtk_widget_show (priv->add_button);
479 
480         priv->del_button = gtk_button_new_with_mnemonic (_("_Remove"));
481         gtk_button_set_image (GTK_BUTTON (priv->del_button), gtk_image_new_from_icon_name ("list-remove", GTK_ICON_SIZE_BUTTON));
482 
483         g_signal_connect (priv->del_button, "clicked",
484                           G_CALLBACK (ev_sidebar_bookmarks_del_clicked),
485                           sidebar_bookmarks);
486         gtk_widget_set_sensitive (priv->del_button, FALSE);
487         gtk_box_pack_start (GTK_BOX (hbox), priv->del_button, TRUE, TRUE, 6);
488         gtk_widget_show (priv->del_button);
489 
490         gtk_box_pack_end (GTK_BOX (sidebar_bookmarks), hbox, FALSE, TRUE, 0);
491         gtk_widget_show (hbox);
492         gtk_widget_show (GTK_WIDGET (sidebar_bookmarks));
493 
494         /* Popup menu */
495         G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
496         priv->action_group = gtk_action_group_new ("BookmarsPopupActions");
497         gtk_action_group_set_translation_domain (priv->action_group, NULL);
498         gtk_action_group_add_actions (priv->action_group, popup_entries,
499                                       G_N_ELEMENTS (popup_entries),
500                                       sidebar_bookmarks);
501         G_GNUC_END_IGNORE_DEPRECATIONS;
502         priv->ui_manager = gtk_ui_manager_new ();
503         gtk_ui_manager_insert_action_group (priv->ui_manager,
504                                             priv->action_group, 0);
505         gtk_ui_manager_add_ui_from_string (priv->ui_manager, popup_menu_ui, -1, NULL);
506 }
507 
508 static void
ev_sidebar_bookmarks_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)509 ev_sidebar_bookmarks_get_property (GObject    *object,
510                                    guint       prop_id,
511                                    GValue     *value,
512                                    GParamSpec *pspec)
513 {
514         EvSidebarBookmarks *sidebar_bookmarks;
515 
516         sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (object);
517 
518         switch (prop_id) {
519         case PROP_WIDGET:
520                 g_value_set_object (value, sidebar_bookmarks->priv->tree_view);
521                 break;
522         default:
523                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
524                 break;
525         }
526 }
527 
528 static void
ev_sidebar_bookmarks_class_init(EvSidebarBookmarksClass * klass)529 ev_sidebar_bookmarks_class_init (EvSidebarBookmarksClass *klass)
530 {
531         GObjectClass   *g_object_class = G_OBJECT_CLASS (klass);
532         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
533 
534         g_object_class->get_property = ev_sidebar_bookmarks_get_property;
535         g_object_class->dispose = ev_sidebar_bookmarks_dispose;
536 
537         widget_class->popup_menu = ev_sidebar_bookmarks_popup_menu;
538 
539         g_object_class_override_property (g_object_class, PROP_WIDGET, "main-widget");
540 
541         signals[ADD_BOOKMARK] =
542                 g_signal_new ("add-bookmark",
543                               G_TYPE_FROM_CLASS (g_object_class),
544                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
545                               G_STRUCT_OFFSET (EvSidebarBookmarksClass, add_bookmark),
546                               NULL, NULL,
547                               g_cclosure_marshal_VOID__VOID,
548                               G_TYPE_NONE, 0,
549                               G_TYPE_NONE);
550 }
551 
552 GtkWidget *
ev_sidebar_bookmarks_new(void)553 ev_sidebar_bookmarks_new (void)
554 {
555         return GTK_WIDGET (g_object_new (EV_TYPE_SIDEBAR_BOOKMARKS, NULL));
556 }
557 
558 void
ev_sidebar_bookmarks_set_bookmarks(EvSidebarBookmarks * sidebar_bookmarks,EvBookmarks * bookmarks)559 ev_sidebar_bookmarks_set_bookmarks (EvSidebarBookmarks *sidebar_bookmarks,
560                                     EvBookmarks        *bookmarks)
561 {
562         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
563 
564         g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));
565 
566         if (priv->bookmarks == bookmarks)
567                 return;
568 
569         if (priv->bookmarks)
570                 g_object_unref (priv->bookmarks);
571         priv->bookmarks = g_object_ref (bookmarks);
572         g_signal_connect (priv->bookmarks, "changed",
573                           G_CALLBACK (ev_sidebar_bookmarks_changed),
574                           sidebar_bookmarks);
575 
576         gtk_widget_set_sensitive (priv->add_button, TRUE);
577         ev_sidebar_bookmarks_update (sidebar_bookmarks);
578 }
579 
580 /* EvSidebarPageIface */
581 static void
ev_sidebar_bookmarks_set_model(EvSidebarPage * sidebar_page,EvDocumentModel * model)582 ev_sidebar_bookmarks_set_model (EvSidebarPage   *sidebar_page,
583                                 EvDocumentModel *model)
584 {
585         EvSidebarBookmarks *sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (sidebar_page);
586         EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
587 
588         if (priv->model == model)
589                 return;
590 
591         if (priv->model)
592                 g_object_unref (priv->model);
593         priv->model = g_object_ref (model);
594 }
595 
596 static gboolean
ev_sidebar_bookmarks_support_document(EvSidebarPage * sidebar_page,EvDocument * document)597 ev_sidebar_bookmarks_support_document (EvSidebarPage *sidebar_page,
598                                        EvDocument    *document)
599 {
600         return TRUE;
601 }
602 
603 static const gchar *
ev_sidebar_bookmarks_get_label(EvSidebarPage * sidebar_page)604 ev_sidebar_bookmarks_get_label (EvSidebarPage *sidebar_page)
605 {
606         return _("Bookmarks");
607 }
608 
609 static void
ev_sidebar_bookmarks_page_iface_init(EvSidebarPageInterface * iface)610 ev_sidebar_bookmarks_page_iface_init (EvSidebarPageInterface *iface)
611 {
612         iface->support_document = ev_sidebar_bookmarks_support_document;
613         iface->set_model = ev_sidebar_bookmarks_set_model;
614         iface->get_label = ev_sidebar_bookmarks_get_label;
615 }
616