]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-action.c
Preliminary history implementation
[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
37 struct _EvPageActionPrivate
38 {
39         EvPageCache *page_cache;
40         GtkTreeModel *model;
41 };
42
43
44 static void ev_page_action_init       (EvPageAction *action);
45 static void ev_page_action_class_init (EvPageActionClass *class);
46
47 enum
48 {
49         ACTIVATE_LINK,
50         ACTIVATE_LABEL,
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         gboolean changed;
117
118         text = gtk_entry_get_text (GTK_ENTRY (entry));
119         page_cache = page->priv->page_cache;
120
121         g_signal_emit (action, signals[ACTIVATE_LABEL], 0, text, &changed);
122
123         if (!changed) {
124                 /* rest the entry to the current page if we were unable to
125                  * change it */
126                 gchar *page_label =
127                         ev_page_cache_get_page_label (page_cache,
128                                                       ev_page_cache_get_current_page (page_cache));
129                 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
130                 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
131                 g_free (page_label);
132         }
133 }
134
135 static GtkWidget *
136 create_tool_item (GtkAction *action)
137 {
138         EvPageActionWidget *proxy;
139         GtkWidget *hbox;
140
141         proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
142         gtk_container_set_border_width (GTK_CONTAINER (proxy), 6); 
143         gtk_widget_show (GTK_WIDGET (proxy));
144
145         hbox = gtk_hbox_new (FALSE, 0);
146         gtk_box_set_spacing (GTK_BOX (hbox), 6);
147
148         proxy->entry = gtk_entry_new ();
149         gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
150         gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
151         gtk_widget_show (proxy->entry);
152         g_signal_connect (proxy->entry, "activate",
153                           G_CALLBACK (activate_cb),
154                           action);
155
156         proxy->label = gtk_label_new (NULL);
157         gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
158         gtk_widget_show (proxy->label);
159
160         gtk_container_add (GTK_CONTAINER (proxy), hbox);
161         gtk_widget_show (hbox);
162
163         return GTK_WIDGET (proxy);
164 }
165
166 static void
167 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
168 {
169         EvPageCache *page_cache;
170         guint signal_id;
171
172         page_cache = page->priv->page_cache;
173
174         /* clear the old signal */
175         if (proxy->signal_id > 0 && proxy->page_cache)
176                 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
177         
178         if (page_cache != NULL) {
179                 signal_id = g_signal_connect_object (page_cache,
180                                                      "page-changed",
181                                                      G_CALLBACK (page_changed_cb),
182                                                      proxy, 0);
183                 /* Set the initial value */
184                 page_changed_cb (page_cache,
185                                  ev_page_cache_get_current_page (page_cache),
186                                  proxy);
187         } else {
188                 /* Or clear the entry */
189                 signal_id = 0;
190                 page_changed_cb (NULL, 0, proxy);
191         }
192         ev_page_action_widget_set_page_cache (proxy, page_cache);
193         proxy->signal_id = signal_id;
194 }
195
196 static void
197 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
198 {
199         g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
200 }
201
202 static void
203 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
204 {       
205         GtkTreeModel *model;
206
207         g_object_get (G_OBJECT (page),
208                       "model", &model,
209                       NULL);
210
211         ev_page_action_widget_update_model (proxy, model);
212 }
213
214 static void
215 connect_proxy (GtkAction *action, GtkWidget *proxy)
216 {
217         if (GTK_IS_TOOL_ITEM (proxy)) {
218                 g_signal_connect_object (action, "notify::page-cache",
219                                          G_CALLBACK (update_page_cache),
220                                          proxy, 0);
221                 g_signal_connect (proxy, "activate_link",
222                                   G_CALLBACK (activate_link_cb),
223                                   action);
224                 update_page_cache (EV_PAGE_ACTION (action), NULL,
225                                    EV_PAGE_ACTION_WIDGET (proxy));
226                 g_signal_connect_object (action, "notify::model",
227                                          G_CALLBACK (update_model),
228                                          proxy, 0);
229         }
230
231         GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
232 }
233
234 static void
235 ev_page_action_dispose (GObject *object)
236 {
237         EvPageAction *page = EV_PAGE_ACTION (object);
238
239         if (page->priv->page_cache) {
240                 g_object_unref (page->priv->page_cache);
241                 page->priv->page_cache = NULL;
242         }
243
244         G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
245 }
246
247 static void
248 ev_page_action_set_property (GObject      *object,
249                              guint         prop_id,
250                              const GValue *value,
251                              GParamSpec   *pspec)
252 {
253         EvPageAction *page;
254         EvPageCache *page_cache;
255         GtkTreeModel *model;
256   
257         page = EV_PAGE_ACTION (object);
258
259         switch (prop_id)
260         {
261         case PROP_PAGE_CACHE:
262                 page_cache = page->priv->page_cache;
263                 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
264                 if (page_cache)
265                         g_object_unref (page_cache);
266                 break;
267         case PROP_MODEL:
268                 model = page->priv->model;
269                 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
270                 if (model)
271                         g_object_unref (model);
272                 break;
273         default:
274                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275                 break;
276         }
277 }
278
279 static void
280 ev_page_action_get_property (GObject    *object,
281                              guint       prop_id,
282                              GValue     *value,
283                              GParamSpec *pspec)
284 {
285         EvPageAction *page;
286   
287         page = EV_PAGE_ACTION (object);
288
289         switch (prop_id)
290         {
291         case PROP_PAGE_CACHE:
292                 g_value_set_object (value, page->priv->page_cache);
293                 break;
294         case PROP_MODEL:
295                 g_value_set_object (value, page->priv->model);
296                 break;
297         default:
298                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299                 break;
300         }
301 }
302
303 void
304 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
305 {
306         EvPageCache *page_cache = NULL;
307
308         if (document)
309                 page_cache = ev_page_cache_get (document);
310         
311         g_object_set (page,
312                       "page-cache", page_cache,
313                       "model", NULL,
314                       NULL);
315 }
316
317 void
318 ev_page_action_set_model (EvPageAction *page_action,
319                           GtkTreeModel *model)
320 {
321         g_object_set (page_action,
322                       "model", model,
323                       NULL);
324 }
325
326 void
327 ev_page_action_grab_focus (EvPageAction *page_action)
328 {
329         GSList *proxies;
330
331         proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
332         for (; proxies != NULL; proxies = proxies->next) {
333                 EvPageActionWidget *proxy;
334
335                 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
336                 gtk_widget_grab_focus (proxy->entry);
337         }
338 }
339
340 static void
341 ev_page_action_init (EvPageAction *page)
342 {
343         page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
344 }
345
346 static void
347 ev_page_action_class_init (EvPageActionClass *class)
348 {
349         GObjectClass *object_class = G_OBJECT_CLASS (class);
350         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
351
352         object_class->dispose = ev_page_action_dispose;
353         object_class->set_property = ev_page_action_set_property;
354         object_class->get_property = ev_page_action_get_property;
355
356         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
357         action_class->create_tool_item = create_tool_item;
358         action_class->connect_proxy = connect_proxy;
359
360         signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
361                                                G_OBJECT_CLASS_TYPE (object_class),
362                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
363                                                G_STRUCT_OFFSET (EvPageActionClass, activate_link),
364                                                NULL, NULL,
365                                                g_cclosure_marshal_VOID__OBJECT,
366                                                G_TYPE_NONE, 1,
367                                                G_TYPE_OBJECT);
368         signals[ACTIVATE_LABEL] = g_signal_new ("activate_label",
369                                                 G_OBJECT_CLASS_TYPE (object_class),
370                                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
371                                                 G_STRUCT_OFFSET (EvPageActionClass, activate_label),
372                                                 NULL, NULL,
373                                                 ev_marshal_BOOLEAN__STRING,
374                                                 G_TYPE_BOOLEAN, 1,
375                                                 G_TYPE_STRING);
376
377         g_object_class_install_property (object_class,
378                                          PROP_PAGE_CACHE,
379                                          g_param_spec_object ("page-cache",
380                                                               "Page Cache",
381                                                               "Current page cache",
382                                                               EV_TYPE_PAGE_CACHE,
383                                                               G_PARAM_READWRITE));
384
385         g_object_class_install_property (object_class,
386                                          PROP_MODEL,
387                                          g_param_spec_object ("model",
388                                                               "Model",
389                                                               "Current Model",
390                                                               GTK_TYPE_TREE_MODEL,
391                                                               G_PARAM_READWRITE));
392
393         g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));
394 }