]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-action.c
97abd1a7870a6dfdfc4a39074a11e4d4bb53f2d8
[evince.git] / shell / ev-page-action.c
1 /*
2  *  Copyright (C) 2003, 2004 Marco Pesenti Gritti
3  *  Copyright (C) 2003, 2004 Christian Persch
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  *  $Id$
20  */
21
22 #include "config.h"
23
24 #include "ev-page-action.h"
25 #include "ev-page-cache.h"
26 #include "ev-window.h"
27 #include "ev-document-links.h"
28 #include "ev-marshal.h"
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtkentry.h>
32 #include <gtk/gtktoolitem.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkhbox.h>
35 #include <string.h>
36
37 typedef struct _EvPageActionWidget EvPageActionWidget;
38 typedef struct _EvPageActionWidgetClass EvPageActionWidgetClass;
39 struct _EvPageActionWidget
40 {
41         GtkToolItem parent;
42
43         GtkWidget *entry;
44         GtkWidget *label;
45         EvPageCache *page_cache;
46         guint signal_id;
47         GtkTreeModel *filter_model;
48         GtkTreeModel *model;
49 };
50
51 struct _EvPageActionWidgetClass
52 {
53         GtkToolItemClass parent_class;
54
55         void (* activate_link) (EvPageActionWidget *page_action,
56                                 EvLink             *link);
57 };
58
59 struct _EvPageActionPrivate
60 {
61         EvPageCache *page_cache;
62         GtkTreeModel *model;
63 };
64
65
66 /* Widget we pass back */
67 static GType ev_page_action_widget_get_type   (void);
68 static void  ev_page_action_widget_init       (EvPageActionWidget      *action_widget);
69 static void  ev_page_action_widget_class_init (EvPageActionWidgetClass *action_widget);
70
71 #define EV_TYPE_PAGE_ACTION_WIDGET (ev_page_action_widget_get_type ())
72 #define EV_PAGE_ACTION_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_PAGE_ACTION_WIDGET, EvPageActionWidget))
73
74 enum
75 {
76         WIDGET_ACTIVATE_LINK,
77         WIDGET_N_SIGNALS
78 };
79
80 static guint widget_signals[WIDGET_N_SIGNALS] = {0, };
81
82 G_DEFINE_TYPE (EvPageActionWidget, ev_page_action_widget, GTK_TYPE_TOOL_ITEM)
83
84 static void
85 ev_page_action_widget_init (EvPageActionWidget *action_widget)
86 {
87
88 }
89
90 static void
91 ev_page_action_widget_set_page_cache (EvPageActionWidget *action_widget,
92                                       EvPageCache        *page_cache)
93 {
94         if (action_widget->page_cache != NULL) {
95                 g_object_remove_weak_pointer (G_OBJECT (action_widget->page_cache),
96                                               (gpointer *)&action_widget->page_cache);
97                 action_widget->page_cache = NULL;
98         }
99
100         if (page_cache != NULL) {
101                 action_widget->page_cache = page_cache;
102                 g_object_add_weak_pointer (G_OBJECT (page_cache),
103                                            (gpointer *)&action_widget->page_cache);
104         }
105 }
106
107 static void
108 ev_page_action_widget_finalize (GObject *object)
109 {
110         EvPageActionWidget *action_widget = EV_PAGE_ACTION_WIDGET (object);
111
112         ev_page_action_widget_set_page_cache (action_widget, NULL);
113 }
114
115 static void
116 ev_page_action_widget_class_init (EvPageActionWidgetClass *class)
117 {
118         GObjectClass *object_class = G_OBJECT_CLASS (class);
119
120         object_class->finalize = ev_page_action_widget_finalize;
121
122         widget_signals[WIDGET_ACTIVATE_LINK] = g_signal_new ("activate_link",
123                                                G_OBJECT_CLASS_TYPE (object_class),
124                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
125                                                G_STRUCT_OFFSET (EvPageActionClass, activate_link),
126                                                NULL, NULL,
127                                                g_cclosure_marshal_VOID__OBJECT,
128                                                G_TYPE_NONE, 1,
129                                                G_TYPE_OBJECT);
130
131 }
132
133 static void ev_page_action_init       (EvPageAction *action);
134 static void ev_page_action_class_init (EvPageActionClass *class);
135
136 enum
137 {
138         ACTIVATE_LINK,
139         ACTIVATE_LABEL,
140         N_SIGNALS
141 };
142
143 static guint signals[N_SIGNALS] = {0, };
144
145 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
146
147 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
148
149 enum {
150         PROP_0,
151         PROP_PAGE_CACHE,
152         PROP_MODEL,
153 };
154
155 /* user data to set on the widget. */
156 #define EPA_FILTER_MODEL_DATA "epa-filter-model"
157
158 static void
159 update_pages_label (EvPageActionWidget *proxy,
160                     gint                page,
161                     EvPageCache        *page_cache)
162 {
163         char *label_text;
164         gint n_pages;
165
166         n_pages = page_cache ? ev_page_cache_get_n_pages (page_cache) : 0;
167         if (page_cache && ev_page_cache_has_nonnumeric_page_labels (page_cache)) {
168                 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
169         } else {
170                 label_text = g_strdup_printf (_("of %d"), n_pages);
171         }
172         gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
173         g_free (label_text);
174 }
175
176 static void
177 page_changed_cb (EvPageCache        *page_cache,
178                  gint                page,
179                  EvPageActionWidget *proxy)
180 {
181         g_assert (proxy);
182         
183         if (page_cache != NULL && page >= 0) {
184                 gchar *page_label;
185
186                 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 
187                                            CLAMP (ev_page_cache_get_max_label_chars (page_cache), 
188                                            6, 12));     
189                 
190                 page_label = ev_page_cache_get_page_label (page_cache, page);
191                 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
192                 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
193                 g_free (page_label);
194                 
195         } else {
196                 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
197         }
198
199         update_pages_label (proxy, page, page_cache);
200 }
201
202 static void
203 activate_cb (GtkWidget *entry, GtkAction *action)
204 {
205         EvPageAction *page = EV_PAGE_ACTION (action);
206         EvPageCache *page_cache;
207         const char *text;
208         gboolean changed;
209
210         text = gtk_entry_get_text (GTK_ENTRY (entry));
211         page_cache = page->priv->page_cache;
212
213         g_signal_emit (action, signals[ACTIVATE_LABEL], 0, text, &changed);
214
215         if (!changed) {
216                 /* rest the entry to the current page if we were unable to
217                  * change it */
218                 gchar *page_label =
219                         ev_page_cache_get_page_label (page_cache,
220                                                       ev_page_cache_get_current_page (page_cache));
221                 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
222                 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
223                 g_free (page_label);
224         }
225 }
226
227 static GtkWidget *
228 create_tool_item (GtkAction *action)
229 {
230         EvPageActionWidget *proxy;
231         GtkWidget *hbox;
232
233         proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
234         gtk_container_set_border_width (GTK_CONTAINER (proxy), 6); 
235         gtk_widget_show (GTK_WIDGET (proxy));
236
237         hbox = gtk_hbox_new (FALSE, 0);
238         gtk_box_set_spacing (GTK_BOX (hbox), 6);
239
240         proxy->entry = gtk_entry_new ();
241         gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
242         gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
243         gtk_widget_show (proxy->entry);
244         g_signal_connect (proxy->entry, "activate",
245                           G_CALLBACK (activate_cb),
246                           action);
247
248         proxy->label = gtk_label_new (NULL);
249         gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
250         gtk_widget_show (proxy->label);
251
252         gtk_container_add (GTK_CONTAINER (proxy), hbox);
253         gtk_widget_show (hbox);
254
255         return GTK_WIDGET (proxy);
256 }
257
258 static void
259 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
260 {
261         EvPageCache *page_cache;
262         guint signal_id;
263
264         page_cache = page->priv->page_cache;
265
266         /* clear the old signal */
267         if (proxy->signal_id > 0 && proxy->page_cache)
268                 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
269         
270         if (page_cache != NULL) {
271                 signal_id = g_signal_connect_object (page_cache,
272                                                      "page-changed",
273                                                      G_CALLBACK (page_changed_cb),
274                                                      proxy, 0);
275                 /* Set the initial value */
276                 page_changed_cb (page_cache,
277                                  ev_page_cache_get_current_page (page_cache),
278                                  proxy);
279         } else {
280                 /* Or clear the entry */
281                 signal_id = 0;
282                 page_changed_cb (NULL, 0, proxy);
283         }
284         ev_page_action_widget_set_page_cache (proxy, page_cache);
285         proxy->signal_id = signal_id;
286 }
287
288 static gboolean
289 build_new_tree_cb (GtkTreeModel *model,
290                    GtkTreePath  *path,
291                    GtkTreeIter  *iter,
292                    gpointer      data)
293 {
294         GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
295         EvLink *link;
296         EvLinkAction *action;
297         EvLinkActionType type;
298
299         gtk_tree_model_get (model, iter,
300                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
301                             -1);
302
303         if (!link)
304                 return FALSE;
305
306         action = ev_link_get_action (link);
307         if (!action) {
308                 g_object_unref (link);
309                 return FALSE;
310         }
311         
312         type = ev_link_action_get_action_type (action);
313
314         if (type == EV_LINK_ACTION_TYPE_GOTO_DEST) {
315                 GtkTreeIter filter_iter;
316
317                 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
318                 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
319                                     0, iter,
320                                     -1);
321         }
322         
323         g_object_unref (link);
324         
325         return FALSE;
326 }
327
328 static GtkTreeModel *
329 get_filter_model_from_model (GtkTreeModel *model)
330 {
331         GtkTreeModel *filter_model;
332
333         filter_model =
334                 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
335         if (filter_model == NULL) {
336                 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
337
338                 gtk_tree_model_foreach (model,
339                                         build_new_tree_cb,
340                                         filter_model);
341                 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
342         }
343
344         return filter_model;
345 }
346
347 static gboolean
348 match_selected_cb (GtkEntryCompletion *completion,
349                    GtkTreeModel       *filter_model,
350                    GtkTreeIter        *filter_iter,
351                    EvPageActionWidget *proxy)
352 {
353         EvLink *link;
354         GtkTreeIter *iter;
355
356         gtk_tree_model_get (filter_model, filter_iter,
357                             0, &iter,
358                             -1);
359         gtk_tree_model_get (proxy->model, iter,
360                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
361                             -1);
362
363         g_signal_emit (proxy, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
364
365         if (link)
366                 g_object_unref (link);
367
368         gtk_tree_iter_free (iter);
369         
370         return TRUE;
371 }
372                    
373
374 static void
375 display_completion_text (GtkCellLayout      *cell_layout,
376                          GtkCellRenderer    *renderer,
377                          GtkTreeModel       *filter_model,
378                          GtkTreeIter        *filter_iter,
379                          EvPageActionWidget *proxy)
380 {
381         EvLink *link;
382         GtkTreeIter *iter;
383
384         gtk_tree_model_get (filter_model, filter_iter,
385                             0, &iter,
386                             -1);
387         gtk_tree_model_get (proxy->model, iter,
388                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
389                             -1);
390
391         g_object_set (renderer, "text", ev_link_get_title (link), NULL);
392
393         if (link)
394                 g_object_unref (link);
395         
396         gtk_tree_iter_free (iter);
397 }
398
399 static gboolean
400 match_completion (GtkEntryCompletion *completion,
401                   const gchar        *key,
402                   GtkTreeIter        *filter_iter,
403                   EvPageActionWidget *proxy)
404 {
405         EvLink *link;
406         GtkTreeIter *iter;
407         const gchar *text = NULL;
408
409         gtk_tree_model_get (gtk_entry_completion_get_model (completion),
410                             filter_iter,
411                             0, &iter,
412                             -1);
413         gtk_tree_model_get (proxy->model, iter,
414                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
415                             -1);
416
417
418         if (link) {
419                 text = ev_link_get_title (link);
420                 g_object_unref (link);
421         }
422
423         gtk_tree_iter_free (iter);
424
425         if (text && key) {
426                 gchar *normalized_text;
427                 gchar *normalized_key;
428                 gchar *case_normalized_text;
429                 gchar *case_normalized_key;
430                 gboolean retval = FALSE;
431
432                 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
433                 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
434                 case_normalized_text = g_utf8_casefold (normalized_text, -1);
435                 case_normalized_key = g_utf8_casefold (normalized_key, -1);
436
437                 if (strstr (case_normalized_text, case_normalized_key))
438                         retval = TRUE;
439
440                 g_free (normalized_text);
441                 g_free (normalized_key);
442                 g_free (case_normalized_text);
443                 g_free (case_normalized_key);
444
445                 return retval;
446         }
447
448         return FALSE;
449 }
450
451
452 static void
453 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
454 {
455         GtkTreeModel *model;
456         GtkTreeModel *filter_model;
457
458         g_object_get (G_OBJECT (page),
459                       "model", &model,
460                       NULL);
461         if (model != NULL) {
462                 /* Magik */
463                 GtkEntryCompletion *completion;
464                 GtkCellRenderer *renderer;
465
466                 proxy->model = model;
467                 filter_model = get_filter_model_from_model (model);
468
469                 completion = gtk_entry_completion_new ();
470
471                 /* popup-set-width is 2.7.0 only */
472                 g_object_set (G_OBJECT (completion),
473                               "popup-set-width", FALSE,
474                               "model", filter_model,
475                               NULL);
476
477                 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
478                 gtk_entry_completion_set_match_func (completion,
479                                                      (GtkEntryCompletionMatchFunc) match_completion,
480                                                      proxy, NULL);
481
482                 /* Set up the layout */
483                 renderer = (GtkCellRenderer *)
484                         g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
485                                       "ellipsize", PANGO_ELLIPSIZE_END,
486                                       "width_chars", 30,
487                                       NULL);
488                 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
489                 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
490                                                     renderer,
491                                                     (GtkCellLayoutDataFunc) display_completion_text,
492                                                     proxy, NULL);
493                 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
494                 
495                 g_object_unref (completion);
496                 g_object_unref (model);
497         }
498 }
499
500 static void
501 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
502 {
503         g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
504 }
505
506 static void
507 connect_proxy (GtkAction *action, GtkWidget *proxy)
508 {
509         if (GTK_IS_TOOL_ITEM (proxy)) {
510                 g_signal_connect_object (action, "notify::page-cache",
511                                          G_CALLBACK (update_page_cache),
512                                          proxy, 0);
513                 g_signal_connect (proxy, "activate_link",
514                                   G_CALLBACK (activate_link_cb),
515                                   action);
516                 update_page_cache (EV_PAGE_ACTION (action), NULL,
517                                    EV_PAGE_ACTION_WIDGET (proxy));
518                 /* We only go through this whole rigmarole if we can set
519                  * GtkEntryCompletion::popup-set-width, which appeared in
520                  * GTK+-2.7.0 */
521                 if (gtk_check_version (2, 7, 0) == NULL) {
522                         g_signal_connect_object (action, "notify::model",
523                                                  G_CALLBACK (update_model),
524                                                  proxy, 0);
525                 }
526         }
527
528         GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
529 }
530
531 static void
532 ev_page_action_dispose (GObject *object)
533 {
534         EvPageAction *page = EV_PAGE_ACTION (object);
535
536         if (page->priv->page_cache) {
537                 g_object_unref (page->priv->page_cache);
538                 page->priv->page_cache = NULL;
539         }
540
541         G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
542 }
543
544 static void
545 ev_page_action_set_property (GObject      *object,
546                              guint         prop_id,
547                              const GValue *value,
548                              GParamSpec   *pspec)
549 {
550         EvPageAction *page;
551         EvPageCache *page_cache;
552         GtkTreeModel *model;
553   
554         page = EV_PAGE_ACTION (object);
555
556         switch (prop_id)
557         {
558         case PROP_PAGE_CACHE:
559                 page_cache = page->priv->page_cache;
560                 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
561                 if (page_cache)
562                         g_object_unref (page_cache);
563                 break;
564         case PROP_MODEL:
565                 model = page->priv->model;
566                 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
567                 if (model)
568                         g_object_unref (model);
569                 break;
570         default:
571                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
572                 break;
573         }
574 }
575
576 static void
577 ev_page_action_get_property (GObject    *object,
578                              guint       prop_id,
579                              GValue     *value,
580                              GParamSpec *pspec)
581 {
582         EvPageAction *page;
583   
584         page = EV_PAGE_ACTION (object);
585
586         switch (prop_id)
587         {
588         case PROP_PAGE_CACHE:
589                 g_value_set_object (value, page->priv->page_cache);
590                 break;
591         case PROP_MODEL:
592                 g_value_set_object (value, page->priv->model);
593                 break;
594         default:
595                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
596                 break;
597         }
598 }
599
600 void
601 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
602 {
603         EvPageCache *page_cache = NULL;
604
605         if (document)
606                 page_cache = ev_page_cache_get (document);
607         
608         g_object_set (page,
609                       "page-cache", page_cache,
610                       "model", NULL,
611                       NULL);
612 }
613
614 void
615 ev_page_action_set_model (EvPageAction *page_action,
616                           GtkTreeModel *model)
617 {
618         g_object_set (page_action,
619                       "model", model,
620                       NULL);
621 }
622
623 void
624 ev_page_action_grab_focus (EvPageAction *page_action)
625 {
626         GSList *proxies;
627
628         proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
629         for (; proxies != NULL; proxies = proxies->next) {
630                 EvPageActionWidget *proxy;
631
632                 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
633                 gtk_widget_grab_focus (proxy->entry);
634         }
635 }
636
637 static void
638 ev_page_action_init (EvPageAction *page)
639 {
640         page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
641 }
642
643 static void
644 ev_page_action_class_init (EvPageActionClass *class)
645 {
646         GObjectClass *object_class = G_OBJECT_CLASS (class);
647         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
648
649         object_class->dispose = ev_page_action_dispose;
650         object_class->set_property = ev_page_action_set_property;
651         object_class->get_property = ev_page_action_get_property;
652
653         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
654         action_class->create_tool_item = create_tool_item;
655         action_class->connect_proxy = connect_proxy;
656
657         signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
658                                                G_OBJECT_CLASS_TYPE (object_class),
659                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
660                                                G_STRUCT_OFFSET (EvPageActionClass, activate_link),
661                                                NULL, NULL,
662                                                g_cclosure_marshal_VOID__OBJECT,
663                                                G_TYPE_NONE, 1,
664                                                G_TYPE_OBJECT);
665         signals[ACTIVATE_LABEL] = g_signal_new ("activate_label",
666                                                 G_OBJECT_CLASS_TYPE (object_class),
667                                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
668                                                 G_STRUCT_OFFSET (EvPageActionClass, activate_label),
669                                                 NULL, NULL,
670                                                 ev_marshal_BOOLEAN__STRING,
671                                                 G_TYPE_BOOLEAN, 1,
672                                                 G_TYPE_STRING);
673
674         g_object_class_install_property (object_class,
675                                          PROP_PAGE_CACHE,
676                                          g_param_spec_object ("page-cache",
677                                                               "Page Cache",
678                                                               "Current page cache",
679                                                               EV_TYPE_PAGE_CACHE,
680                                                               G_PARAM_READWRITE));
681
682         g_object_class_install_property (object_class,
683                                          PROP_MODEL,
684                                          g_param_spec_object ("model",
685                                                               "Model",
686                                                               "Current Model",
687                                                               GTK_TYPE_TREE_MODEL,
688                                                               G_PARAM_READWRITE));
689
690         g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));
691 }