Updated Spanish translation
[anjuta-git-plugin.git] / libegg / egg-entry-action.c
blob32d17e160907ace00676011535868f4af9bfc893
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /* egg-entry-action widget
5 * Copyright (C) Naba Kumar <naba@gnome.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
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 GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
22 #include <gtk/gtkwidget.h>
23 #include <gtk/gtkentry.h>
24 #include <gtk/gtktoolitem.h>
25 //#include <libegg/toolbar/eggtoolitem.h>
27 #include "egg-entry-action.h"
29 #ifndef _
30 # define _(s) (s)
31 #endif
33 enum {
34 CHANGED,
35 FOCUS_IN,
36 FOCUS_OUT,
37 LAST_SIGNAL
40 enum {
41 PROP_0,
42 PROP_TEXT,
43 PROP_WIDTH
46 static void egg_entry_action_init (EggEntryAction *action);
47 static void egg_entry_action_class_init (EggEntryActionClass *class);
49 GType
50 egg_entry_action_get_type (void)
52 static GtkType type = 0;
54 if (!type)
56 static const GTypeInfo type_info =
58 sizeof (EggEntryActionClass),
59 (GBaseInitFunc) NULL,
60 (GBaseFinalizeFunc) NULL,
61 (GClassInitFunc) egg_entry_action_class_init,
62 (GClassFinalizeFunc) NULL,
63 NULL,
65 sizeof (EggEntryAction),
66 0, /* n_preallocs */
67 (GInstanceInitFunc) egg_entry_action_init,
70 type = g_type_register_static (GTK_TYPE_ACTION,
71 "EggEntryAction",
72 &type_info, 0);
74 return type;
77 //static void egg_entry_action_activate (GtkAction *action);
78 static void egg_entry_action_real_changed (EggEntryAction *action);
79 static GtkWidget * create_tool_item (GtkAction *action);
80 static void connect_proxy (GtkAction *action,
81 GtkWidget *proxy);
82 static void disconnect_proxy (GtkAction *action,
83 GtkWidget *proxy);
84 static void entry_changed (GtkEditable *editable,
85 EggEntryAction *entry_action);
86 static gboolean entry_focus_in (GtkEntry *entry, GdkEvent *event,
87 EggEntryAction *action);
88 static gboolean entry_focus_out (GtkEntry *entry, GdkEvent *event,
89 EggEntryAction *action);
90 static void entry_activate (GtkEntry *entry, GtkAction *action);
91 static void egg_entry_action_finalize (GObject *object);
92 static void egg_entry_action_set_property (GObject *object,
93 guint prop_id,
94 const GValue *value,
95 GParamSpec *pspec);
96 static void egg_entry_action_get_property (GObject *object,
97 guint prop_id,
98 GValue *value,
99 GParamSpec *pspec);
101 static GObjectClass *parent_class = NULL;
102 static guint action_signals[LAST_SIGNAL] = { 0 };
104 static void
105 egg_entry_action_class_init (EggEntryActionClass *class)
107 GtkActionClass *action_class;
108 GObjectClass *object_class;
110 parent_class = g_type_class_peek_parent (class);
111 action_class = GTK_ACTION_CLASS (class);
112 object_class = G_OBJECT_CLASS (class);
114 object_class->finalize = egg_entry_action_finalize;
115 object_class->set_property = egg_entry_action_set_property;
116 object_class->get_property = egg_entry_action_get_property;
118 //action_class->activate = egg_entry_action_activate;
119 action_class->connect_proxy = connect_proxy;
120 action_class->disconnect_proxy = disconnect_proxy;
122 action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
123 action_class->toolbar_item_type = GTK_TYPE_TOOL_ITEM;
124 action_class->create_tool_item = create_tool_item;
126 class->changed = egg_entry_action_real_changed;
128 g_object_class_install_property (object_class,
129 PROP_TEXT,
130 g_param_spec_string ("text",
131 _("Text"),
132 _("Text in the entry"),
134 G_PARAM_READWRITE |
135 G_PARAM_CONSTRUCT_ONLY));
136 g_object_class_install_property (object_class,
137 PROP_WIDTH,
138 g_param_spec_int ("width",
139 _("Width"),
140 _("Width of the entry."),
141 5, 500, 100,
142 G_PARAM_READWRITE));
143 action_signals[CHANGED] =
144 g_signal_new ("changed",
145 G_OBJECT_CLASS_TYPE (class),
146 G_SIGNAL_RUN_FIRST,
147 G_STRUCT_OFFSET (EggEntryActionClass, changed),
148 NULL, NULL,
149 g_cclosure_marshal_VOID__VOID,
150 G_TYPE_NONE, 0);
151 action_signals[FOCUS_IN] =
152 g_signal_new ("focus-in",
153 G_OBJECT_CLASS_TYPE (class),
154 G_SIGNAL_RUN_FIRST,
155 G_STRUCT_OFFSET (EggEntryActionClass, focus_in),
156 NULL, NULL,
157 g_cclosure_marshal_VOID__VOID,
158 G_TYPE_NONE, 0);
159 action_signals[FOCUS_OUT] =
160 g_signal_new ("focus-out",
161 G_OBJECT_CLASS_TYPE (class),
162 G_SIGNAL_RUN_FIRST,
163 G_STRUCT_OFFSET (EggEntryActionClass, focus_out),
164 NULL, NULL,
165 g_cclosure_marshal_VOID__VOID,
166 G_TYPE_NONE, 0);
169 static void
170 egg_entry_action_init (EggEntryAction *action)
172 action->text = g_strdup("");
173 action->width = 100;
176 static void
177 egg_entry_action_finalize (GObject *object)
179 g_return_if_fail (EGG_IS_ENTRY_ACTION (object));
180 g_free (EGG_ENTRY_ACTION (object)->text);
181 if (parent_class->finalize)
182 parent_class->finalize (object);
185 static void
186 egg_entry_action_set_property (GObject *object,
187 guint prop_id,
188 const GValue *value,
189 GParamSpec *pspec)
191 EggEntryAction *action;
193 action = EGG_ENTRY_ACTION (object);
195 switch (prop_id)
197 case PROP_TEXT:
198 egg_entry_action_set_text (action, g_value_get_string (value));
199 break;
200 case PROP_WIDTH:
201 action->width = g_value_get_int (value);
202 break;
203 default:
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 break;
209 static void
210 egg_entry_action_get_property (GObject *object,
211 guint prop_id,
212 GValue *value,
213 GParamSpec *pspec)
215 EggEntryAction *action;
217 action = EGG_ENTRY_ACTION (object);
219 switch (prop_id)
221 case PROP_TEXT:
222 g_value_set_string (value, action->text);
223 break;
224 case PROP_WIDTH:
225 g_value_set_int (value, action->width);
226 break;
227 default:
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 break;
233 static void
234 egg_entry_action_real_changed (EggEntryAction *action)
236 GSList *slist;
238 g_return_if_fail (EGG_IS_ENTRY_ACTION (action));
240 for (slist = gtk_action_get_proxies (GTK_ACTION (action));
241 slist; slist = slist->next)
243 GtkWidget *proxy = slist->data;
245 gtk_action_block_activate_from (GTK_ACTION (action), proxy);
246 if (GTK_IS_CHECK_MENU_ITEM (proxy))
247 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (proxy),
248 TRUE);
249 else if (GTK_IS_TOOL_ITEM (proxy))
251 GtkWidget *entry;
252 entry = gtk_bin_get_child (GTK_BIN (proxy));
253 if (GTK_IS_ENTRY (entry))
255 g_signal_handlers_block_by_func (entry,
256 G_CALLBACK (entry_changed),
257 action);
258 gtk_entry_set_text (GTK_ENTRY (entry), action->text);
259 g_signal_handlers_unblock_by_func (entry,
260 G_CALLBACK (entry_changed),
261 action);
263 else
265 g_warning ("Don't know how to change `%s' widgets",
266 G_OBJECT_TYPE_NAME (proxy));
269 else
271 g_warning ("Don't know how to change `%s' widgets",
272 G_OBJECT_TYPE_NAME (proxy));
274 gtk_action_unblock_activate_from (GTK_ACTION (action), proxy);
278 static GtkWidget *
279 create_tool_item (GtkAction *action)
281 GtkToolItem *item;
282 GtkWidget *entry;
284 g_return_val_if_fail (EGG_IS_ENTRY_ACTION (action), NULL);
286 item = gtk_tool_item_new ();
287 entry = gtk_entry_new();
288 gtk_widget_set_size_request (entry, EGG_ENTRY_ACTION (action)->width, -1);
289 gtk_widget_show(entry);
290 gtk_container_add (GTK_CONTAINER (item), entry);
291 return GTK_WIDGET (item);
294 static void
295 connect_proxy (GtkAction *action, GtkWidget *proxy)
297 EggEntryAction *entry_action;
299 entry_action = EGG_ENTRY_ACTION (action);
301 /* do this before hand, so that we don't call the "activate" handler */
302 if (GTK_IS_MENU_ITEM (proxy))
303 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (proxy),
304 TRUE);
305 else if (GTK_IS_TOOL_ITEM (proxy))
307 GtkWidget *entry;
308 entry = gtk_bin_get_child (GTK_BIN (proxy));
309 if (GTK_IS_ENTRY (entry))
311 gtk_entry_set_text (GTK_ENTRY (entry), entry_action->text);
312 g_signal_connect (entry, "activate",
313 G_CALLBACK (entry_activate), action);
314 g_signal_connect (entry, "changed",
315 G_CALLBACK (entry_changed), action);
316 g_signal_connect (entry, "focus-in-event",
317 G_CALLBACK (entry_focus_in), action);
318 g_signal_connect (entry, "focus-out-event",
319 G_CALLBACK (entry_focus_out), action);
322 (* GTK_ACTION_CLASS (parent_class)->connect_proxy) (action, proxy);
325 static void
326 disconnect_proxy (GtkAction *action, GtkWidget *proxy)
328 EggEntryAction *entry_action;
330 entry_action = EGG_ENTRY_ACTION (action);
331 if (GTK_IS_TOOL_ITEM (proxy))
333 GtkWidget *entry;
334 entry = gtk_bin_get_child (GTK_BIN (proxy));
335 if (GTK_IS_ENTRY (entry))
337 g_signal_handlers_disconnect_by_func (entry,
338 G_CALLBACK (entry_changed),
339 action);
340 g_signal_handlers_disconnect_by_func (entry,
341 G_CALLBACK (entry_activate),
342 action);
343 g_signal_handlers_disconnect_by_func (entry,
344 G_CALLBACK (entry_focus_in),
345 action);
346 g_signal_handlers_disconnect_by_func (entry,
347 G_CALLBACK (entry_focus_out),
348 action);
351 (* GTK_ACTION_CLASS (parent_class)->disconnect_proxy) (action, proxy);
354 static void
355 entry_changed (GtkEditable *editable, EggEntryAction *entry_action)
357 if (entry_action->text)
358 g_free (entry_action->text);
359 entry_action->text = g_strdup (gtk_entry_get_text (GTK_ENTRY (editable)));
360 egg_entry_action_changed (entry_action);
363 static void
364 entry_activate (GtkEntry *entry, GtkAction *action)
366 gtk_action_activate (action);
369 static gboolean
370 entry_focus_in (GtkEntry *entry, GdkEvent *event, EggEntryAction *action)
372 g_signal_emit (action, action_signals[FOCUS_IN], 0);
373 return FALSE;
376 static gboolean
377 entry_focus_out (GtkEntry *entry, GdkEvent *event, EggEntryAction *action)
379 g_signal_emit (action, action_signals[FOCUS_OUT], 0);
380 return FALSE;
384 * egg_entry_action_changed:
385 * @action: the action object
387 * Emits the "changed" signal on the toggle action.
389 void
390 egg_entry_action_changed (EggEntryAction *action)
392 g_return_if_fail (EGG_IS_ENTRY_ACTION (action));
394 g_signal_emit (action, action_signals[CHANGED], 0);
398 * egg_entry_action_set_text:
399 * @action: the action object
400 * @text: Text to set in the entry
402 * Sets the checked state on the toggle action.
404 void
405 egg_entry_action_set_text (EggEntryAction *action, const gchar *text)
407 g_return_if_fail (EGG_IS_ENTRY_ACTION (action));
408 g_return_if_fail (text != NULL);
409 if (action->text)
410 g_free (action->text);
411 action->text = g_strdup (text);
412 egg_entry_action_changed (action);
416 * egg_entry_action_get_active:
417 * @action: the action object
419 * Returns: The text in the entry
421 const gchar *
422 egg_entry_action_get_text (EggEntryAction *action)
424 g_return_val_if_fail (EGG_IS_ENTRY_ACTION (action), FALSE);
426 return action->text;