1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Red Hat, Inc.
6 * Jonathan Blandford <jrb@alum.mit.edu>
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.
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.
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.
29 #include <glib/gi18n.h>
32 #include "ev-document-links.h"
33 #include "ev-job-scheduler.h"
34 #include "ev-sidebar-links.h"
35 #include "ev-sidebar-page.h"
36 #include "ev-window.h"
38 struct _EvSidebarLinksPrivate {
41 /* Keep these ids around for blocking */
43 guint page_changed_id;
44 guint row_activated_id;
49 EvDocumentModel *doc_model;
63 static void update_page_callback (EvSidebarLinks *sidebar_links,
66 static void row_activated_callback (GtkTreeView *treeview,
68 GtkTreeViewColumn *arg2,
70 static void ev_sidebar_links_set_links_model (EvSidebarLinks *links,
72 static void job_finished_callback (EvJobLinks *job,
73 EvSidebarLinks *sidebar_links);
74 static void ev_sidebar_links_set_current_page (EvSidebarLinks *sidebar_links,
76 static void ev_sidebar_links_page_iface_init (EvSidebarPageIface *iface);
77 static gboolean ev_sidebar_links_support_document (EvSidebarPage *sidebar_page,
78 EvDocument *document);
79 static const gchar* ev_sidebar_links_get_label (EvSidebarPage *sidebar_page);
81 static guint signals[N_SIGNALS];
83 G_DEFINE_TYPE_EXTENDED (EvSidebarLinks,
87 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
88 ev_sidebar_links_page_iface_init))
91 #define EV_SIDEBAR_LINKS_GET_PRIVATE(object) \
92 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_LINKS, EvSidebarLinksPrivate))
95 ev_sidebar_links_set_property (GObject *object,
100 EvSidebarLinks *ev_sidebar_links = EV_SIDEBAR_LINKS (object);
105 ev_sidebar_links_set_links_model (ev_sidebar_links, g_value_get_object (value));
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114 ev_sidebar_links_get_property (GObject *object,
119 EvSidebarLinks *ev_sidebar_links;
121 ev_sidebar_links = EV_SIDEBAR_LINKS (object);
126 g_value_set_object (value, ev_sidebar_links->priv->model);
129 g_value_set_object (value, ev_sidebar_links->priv->tree_view);
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 ev_sidebar_links_dispose (GObject *object)
140 EvSidebarLinks *sidebar = EV_SIDEBAR_LINKS (object);
142 if (sidebar->priv->job) {
143 g_signal_handlers_disconnect_by_func (sidebar->priv->job,
144 job_finished_callback, sidebar);
145 ev_job_cancel (sidebar->priv->job);
146 g_object_unref (sidebar->priv->job);
147 sidebar->priv->job = NULL;
150 if (sidebar->priv->model) {
151 g_object_unref (sidebar->priv->model);
152 sidebar->priv->model = NULL;
155 if (sidebar->priv->document) {
156 g_object_unref (sidebar->priv->document);
157 sidebar->priv->document = NULL;
158 sidebar->priv->doc_model = NULL;
161 G_OBJECT_CLASS (ev_sidebar_links_parent_class)->dispose (object);
165 ev_sidebar_links_map (GtkWidget *widget)
167 EvSidebarLinks *links;
169 links = EV_SIDEBAR_LINKS (widget);
171 GTK_WIDGET_CLASS (ev_sidebar_links_parent_class)->map (widget);
173 if (links->priv->model) {
174 ev_sidebar_links_set_current_page (links,
175 ev_document_model_get_page (links->priv->doc_model));
180 ev_sidebar_links_class_init (EvSidebarLinksClass *ev_sidebar_links_class)
182 GObjectClass *g_object_class;
183 GtkWidgetClass *widget_class;
185 g_object_class = G_OBJECT_CLASS (ev_sidebar_links_class);
186 widget_class = GTK_WIDGET_CLASS (ev_sidebar_links_class);
188 g_object_class->set_property = ev_sidebar_links_set_property;
189 g_object_class->get_property = ev_sidebar_links_get_property;
190 g_object_class->dispose = ev_sidebar_links_dispose;
192 widget_class->map = ev_sidebar_links_map;
194 signals[LINK_ACTIVATED] = g_signal_new ("link-activated",
195 G_TYPE_FROM_CLASS (g_object_class),
196 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
197 G_STRUCT_OFFSET (EvSidebarLinksClass, link_activated),
199 g_cclosure_marshal_VOID__OBJECT,
200 G_TYPE_NONE, 1, G_TYPE_OBJECT);
202 g_object_class_install_property (g_object_class,
204 g_param_spec_object ("model",
209 g_object_class_override_property (g_object_class,
213 g_type_class_add_private (g_object_class, sizeof (EvSidebarLinksPrivate));
217 selection_changed_callback (GtkTreeSelection *selection,
218 EvSidebarLinks *ev_sidebar_links)
220 EvDocument *document;
224 document = EV_DOCUMENT (ev_sidebar_links->priv->document);
225 g_return_if_fail (ev_sidebar_links->priv->document != NULL);
227 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
230 gtk_tree_model_get (model, &iter,
231 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
237 g_signal_handler_block (ev_sidebar_links->priv->doc_model,
238 ev_sidebar_links->priv->page_changed_id);
239 g_signal_emit (ev_sidebar_links, signals[LINK_ACTIVATED], 0, link);
240 g_signal_handler_unblock (ev_sidebar_links->priv->doc_model,
241 ev_sidebar_links->priv->page_changed_id);
243 g_object_unref (link);
247 static GtkTreeModel *
248 create_loading_model (void)
250 GtkTreeModel *retval;
254 /* Creates a fake model to indicate that we're loading */
255 retval = (GtkTreeModel *)gtk_list_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
261 gtk_list_store_append (GTK_LIST_STORE (retval), &iter);
262 markup = g_strdup_printf ("<span size=\"larger\" style=\"italic\">%s</span>", _("Loading…"));
263 gtk_list_store_set (GTK_LIST_STORE (retval), &iter,
264 EV_DOCUMENT_LINKS_COLUMN_MARKUP, markup,
265 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
266 EV_DOCUMENT_LINKS_COLUMN_LINK, NULL,
274 print_section_cb (GtkWidget *menuitem, EvSidebarLinks *sidebar)
277 GtkTreeSelection *selection;
281 selection = gtk_tree_view_get_selection
282 (GTK_TREE_VIEW (sidebar->priv->tree_view));
284 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
286 int first_page, last_page = -1;
288 gtk_tree_model_get (model, &iter,
289 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
295 first_page = ev_link_get_page (link);
296 if (first_page == -1) {
297 g_object_unref (link);
302 g_object_unref (link);
304 if (gtk_tree_model_iter_next (model, &iter)) {
305 gtk_tree_model_get (model, &iter,
306 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
310 last_page = ev_link_get_page (link);
311 g_object_unref (link);
314 last_page = ev_document_get_n_pages (sidebar->priv->document);
318 last_page = ev_document_get_n_pages (sidebar->priv->document);
320 window = gtk_widget_get_toplevel (GTK_WIDGET (sidebar));
321 if (EV_IS_WINDOW (window)) {
322 ev_window_print_range (EV_WINDOW (window), first_page, last_page);
328 build_popup_menu (EvSidebarLinks *sidebar)
333 menu = gtk_menu_new ();
334 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_PRINT, NULL);
335 gtk_label_set_label (GTK_LABEL (gtk_bin_get_child (GTK_BIN (item))), _("Print…"));
336 gtk_widget_show (item);
337 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
338 g_signal_connect (item, "activate",
339 G_CALLBACK (print_section_cb), sidebar);
341 return GTK_MENU (menu);
345 popup_menu_cb (GtkWidget *treeview, EvSidebarLinks *sidebar)
347 GtkMenu *menu = build_popup_menu (sidebar);
349 gtk_menu_popup (menu, NULL, NULL,
350 ev_gui_menu_position_tree_selection,
351 sidebar->priv->tree_view, 0,
352 gtk_get_current_event_time ());
353 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
357 button_press_cb (GtkWidget *treeview,
358 GdkEventButton *event,
359 EvSidebarLinks *sidebar)
361 GtkTreePath *path = NULL;
363 if (event->button == 3) {
364 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (treeview),
369 gtk_tree_view_set_cursor (GTK_TREE_VIEW (treeview),
371 gtk_menu_popup (build_popup_menu (sidebar), NULL,
372 NULL, NULL, NULL, event->button,
373 gtk_get_current_event_time ());
374 gtk_tree_path_free (path);
385 ev_sidebar_links_construct (EvSidebarLinks *ev_sidebar_links)
387 EvSidebarLinksPrivate *priv;
389 GtkTreeViewColumn *column;
390 GtkCellRenderer *renderer;
391 GtkTreeSelection *selection;
392 GtkTreeModel *loading_model;
394 priv = ev_sidebar_links->priv;
396 swindow = gtk_scrolled_window_new (NULL, NULL);
398 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
399 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
400 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
403 /* Create tree view */
404 loading_model = create_loading_model ();
405 priv->tree_view = gtk_tree_view_new_with_model (loading_model);
406 g_object_unref (loading_model);
408 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
409 gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
410 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
411 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
413 gtk_box_pack_start (GTK_BOX (ev_sidebar_links), swindow, TRUE, TRUE, 0);
414 gtk_widget_show_all (GTK_WIDGET (ev_sidebar_links));
416 column = gtk_tree_view_column_new ();
417 gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
418 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), column);
420 renderer = (GtkCellRenderer*)
421 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
422 "ellipsize", PANGO_ELLIPSIZE_END,
424 gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, TRUE);
425 gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
426 "markup", EV_DOCUMENT_LINKS_COLUMN_MARKUP,
430 renderer = gtk_cell_renderer_text_new ();
431 gtk_tree_view_column_pack_end (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
432 gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
433 "text", EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL,
435 g_object_set (G_OBJECT (renderer), "style", PANGO_STYLE_ITALIC, NULL);
437 g_signal_connect (priv->tree_view,
438 "button_press_event",
439 G_CALLBACK (button_press_cb),
441 g_signal_connect (priv->tree_view,
443 G_CALLBACK (popup_menu_cb),
448 ev_sidebar_links_init (EvSidebarLinks *ev_sidebar_links)
450 ev_sidebar_links->priv = EV_SIDEBAR_LINKS_GET_PRIVATE (ev_sidebar_links);
452 ev_sidebar_links_construct (ev_sidebar_links);
456 fill_page_labels (GtkTreeModel *tree_model,
459 EvSidebarLinks *sidebar_links)
465 gtk_tree_model_get (tree_model, iter,
466 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
472 page = ev_link_get_page (link);
477 page_label = ev_document_get_page_label (sidebar_links->priv->document,
479 gtk_tree_store_set (GTK_TREE_STORE (tree_model), iter,
480 EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL, page_label,
485 g_object_unref (link);
489 /* Public Functions */
492 ev_sidebar_links_new (void)
494 GtkWidget *ev_sidebar_links;
496 ev_sidebar_links = g_object_new (EV_TYPE_SIDEBAR_LINKS, NULL);
498 return ev_sidebar_links;
502 update_page_callback_foreach (GtkTreeModel *model,
507 EvSidebarLinks *sidebar_links = (data);
510 gtk_tree_model_get (model, iter,
511 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
518 dest_page = ev_link_get_page (link);
519 g_object_unref (link);
521 current_page = ev_document_model_get_page (sidebar_links->priv->doc_model);
523 if (dest_page == current_page) {
524 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
526 gtk_tree_view_set_cursor (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
537 ev_sidebar_links_set_current_page (EvSidebarLinks *sidebar_links,
540 GtkTreeSelection *selection;
544 /* Widget is not currently visible */
545 if (!gtk_widget_get_mapped (GTK_WIDGET (sidebar_links)))
548 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (sidebar_links->priv->tree_view));
550 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
553 gtk_tree_model_get (model, &iter,
554 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
559 dest_page = ev_link_get_page (link);
560 g_object_unref (link);
562 if (dest_page == current_page)
567 /* We go through the tree linearly looking for the first page that
568 * matches. This is pretty inefficient. We can do something neat with
569 * a GtkTreeModelSort here to make it faster, if it turns out to be
572 g_signal_handler_block (selection, sidebar_links->priv->selection_id);
573 g_signal_handler_block (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
575 gtk_tree_model_foreach (model,
576 update_page_callback_foreach,
579 g_signal_handler_unblock (selection, sidebar_links->priv->selection_id);
580 g_signal_handler_unblock (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
584 update_page_callback (EvSidebarLinks *sidebar_links,
588 ev_sidebar_links_set_current_page (sidebar_links, new_page);
592 row_activated_callback (GtkTreeView *treeview,
594 GtkTreeViewColumn *arg2,
597 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (treeview), arg1)) {
598 gtk_tree_view_collapse_row (GTK_TREE_VIEW (treeview), arg1);
600 gtk_tree_view_expand_row (GTK_TREE_VIEW (treeview), arg1, FALSE);
605 expand_open_links (GtkTreeView *tree_view, GtkTreeModel *model, GtkTreeIter *parent)
610 if (gtk_tree_model_iter_children (model, &iter, parent)) {
612 gtk_tree_model_get (model, &iter,
613 EV_DOCUMENT_LINKS_COLUMN_EXPAND, &expand,
618 path = gtk_tree_model_get_path (model, &iter);
619 gtk_tree_view_expand_row (tree_view, path, FALSE);
620 gtk_tree_path_free (path);
623 expand_open_links (tree_view, model, &iter);
624 } while (gtk_tree_model_iter_next (model, &iter));
629 ev_sidebar_links_set_links_model (EvSidebarLinks *sidebar_links,
632 EvSidebarLinksPrivate *priv = sidebar_links->priv;
634 if (priv->model == model)
638 g_object_unref (priv->model);
639 priv->model = g_object_ref (model);
641 g_object_notify (G_OBJECT (sidebar_links), "model");
645 job_finished_callback (EvJobLinks *job,
646 EvSidebarLinks *sidebar_links)
648 EvSidebarLinksPrivate *priv = sidebar_links->priv;
649 GtkTreeSelection *selection;
651 ev_sidebar_links_set_links_model (sidebar_links, job->model);
653 gtk_tree_model_foreach (priv->model, (GtkTreeModelForeachFunc)fill_page_labels, sidebar_links);
655 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), job->model);
657 g_object_unref (job);
660 expand_open_links (GTK_TREE_VIEW (priv->tree_view), priv->model, NULL);
662 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
663 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
665 if (priv->selection_id <= 0) {
667 g_signal_connect (selection, "changed",
668 G_CALLBACK (selection_changed_callback),
671 priv->page_changed_id =
672 g_signal_connect_swapped (priv->doc_model, "page-changed",
673 G_CALLBACK (update_page_callback),
675 if (priv->row_activated_id <= 0) {
676 priv->row_activated_id =
677 g_signal_connect (priv->tree_view, "row-activated",
678 G_CALLBACK (row_activated_callback),
682 ev_sidebar_links_set_current_page (sidebar_links,
683 ev_document_model_get_page (priv->doc_model));
687 ev_sidebar_links_document_changed_cb (EvDocumentModel *model,
689 EvSidebarLinks *sidebar_links)
691 EvDocument *document = ev_document_model_get_document (model);
692 EvSidebarLinksPrivate *priv = sidebar_links->priv;
694 if (!EV_IS_DOCUMENT_LINKS (document))
697 if (!ev_document_links_has_document_links (EV_DOCUMENT_LINKS (document)))
700 if (priv->document) {
701 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
702 g_object_unref (priv->document);
705 priv->document = g_object_ref (document);
708 g_signal_handlers_disconnect_by_func (priv->job,
709 job_finished_callback,
711 g_object_unref (priv->job);
714 priv->job = ev_job_links_new (document);
715 g_signal_connect (priv->job,
717 G_CALLBACK (job_finished_callback),
719 /* The priority doesn't matter for this job */
720 ev_job_scheduler_push_job (priv->job, EV_JOB_PRIORITY_NONE);
724 ev_sidebar_links_set_model (EvSidebarPage *sidebar_page,
725 EvDocumentModel *model)
727 EvSidebarLinks *sidebar_links = EV_SIDEBAR_LINKS (sidebar_page);
728 EvSidebarLinksPrivate *priv = sidebar_links->priv;
730 if (priv->doc_model == model)
733 priv->doc_model = model;
734 g_signal_connect (model, "notify::document",
735 G_CALLBACK (ev_sidebar_links_document_changed_cb),
740 ev_sidebar_links_support_document (EvSidebarPage *sidebar_page,
741 EvDocument *document)
743 return (EV_IS_DOCUMENT_LINKS (document) &&
744 ev_document_links_has_document_links (EV_DOCUMENT_LINKS (document)));
748 ev_sidebar_links_get_label (EvSidebarPage *sidebar_page)
754 ev_sidebar_links_page_iface_init (EvSidebarPageIface *iface)
756 iface->support_document = ev_sidebar_links_support_document;
757 iface->set_model = ev_sidebar_links_set_model;
758 iface->get_label = ev_sidebar_links_get_label;