Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / lib / widgets / ephy-zoom-action.c
blob998c0ce2ada7d1b930690967836529a403ffa637
1 /*
2 * Copyright © 2003 Marco Pesenti Gritti
3 * Copyright © 2003, 2004 Christian Persch
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * $Id: ephy-zoom-action.c 6952 2007-03-11 19:42:02Z chpe $
22 #include "config.h"
24 #include "ephy-zoom-action.h"
25 #include "ephy-zoom-control.h"
26 #include "ephy-zoom.h"
28 #include <glib-object.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtkmenu.h>
31 #include <gtk/gtkmenuitem.h>
32 #include <gtk/gtkcheckmenuitem.h>
33 #include <gtk/gtkradiomenuitem.h>
35 #define EPHY_ZOOM_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_ZOOM_ACTION, EphyZoomActionPrivate))
37 struct _EphyZoomActionPrivate
39 float zoom;
42 enum
44 PROP_0,
45 PROP_ZOOM
49 static void ephy_zoom_action_init (EphyZoomAction *action);
50 static void ephy_zoom_action_class_init (EphyZoomActionClass *class);
52 enum
54 ZOOM_TO_LEVEL_SIGNAL,
55 LAST_SIGNAL
58 static guint signals[LAST_SIGNAL] = { 0 };
60 static GObjectClass *parent_class = NULL;
62 GType
63 ephy_zoom_action_get_type (void)
65 static GType type = 0;
67 if (G_UNLIKELY (type == 0))
69 const GTypeInfo our_info =
71 sizeof (EphyZoomActionClass),
72 NULL, /* base_init */
73 NULL, /* base_finalize */
74 (GClassInitFunc) ephy_zoom_action_class_init,
75 NULL,
76 NULL, /* class_data */
77 sizeof (EphyZoomAction),
78 0, /* n_preallocs */
79 (GInstanceInitFunc) ephy_zoom_action_init,
82 type = g_type_register_static (GTK_TYPE_ACTION,
83 "EphyZoomAction",
84 &our_info, 0);
87 return type;
90 static void
91 zoom_to_level_cb (EphyZoomControl *control,
92 float zoom,
93 EphyZoomAction *action)
95 g_signal_emit (action, signals[ZOOM_TO_LEVEL_SIGNAL], 0, zoom);
98 static void
99 sync_zoom_cb (GtkAction *action, GParamSpec *pspec, GtkWidget *proxy)
101 EphyZoomAction *zoom_action = EPHY_ZOOM_ACTION (action);
103 g_object_set (G_OBJECT (proxy), "zoom", zoom_action->priv->zoom, NULL);
106 static void
107 connect_proxy (GtkAction *action, GtkWidget *proxy)
109 if (EPHY_IS_ZOOM_CONTROL (proxy))
111 g_signal_connect_object (action, "notify::zoom",
112 G_CALLBACK (sync_zoom_cb), proxy, 0);
114 g_signal_connect (proxy, "zoom_to_level",
115 G_CALLBACK (zoom_to_level_cb), action);
118 GTK_ACTION_CLASS (parent_class)->connect_proxy (action, proxy);
121 static void
122 proxy_menu_activate_cb (GtkMenuItem *menu_item, EphyZoomAction *action)
124 gint index;
125 float zoom;
127 /* menu item was toggled OFF */
128 if (!gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item))) return;
130 index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item), "zoom-level"));
131 zoom = zoom_levels[index].level;
133 if (zoom != action->priv->zoom)
135 g_signal_emit (action, signals[ZOOM_TO_LEVEL_SIGNAL], 0, zoom);
139 static GtkWidget *
140 create_menu_item (GtkAction *action)
142 EphyZoomActionPrivate *p = EPHY_ZOOM_ACTION (action)->priv;
143 GtkWidget *menu, *menu_item;
144 GSList *group = NULL;
145 int i;
147 menu = gtk_menu_new ();
149 for (i = 0; i < n_zoom_levels; i++)
151 menu_item = gtk_radio_menu_item_new_with_label (group, _(zoom_levels[i].name));
152 group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menu_item));
154 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
155 p->zoom == zoom_levels[i].level);
157 gtk_widget_show (menu_item);
158 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
160 g_object_set_data (G_OBJECT (menu_item), "zoom-level", GINT_TO_POINTER (i));
161 g_signal_connect_object (G_OBJECT (menu_item), "activate",
162 G_CALLBACK (proxy_menu_activate_cb), action, 0);
165 gtk_widget_show (menu);
167 menu_item = GTK_ACTION_CLASS (parent_class)->create_menu_item (action);
169 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
171 gtk_widget_show (menu_item);
173 return menu_item;
176 static void
177 ephy_zoom_action_set_property (GObject *object,
178 guint prop_id,
179 const GValue *value,
180 GParamSpec *pspec)
182 EphyZoomAction *action;
184 action = EPHY_ZOOM_ACTION (object);
186 switch (prop_id)
188 case PROP_ZOOM:
189 action->priv->zoom = g_value_get_float (value);
190 break;
194 static void
195 ephy_zoom_action_get_property (GObject *object,
196 guint prop_id,
197 GValue *value,
198 GParamSpec *pspec)
200 EphyZoomAction *action;
202 action = EPHY_ZOOM_ACTION (object);
204 switch (prop_id)
206 case PROP_ZOOM:
207 g_value_set_float (value, action->priv->zoom);
208 break;
212 static void
213 ephy_zoom_action_class_init (EphyZoomActionClass *class)
215 GObjectClass *object_class = G_OBJECT_CLASS (class);
216 GtkActionClass *action_class = GTK_ACTION_CLASS (class);
218 object_class->set_property = ephy_zoom_action_set_property;
219 object_class->get_property = ephy_zoom_action_get_property;
221 parent_class = g_type_class_peek_parent (class);
223 action_class->toolbar_item_type = EPHY_TYPE_ZOOM_CONTROL;
224 action_class->connect_proxy = connect_proxy;
225 action_class->create_menu_item = create_menu_item;
227 g_object_class_install_property (object_class,
228 PROP_ZOOM,
229 g_param_spec_float ("zoom",
230 "Zoom",
231 "Zoom",
232 ZOOM_MINIMAL,
233 ZOOM_MAXIMAL,
234 1.0,
235 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
237 signals[ZOOM_TO_LEVEL_SIGNAL] =
238 g_signal_new ("zoom_to_level",
239 G_OBJECT_CLASS_TYPE (object_class),
240 G_SIGNAL_RUN_FIRST,
241 G_STRUCT_OFFSET (EphyZoomActionClass, zoom_to_level),
242 NULL, NULL,
243 g_cclosure_marshal_VOID__FLOAT,
244 G_TYPE_NONE,
246 G_TYPE_FLOAT);
248 g_type_class_add_private (object_class, sizeof (EphyZoomActionPrivate));
251 static void
252 ephy_zoom_action_init (EphyZoomAction *action)
254 action->priv = EPHY_ZOOM_ACTION_GET_PRIVATE (action);
256 action->priv->zoom = 1.0;
259 void
260 ephy_zoom_action_set_zoom_level (EphyZoomAction *action, float zoom)
262 g_return_if_fail (EPHY_IS_ZOOM_ACTION (action));
264 if (zoom < ZOOM_MINIMAL || zoom > ZOOM_MAXIMAL) return;
266 action->priv->zoom = zoom;
267 g_object_notify (G_OBJECT (action), "zoom");
270 float
271 ephy_zoom_action_get_zoom_level (EphyZoomAction *action)
273 g_return_val_if_fail (EPHY_IS_ZOOM_ACTION (action), 1.0);
275 return action->priv->zoom;