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 "gtktoggleaction.h"
33 #include "gtkactivatable.h"
34 #include "gtkprivate.h"
35 #include "gtkintl.h"
36 #include "gtkalias.h"
37
38 #define DEFAULT_LEFT_POS 4
39 #define DEFAULT_TOP_POS 4
40 #define DEFAULT_SPACING 7
41
42 enum {
43 TOGGLED,
44 LAST_SIGNAL
45 };
46
47 enum {
48 PROP_0,
49 PROP_ACTIVE,
50 PROP_INCONSISTENT,
51 PROP_DRAW_INDICATOR
52 };
53
54
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
73 static void gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface);
74 static void gtk_toggle_button_update (GtkActivatable *activatable,
75 GtkAction *action,
76 const gchar *property_name);
77 static void gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
78 GtkAction *action);
79
80 static GtkActivatableIface *parent_activatable_iface;
81 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
82
G_DEFINE_TYPE_WITH_CODE(GtkToggleButton,gtk_toggle_button,GTK_TYPE_BUTTON,G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,gtk_toggle_button_activatable_interface_init))83 G_DEFINE_TYPE_WITH_CODE (GtkToggleButton, gtk_toggle_button, GTK_TYPE_BUTTON,
84 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
85 gtk_toggle_button_activatable_interface_init))
86
87 static void
88 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
89 {
90 GObjectClass *gobject_class;
91 GtkWidgetClass *widget_class;
92 GtkButtonClass *button_class;
93
94 gobject_class = G_OBJECT_CLASS (class);
95 widget_class = (GtkWidgetClass*) class;
96 button_class = (GtkButtonClass*) class;
97
98 gobject_class->set_property = gtk_toggle_button_set_property;
99 gobject_class->get_property = gtk_toggle_button_get_property;
100
101 widget_class->expose_event = gtk_toggle_button_expose;
102 widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
103
104 button_class->pressed = gtk_toggle_button_pressed;
105 button_class->released = gtk_toggle_button_released;
106 button_class->clicked = gtk_toggle_button_clicked;
107 button_class->enter = gtk_toggle_button_update_state;
108 button_class->leave = gtk_toggle_button_update_state;
109
110 class->toggled = NULL;
111
112 g_object_class_install_property (gobject_class,
113 PROP_ACTIVE,
114 g_param_spec_boolean ("active",
115 P_("Active"),
116 P_("If the toggle button should be pressed in or not"),
117 FALSE,
118 GTK_PARAM_READWRITE));
119
120 g_object_class_install_property (gobject_class,
121 PROP_INCONSISTENT,
122 g_param_spec_boolean ("inconsistent",
123 P_("Inconsistent"),
124 P_("If the toggle button is in an \"in between\" state"),
125 FALSE,
126 GTK_PARAM_READWRITE));
127
128 g_object_class_install_property (gobject_class,
129 PROP_DRAW_INDICATOR,
130 g_param_spec_boolean ("draw-indicator",
131 P_("Draw Indicator"),
132 P_("If the toggle part of the button is displayed"),
133 FALSE,
134 GTK_PARAM_READWRITE));
135
136 toggle_button_signals[TOGGLED] =
137 g_signal_new (I_("toggled"),
138 G_OBJECT_CLASS_TYPE (gobject_class),
139 G_SIGNAL_RUN_FIRST,
140 G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
141 NULL, NULL,
142 _gtk_marshal_VOID__VOID,
143 G_TYPE_NONE, 0);
144 }
145
146 static void
gtk_toggle_button_init(GtkToggleButton * toggle_button)147 gtk_toggle_button_init (GtkToggleButton *toggle_button)
148 {
149 toggle_button->active = FALSE;
150 toggle_button->draw_indicator = FALSE;
151 GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
152 }
153
154 static void
gtk_toggle_button_activatable_interface_init(GtkActivatableIface * iface)155 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
156 {
157 parent_activatable_iface = g_type_interface_peek_parent (iface);
158 iface->update = gtk_toggle_button_update;
159 iface->sync_action_properties = gtk_toggle_button_sync_action_properties;
160 }
161
162 static void
gtk_toggle_button_update(GtkActivatable * activatable,GtkAction * action,const gchar * property_name)163 gtk_toggle_button_update (GtkActivatable *activatable,
164 GtkAction *action,
165 const gchar *property_name)
166 {
167 GtkToggleButton *button;
168
169 parent_activatable_iface->update (activatable, action, property_name);
170
171 button = GTK_TOGGLE_BUTTON (activatable);
172
173 if (strcmp (property_name, "active") == 0)
174 {
175 gtk_action_block_activate (action);
176 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
177 gtk_action_unblock_activate (action);
178 }
179
180 }
181
182 static void
gtk_toggle_button_sync_action_properties(GtkActivatable * activatable,GtkAction * action)183 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
184 GtkAction *action)
185 {
186 GtkToggleButton *button;
187
188 parent_activatable_iface->sync_action_properties (activatable, action);
189
190 if (!GTK_IS_TOGGLE_ACTION (action))
191 return;
192
193 button = GTK_TOGGLE_BUTTON (activatable);
194
195 gtk_action_block_activate (action);
196 gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
197 gtk_action_unblock_activate (action);
198 }
199
200
201 GtkWidget*
gtk_toggle_button_new(void)202 gtk_toggle_button_new (void)
203 {
204 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
205 }
206
207 GtkWidget*
gtk_toggle_button_new_with_label(const gchar * label)208 gtk_toggle_button_new_with_label (const gchar *label)
209 {
210 return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
211 }
212
213 /**
214 * gtk_toggle_button_new_with_mnemonic:
215 * @label: the text of the button, with an underscore in front of the
216 * mnemonic character
217 * @returns: a new #GtkToggleButton
218 *
219 * Creates a new #GtkToggleButton containing a label. The label
220 * will be created using gtk_label_new_with_mnemonic(), so underscores
221 * in @label indicate the mnemonic for the button.
222 **/
223 GtkWidget*
gtk_toggle_button_new_with_mnemonic(const gchar * label)224 gtk_toggle_button_new_with_mnemonic (const gchar *label)
225 {
226 return g_object_new (GTK_TYPE_TOGGLE_BUTTON,
227 "label", label,
228 "use-underline", TRUE,
229 NULL);
230 }
231
232 static void
gtk_toggle_button_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)233 gtk_toggle_button_set_property (GObject *object,
234 guint prop_id,
235 const 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 gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
246 break;
247 case PROP_INCONSISTENT:
248 gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
249 break;
250 case PROP_DRAW_INDICATOR:
251 gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
252 break;
253 default:
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 break;
256 }
257 }
258
259 static void
gtk_toggle_button_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)260 gtk_toggle_button_get_property (GObject *object,
261 guint prop_id,
262 GValue *value,
263 GParamSpec *pspec)
264 {
265 GtkToggleButton *tb;
266
267 tb = GTK_TOGGLE_BUTTON (object);
268
269 switch (prop_id)
270 {
271 case PROP_ACTIVE:
272 g_value_set_boolean (value, tb->active);
273 break;
274 case PROP_INCONSISTENT:
275 g_value_set_boolean (value, tb->inconsistent);
276 break;
277 case PROP_DRAW_INDICATOR:
278 g_value_set_boolean (value, tb->draw_indicator);
279 break;
280 default:
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 break;
283 }
284 }
285
286 /**
287 * gtk_toggle_button_set_mode:
288 * @toggle_button: a #GtkToggleButton
289 * @draw_indicator: if %TRUE, draw the button as a separate indicator
290 * and label; if %FALSE, draw the button like a normal button
291 *
292 * Sets whether the button is displayed as a separate indicator and label.
293 * You can call this function on a checkbutton or a radiobutton with
294 * @draw_indicator = %FALSE to make the button look like a normal button
295 *
296 * This function only affects instances of classes like #GtkCheckButton
297 * and #GtkRadioButton that derive from #GtkToggleButton,
298 * not instances of #GtkToggleButton itself.
299 */
300 void
gtk_toggle_button_set_mode(GtkToggleButton * toggle_button,gboolean draw_indicator)301 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
302 gboolean draw_indicator)
303 {
304 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
305
306 draw_indicator = draw_indicator ? TRUE : FALSE;
307
308 if (toggle_button->draw_indicator != draw_indicator)
309 {
310 toggle_button->draw_indicator = draw_indicator;
311 GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
312
313 if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
314 gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
315
316 g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
317 }
318 }
319
320 /**
321 * gtk_toggle_button_get_mode:
322 * @toggle_button: a #GtkToggleButton
323 *
324 * Retrieves whether the button is displayed as a separate indicator
325 * and label. See gtk_toggle_button_set_mode().
326 *
327 * Return value: %TRUE if the togglebutton is drawn as a separate indicator
328 * and label.
329 **/
330 gboolean
gtk_toggle_button_get_mode(GtkToggleButton * toggle_button)331 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
332 {
333 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
334
335 return toggle_button->draw_indicator;
336 }
337
338 void
gtk_toggle_button_set_active(GtkToggleButton * toggle_button,gboolean is_active)339 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
340 gboolean is_active)
341 {
342 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
343
344 is_active = is_active != FALSE;
345
346 if (toggle_button->active != is_active)
347 gtk_button_clicked (GTK_BUTTON (toggle_button));
348 }
349
350
351 gboolean
gtk_toggle_button_get_active(GtkToggleButton * toggle_button)352 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
353 {
354 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
355
356 return (toggle_button->active) ? TRUE : FALSE;
357 }
358
359
360 void
gtk_toggle_button_toggled(GtkToggleButton * toggle_button)361 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
362 {
363 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
364
365 g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
366 }
367
368 /**
369 * gtk_toggle_button_set_inconsistent:
370 * @toggle_button: a #GtkToggleButton
371 * @setting: %TRUE if state is inconsistent
372 *
373 * If the user has selected a range of elements (such as some text or
374 * spreadsheet cells) that are affected by a toggle button, and the
375 * current values in that range are inconsistent, you may want to
376 * display the toggle in an "in between" state. This function turns on
377 * "in between" display. Normally you would turn off the inconsistent
378 * state again if the user toggles the toggle button. This has to be
379 * done manually, gtk_toggle_button_set_inconsistent() only affects
380 * visual appearance, it doesn't affect the semantics of the button.
381 *
382 **/
383 void
gtk_toggle_button_set_inconsistent(GtkToggleButton * toggle_button,gboolean setting)384 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
385 gboolean setting)
386 {
387 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
388
389 setting = setting != FALSE;
390
391 if (setting != toggle_button->inconsistent)
392 {
393 toggle_button->inconsistent = setting;
394
395 gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
396 gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
397
398 g_object_notify (G_OBJECT (toggle_button), "inconsistent");
399 }
400 }
401
402 /**
403 * gtk_toggle_button_get_inconsistent:
404 * @toggle_button: a #GtkToggleButton
405 *
406 * Gets the value set by gtk_toggle_button_set_inconsistent().
407 *
408 * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
409 **/
410 gboolean
gtk_toggle_button_get_inconsistent(GtkToggleButton * toggle_button)411 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
412 {
413 g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
414
415 return toggle_button->inconsistent;
416 }
417
418 static gint
gtk_toggle_button_expose(GtkWidget * widget,GdkEventExpose * event)419 gtk_toggle_button_expose (GtkWidget *widget,
420 GdkEventExpose *event)
421 {
422 if (gtk_widget_is_drawable (widget))
423 {
424 GtkWidget *child = GTK_BIN (widget)->child;
425 GtkButton *button = GTK_BUTTON (widget);
426 GtkStateType state_type;
427 GtkShadowType shadow_type;
428
429 state_type = gtk_widget_get_state (widget);
430
431 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
432 {
433 if (state_type == GTK_STATE_ACTIVE)
434 state_type = GTK_STATE_NORMAL;
435 shadow_type = GTK_SHADOW_ETCHED_IN;
436 }
437 else
438 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
439
440 _gtk_button_paint (button, &event->area, state_type, shadow_type,
441 "togglebutton", "togglebuttondefault");
442
443 if (child)
444 gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
445 }
446
447 return FALSE;
448 }
449
450 static gboolean
gtk_toggle_button_mnemonic_activate(GtkWidget * widget,gboolean group_cycling)451 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
452 gboolean group_cycling)
453 {
454 /*
455 * We override the standard implementation in
456 * gtk_widget_real_mnemonic_activate() in order to focus the widget even
457 * if there is no mnemonic conflict.
458 */
459 if (gtk_widget_get_can_focus (widget))
460 gtk_widget_grab_focus (widget);
461
462 if (!group_cycling)
463 gtk_widget_activate (widget);
464
465 return TRUE;
466 }
467
468 static void
gtk_toggle_button_pressed(GtkButton * button)469 gtk_toggle_button_pressed (GtkButton *button)
470 {
471 button->button_down = TRUE;
472
473 gtk_toggle_button_update_state (button);
474 gtk_widget_queue_draw (GTK_WIDGET (button));
475 }
476
477 static void
gtk_toggle_button_released(GtkButton * button)478 gtk_toggle_button_released (GtkButton *button)
479 {
480 if (button->button_down)
481 {
482 button->button_down = FALSE;
483
484 if (button->in_button)
485 gtk_button_clicked (button);
486
487 gtk_toggle_button_update_state (button);
488 gtk_widget_queue_draw (GTK_WIDGET (button));
489 }
490 }
491
492 static void
gtk_toggle_button_clicked(GtkButton * button)493 gtk_toggle_button_clicked (GtkButton *button)
494 {
495 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
496 toggle_button->active = !toggle_button->active;
497
498 gtk_toggle_button_toggled (toggle_button);
499
500 gtk_toggle_button_update_state (button);
501
502 g_object_notify (G_OBJECT (toggle_button), "active");
503
504 if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
505 GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
506 }
507
508 static void
gtk_toggle_button_update_state(GtkButton * button)509 gtk_toggle_button_update_state (GtkButton *button)
510 {
511 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
512 gboolean depressed, touchscreen;
513 GtkStateType new_state;
514
515 g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
516 "gtk-touchscreen-mode", &touchscreen,
517 NULL);
518
519 if (toggle_button->inconsistent)
520 depressed = FALSE;
521 else if (button->in_button && button->button_down)
522 depressed = TRUE;
523 else
524 depressed = toggle_button->active;
525
526 if (!touchscreen && button->in_button && (!button->button_down || toggle_button->draw_indicator))
527 new_state = GTK_STATE_PRELIGHT;
528 else
529 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
530
531 _gtk_button_set_depressed (button, depressed);
532 gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
533 }
534
535 #define __GTK_TOGGLE_BUTTON_C__
536 #include "gtkaliasdef.c"
537