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.
28 #include <glib/gi18n.h>
31 #include "ev-page-action.h"
32 #include "ev-page-action-widget.h"
34 struct _EvPageActionPrivate
36 EvPageCache *page_cache;
41 static void ev_page_action_init (EvPageAction *action);
42 static void ev_page_action_class_init (EvPageActionClass *class);
50 static guint signals[N_SIGNALS] = {0, };
52 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
54 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
63 update_pages_label (EvPageActionWidget *proxy,
65 EvPageCache *page_cache)
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);
74 label_text = g_strdup_printf (_("of %d"), n_pages);
76 gtk_label_set_text (GTK_LABEL (proxy->label), label_text);
81 page_changed_cb (EvPageCache *page_cache,
83 EvPageActionWidget *proxy)
87 if (page_cache != NULL && page >= 0) {
90 gtk_entry_set_width_chars (GTK_ENTRY (proxy->entry),
91 CLAMP (ev_page_cache_get_max_label_chars (page_cache),
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);
100 gtk_entry_set_text (GTK_ENTRY (proxy->entry), "");
103 update_pages_label (proxy, page, page_cache);
107 activate_cb (GtkWidget *entry, GtkAction *action)
109 EvPageAction *page = EV_PAGE_ACTION (action);
110 EvPageCache *page_cache;
114 EvLinkDest *link_dest;
115 EvLinkAction *link_action;
119 text = gtk_entry_get_text (GTK_ENTRY (entry));
120 page_cache = page->priv->page_cache;
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);
128 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
130 g_object_unref (link);
133 /* rest the entry to the current page if we were unable to
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);
142 static gboolean page_scroll_cb(GtkWidget *widget, GdkEventScroll *event, EvPageAction* action)
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))
150 if ((event->direction == GDK_SCROLL_UP) && (pageno > 0))
152 ev_page_cache_set_current_page (action->priv->page_cache, pageno);
158 create_tool_item (GtkAction *action)
160 EvPageActionWidget *proxy;
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));
168 hbox = gtk_hbox_new (FALSE, 0);
169 gtk_box_set_spacing (GTK_BOX (hbox), 6);
171 proxy->entry = gtk_entry_new ();
172 obj = gtk_widget_get_accessible (proxy->entry);
173 atk_object_set_name (obj, "page-label-entry");
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),
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);
188 gtk_container_add (GTK_CONTAINER (proxy), hbox);
189 gtk_widget_show (hbox);
191 return GTK_WIDGET (proxy);
195 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
197 EvPageCache *page_cache;
200 page_cache = page->priv->page_cache;
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);
206 if (page_cache != NULL) {
207 signal_id = g_signal_connect_object (page_cache,
209 G_CALLBACK (page_changed_cb),
211 /* Set the initial value */
212 page_changed_cb (page_cache,
213 ev_page_cache_get_current_page (page_cache),
216 /* Or clear the entry */
218 page_changed_cb (NULL, 0, proxy);
220 ev_page_action_widget_set_page_cache (proxy, page_cache);
221 proxy->signal_id = signal_id;
225 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
227 g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
231 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
235 g_object_get (G_OBJECT (page),
239 ev_page_action_widget_update_model (proxy, model);
243 connect_proxy (GtkAction *action, GtkWidget *proxy)
245 if (GTK_IS_TOOL_ITEM (proxy)) {
246 g_signal_connect_object (action, "notify::page-cache",
247 G_CALLBACK (update_page_cache),
249 g_signal_connect (proxy, "activate_link",
250 G_CALLBACK (activate_link_cb),
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),
259 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
263 ev_page_action_dispose (GObject *object)
265 EvPageAction *page = EV_PAGE_ACTION (object);
267 if (page->priv->page_cache) {
268 g_object_unref (page->priv->page_cache);
269 page->priv->page_cache = NULL;
272 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
276 ev_page_action_set_property (GObject *object,
282 EvPageCache *page_cache;
285 page = EV_PAGE_ACTION (object);
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));
293 g_object_unref (page_cache);
296 model = page->priv->model;
297 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
299 g_object_unref (model);
302 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 ev_page_action_get_property (GObject *object,
315 page = EV_PAGE_ACTION (object);
319 case PROP_PAGE_CACHE:
320 g_value_set_object (value, page->priv->page_cache);
323 g_value_set_object (value, page->priv->model);
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
332 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
334 EvPageCache *page_cache = NULL;
337 page_cache = ev_page_cache_get (document);
340 "page-cache", page_cache,
346 ev_page_action_set_model (EvPageAction *page_action,
349 g_object_set (page_action,
355 ev_page_action_grab_focus (EvPageAction *page_action)
359 proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
360 for (; proxies != NULL; proxies = proxies->next) {
361 EvPageActionWidget *proxy;
363 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
365 if (GTK_WIDGET_MAPPED (GTK_WIDGET (proxy)))
366 gtk_widget_grab_focus (proxy->entry);
371 ev_page_action_init (EvPageAction *page)
373 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
377 ev_page_action_class_init (EvPageActionClass *class)
379 GObjectClass *object_class = G_OBJECT_CLASS (class);
380 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
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;
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;
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),
395 g_cclosure_marshal_VOID__OBJECT,
399 g_object_class_install_property (object_class,
401 g_param_spec_object ("page-cache",
403 "Current page cache",
407 g_object_class_install_property (object_class,
409 g_param_spec_object ("model",
415 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));