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
53 static void gtk_toggle_tool_button_init (GtkToggleToolButton *button);
54 static void gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass);
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);
72 static GObjectClass *parent_class = NULL;
73 static guint toggle_signals[LAST_SIGNAL] = { 0 };
76 gtk_toggle_tool_button_get_type (void)
78 static GType type = 0;
82 static const GTypeInfo type_info =
84 sizeof (GtkToggleToolButtonClass),
86 (GBaseFinalizeFunc) 0,
87 (GClassInitFunc) gtk_toggle_tool_button_class_init,
88 (GClassFinalizeFunc) 0,
90 sizeof (GtkToggleToolButton),
92 (GInstanceInitFunc) gtk_toggle_tool_button_init
95 type = g_type_register_static (GTK_TYPE_TOOL_BUTTON,
96 I_("GtkToggleToolButton"), &type_info, 0);
103 gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass)
105 GObjectClass *object_class;
106 GtkToolItemClass *toolitem_class;
107 GtkToolButtonClass *toolbutton_class;
109 parent_class = g_type_class_peek_parent (klass);
111 object_class = (GObjectClass *)klass;
112 toolitem_class = (GtkToolItemClass *)klass;
113 toolbutton_class = (GtkToolButtonClass *)klass;
115 object_class->set_property = gtk_toggle_tool_button_set_property;
116 object_class->get_property = gtk_toggle_tool_button_get_property;
118 toolitem_class->create_menu_proxy = gtk_toggle_tool_button_create_menu_proxy;
119 toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON;
122 * GtkToggleToolButton:active:
124 * If the toggle tool button should be pressed in or not.
128 g_object_class_install_property (object_class,
130 g_param_spec_boolean ("active",
132 P_("If the toggle button should be pressed in or not"),
134 GTK_PARAM_READWRITE));
137 * GtkToggleToolButton::toggled:
138 * @toggle_tool_button: the object that emitted the signal
140 * Emitted whenever the toggle tool button changes state.
142 toggle_signals[TOGGLED] =
143 g_signal_new (I_("toggled"),
144 G_OBJECT_CLASS_TYPE (klass),
146 G_STRUCT_OFFSET (GtkToggleToolButtonClass, toggled),
148 g_cclosure_marshal_VOID__VOID,
151 g_type_class_add_private (object_class, sizeof (GtkToggleToolButtonPrivate));
155 gtk_toggle_tool_button_init (GtkToggleToolButton *button)
157 GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
158 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button));
160 button->priv = GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE (button);
162 /* If the real button is a radio button, it may have been
163 * active at the time it was created.
165 button->priv->active = gtk_toggle_button_get_active (toggle_button);
167 g_signal_connect_object (toggle_button,
168 "toggled", G_CALLBACK (button_toggled), button, 0);
172 gtk_toggle_tool_button_set_property (GObject *object,
177 GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
182 button->priv->active = g_value_get_boolean (value);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 gtk_toggle_tool_button_get_property (GObject *object,
197 GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
202 g_value_set_boolean (value, gtk_toggle_tool_button_get_active (button));
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *item)
214 GtkToolButton *tool_button = GTK_TOOL_BUTTON (item);
215 GtkToggleToolButton *toggle_tool_button = GTK_TOGGLE_TOOL_BUTTON (item);
216 GtkWidget *menu_item = NULL;
217 GtkStockItem stock_item;
218 gboolean use_mnemonic = TRUE;
221 GtkWidget *label_widget = gtk_tool_button_get_label_widget (tool_button);
222 const gchar *label_text = gtk_tool_button_get_label (tool_button);
223 const gchar *stock_id = gtk_tool_button_get_stock_id (tool_button);
225 if (label_widget && GTK_IS_LABEL (label_widget))
227 label = gtk_label_get_label (GTK_LABEL (label_widget));
228 use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (label_widget));
233 use_mnemonic = gtk_tool_button_get_use_underline (tool_button);
235 else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
237 label = stock_item.label;
245 menu_item = gtk_check_menu_item_new_with_mnemonic (label);
247 menu_item = gtk_check_menu_item_new_with_label (label);
249 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
250 toggle_tool_button->priv->active);
252 if (GTK_IS_RADIO_TOOL_BUTTON (toggle_tool_button))
254 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (menu_item),
258 g_signal_connect_closure_by_id (menu_item,
259 g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
260 g_cclosure_new_object (G_CALLBACK (menu_item_activated),
261 G_OBJECT (toggle_tool_button)),
264 gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
269 /* There are two activatable widgets, a toggle button and a menu item.
271 * If a widget is activated and the state of the tool button is the same as
272 * the new state of the activated widget, then the other widget was the one
273 * that was activated by the user and updated the tool button's state.
275 * If the state of the tool button is not the same as the new state of the
276 * activated widget, then the activation was activated by the user, and the
277 * widget needs to make sure the tool button is updated before the other
278 * widget is activated. This will make sure the other widget a tool button
279 * in a state that matches its own new state.
282 menu_item_activated (GtkWidget *menu_item,
283 GtkToggleToolButton *toggle_tool_button)
285 GtkToolButton *tool_button = GTK_TOOL_BUTTON (toggle_tool_button);
286 gboolean menu_active = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item));
288 if (toggle_tool_button->priv->active != menu_active)
290 toggle_tool_button->priv->active = menu_active;
292 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)),
293 toggle_tool_button->priv->active);
295 g_object_notify (G_OBJECT (toggle_tool_button), "active");
296 g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
301 button_toggled (GtkWidget *widget,
302 GtkToggleToolButton *toggle_tool_button)
304 gboolean toggle_active = GTK_TOGGLE_BUTTON (widget)->active;
306 if (toggle_tool_button->priv->active != toggle_active)
308 GtkWidget *menu_item;
310 toggle_tool_button->priv->active = toggle_active;
313 gtk_tool_item_get_proxy_menu_item (GTK_TOOL_ITEM (toggle_tool_button), MENU_ID)))
315 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
316 toggle_tool_button->priv->active);
319 g_object_notify (G_OBJECT (toggle_tool_button), "active");
320 g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
325 * gtk_toggle_tool_button_new:
327 * Returns a new #GtkToggleToolButton
329 * Return value: a newly created #GtkToggleToolButton
334 gtk_toggle_tool_button_new (void)
336 GtkToolButton *button;
338 button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
341 return GTK_TOOL_ITEM (button);
345 * gtk_toggle_tool_button_new_from_stock:
346 * @stock_id: the name of the stock item
348 * Creates a new #GtkToggleToolButton containing the image and text from a
349 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
350 * and #GTK_STOCK_APPLY.
352 * It is an error if @stock_id is not a name of a stock item.
354 * Return value: A new #GtkToggleToolButton
359 gtk_toggle_tool_button_new_from_stock (const gchar *stock_id)
361 GtkToolButton *button;
363 g_return_val_if_fail (stock_id != NULL, NULL);
365 button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
366 "stock-id", stock_id,
369 return GTK_TOOL_ITEM (button);
373 * gtk_toggle_tool_button_set_active:
374 * @button: a #GtkToggleToolButton
375 * @is_active: whether @button should be active
377 * Sets the status of the toggle tool button. Set to %TRUE if you
378 * want the GtkToggleButton to be 'pressed in', and %FALSE to raise it.
379 * This action causes the toggled signal to be emitted.
384 gtk_toggle_tool_button_set_active (GtkToggleToolButton *button,
387 g_return_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button));
389 is_active = is_active != FALSE;
391 if (button->priv->active != is_active)
392 gtk_button_clicked (GTK_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button))));
396 * gtk_toggle_tool_button_get_active:
397 * @button: a #GtkToggleToolButton
399 * Queries a #GtkToggleToolButton and returns its current state.
400 * Returns %TRUE if the toggle button is pressed in and %FALSE if it is raised.
402 * Return value: %TRUE if the toggle tool button is pressed in, %FALSE if not
407 gtk_toggle_tool_button_get_active (GtkToggleToolButton *button)
409 g_return_val_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button), FALSE);
411 return button->priv->active;
414 #define __GTK_TOGGLE_TOOL_BUTTON_C__
415 #include "gtkaliasdef.c"