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.
27 #include <glib/gi18n.h>
30 #include "ev-page-action.h"
31 #include "ev-page-cache.h"
32 #include "ev-window.h"
33 #include "ev-document-links.h"
34 #include "ev-page-action-widget.h"
35 #include "ev-marshal.h"
37 struct _EvPageActionPrivate
39 EvPageCache *page_cache;
44 static void ev_page_action_init (EvPageAction *action);
45 static void ev_page_action_class_init (EvPageActionClass *class);
53 static guint signals[N_SIGNALS] = {0, };
55 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
57 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
66 update_pages_label (EvPageActionWidget *proxy,
68 EvPageCache *page_cache)
73 n_pages = page_cache ? ev_page_cache_get_n_pages (page_cache) : 0;
74 if (page_cache && ev_page_cache_has_nonnumeric_page_labels (page_cache)) {
75 label_text = g_strdup_printf (_("(%d of %d)"), page + 1, n_pages);
77 label_text = g_strdup_printf (_("of %d"), n_pages);
79 gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
84 page_changed_cb (EvPageCache *page_cache,
86 EvPageActionWidget *proxy)
90 if (page_cache != NULL && page >= 0) {
93 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry),
94 CLAMP (ev_page_cache_get_max_label_chars (page_cache),
97 page_label = ev_page_cache_get_page_label (page_cache, page);
98 gtk_entry_set_text (GTK_ENTRY (proxy->entry), page_label);
99 gtk_editable_set_position (GTK_EDITABLE (proxy->entry), -1);
103 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
106 update_pages_label (proxy, page, page_cache);
110 activate_cb (GtkWidget *entry, GtkAction *action)
112 EvPageAction *page = EV_PAGE_ACTION (action);
113 EvPageCache *page_cache;
117 EvLinkDest *link_dest;
118 EvLinkAction *link_action;
122 text = gtk_entry_get_text (GTK_ENTRY (entry));
123 page_cache = page->priv->page_cache;
126 link_dest = ev_link_dest_new_page_label (text);
127 link_action = ev_link_action_new_dest (link_dest);
128 link_text = g_strdup_printf ("Page: %s", text);
129 link = ev_link_new (link_text, link_action);
131 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
133 g_object_unref (link);
136 /* rest the entry to the current page if we were unable to
138 page_label = ev_page_cache_get_page_label (page_cache,
139 ev_page_cache_get_current_page (page_cache));
140 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
141 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
145 static gboolean page_scroll_cb(GtkWidget *widget, GdkEventScroll *event, EvPageAction* action)
149 pageno = ev_page_cache_get_current_page (action->priv->page_cache);
150 if ((event->direction == GDK_SCROLL_DOWN) &&
151 (pageno < ev_page_cache_get_n_pages(action->priv->page_cache) - 1))
153 if ((event->direction == GDK_SCROLL_UP) && (pageno > 0))
155 ev_page_cache_set_current_page (action->priv->page_cache, pageno);
161 create_tool_item (GtkAction *action)
163 EvPageActionWidget *proxy;
166 proxy = g_object_new (ev_page_action_widget_get_type (), NULL);
167 gtk_container_set_border_width (GTK_CONTAINER (proxy), 6);
168 gtk_widget_show (GTK_WIDGET (proxy));
170 hbox = gtk_hbox_new (FALSE, 0);
171 gtk_box_set_spacing (GTK_BOX (hbox), 6);
173 proxy->entry = gtk_entry_new ();
174 g_signal_connect(proxy->entry, "scroll-event",G_CALLBACK(page_scroll_cb),action);
175 gtk_widget_add_events(GTK_WIDGET(proxy->entry),GDK_BUTTON_MOTION_MASK);
176 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry), 5);
177 gtk_box_pack_start (GTK_BOX (hbox), proxy->entry, FALSE, FALSE, 0);
178 gtk_widget_show (proxy->entry);
179 g_signal_connect (proxy->entry, "activate",
180 G_CALLBACK (activate_cb),
183 proxy->label = gtk_label_new (NULL);
184 gtk_box_pack_start (GTK_BOX (hbox), proxy->label, FALSE, FALSE, 0);
185 gtk_widget_show (proxy->label);
187 gtk_container_add (GTK_CONTAINER (proxy), hbox);
188 gtk_widget_show (hbox);
190 return GTK_WIDGET (proxy);
194 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
196 EvPageCache *page_cache;
199 page_cache = page->priv->page_cache;
201 /* clear the old signal */
202 if (proxy->signal_id > 0 && proxy->page_cache)
203 g_signal_handler_disconnect (proxy->page_cache, proxy->signal_id);
205 if (page_cache != NULL) {
206 signal_id = g_signal_connect_object (page_cache,
208 G_CALLBACK (page_changed_cb),
210 /* Set the initial value */
211 page_changed_cb (page_cache,
212 ev_page_cache_get_current_page (page_cache),
215 /* Or clear the entry */
217 page_changed_cb (NULL, 0, proxy);
219 ev_page_action_widget_set_page_cache (proxy, page_cache);
220 proxy->signal_id = signal_id;
224 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
226 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
230 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
234 g_object_get (G_OBJECT (page),
238 ev_page_action_widget_update_model (proxy, model);
242 connect_proxy (GtkAction *action, GtkWidget *proxy)
244 if (GTK_IS_TOOL_ITEM (proxy)) {
245 g_signal_connect_object (action, "notify::page-cache",
246 G_CALLBACK (update_page_cache),
248 g_signal_connect (proxy, "activate_link",
249 G_CALLBACK (activate_link_cb),
251 update_page_cache (EV_PAGE_ACTION (action), NULL,
252 EV_PAGE_ACTION_WIDGET (proxy));
253 g_signal_connect_object (action, "notify::model",
254 G_CALLBACK (update_model),
258 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
262 ev_page_action_dispose (GObject *object)
264 EvPageAction *page = EV_PAGE_ACTION (object);
266 if (page->priv->page_cache) {
267 g_object_unref (page->priv->page_cache);
268 page->priv->page_cache = NULL;
271 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
275 ev_page_action_set_property (GObject *object,
281 EvPageCache *page_cache;
284 page = EV_PAGE_ACTION (object);
288 case PROP_PAGE_CACHE:
289 page_cache = page->priv->page_cache;
290 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
292 g_object_unref (page_cache);
295 model = page->priv->model;
296 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
298 g_object_unref (model);
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
307 ev_page_action_get_property (GObject *object,
314 page = EV_PAGE_ACTION (object);
318 case PROP_PAGE_CACHE:
319 g_value_set_object (value, page->priv->page_cache);
322 g_value_set_object (value, page->priv->model);
325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
333 EvPageCache *page_cache = NULL;
336 page_cache = ev_page_cache_get (document);
339 "page-cache", page_cache,
345 ev_page_action_set_model (EvPageAction *page_action,
348 g_object_set (page_action,
354 ev_page_action_grab_focus (EvPageAction *page_action)
358 proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
359 for (; proxies != NULL; proxies = proxies->next) {
360 EvPageActionWidget *proxy;
362 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
364 if (GTK_WIDGET_MAPPED (GTK_WIDGET (proxy)))
365 gtk_widget_grab_focus (proxy->entry);
370 ev_page_action_init (EvPageAction *page)
372 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
376 ev_page_action_class_init (EvPageActionClass *class)
378 GObjectClass *object_class = G_OBJECT_CLASS (class);
379 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
381 object_class->dispose = ev_page_action_dispose;
382 object_class->set_property = ev_page_action_set_property;
383 object_class->get_property = ev_page_action_get_property;
385 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
386 action_class->create_tool_item = create_tool_item;
387 action_class->connect_proxy = connect_proxy;
389 signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
390 G_OBJECT_CLASS_TYPE (object_class),
391 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
392 G_STRUCT_OFFSET (EvPageActionClass, activate_link),
394 g_cclosure_marshal_VOID__OBJECT,
398 g_object_class_install_property (object_class,
400 g_param_spec_object ("page-cache",
402 "Current page cache",
406 g_object_class_install_property (object_class,
408 g_param_spec_object ("model",
414 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));