1 /* ev-sidebar-bookmarks.c
2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
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.
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.
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.
23 #include <glib/gi18n.h>
25 #include "ev-sidebar-bookmarks.h"
27 #include "ev-document.h"
28 #include "ev-sidebar-page.h"
47 struct _EvSidebarBookmarksPrivate {
48 EvDocumentModel *model;
49 EvBookmarks *bookmarks;
52 GtkWidget *del_button;
53 GtkWidget *add_button;
57 GtkUIManager *ui_manager;
58 GtkActionGroup *action_group;
61 static void ev_sidebar_bookmarks_page_iface_init (EvSidebarPageInterface *iface);
63 G_DEFINE_TYPE_EXTENDED (EvSidebarBookmarks,
67 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
68 ev_sidebar_bookmarks_page_iface_init))
70 static guint signals[N_SIGNALS];
72 static const gchar popup_menu_ui[] =
73 "<popup name=\"BookmarksPopup\" action=\"BookmarksPopupAction\">\n"
74 " <menuitem name=\"OpenBookmark\" action=\"OpenBookmark\"/>\n"
76 " <menuitem name=\"RenameBookmark\" action=\"RenameBookmark\"/>\n"
77 " <menuitem name=\"RemoveBookmark\" action=\"RemoveBookmark\"/>\n"
81 ev_sidebar_bookmarks_get_selected_page (EvSidebarBookmarks *sidebar_bookmarks,
82 GtkTreeSelection *selection)
87 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
90 gtk_tree_model_get (model, &iter,
100 ev_bookmarks_popup_cmd_open_bookmark (GtkAction *action,
101 EvSidebarBookmarks *sidebar_bookmarks)
103 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
104 GtkTreeSelection *selection;
107 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
108 page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
109 ev_document_model_set_page (priv->model, page);
113 ev_bookmarks_popup_cmd_rename_bookmark (GtkAction *action,
114 EvSidebarBookmarks *sidebar_bookmarks)
116 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
117 GtkTreeView *tree_view = GTK_TREE_VIEW (priv->tree_view);
118 GtkTreeSelection *selection;
123 selection = gtk_tree_view_get_selection (tree_view);
124 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
127 path = gtk_tree_model_get_path (model, &iter);
128 gtk_tree_view_set_cursor (tree_view, path,
129 gtk_tree_view_get_column (tree_view, 0),
131 gtk_tree_path_free (path);
136 ev_bookmarks_popup_cmd_remove_bookmark (GtkAction *action,
137 EvSidebarBookmarks *sidebar_bookmarks)
139 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
140 GtkTreeSelection *selection;
144 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
145 page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
148 ev_bookmarks_delete (priv->bookmarks, &bm);
151 static const GtkActionEntry popup_entries[] = {
152 { "OpenBookmark", GTK_STOCK_OPEN, N_("_Open Bookmark"), NULL,
153 NULL, G_CALLBACK (ev_bookmarks_popup_cmd_open_bookmark) },
154 { "RenameBookmark", NULL, N_("_Rename Bookmark"), NULL,
155 NULL, G_CALLBACK (ev_bookmarks_popup_cmd_rename_bookmark) },
156 { "RemoveBookmark", NULL, N_("_Remove Bookmark"), NULL,
157 NULL, G_CALLBACK (ev_bookmarks_popup_cmd_remove_bookmark) }
161 compare_bookmarks (EvBookmark *a,
164 if (a->page < b->page)
166 if (a->page > b->page)
172 ev_sidebar_bookmarks_update (EvSidebarBookmarks *sidebar_bookmarks)
174 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
179 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view)));
180 gtk_list_store_clear (model);
182 if (!priv->bookmarks) {
183 g_object_set (priv->tree_view, "has-tooltip", FALSE, NULL);
187 items = ev_bookmarks_get_bookmarks (priv->bookmarks);
188 items = g_list_sort (items, (GCompareFunc)compare_bookmarks);
189 for (l = items; l; l = g_list_next (l)) {
190 EvBookmark *bm = (EvBookmark *)l->data;
192 gtk_list_store_append (model, &iter);
193 gtk_list_store_set (model, &iter,
194 COLUMN_MARKUP, bm->title,
195 COLUMN_PAGE, bm->page,
199 g_object_set (priv->tree_view, "has-tooltip", TRUE, NULL);
203 ev_sidebar_bookmarks_changed (EvBookmarks *bookmarks,
204 EvSidebarBookmarks *sidebar_bookmarks)
206 ev_sidebar_bookmarks_update (sidebar_bookmarks);
210 ev_sidebar_bookmarks_selection_changed (GtkTreeSelection *selection,
211 EvSidebarBookmarks *sidebar_bookmarks)
213 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
216 page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
218 ev_document_model_set_page (priv->model, page);
219 gtk_widget_set_sensitive (priv->del_button, TRUE);
221 gtk_widget_set_sensitive (priv->del_button, FALSE);
226 ev_sidebar_bookmarks_add_clicked (GtkWidget *button,
227 EvSidebarBookmarks *sidebar_bookmarks)
229 /* Let the window add the bookmark since
230 * since we don't know the page title
232 g_signal_emit (sidebar_bookmarks, signals[ADD_BOOKMARK], 0);
236 ev_sidebar_bookmarks_del_clicked (GtkWidget *button,
237 EvSidebarBookmarks *sidebar_bookmarks)
239 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
240 GtkTreeSelection *selection;
244 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
245 page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
251 ev_bookmarks_delete (priv->bookmarks, &bm);
255 ev_sidebar_bookmarks_bookmark_renamed (GtkCellRendererText *renderer,
256 const gchar *path_string,
257 const gchar *new_text,
258 EvSidebarBookmarks *sidebar_bookmarks)
260 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
261 GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
267 if (!new_text || new_text[0] == '\0')
270 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view));
271 gtk_tree_model_get_iter (model, &iter, path);
272 gtk_tree_model_get (model, &iter,
275 gtk_tree_path_free (path);
278 bm.title = g_strdup (new_text);
279 ev_bookmarks_update (priv->bookmarks, &bm);
283 ev_sidebar_bookmarks_query_tooltip (GtkWidget *widget,
286 gboolean keyboard_tip,
288 EvSidebarBookmarks *sidebar_bookmarks)
290 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
293 GtkTreePath *path = NULL;
294 EvDocument *document;
299 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view));
300 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (priv->tree_view),
301 &x, &y, keyboard_tip,
302 &model, &path, &iter))
305 gtk_tree_model_get (model, &iter,
309 document = ev_document_model_get_document (priv->model);
310 page_label = ev_document_get_page_label (document, page);
311 text = g_strdup_printf (_("Page %s"), page_label);
312 gtk_tooltip_set_text (tooltip, text);
316 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (priv->tree_view),
318 gtk_tree_path_free (path);
324 ev_sidebar_bookmarks_popup_menu_show (EvSidebarBookmarks *sidebar_bookmarks,
327 gboolean keyboard_mode)
329 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
330 GtkTreeView *tree_view = GTK_TREE_VIEW (sidebar_bookmarks->priv->tree_view);
331 GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
334 if (!gtk_tree_selection_get_selected (selection, NULL, NULL))
339 if (!gtk_tree_view_get_path_at_pos (tree_view, x, y, &path, NULL, NULL, NULL))
342 g_signal_handlers_block_by_func (selection,
343 ev_sidebar_bookmarks_selection_changed,
345 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
346 g_signal_handlers_unblock_by_func (selection,
347 ev_sidebar_bookmarks_selection_changed,
349 gtk_tree_path_free (path);
353 priv->popup = gtk_ui_manager_get_widget (priv->ui_manager, "/BookmarksPopup");
355 gtk_menu_popup (GTK_MENU (priv->popup),
357 keyboard_mode ? ev_gui_menu_position_tree_selection : NULL,
358 keyboard_mode ? tree_view : NULL,
359 keyboard_mode ? 0 : 3,
360 gtk_get_current_event_time ());
365 ev_sidebar_bookmarks_button_press (GtkWidget *widget,
366 GdkEventButton *event,
367 EvSidebarBookmarks *sidebar_bookmarks)
369 if (event->button != 3)
372 return ev_sidebar_bookmarks_popup_menu_show (sidebar_bookmarks, event->x, event->y, FALSE);
376 ev_sidebar_bookmarks_popup_menu (GtkWidget *widget)
378 EvSidebarBookmarks *sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (widget);
381 gtk_widget_get_pointer (widget, &x, &y);
382 return ev_sidebar_bookmarks_popup_menu_show (sidebar_bookmarks, x, y, TRUE);
386 ev_sidebar_bookmarks_dispose (GObject *object)
388 EvSidebarBookmarks *sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (object);
389 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
392 g_object_unref (priv->model);
396 if (priv->bookmarks) {
397 g_object_unref (priv->bookmarks);
398 priv->bookmarks = NULL;
401 if (priv->action_group) {
402 g_object_unref (priv->action_group);
403 priv->action_group = NULL;
406 if (priv->ui_manager) {
407 g_object_unref (priv->ui_manager);
408 priv->ui_manager = NULL;
411 G_OBJECT_CLASS (ev_sidebar_bookmarks_parent_class)->dispose (object);
415 ev_sidebar_bookmarks_init (EvSidebarBookmarks *sidebar_bookmarks)
417 EvSidebarBookmarksPrivate *priv;
421 GtkCellRenderer *renderer;
422 GtkTreeSelection *selection;
424 sidebar_bookmarks->priv = G_TYPE_INSTANCE_GET_PRIVATE (sidebar_bookmarks,
425 EV_TYPE_SIDEBAR_BOOKMARKS,
426 EvSidebarBookmarksPrivate);
427 priv = sidebar_bookmarks->priv;
429 gtk_box_set_spacing (GTK_BOX (sidebar_bookmarks), 6);
431 swindow = gtk_scrolled_window_new (NULL, NULL);
432 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
433 GTK_POLICY_AUTOMATIC,
434 GTK_POLICY_AUTOMATIC);
435 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
437 gtk_box_pack_start (GTK_BOX (sidebar_bookmarks), swindow, TRUE, TRUE, 0);
438 gtk_widget_show (swindow);
440 model = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_UINT);
441 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
442 g_object_unref (model);
443 g_signal_connect (priv->tree_view, "query-tooltip",
444 G_CALLBACK (ev_sidebar_bookmarks_query_tooltip),
446 g_signal_connect (priv->tree_view,
447 "button-press-event",
448 G_CALLBACK (ev_sidebar_bookmarks_button_press),
450 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
451 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
452 g_signal_connect (selection, "changed",
453 G_CALLBACK (ev_sidebar_bookmarks_selection_changed),
456 renderer = gtk_cell_renderer_text_new ();
457 g_object_set (renderer,
458 "ellipsize", PANGO_ELLIPSIZE_END,
461 g_signal_connect (renderer, "edited",
462 G_CALLBACK (ev_sidebar_bookmarks_bookmark_renamed),
464 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view),
466 "markup", COLUMN_MARKUP,
468 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
469 gtk_widget_show (priv->tree_view);
471 hbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
473 priv->add_button = gtk_button_new_from_stock (GTK_STOCK_ADD);
474 g_signal_connect (priv->add_button, "clicked",
475 G_CALLBACK (ev_sidebar_bookmarks_add_clicked),
477 gtk_widget_set_sensitive (priv->add_button, FALSE);
478 gtk_box_pack_start (GTK_BOX (hbox), priv->add_button, TRUE, TRUE, 6);
479 gtk_widget_show (priv->add_button);
481 priv->del_button = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
482 g_signal_connect (priv->del_button, "clicked",
483 G_CALLBACK (ev_sidebar_bookmarks_del_clicked),
485 gtk_widget_set_sensitive (priv->del_button, FALSE);
486 gtk_box_pack_start (GTK_BOX (hbox), priv->del_button, TRUE, TRUE, 6);
487 gtk_widget_show (priv->del_button);
489 gtk_box_pack_end (GTK_BOX (sidebar_bookmarks), hbox, FALSE, TRUE, 0);
490 gtk_widget_show (hbox);
491 gtk_widget_show (GTK_WIDGET (sidebar_bookmarks));
494 priv->action_group = gtk_action_group_new ("BookmarsPopupActions");
495 gtk_action_group_set_translation_domain (priv->action_group, NULL);
496 gtk_action_group_add_actions (priv->action_group, popup_entries,
497 G_N_ELEMENTS (popup_entries),
499 priv->ui_manager = gtk_ui_manager_new ();
500 gtk_ui_manager_insert_action_group (priv->ui_manager,
501 priv->action_group, 0);
502 gtk_ui_manager_add_ui_from_string (priv->ui_manager, popup_menu_ui, -1, NULL);
506 ev_sidebar_bookmarks_get_property (GObject *object,
511 EvSidebarBookmarks *sidebar_bookmarks;
513 sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (object);
517 g_value_set_object (value, sidebar_bookmarks->priv->tree_view);
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
526 ev_sidebar_bookmarks_class_init (EvSidebarBookmarksClass *klass)
528 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
529 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
531 g_object_class->get_property = ev_sidebar_bookmarks_get_property;
532 g_object_class->dispose = ev_sidebar_bookmarks_dispose;
534 widget_class->popup_menu = ev_sidebar_bookmarks_popup_menu;
536 g_type_class_add_private (g_object_class, sizeof (EvSidebarBookmarksPrivate));
538 g_object_class_override_property (g_object_class, PROP_WIDGET, "main-widget");
540 signals[ADD_BOOKMARK] =
541 g_signal_new ("add-bookmark",
542 G_TYPE_FROM_CLASS (g_object_class),
543 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
544 G_STRUCT_OFFSET (EvSidebarBookmarksClass, add_bookmark),
546 g_cclosure_marshal_VOID__VOID,
552 ev_sidebar_bookmarks_new (void)
554 return GTK_WIDGET (g_object_new (EV_TYPE_SIDEBAR_BOOKMARKS, NULL));
558 ev_sidebar_bookmarks_set_bookmarks (EvSidebarBookmarks *sidebar_bookmarks,
559 EvBookmarks *bookmarks)
561 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
563 g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));
565 if (priv->bookmarks == bookmarks)
569 g_object_unref (priv->bookmarks);
570 priv->bookmarks = g_object_ref (bookmarks);
571 g_signal_connect (priv->bookmarks, "changed",
572 G_CALLBACK (ev_sidebar_bookmarks_changed),
575 gtk_widget_set_sensitive (priv->add_button, TRUE);
576 ev_sidebar_bookmarks_update (sidebar_bookmarks);
579 /* EvSidebarPageIface */
581 ev_sidebar_bookmarks_set_model (EvSidebarPage *sidebar_page,
582 EvDocumentModel *model)
584 EvSidebarBookmarks *sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (sidebar_page);
585 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
587 if (priv->model == model)
591 g_object_unref (priv->model);
592 priv->model = g_object_ref (model);
596 ev_sidebar_bookmarks_support_document (EvSidebarPage *sidebar_page,
597 EvDocument *document)
603 ev_sidebar_bookmarks_get_label (EvSidebarPage *sidebar_page)
605 return _("Bookmarks");
609 ev_sidebar_bookmarks_page_iface_init (EvSidebarPageInterface *iface)
611 iface->support_document = ev_sidebar_bookmarks_support_document;
612 iface->set_model = ev_sidebar_bookmarks_set_model;
613 iface->get_label = ev_sidebar_bookmarks_get_label;