1 /* gtktoggletoolbutton.c
3 * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
4 * Copyright (C) 2002 James Henstridge <james@daa.com.au>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "gtktoggletoolbutton.h"
24 #include "gtkcheckmenuitem.h"
26 #include "gtktogglebutton.h"
29 #include "gtkradiotoolbutton.h"
30 #include "gtktoggleaction.h"
31 #include "gtkactivatable.h"
32 #include "gtkprivate.h"
35 #define MENU_ID "gtk-toggle-tool-button-menu-id"
48 #define GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TOGGLE_TOOL_BUTTON, GtkToggleToolButtonPrivate))
50 struct _GtkToggleToolButtonPrivate
56 static void gtk_toggle_tool_button_set_property (GObject *object,
60 static void gtk_toggle_tool_button_get_property (GObject *object,
65 static gboolean gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *button);
67 static void button_toggled (GtkWidget *widget,
68 GtkToggleToolButton *button);
69 static void menu_item_activated (GtkWidget *widget,
70 GtkToggleToolButton *button);
73 static void gtk_toggle_tool_button_activatable_interface_init (GtkActivatableIface *iface);
74 static void gtk_toggle_tool_button_update (GtkActivatable *activatable,
76 const gchar *property_name);
77 static void gtk_toggle_tool_button_sync_action_properties (GtkActivatable *activatable,
80 static GtkActivatableIface *parent_activatable_iface;
81 static guint toggle_signals[LAST_SIGNAL] = { 0 };
83 G_DEFINE_TYPE_WITH_CODE (GtkToggleToolButton, gtk_toggle_tool_button, GTK_TYPE_TOOL_BUTTON,
84 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
85 gtk_toggle_tool_button_activatable_interface_init))
88 gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass)
90 GObjectClass *object_class;
91 GtkToolItemClass *toolitem_class;
92 GtkToolButtonClass *toolbutton_class;
94 object_class = (GObjectClass *)klass;
95 toolitem_class = (GtkToolItemClass *)klass;
96 toolbutton_class = (GtkToolButtonClass *)klass;
98 object_class->set_property = gtk_toggle_tool_button_set_property;
99 object_class->get_property = gtk_toggle_tool_button_get_property;
101 toolitem_class->create_menu_proxy = gtk_toggle_tool_button_create_menu_proxy;
102 toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON;
105 * GtkToggleToolButton:active:
107 * If the toggle tool button should be pressed in or not.
111 g_object_class_install_property (object_class,
113 g_param_spec_boolean ("active",
115 P_("If the toggle button should be pressed in or not"),
117 GTK_PARAM_READWRITE));
120 * GtkToggleToolButton::toggled:
121 * @toggle_tool_button: the object that emitted the signal
123 * Emitted whenever the toggle tool button changes state.
125 toggle_signals[TOGGLED] =
126 g_signal_new (I_("toggled"),
127 G_OBJECT_CLASS_TYPE (klass),
129 G_STRUCT_OFFSET (GtkToggleToolButtonClass, toggled),
131 g_cclosure_marshal_VOID__VOID,
134 g_type_class_add_private (object_class, sizeof (GtkToggleToolButtonPrivate));
138 gtk_toggle_tool_button_init (GtkToggleToolButton *button)
140 GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
141 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button));
143 button->priv = GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE (button);
145 /* If the real button is a radio button, it may have been
146 * active at the time it was created.
148 button->priv->active = gtk_toggle_button_get_active (toggle_button);
150 g_signal_connect_object (toggle_button,
151 "toggled", G_CALLBACK (button_toggled), button, 0);
155 gtk_toggle_tool_button_set_property (GObject *object,
160 GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
165 gtk_toggle_tool_button_set_active (button,
166 g_value_get_boolean (value));
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 gtk_toggle_tool_button_get_property (GObject *object,
181 GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
186 g_value_set_boolean (value, gtk_toggle_tool_button_get_active (button));
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *item)
198 GtkToolButton *tool_button = GTK_TOOL_BUTTON (item);
199 GtkToggleToolButton *toggle_tool_button = GTK_TOGGLE_TOOL_BUTTON (item);
200 GtkWidget *menu_item = NULL;
201 GtkStockItem stock_item;
202 gboolean use_mnemonic = TRUE;
204 GtkWidget *label_widget;
205 const gchar *label_text;
206 const gchar *stock_id;
208 if (_gtk_tool_item_create_menu_proxy (item))
211 label_widget = gtk_tool_button_get_label_widget (tool_button);
212 label_text = gtk_tool_button_get_label (tool_button);
213 stock_id = gtk_tool_button_get_stock_id (tool_button);
215 if (GTK_IS_LABEL (label_widget))
217 label = gtk_label_get_label (GTK_LABEL (label_widget));
218 use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (label_widget));
223 use_mnemonic = gtk_tool_button_get_use_underline (tool_button);
225 else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
227 label = stock_item.label;
235 menu_item = gtk_check_menu_item_new_with_mnemonic (label);
237 menu_item = gtk_check_menu_item_new_with_label (label);
239 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
240 toggle_tool_button->priv->active);
242 if (GTK_IS_RADIO_TOOL_BUTTON (toggle_tool_button))
244 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (menu_item),
248 g_signal_connect_closure_by_id (menu_item,
249 g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
250 g_cclosure_new_object (G_CALLBACK (menu_item_activated),
251 G_OBJECT (toggle_tool_button)),
254 gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
259 /* There are two activatable widgets, a toggle button and a menu item.
261 * If a widget is activated and the state of the tool button is the same as
262 * the new state of the activated widget, then the other widget was the one
263 * that was activated by the user and updated the tool button's state.
265 * If the state of the tool button is not the same as the new state of the
266 * activated widget, then the activation was activated by the user, and the
267 * widget needs to make sure the tool button is updated before the other
268 * widget is activated. This will make sure the other widget a tool button
269 * in a state that matches its own new state.
272 menu_item_activated (GtkWidget *menu_item,
273 GtkToggleToolButton *toggle_tool_button)
275 GtkToolButton *tool_button = GTK_TOOL_BUTTON (toggle_tool_button);
276 gboolean menu_active = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item));
278 if (toggle_tool_button->priv->active != menu_active)
280 toggle_tool_button->priv->active = menu_active;
282 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)),
283 toggle_tool_button->priv->active);
285 g_object_notify (G_OBJECT (toggle_tool_button), "active");
286 g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
291 button_toggled (GtkWidget *widget,
292 GtkToggleToolButton *toggle_tool_button)
294 gboolean toggle_active = GTK_TOGGLE_BUTTON (widget)->active;
296 if (toggle_tool_button->priv->active != toggle_active)
298 GtkWidget *menu_item;
300 toggle_tool_button->priv->active = toggle_active;
303 gtk_tool_item_get_proxy_menu_item (GTK_TOOL_ITEM (toggle_tool_button), MENU_ID)))
305 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
306 toggle_tool_button->priv->active);
309 g_object_notify (G_OBJECT (toggle_tool_button), "active");
310 g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
315 gtk_toggle_tool_button_activatable_interface_init (GtkActivatableIface *iface)
317 parent_activatable_iface = g_type_interface_peek_parent (iface);
318 iface->update = gtk_toggle_tool_button_update;
319 iface->sync_action_properties = gtk_toggle_tool_button_sync_action_properties;
323 gtk_toggle_tool_button_update (GtkActivatable *activatable,
325 const gchar *property_name)
327 GtkToggleToolButton *button;
329 parent_activatable_iface->update (activatable, action, property_name);
331 button = GTK_TOGGLE_TOOL_BUTTON (activatable);
333 if (strcmp (property_name, "active") == 0)
335 gtk_action_block_activate (action);
336 gtk_toggle_tool_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
337 gtk_action_unblock_activate (action);
342 gtk_toggle_tool_button_sync_action_properties (GtkActivatable *activatable,
345 GtkToggleToolButton *button;
347 parent_activatable_iface->sync_action_properties (activatable, action);
349 if (!GTK_IS_TOGGLE_ACTION (action))
352 button = GTK_TOGGLE_TOOL_BUTTON (activatable);
354 gtk_action_block_activate (action);
355 gtk_toggle_tool_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
356 gtk_action_unblock_activate (action);
361 * gtk_toggle_tool_button_new:
363 * Returns a new #GtkToggleToolButton
365 * Return value: a newly created #GtkToggleToolButton
370 gtk_toggle_tool_button_new (void)
372 GtkToolButton *button;
374 button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
377 return GTK_TOOL_ITEM (button);
381 * gtk_toggle_tool_button_new_from_stock:
382 * @stock_id: the name of the stock item
384 * Creates a new #GtkToggleToolButton containing the image and text from a
385 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
386 * and #GTK_STOCK_APPLY.
388 * It is an error if @stock_id is not a name of a stock item.
390 * Return value: A new #GtkToggleToolButton
395 gtk_toggle_tool_button_new_from_stock (const gchar *stock_id)
397 GtkToolButton *button;
399 g_return_val_if_fail (stock_id != NULL, NULL);
401 button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
402 "stock-id", stock_id,
405 return GTK_TOOL_ITEM (button);
409 * gtk_toggle_tool_button_set_active:
410 * @button: a #GtkToggleToolButton
411 * @is_active: whether @button should be active
413 * Sets the status of the toggle tool button. Set to %TRUE if you
414 * want the GtkToggleButton to be 'pressed in', and %FALSE to raise it.
415 * This action causes the toggled signal to be emitted.
420 gtk_toggle_tool_button_set_active (GtkToggleToolButton *button,
423 g_return_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button));
425 is_active = is_active != FALSE;
427 if (button->priv->active != is_active)
428 gtk_button_clicked (GTK_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button))));
432 * gtk_toggle_tool_button_get_active:
433 * @button: a #GtkToggleToolButton
435 * Queries a #GtkToggleToolButton and returns its current state.
436 * Returns %TRUE if the toggle button is pressed in and %FALSE if it is raised.
438 * Return value: %TRUE if the toggle tool button is pressed in, %FALSE if not
443 gtk_toggle_tool_button_get_active (GtkToggleToolButton *button)
445 g_return_val_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button), FALSE);
447 return button->priv->active;
450 #define __GTK_TOGGLE_TOOL_BUTTON_C__
451 #include "gtkaliasdef.c"