]> www.fi.muni.cz Git - evince.git/blob - libmisc/ev-page-action.c
ced16663ffddedabb046aa79d57e2670798bd819
[evince.git] / libmisc / 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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
31 #include "ev-page-action.h"
32 #include "ev-page-action-widget.h"
33
34 struct _EvPageActionPrivate
35 {
36         EvPageCache *page_cache;
37         GtkTreeModel *model;
38 };
39
40
41 static void ev_page_action_init       (EvPageAction *action);
42 static void ev_page_action_class_init (EvPageActionClass *class);
43
44 enum
45 {
46         ACTIVATE_LINK,
47         N_SIGNALS
48 };
49
50 static guint signals[N_SIGNALS] = {0, };
51
52 G_DEFINE_TYPE (EvPageAction, ev_page_action, GTK_TYPE_ACTION)
53
54 #define EV_PAGE_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PAGE_ACTION, EvPageActionPrivate))
55
56 enum {
57         PROP_0,
58         PROP_PAGE_CACHE,
59         PROP_MODEL,
60 };
61
62 static GtkWidget *
63 create_tool_item (GtkAction *action)
64 {
65         GtkWidget *proxy;
66
67         proxy = g_object_new (EV_TYPE_PAGE_ACTION_WIDGET, NULL);
68
69         return proxy;
70 }
71
72 static void
73 update_page_cache (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
74 {
75         ev_page_action_widget_set_page_cache (proxy, page->priv->page_cache);
76 }
77
78 static void
79 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
80 {
81         GtkTreeModel *model;
82
83         g_object_get (G_OBJECT (page),
84                       "model", &model,
85                       NULL);
86
87         ev_page_action_widget_update_model (proxy, model);
88 }
89
90 static void
91 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
92 {
93         g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
94 }
95
96 static void
97 connect_proxy (GtkAction *action, GtkWidget *proxy)
98 {
99         if (GTK_IS_TOOL_ITEM (proxy)) {
100                 g_signal_connect_object (action, "notify::page-cache",
101                                          G_CALLBACK (update_page_cache),
102                                          proxy, 0);
103                 g_signal_connect (proxy, "activate_link",
104                                   G_CALLBACK (activate_link_cb),
105                                   action);
106                 update_page_cache (EV_PAGE_ACTION (action), NULL,
107                                    EV_PAGE_ACTION_WIDGET (proxy));
108                 g_signal_connect_object (action, "notify::model",
109                                          G_CALLBACK (update_model),
110                                          proxy, 0);
111         }
112
113         GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
114 }
115
116 static void
117 ev_page_action_dispose (GObject *object)
118 {
119         EvPageAction *page = EV_PAGE_ACTION (object);
120
121         if (page->priv->page_cache) {
122                 g_object_unref (page->priv->page_cache);
123                 page->priv->page_cache = NULL;
124         }
125
126         G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
127 }
128
129 static void
130 ev_page_action_set_property (GObject      *object,
131                              guint         prop_id,
132                              const GValue *value,
133                              GParamSpec   *pspec)
134 {
135         EvPageAction *page;
136         EvPageCache *page_cache;
137         GtkTreeModel *model;
138   
139         page = EV_PAGE_ACTION (object);
140
141         switch (prop_id)
142         {
143         case PROP_PAGE_CACHE:
144                 page_cache = page->priv->page_cache;
145                 page->priv->page_cache = EV_PAGE_CACHE (g_value_dup_object (value));
146                 if (page_cache)
147                         g_object_unref (page_cache);
148                 break;
149         case PROP_MODEL:
150                 model = page->priv->model;
151                 page->priv->model = GTK_TREE_MODEL (g_value_dup_object (value));
152                 if (model)
153                         g_object_unref (model);
154                 break;
155         default:
156                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157                 break;
158         }
159 }
160
161 static void
162 ev_page_action_get_property (GObject    *object,
163                              guint       prop_id,
164                              GValue     *value,
165                              GParamSpec *pspec)
166 {
167         EvPageAction *page;
168   
169         page = EV_PAGE_ACTION (object);
170
171         switch (prop_id)
172         {
173         case PROP_PAGE_CACHE:
174                 g_value_set_object (value, page->priv->page_cache);
175                 break;
176         case PROP_MODEL:
177                 g_value_set_object (value, page->priv->model);
178                 break;
179         default:
180                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181                 break;
182         }
183 }
184
185 void
186 ev_page_action_set_document (EvPageAction *page, EvDocument *document)
187 {
188         EvPageCache *page_cache = NULL;
189
190         if (document)
191                 page_cache = ev_page_cache_get (document);
192         
193         g_object_set (page,
194                       "page-cache", page_cache,
195                       "model", NULL,
196                       NULL);
197 }
198
199 void
200 ev_page_action_set_model (EvPageAction *page_action,
201                           GtkTreeModel *model)
202 {
203         g_object_set (page_action,
204                       "model", model,
205                       NULL);
206 }
207
208 void
209 ev_page_action_grab_focus (EvPageAction *page_action)
210 {
211         GSList *proxies;
212
213         proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
214         for (; proxies != NULL; proxies = proxies->next) {
215                 EvPageActionWidget *proxy;
216
217                 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
218
219                 if (GTK_WIDGET_MAPPED (GTK_WIDGET (proxy)))
220                         ev_page_action_widget_grab_focus (proxy);
221         }
222 }
223
224 static void
225 ev_page_action_init (EvPageAction *page)
226 {
227         page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
228 }
229
230 static void
231 ev_page_action_class_init (EvPageActionClass *class)
232 {
233         GObjectClass *object_class = G_OBJECT_CLASS (class);
234         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
235
236         object_class->dispose = ev_page_action_dispose;
237         object_class->set_property = ev_page_action_set_property;
238         object_class->get_property = ev_page_action_get_property;
239
240         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
241         action_class->create_tool_item = create_tool_item;
242         action_class->connect_proxy = connect_proxy;
243
244         signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
245                                                G_OBJECT_CLASS_TYPE (object_class),
246                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
247                                                G_STRUCT_OFFSET (EvPageActionClass, activate_link),
248                                                NULL, NULL,
249                                                g_cclosure_marshal_VOID__OBJECT,
250                                                G_TYPE_NONE, 1,
251                                                G_TYPE_OBJECT);
252
253         g_object_class_install_property (object_class,
254                                          PROP_PAGE_CACHE,
255                                          g_param_spec_object ("page-cache",
256                                                               "Page Cache",
257                                                               "Current page cache",
258                                                               EV_TYPE_PAGE_CACHE,
259                                                               G_PARAM_READWRITE));
260
261         g_object_class_install_property (object_class,
262                                          PROP_MODEL,
263                                          g_param_spec_object ("model",
264                                                               "Model",
265                                                               "Current Model",
266                                                               GTK_TYPE_TREE_MODEL,
267                                                               G_PARAM_READWRITE));
268
269         g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));
270 }