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"
46 static void egg_entry_action_init (EggEntryAction
*action
);
47 static void egg_entry_action_class_init (EggEntryActionClass
*class);
50 egg_entry_action_get_type (void)
52 static GtkType type
= 0;
56 static const GTypeInfo type_info
=
58 sizeof (EggEntryActionClass
),
60 (GBaseFinalizeFunc
) NULL
,
61 (GClassInitFunc
) egg_entry_action_class_init
,
62 (GClassFinalizeFunc
) NULL
,
65 sizeof (EggEntryAction
),
67 (GInstanceInitFunc
) egg_entry_action_init
,
70 type
= g_type_register_static (GTK_TYPE_ACTION
,
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
,
82 static void disconnect_proxy (GtkAction
*action
,
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
,
96 static void egg_entry_action_get_property (GObject
*object
,
101 static GObjectClass
*parent_class
= NULL
;
102 static guint action_signals
[LAST_SIGNAL
] = { 0 };
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
,
130 g_param_spec_string ("text",
132 _("Text in the entry"),
135 G_PARAM_CONSTRUCT_ONLY
));
136 g_object_class_install_property (object_class
,
138 g_param_spec_int ("width",
140 _("Width of the entry."),
143 action_signals
[CHANGED
] =
144 g_signal_new ("changed",
145 G_OBJECT_CLASS_TYPE (class),
147 G_STRUCT_OFFSET (EggEntryActionClass
, changed
),
149 g_cclosure_marshal_VOID__VOID
,
151 action_signals
[FOCUS_IN
] =
152 g_signal_new ("focus-in",
153 G_OBJECT_CLASS_TYPE (class),
155 G_STRUCT_OFFSET (EggEntryActionClass
, focus_in
),
157 g_cclosure_marshal_VOID__VOID
,
159 action_signals
[FOCUS_OUT
] =
160 g_signal_new ("focus-out",
161 G_OBJECT_CLASS_TYPE (class),
163 G_STRUCT_OFFSET (EggEntryActionClass
, focus_out
),
165 g_cclosure_marshal_VOID__VOID
,
170 egg_entry_action_init (EggEntryAction
*action
)
172 action
->text
= g_strdup("");
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
);
186 egg_entry_action_set_property (GObject
*object
,
191 EggEntryAction
*action
;
193 action
= EGG_ENTRY_ACTION (object
);
198 egg_entry_action_set_text (action
, g_value_get_string (value
));
201 action
->width
= g_value_get_int (value
);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
210 egg_entry_action_get_property (GObject
*object
,
215 EggEntryAction
*action
;
217 action
= EGG_ENTRY_ACTION (object
);
222 g_value_set_string (value
, action
->text
);
225 g_value_set_int (value
, action
->width
);
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
234 egg_entry_action_real_changed (EggEntryAction
*action
)
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
),
249 else if (GTK_IS_TOOL_ITEM (proxy
))
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
),
258 gtk_entry_set_text (GTK_ENTRY (entry
), action
->text
);
259 g_signal_handlers_unblock_by_func (entry
,
260 G_CALLBACK (entry_changed
),
265 g_warning ("Don't know how to change `%s' widgets",
266 G_OBJECT_TYPE_NAME (proxy
));
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
);
279 create_tool_item (GtkAction
*action
)
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
);
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
),
305 else if (GTK_IS_TOOL_ITEM (proxy
))
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
);
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
))
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
),
340 g_signal_handlers_disconnect_by_func (entry
,
341 G_CALLBACK (entry_activate
),
343 g_signal_handlers_disconnect_by_func (entry
,
344 G_CALLBACK (entry_focus_in
),
346 g_signal_handlers_disconnect_by_func (entry
,
347 G_CALLBACK (entry_focus_out
),
351 (* GTK_ACTION_CLASS (parent_class
)->disconnect_proxy
) (action
, proxy
);
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
);
364 entry_activate (GtkEntry
*entry
, GtkAction
*action
)
366 gtk_action_activate (action
);
370 entry_focus_in (GtkEntry
*entry
, GdkEvent
*event
, EggEntryAction
*action
)
372 g_signal_emit (action
, action_signals
[FOCUS_IN
], 0);
377 entry_focus_out (GtkEntry
*entry
, GdkEvent
*event
, EggEntryAction
*action
)
379 g_signal_emit (action
, action_signals
[FOCUS_OUT
], 0);
384 * egg_entry_action_changed:
385 * @action: the action object
387 * Emits the "changed" signal on the toggle action.
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.
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
);
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
422 egg_entry_action_get_text (EggEntryAction
*action
)
424 g_return_val_if_fail (EGG_IS_ENTRY_ACTION (action
), FALSE
);