]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/zoom-control/ephy-zoom-action.c
a264649aee374617faefcf312f8cd3b54c6d5e0e
[evince.git] / cut-n-paste / zoom-control / ephy-zoom-action.c
1 /*
2  *  Copyright (C) 2003 Marco Pesenti Gritti
3  *  Copyright (C) 2003, 2004 Christian Persch
4  *
5  *  Modified 2005 by James Bowes for use in evince.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  *  $Id$
22  */
23
24 #include "config.h"
25
26 #include "ephy-zoom-action.h"
27 #include "ephy-zoom-control.h"
28 #include "ephy-zoom.h"
29
30 #include <glib-object.h>
31 #include <glib/gi18n.h>
32 #include <gtk/gtk.h>
33
34 #define EPHY_ZOOM_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_ZOOM_ACTION, EphyZoomActionPrivate))
35
36 struct _EphyZoomActionPrivate
37 {
38         float zoom;
39         float min_zoom;
40         float max_zoom;
41 };
42
43 enum
44 {
45         PROP_0,
46         PROP_ZOOM,
47         PROP_MIN_ZOOM,
48         PROP_MAX_ZOOM
49 };
50
51
52 static void ephy_zoom_action_init       (EphyZoomAction *action);
53 static void ephy_zoom_action_class_init (EphyZoomActionClass *class);
54
55 enum
56 {
57         ZOOM_TO_LEVEL_SIGNAL,
58         LAST_SIGNAL
59 };
60
61 static guint signals[LAST_SIGNAL] = { 0 };
62
63 static GObjectClass *parent_class = NULL;
64
65 GType
66 ephy_zoom_action_get_type (void)
67 {
68         static GType type = 0;
69
70         if (G_UNLIKELY (type == 0))
71         {
72                 const GTypeInfo our_info =
73                         {
74                                 sizeof (EphyZoomActionClass),
75                                 NULL, /* base_init */
76                                 NULL, /* base_finalize */
77                                 (GClassInitFunc) ephy_zoom_action_class_init,
78                                 NULL,
79                                 NULL, /* class_data */
80                                 sizeof (EphyZoomAction),
81                                 0, /* n_preallocs */
82                                 (GInstanceInitFunc) ephy_zoom_action_init,
83                         };
84
85                 type = g_type_register_static (GTK_TYPE_ACTION,
86                                                "EphyZoomAction",
87                                                &our_info, 0);
88         }
89
90         return type;
91 }
92
93 static void
94 zoom_to_level_cb (EphyZoomControl *control,
95                   float zoom,
96                   EphyZoomAction *action)
97 {
98         g_signal_emit (action, signals[ZOOM_TO_LEVEL_SIGNAL], 0, zoom);
99 }
100
101 static void
102 sync_zoom_cb (GtkAction *action, GParamSpec *pspec, GtkWidget *proxy)
103 {
104         EphyZoomAction *zoom_action = EPHY_ZOOM_ACTION (action);
105
106         g_object_set (G_OBJECT (proxy), "zoom", zoom_action->priv->zoom, NULL);
107 }
108
109 static void
110 sync_min_zoom_cb (GtkAction *action, GParamSpec *pspec, GtkWidget *proxy)
111 {
112         EphyZoomAction *zoom_action = EPHY_ZOOM_ACTION (action);
113
114         g_object_set (G_OBJECT (proxy), "min-zoom", zoom_action->priv->min_zoom, NULL);
115 }
116
117 static void
118 sync_max_zoom_cb (GtkAction *action, GParamSpec *pspec, GtkWidget *proxy)
119 {
120         EphyZoomAction *zoom_action = EPHY_ZOOM_ACTION (action);
121
122         g_object_set (G_OBJECT (proxy), "max-zoom", zoom_action->priv->max_zoom, NULL);
123 }
124
125 static void
126 connect_proxy (GtkAction *action, GtkWidget *proxy)
127 {
128         if (EPHY_IS_ZOOM_CONTROL (proxy))
129         {
130                 g_signal_connect_object (action, "notify::zoom",
131                                          G_CALLBACK (sync_zoom_cb), proxy, 0);
132                 g_signal_connect_object (action, "notify::min-zoom",
133                                          G_CALLBACK (sync_min_zoom_cb), proxy, 0);
134                 g_signal_connect_object (action, "notify::max-zoom",
135                                          G_CALLBACK (sync_max_zoom_cb), proxy, 0);
136                 g_signal_connect (proxy, "zoom_to_level",
137                                   G_CALLBACK (zoom_to_level_cb), action);
138         }
139
140         GTK_ACTION_CLASS (parent_class)->connect_proxy (action, proxy);
141 }
142
143 static void
144 proxy_menu_activate_cb (GtkMenuItem *menu_item, EphyZoomAction *action)
145 {
146         gint index;
147         float zoom;
148
149         /* menu item was toggled OFF */
150         if (!gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item))) return;
151
152         index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "zoom-level"));
153         zoom = zoom_levels[index].level;
154
155         if (zoom != action->priv->zoom)
156         {
157                 g_signal_emit (action, signals[ZOOM_TO_LEVEL_SIGNAL], 0, zoom);
158         }
159 }
160
161 static GtkWidget *
162 create_menu_item (GtkAction *action)
163 {
164         EphyZoomActionPrivate *p = EPHY_ZOOM_ACTION (action)->priv;
165         GtkWidget *menu, *menu_item;
166         GSList *group = NULL;
167         int i;
168
169         menu = gtk_menu_new ();
170
171         for (i = 0; i < n_zoom_levels; i++)
172         {
173                 if (zoom_levels[i].level == EPHY_ZOOM_SEPARATOR) 
174                 {
175                         menu_item = gtk_separator_menu_item_new ();
176                 } 
177                 else 
178                 {
179                         menu_item = gtk_radio_menu_item_new_with_label (group, 
180                                                                         _(zoom_levels[i].name));
181                         group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menu_item));
182
183                         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
184                                                         p->zoom == zoom_levels[i].level);
185         
186                         g_object_set_data (G_OBJECT (menu_item), "zoom-level", GINT_TO_POINTER (i));
187                         g_signal_connect_object (G_OBJECT (menu_item), "activate",
188                                                 G_CALLBACK (proxy_menu_activate_cb), action, 0);
189                 }
190         
191                 gtk_widget_show (menu_item);
192                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
193         }
194
195         gtk_widget_show (menu);
196
197         menu_item = GTK_ACTION_CLASS (parent_class)->create_menu_item (action);
198
199         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
200
201         gtk_widget_show (menu_item);
202
203         return menu_item;
204 }
205
206 static void
207 ephy_zoom_action_set_property (GObject *object,
208                                guint prop_id,
209                                const GValue *value,
210                                GParamSpec *pspec)
211 {
212         EphyZoomAction *action;
213
214         action = EPHY_ZOOM_ACTION (object);
215
216         switch (prop_id)
217         {
218                 case PROP_ZOOM:
219                         action->priv->zoom = g_value_get_float (value);
220                         break;
221                 case PROP_MIN_ZOOM:
222                         action->priv->min_zoom = g_value_get_float (value);
223                         break;
224                 case PROP_MAX_ZOOM:
225                         action->priv->max_zoom = g_value_get_float (value);
226                         break;
227         }
228 }
229
230 static void
231 ephy_zoom_action_get_property (GObject *object,
232                                guint prop_id,
233                                GValue *value,
234                                GParamSpec *pspec)
235 {
236         EphyZoomAction *action;
237
238         action = EPHY_ZOOM_ACTION (object);
239
240         switch (prop_id)
241         {
242                 case PROP_ZOOM:
243                         g_value_set_float (value, action->priv->zoom);
244                         break;
245                 case PROP_MIN_ZOOM:
246                         g_value_set_float (value, action->priv->min_zoom);
247                         break;
248                 case PROP_MAX_ZOOM:
249                         g_value_set_float (value, action->priv->max_zoom);
250                         break;
251         }
252 }
253
254 static void
255 ephy_zoom_action_class_init (EphyZoomActionClass *class)
256 {
257         GObjectClass *object_class = G_OBJECT_CLASS (class);
258         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
259
260         object_class->set_property = ephy_zoom_action_set_property;
261         object_class->get_property = ephy_zoom_action_get_property;
262
263         parent_class = g_type_class_peek_parent (class);
264
265         action_class->toolbar_item_type = EPHY_TYPE_ZOOM_CONTROL;
266         action_class->connect_proxy = connect_proxy;
267         action_class->create_menu_item = create_menu_item;
268
269         g_object_class_install_property (object_class,
270                                          PROP_ZOOM,
271                                          g_param_spec_float ("zoom",
272                                                              "Zoom",
273                                                              "Zoom",
274                                                              ZOOM_MINIMAL,
275                                                              ZOOM_MAXIMAL,
276                                                              1.0,
277                                                              G_PARAM_READWRITE));
278         g_object_class_install_property (object_class,
279                                          PROP_MIN_ZOOM,
280                                          g_param_spec_float ("min-zoom",
281                                                              "MinZoom",
282                                                              "The minimum zoom",
283                                                              ZOOM_MINIMAL,
284                                                              ZOOM_MAXIMAL,
285                                                              ZOOM_MINIMAL,
286                                                              G_PARAM_READWRITE));
287         g_object_class_install_property (object_class,
288                                          PROP_MAX_ZOOM,
289                                          g_param_spec_float ("max-zoom",
290                                                              "MaxZoom",
291                                                              "The maximum zoom",
292                                                              ZOOM_MINIMAL,
293                                                              ZOOM_MAXIMAL,
294                                                              ZOOM_MAXIMAL,
295                                                              G_PARAM_READWRITE));
296
297         signals[ZOOM_TO_LEVEL_SIGNAL] =
298                 g_signal_new ("zoom_to_level",
299                               G_OBJECT_CLASS_TYPE (object_class),
300                               G_SIGNAL_RUN_FIRST,
301                               G_STRUCT_OFFSET (EphyZoomActionClass, zoom_to_level),
302                               NULL, NULL,
303                               g_cclosure_marshal_VOID__FLOAT,
304                               G_TYPE_NONE,
305                               1,
306                               G_TYPE_FLOAT);
307
308         g_type_class_add_private (object_class, sizeof (EphyZoomActionPrivate));
309 }
310
311 static void
312 ephy_zoom_action_init (EphyZoomAction *action)
313 {
314         action->priv = EPHY_ZOOM_ACTION_GET_PRIVATE (action);
315
316         action->priv->zoom = 1.0;
317 }
318
319 void
320 ephy_zoom_action_set_zoom_level (EphyZoomAction *action, float zoom)
321 {
322         g_return_if_fail (EPHY_IS_ZOOM_ACTION (action));
323
324         if (zoom < ZOOM_MINIMAL || zoom > ZOOM_MAXIMAL) return;
325
326         action->priv->zoom = zoom;
327         g_object_notify (G_OBJECT (action), "zoom");
328 }
329
330 float
331 ephy_zoom_action_get_zoom_level (EphyZoomAction *action)
332 {
333         g_return_val_if_fail (EPHY_IS_ZOOM_ACTION (action), 1.0);
334         
335         return action->priv->zoom;
336 }
337
338 void
339 ephy_zoom_action_set_min_zoom_level (EphyZoomAction *action,
340                                      float           zoom)
341 {
342         g_return_if_fail (EPHY_IS_ZOOM_ACTION (action));
343
344         if (zoom < ZOOM_MINIMAL || zoom > ZOOM_MAXIMAL) return;
345
346         action->priv->min_zoom = zoom;
347         if (action->priv->zoom > 0 && action->priv->zoom < zoom)
348                 ephy_zoom_action_set_zoom_level (action, zoom);
349
350         g_object_notify (G_OBJECT (action), "min-zoom");
351 }
352
353 void
354 ephy_zoom_action_set_max_zoom_level (EphyZoomAction *action,
355                                      float           zoom)
356 {
357         g_return_if_fail (EPHY_IS_ZOOM_ACTION (action));
358
359         if (zoom < ZOOM_MINIMAL || zoom > ZOOM_MAXIMAL) return;
360
361         action->priv->max_zoom = zoom;
362         if (action->priv->zoom > 0 && action->priv->zoom > zoom)
363                 ephy_zoom_action_set_zoom_level (action, zoom);
364
365         g_object_notify (G_OBJECT (action), "max-zoom");
366 }