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