gtkradioaction.c 16 KB
Newer Older
1
/*
Cody Russell's avatar
Cody Russell committed
2
 * GTK - The GIMP Toolkit
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 * Copyright (C) 1998, 1999 Red Hat, Inc.
 * All rights reserved.
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19
20
21
22
23
24
25
26
27
28
 */

/*
 * Author: James Henstridge <james@daa.com.au>
 *
 * Modified by the GTK+ Team and others 2003.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

29
#include "config.h"
30

31
32
#define GDK_DISABLE_DEPRECATION_WARNINGS

33
#include "gtkradioaction.h"
34
#include "gtkradiomenuitem.h"
35
#include "gtktoggletoolbutton.h"
36
#include "gtkintl.h"
37
#include "gtkprivate.h"
38
39
40
41
42
43
44
45
46
47
48
49

/**
 * SECTION:gtkradioaction
 * @Short_description: An action of which only one in a group can be active
 * @Title: GtkRadioAction
 *
 * A #GtkRadioAction is similar to #GtkRadioMenuItem. A number of radio
 * actions can be linked together so that only one may be active at any
 * one time.
 */


50
51
52
struct _GtkRadioActionPrivate 
{
  GSList *group;
53
  gint    value;
54
55
};

56
57
58
59
60
61
62
63
64
enum 
{
  CHANGED,
  LAST_SIGNAL
};

enum 
{
  PROP_0,
65
  PROP_VALUE,
66
67
  PROP_GROUP,
  PROP_CURRENT_VALUE
68
69
70
71
72
73
74
75
76
77
78
79
};

static void gtk_radio_action_finalize     (GObject *object);
static void gtk_radio_action_set_property (GObject         *object,
				           guint            prop_id,
				           const GValue    *value,
				           GParamSpec      *pspec);
static void gtk_radio_action_get_property (GObject         *object,
				           guint            prop_id,
				           GValue          *value,
				           GParamSpec      *pspec);
static void gtk_radio_action_activate     (GtkAction *action);
80
static GtkWidget *create_menu_item        (GtkAction *action);
81

82

83
G_DEFINE_TYPE_WITH_PRIVATE (GtkRadioAction, gtk_radio_action, GTK_TYPE_TOGGLE_ACTION)
84

85
static guint         radio_action_signals[LAST_SIGNAL] = { 0 };
86
87
88
89
90
91
92
93
94
95
96

static void
gtk_radio_action_class_init (GtkRadioActionClass *klass)
{
  GObjectClass *gobject_class;
  GtkActionClass *action_class;

  gobject_class = G_OBJECT_CLASS (klass);
  action_class = GTK_ACTION_CLASS (klass);

  gobject_class->finalize = gtk_radio_action_finalize;
97
98
  gobject_class->set_property = gtk_radio_action_set_property;
  gobject_class->get_property = gtk_radio_action_get_property;
99
100
101

  action_class->activate = gtk_radio_action_activate;

102
103
  action_class->create_menu_item = create_menu_item;

104
105
106
107
108
109
110
111
112
113
  /**
   * GtkRadioAction:value:
   *
   * The value is an arbitrary integer which can be used as a
   * convenient way to determine which action in the group is 
   * currently active in an ::activate or ::changed signal handler.
   * See gtk_radio_action_get_current_value() and #GtkRadioActionEntry
   * for convenient ways to get and set this property.
   *
   * Since: 2.4
114
115
   *
   * Deprecated: 3.10
116
117
118
119
   */
  g_object_class_install_property (gobject_class,
				   PROP_VALUE,
				   g_param_spec_int ("value",
120
121
						     P_("The value"),
						     P_("The value returned by gtk_radio_action_get_current_value() when this action is the current action of its group."),
122
123
124
						     G_MININT,
						     G_MAXINT,
						     0,
125
						     GTK_PARAM_READWRITE));
126

127
128
129
130
131
132
  /**
   * GtkRadioAction:group:
   *
   * Sets a new group for a radio action.
   *
   * Since: 2.4
133
134
   *
   * Deprecated: 3.10
135
136
137
138
   */
  g_object_class_install_property (gobject_class,
				   PROP_GROUP,
				   g_param_spec_object ("group",
139
							P_("Group"),
140
							P_("The radio action whose group this action belongs to."),
141
							GTK_TYPE_RADIO_ACTION,
142
							GTK_PARAM_WRITABLE));
143

144
145
146
147
148
149
150
  /**
   * GtkRadioAction:current-value:
   *
   * The value property of the currently active member of the group to which
   * this action belongs. 
   *
   * Since: 2.10
151
152
   *
   * Deprecated: 3.10
153
154
155
156
157
158
159
160
161
162
163
   */
  g_object_class_install_property (gobject_class,
				   PROP_CURRENT_VALUE,
                                   g_param_spec_int ("current-value",
						     P_("The current value"),
						     P_("The value property of the currently active member of the group to which this action belongs."),
						     G_MININT,
						     G_MAXINT,
						     0,
						     GTK_PARAM_READWRITE));

164
165
166
  /**
   * GtkRadioAction::changed:
   * @action: the action on which the signal is emitted
167
   * @current: the member of @action's group which has just been activated
168
169
170
171
172
173
   *
   * The ::changed signal is emitted on every member of a radio group when the
   * active member is changed. The signal gets emitted after the ::activate signals
   * for the previous and current active members.
   *
   * Since: 2.4
174
175
   *
   * Deprecated: 3.10
176
177
   */
  radio_action_signals[CHANGED] =
Matthias Clasen's avatar
Matthias Clasen committed
178
    g_signal_new (I_("changed"),
179
180
181
		  G_OBJECT_CLASS_TYPE (klass),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkRadioActionClass, changed),  NULL, NULL,
182
		  NULL,
183
		  G_TYPE_NONE, 1, GTK_TYPE_RADIO_ACTION);
184
185
186
187
188
}

static void
gtk_radio_action_init (GtkRadioAction *action)
{
189
  action->private_data = gtk_radio_action_get_instance_private (action);
190
  action->private_data->group = g_slist_prepend (NULL, action);
191
  action->private_data->value = 0;
Matthias Clasen's avatar
Matthias Clasen committed
192
193

  gtk_toggle_action_set_draw_as_radio (GTK_TOGGLE_ACTION (action), TRUE);
194
195
}

196
197
198
/**
 * gtk_radio_action_new:
 * @name: A unique name for the action
199
200
 * @label: (allow-none): The label displayed in menu items and on buttons,
 *   or %NULL
Johan Dahlin's avatar
Johan Dahlin committed
201
 * @tooltip: (allow-none): A tooltip for this action, or %NULL
202
203
 * @stock_id: (allow-none): The stock icon to display in widgets representing
 *   this action, or %NULL
204
205
 * @value: The value which gtk_radio_action_get_current_value() should
 *   return if this action is selected.
206
207
208
209
210
 *
 * Creates a new #GtkRadioAction object. To add the action to
 * a #GtkActionGroup and set the accelerator for the action,
 * call gtk_action_group_add_action_with_accel().
 *
211
 * Returns: a new #GtkRadioAction
212
213
 *
 * Since: 2.4
214
215
 *
 * Deprecated: 3.10
216
217
218
219
220
221
222
223
 */
GtkRadioAction *
gtk_radio_action_new (const gchar *name,
		      const gchar *label,
		      const gchar *tooltip,
		      const gchar *stock_id,
		      gint value)
{
224
225
226
  g_return_val_if_fail (name != NULL, NULL);

  return g_object_new (GTK_TYPE_RADIO_ACTION,
Matthias Clasen's avatar
Matthias Clasen committed
227
228
229
230
231
232
                       "name", name,
                       "label", label,
                       "tooltip", tooltip,
                       "stock-id", stock_id,
                       "value", value,
                       NULL);
233
234
}

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
static void
gtk_radio_action_finalize (GObject *object)
{
  GtkRadioAction *action;
  GSList *tmp_list;

  action = GTK_RADIO_ACTION (object);

  action->private_data->group = g_slist_remove (action->private_data->group, action);

  tmp_list = action->private_data->group;

  while (tmp_list)
    {
      GtkRadioAction *tmp_action = tmp_list->data;

      tmp_list = tmp_list->next;
      tmp_action->private_data->group = action->private_data->group;
    }

Matthias Clasen's avatar
Matthias Clasen committed
255
  G_OBJECT_CLASS (gtk_radio_action_parent_class)->finalize (object);
256
257
}

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
static void
gtk_radio_action_set_property (GObject         *object,
			       guint            prop_id,
			       const GValue    *value,
			       GParamSpec      *pspec)
{
  GtkRadioAction *radio_action;
  
  radio_action = GTK_RADIO_ACTION (object);

  switch (prop_id)
    {
    case PROP_VALUE:
      radio_action->private_data->value = g_value_get_int (value);
      break;
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    case PROP_GROUP: 
      {
	GtkRadioAction *arg;
	GSList *slist = NULL;
	
	if (G_VALUE_HOLDS_OBJECT (value)) 
	  {
	    arg = GTK_RADIO_ACTION (g_value_get_object (value));
	    if (arg)
	      slist = gtk_radio_action_get_group (arg);
	    gtk_radio_action_set_group (radio_action, slist);
	  }
      }
      break;
287
288
289
290
    case PROP_CURRENT_VALUE:
      gtk_radio_action_set_current_value (radio_action,
                                          g_value_get_int (value));
      break;
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_radio_action_get_property (GObject    *object,
			       guint       prop_id,
			       GValue     *value,
			       GParamSpec *pspec)
{
  GtkRadioAction *radio_action;

  radio_action = GTK_RADIO_ACTION (object);

  switch (prop_id)
    {
    case PROP_VALUE:
      g_value_set_int (value, radio_action->private_data->value);
      break;
312
313
314
315
    case PROP_CURRENT_VALUE:
      g_value_set_int (value,
                       gtk_radio_action_get_current_value (radio_action));
      break;
316
317
318
319
320
321
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

322
323
324
325
326
327
328
static void
gtk_radio_action_activate (GtkAction *action)
{
  GtkRadioAction *radio_action;
  GtkToggleAction *toggle_action;
  GtkToggleAction *tmp_action;
  GSList *tmp_list;
329
  gboolean active;
330
331
332
333

  radio_action = GTK_RADIO_ACTION (action);
  toggle_action = GTK_TOGGLE_ACTION (action);

334
335
  active = gtk_toggle_action_get_active (toggle_action);
  if (active)
336
337
338
339
340
341
342
343
    {
      tmp_list = radio_action->private_data->group;

      while (tmp_list)
	{
	  tmp_action = tmp_list->data;
	  tmp_list = tmp_list->next;

344
345
	  if (gtk_toggle_action_get_active (tmp_action) &&
              (tmp_action != toggle_action))
346
	    {
347
              _gtk_toggle_action_set_active (toggle_action, !active);
348

349
350
351
	      break;
	    }
	}
352
      g_object_notify (G_OBJECT (action), "active");
353
354
355
    }
  else
    {
356
      _gtk_toggle_action_set_active (toggle_action, !active);
357
      g_object_notify (G_OBJECT (action), "active");
358
359
360
361
362
363
364

      tmp_list = radio_action->private_data->group;
      while (tmp_list)
	{
	  tmp_action = tmp_list->data;
	  tmp_list = tmp_list->next;

365
366
          if (gtk_toggle_action_get_active (tmp_action) &&
              (tmp_action != toggle_action))
367
	    {
368
	      _gtk_action_emit_activate (GTK_ACTION (tmp_action));
369
370
371
	      break;
	    }
	}
372
373
374
375
376
377
378

      tmp_list = radio_action->private_data->group;
      while (tmp_list)
	{
	  tmp_action = tmp_list->data;
	  tmp_list = tmp_list->next;
	  
379
380
          g_object_notify (G_OBJECT (tmp_action), "current-value");

381
382
	  g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
	}
383
384
385
386
387
    }

  gtk_toggle_action_toggled (toggle_action);
}

388
389
390
391
static GtkWidget *
create_menu_item (GtkAction *action)
{
  return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
392
		       "draw-as-radio", TRUE,
393
394
395
		       NULL);
}

396
397
398
399
/**
 * gtk_radio_action_get_group:
 * @action: the action object
 *
400
401
402
403
404
 * Returns the list representing the radio group for this object.
 * Note that the returned list is only valid until the next change
 * to the group. 
 *
 * A common way to set up a group of radio group is the following:
405
 * |[<!-- language="C" -->
406
407
408
 *   GSList *group = NULL;
 *   GtkRadioAction *action;
 *  
409
 *   while ( ...more actions to add... /)
410
411
 *     {
 *        action = gtk_radio_action_new (...);
412
 *        
413
414
415
 *        gtk_radio_action_set_group (action, group);
 *        group = gtk_radio_action_get_group (action);
 *     }
Matthias Clasen's avatar
Matthias Clasen committed
416
 * ]|
417
 *
418
 * Returns:  (element-type GtkRadioAction) (transfer none): the list representing the radio group for this object
419
420
 *
 * Since: 2.4
421
422
 *
 * Deprecated: 3.10
423
424
425
426
427
428
429
430
431
432
433
434
 */
GSList *
gtk_radio_action_get_group (GtkRadioAction *action)
{
  g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);

  return action->private_data->group;
}

/**
 * gtk_radio_action_set_group:
 * @action: the action object
435
 * @group: (element-type GtkRadioAction) (allow-none): a list representing a radio group, or %NULL
436
437
438
439
 *
 * Sets the radio group for the radio action object.
 *
 * Since: 2.4
440
441
 *
 * Deprecated: 3.10
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
 */
void
gtk_radio_action_set_group (GtkRadioAction *action, 
			    GSList         *group)
{
  g_return_if_fail (GTK_IS_RADIO_ACTION (action));
  g_return_if_fail (!g_slist_find (group, action));

  if (action->private_data->group)
    {
      GSList *slist;

      action->private_data->group = g_slist_remove (action->private_data->group, action);

      for (slist = action->private_data->group; slist; slist = slist->next)
	{
	  GtkRadioAction *tmp_action = slist->data;

	  tmp_action->private_data->group = action->private_data->group;
	}
    }

  action->private_data->group = g_slist_prepend (group, action);

  if (group)
    {
      GSList *slist;

      for (slist = action->private_data->group; slist; slist = slist->next)
	{
	  GtkRadioAction *tmp_action = slist->data;

	  tmp_action->private_data->group = action->private_data->group;
	}
    }
  else
    {
      gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
    }
}
482

483
484
485
486
487
488
489
490
491
492
493
494
/**
 * gtk_radio_action_join_group:
 * @action: the action object
 * @group_source: (allow-none): a radio action object whos group we are 
 *   joining, or %NULL to remove the radio action from its group
 *
 * Joins a radio action object to the group of another radio action object.
 *
 * Use this in language bindings instead of the gtk_radio_action_get_group() 
 * and gtk_radio_action_set_group() methods
 *
 * A common way to set up a group of radio actions is the following:
495
 * |[<!-- language="C" -->
496
497
498
 *   GtkRadioAction *action;
 *   GtkRadioAction *last_action;
 *  
499
 *   while ( ...more actions to add... /)
500
501
502
503
504
505
506
507
508
 *     {
 *        action = gtk_radio_action_new (...);
 *        
 *        gtk_radio_action_join_group (action, last_action);
 *        last_action = action;
 *     }
 * ]|
 * 
 * Since: 3.0
509
510
 *
 * Deprecated: 3.10
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
 */
void
gtk_radio_action_join_group (GtkRadioAction *action, 
			     GtkRadioAction *group_source)
{
  g_return_if_fail (GTK_IS_RADIO_ACTION (action));
  g_return_if_fail (group_source == NULL || GTK_IS_RADIO_ACTION (group_source));  

  if (group_source)
    {
      GSList *group;
      group = gtk_radio_action_get_group (group_source);
      
      if (!group)
        {
          /* if we are not already part of a group we need to set up a new one
             and then get the newly created group */  
          gtk_radio_action_set_group (group_source, NULL);
          group = gtk_radio_action_get_group (group_source);
        }

      gtk_radio_action_set_group (action, group);
    }
  else
    {
      gtk_radio_action_set_group (action, NULL);
    }
}

540
541
542
543
/**
 * gtk_radio_action_get_current_value:
 * @action: a #GtkRadioAction
 * 
544
 * Obtains the value property of the currently active member of 
545
546
 * the group to which @action belongs.
 * 
547
 * Returns: The value of the currently active group member
548
549
 *
 * Since: 2.4
550
551
 *
 * Deprecated: 3.10
552
553
554
555
556
557
558
559
560
561
562
563
564
565
 **/
gint
gtk_radio_action_get_current_value (GtkRadioAction *action)
{
  GSList *slist;

  g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);

  if (action->private_data->group)
    {
      for (slist = action->private_data->group; slist; slist = slist->next)
	{
	  GtkToggleAction *toggle_action = slist->data;

566
	  if (gtk_toggle_action_get_active (toggle_action))
567
568
569
570
571
572
	    return GTK_RADIO_ACTION (toggle_action)->private_data->value;
	}
    }

  return action->private_data->value;
}
573

574
575
576
577
578
579
580
581
582
/**
 * gtk_radio_action_set_current_value:
 * @action: a #GtkRadioAction
 * @current_value: the new value
 * 
 * Sets the currently active group member to the member with value
 * property @current_value.
 *
 * Since: 2.10
583
584
 *
 * Deprecated: 3.10
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
 **/
void
gtk_radio_action_set_current_value (GtkRadioAction *action,
                                    gint            current_value)
{
  GSList *slist;

  g_return_if_fail (GTK_IS_RADIO_ACTION (action));

  if (action->private_data->group)
    {
      for (slist = action->private_data->group; slist; slist = slist->next)
	{
	  GtkRadioAction *radio_action = slist->data;

	  if (radio_action->private_data->value == current_value)
            {
              gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action),
                                            TRUE);
              return;
            }
	}
    }

  if (action->private_data->value == current_value)
    gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
  else
    g_warning ("Radio group does not contain an action with value '%d'",
	       current_value);
}