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 "gtkprivate.h"
33 #define MENU_ID "gtk-toggle-tool-button-menu-id"
46 #define GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TOGGLE_TOOL_BUTTON, GtkToggleToolButtonPrivate))
48 struct _GtkToggleToolButtonPrivate
54 static void gtk_toggle_tool_button_set_property (GObject *object,
58 static void gtk_toggle_tool_button_get_property (GObject *object,
63 static gboolean gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *button);
65 static void button_toggled (GtkWidget *widget,
66 GtkToggleToolButton *button);
67 static void menu_item_activated (GtkWidget *widget,
68 GtkToggleToolButton *button);
70 static guint toggle_signals[LAST_SIGNAL] = { 0 };
72 G_DEFINE_TYPE (GtkToggleToolButton, gtk_toggle_tool_button, GTK_TYPE_TOOL_BUTTON)
75 gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass)
77 GObjectClass *object_class;
78 GtkToolItemClass *toolitem_class;
79 GtkToolButtonClass *toolbutton_class;
81 object_class = (GObjectClass *)klass;
82 toolitem_class = (GtkToolItemClass *)klass;
83 toolbutton_class = (GtkToolButtonClass *)klass;
85 object_class->set_property = gtk_toggle_tool_button_set_property;
86 object_class->get_property = gtk_toggle_tool_button_get_property;
88 toolitem_class->create_menu_proxy = gtk_toggle_tool_button_create_menu_proxy;
89 toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON;
92 * GtkToggleToolButton:active:
94 * If the toggle tool button should be pressed in or not.
98 g_object_class_install_property (object_class,
100 g_param_spec_boolean ("active",
102 P_("If the toggle button should be pressed in or not"),
104 GTK_PARAM_READWRITE));
107 * GtkToggleToolButton::toggled:
108 * @toggle_tool_button: the object that emitted the signal
110 * Emitted whenever the toggle tool button changes state.
112 toggle_signals[TOGGLED] =
113 g_signal_new (I_("toggled"),
114 G_OBJECT_CLASS_TYPE (klass),
116 G_STRUCT_OFFSET (GtkToggleToolButtonClass, toggled),
118 g_cclosure_marshal_VOID__VOID,
121 g_type_class_add_private (object_class, sizeof (GtkToggleToolButtonPrivate));
125 gtk_toggle_tool_button_init (GtkToggleToolButton *button)
127 GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
128 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button));
130 button->priv = GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE (button);
132 /* If the real button is a radio button, it may have been
133 * active at the time it was created.
135 button->priv->active = gtk_toggle_button_get_active (toggle_button);
137 g_signal_connect_object (toggle_button,
138 "toggled", G_CALLBACK (button_toggled), button, 0);
142 gtk_toggle_tool_button_set_property (GObject *object,
147 GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
152 gtk_toggle_tool_button_set_active (button,
153 g_value_get_boolean (value));
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 gtk_toggle_tool_button_get_property (GObject *object,
168 GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
173 g_value_set_boolean (value, gtk_toggle_tool_button_get_active (button));
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *item)
185 GtkToolButton *tool_button = GTK_TOOL_BUTTON (item);
186 GtkToggleToolButton *toggle_tool_button = GTK_TOGGLE_TOOL_BUTTON (item);
187 GtkWidget *menu_item = NULL;
188 GtkStockItem stock_item;
189 gboolean use_mnemonic = TRUE;
192 GtkWidget *label_widget = gtk_tool_button_get_label_widget (tool_button);
193 const gchar *label_text = gtk_tool_button_get_label (tool_button);
194 const gchar *stock_id = gtk_tool_button_get_stock_id (tool_button);
196 if (GTK_IS_LABEL (label_widget))
198 label = gtk_label_get_label (GTK_LABEL (label_widget));
199 use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (label_widget));
204 use_mnemonic = gtk_tool_button_get_use_underline (tool_button);
206 else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
208 label = stock_item.label;
216 menu_item = gtk_check_menu_item_new_with_mnemonic (label);
218 menu_item = gtk_check_menu_item_new_with_label (label);
220 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
221 toggle_tool_button->priv->active);
223 if (GTK_IS_RADIO_TOOL_BUTTON (toggle_tool_button))
225 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (menu_item),
229 g_signal_connect_closure_by_id (menu_item,
230 g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
231 g_cclosure_new_object (G_CALLBACK (menu_item_activated),
232 G_OBJECT (toggle_tool_button)),
235 gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
240 /* There are two activatable widgets, a toggle button and a menu item.
242 * If a widget is activated and the state of the tool button is the same as
243 * the new state of the activated widget, then the other widget was the one
244 * that was activated by the user and updated the tool button's state.
246 * If the state of the tool button is not the same as the new state of the
247 * activated widget, then the activation was activated by the user, and the
248 * widget needs to make sure the tool button is updated before the other
249 * widget is activated. This will make sure the other widget a tool button
250 * in a state that matches its own new state.
253 menu_item_activated (GtkWidget *menu_item,
254 GtkToggleToolButton *toggle_tool_button)
256 GtkToolButton *tool_button = GTK_TOOL_BUTTON (toggle_tool_button);
257 gboolean menu_active = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item));
259 if (toggle_tool_button->priv->active != menu_active)
261 toggle_tool_button->priv->active = menu_active;
263 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)),
264 toggle_tool_button->priv->active);
266 g_object_notify (G_OBJECT (toggle_tool_button), "active");
267 g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
272 button_toggled (GtkWidget *widget,
273 GtkToggleToolButton *toggle_tool_button)
275 gboolean toggle_active = GTK_TOGGLE_BUTTON (widget)->active;
277 if (toggle_tool_button->priv->active != toggle_active)
279 GtkWidget *menu_item;
281 toggle_tool_button->priv->active = toggle_active;
284 gtk_tool_item_get_proxy_menu_item (GTK_TOOL_ITEM (toggle_tool_button), MENU_ID)))
286 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
287 toggle_tool_button->priv->active);
290 g_object_notify (G_OBJECT (toggle_tool_button), "active");
291 g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
296 * gtk_toggle_tool_button_new:
298 * Returns a new #GtkToggleToolButton
300 * Return value: a newly created #GtkToggleToolButton
305 gtk_toggle_tool_button_new (void)
307 GtkToolButton *button;
309 button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
312 return GTK_TOOL_ITEM (button);
316 * gtk_toggle_tool_button_new_from_stock:
317 * @stock_id: the name of the stock item
319 * Creates a new #GtkToggleToolButton containing the image and text from a
320 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
321 * and #GTK_STOCK_APPLY.
323 * It is an error if @stock_id is not a name of a stock item.
325 * Return value: A new #GtkToggleToolButton
330 gtk_toggle_tool_button_new_from_stock (const gchar *stock_id)
332 GtkToolButton *button;
334 g_return_val_if_fail (stock_id != NULL, NULL);
336 button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
337 "stock-id", stock_id,
340 return GTK_TOOL_ITEM (button);
344 * gtk_toggle_tool_button_set_active:
345 * @button: a #GtkToggleToolButton
346 * @is_active: whether @button should be active
348 * Sets the status of the toggle tool button. Set to %TRUE if you
349 * want the GtkToggleButton to be 'pressed in', and %FALSE to raise it.
350 * This action causes the toggled signal to be emitted.
355 gtk_toggle_tool_button_set_active (GtkToggleToolButton *button,
358 g_return_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button));
360 is_active = is_active != FALSE;
362 if (button->priv->active != is_active)
363 gtk_button_clicked (GTK_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button))));
367 * gtk_toggle_tool_button_get_active:
368 * @button: a #GtkToggleToolButton
370 * Queries a #GtkToggleToolButton and returns its current state.
371 * Returns %TRUE if the toggle button is pressed in and %FALSE if it is raised.
373 * Return value: %TRUE if the toggle tool button is pressed in, %FALSE if not
378 gtk_toggle_tool_button_get_active (GtkToggleToolButton *button)
380 g_return_val_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button), FALSE);
382 return button->priv->active;
385 #define __GTK_TOGGLE_TOOL_BUTTON_C__
386 #include "gtkaliasdef.c"