]> Pileus Git - ~andy/gtk/blob - gtk/gtktoggletoolbutton.c
Move documentation to inline comments: GtkToggleToolButton
[~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 "gtktoggleaction.h"
31 #include "gtkactivatable.h"
32 #include "gtkprivate.h"
33
34
35 /**
36  * SECTION:gtktoggletoolbutton
37  * @Short_description: A GtkToolItem containing a toggle button
38  * @Title: GtkToggleToolButton
39  * @See_also: #GtkToolbar, #GtkToolButton, #GtkSeparatorToolItem
40  *
41  * A #GtkToggleToolButton is a #GtkToolItem that contains a toggle
42  * button.
43  *
44  * Use gtk_toggle_tool_button_new() to create a new
45  * #GtkToggleToolButton. Use gtk_toggle_tool_button_new_from_stock() to
46  * create a new #GtkToggleToolButton containing a stock item.
47  */
48
49
50 #define MENU_ID "gtk-toggle-tool-button-menu-id"
51
52 enum {
53   TOGGLED,
54   LAST_SIGNAL
55 };
56
57 enum {
58   PROP_0,
59   PROP_ACTIVE
60 };
61
62
63 struct _GtkToggleToolButtonPrivate
64 {
65   guint active : 1;
66 };
67   
68
69 static void     gtk_toggle_tool_button_set_property        (GObject      *object,
70                                                             guint         prop_id,
71                                                             const GValue *value,
72                                                             GParamSpec   *pspec);
73 static void     gtk_toggle_tool_button_get_property        (GObject      *object,
74                                                             guint         prop_id,
75                                                             GValue       *value,
76                                                             GParamSpec   *pspec);
77
78 static gboolean gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *button);
79
80 static void button_toggled      (GtkWidget           *widget,
81                                  GtkToggleToolButton *button);
82 static void menu_item_activated (GtkWidget           *widget,
83                                  GtkToggleToolButton *button);
84
85
86 static void gtk_toggle_tool_button_activatable_interface_init (GtkActivatableIface  *iface);
87 static void gtk_toggle_tool_button_update                     (GtkActivatable       *activatable,
88                                                                GtkAction            *action,
89                                                                const gchar          *property_name);
90 static void gtk_toggle_tool_button_sync_action_properties     (GtkActivatable       *activatable,
91                                                                GtkAction            *action);
92
93 static GtkActivatableIface *parent_activatable_iface;
94 static guint                toggle_signals[LAST_SIGNAL] = { 0 };
95
96 G_DEFINE_TYPE_WITH_CODE (GtkToggleToolButton, gtk_toggle_tool_button, GTK_TYPE_TOOL_BUTTON,
97                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
98                                                 gtk_toggle_tool_button_activatable_interface_init))
99
100 static void
101 gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass)
102 {
103   GObjectClass *object_class;
104   GtkToolItemClass *toolitem_class;
105   GtkToolButtonClass *toolbutton_class;
106
107   object_class = (GObjectClass *)klass;
108   toolitem_class = (GtkToolItemClass *)klass;
109   toolbutton_class = (GtkToolButtonClass *)klass;
110
111   object_class->set_property = gtk_toggle_tool_button_set_property;
112   object_class->get_property = gtk_toggle_tool_button_get_property;
113
114   toolitem_class->create_menu_proxy = gtk_toggle_tool_button_create_menu_proxy;
115   toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON;
116
117   /**
118    * GtkToggleToolButton:active:
119    *
120    * If the toggle tool button should be pressed in.
121    *
122    * Since: 2.8
123    */
124   g_object_class_install_property (object_class,
125                                    PROP_ACTIVE,
126                                    g_param_spec_boolean ("active",
127                                                          P_("Active"),
128                                                          P_("If the toggle button should be pressed in"),
129                                                          FALSE,
130                                                          GTK_PARAM_READWRITE));
131
132 /**
133  * GtkToggleToolButton::toggled:
134  * @toggle_tool_button: the object that emitted the signal
135  *
136  * Emitted whenever the toggle tool button changes state.
137  **/
138   toggle_signals[TOGGLED] =
139     g_signal_new (I_("toggled"),
140                   G_OBJECT_CLASS_TYPE (klass),
141                   G_SIGNAL_RUN_FIRST,
142                   G_STRUCT_OFFSET (GtkToggleToolButtonClass, toggled),
143                   NULL, NULL,
144                   g_cclosure_marshal_VOID__VOID,
145                   G_TYPE_NONE, 0);
146
147   g_type_class_add_private (object_class, sizeof (GtkToggleToolButtonPrivate));
148 }
149
150 static void
151 gtk_toggle_tool_button_init (GtkToggleToolButton *button)
152 {
153   GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
154   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button));
155
156   button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
157                                               GTK_TYPE_TOGGLE_TOOL_BUTTON,
158                                               GtkToggleToolButtonPrivate);
159
160   /* If the real button is a radio button, it may have been
161    * active at the time it was created.
162    */
163   button->priv->active = gtk_toggle_button_get_active (toggle_button);
164     
165   g_signal_connect_object (toggle_button,
166                            "toggled", G_CALLBACK (button_toggled), button, 0);
167 }
168
169 static void
170 gtk_toggle_tool_button_set_property (GObject      *object,
171                                      guint         prop_id,
172                                      const GValue *value,
173                                      GParamSpec   *pspec)
174 {
175   GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
176
177   switch (prop_id)
178     {
179       case PROP_ACTIVE:
180         gtk_toggle_tool_button_set_active (button, 
181                                            g_value_get_boolean (value));
182         break;
183
184       default:
185         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186         break;
187     }
188 }
189
190 static void
191 gtk_toggle_tool_button_get_property (GObject    *object,
192                                      guint       prop_id,
193                                      GValue     *value,
194                                      GParamSpec *pspec)
195 {
196   GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
197
198   switch (prop_id)
199     {
200       case PROP_ACTIVE:
201         g_value_set_boolean (value, gtk_toggle_tool_button_get_active (button));
202         break;
203
204       default:
205         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206         break;
207     }
208 }
209
210 static gboolean
211 gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *item)
212 {
213   GtkToolButton *tool_button = GTK_TOOL_BUTTON (item);
214   GtkToggleToolButton *toggle_tool_button = GTK_TOGGLE_TOOL_BUTTON (item);
215   GtkWidget *menu_item = NULL;
216   GtkStockItem stock_item;
217   gboolean use_mnemonic = TRUE;
218   const char *label;
219   GtkWidget *label_widget;
220   const gchar *label_text;
221   const gchar *stock_id;
222
223   if (_gtk_tool_item_create_menu_proxy (item))
224     return TRUE;
225
226   label_widget = gtk_tool_button_get_label_widget (tool_button);
227   label_text = gtk_tool_button_get_label (tool_button);
228   stock_id = gtk_tool_button_get_stock_id (tool_button);
229
230   if (GTK_IS_LABEL (label_widget))
231     {
232       label = gtk_label_get_label (GTK_LABEL (label_widget));
233       use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (label_widget));
234     }
235   else if (label_text)
236     {
237       label = label_text;
238       use_mnemonic = gtk_tool_button_get_use_underline (tool_button);
239     }
240   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
241     {
242       label = stock_item.label;
243     }
244   else
245     {
246       label = "";
247     }
248   
249   if (use_mnemonic)
250     menu_item = gtk_check_menu_item_new_with_mnemonic (label);
251   else
252     menu_item = gtk_check_menu_item_new_with_label (label);
253
254   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
255                                   toggle_tool_button->priv->active);
256
257   if (GTK_IS_RADIO_TOOL_BUTTON (toggle_tool_button))
258     {
259       gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (menu_item),
260                                              TRUE);
261     }
262
263   g_signal_connect_closure_by_id (menu_item,
264                                   g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
265                                   g_cclosure_new_object (G_CALLBACK (menu_item_activated),
266                                                          G_OBJECT (toggle_tool_button)),
267                                   FALSE);
268
269   gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
270   
271   return TRUE;
272 }
273
274 /* There are two activatable widgets, a toggle button and a menu item.
275  *
276  * If a widget is activated and the state of the tool button is the same as
277  * the new state of the activated widget, then the other widget was the one
278  * that was activated by the user and updated the tool button's state.
279  *
280  * If the state of the tool button is not the same as the new state of the
281  * activated widget, then the activation was activated by the user, and the
282  * widget needs to make sure the tool button is updated before the other
283  * widget is activated. This will make sure the other widget a tool button
284  * in a state that matches its own new state.
285  */
286 static void
287 menu_item_activated (GtkWidget           *menu_item,
288                      GtkToggleToolButton *toggle_tool_button)
289 {
290   GtkToolButton *tool_button = GTK_TOOL_BUTTON (toggle_tool_button);
291   gboolean menu_active = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item));
292
293   if (toggle_tool_button->priv->active != menu_active)
294     {
295       toggle_tool_button->priv->active = menu_active;
296
297       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)),
298                                     toggle_tool_button->priv->active);
299
300       g_object_notify (G_OBJECT (toggle_tool_button), "active");
301       g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
302     }
303 }
304
305 static void
306 button_toggled (GtkWidget           *widget,
307                 GtkToggleToolButton *toggle_tool_button)
308 {
309   gboolean toggle_active;
310
311   toggle_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
312
313   if (toggle_tool_button->priv->active != toggle_active)
314     {
315       GtkWidget *menu_item;
316       
317       toggle_tool_button->priv->active = toggle_active;
318        
319       if ((menu_item =
320            gtk_tool_item_get_proxy_menu_item (GTK_TOOL_ITEM (toggle_tool_button), MENU_ID)))
321         {
322           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
323                                           toggle_tool_button->priv->active);
324         }
325
326       g_object_notify (G_OBJECT (toggle_tool_button), "active");
327       g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
328     }
329 }
330
331 static void
332 gtk_toggle_tool_button_activatable_interface_init (GtkActivatableIface *iface)
333 {
334   parent_activatable_iface = g_type_interface_peek_parent (iface);
335   iface->update = gtk_toggle_tool_button_update;
336   iface->sync_action_properties = gtk_toggle_tool_button_sync_action_properties;
337 }
338
339 static void
340 gtk_toggle_tool_button_update (GtkActivatable *activatable,
341                                GtkAction      *action,
342                                const gchar    *property_name)
343 {
344   GtkToggleToolButton *button;
345
346   parent_activatable_iface->update (activatable, action, property_name);
347
348   button = GTK_TOGGLE_TOOL_BUTTON (activatable);
349
350   if (strcmp (property_name, "active") == 0)
351     {
352       gtk_action_block_activate (action);
353       gtk_toggle_tool_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
354       gtk_action_unblock_activate (action);
355     }
356 }
357
358 static void
359 gtk_toggle_tool_button_sync_action_properties (GtkActivatable *activatable,
360                                                GtkAction      *action)
361 {
362   GtkToggleToolButton *button;
363
364   parent_activatable_iface->sync_action_properties (activatable, action);
365
366   if (!GTK_IS_TOGGLE_ACTION (action))
367     return;
368
369   button = GTK_TOGGLE_TOOL_BUTTON (activatable);
370
371   gtk_action_block_activate (action);
372   gtk_toggle_tool_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
373   gtk_action_unblock_activate (action);
374 }
375
376
377 /**
378  * gtk_toggle_tool_button_new:
379  * 
380  * Returns a new #GtkToggleToolButton
381  * 
382  * Return value: a newly created #GtkToggleToolButton
383  * 
384  * Since: 2.4
385  **/
386 GtkToolItem *
387 gtk_toggle_tool_button_new (void)
388 {
389   GtkToolButton *button;
390
391   button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
392                          NULL);
393   
394   return GTK_TOOL_ITEM (button);
395 }
396
397 /**
398  * gtk_toggle_tool_button_new_from_stock:
399  * @stock_id: the name of the stock item 
400  *
401  * Creates a new #GtkToggleToolButton containing the image and text from a
402  * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
403  * and #GTK_STOCK_APPLY.
404  *
405  * It is an error if @stock_id is not a name of a stock item.
406  * 
407  * Return value: A new #GtkToggleToolButton
408  * 
409  * Since: 2.4
410  **/
411 GtkToolItem *
412 gtk_toggle_tool_button_new_from_stock (const gchar *stock_id)
413 {
414   GtkToolButton *button;
415
416   g_return_val_if_fail (stock_id != NULL, NULL);
417   
418   button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
419                          "stock-id", stock_id,
420                          NULL);
421   
422   return GTK_TOOL_ITEM (button);
423 }
424
425 /**
426  * gtk_toggle_tool_button_set_active:
427  * @button: a #GtkToggleToolButton
428  * @is_active: whether @button should be active
429  * 
430  * Sets the status of the toggle tool button. Set to %TRUE if you
431  * want the GtkToggleButton to be 'pressed in', and %FALSE to raise it.
432  * This action causes the toggled signal to be emitted.
433  * 
434  * Since: 2.4
435  **/
436 void
437 gtk_toggle_tool_button_set_active (GtkToggleToolButton *button,
438                                    gboolean is_active)
439 {
440   g_return_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button));
441
442   is_active = is_active != FALSE;
443
444   if (button->priv->active != is_active)
445     gtk_button_clicked (GTK_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button))));
446 }
447
448 /**
449  * gtk_toggle_tool_button_get_active:
450  * @button: a #GtkToggleToolButton
451  * 
452  * Queries a #GtkToggleToolButton and returns its current state.
453  * Returns %TRUE if the toggle button is pressed in and %FALSE if it is raised.
454  * 
455  * Return value: %TRUE if the toggle tool button is pressed in, %FALSE if not
456  * 
457  * Since: 2.4
458  **/
459 gboolean
460 gtk_toggle_tool_button_get_active (GtkToggleToolButton *button)
461 {
462   g_return_val_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button), FALSE);
463
464   return button->priv->active;
465 }