]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-action.c
Translation updated by Tino Meinen.
[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         
185                 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 
186                                            CLAMP (ev_page_cache_get_max_label_chars (page_cache), 
187                                            4, 12));     
188                 
189                 gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
190                 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
191                 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
192                 g_free (page_label);
193                 
194         } else {
195                 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
196         }
197
198         update_pages_label (proxy, page, page_cache);
199 }
200
201 static void
202 activate_cb (GtkWidget *entry, GtkAction *action)
203 {
204         EvPageAction *page = EV_PAGE_ACTION (action);
205         EvPageCache *page_cache;
206         const char *text;
207         gboolean changed;
208
209         text = gtk_entry_get_text (GTK_ENTRY (entry));
210         page_cache = page->priv->page_cache;
211
212         g_signal_emit (action, signals[ACTIVATE_LABEL], 0, text, &changed);
213
214         if (!changed) {
215                 /* rest the entry to the current page if we were unable to
216                  * change it */
217                 gchar *page_label =
218                         ev_page_cache_get_page_label (page_cache,
219                                                       ev_page_cache_get_current_page (page_cache));
220                 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
221                 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
222                 g_free (page_label);
223         }
224 }
225
226 static GtkWidget *
227 create_tool_item (GtkAction *action)
228 {
229         EvPageActionWidget *proxy;
230         GtkWidget *hbox;
231
232         proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
233         gtk_container_set_border_width (GTK_CONTAINER (proxy), 6); 
234         gtk_widget_show (GTK_WIDGET (proxy));
235
236         hbox = gtk_hbox_new (FALSE, 0);
237         gtk_box_set_spacing (GTK_BOX (hbox), 6);
238
239         proxy->entry = gtk_entry_new ();
240         gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
241         gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
242         gtk_widget_show (proxy->entry);
243         g_signal_connect (proxy->entry, "activate",
244                           G_CALLBACK (activate_cb),
245                           action);
246
247         proxy->label = gtk_label_new (NULL);
248         gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
249         gtk_widget_show (proxy->label);
250
251         gtk_container_add (GTK_CONTAINER (proxy), hbox);
252         gtk_widget_show (hbox);
253
254         return GTK_WIDGET (proxy);
255 }
256
257 static void
258 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
259 {
260         EvPageCache *page_cache;
261         guint signal_id;
262
263         page_cache = page->priv->page_cache;
264
265         /* clear the old signal */
266         if (proxy->signal_id > 0 && proxy->page_cache)
267                 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
268         
269         if (page_cache != NULL) {
270                 signal_id = g_signal_connect_object (page_cache,
271                                                      "page-changed",
272                                                      G_CALLBACK (page_changed_cb),
273                                                      proxy, 0);
274                 /* Set the initial value */
275                 page_changed_cb (page_cache,
276                                  ev_page_cache_get_current_page (page_cache),
277                                  proxy);
278         } else {
279                 /* Or clear the entry */
280                 signal_id = 0;
281                 page_changed_cb (NULL, 0, proxy);
282         }
283         ev_page_action_widget_set_page_cache (proxy, page_cache);
284         proxy->signal_id = signal_id;
285 }
286
287 static gboolean
288 build_new_tree_cb (GtkTreeModel *model,
289                    GtkTreePath  *path,
290                    GtkTreeIter  *iter,
291                    gpointer      data)
292 {
293         GtkTreeModel *filter_model = GTK_TREE_MODEL (data);
294         EvLink *link;
295
296         gtk_tree_model_get (model, iter,
297                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
298                             -1);
299
300         if (link && ev_link_get_link_type (link) == EV_LINK_TYPE_PAGE) {
301                 GtkTreeIter filter_iter;
302
303                 gtk_list_store_append (GTK_LIST_STORE (filter_model), &filter_iter);
304                 gtk_list_store_set (GTK_LIST_STORE (filter_model), &filter_iter,
305                                     0, iter,
306                                     -1);
307         }
308         
309         if (link)
310                 g_object_unref (link);
311         
312         return FALSE;
313 }
314
315 static GtkTreeModel *
316 get_filter_model_from_model (GtkTreeModel *model)
317 {
318         GtkTreeModel *filter_model;
319
320         filter_model =
321                 (GtkTreeModel *) g_object_get_data (G_OBJECT (model), EPA_FILTER_MODEL_DATA);
322         if (filter_model == NULL) {
323                 filter_model = (GtkTreeModel *) gtk_list_store_new (1, GTK_TYPE_TREE_ITER);
324
325                 gtk_tree_model_foreach (model,
326                                         build_new_tree_cb,
327                                         filter_model);
328                 g_object_set_data_full (G_OBJECT (model), EPA_FILTER_MODEL_DATA, filter_model, g_object_unref);
329         }
330
331         return filter_model;
332 }
333
334 static gboolean
335 match_selected_cb (GtkEntryCompletion *completion,
336                    GtkTreeModel       *filter_model,
337                    GtkTreeIter        *filter_iter,
338                    EvPageActionWidget *proxy)
339 {
340         EvLink *link;
341         GtkTreeIter *iter;
342
343         gtk_tree_model_get (filter_model, filter_iter,
344                             0, &iter,
345                             -1);
346         gtk_tree_model_get (proxy->model, iter,
347                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
348                             -1);
349
350         g_signal_emit (proxy, widget_signals[WIDGET_ACTIVATE_LINK], 0, link);
351
352         if (link)
353                 g_object_unref (link);
354
355         gtk_tree_iter_free (iter);
356         
357         return TRUE;
358 }
359                    
360
361 static void
362 display_completion_text (GtkCellLayout      *cell_layout,
363                          GtkCellRenderer    *renderer,
364                          GtkTreeModel       *filter_model,
365                          GtkTreeIter        *filter_iter,
366                          EvPageActionWidget *proxy)
367 {
368         EvLink *link;
369         GtkTreeIter *iter;
370
371         gtk_tree_model_get (filter_model, filter_iter,
372                             0, &iter,
373                             -1);
374         gtk_tree_model_get (proxy->model, iter,
375                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
376                             -1);
377
378         g_object_set (renderer, "text", ev_link_get_title (link), NULL);
379
380         if (link)
381                 g_object_unref (link);
382         
383         gtk_tree_iter_free (iter);
384 }
385
386 static gboolean
387 match_completion (GtkEntryCompletion *completion,
388                   const gchar        *key,
389                   GtkTreeIter        *filter_iter,
390                   EvPageActionWidget *proxy)
391 {
392         EvLink *link;
393         GtkTreeIter *iter;
394         const gchar *text = NULL;
395
396         gtk_tree_model_get (gtk_entry_completion_get_model (completion),
397                             filter_iter,
398                             0, &iter,
399                             -1);
400         gtk_tree_model_get (proxy->model, iter,
401                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
402                             -1);
403
404
405         if (link) {
406                 text = ev_link_get_title (link);
407                 g_object_unref (link);
408         }
409
410         gtk_tree_iter_free (iter);
411
412         if (text && key ) {
413                 gchar *normalized_text;
414                 gchar *normalized_key;
415                 gchar *case_normalized_text;
416                 gchar *case_normalized_key;
417                 gboolean retval = FALSE;
418
419                 normalized_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
420                 normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
421                 case_normalized_text = g_utf8_casefold (normalized_text, -1);
422                 case_normalized_key = g_utf8_casefold (normalized_key, -1);
423
424                 if (strstr (case_normalized_text, case_normalized_key))
425                         retval = TRUE;
426
427                 g_free (normalized_text);
428                 g_free (normalized_key);
429                 g_free (case_normalized_text);
430                 g_free (case_normalized_key);
431
432                 return retval;
433         }
434
435         return FALSE;
436 }
437
438
439 static void
440 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
441 {
442         GtkTreeModel *model;
443         GtkTreeModel *filter_model;
444
445         g_object_get (G_OBJECT (page),
446                       "model", &model,
447                       NULL);
448         if (model != NULL) {
449                 /* Magik */
450                 GtkEntryCompletion *completion;
451                 GtkCellRenderer *renderer;
452
453                 proxy->model = model;
454                 filter_model = get_filter_model_from_model (model);
455
456                 completion = gtk_entry_completion_new ();
457
458                 /* popup-set-width is 2.7.0 only */
459                 g_object_set (G_OBJECT (completion),
460                               "popup-set-width", FALSE,
461                               "model", filter_model,
462                               NULL);
463
464                 g_signal_connect (completion, "match-selected", G_CALLBACK (match_selected_cb), proxy);
465                 gtk_entry_completion_set_match_func (completion,
466                                                      (GtkEntryCompletionMatchFunc) match_completion,
467                                                      proxy, NULL);
468
469                 /* Set up the layout */
470                 renderer = (GtkCellRenderer *)
471                         g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
472                                       "ellipsize", PANGO_ELLIPSIZE_END,
473                                       "width_chars", 30,
474                                       NULL);
475                 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
476                 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
477                                                     renderer,
478                                                     (GtkCellLayoutDataFunc) display_completion_text,
479                                                     proxy, NULL);
480                 gtk_entry_set_completion (GTK_ENTRY (proxy->entry), completion);
481
482                 g_object_unref (model);
483         }
484 }
485
486 static void
487 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
488 {
489         g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
490 }
491
492 static void
493 connect_proxy (GtkAction *action, GtkWidget *proxy)
494 {
495         if (GTK_IS_TOOL_ITEM (proxy)) {
496                 g_signal_connect_object (action, "notify::page-cache",
497                                          G_CALLBACK (update_page_cache),
498                                          proxy, 0);
499                 g_signal_connect (proxy, "activate_link",
500                                   G_CALLBACK (activate_link_cb),
501                                   action);
502                 update_page_cache (EV_PAGE_ACTION (action), NULL,
503                                    EV_PAGE_ACTION_WIDGET (proxy));
504                 /* We only go through this whole rigmarole if we can set
505                  * GtkEntryCompletion::popup-set-width, which appeared in
506                  * GTK+-2.7.0 */
507                 if (gtk_check_version (2, 7, 0) == NULL) {
508                         g_signal_connect_object (action, "notify::model",
509                                                  G_CALLBACK (update_model),
510                                                  proxy, 0);
511                 }
512         }
513
514         GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
515 }
516
517 static void
518 ev_page_action_dispose (GObject *object)
519 {
520         EvPageAction *page = EV_PAGE_ACTION (object);
521
522         if (page->priv->page_cache) {
523                 g_object_unref (page->priv->page_cache);
524                 page->priv->page_cache = NULL;
525         }
526
527         G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
528 }
529
530 static void
531 ev_page_action_set_property (GObject      *object,
532                              guint         prop_id,
533                              const GValue *value,
534                              GParamSpec   *pspec)
535 {
536         EvPageAction *page;
537         EvPageCache *page_cache;
538         GtkTreeModel *model;
539   
540         page = EV_PAGE_ACTION (object);
541
542         switch (prop_id)
543         {
544         case PROP_PAGE_CACHE:
545                 page_cache = page->priv->page_cache;
546                 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
547                 if (page_cache)
548                         g_object_unref (page_cache);
549                 break;
550         case PROP_MODEL:
551                 model = page->priv->model;
552                 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
553                 if (model)
554                         g_object_unref (model);
555                 break;
556         default:
557                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558                 break;
559         }
560 }
561
562 static void
563 ev_page_action_get_property (GObject    *object,
564                              guint       prop_id,
565                              GValue     *value,
566                              GParamSpec *pspec)
567 {
568         EvPageAction *page;
569   
570         page = EV_PAGE_ACTION (object);
571
572         switch (prop_id)
573         {
574         case PROP_PAGE_CACHE:
575                 g_value_set_object (value, page->priv->page_cache);
576                 break;
577         case PROP_MODEL:
578                 g_value_set_object (value, page->priv->model);
579                 break;
580         default:
581                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
582                 break;
583         }
584 }
585
586 void
587 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
588 {
589         EvPageCache *page_cache = NULL;
590
591         if (document)
592                 page_cache = ev_page_cache_get (document);
593         
594         g_object_set (page,
595                       "page-cache", page_cache,
596                       "model", NULL,
597                       NULL);
598 }
599
600 void
601 ev_page_action_set_model (EvPageAction *page_action,
602                           GtkTreeModel *model)
603 {
604         g_object_set (page_action,
605                       "model", model,
606                       NULL);
607 }
608
609 void
610 ev_page_action_grab_focus (EvPageAction *page_action)
611 {
612         GSList *proxies;
613
614         proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
615         for (; proxies != NULL; proxies = proxies->next) {
616                 EvPageActionWidget *proxy;
617
618                 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
619                 gtk_widget_grab_focus (proxy->entry);
620         }
621 }
622
623 static void
624 ev_page_action_init (EvPageAction *page)
625 {
626         page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
627 }
628
629 static void
630 ev_page_action_class_init (EvPageActionClass *class)
631 {
632         GObjectClass *object_class = G_OBJECT_CLASS (class);
633         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
634
635         object_class->dispose = ev_page_action_dispose;
636         object_class->set_property = ev_page_action_set_property;
637         object_class->get_property = ev_page_action_get_property;
638
639         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
640         action_class->create_tool_item = create_tool_item;
641         action_class->connect_proxy = connect_proxy;
642
643         signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
644                                                G_OBJECT_CLASS_TYPE (object_class),
645                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
646                                                G_STRUCT_OFFSET (EvPageActionClass, activate_link),
647                                                NULL, NULL,
648                                                g_cclosure_marshal_VOID__OBJECT,
649                                                G_TYPE_NONE, 1,
650                                                G_TYPE_OBJECT);
651         signals[ACTIVATE_LABEL] = g_signal_new ("activate_label",
652                                                 G_OBJECT_CLASS_TYPE (object_class),
653                                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
654                                                 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
655                                                 NULL, NULL,
656                                                 ev_marshal_BOOLEAN__STRING,
657                                                 G_TYPE_BOOLEAN, 1,
658                                                 G_TYPE_STRING);
659
660         g_object_class_install_property (object_class,
661                                          PROP_PAGE_CACHE,
662                                          g_param_spec_object ("page-cache",
663                                                               "Page Cache",
664                                                               "Current page cache",
665                                                               EV_TYPE_PAGE_CACHE,
666                                                               G_PARAM_READWRITE));
667
668         g_object_class_install_property (object_class,
669                                          PROP_MODEL,
670                                          g_param_spec_object ("model",
671                                                               "Model",
672                                                               "Current Model",
673                                                               GTK_TYPE_TREE_MODEL,
674                                                               G_PARAM_READWRITE));
675
676         g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));
677 }