]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-action.c
Fixes program description translation issue. Bug #450148.
[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  */
20
21 #include "config.h"
22
23 #include "ev-page-action.h"
24 #include "ev-page-cache.h"
25 #include "ev-window.h"
26 #include "ev-document-links.h"
27 #include "ev-page-action-widget.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 #include <stdlib.h>
37
38 struct _EvPageActionPrivate
39 {
40         EvPageCache *page_cache;
41         GtkTreeModel *model;
42 };
43
44
45 static void ev_page_action_init       (EvPageAction *action);
46 static void ev_page_action_class_init (EvPageActionClass *class);
47
48 enum
49 {
50         ACTIVATE_LINK,
51         N_SIGNALS
52 };
53
54 static guint signals[N_SIGNALS] = {0, };
55
56 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
57
58 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
59
60 enum {
61         PROP_0,
62         PROP_PAGE_CACHE,
63         PROP_MODEL,
64 };
65
66 static void
67 update_pages_label (EvPageActionWidget *proxy,
68                     gint                page,
69                     EvPageCache        *page_cache)
70 {
71         char *label_text;
72         gint n_pages;
73
74         n_pages = page_cache ? ev_page_cache_get_n_pages (page_cache) : 0;
75         if (page_cache && ev_page_cache_has_nonnumeric_page_labels (page_cache)) {
76                 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
77         } else {
78                 label_text = g_strdup_printf (_("of %d"), n_pages);
79         }
80         gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
81         g_free (label_text);
82 }
83
84 static void
85 page_changed_cb (EvPageCache        *page_cache,
86                  gint                page,
87                  EvPageActionWidget *proxy)
88 {
89         g_assert (proxy);
90         
91         if (page_cache != NULL && page >= 0) {
92                 gchar *page_label;
93
94                 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 
95                                            CLAMP (ev_page_cache_get_max_label_chars (page_cache), 
96                                            6, 12));     
97                 
98                 page_label = ev_page_cache_get_page_label (page_cache, page);
99                 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
100                 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
101                 g_free (page_label);
102                 
103         } else {
104                 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
105         }
106
107         update_pages_label (proxy, page, page_cache);
108 }
109
110 static void
111 activate_cb (GtkWidget *entry, GtkAction *action)
112 {
113         EvPageAction *page = EV_PAGE_ACTION (action);
114         EvPageCache *page_cache;
115         const char *text;
116         gchar *page_label;
117         
118         EvLinkDest *link_dest;
119         EvLinkAction *link_action;
120         EvLink *link;
121         gchar *link_text;
122
123         text = gtk_entry_get_text (GTK_ENTRY (entry));
124         page_cache = page->priv->page_cache;
125
126         
127         link_dest = ev_link_dest_new_page_label (text);
128         link_action = ev_link_action_new_dest (link_dest);
129         link_text = g_strdup_printf ("Page: %s", text);
130         link = ev_link_new (link_text, link_action);
131
132         g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
133
134         g_object_unref (link);
135         g_free (link_text);
136         
137         /* rest the entry to the current page if we were unable to
138          * change it */
139         page_label = ev_page_cache_get_page_label (page_cache,
140                                                    ev_page_cache_get_current_page (page_cache));
141         gtk_entry_set_text (GTK_ENTRY (entry), page_label);
142         gtk_editable_set_position (GTK_EDITABLE (entry), -1);
143         g_free (page_label);
144 }
145
146 static GtkWidget *
147 create_tool_item (GtkAction *action)
148 {
149         EvPageActionWidget *proxy;
150         GtkWidget *hbox;
151
152         proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
153         gtk_container_set_border_width (GTK_CONTAINER (proxy), 6); 
154         gtk_widget_show (GTK_WIDGET (proxy));
155
156         hbox = gtk_hbox_new (FALSE, 0);
157         gtk_box_set_spacing (GTK_BOX (hbox), 6);
158
159         proxy->entry = gtk_entry_new ();
160         gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
161         gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
162         gtk_widget_show (proxy->entry);
163         g_signal_connect (proxy->entry, "activate",
164                           G_CALLBACK (activate_cb),
165                           action);
166
167         proxy->label = gtk_label_new (NULL);
168         gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
169         gtk_widget_show (proxy->label);
170
171         gtk_container_add (GTK_CONTAINER (proxy), hbox);
172         gtk_widget_show (hbox);
173
174         return GTK_WIDGET (proxy);
175 }
176
177 static void
178 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
179 {
180         EvPageCache *page_cache;
181         guint signal_id;
182
183         page_cache = page->priv->page_cache;
184
185         /* clear the old signal */
186         if (proxy->signal_id > 0 && proxy->page_cache)
187                 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
188         
189         if (page_cache != NULL) {
190                 signal_id = g_signal_connect_object (page_cache,
191                                                      "page-changed",
192                                                      G_CALLBACK (page_changed_cb),
193                                                      proxy, 0);
194                 /* Set the initial value */
195                 page_changed_cb (page_cache,
196                                  ev_page_cache_get_current_page (page_cache),
197                                  proxy);
198         } else {
199                 /* Or clear the entry */
200                 signal_id = 0;
201                 page_changed_cb (NULL, 0, proxy);
202         }
203         ev_page_action_widget_set_page_cache (proxy, page_cache);
204         proxy->signal_id = signal_id;
205 }
206
207 static void
208 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
209 {
210         g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
211 }
212
213 static void
214 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
215 {       
216         GtkTreeModel *model;
217
218         g_object_get (G_OBJECT (page),
219                       "model", &model,
220                       NULL);
221
222         ev_page_action_widget_update_model (proxy, model);
223 }
224
225 static void
226 connect_proxy (GtkAction *action, GtkWidget *proxy)
227 {
228         if (GTK_IS_TOOL_ITEM (proxy)) {
229                 g_signal_connect_object (action, "notify::page-cache",
230                                          G_CALLBACK (update_page_cache),
231                                          proxy, 0);
232                 g_signal_connect (proxy, "activate_link",
233                                   G_CALLBACK (activate_link_cb),
234                                   action);
235                 update_page_cache (EV_PAGE_ACTION (action), NULL,
236                                    EV_PAGE_ACTION_WIDGET (proxy));
237                 g_signal_connect_object (action, "notify::model",
238                                          G_CALLBACK (update_model),
239                                          proxy, 0);
240         }
241
242         GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
243 }
244
245 static void
246 ev_page_action_dispose (GObject *object)
247 {
248         EvPageAction *page = EV_PAGE_ACTION (object);
249
250         if (page->priv->page_cache) {
251                 g_object_unref (page->priv->page_cache);
252                 page->priv->page_cache = NULL;
253         }
254
255         G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
256 }
257
258 static void
259 ev_page_action_set_property (GObject      *object,
260                              guint         prop_id,
261                              const GValue *value,
262                              GParamSpec   *pspec)
263 {
264         EvPageAction *page;
265         EvPageCache *page_cache;
266         GtkTreeModel *model;
267   
268         page = EV_PAGE_ACTION (object);
269
270         switch (prop_id)
271         {
272         case PROP_PAGE_CACHE:
273                 page_cache = page->priv->page_cache;
274                 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
275                 if (page_cache)
276                         g_object_unref (page_cache);
277                 break;
278         case PROP_MODEL:
279                 model = page->priv->model;
280                 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
281                 if (model)
282                         g_object_unref (model);
283                 break;
284         default:
285                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286                 break;
287         }
288 }
289
290 static void
291 ev_page_action_get_property (GObject    *object,
292                              guint       prop_id,
293                              GValue     *value,
294                              GParamSpec *pspec)
295 {
296         EvPageAction *page;
297   
298         page = EV_PAGE_ACTION (object);
299
300         switch (prop_id)
301         {
302         case PROP_PAGE_CACHE:
303                 g_value_set_object (value, page->priv->page_cache);
304                 break;
305         case PROP_MODEL:
306                 g_value_set_object (value, page->priv->model);
307                 break;
308         default:
309                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310                 break;
311         }
312 }
313
314 void
315 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
316 {
317         EvPageCache *page_cache = NULL;
318
319         if (document)
320                 page_cache = ev_page_cache_get (document);
321         
322         g_object_set (page,
323                       "page-cache", page_cache,
324                       "model", NULL,
325                       NULL);
326 }
327
328 void
329 ev_page_action_set_model (EvPageAction *page_action,
330                           GtkTreeModel *model)
331 {
332         g_object_set (page_action,
333                       "model", model,
334                       NULL);
335 }
336
337 void
338 ev_page_action_grab_focus (EvPageAction *page_action)
339 {
340         GSList *proxies;
341
342         proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
343         for (; proxies != NULL; proxies = proxies->next) {
344                 EvPageActionWidget *proxy;
345
346                 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
347                 gtk_widget_grab_focus (proxy->entry);
348         }
349 }
350
351 static void
352 ev_page_action_init (EvPageAction *page)
353 {
354         page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
355 }
356
357 static void
358 ev_page_action_class_init (EvPageActionClass *class)
359 {
360         GObjectClass *object_class = G_OBJECT_CLASS (class);
361         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
362
363         object_class->dispose = ev_page_action_dispose;
364         object_class->set_property = ev_page_action_set_property;
365         object_class->get_property = ev_page_action_get_property;
366
367         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
368         action_class->create_tool_item = create_tool_item;
369         action_class->connect_proxy = connect_proxy;
370
371         signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
372                                                G_OBJECT_CLASS_TYPE (object_class),
373                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
374                                                G_STRUCT_OFFSET (EvPageActionClass, activate_link),
375                                                NULL, NULL,
376                                                g_cclosure_marshal_VOID__OBJECT,
377                                                G_TYPE_NONE, 1,
378                                                G_TYPE_OBJECT);
379
380         g_object_class_install_property (object_class,
381                                          PROP_PAGE_CACHE,
382                                          g_param_spec_object ("page-cache",
383                                                               "Page Cache",
384                                                               "Current page cache",
385                                                               EV_TYPE_PAGE_CACHE,
386                                                               G_PARAM_READWRITE));
387
388         g_object_class_install_property (object_class,
389                                          PROP_MODEL,
390                                          g_param_spec_object ("model",
391                                                               "Model",
392                                                               "Current Model",
393                                                               GTK_TYPE_TREE_MODEL,
394                                                               G_PARAM_READWRITE));
395
396         g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));
397 }