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