]> www.fi.muni.cz Git - evince.git/blob - libmisc/ev-page-action.c
Remove EvPageCache and use EvDocumentModel instead
[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         EvDocumentModel *doc_model;
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_MODEL
59 };
60
61 static GtkWidget *
62 create_tool_item (GtkAction *action)
63 {
64         GtkWidget *proxy;
65
66         proxy = g_object_new (EV_TYPE_PAGE_ACTION_WIDGET, NULL);
67
68         return proxy;
69 }
70
71 static void
72 update_model (EvPageAction *page, GParamSpec *pspec, EvPageActionWidget *proxy)
73 {
74         ev_page_action_widget_update_links_model (proxy, page->priv->model);
75 }
76
77 static void
78 activate_link_cb (EvPageActionWidget *proxy, EvLink *link, EvPageAction *action)
79 {
80         g_signal_emit (action, signals[ACTIVATE_LINK], 0, link);
81 }
82
83 static void
84 connect_proxy (GtkAction *action, GtkWidget *proxy)
85 {
86         EvPageAction *page = EV_PAGE_ACTION (action);
87
88         if (GTK_IS_TOOL_ITEM (proxy)) {
89                 ev_page_action_widget_set_model (EV_PAGE_ACTION_WIDGET (proxy),
90                                                  page->priv->doc_model);
91                 g_signal_connect (proxy, "activate_link",
92                                   G_CALLBACK (activate_link_cb),
93                                   action);
94                 g_signal_connect_object (action, "notify::model",
95                                          G_CALLBACK (update_model),
96                                          proxy, 0);
97         }
98
99         GTK_ACTION_CLASS (ev_page_action_parent_class)->connect_proxy (action, proxy);
100 }
101
102 static void
103 ev_page_action_dispose (GObject *object)
104 {
105         EvPageAction *page = EV_PAGE_ACTION (object);
106
107         if (page->priv->model) {
108                 g_object_unref (page->priv->model);
109                 page->priv->model = NULL;
110         }
111
112         page->priv->doc_model = NULL;
113
114         G_OBJECT_CLASS (ev_page_action_parent_class)->dispose (object);
115 }
116
117 static void
118 ev_page_action_set_property (GObject      *object,
119                              guint         prop_id,
120                              const GValue *value,
121                              GParamSpec   *pspec)
122 {
123         EvPageAction *page = EV_PAGE_ACTION (object);
124         GtkTreeModel *model;
125
126         switch (prop_id)
127         {
128         case PROP_MODEL:
129                 ev_page_action_set_links_model (page, g_value_get_object (value));
130                 break;
131         default:
132                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133                 break;
134         }
135 }
136
137 static void
138 ev_page_action_get_property (GObject    *object,
139                              guint       prop_id,
140                              GValue     *value,
141                              GParamSpec *pspec)
142 {
143         EvPageAction *page = EV_PAGE_ACTION (object);
144
145         switch (prop_id)
146         {
147         case PROP_MODEL:
148                 g_value_set_object (value, page->priv->model);
149                 break;
150         default:
151                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152                 break;
153         }
154 }
155
156 void
157 ev_page_action_set_model (EvPageAction    *page,
158                           EvDocumentModel *model)
159 {
160         g_return_if_fail (EV_IS_PAGE_ACTION (page));
161         g_return_if_fail (EV_IS_DOCUMENT_MODEL (model));
162
163         if (page->priv->doc_model == model)
164                 return;
165
166         page->priv->doc_model = model;
167 }
168
169 void
170 ev_page_action_set_links_model (EvPageAction *page,
171                                 GtkTreeModel *links_model)
172 {
173         g_return_if_fail (EV_IS_PAGE_ACTION (page));
174         g_return_if_fail (GTK_IS_TREE_MODEL (links_model));
175
176         if (page->priv->model == links_model)
177                 return;
178
179         if (page->priv->model)
180                 g_object_unref (page->priv->model);
181         page->priv->model = g_object_ref (links_model);
182
183         g_object_notify (G_OBJECT (page), "model");
184 }
185
186 void
187 ev_page_action_grab_focus (EvPageAction *page_action)
188 {
189         GSList *proxies;
190
191         proxies = gtk_action_get_proxies (GTK_ACTION (page_action));
192         for (; proxies != NULL; proxies = proxies->next) {
193                 EvPageActionWidget *proxy;
194
195                 proxy = EV_PAGE_ACTION_WIDGET (proxies->data);
196
197                 if (GTK_WIDGET_MAPPED (GTK_WIDGET (proxy)))
198                         ev_page_action_widget_grab_focus (proxy);
199         }
200 }
201
202 static void
203 ev_page_action_init (EvPageAction *page)
204 {
205         page->priv = EV_PAGE_ACTION_GET_PRIVATE (page);
206 }
207
208 static void
209 ev_page_action_class_init (EvPageActionClass *class)
210 {
211         GObjectClass *object_class = G_OBJECT_CLASS (class);
212         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
213
214         object_class->dispose = ev_page_action_dispose;
215         object_class->set_property = ev_page_action_set_property;
216         object_class->get_property = ev_page_action_get_property;
217
218         action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
219         action_class->create_tool_item = create_tool_item;
220         action_class->connect_proxy = connect_proxy;
221
222         signals[ACTIVATE_LINK] = g_signal_new ("activate_link",
223                                                G_OBJECT_CLASS_TYPE (object_class),
224                                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
225                                                G_STRUCT_OFFSET (EvPageActionClass, activate_link),
226                                                NULL, NULL,
227                                                g_cclosure_marshal_VOID__OBJECT,
228                                                G_TYPE_NONE, 1,
229                                                G_TYPE_OBJECT);
230
231         g_object_class_install_property (object_class,
232                                          PROP_MODEL,
233                                          g_param_spec_object ("model",
234                                                               "Model",
235                                                               "Current Links Model",
236                                                               GTK_TYPE_TREE_MODEL,
237                                                               G_PARAM_READWRITE));
238
239         g_type_class_add_private (object_class, sizeof (EvPageActionPrivate));
240 }