]> Pileus Git - ~andy/gtk/blob - gtk/gtktoggletoolbutton.c
Define macros GTK_PARAM_READABLE, GTK_PARAM_WRITABLE, GTK_PARAM_READWRITE
[~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 #define GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TOGGLE_TOOL_BUTTON, GtkToggleToolButtonPrivate))
41
42 struct _GtkToggleToolButtonPrivate
43 {
44   guint active : 1;
45 };
46   
47 static void gtk_toggle_tool_button_init       (GtkToggleToolButton      *button);
48 static void gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass);
49
50 static gboolean gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *button);
51
52 static void button_toggled      (GtkWidget           *widget,
53                                  GtkToggleToolButton *button);
54 static void menu_item_activated (GtkWidget           *widget,
55                                  GtkToggleToolButton *button);
56
57 static GObjectClass *parent_class = NULL;
58 static guint         toggle_signals[LAST_SIGNAL] = { 0 };
59
60 GType
61 gtk_toggle_tool_button_get_type (void)
62 {
63   static GType type = 0;
64
65   if (!type)
66     {
67       static const GTypeInfo type_info =
68         {
69           sizeof (GtkToggleToolButtonClass),
70           (GBaseInitFunc) 0,
71           (GBaseFinalizeFunc) 0,
72           (GClassInitFunc) gtk_toggle_tool_button_class_init,
73           (GClassFinalizeFunc) 0,
74           NULL,
75           sizeof (GtkToggleToolButton),
76           0, /* n_preallocs */
77           (GInstanceInitFunc) gtk_toggle_tool_button_init
78         };
79
80       type = g_type_register_static (GTK_TYPE_TOOL_BUTTON,
81                                      "GtkToggleToolButton", &type_info, 0);
82     }
83   return type;
84 }
85
86
87 static void
88 gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass)
89 {
90   GObjectClass *object_class;
91   GtkToolItemClass *toolitem_class;
92   GtkToolButtonClass *toolbutton_class;
93
94   parent_class = g_type_class_peek_parent (klass);
95
96   object_class = (GObjectClass *)klass;
97   toolitem_class = (GtkToolItemClass *)klass;
98   toolbutton_class = (GtkToolButtonClass *)klass;
99
100   toolitem_class->create_menu_proxy = gtk_toggle_tool_button_create_menu_proxy;
101   toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON;
102   
103 /**
104  * GtkToggleToolButton::toggled:
105  * @toggle_tool_button: the object that emitted the signal
106  *
107  * Emitted whenever the toggle tool button changes state.
108  **/
109   toggle_signals[TOGGLED] =
110     g_signal_new ("toggled",
111                   G_OBJECT_CLASS_TYPE (klass),
112                   G_SIGNAL_RUN_FIRST,
113                   G_STRUCT_OFFSET (GtkToggleToolButtonClass, toggled),
114                   NULL, NULL,
115                   g_cclosure_marshal_VOID__VOID,
116                   G_TYPE_NONE, 0);
117
118   g_type_class_add_private (object_class, sizeof (GtkToggleToolButtonPrivate));
119 }
120
121 static void
122 gtk_toggle_tool_button_init (GtkToggleToolButton *button)
123 {
124   GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
125   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button));
126
127   button->priv = GTK_TOGGLE_TOOL_BUTTON_GET_PRIVATE (button);
128
129   /* If the real button is a radio button, it may have been
130    * active at the time it was created.
131    */
132   button->priv->active = gtk_toggle_button_get_active (toggle_button);
133     
134   g_signal_connect_object (toggle_button,
135                            "toggled", G_CALLBACK (button_toggled), button, 0);
136 }
137
138 static gboolean
139 gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *item)
140 {
141   GtkToolButton *tool_button = GTK_TOOL_BUTTON (item);
142   GtkToggleToolButton *toggle_tool_button = GTK_TOGGLE_TOOL_BUTTON (item);
143   GtkWidget *menu_item = NULL;
144   GtkStockItem stock_item;
145   gboolean use_mnemonic = TRUE;
146   const char *label;
147
148   GtkWidget *label_widget = gtk_tool_button_get_label_widget (tool_button);
149   const gchar *label_text = gtk_tool_button_get_label (tool_button);
150   const gchar *stock_id = gtk_tool_button_get_stock_id (tool_button);
151
152   if (label_widget && GTK_IS_LABEL (label_widget))
153     {
154       label = gtk_label_get_label (GTK_LABEL (label_widget));
155       use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (label_widget));
156     }
157   else if (label_text)
158     {
159       label = label_text;
160       use_mnemonic = gtk_tool_button_get_use_underline (tool_button);
161     }
162   else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
163     {
164       label = stock_item.label;
165     }
166   else
167     {
168       label = "";
169     }
170   
171   if (use_mnemonic)
172     menu_item = gtk_check_menu_item_new_with_mnemonic (label);
173   else
174     menu_item = gtk_check_menu_item_new_with_label (label);
175
176   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
177                                   toggle_tool_button->priv->active);
178
179   if (GTK_IS_RADIO_TOOL_BUTTON (toggle_tool_button))
180     {
181       gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (menu_item),
182                                              TRUE);
183     }
184
185   g_signal_connect_closure_by_id (menu_item,
186                                   g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
187                                   g_cclosure_new_object (G_CALLBACK (menu_item_activated),
188                                                          G_OBJECT (toggle_tool_button)),
189                                   FALSE);
190
191   gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
192   
193   return TRUE;
194 }
195
196 /* There are two activatable widgets, a toggle button and a menu item.
197  *
198  * If a widget is activated and the state of the tool button is the same as
199  * the new state of the activated widget, then the other widget was the one
200  * that was activated by the user and updated the tool button's state.
201  *
202  * If the state of the tool button is not the same as the new state of the
203  * activated widget, then the activation was activated by the user, and the
204  * widget needs to make sure the tool button is updated before the other
205  * widget is activated. This will make sure the other widget a tool button
206  * in a state that matches its own new state.
207  */
208 static void
209 menu_item_activated (GtkWidget           *menu_item,
210                      GtkToggleToolButton *toggle_tool_button)
211 {
212   GtkToolButton *tool_button = GTK_TOOL_BUTTON (toggle_tool_button);
213   gboolean menu_active = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item));
214
215   if (toggle_tool_button->priv->active != menu_active)
216     {
217       toggle_tool_button->priv->active = menu_active;
218
219       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)),
220                                     toggle_tool_button->priv->active);
221
222       g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
223     }
224 }
225
226 static void
227 button_toggled (GtkWidget           *widget,
228                 GtkToggleToolButton *toggle_tool_button)
229 {
230   gboolean toggle_active = GTK_TOGGLE_BUTTON (widget)->active;
231
232   if (toggle_tool_button->priv->active != toggle_active)
233     {
234       GtkWidget *menu_item;
235       
236       toggle_tool_button->priv->active = toggle_active;
237        
238       if ((menu_item =
239            gtk_tool_item_get_proxy_menu_item (GTK_TOOL_ITEM (toggle_tool_button), MENU_ID)))
240         {
241           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
242                                           toggle_tool_button->priv->active);
243         }
244
245       g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
246     }
247 }
248
249 /**
250  * gtk_toggle_tool_button_new:
251  * 
252  * Returns a new #GtkToggleToolButton
253  * 
254  * Return value: a newly created #GtkToggleToolButton
255  * 
256  * Since: 2.4
257  **/
258 GtkToolItem *
259 gtk_toggle_tool_button_new (void)
260 {
261   GtkToolButton *button;
262
263   button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
264                          NULL);
265   
266   return GTK_TOOL_ITEM (button);
267 }
268
269 /**
270  * gtk_toggle_tool_button_new_from_stock:
271  * @stock_id: the name of the stock item 
272  *
273  * Creates a new #GtkToggleToolButton containing the image and text from a
274  * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
275  * and #GTK_STOCK_APPLY.
276  *
277  * It is an error if @stock_id is not a name of a stock item.
278  * 
279  * Return value: A new #GtkToggleToolButton
280  * 
281  * Since: 2.4
282  **/
283 GtkToolItem *
284 gtk_toggle_tool_button_new_from_stock (const gchar *stock_id)
285 {
286   GtkToolButton *button;
287
288   g_return_val_if_fail (stock_id != NULL, NULL);
289   
290   button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
291                          "stock_id", stock_id,
292                          NULL);
293   
294   return GTK_TOOL_ITEM (button);
295 }
296
297 /**
298  * gtk_toggle_tool_button_set_active:
299  * @button: a #GtkToggleToolButton
300  * @is_active: whether @button should be active
301  * 
302  * Sets the status of the toggle tool button. Set to %TRUE if you
303  * want the GtkToggleButton to be 'pressed in', and %FALSE to raise it.
304  * This action causes the toggled signal to be emitted.
305  * 
306  * Since: 2.4
307  **/
308 void
309 gtk_toggle_tool_button_set_active (GtkToggleToolButton *button,
310                                    gboolean is_active)
311 {
312   g_return_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button));
313
314   is_active = is_active != FALSE;
315
316   if (button->priv->active != is_active)
317     gtk_button_clicked (GTK_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button))));
318 }
319
320 /**
321  * gtk_toggle_tool_button_get_active:
322  * @button: a #GtkToggleToolButton
323  * 
324  * Queries a #GtkToggleToolButton and returns its current state.
325  * Returns %TRUE if the toggle button is pressed in and %FALSE if it is raised.
326  * 
327  * Return value: %TRUE if the toggle tool button is pressed in, %FALSE if not
328  * 
329  * Since: 2.4
330  **/
331 gboolean
332 gtk_toggle_tool_button_get_active (GtkToggleToolButton *button)
333 {
334   g_return_val_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button), FALSE);
335
336   return button->priv->active;
337 }
338
339 #define __GTK_TOGGLE_TOOL_BUTTON_C__
340 #include "gtkaliasdef.c"