]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
Turn off prelighting when gtk-touchscreen-mode is enabled (#135666)
[~andy/gtk] / gtk / gtktogglebutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <config.h>
28 #include "gtklabel.h"
29 #include "gtkmain.h"
30 #include "gtkmarshalers.h"
31 #include "gtktogglebutton.h"
32 #include "gtkprivate.h"
33 #include "gtkintl.h"
34 #include "gtkalias.h"
35
36 #define DEFAULT_LEFT_POS  4
37 #define DEFAULT_TOP_POS   4
38 #define DEFAULT_SPACING   7
39
40 enum {
41   TOGGLED,
42   LAST_SIGNAL
43 };
44
45 enum {
46   PROP_0,
47   PROP_ACTIVE,
48   PROP_INCONSISTENT,
49   PROP_DRAW_INDICATOR
50 };
51
52
53 static void gtk_toggle_button_class_init    (GtkToggleButtonClass *klass);
54 static void gtk_toggle_button_init          (GtkToggleButton      *toggle_button);
55 static gint gtk_toggle_button_expose        (GtkWidget            *widget,
56                                              GdkEventExpose       *event);
57 static gboolean gtk_toggle_button_mnemonic_activate  (GtkWidget            *widget,
58                                                       gboolean              group_cycling);
59 static void gtk_toggle_button_pressed       (GtkButton            *button);
60 static void gtk_toggle_button_released      (GtkButton            *button);
61 static void gtk_toggle_button_clicked       (GtkButton            *button);
62 static void gtk_toggle_button_set_property  (GObject              *object,
63                                              guint                 prop_id,
64                                              const GValue         *value,
65                                              GParamSpec           *pspec);
66 static void gtk_toggle_button_get_property  (GObject              *object,
67                                              guint                 prop_id,
68                                              GValue               *value,
69                                              GParamSpec           *pspec);
70 static void gtk_toggle_button_update_state  (GtkButton            *button);
71
72 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
73 static GtkContainerClass *parent_class = NULL;
74
75 GType
76 gtk_toggle_button_get_type (void)
77 {
78   static GType toggle_button_type = 0;
79
80   if (!toggle_button_type)
81     {
82       static const GTypeInfo toggle_button_info =
83       {
84         sizeof (GtkToggleButtonClass),
85         NULL,           /* base_init */
86         NULL,           /* base_finalize */
87         (GClassInitFunc) gtk_toggle_button_class_init,
88         NULL,           /* class_finalize */
89         NULL,           /* class_data */
90         sizeof (GtkToggleButton),
91         0,              /* n_preallocs */
92         (GInstanceInitFunc) gtk_toggle_button_init,
93       };
94
95       toggle_button_type =
96         g_type_register_static (GTK_TYPE_BUTTON, I_("GtkToggleButton"),
97                                 &toggle_button_info, 0);
98     }
99
100   return toggle_button_type;
101 }
102
103 static void
104 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
105 {
106   GObjectClass *gobject_class;
107   GtkWidgetClass *widget_class;
108   GtkContainerClass *container_class;
109   GtkButtonClass *button_class;
110
111   gobject_class = G_OBJECT_CLASS (class);
112   widget_class = (GtkWidgetClass*) class;
113   container_class = (GtkContainerClass*) class;
114   button_class = (GtkButtonClass*) class;
115
116   parent_class = g_type_class_peek_parent (class);
117
118   gobject_class->set_property = gtk_toggle_button_set_property;
119   gobject_class->get_property = gtk_toggle_button_get_property;
120
121   widget_class->expose_event = gtk_toggle_button_expose;
122   widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
123
124   button_class->pressed = gtk_toggle_button_pressed;
125   button_class->released = gtk_toggle_button_released;
126   button_class->clicked = gtk_toggle_button_clicked;
127   button_class->enter = gtk_toggle_button_update_state;
128   button_class->leave = gtk_toggle_button_update_state;
129
130   class->toggled = NULL;
131
132   g_object_class_install_property (gobject_class,
133                                    PROP_ACTIVE,
134                                    g_param_spec_boolean ("active",
135                                                          P_("Active"),
136                                                          P_("If the toggle button should be pressed in or not"),
137                                                          FALSE,
138                                                          GTK_PARAM_READWRITE));
139
140   g_object_class_install_property (gobject_class,
141                                    PROP_INCONSISTENT,
142                                    g_param_spec_boolean ("inconsistent",
143                                                          P_("Inconsistent"),
144                                                          P_("If the toggle button is in an \"in between\" state"),
145                                                          FALSE,
146                                                          GTK_PARAM_READWRITE));
147
148   g_object_class_install_property (gobject_class,
149                                    PROP_DRAW_INDICATOR,
150                                    g_param_spec_boolean ("draw-indicator",
151                                                          P_("Draw Indicator"),
152                                                          P_("If the toggle part of the button is displayed"),
153                                                          FALSE,
154                                                          GTK_PARAM_READWRITE));
155
156   toggle_button_signals[TOGGLED] =
157     g_signal_new (I_("toggled"),
158                   G_OBJECT_CLASS_TYPE (gobject_class),
159                   G_SIGNAL_RUN_FIRST,
160                   G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
161                   NULL, NULL,
162                   _gtk_marshal_VOID__VOID,
163                   G_TYPE_NONE, 0);
164 }
165
166 static void
167 gtk_toggle_button_init (GtkToggleButton *toggle_button)
168 {
169   toggle_button->active = FALSE;
170   toggle_button->draw_indicator = FALSE;
171   GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
172 }
173
174
175 GtkWidget*
176 gtk_toggle_button_new (void)
177 {
178   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
179 }
180
181 GtkWidget*
182 gtk_toggle_button_new_with_label (const gchar *label)
183 {
184   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
185 }
186
187 /**
188  * gtk_toggle_button_new_with_mnemonic:
189  * @label: the text of the button, with an underscore in front of the
190  *         mnemonic character
191  * @returns: a new #GtkToggleButton
192  *
193  * Creates a new #GtkToggleButton containing a label. The label
194  * will be created using gtk_label_new_with_mnemonic(), so underscores
195  * in @label indicate the mnemonic for the button.
196  **/
197 GtkWidget*
198 gtk_toggle_button_new_with_mnemonic (const gchar *label)
199 {
200   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, 
201                        "label", label, 
202                        "use-underline", TRUE, 
203                        NULL);
204 }
205
206 static void
207 gtk_toggle_button_set_property (GObject      *object,
208                                 guint         prop_id,
209                                 const GValue *value,
210                                 GParamSpec   *pspec)
211 {
212   GtkToggleButton *tb;
213
214   tb = GTK_TOGGLE_BUTTON (object);
215
216   switch (prop_id)
217     {
218     case PROP_ACTIVE:
219       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
220       break;
221     case PROP_INCONSISTENT:
222       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
223       break;
224     case PROP_DRAW_INDICATOR:
225       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
226       break;
227     default:
228       break;
229     }
230 }
231
232 static void
233 gtk_toggle_button_get_property (GObject      *object,
234                                 guint         prop_id,
235                                 GValue       *value,
236                                 GParamSpec   *pspec)
237 {
238   GtkToggleButton *tb;
239
240   tb = GTK_TOGGLE_BUTTON (object);
241
242   switch (prop_id)
243     {
244     case PROP_ACTIVE:
245       g_value_set_boolean (value, tb->active);
246       break;
247     case PROP_INCONSISTENT:
248       g_value_set_boolean (value, tb->inconsistent);
249       break;
250     case PROP_DRAW_INDICATOR:
251       g_value_set_boolean (value, tb->draw_indicator);
252       break;
253     default:
254       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255       break;
256     }
257 }
258
259 /**
260  * gtk_toggle_button_set_mode:
261  * @toggle_button: a #GtkToggleButton
262  * @draw_indicator: if %TRUE, draw the button as a separate indicator
263  * and label; if %FALSE, draw the button like a normal button
264  *
265  * Sets whether the button is displayed as a separate indicator and label.
266  * You can call this function on a checkbutton or a radiobutton with
267  * @draw_indicator = %FALSE to make the button look like a normal button
268  *
269  * This function only affects instances of classes like #GtkCheckButton
270  * and #GtkRadioButton that derive from #GtkToggleButton,
271  * not instances of #GtkToggleButton itself.
272  */
273 void
274 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
275                             gboolean         draw_indicator)
276 {
277   GtkWidget *widget;
278
279   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
280
281   widget = GTK_WIDGET (toggle_button);
282
283   draw_indicator = draw_indicator ? TRUE : FALSE;
284
285   if (toggle_button->draw_indicator != draw_indicator)
286     {
287       toggle_button->draw_indicator = draw_indicator;
288       GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
289       
290       if (GTK_WIDGET_VISIBLE (toggle_button))
291         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
292
293       g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
294     }
295 }
296
297 /**
298  * gtk_toggle_button_get_mode:
299  * @toggle_button: a #GtkToggleButton
300  *
301  * Retrieves whether the button is displayed as a separate indicator
302  * and label. See gtk_toggle_button_set_mode().
303  *
304  * Return value: %TRUE if the togglebutton is drawn as a separate indicator
305  *   and label.
306  **/
307 gboolean
308 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
309 {
310   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
311
312   return toggle_button->draw_indicator;
313 }
314
315 void
316 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
317                               gboolean         is_active)
318 {
319   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
320
321   is_active = is_active != FALSE;
322
323   if (toggle_button->active != is_active)
324     gtk_button_clicked (GTK_BUTTON (toggle_button));
325 }
326
327
328 gboolean
329 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
330 {
331   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
332
333   return (toggle_button->active) ? TRUE : FALSE;
334 }
335
336
337 void
338 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
339 {
340   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
341
342   g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
343 }
344
345 /**
346  * gtk_toggle_button_set_inconsistent:
347  * @toggle_button: a #GtkToggleButton
348  * @setting: %TRUE if state is inconsistent
349  *
350  * If the user has selected a range of elements (such as some text or
351  * spreadsheet cells) that are affected by a toggle button, and the
352  * current values in that range are inconsistent, you may want to
353  * display the toggle in an "in between" state. This function turns on
354  * "in between" display.  Normally you would turn off the inconsistent
355  * state again if the user toggles the toggle button. This has to be
356  * done manually, gtk_toggle_button_set_inconsistent() only affects
357  * visual appearance, it doesn't affect the semantics of the button.
358  * 
359  **/
360 void
361 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
362                                     gboolean         setting)
363 {
364   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
365   
366   setting = setting != FALSE;
367
368   if (setting != toggle_button->inconsistent)
369     {
370       toggle_button->inconsistent = setting;
371       
372       gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
373       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
374
375       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
376     }
377 }
378
379 /**
380  * gtk_toggle_button_get_inconsistent:
381  * @toggle_button: a #GtkToggleButton
382  * 
383  * Gets the value set by gtk_toggle_button_set_inconsistent().
384  * 
385  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
386  **/
387 gboolean
388 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
389 {
390   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
391
392   return toggle_button->inconsistent;
393 }
394
395 static gint
396 gtk_toggle_button_expose (GtkWidget      *widget,
397                           GdkEventExpose *event)
398 {
399   if (GTK_WIDGET_DRAWABLE (widget))
400     {
401       GtkWidget *child = GTK_BIN (widget)->child;
402       GtkButton *button = GTK_BUTTON (widget);
403       GtkStateType state_type;
404       GtkShadowType shadow_type;
405
406       state_type = GTK_WIDGET_STATE (widget);
407       
408       if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
409         {
410           if (state_type == GTK_STATE_ACTIVE)
411             state_type = GTK_STATE_NORMAL;
412           shadow_type = GTK_SHADOW_ETCHED_IN;
413         }
414       else
415         shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
416
417       _gtk_button_paint (button, &event->area, state_type, shadow_type,
418                          "togglebutton", "togglebuttondefault");
419
420       if (child)
421         gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
422     }
423   
424   return FALSE;
425 }
426
427 static gboolean
428 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
429                                      gboolean   group_cycling)
430 {
431   /*
432    * We override the standard implementation in 
433    * gtk_widget_real_mnemonic_activate() in order to focus the widget even
434    * if there is no mnemonic conflict.
435    */
436   if (GTK_WIDGET_CAN_FOCUS (widget))
437     gtk_widget_grab_focus (widget);
438
439   if (!group_cycling)
440     gtk_widget_activate (widget);
441
442   return TRUE;
443 }
444
445 static void
446 gtk_toggle_button_pressed (GtkButton *button)
447 {
448   button->button_down = TRUE;
449
450   gtk_toggle_button_update_state (button);
451   gtk_widget_queue_draw (GTK_WIDGET (button));
452 }
453
454 static void
455 gtk_toggle_button_released (GtkButton *button)
456 {
457   if (button->button_down)
458     {
459       button->button_down = FALSE;
460
461       if (button->in_button)
462         gtk_button_clicked (button);
463
464       gtk_toggle_button_update_state (button);
465       gtk_widget_queue_draw (GTK_WIDGET (button));
466     }
467 }
468
469 static void
470 gtk_toggle_button_clicked (GtkButton *button)
471 {
472   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
473   toggle_button->active = !toggle_button->active;
474
475   gtk_toggle_button_toggled (toggle_button);
476
477   gtk_toggle_button_update_state (button);
478
479   g_object_notify (G_OBJECT (toggle_button), "active");
480 }
481
482 static void
483 gtk_toggle_button_update_state (GtkButton *button)
484 {
485   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
486   gboolean depressed, touchscreen;
487   GtkStateType new_state;
488
489   g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
490                 "gtk-touchscreen-mode", &touchscreen,
491                 NULL);
492
493   if (toggle_button->inconsistent)
494     depressed = FALSE;
495   else if (button->in_button && button->button_down)
496     depressed = TRUE;
497   else
498     depressed = toggle_button->active;
499       
500   if (!touchscreen && button->in_button && (!button->button_down || toggle_button->draw_indicator))
501     new_state = GTK_STATE_PRELIGHT;
502   else
503     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
504
505   _gtk_button_set_depressed (button, depressed); 
506   gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
507 }
508
509 #define __GTK_TOGGLE_BUTTON_C__
510 #include "gtkaliasdef.c"