]> Pileus Git - ~andy/gtk/blob - gtk/gtktoggletoolbutton.c
88266463ac98d5b46372a6560a89505d6b451d7c
[~andy/gtk] / gtk / gtktoggletoolbutton.c
1 /* gtktoggletoolbutton.c
2  *
3  * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
4  * Copyright (C) 2002 James Henstridge <james@daa.com.au>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <config.h>
23 #include "gtktoggletoolbutton.h"
24 #include "gtkcheckmenuitem.h"
25 #include "gtklabel.h"
26 #include "gtktogglebutton.h"
27 #include "gtkstock.h"
28 #include "gtkintl.h"
29 #include "gtkradiotoolbutton.h"
30 #include "gtkprivate.h"
31 #include "gtkalias.h"
32
33 #define MENU_ID "gtk-toggle-tool-button-menu-id"
34
35 enum {
36   TOGGLED,
37   LAST_SIGNAL
38 };
39
40 enum {
41   PROP_0,
42   PROP_ACTIVE
43 };
44
45
46 #define GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TOGGLE_TOOL_BUTTON, GtkToggleToolButtonPrivate))
47
48 struct _GtkToggleToolButtonPrivate
49 {
50   guint active : 1;
51 };
52   
53
54 static void     gtk_toggle_tool_button_set_property        (GObject      *object,
55                                                             guint         prop_id,
56                                                             const GValue *value,
57                                                             GParamSpec   *pspec);
58 static void     gtk_toggle_tool_button_get_property        (GObject      *object,
59                                                             guint         prop_id,
60                                                             GValue       *value,
61                                                             GParamSpec   *pspec);
62
63 static gboolean gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *button);
64
65 static void button_toggled      (GtkWidget           *widget,
66                                  GtkToggleToolButton *button);
67 static void menu_item_activated (GtkWidget           *widget,
68                                  GtkToggleToolButton *button);
69
70 static guint         toggle_signals[LAST_SIGNAL] = { 0 };
71
72 G_DEFINE_TYPE (GtkToggleToolButton, gtk_toggle_tool_button, GTK_TYPE_TOOL_BUTTON)
73
74 static void
75 gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass)
76 {
77   GObjectClass *object_class;
78   GtkToolItemClass *toolitem_class;
79   GtkToolButtonClass *toolbutton_class;
80
81   object_class = (GObjectClass *)klass;
82   toolitem_class = (GtkToolItemClass *)klass;
83   toolbutton_class = (GtkToolButtonClass *)klass;
84
85   object_class->set_property = gtk_toggle_tool_button_set_property;
86   object_class->get_property = gtk_toggle_tool_button_get_property;
87
88   toolitem_class->create_menu_proxy = gtk_toggle_tool_button_create_menu_proxy;
89   toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON;
90
91   /**
92    * GtkToggleToolButton:active:
93    *
94    * If the toggle tool button should be pressed in or not.
95    *
96    * Since: 2.8
97    */
98   g_object_class_install_property (object_class,
99                                    PROP_ACTIVE,
100                                    g_param_spec_boolean ("active",
101                                                          P_("Active"),
102                                                          P_("If the toggle button should be pressed in or not"),
103                                                          FALSE,
104                                                          GTK_PARAM_READWRITE));
105
106 /**
107  * GtkToggleToolButton::toggled:
108  * @toggle_tool_button: the object that emitted the signal
109  *
110  * Emitted whenever the toggle tool button changes state.
111  **/
112   toggle_signals[TOGGLED] =
113     g_signal_new (I_("toggled"),
114                   G_OBJECT_CLASS_TYPE (klass),
115                   G_SIGNAL_RUN_FIRST,
116                   G_STRUCT_OFFSET (GtkToggleToolButtonClass, toggled),
117                   NULL, NULL,
118                   g_cclosure_marshal_VOID__VOID,
119                   G_TYPE_NONE, 0);
120
121   g_type_class_add_private (object_class, sizeof (GtkToggleToolButtonPrivate));
122 }
123
124 static void
125 gtk_toggle_tool_button_init (GtkToggleToolButton *button)
126 {
127   GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
128   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button));
129
130   button->priv = GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE (button);
131
132   /* If the real button is a radio button, it may have been
133    * active at the time it was created.
134    */
135   button->priv->active = gtk_toggle_button_get_active (toggle_button);
136     
137   g_signal_connect_object (toggle_button,
138                            "toggled", G_CALLBACK (button_toggled), button, 0);
139 }
140
141 static void
142 gtk_toggle_tool_button_set_property (GObject      *object,
143                                      guint         prop_id,
144                                      const GValue *value,
145                                      GParamSpec   *pspec)
146 {
147   GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
148
149   switch (prop_id)
150     {
151       case PROP_ACTIVE:
152         gtk_toggle_tool_button_set_active (button, 
153                                            g_value_get_boolean (value));
154         break;
155
156       default:
157         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158         break;
159     }
160 }
161
162 static void
163 gtk_toggle_tool_button_get_property (GObject    *object,
164                                      guint       prop_id,
165                                      GValue     *value,
166                                      GParamSpec *pspec)
167 {
168   GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
169
170   switch (prop_id)
171     {
172       case PROP_ACTIVE:
173         g_value_set_boolean (value, gtk_toggle_tool_button_get_active (button));
174         break;
175
176       default:
177         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178         break;
179     }
180 }
181
182 static gboolean
183 gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *item)
184 {
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;
190   const char *label;
191
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);
195
196   if (GTK_IS_LABEL (label_widget))
197     {
198       label = gtk_label_get_label (GTK_LABEL (label_widget));
199       use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (label_widget));
200     }
201   else if (label_text)
202     {
203       label = label_text;
204       use_mnemonic = gtk_tool_button_get_use_underline (tool_button);
205     }
206   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
207     {
208       label = stock_item.label;
209     }
210   else
211     {
212       label = "";
213     }
214   
215   if (use_mnemonic)
216     menu_item = gtk_check_menu_item_new_with_mnemonic (label);
217   else
218     menu_item = gtk_check_menu_item_new_with_label (label);
219
220   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
221                                   toggle_tool_button->priv->active);
222
223   if (GTK_IS_RADIO_TOOL_BUTTON (toggle_tool_button))
224     {
225       gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (menu_item),
226                                              TRUE);
227     }
228
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)),
233                                   FALSE);
234
235   gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
236   
237   return TRUE;
238 }
239
240 /* There are two activatable widgets, a toggle button and a menu item.
241  *
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.
245  *
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.
251  */
252 static void
253 menu_item_activated (GtkWidget           *menu_item,
254                      GtkToggleToolButton *toggle_tool_button)
255 {
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));
258
259   if (toggle_tool_button->priv->active != menu_active)
260     {
261       toggle_tool_button->priv->active = menu_active;
262
263       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)),
264                                     toggle_tool_button->priv->active);
265
266       g_object_notify (G_OBJECT (toggle_tool_button), "active");
267       g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
268     }
269 }
270
271 static void
272 button_toggled (GtkWidget           *widget,
273                 GtkToggleToolButton *toggle_tool_button)
274 {
275   gboolean toggle_active = GTK_TOGGLE_BUTTON (widget)->active;
276
277   if (toggle_tool_button->priv->active != toggle_active)
278     {
279       GtkWidget *menu_item;
280       
281       toggle_tool_button->priv->active = toggle_active;
282        
283       if ((menu_item =
284            gtk_tool_item_get_proxy_menu_item (GTK_TOOL_ITEM (toggle_tool_button), MENU_ID)))
285         {
286           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
287                                           toggle_tool_button->priv->active);
288         }
289
290       g_object_notify (G_OBJECT (toggle_tool_button), "active");
291       g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
292     }
293 }
294
295 /**
296  * gtk_toggle_tool_button_new:
297  * 
298  * Returns a new #GtkToggleToolButton
299  * 
300  * Return value: a newly created #GtkToggleToolButton
301  * 
302  * Since: 2.4
303  **/
304 GtkToolItem *
305 gtk_toggle_tool_button_new (void)
306 {
307   GtkToolButton *button;
308
309   button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
310                          NULL);
311   
312   return GTK_TOOL_ITEM (button);
313 }
314
315 /**
316  * gtk_toggle_tool_button_new_from_stock:
317  * @stock_id: the name of the stock item 
318  *
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.
322  *
323  * It is an error if @stock_id is not a name of a stock item.
324  * 
325  * Return value: A new #GtkToggleToolButton
326  * 
327  * Since: 2.4
328  **/
329 GtkToolItem *
330 gtk_toggle_tool_button_new_from_stock (const gchar *stock_id)
331 {
332   GtkToolButton *button;
333
334   g_return_val_if_fail (stock_id != NULL, NULL);
335   
336   button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
337                          "stock-id", stock_id,
338                          NULL);
339   
340   return GTK_TOOL_ITEM (button);
341 }
342
343 /**
344  * gtk_toggle_tool_button_set_active:
345  * @button: a #GtkToggleToolButton
346  * @is_active: whether @button should be active
347  * 
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.
351  * 
352  * Since: 2.4
353  **/
354 void
355 gtk_toggle_tool_button_set_active (GtkToggleToolButton *button,
356                                    gboolean is_active)
357 {
358   g_return_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button));
359
360   is_active = is_active != FALSE;
361
362   if (button->priv->active != is_active)
363     gtk_button_clicked (GTK_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button))));
364 }
365
366 /**
367  * gtk_toggle_tool_button_get_active:
368  * @button: a #GtkToggleToolButton
369  * 
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.
372  * 
373  * Return value: %TRUE if the toggle tool button is pressed in, %FALSE if not
374  * 
375  * Since: 2.4
376  **/
377 gboolean
378 gtk_toggle_tool_button_get_active (GtkToggleToolButton *button)
379 {
380   g_return_val_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button), FALSE);
381
382   return button->priv->active;
383 }
384
385 #define __GTK_TOGGLE_TOOL_BUTTON_C__
386 #include "gtkaliasdef.c"