]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/zoom-control/ephy-zoom-action.c
986e3ef8dffce7c2eb5ccce7657284d0b8d2c548
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 };
40
41 enum
42 {
43         PROP_0,
44         PROP_ZOOM
45 };
46
47
48 static void ephy_zoom_action_init       (EphyZoomAction *action);
49 static void ephy_zoom_action_class_init (EphyZoomActionClass *class);
50
51 enum
52 {
53         ZOOM_TO_LEVEL_SIGNAL,
54         LAST_SIGNAL
55 };
56
57 static guint signals[LAST_SIGNAL] = { 0 };
58
59 static GObjectClass *parent_class = NULL;
60
61 GType
62 ephy_zoom_action_get_type (void)
63 {
64         static GType type = 0;
65
66         if (G_UNLIKELY (type == 0))
67         {
68                 const GTypeInfo our_info =
69                         {
70                                 sizeof (EphyZoomActionClass),
71                                 NULL, /* base_init */
72                                 NULL, /* base_finalize */
73                                 (GClassInitFunc) ephy_zoom_action_class_init,
74                                 NULL,
75                                 NULL, /* class_data */
76                                 sizeof (EphyZoomAction),
77                                 0, /* n_preallocs */
78                                 (GInstanceInitFunc) ephy_zoom_action_init,
79                         };
80
81                 type = g_type_register_static (GTK_TYPE_ACTION,
82                                                "EphyZoomAction",
83                                                &our_info, 0);
84         }
85
86         return type;
87 }
88
89 static void
90 zoom_to_level_cb (EphyZoomControl *control,
91                   float zoom,
92                   EphyZoomAction *action)
93 {
94         g_signal_emit (action, signals[ZOOM_TO_LEVEL_SIGNAL], 0, zoom);
95 }
96
97 static void
98 sync_zoom_cb (GtkAction *action, GParamSpec *pspec, GtkWidget *proxy)
99 {
100         EphyZoomAction *zoom_action = EPHY_ZOOM_ACTION (action);
101
102         g_object_set (G_OBJECT (proxy), "zoom", zoom_action->priv->zoom, NULL);
103 }
104
105 static void
106 connect_proxy (GtkAction *action, GtkWidget *proxy)
107 {
108         if (EPHY_IS_ZOOM_CONTROL (proxy))
109         {
110                 g_signal_connect_object (action, "notify::zoom",
111                                          G_CALLBACK (sync_zoom_cb), proxy, 0);
112         
113                 g_signal_connect (proxy, "zoom_to_level",
114                                   G_CALLBACK (zoom_to_level_cb), action);
115         }
116
117         GTK_ACTION_CLASS (parent_class)->connect_proxy (action, proxy);
118 }
119
120 static void
121 proxy_menu_activate_cb (GtkMenuItem *menu_item, EphyZoomAction *action)
122 {
123         gint index;
124         float zoom;
125
126         /* menu item was toggled OFF */
127         if (!gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item))) return;
128
129         index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "zoom-level"));
130         zoom = zoom_levels[index].level;
131
132         if (zoom != action->priv->zoom)
133         {
134                 g_signal_emit (action, signals[ZOOM_TO_LEVEL_SIGNAL], 0, zoom);
135         }
136 }
137
138 static GtkWidget *
139 create_menu_item (GtkAction *action)
140 {
141         EphyZoomActionPrivate *p = EPHY_ZOOM_ACTION (action)->priv;
142         GtkWidget *menu, *menu_item;
143         GSList *group = NULL;
144         int i;
145
146         menu = gtk_menu_new ();
147
148         for (i = 0; i < n_zoom_levels; i++)
149         {
150                 if (zoom_levels[i].level == EPHY_ZOOM_SEPARATOR) 
151                 {
152                         menu_item = gtk_separator_menu_item_new ();
153                 } 
154                 else 
155                 {
156                         menu_item = gtk_radio_menu_item_new_with_label (group, 
157                                                                         zoom_levels[i].name);
158                         group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menu_item));
159
160                         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
161                                                         p->zoom == zoom_levels[i].level);
162         
163                         g_object_set_data (G_OBJECT (menu_item), "zoom-level", GINT_TO_POINTER (i));
164                         g_signal_connect_object (G_OBJECT (menu_item), "activate",
165                                                 G_CALLBACK (proxy_menu_activate_cb), action, 0);
166                 }
167         
168                 gtk_widget_show (menu_item);
169                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
170         }
171
172         gtk_widget_show (menu);
173
174         menu_item = GTK_ACTION_CLASS (parent_class)->create_menu_item (action);
175
176         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
177
178         gtk_widget_show (menu_item);
179
180         return menu_item;
181 }
182
183 static void
184 ephy_zoom_action_set_property (GObject *object,
185                                guint prop_id,
186                                const GValue *value,
187                                GParamSpec *pspec)
188 {
189         EphyZoomAction *action;
190
191         action = EPHY_ZOOM_ACTION (object);
192
193         switch (prop_id)
194         {
195                 case PROP_ZOOM:
196                         action->priv->zoom = g_value_get_float (value);
197                         break;
198         }
199 }
200
201 static void
202 ephy_zoom_action_get_property (GObject *object,
203                                guint prop_id,
204                                GValue *value,
205                                GParamSpec *pspec)
206 {
207         EphyZoomAction *action;
208
209         action = EPHY_ZOOM_ACTION (object);
210
211         switch (prop_id)
212         {
213                 case PROP_ZOOM:
214                         g_value_set_float (value, action->priv->zoom);
215                         break;
216         }
217 }
218
219 static void
220 ephy_zoom_action_class_init (EphyZoomActionClass *class)
221 {
222         GObjectClass *object_class = G_OBJECT_CLASS (class);
223         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
224
225         object_class->set_property = ephy_zoom_action_set_property;
226         object_class->get_property = ephy_zoom_action_get_property;
227
228         parent_class = g_type_class_peek_parent (class);
229
230         action_class->toolbar_item_type = EPHY_TYPE_ZOOM_CONTROL;
231         action_class->connect_proxy = connect_proxy;
232         action_class->create_menu_item = create_menu_item;
233
234         g_object_class_install_property (object_class,
235                                          PROP_ZOOM,
236                                          g_param_spec_float ("zoom",
237                                                              "Zoom",
238                                                              "Zoom",
239                                                              ZOOM_MINIMAL,
240                                                              ZOOM_MAXIMAL,
241                                                              1.0,
242                                                              G_PARAM_READWRITE));
243
244         signals[ZOOM_TO_LEVEL_SIGNAL] =
245                 g_signal_new ("zoom_to_level",
246                               G_OBJECT_CLASS_TYPE (object_class),
247                               G_SIGNAL_RUN_FIRST,
248                               G_STRUCT_OFFSET (EphyZoomActionClass, zoom_to_level),
249                               NULL, NULL,
250                               g_cclosure_marshal_VOID__FLOAT,
251                               G_TYPE_NONE,
252                               1,
253                               G_TYPE_FLOAT);
254
255         g_type_class_add_private (object_class, sizeof (EphyZoomActionPrivate));
256 }
257
258 static void
259 ephy_zoom_action_init (EphyZoomAction *action)
260 {
261         action->priv = EPHY_ZOOM_ACTION_GET_PRIVATE (action);
262
263         action->priv->zoom = 1.0;
264 }
265
266 void
267 ephy_zoom_action_set_zoom_level (EphyZoomAction *action, float zoom)
268 {
269         g_return_if_fail (EPHY_IS_ZOOM_ACTION (action));
270
271         if (zoom < ZOOM_MINIMAL || zoom > ZOOM_MAXIMAL) return;
272
273         action->priv->zoom = zoom;
274         g_object_notify (G_OBJECT (action), "zoom");
275 }
276
277 float
278 ephy_zoom_action_get_zoom_level (EphyZoomAction *action)
279 {
280         g_return_val_if_fail (EPHY_IS_ZOOM_ACTION (action), 1.0);
281         
282         return action->priv->zoom;
283 }