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 (EvSidebarPageInterface *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;
287 EvDocumentLinks *document_links;
289 gtk_tree_model_get (model, &iter,
290 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
296 document_links = EV_DOCUMENT_LINKS (sidebar->priv->document);
298 first_page = ev_document_links_get_link_page (document_links, link);
299 if (first_page == -1) {
300 g_object_unref (link);
305 g_object_unref (link);
307 if (gtk_tree_model_iter_next (model, &iter)) {
308 gtk_tree_model_get (model, &iter,
309 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
313 last_page = ev_document_links_get_link_page (document_links, link);;
314 g_object_unref (link);
317 last_page = ev_document_get_n_pages (sidebar->priv->document);
321 last_page = ev_document_get_n_pages (sidebar->priv->document);
323 window = gtk_widget_get_toplevel (GTK_WIDGET (sidebar));
324 if (EV_IS_WINDOW (window)) {
325 ev_window_print_range (EV_WINDOW (window), first_page, last_page);
331 build_popup_menu (EvSidebarLinks *sidebar)
336 menu = gtk_menu_new ();
337 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_PRINT, NULL);
338 gtk_label_set_label (GTK_LABEL (gtk_bin_get_child (GTK_BIN (item))), _("Print…"));
339 gtk_widget_show (item);
340 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
341 g_signal_connect (item, "activate",
342 G_CALLBACK (print_section_cb), sidebar);
344 return GTK_MENU (menu);
348 popup_menu_cb (GtkWidget *treeview, EvSidebarLinks *sidebar)
350 GtkMenu *menu = build_popup_menu (sidebar);
352 gtk_menu_popup (menu, NULL, NULL,
353 ev_gui_menu_position_tree_selection,
354 sidebar->priv->tree_view, 0,
355 gtk_get_current_event_time ());
356 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
360 button_press_cb (GtkWidget *treeview,
361 GdkEventButton *event,
362 EvSidebarLinks *sidebar)
364 GtkTreePath *path = NULL;
366 if (event->button == 3) {
367 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (treeview),
372 gtk_tree_view_set_cursor (GTK_TREE_VIEW (treeview),
374 gtk_menu_popup (build_popup_menu (sidebar), NULL,
375 NULL, NULL, NULL, event->button,
376 gtk_get_current_event_time ());
377 gtk_tree_path_free (path);
388 ev_sidebar_links_construct (EvSidebarLinks *ev_sidebar_links)
390 EvSidebarLinksPrivate *priv;
392 GtkTreeViewColumn *column;
393 GtkCellRenderer *renderer;
394 GtkTreeSelection *selection;
395 GtkTreeModel *loading_model;
397 priv = ev_sidebar_links->priv;
399 swindow = gtk_scrolled_window_new (NULL, NULL);
401 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
402 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
403 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
406 /* Create tree view */
407 loading_model = create_loading_model ();
408 priv->tree_view = gtk_tree_view_new_with_model (loading_model);
409 g_object_unref (loading_model);
411 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
412 gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
413 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
414 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
416 gtk_box_pack_start (GTK_BOX (ev_sidebar_links), swindow, TRUE, TRUE, 0);
417 gtk_widget_show_all (GTK_WIDGET (ev_sidebar_links));
419 column = gtk_tree_view_column_new ();
420 gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
421 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), column);
423 renderer = (GtkCellRenderer*)
424 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
425 "ellipsize", PANGO_ELLIPSIZE_END,
427 gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, TRUE);
428 gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
429 "markup", EV_DOCUMENT_LINKS_COLUMN_MARKUP,
433 renderer = gtk_cell_renderer_text_new ();
434 gtk_tree_view_column_pack_end (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
435 gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
436 "text", EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL,
438 g_object_set (G_OBJECT (renderer), "style", PANGO_STYLE_ITALIC, NULL);
440 g_signal_connect (priv->tree_view,
441 "button_press_event",
442 G_CALLBACK (button_press_cb),
444 g_signal_connect (priv->tree_view,
446 G_CALLBACK (popup_menu_cb),
451 ev_sidebar_links_init (EvSidebarLinks *ev_sidebar_links)
453 ev_sidebar_links->priv = EV_SIDEBAR_LINKS_GET_PRIVATE (ev_sidebar_links);
455 ev_sidebar_links_construct (ev_sidebar_links);
458 /* Public Functions */
461 ev_sidebar_links_new (void)
463 GtkWidget *ev_sidebar_links;
465 ev_sidebar_links = g_object_new (EV_TYPE_SIDEBAR_LINKS, NULL);
467 return ev_sidebar_links;
471 update_page_callback_foreach (GtkTreeModel *model,
476 EvSidebarLinks *sidebar_links = (data);
479 gtk_tree_model_get (model, iter,
480 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
486 EvDocumentLinks *document_links = EV_DOCUMENT_LINKS (sidebar_links->priv->document);
488 dest_page = ev_document_links_get_link_page (document_links, link);
489 g_object_unref (link);
491 current_page = ev_document_model_get_page (sidebar_links->priv->doc_model);
493 if (dest_page == current_page) {
494 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
496 gtk_tree_view_set_cursor (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
507 ev_sidebar_links_set_current_page (EvSidebarLinks *sidebar_links,
510 GtkTreeSelection *selection;
514 /* Widget is not currently visible */
515 if (!gtk_widget_get_mapped (GTK_WIDGET (sidebar_links)))
518 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (sidebar_links->priv->tree_view));
520 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
523 gtk_tree_model_get (model, &iter,
524 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
528 EvDocumentLinks *document_links = EV_DOCUMENT_LINKS (sidebar_links->priv->document);
530 dest_page = ev_document_links_get_link_page (document_links, link);
531 g_object_unref (link);
533 if (dest_page == current_page)
538 /* We go through the tree linearly looking for the first page that
539 * matches. This is pretty inefficient. We can do something neat with
540 * a GtkTreeModelSort here to make it faster, if it turns out to be
543 g_signal_handler_block (selection, sidebar_links->priv->selection_id);
544 g_signal_handler_block (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
546 gtk_tree_model_foreach (model,
547 update_page_callback_foreach,
550 g_signal_handler_unblock (selection, sidebar_links->priv->selection_id);
551 g_signal_handler_unblock (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
555 update_page_callback (EvSidebarLinks *sidebar_links,
559 ev_sidebar_links_set_current_page (sidebar_links, new_page);
563 row_activated_callback (GtkTreeView *treeview,
565 GtkTreeViewColumn *arg2,
568 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (treeview), arg1)) {
569 gtk_tree_view_collapse_row (GTK_TREE_VIEW (treeview), arg1);
571 gtk_tree_view_expand_row (GTK_TREE_VIEW (treeview), arg1, FALSE);
576 expand_open_links (GtkTreeView *tree_view, GtkTreeModel *model, GtkTreeIter *parent)
581 if (gtk_tree_model_iter_children (model, &iter, parent)) {
583 gtk_tree_model_get (model, &iter,
584 EV_DOCUMENT_LINKS_COLUMN_EXPAND, &expand,
589 path = gtk_tree_model_get_path (model, &iter);
590 gtk_tree_view_expand_row (tree_view, path, FALSE);
591 gtk_tree_path_free (path);
594 expand_open_links (tree_view, model, &iter);
595 } while (gtk_tree_model_iter_next (model, &iter));
600 ev_sidebar_links_set_links_model (EvSidebarLinks *sidebar_links,
603 EvSidebarLinksPrivate *priv = sidebar_links->priv;
605 if (priv->model == model)
609 g_object_unref (priv->model);
610 priv->model = g_object_ref (model);
612 g_object_notify (G_OBJECT (sidebar_links), "model");
616 job_finished_callback (EvJobLinks *job,
617 EvSidebarLinks *sidebar_links)
619 EvSidebarLinksPrivate *priv = sidebar_links->priv;
620 GtkTreeSelection *selection;
622 ev_sidebar_links_set_links_model (sidebar_links, job->model);
624 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), job->model);
626 g_object_unref (job);
629 expand_open_links (GTK_TREE_VIEW (priv->tree_view), priv->model, NULL);
631 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
632 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
634 if (priv->selection_id <= 0) {
636 g_signal_connect (selection, "changed",
637 G_CALLBACK (selection_changed_callback),
640 priv->page_changed_id =
641 g_signal_connect_swapped (priv->doc_model, "page-changed",
642 G_CALLBACK (update_page_callback),
644 if (priv->row_activated_id <= 0) {
645 priv->row_activated_id =
646 g_signal_connect (priv->tree_view, "row-activated",
647 G_CALLBACK (row_activated_callback),
651 ev_sidebar_links_set_current_page (sidebar_links,
652 ev_document_model_get_page (priv->doc_model));
656 ev_sidebar_links_document_changed_cb (EvDocumentModel *model,
658 EvSidebarLinks *sidebar_links)
660 EvDocument *document = ev_document_model_get_document (model);
661 EvSidebarLinksPrivate *priv = sidebar_links->priv;
663 if (!EV_IS_DOCUMENT_LINKS (document))
666 if (!ev_document_links_has_document_links (EV_DOCUMENT_LINKS (document)))
669 if (priv->document) {
670 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
671 g_object_unref (priv->document);
674 priv->document = g_object_ref (document);
677 g_signal_handlers_disconnect_by_func (priv->job,
678 job_finished_callback,
680 g_object_unref (priv->job);
683 priv->job = ev_job_links_new (document);
684 g_signal_connect (priv->job,
686 G_CALLBACK (job_finished_callback),
688 /* The priority doesn't matter for this job */
689 ev_job_scheduler_push_job (priv->job, EV_JOB_PRIORITY_NONE);
693 ev_sidebar_links_set_model (EvSidebarPage *sidebar_page,
694 EvDocumentModel *model)
696 EvSidebarLinks *sidebar_links = EV_SIDEBAR_LINKS (sidebar_page);
697 EvSidebarLinksPrivate *priv = sidebar_links->priv;
699 if (priv->doc_model == model)
702 priv->doc_model = model;
703 g_signal_connect (model, "notify::document",
704 G_CALLBACK (ev_sidebar_links_document_changed_cb),
709 ev_sidebar_links_support_document (EvSidebarPage *sidebar_page,
710 EvDocument *document)
712 return (EV_IS_DOCUMENT_LINKS (document) &&
713 ev_document_links_has_document_links (EV_DOCUMENT_LINKS (document)));
717 ev_sidebar_links_get_label (EvSidebarPage *sidebar_page)
723 ev_sidebar_links_page_iface_init (EvSidebarPageInterface *iface)
725 iface->support_document = ev_sidebar_links_support_document;
726 iface->set_model = ev_sidebar_links_set_model;
727 iface->get_label = ev_sidebar_links_get_label;