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"
46 struct _EvSidebarBookmarksPrivate {
47 EvDocumentModel *model;
48 EvBookmarks *bookmarks;
51 GtkWidget *del_button;
52 GtkWidget *add_button;
55 static void ev_sidebar_bookmarks_page_iface_init (EvSidebarPageInterface *iface);
57 G_DEFINE_TYPE_EXTENDED (EvSidebarBookmarks,
61 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
62 ev_sidebar_bookmarks_page_iface_init))
64 static guint signals[N_SIGNALS];
67 compare_bookmarks (EvBookmark *a,
70 if (a->page < b->page)
72 if (a->page > b->page)
78 ev_sidebar_bookmarks_update (EvSidebarBookmarks *sidebar_bookmarks)
80 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
85 model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view)));
86 gtk_list_store_clear (model);
88 if (!priv->bookmarks) {
89 g_object_set (priv->tree_view, "has-tooltip", FALSE, NULL);
93 items = ev_bookmarks_get_bookmarks (priv->bookmarks);
94 items = g_list_sort (items, (GCompareFunc)compare_bookmarks);
95 for (l = items; l; l = g_list_next (l)) {
96 EvBookmark *bm = (EvBookmark *)l->data;
98 gtk_list_store_append (model, &iter);
99 gtk_list_store_set (model, &iter,
100 COLUMN_MARKUP, bm->title,
101 COLUMN_PAGE, bm->page,
105 g_object_set (priv->tree_view, "has-tooltip", TRUE, NULL);
109 ev_sidebar_bookmarks_changed (EvBookmarks *bookmarks,
110 EvSidebarBookmarks *sidebar_bookmarks)
112 ev_sidebar_bookmarks_update (sidebar_bookmarks);
116 ev_sidebar_bookmarks_get_selected_page (EvSidebarBookmarks *sidebar_bookmarks,
117 GtkTreeSelection *selection)
122 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
125 gtk_tree_model_get (model, &iter,
135 ev_sidebar_bookmarks_selection_changed (GtkTreeSelection *selection,
136 EvSidebarBookmarks *sidebar_bookmarks)
138 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
141 page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
143 ev_document_model_set_page (priv->model, page);
144 gtk_widget_set_sensitive (priv->del_button, TRUE);
146 gtk_widget_set_sensitive (priv->del_button, FALSE);
151 ev_sidebar_bookmarks_add_clicked (GtkWidget *button,
152 EvSidebarBookmarks *sidebar_bookmarks)
154 /* Let the window add the bookmark since
155 * since we don't know the page title
157 g_signal_emit (sidebar_bookmarks, signals[ADD_BOOKMARK], 0);
161 ev_sidebar_bookmarks_del_clicked (GtkWidget *button,
162 EvSidebarBookmarks *sidebar_bookmarks)
164 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
165 GtkTreeSelection *selection;
169 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
170 page = ev_sidebar_bookmarks_get_selected_page (sidebar_bookmarks, selection);
176 ev_bookmarks_delete (priv->bookmarks, &bm);
180 ev_sidebar_bookmarks_bookmark_renamed (GtkCellRendererText *renderer,
181 const gchar *path_string,
182 const gchar *new_text,
183 EvSidebarBookmarks *sidebar_bookmarks)
185 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
186 GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
192 if (!new_text || new_text[0] == '\0')
195 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view));
196 gtk_tree_model_get_iter (model, &iter, path);
197 gtk_tree_model_get (model, &iter,
200 gtk_tree_path_free (path);
203 bm.title = g_strdup (new_text);
204 ev_bookmarks_update (priv->bookmarks, &bm);
208 ev_sidebar_bookmarks_query_tooltip (GtkWidget *widget,
211 gboolean keyboard_tip,
213 EvSidebarBookmarks *sidebar_bookmarks)
215 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
218 GtkTreePath *path = NULL;
219 EvDocument *document;
224 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view));
225 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (priv->tree_view),
226 &x, &y, keyboard_tip,
227 &model, &path, &iter))
230 gtk_tree_model_get (model, &iter,
234 document = ev_document_model_get_document (priv->model);
235 page_label = ev_document_get_page_label (document, page);
236 text = g_strdup_printf (_("Page %s"), page_label);
237 gtk_tooltip_set_text (tooltip, text);
241 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (priv->tree_view),
243 gtk_tree_path_free (path);
249 ev_sidebar_bookmarks_dispose (GObject *object)
251 EvSidebarBookmarks *sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (object);
252 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
255 g_object_unref (priv->model);
259 if (priv->bookmarks) {
260 g_object_unref (priv->bookmarks);
261 priv->bookmarks = NULL;
264 G_OBJECT_CLASS (ev_sidebar_bookmarks_parent_class)->dispose (object);
268 ev_sidebar_bookmarks_init (EvSidebarBookmarks *sidebar_bookmarks)
270 EvSidebarBookmarksPrivate *priv;
274 GtkCellRenderer *renderer;
275 GtkTreeSelection *selection;
277 sidebar_bookmarks->priv = G_TYPE_INSTANCE_GET_PRIVATE (sidebar_bookmarks,
278 EV_TYPE_SIDEBAR_BOOKMARKS,
279 EvSidebarBookmarksPrivate);
280 priv = sidebar_bookmarks->priv;
282 gtk_box_set_spacing (GTK_BOX (sidebar_bookmarks), 6);
284 swindow = gtk_scrolled_window_new (NULL, NULL);
285 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
286 GTK_POLICY_AUTOMATIC,
287 GTK_POLICY_AUTOMATIC);
288 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
290 gtk_box_pack_start (GTK_BOX (sidebar_bookmarks), swindow, TRUE, TRUE, 0);
291 gtk_widget_show (swindow);
293 model = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_UINT);
294 priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
295 g_object_unref (model);
296 g_signal_connect (priv->tree_view, "query-tooltip",
297 G_CALLBACK (ev_sidebar_bookmarks_query_tooltip),
299 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
300 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
301 g_signal_connect (selection, "changed",
302 G_CALLBACK (ev_sidebar_bookmarks_selection_changed),
305 renderer = gtk_cell_renderer_text_new ();
306 g_object_set (renderer,
307 "ellipsize", PANGO_ELLIPSIZE_END,
310 g_signal_connect (renderer, "edited",
311 G_CALLBACK (ev_sidebar_bookmarks_bookmark_renamed),
313 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view),
315 "markup", COLUMN_MARKUP,
317 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
318 gtk_widget_show (priv->tree_view);
320 hbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
322 priv->add_button = gtk_button_new_from_stock (GTK_STOCK_ADD);
323 g_signal_connect (priv->add_button, "clicked",
324 G_CALLBACK (ev_sidebar_bookmarks_add_clicked),
326 gtk_widget_set_sensitive (priv->add_button, FALSE);
327 gtk_box_pack_start (GTK_BOX (hbox), priv->add_button, TRUE, TRUE, 6);
328 gtk_widget_show (priv->add_button);
330 priv->del_button = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
331 g_signal_connect (priv->del_button, "clicked",
332 G_CALLBACK (ev_sidebar_bookmarks_del_clicked),
334 gtk_widget_set_sensitive (priv->del_button, FALSE);
335 gtk_box_pack_start (GTK_BOX (hbox), priv->del_button, TRUE, TRUE, 6);
336 gtk_widget_show (priv->del_button);
338 gtk_box_pack_end (GTK_BOX (sidebar_bookmarks), hbox, FALSE, TRUE, 0);
339 gtk_widget_show (hbox);
340 gtk_widget_show (GTK_WIDGET (sidebar_bookmarks));
344 ev_sidebar_bookmarks_get_property (GObject *object,
349 EvSidebarBookmarks *sidebar_bookmarks;
351 sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (object);
355 g_value_set_object (value, sidebar_bookmarks->priv->tree_view);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364 ev_sidebar_bookmarks_class_init (EvSidebarBookmarksClass *klass)
366 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
368 g_object_class->get_property = ev_sidebar_bookmarks_get_property;
369 g_object_class->dispose = ev_sidebar_bookmarks_dispose;
371 g_type_class_add_private (g_object_class, sizeof (EvSidebarBookmarksPrivate));
373 g_object_class_override_property (g_object_class, PROP_WIDGET, "main-widget");
375 signals[ADD_BOOKMARK] =
376 g_signal_new ("add-bookmark",
377 G_TYPE_FROM_CLASS (g_object_class),
378 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
379 G_STRUCT_OFFSET (EvSidebarBookmarksClass, add_bookmark),
381 g_cclosure_marshal_VOID__VOID,
387 ev_sidebar_bookmarks_new (void)
389 return GTK_WIDGET (g_object_new (EV_TYPE_SIDEBAR_BOOKMARKS, NULL));
393 ev_sidebar_bookmarks_set_bookmarks (EvSidebarBookmarks *sidebar_bookmarks,
394 EvBookmarks *bookmarks)
396 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
398 g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));
400 if (priv->bookmarks == bookmarks)
404 g_object_unref (priv->bookmarks);
405 priv->bookmarks = g_object_ref (bookmarks);
406 g_signal_connect (priv->bookmarks, "changed",
407 G_CALLBACK (ev_sidebar_bookmarks_changed),
410 gtk_widget_set_sensitive (priv->add_button, TRUE);
411 ev_sidebar_bookmarks_update (sidebar_bookmarks);
414 /* EvSidebarPageIface */
416 ev_sidebar_bookmarks_set_model (EvSidebarPage *sidebar_page,
417 EvDocumentModel *model)
419 EvSidebarBookmarks *sidebar_bookmarks = EV_SIDEBAR_BOOKMARKS (sidebar_page);
420 EvSidebarBookmarksPrivate *priv = sidebar_bookmarks->priv;
422 if (priv->model == model)
426 g_object_unref (priv->model);
427 priv->model = g_object_ref (model);
431 ev_sidebar_bookmarks_support_document (EvSidebarPage *sidebar_page,
432 EvDocument *document)
438 ev_sidebar_bookmarks_get_label (EvSidebarPage *sidebar_page)
440 return _("Bookmarks");
444 ev_sidebar_bookmarks_page_iface_init (EvSidebarPageInterface *iface)
446 iface->support_document = ev_sidebar_bookmarks_support_document;
447 iface->set_model = ev_sidebar_bookmarks_set_model;
448 iface->get_label = ev_sidebar_bookmarks_get_label;