]> Pileus Git - ~andy/gtk/blob - gtk/gtktoggletoolbutton.c
Intern some more strings.
[~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 static void gtk_toggle_tool_button_init       (GtkToggleToolButton      *button);
54 static void gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass);
55
56 static void     gtk_toggle_tool_button_set_property        (GObject      *object,
57                                                             guint         prop_id,
58                                                             const GValue *value,
59                                                             GParamSpec   *pspec);
60 static void     gtk_toggle_tool_button_get_property        (GObject      *object,
61                                                             guint         prop_id,
62                                                             GValue       *value,
63                                                             GParamSpec   *pspec);
64
65 static gboolean gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *button);
66
67 static void button_toggled      (GtkWidget           *widget,
68                                  GtkToggleToolButton *button);
69 static void menu_item_activated (GtkWidget           *widget,
70                                  GtkToggleToolButton *button);
71
72 static GObjectClass *parent_class = NULL;
73 static guint         toggle_signals[LAST_SIGNAL] = { 0 };
74
75 GType
76 gtk_toggle_tool_button_get_type (void)
77 {
78   static GType type = 0;
79
80   if (!type)
81     {
82       static const GTypeInfo type_info =
83         {
84           sizeof (GtkToggleToolButtonClass),
85           (GBaseInitFunc) 0,
86           (GBaseFinalizeFunc) 0,
87           (GClassInitFunc) gtk_toggle_tool_button_class_init,
88           (GClassFinalizeFunc) 0,
89           NULL,
90           sizeof (GtkToggleToolButton),
91           0, /* n_preallocs */
92           (GInstanceInitFunc) gtk_toggle_tool_button_init
93         };
94
95       type = g_type_register_static (GTK_TYPE_TOOL_BUTTON,
96                                      I_("GtkToggleToolButton"), &type_info, 0);
97     }
98   return type;
99 }
100
101
102 static void
103 gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass)
104 {
105   GObjectClass *object_class;
106   GtkToolItemClass *toolitem_class;
107   GtkToolButtonClass *toolbutton_class;
108
109   parent_class = g_type_class_peek_parent (klass);
110
111   object_class = (GObjectClass *)klass;
112   toolitem_class = (GtkToolItemClass *)klass;
113   toolbutton_class = (GtkToolButtonClass *)klass;
114
115   object_class->set_property = gtk_toggle_tool_button_set_property;
116   object_class->get_property = gtk_toggle_tool_button_get_property;
117
118   toolitem_class->create_menu_proxy = gtk_toggle_tool_button_create_menu_proxy;
119   toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON;
120
121   /**
122    * GtkToggleToolButton:active:
123    *
124    * If the toggle tool button should be pressed in or not.
125    *
126    * Since: 2.8
127    */
128   g_object_class_install_property (object_class,
129                                    PROP_ACTIVE,
130                                    g_param_spec_boolean ("active",
131                                                          P_("Active"),
132                                                          P_("If the toggle button should be pressed in or not"),
133                                                          FALSE,
134                                                          GTK_PARAM_READWRITE));
135
136 /**
137  * GtkToggleToolButton::toggled:
138  * @toggle_tool_button: the object that emitted the signal
139  *
140  * Emitted whenever the toggle tool button changes state.
141  **/
142   toggle_signals[TOGGLED] =
143     g_signal_new (I_("toggled"),
144                   G_OBJECT_CLASS_TYPE (klass),
145                   G_SIGNAL_RUN_FIRST,
146                   G_STRUCT_OFFSET (GtkToggleToolButtonClass, toggled),
147                   NULL, NULL,
148                   g_cclosure_marshal_VOID__VOID,
149                   G_TYPE_NONE, 0);
150
151   g_type_class_add_private (object_class, sizeof (GtkToggleToolButtonPrivate));
152 }
153
154 static void
155 gtk_toggle_tool_button_init (GtkToggleToolButton *button)
156 {
157   GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
158   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button));
159
160   button->priv = GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE (button);
161
162   /* If the real button is a radio button, it may have been
163    * active at the time it was created.
164    */
165   button->priv->active = gtk_toggle_button_get_active (toggle_button);
166     
167   g_signal_connect_object (toggle_button,
168                            "toggled", G_CALLBACK (button_toggled), button, 0);
169 }
170
171 static void
172 gtk_toggle_tool_button_set_property (GObject      *object,
173                                      guint         prop_id,
174                                      const GValue *value,
175                                      GParamSpec   *pspec)
176 {
177   GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
178
179   switch (prop_id)
180     {
181       case PROP_ACTIVE:
182         button->priv->active = g_value_get_boolean (value);
183         break;
184
185       default:
186         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187         break;
188     }
189 }
190
191 static void
192 gtk_toggle_tool_button_get_property (GObject    *object,
193                                      guint       prop_id,
194                                      GValue     *value,
195                                      GParamSpec *pspec)
196 {
197   GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);
198
199   switch (prop_id)
200     {
201       case PROP_ACTIVE:
202         g_value_set_boolean (value, gtk_toggle_tool_button_get_active (button));
203         break;
204
205       default:
206         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207         break;
208     }
209 }
210
211 static gboolean
212 gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *item)
213 {
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;
219   const char *label;
220
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);
224
225   if (label_widget && GTK_IS_LABEL (label_widget))
226     {
227       label = gtk_label_get_label (GTK_LABEL (label_widget));
228       use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (label_widget));
229     }
230   else if (label_text)
231     {
232       label = label_text;
233       use_mnemonic = gtk_tool_button_get_use_underline (tool_button);
234     }
235   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
236     {
237       label = stock_item.label;
238     }
239   else
240     {
241       label = "";
242     }
243   
244   if (use_mnemonic)
245     menu_item = gtk_check_menu_item_new_with_mnemonic (label);
246   else
247     menu_item = gtk_check_menu_item_new_with_label (label);
248
249   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
250                                   toggle_tool_button->priv->active);
251
252   if (GTK_IS_RADIO_TOOL_BUTTON (toggle_tool_button))
253     {
254       gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (menu_item),
255                                              TRUE);
256     }
257
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)),
262                                   FALSE);
263
264   gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
265   
266   return TRUE;
267 }
268
269 /* There are two activatable widgets, a toggle button and a menu item.
270  *
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.
274  *
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.
280  */
281 static void
282 menu_item_activated (GtkWidget           *menu_item,
283                      GtkToggleToolButton *toggle_tool_button)
284 {
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));
287
288   if (toggle_tool_button->priv->active != menu_active)
289     {
290       toggle_tool_button->priv->active = menu_active;
291
292       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)),
293                                     toggle_tool_button->priv->active);
294
295       g_object_notify (G_OBJECT (toggle_tool_button), "active");
296       g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
297     }
298 }
299
300 static void
301 button_toggled (GtkWidget           *widget,
302                 GtkToggleToolButton *toggle_tool_button)
303 {
304   gboolean toggle_active = GTK_TOGGLE_BUTTON (widget)->active;
305
306   if (toggle_tool_button->priv->active != toggle_active)
307     {
308       GtkWidget *menu_item;
309       
310       toggle_tool_button->priv->active = toggle_active;
311        
312       if ((menu_item =
313            gtk_tool_item_get_proxy_menu_item (GTK_TOOL_ITEM (toggle_tool_button), MENU_ID)))
314         {
315           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
316                                           toggle_tool_button->priv->active);
317         }
318
319       g_object_notify (G_OBJECT (toggle_tool_button), "active");
320       g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
321     }
322 }
323
324 /**
325  * gtk_toggle_tool_button_new:
326  * 
327  * Returns a new #GtkToggleToolButton
328  * 
329  * Return value: a newly created #GtkToggleToolButton
330  * 
331  * Since: 2.4
332  **/
333 GtkToolItem *
334 gtk_toggle_tool_button_new (void)
335 {
336   GtkToolButton *button;
337
338   button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
339                          NULL);
340   
341   return GTK_TOOL_ITEM (button);
342 }
343
344 /**
345  * gtk_toggle_tool_button_new_from_stock:
346  * @stock_id: the name of the stock item 
347  *
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.
351  *
352  * It is an error if @stock_id is not a name of a stock item.
353  * 
354  * Return value: A new #GtkToggleToolButton
355  * 
356  * Since: 2.4
357  **/
358 GtkToolItem *
359 gtk_toggle_tool_button_new_from_stock (const gchar *stock_id)
360 {
361   GtkToolButton *button;
362
363   g_return_val_if_fail (stock_id != NULL, NULL);
364   
365   button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
366                          "stock-id", stock_id,
367                          NULL);
368   
369   return GTK_TOOL_ITEM (button);
370 }
371
372 /**
373  * gtk_toggle_tool_button_set_active:
374  * @button: a #GtkToggleToolButton
375  * @is_active: whether @button should be active
376  * 
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.
380  * 
381  * Since: 2.4
382  **/
383 void
384 gtk_toggle_tool_button_set_active (GtkToggleToolButton *button,
385                                    gboolean is_active)
386 {
387   g_return_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button));
388
389   is_active = is_active != FALSE;
390
391   if (button->priv->active != is_active)
392     gtk_button_clicked (GTK_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button))));
393 }
394
395 /**
396  * gtk_toggle_tool_button_get_active:
397  * @button: a #GtkToggleToolButton
398  * 
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.
401  * 
402  * Return value: %TRUE if the toggle tool button is pressed in, %FALSE if not
403  * 
404  * Since: 2.4
405  **/
406 gboolean
407 gtk_toggle_tool_button_get_active (GtkToggleToolButton *button)
408 {
409   g_return_val_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button), FALSE);
410
411   return button->priv->active;
412 }
413
414 #define __GTK_TOGGLE_TOOL_BUTTON_C__
415 #include "gtkaliasdef.c"