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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
28 #include <glib/gi18n.h>
31 #include "ev-sidebar-page.h"
32 #include "ev-sidebar-links.h"
33 #include "ev-job-scheduler.h"
34 #include "ev-document-links.h"
35 #include "ev-window.h"
37 struct _EvSidebarLinksPrivate {
40 /* Keep these ids around for blocking */
42 guint page_changed_id;
43 guint row_activated_id;
48 EvPageCache *page_cache;
62 static void update_page_callback (EvPageCache *page_cache,
64 EvSidebarLinks *sidebar_links);
65 static void row_activated_callback (GtkTreeView *treeview,
67 GtkTreeViewColumn *arg2,
69 static void job_finished_callback (EvJobLinks *job,
70 EvSidebarLinks *sidebar_links);
71 static void ev_sidebar_links_page_iface_init (EvSidebarPageIface *iface);
72 static void ev_sidebar_links_set_document (EvSidebarPage *sidebar_page,
73 EvDocument *document);
74 static gboolean ev_sidebar_links_support_document (EvSidebarPage *sidebar_page,
75 EvDocument *document);
76 static const gchar* ev_sidebar_links_get_label (EvSidebarPage *sidebar_page);
78 static guint signals[N_SIGNALS];
80 G_DEFINE_TYPE_EXTENDED (EvSidebarLinks,
84 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
85 ev_sidebar_links_page_iface_init))
88 #define EV_SIDEBAR_LINKS_GET_PRIVATE(object) \
89 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_LINKS, EvSidebarLinksPrivate))
92 ev_sidebar_links_set_property (GObject *object,
97 EvSidebarLinks *ev_sidebar_links;
100 ev_sidebar_links = EV_SIDEBAR_LINKS (object);
105 model = ev_sidebar_links->priv->model;
106 ev_sidebar_links->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
108 g_object_unref (model);
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 ev_sidebar_links_get_property (GObject *object,
122 EvSidebarLinks *ev_sidebar_links;
124 ev_sidebar_links = EV_SIDEBAR_LINKS (object);
129 g_value_set_object (value, ev_sidebar_links->priv->model);
132 g_value_set_object (value, ev_sidebar_links->priv->tree_view);
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 ev_sidebar_links_dispose (GObject *object)
143 EvSidebarLinks *sidebar = EV_SIDEBAR_LINKS (object);
145 if (sidebar->priv->job) {
146 g_signal_handlers_disconnect_by_func (sidebar->priv->job,
147 job_finished_callback, sidebar);
148 ev_job_cancel (sidebar->priv->job);
149 g_object_unref (sidebar->priv->job);
150 sidebar->priv->job = NULL;
153 if (sidebar->priv->model) {
154 g_object_unref (sidebar->priv->model);
155 sidebar->priv->model = NULL;
158 if (sidebar->priv->document) {
159 g_object_unref (sidebar->priv->document);
160 sidebar->priv->document = NULL;
161 sidebar->priv->page_cache = NULL;
164 G_OBJECT_CLASS (ev_sidebar_links_parent_class)->dispose (object);
168 ev_sidebar_links_map (GtkWidget *widget)
170 EvSidebarLinks *links;
172 links = EV_SIDEBAR_LINKS (widget);
174 GTK_WIDGET_CLASS (ev_sidebar_links_parent_class)->map (widget);
176 if (links->priv->model) {
177 update_page_callback (links->priv->page_cache,
178 ev_page_cache_get_current_page (links->priv->page_cache),
184 ev_sidebar_links_class_init (EvSidebarLinksClass *ev_sidebar_links_class)
186 GObjectClass *g_object_class;
187 GtkWidgetClass *widget_class;
189 g_object_class = G_OBJECT_CLASS (ev_sidebar_links_class);
190 widget_class = GTK_WIDGET_CLASS (ev_sidebar_links_class);
192 g_object_class->set_property = ev_sidebar_links_set_property;
193 g_object_class->get_property = ev_sidebar_links_get_property;
194 g_object_class->dispose = ev_sidebar_links_dispose;
196 widget_class->map = ev_sidebar_links_map;
198 signals[LINK_ACTIVATED] = g_signal_new ("link-activated",
199 G_TYPE_FROM_CLASS (g_object_class),
200 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
201 G_STRUCT_OFFSET (EvSidebarLinksClass, link_activated),
203 g_cclosure_marshal_VOID__OBJECT,
204 G_TYPE_NONE, 1, G_TYPE_OBJECT);
206 g_object_class_install_property (g_object_class,
208 g_param_spec_object ("model",
213 g_object_class_override_property (g_object_class,
217 g_type_class_add_private (g_object_class, sizeof (EvSidebarLinksPrivate));
221 selection_changed_callback (GtkTreeSelection *selection,
222 EvSidebarLinks *ev_sidebar_links)
224 EvDocument *document;
228 document = EV_DOCUMENT (ev_sidebar_links->priv->document);
229 g_return_if_fail (ev_sidebar_links->priv->document != NULL);
231 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
234 gtk_tree_model_get (model, &iter,
235 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
241 g_signal_handler_block (ev_sidebar_links->priv->page_cache,
242 ev_sidebar_links->priv->page_changed_id);
243 g_signal_emit (ev_sidebar_links, signals[LINK_ACTIVATED], 0, link);
244 g_signal_handler_unblock (ev_sidebar_links->priv->page_cache,
245 ev_sidebar_links->priv->page_changed_id);
247 g_object_unref (link);
251 static GtkTreeModel *
252 create_loading_model (void)
254 GtkTreeModel *retval;
258 /* Creates a fake model to indicate that we're loading */
259 retval = (GtkTreeModel *)gtk_list_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
265 gtk_list_store_append (GTK_LIST_STORE (retval), &iter);
266 markup = g_strdup_printf ("<span size=\"larger\" style=\"italic\">%s</span>", _("Loading..."));
267 gtk_list_store_set (GTK_LIST_STORE (retval), &iter,
268 EV_DOCUMENT_LINKS_COLUMN_MARKUP, markup,
269 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
270 EV_DOCUMENT_LINKS_COLUMN_LINK, NULL,
278 print_section_cb (GtkWidget *menuitem, EvSidebarLinks *sidebar)
281 GtkTreeSelection *selection;
285 selection = gtk_tree_view_get_selection
286 (GTK_TREE_VIEW (sidebar->priv->tree_view));
288 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
290 int first_page, last_page = -1;
292 gtk_tree_model_get (model, &iter,
293 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
299 first_page = ev_link_get_page (link);
300 if (first_page == -1) {
301 g_object_unref (link);
306 g_object_unref (link);
308 if (gtk_tree_model_iter_next (model, &iter)) {
309 gtk_tree_model_get (model, &iter,
310 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
314 last_page = ev_link_get_page (link);
315 g_object_unref (link);
318 last_page = ev_page_cache_get_n_pages (sidebar->priv->page_cache);
322 last_page = ev_page_cache_get_n_pages (sidebar->priv->page_cache);
324 window = gtk_widget_get_toplevel (GTK_WIDGET (sidebar));
325 if (EV_IS_WINDOW (window)) {
326 ev_window_print_range (EV_WINDOW (window), first_page, last_page);
332 build_popup_menu (EvSidebarLinks *sidebar)
337 menu = gtk_menu_new ();
338 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_PRINT, NULL);
339 gtk_label_set_label (GTK_LABEL (GTK_BIN (item)->child), _("Print..."));
340 gtk_widget_show (item);
341 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
342 g_signal_connect (item, "activate",
343 G_CALLBACK (print_section_cb), sidebar);
345 return GTK_MENU (menu);
349 popup_menu_cb (GtkWidget *treeview, EvSidebarLinks *sidebar)
351 GtkMenu *menu = build_popup_menu (sidebar);
353 gtk_menu_popup (menu, NULL, NULL,
354 ev_gui_menu_position_tree_selection,
355 sidebar->priv->tree_view, 0,
356 gtk_get_current_event_time ());
357 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
361 button_press_cb (GtkWidget *treeview,
362 GdkEventButton *event,
363 EvSidebarLinks *sidebar)
365 GtkTreePath *path = NULL;
367 if (event->button == 3) {
368 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (treeview),
373 gtk_tree_view_set_cursor (GTK_TREE_VIEW (treeview),
375 gtk_menu_popup (build_popup_menu (sidebar), NULL,
376 NULL, NULL, NULL, event->button,
377 gtk_get_current_event_time ());
378 gtk_tree_path_free (path);
389 ev_sidebar_links_construct (EvSidebarLinks *ev_sidebar_links)
391 EvSidebarLinksPrivate *priv;
393 GtkTreeViewColumn *column;
394 GtkCellRenderer *renderer;
395 GtkTreeSelection *selection;
396 GtkTreeModel *loading_model;
398 priv = ev_sidebar_links->priv;
400 swindow = gtk_scrolled_window_new (NULL, NULL);
402 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
403 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
404 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
407 /* Create tree view */
408 loading_model = create_loading_model ();
409 priv->tree_view = gtk_tree_view_new_with_model (loading_model);
410 g_object_unref (loading_model);
412 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
413 gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
414 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
415 gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
417 gtk_box_pack_start (GTK_BOX (ev_sidebar_links), swindow, TRUE, TRUE, 0);
418 gtk_widget_show_all (GTK_WIDGET (ev_sidebar_links));
420 column = gtk_tree_view_column_new ();
421 gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
422 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), column);
424 renderer = (GtkCellRenderer*)
425 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
426 "ellipsize", PANGO_ELLIPSIZE_END,
428 gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, TRUE);
429 gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
430 "markup", EV_DOCUMENT_LINKS_COLUMN_MARKUP,
434 renderer = gtk_cell_renderer_text_new ();
435 gtk_tree_view_column_pack_end (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
436 gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
437 "text", EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL,
439 g_object_set (G_OBJECT (renderer), "style", PANGO_STYLE_ITALIC, NULL);
441 g_signal_connect (GTK_TREE_VIEW (priv->tree_view),
442 "button_press_event",
443 G_CALLBACK (button_press_cb),
445 g_signal_connect (GTK_TREE_VIEW (priv->tree_view),
447 G_CALLBACK (popup_menu_cb),
452 ev_sidebar_links_init (EvSidebarLinks *ev_sidebar_links)
454 ev_sidebar_links->priv = EV_SIDEBAR_LINKS_GET_PRIVATE (ev_sidebar_links);
456 ev_sidebar_links_construct (ev_sidebar_links);
460 fill_page_labels (GtkTreeModel *tree_model,
463 EvSidebarLinks *sidebar_links)
469 gtk_tree_model_get (tree_model, iter,
470 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
476 page = ev_link_get_page (link);
481 page_label = ev_page_cache_get_page_label (sidebar_links->priv->page_cache,
483 gtk_tree_store_set (GTK_TREE_STORE (tree_model), iter,
484 EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL, page_label,
489 g_object_unref (link);
493 /* Public Functions */
496 ev_sidebar_links_new (void)
498 GtkWidget *ev_sidebar_links;
500 ev_sidebar_links = g_object_new (EV_TYPE_SIDEBAR_LINKS, NULL);
502 return ev_sidebar_links;
506 update_page_callback_foreach (GtkTreeModel *model,
511 EvSidebarLinks *sidebar_links = (data);
514 gtk_tree_model_get (model, iter,
515 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
522 dest_page = ev_link_get_page (link);
523 g_object_unref (link);
525 current_page = ev_page_cache_get_current_page (sidebar_links->priv->page_cache);
527 if (dest_page == current_page) {
528 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
530 gtk_tree_view_set_cursor (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
541 update_page_callback (EvPageCache *page_cache,
543 EvSidebarLinks *sidebar_links)
545 GtkTreeSelection *selection;
549 /* Widget is not currently visible */
550 if (!GTK_WIDGET_MAPPED (sidebar_links))
553 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (sidebar_links->priv->tree_view));
555 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
558 gtk_tree_model_get (model, &iter,
559 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
564 dest_page = ev_link_get_page (link);
565 g_object_unref (link);
567 if (dest_page == current_page)
572 /* We go through the tree linearly looking for the first page that
573 * matches. This is pretty inefficient. We can do something neat with
574 * a GtkTreeModelSort here to make it faster, if it turns out to be
577 g_signal_handler_block (selection, sidebar_links->priv->selection_id);
578 g_signal_handler_block (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
580 gtk_tree_model_foreach (model,
581 update_page_callback_foreach,
584 g_signal_handler_unblock (selection, sidebar_links->priv->selection_id);
585 g_signal_handler_unblock (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
589 row_activated_callback (GtkTreeView *treeview,
591 GtkTreeViewColumn *arg2,
594 if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (treeview), arg1)) {
595 gtk_tree_view_collapse_row (GTK_TREE_VIEW (treeview), arg1);
597 gtk_tree_view_expand_row (GTK_TREE_VIEW (treeview), arg1, FALSE);
602 expand_open_links (GtkTreeView *tree_view, GtkTreeModel *model, GtkTreeIter *parent)
607 if (gtk_tree_model_iter_children (model, &iter, parent)) {
609 gtk_tree_model_get (model, &iter,
610 EV_DOCUMENT_LINKS_COLUMN_EXPAND, &expand,
615 path = gtk_tree_model_get_path (model, &iter);
616 gtk_tree_view_expand_row (tree_view, path, FALSE);
617 gtk_tree_path_free (path);
620 expand_open_links (tree_view, model, &iter);
621 } while (gtk_tree_model_iter_next (model, &iter));
626 job_finished_callback (EvJobLinks *job,
627 EvSidebarLinks *sidebar_links)
629 EvSidebarLinksPrivate *priv;
630 GtkTreeSelection *selection;
632 priv = sidebar_links->priv;
634 priv->model = job->model;
635 g_object_notify (G_OBJECT (sidebar_links), "model");
637 gtk_tree_model_foreach (priv->model, (GtkTreeModelForeachFunc)fill_page_labels, sidebar_links);
639 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), job->model);
641 g_object_unref (job);
644 expand_open_links (GTK_TREE_VIEW (priv->tree_view), priv->model, NULL);
646 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
647 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
649 if (priv->selection_id <= 0) {
651 g_signal_connect (selection, "changed",
652 G_CALLBACK (selection_changed_callback),
655 priv->page_changed_id = g_signal_connect (priv->page_cache, "page-changed",
656 G_CALLBACK (update_page_callback),
658 if (priv->row_activated_id <= 0) {
659 priv->row_activated_id =
660 g_signal_connect (G_OBJECT (priv->tree_view), "row-activated",
661 G_CALLBACK (row_activated_callback),
665 update_page_callback (priv->page_cache,
666 ev_page_cache_get_current_page (priv->page_cache),
671 ev_sidebar_links_set_document (EvSidebarPage *sidebar_page,
672 EvDocument *document)
674 EvSidebarLinks *sidebar_links;
675 EvSidebarLinksPrivate *priv;
677 g_return_if_fail (EV_IS_SIDEBAR_PAGE (sidebar_page));
678 g_return_if_fail (EV_IS_DOCUMENT (document));
680 sidebar_links = EV_SIDEBAR_LINKS (sidebar_page);
682 priv = sidebar_links->priv;
684 if (priv->document) {
685 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
686 g_object_unref (priv->document);
689 priv->document = g_object_ref (document);
690 priv->page_cache = ev_page_cache_get (document);
693 g_signal_handlers_disconnect_by_func (priv->job,
694 job_finished_callback,
696 g_object_unref (priv->job);
699 priv->job = ev_job_links_new (document);
700 g_signal_connect (priv->job,
702 G_CALLBACK (job_finished_callback),
704 /* The priority doesn't matter for this job */
705 ev_job_scheduler_push_job (priv->job, EV_JOB_PRIORITY_NONE);
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 (EvSidebarPageIface *iface)
725 iface->support_document = ev_sidebar_links_support_document;
726 iface->set_document = ev_sidebar_links_set_document;
727 iface->get_label = ev_sidebar_links_get_label;