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