]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/zoom-control/ephy-zoom-action.c
3aadf84eb36bd484b152fa4eff69aa7818543d04
[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/gtkmenu.h>
33 #include <gtk/gtkmenuitem.h>
34 #include <gtk/gtkcheckmenuitem.h>
35 #include <gtk/gtkradiomenuitem.h>
36 #include <gtk/gtkseparatormenuitem.h>
37
38 #define EPHY_ZOOM_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_ZOOM_ACTION, EphyZoomActionPrivate))
39
40 struct _EphyZoomActionPrivate
41 {
42         float zoom;
43 };
44
45 enum
46 {
47         PROP_0,
48         PROP_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 connect_proxy (GtkAction *action, GtkWidget *proxy)
111 {
112         if (EPHY_IS_ZOOM_CONTROL (proxy))
113         {
114                 g_signal_connect_object (action, "notify::zoom",
115                                          G_CALLBACK (sync_zoom_cb), proxy, 0);
116         
117                 g_signal_connect (proxy, "zoom_to_level",
118                                   G_CALLBACK (zoom_to_level_cb), action);
119         }
120
121         GTK_ACTION_CLASS (parent_class)->connect_proxy (action, proxy);
122 }
123
124 static void
125 proxy_menu_activate_cb (GtkMenuItem *menu_item, EphyZoomAction *action)
126 {
127         gint index;
128         float zoom;
129
130         /* menu item was toggled OFF */
131         if (!gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item))) return;
132
133         index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "zoom-level"));
134         zoom = zoom_levels[index].level;
135
136         if (zoom != action->priv->zoom)
137         {
138                 g_signal_emit (action, signals[ZOOM_TO_LEVEL_SIGNAL], 0, zoom);
139         }
140 }
141
142 static GtkWidget *
143 create_menu_item (GtkAction *action)
144 {
145         EphyZoomActionPrivate *p = EPHY_ZOOM_ACTION (action)->priv;
146         GtkWidget *menu, *menu_item;
147         GSList *group = NULL;
148         int i;
149
150         menu = gtk_menu_new ();
151
152         for (i = 0; i < n_zoom_levels; i++)
153         {
154                 if (zoom_levels[i].level == EPHY_ZOOM_SEPARATOR) 
155                 {
156                         menu_item = gtk_separator_menu_item_new ();
157                 } 
158                 else 
159                 {
160                         menu_item = gtk_radio_menu_item_new_with_label (group, 
161                                                                         zoom_levels[i].name);
162                         group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menu_item));
163
164                         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
165                                                         p->zoom == zoom_levels[i].level);
166         
167                         g_object_set_data (G_OBJECT (menu_item), "zoom-level", GINT_TO_POINTER (i));
168                         g_signal_connect_object (G_OBJECT (menu_item), "activate",
169                                                 G_CALLBACK (proxy_menu_activate_cb), action, 0);
170                 }
171         
172                 gtk_widget_show (menu_item);
173                 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
174         }
175
176         gtk_widget_show (menu);
177
178         menu_item = GTK_ACTION_CLASS (parent_class)->create_menu_item (action);
179
180         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
181
182         gtk_widget_show (menu_item);
183
184         return menu_item;
185 }
186
187 static void
188 ephy_zoom_action_set_property (GObject *object,
189                                guint prop_id,
190                                const GValue *value,
191                                GParamSpec *pspec)
192 {
193         EphyZoomAction *action;
194
195         action = EPHY_ZOOM_ACTION (object);
196
197         switch (prop_id)
198         {
199                 case PROP_ZOOM:
200                         action->priv->zoom = g_value_get_float (value);
201                         break;
202         }
203 }
204
205 static void
206 ephy_zoom_action_get_property (GObject *object,
207                                guint prop_id,
208                                GValue *value,
209                                GParamSpec *pspec)
210 {
211         EphyZoomAction *action;
212
213         action = EPHY_ZOOM_ACTION (object);
214
215         switch (prop_id)
216         {
217                 case PROP_ZOOM:
218                         g_value_set_float (value, action->priv->zoom);
219                         break;
220         }
221 }
222
223 static void
224 ephy_zoom_action_class_init (EphyZoomActionClass *class)
225 {
226         GObjectClass *object_class = G_OBJECT_CLASS (class);
227         GtkActionClass *action_class = GTK_ACTION_CLASS (class);
228
229         object_class->set_property = ephy_zoom_action_set_property;
230         object_class->get_property = ephy_zoom_action_get_property;
231
232         parent_class = g_type_class_peek_parent (class);
233
234         action_class->toolbar_item_type = EPHY_TYPE_ZOOM_CONTROL;
235         action_class->connect_proxy = connect_proxy;
236         action_class->create_menu_item = create_menu_item;
237
238         g_object_class_install_property (object_class,
239                                          PROP_ZOOM,
240                                          g_param_spec_float ("zoom",
241                                                              "Zoom",
242                                                              "Zoom",
243                                                              ZOOM_MINIMAL,
244                                                              ZOOM_MAXIMAL,
245                                                              1.0,
246                                                              G_PARAM_READWRITE));
247
248         signals[ZOOM_TO_LEVEL_SIGNAL] =
249                 g_signal_new ("zoom_to_level",
250                               G_OBJECT_CLASS_TYPE (object_class),
251                               G_SIGNAL_RUN_FIRST,
252                               G_STRUCT_OFFSET (EphyZoomActionClass, zoom_to_level),
253                               NULL, NULL,
254                               g_cclosure_marshal_VOID__FLOAT,
255                               G_TYPE_NONE,
256                               1,
257                               G_TYPE_FLOAT);
258
259         g_type_class_add_private (object_class, sizeof (EphyZoomActionPrivate));
260 }
261
262 static void
263 ephy_zoom_action_init (EphyZoomAction *action)
264 {
265         action->priv = EPHY_ZOOM_ACTION_GET_PRIVATE (action);
266
267         action->priv->zoom = 1.0;
268 }
269
270 void
271 ephy_zoom_action_set_zoom_level (EphyZoomAction *action, float zoom)
272 {
273         g_return_if_fail (EPHY_IS_ZOOM_ACTION (action));
274
275         if (zoom < ZOOM_MINIMAL || zoom > ZOOM_MAXIMAL) return;
276
277         action->priv->zoom = zoom;
278         g_object_notify (G_OBJECT (action), "zoom");
279 }
280
281 float
282 ephy_zoom_action_get_zoom_level (EphyZoomAction *action)
283 {
284         g_return_val_if_fail (EPHY_IS_ZOOM_ACTION (action), 1.0);
285         
286         return action->priv->zoom;
287 }