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.
24 #include "ev-page-action.h"
25 #include "ev-window.h"
27 #include <glib/gi18n.h>
28 #include <gtk/gtkentry.h>
29 #include <gtk/gtktoolitem.h>
30 #include <gtk/gtklabel.h>
31 #include <gtk/gtkhbox.h>
33 struct _EvPageActionPrivate
35 EvPageCache *page_cache;
39 static void ev_page_action_init (EvPageAction *action);
40 static void ev_page_action_class_init (EvPageActionClass *class);
42 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
44 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
51 #define ENTRY_DATA "epa-entry"
52 #define PAGE_CACHE_DATA "epa-page-cache"
53 #define SIGNAL_ID_DATA "epa-signal-id"
56 page_changed_cb (EvPageCache *page_cache,
62 entry = GTK_WIDGET (g_object_get_data (G_OBJECT (proxy), ENTRY_DATA));
63 if (page_cache != NULL) {
64 gchar *page_label = ev_page_cache_get_page_label (page_cache, page);
65 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
66 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
69 gtk_entry_set_text (GTK_ENTRY (entry), "");
74 activate_cb (GtkWidget *entry, GtkAction *action)
76 EvPageAction *page = EV_PAGE_ACTION (action);
77 EvPageCache *page_cache;
80 text = gtk_entry_get_text (GTK_ENTRY (entry));
81 page_cache = page->priv->page_cache;
83 if (! ev_page_cache_set_page_label (page_cache, text)) {
84 /* rest the entry to the current page if we were unable to
87 ev_page_cache_get_page_label (page_cache,
88 ev_page_cache_get_current_page (page_cache));
89 gtk_entry_set_text (GTK_ENTRY (entry), page_label);
90 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
96 create_tool_item (GtkAction *action)
98 GtkWidget *hbox, *entry, *item;
100 hbox = gtk_hbox_new (FALSE, 6);
101 gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
102 gtk_widget_show (hbox);
104 item = GTK_WIDGET (gtk_tool_item_new ());
105 gtk_widget_show (item);
107 entry = gtk_entry_new ();
108 gtk_entry_set_width_chars (GTK_ENTRY (entry), 5);
109 g_object_set_data (G_OBJECT (item), ENTRY_DATA, entry);
110 gtk_widget_show (entry);
112 g_signal_connect (entry, "activate",
113 G_CALLBACK (activate_cb),
116 gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
117 gtk_container_add (GTK_CONTAINER (item), hbox);
123 update_page_cache (EvPageAction *page, gpointer dummy, GtkWidget *proxy)
125 EvPageCache *page_cache;
126 EvPageCache *old_page_cache;
129 page_cache = page->priv->page_cache;
130 old_page_cache = (EvPageCache *) g_object_get_data (G_OBJECT (proxy), PAGE_CACHE_DATA);
131 signal_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (proxy), SIGNAL_ID_DATA));
133 /* clear the old signal */
134 if (signal_id > 0 && old_page_cache)
135 g_signal_handler_disconnect (old_page_cache, signal_id);
137 if (page_cache != NULL) {
138 signal_id = g_signal_connect (page_cache,
140 G_CALLBACK (page_changed_cb),
142 /* Set the initial value */
143 page_changed_cb (page_cache,
144 ev_page_cache_get_current_page (page_cache),
147 /* Or clear the entry */
149 page_changed_cb (NULL, 0, proxy);
151 g_object_set_data (G_OBJECT (proxy), PAGE_CACHE_DATA, page_cache);
152 g_object_set_data (G_OBJECT (proxy), SIGNAL_ID_DATA, GINT_TO_POINTER (signal_id));
156 connect_proxy (GtkAction *action, GtkWidget *proxy)
158 if (GTK_IS_TOOL_ITEM (proxy)) {
159 g_signal_connect_object (action, "notify::page-cache",
160 G_CALLBACK (update_page_cache),
164 GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
168 ev_page_action_dispose (GObject *object)
170 EvPageAction *page = EV_PAGE_ACTION (object);
172 if (page->priv->page_cache) {
173 g_object_unref (page->priv->page_cache);
174 page->priv->page_cache = NULL;
177 G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
181 ev_page_action_set_property (GObject *object,
187 EvPageCache *page_cache;
189 page = EV_PAGE_ACTION (object);
193 case PROP_PAGE_CACHE:
194 page_cache = page->priv->page_cache;
195 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
197 g_object_unref (page_cache);
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206 ev_page_action_get_property (GObject *object,
213 page = EV_PAGE_ACTION (object);
217 case PROP_PAGE_CACHE:
218 g_value_set_object (value, page->priv->page_cache);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
229 EvPageCache *page_cache = NULL;
232 page_cache = ev_document_get_page_cache (document);
234 g_object_set (page, "page-cache", page_cache, NULL);
238 ev_page_action_init (EvPageAction *page)
240 page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
244 ev_page_action_class_init (EvPageActionClass *class)
246 GObjectClass *object_class = G_OBJECT_CLASS (class);
247 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
249 object_class->dispose = ev_page_action_dispose;
250 object_class->set_property = ev_page_action_set_property;
251 object_class->get_property = ev_page_action_get_property;
253 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
254 action_class->create_tool_item = create_tool_item;
255 action_class->connect_proxy = connect_proxy;
257 g_object_class_install_property (object_class,
259 g_param_spec_object ("page-cache",
261 "Current page cache",
265 g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));