2 * Copyright (C) 2003, 2004 Marco Pesenti Gritti
3 * Copyright (C) 2003, 2004 Christian Persch
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)
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.
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.
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"
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>
38 struct _EvPageActionPrivate
40 EvPageCache *page_cache;
45 static void ev_page_action_init (EvPageAction *action);
46 static void ev_page_action_class_init (EvPageActionClass *class);
54 static guint signals[N_SIGNALS] = {0, };
56 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
58 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
67 update_pages_label (EvPageActionWidget *proxy,
69 EvPageCache *page_cache)
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);
78 label_text = g_strdup_printf (_("of %d"), n_pages);
80 gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
85 page_changed_cb (EvPageCache *page_cache,
87 EvPageActionWidget *proxy)
91 if (page_cache != NULL && page >= 0) {
94 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry),
95 CLAMP (ev_page_cache_get_max_label_chars (page_cache),
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);
104 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
107 update_pages_label (proxy, page, page_cache);
111 activate_cb (GtkWidget *entry, GtkAction *action)
113 EvPageAction *page = EV_PAGE_ACTION (action);
114 EvPageCache *page_cache;
118 EvLinkDest *link_dest;
119 EvLinkAction *link_action;
123 text = gtk_entry_get_text (GTK_ENTRY (entry));
124 page_cache = page->priv->page_cache;
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);
132 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
134 g_object_unref (link);
137 /* rest the entry to the current page if we were unable to
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);
147 create_tool_item (GtkAction *action)
149 EvPageActionWidget *proxy;
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));
156 hbox = gtk_hbox_new (FALSE, 0);
157 gtk_box_set_spacing (GTK_BOX (hbox), 6);
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),
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);
171 gtk_container_add (GTK_CONTAINER (proxy), hbox);
172 gtk_widget_show (hbox);
174 return GTK_WIDGET (proxy);
178 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
180 EvPageCache *page_cache;
183 page_cache = page->priv->page_cache;
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);
189 if (page_cache != NULL) {
190 signal_id = g_signal_connect_object (page_cache,
192 G_CALLBACK (page_changed_cb),
194 /* Set the initial value */
195 page_changed_cb (page_cache,
196 ev_page_cache_get_current_page (page_cache),
199 /* Or clear the entry */
201 page_changed_cb (NULL, 0, proxy);
203 ev_page_action_widget_set_page_cache (proxy, page_cache);
204 proxy->signal_id = signal_id;
208 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
210 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
214 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
218 g_object_get (G_OBJECT (page),
222 ev_page_action_widget_update_model (proxy, model);
226 connect_proxy (GtkAction *action, GtkWidget *proxy)
228 if (GTK_IS_TOOL_ITEM (proxy)) {
229 g_signal_connect_object (action, "notify::page-cache",
230 G_CALLBACK (update_page_cache),
232 g_signal_connect (proxy, "activate_link",
233 G_CALLBACK (activate_link_cb),
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),
242 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
246 ev_page_action_dispose (GObject *object)
248 EvPageAction *page = EV_PAGE_ACTION (object);
250 if (page->priv->page_cache) {
251 g_object_unref (page->priv->page_cache);
252 page->priv->page_cache = NULL;
255 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
259 ev_page_action_set_property (GObject *object,
265 EvPageCache *page_cache;
268 page = EV_PAGE_ACTION (object);
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));
276 g_object_unref (page_cache);
279 model = page->priv->model;
280 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
282 g_object_unref (model);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 ev_page_action_get_property (GObject *object,
298 page = EV_PAGE_ACTION (object);
302 case PROP_PAGE_CACHE:
303 g_value_set_object (value, page->priv->page_cache);
306 g_value_set_object (value, page->priv->model);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
317 EvPageCache *page_cache = NULL;
320 page_cache = ev_page_cache_get (document);
323 "page-cache", page_cache,
329 ev_page_action_set_model (EvPageAction *page_action,
332 g_object_set (page_action,
338 ev_page_action_grab_focus (EvPageAction *page_action)
342 proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
343 for (; proxies != NULL; proxies = proxies->next) {
344 EvPageActionWidget *proxy;
346 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
347 gtk_widget_grab_focus (proxy->entry);
352 ev_page_action_init (EvPageAction *page)
354 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
358 ev_page_action_class_init (EvPageActionClass *class)
360 GObjectClass *object_class = G_OBJECT_CLASS (class);
361 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
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;
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;
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),
376 g_cclosure_marshal_VOID__OBJECT,
380 g_object_class_install_property (object_class,
382 g_param_spec_object ("page-cache",
384 "Current page cache",
388 g_object_class_install_property (object_class,
390 g_param_spec_object ("model",
396 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));