]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
Return FALSE from expose handlers.
[~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 "gtklabel.h"
28 #include "gtkmain.h"
29 #include "gtkmarshalers.h"
30 #include "gtksignal.h"
31 #include "gtktogglebutton.h"
32 #include "gtkintl.h"
33
34 #define DEFAULT_LEFT_POS  4
35 #define DEFAULT_TOP_POS   4
36 #define DEFAULT_SPACING   7
37
38 enum {
39   TOGGLED,
40   LAST_SIGNAL
41 };
42
43 enum {
44   PROP_0,
45   PROP_ACTIVE,
46   PROP_INCONSISTENT,
47   PROP_DRAW_INDICATOR
48 };
49
50
51 static void gtk_toggle_button_class_init    (GtkToggleButtonClass *klass);
52 static void gtk_toggle_button_init          (GtkToggleButton      *toggle_button);
53 static void gtk_toggle_button_paint         (GtkWidget            *widget,
54                                              GdkRectangle         *area);
55 static gint gtk_toggle_button_expose        (GtkWidget            *widget,
56                                              GdkEventExpose       *event);
57 static void gtk_toggle_button_pressed       (GtkButton            *button);
58 static void gtk_toggle_button_released      (GtkButton            *button);
59 static void gtk_toggle_button_clicked       (GtkButton            *button);
60 static void gtk_toggle_button_set_property  (GObject              *object,
61                                              guint                 prop_id,
62                                              const GValue         *value,
63                                              GParamSpec           *pspec);
64 static void gtk_toggle_button_get_property  (GObject              *object,
65                                              guint                 prop_id,
66                                              GValue               *value,
67                                              GParamSpec           *pspec);
68 static void gtk_toggle_button_update_state  (GtkButton            *button);
69
70 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
71 static GtkContainerClass *parent_class = NULL;
72
73 GtkType
74 gtk_toggle_button_get_type (void)
75 {
76   static GtkType toggle_button_type = 0;
77
78   if (!toggle_button_type)
79     {
80       static const GtkTypeInfo toggle_button_info =
81       {
82         "GtkToggleButton",
83         sizeof (GtkToggleButton),
84         sizeof (GtkToggleButtonClass),
85         (GtkClassInitFunc) gtk_toggle_button_class_init,
86         (GtkObjectInitFunc) gtk_toggle_button_init,
87         /* reserved_1 */ NULL,
88         /* reserved_2 */ NULL,
89         (GtkClassInitFunc) NULL,
90       };
91
92       toggle_button_type = gtk_type_unique (GTK_TYPE_BUTTON, &toggle_button_info);
93     }
94
95   return toggle_button_type;
96 }
97
98 static void
99 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
100 {
101   GtkObjectClass *object_class;
102   GObjectClass   *gobject_class;
103   GtkWidgetClass *widget_class;
104   GtkContainerClass *container_class;
105   GtkButtonClass *button_class;
106
107   object_class = (GtkObjectClass*) class;
108   gobject_class = G_OBJECT_CLASS (class);
109   widget_class = (GtkWidgetClass*) class;
110   container_class = (GtkContainerClass*) class;
111   button_class = (GtkButtonClass*) class;
112
113   parent_class = gtk_type_class (GTK_TYPE_BUTTON);
114
115
116   gobject_class->set_property = gtk_toggle_button_set_property;
117   gobject_class->get_property = gtk_toggle_button_get_property;
118
119   widget_class->expose_event = gtk_toggle_button_expose;
120
121   button_class->pressed = gtk_toggle_button_pressed;
122   button_class->released = gtk_toggle_button_released;
123   button_class->clicked = gtk_toggle_button_clicked;
124   button_class->enter = gtk_toggle_button_update_state;
125   button_class->leave = gtk_toggle_button_update_state;
126
127   class->toggled = NULL;
128
129   g_object_class_install_property (gobject_class,
130                                    PROP_ACTIVE,
131                                    g_param_spec_boolean ("active",
132                                                          _("Active"),
133                                                          _("If the toggle button should be pressed in or not"),
134                                                          FALSE,
135                                                          G_PARAM_READWRITE));
136
137   g_object_class_install_property (gobject_class,
138                                    PROP_INCONSISTENT,
139                                    g_param_spec_boolean ("inconsistent",
140                                                          _("Inconsistent"),
141                                                          _("If the toggle button is in an \"in between\" state."),
142                                                          FALSE,
143                                                          G_PARAM_READWRITE));
144
145   g_object_class_install_property (gobject_class,
146                                    PROP_DRAW_INDICATOR,
147                                    g_param_spec_boolean ("draw_indicator",
148                                                          _("Draw Indicator"),
149                                                          _("If the toggle part of the button is displayed"),
150                                                          FALSE,
151                                                          G_PARAM_READWRITE));
152
153   toggle_button_signals[TOGGLED] =
154     gtk_signal_new ("toggled",
155                     GTK_RUN_FIRST,
156                     GTK_CLASS_TYPE (object_class),
157                     GTK_SIGNAL_OFFSET (GtkToggleButtonClass, toggled),
158                     _gtk_marshal_VOID__VOID,
159                     GTK_TYPE_NONE, 0);
160 }
161
162 static void
163 gtk_toggle_button_init (GtkToggleButton *toggle_button)
164 {
165   toggle_button->active = FALSE;
166   toggle_button->draw_indicator = FALSE;
167 }
168
169
170 GtkWidget*
171 gtk_toggle_button_new (void)
172 {
173   return GTK_WIDGET (gtk_type_new (gtk_toggle_button_get_type ()));
174 }
175
176 GtkWidget*
177 gtk_toggle_button_new_with_label (const gchar *label)
178 {
179   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
180 }
181
182 /**
183  * gtk_toggle_button_new_with_mnemonic:
184  * @label: the text of the button, with an underscore in front of the
185  *         mnemonic character
186  * @returns: a new #GtkToggleButton
187  *
188  * Creates a new #GtkToggleButton containing a label. The label
189  * will be created using gtk_label_new_with_mnemonic(), so underscores
190  * in @label indicate the mnemonic for the button.
191  **/
192 GtkWidget*
193 gtk_toggle_button_new_with_mnemonic (const gchar *label)
194 {
195   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, "use_underline", TRUE, NULL);
196 }
197
198 static void
199 gtk_toggle_button_set_property (GObject      *object,
200                                 guint         prop_id,
201                                 const GValue *value,
202                                 GParamSpec   *pspec)
203 {
204   GtkToggleButton *tb;
205
206   tb = GTK_TOGGLE_BUTTON (object);
207
208   switch (prop_id)
209     {
210     case PROP_ACTIVE:
211       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
212       break;
213     case PROP_INCONSISTENT:
214       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
215       break;
216     case PROP_DRAW_INDICATOR:
217       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
218       break;
219     default:
220       break;
221     }
222 }
223
224 static void
225 gtk_toggle_button_get_property (GObject      *object,
226                                 guint         prop_id,
227                                 GValue       *value,
228                                 GParamSpec   *pspec)
229 {
230   GtkToggleButton *tb;
231
232   tb = GTK_TOGGLE_BUTTON (object);
233
234   switch (prop_id)
235     {
236     case PROP_ACTIVE:
237       g_value_set_boolean (value, tb->active);
238       break;
239     case PROP_INCONSISTENT:
240       g_value_set_boolean (value, tb->inconsistent);
241       break;
242     case PROP_DRAW_INDICATOR:
243       g_value_set_boolean (value, tb->draw_indicator);
244       break;
245     default:
246       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247       break;
248     }
249 }
250
251 void
252 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
253                             gboolean         draw_indicator)
254 {
255   GtkWidget *widget;
256
257   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
258
259   widget = GTK_WIDGET (toggle_button);
260
261   draw_indicator = draw_indicator ? TRUE : FALSE;
262
263   if (toggle_button->draw_indicator != draw_indicator)
264     {
265       toggle_button->draw_indicator = draw_indicator;
266
267       if (GTK_WIDGET_VISIBLE (toggle_button))
268         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
269
270       g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
271     }
272 }
273
274 /**
275  * gtk_toggle_button_get_mode:
276  * @toggle_button: a #GtkToggleButton
277  *
278  * Retrieves whether the button is displayed as a separate indicator
279  * and label. See gtk_toggle_button_set_mode().
280  *
281  * Return value: %TRUE if the togglebutton is drawn as a separate indicator
282  *   and label.
283  **/
284 gboolean
285 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
286 {
287   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
288
289   return toggle_button->draw_indicator;
290 }
291
292 void
293 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
294                               gboolean         is_active)
295 {
296   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
297
298   is_active = is_active != FALSE;
299
300   if (toggle_button->active != is_active)
301     gtk_button_clicked (GTK_BUTTON (toggle_button));
302 }
303
304
305 gboolean
306 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
307 {
308   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
309
310   return (toggle_button->active) ? TRUE : FALSE;
311 }
312
313
314 void
315 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
316 {
317   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
318
319   gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]);
320 }
321
322 /**
323  * gtk_toggle_button_set_inconsistent:
324  * @toggle_button: a #GtkToggleButton
325  * @setting: %TRUE if state is inconsistent
326  *
327  * If the user has selected a range of elements (such as some text or
328  * spreadsheet cells) that are affected by a toggle button, and the
329  * current values in that range are inconsistent, you may want to
330  * display the toggle in an "in between" state. This function turns on
331  * "in between" display.  Normally you would turn off the inconsistent
332  * state again if the user toggles the toggle button. This has to be
333  * done manually, gtk_toggle_button_set_inconsistent() only affects
334  * visual appearance, it doesn't affect the semantics of the button.
335  * 
336  **/
337 void
338 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
339                                     gboolean         setting)
340 {
341   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
342   
343   setting = setting != FALSE;
344
345   if (setting != toggle_button->inconsistent)
346     {
347       toggle_button->inconsistent = setting;
348       
349       gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
350       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
351
352       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
353     }
354 }
355
356 /**
357  * gtk_toggle_button_get_inconsistent:
358  * @toggle_button: a #GtkToggleButton
359  * 
360  * Gets the value set by gtk_toggle_button_set_inconsistent().
361  * 
362  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
363  **/
364 gboolean
365 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
366 {
367   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
368
369   return toggle_button->inconsistent;
370 }
371
372 static void
373 gtk_toggle_button_paint (GtkWidget    *widget,
374                          GdkRectangle *area)
375 {
376   GtkButton *button;
377   GtkToggleButton *toggle_button;
378   GtkShadowType shadow_type;
379   GtkStateType state_type;
380   gint width, height;
381   gboolean interior_focus;
382   gint border_width;
383   gint x, y;
384
385   button = GTK_BUTTON (widget);
386   toggle_button = GTK_TOGGLE_BUTTON (widget);
387
388   if (GTK_WIDGET_DRAWABLE (widget))
389     {
390       border_width = GTK_CONTAINER (widget)->border_width;
391       
392       gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
393       
394       x = widget->allocation.x + border_width;
395       y = widget->allocation.y + border_width;
396       width = widget->allocation.width - border_width * 2;
397       height = widget->allocation.height - border_width * 2;
398
399       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
400           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
401         {
402           gtk_paint_box (widget->style, widget->window,
403                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
404                          area, widget, "togglebuttondefault",
405                          x, y, width, height);
406         }
407
408       if (GTK_WIDGET_CAN_DEFAULT (widget))
409         {
410           x += widget->style->xthickness;
411           y += widget->style->ythickness;
412           width -= 2 * x + DEFAULT_SPACING;
413           height -= 2 * y + DEFAULT_SPACING;
414           x += DEFAULT_LEFT_POS;
415           y += DEFAULT_TOP_POS;
416         }
417
418       if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
419         {
420           x += 1;
421           y += 1;
422           width -= 2;
423           height -= 2;
424         }
425
426       state_type = GTK_WIDGET_STATE (widget);
427       
428       if (toggle_button->inconsistent)
429         {
430           if (state_type == GTK_STATE_ACTIVE)
431             state_type = GTK_STATE_NORMAL;
432           shadow_type = GTK_SHADOW_ETCHED_IN;
433         }
434       else
435         shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
436
437       if (button->relief != GTK_RELIEF_NONE ||
438           (GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL &&
439            GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE))
440         gtk_paint_box (widget->style, widget->window,
441                        state_type,
442                        shadow_type, area, widget, "togglebutton",
443                        x, y, width, height);
444       
445       if (GTK_WIDGET_HAS_FOCUS (widget))
446         {
447           if (interior_focus)
448             {
449               x += widget->style->xthickness + 1;
450               y += widget->style->xthickness + 1;
451               width -= 2 * (widget->style->xthickness + 1);
452               height -= 2 * (widget->style->ythickness + 1);
453             }
454           else
455             {
456               x -= 1;
457               y -= 1;
458               width += 2;
459               height += 2;
460             }
461
462           gtk_paint_focus (widget->style, widget->window,
463                            area, widget, "togglebutton",
464                            x, y, width - 1, height - 1);
465         }
466     }
467 }
468
469 static gint
470 gtk_toggle_button_expose (GtkWidget      *widget,
471                           GdkEventExpose *event)
472 {
473   if (GTK_WIDGET_DRAWABLE (widget))
474     {
475       GtkWidget *child = GTK_BIN (widget)->child;
476
477       gtk_toggle_button_paint (widget, &event->area);
478
479       if (child)
480         gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
481     }
482   
483   return FALSE;
484 }
485
486 static void
487 gtk_toggle_button_pressed (GtkButton *button)
488 {
489   button->button_down = TRUE;
490
491   gtk_toggle_button_update_state (button);
492 }
493
494 static void
495 gtk_toggle_button_released (GtkButton *button)
496 {
497   if (button->button_down)
498     {
499       button->button_down = FALSE;
500
501       if (button->in_button)
502         gtk_button_clicked (button);
503
504       gtk_toggle_button_update_state (button);
505     }
506 }
507
508 static void
509 gtk_toggle_button_clicked (GtkButton *button)
510 {
511   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
512   toggle_button->active = !toggle_button->active;
513
514   gtk_toggle_button_toggled (toggle_button);
515
516   gtk_toggle_button_update_state (button);
517
518   g_object_notify (G_OBJECT (toggle_button), "active");
519 }
520
521 static void
522 gtk_toggle_button_update_state (GtkButton *button)
523 {
524   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
525   gboolean depressed;
526   GtkStateType new_state;
527
528   if (toggle_button->inconsistent)
529     depressed = FALSE;
530   else if (button->in_button && button->button_down)
531     depressed = !toggle_button->active;
532   else
533     depressed = toggle_button->active;
534       
535   if (!button->button_down && button->in_button)
536     new_state = GTK_STATE_PRELIGHT;
537   else
538     new_state = depressed ? GTK_STATE_ACTIVE: GTK_STATE_NORMAL;
539
540   _gtk_button_set_depressed (button, depressed); 
541   gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
542 }