gtkradiomenuitem.c 12.9 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * 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/. 
 */

27
#include <config.h>
Tim Janik's avatar
Tim Janik committed
28
#include "gtkaccellabel.h"
29
#include "gtkmarshalers.h"
Elliot Lee's avatar
Elliot Lee committed
30
#include "gtkradiomenuitem.h"
31 32
#include "gtkprivate.h"
#include "gtkintl.h"
33
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
34 35


36 37 38 39 40 41
enum {
  PROP_0,
  PROP_GROUP
};


42
static void gtk_radio_menu_item_destroy        (GtkObject             *object);
Elliot Lee's avatar
Elliot Lee committed
43
static void gtk_radio_menu_item_activate       (GtkMenuItem           *menu_item);
44 45 46 47 48 49 50 51
static void gtk_radio_menu_item_set_property   (GObject               *object,
						guint                  prop_id,
						const GValue          *value,
						GParamSpec            *pspec);
static void gtk_radio_menu_item_get_property   (GObject               *object,
						guint                  prop_id,
						GValue                *value,
						GParamSpec            *pspec);
Elliot Lee's avatar
Elliot Lee committed
52

53 54
static guint group_changed_signal = 0;

Matthias Clasen's avatar
Matthias Clasen committed
55
G_DEFINE_TYPE (GtkRadioMenuItem, gtk_radio_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
Elliot Lee's avatar
Elliot Lee committed
56 57 58 59 60 61

GtkWidget*
gtk_radio_menu_item_new (GSList *group)
{
  GtkRadioMenuItem *radio_menu_item;

Manish Singh's avatar
Manish Singh committed
62
  radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
Elliot Lee's avatar
Elliot Lee committed
63

64 65 66 67
  gtk_radio_menu_item_set_group (radio_menu_item, group);

  return GTK_WIDGET (radio_menu_item);
}
Elliot Lee's avatar
Elliot Lee committed
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static void
gtk_radio_menu_item_set_property (GObject      *object,
				  guint         prop_id,
				  const GValue *value,
				  GParamSpec   *pspec)
{
  GtkRadioMenuItem *radio_menu_item;

  radio_menu_item = GTK_RADIO_MENU_ITEM (object);

  switch (prop_id)
    {
      GSList *slist;

    case PROP_GROUP:
      if (G_VALUE_HOLDS_OBJECT (value))
	slist = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
      else
	slist = NULL;
      gtk_radio_menu_item_set_group (radio_menu_item, slist);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_radio_menu_item_get_property (GObject    *object,
				  guint       prop_id,
				  GValue     *value,
				  GParamSpec *pspec)
{
  switch (prop_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

110 111 112 113
void
gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
			       GSList           *group)
{
114 115 116
  GtkWidget *old_group_singleton = NULL;
  GtkWidget *new_group_singleton = NULL;
  
117 118
  g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
  g_return_if_fail (!g_slist_find (group, radio_menu_item));
119

120
  if (radio_menu_item->group)
Elliot Lee's avatar
Elliot Lee committed
121
    {
122
      GSList *slist;
123

124 125
      radio_menu_item->group = g_slist_remove (radio_menu_item->group, radio_menu_item);
      
126 127 128
      if (radio_menu_item->group && !radio_menu_item->group->next)
	old_group_singleton = g_object_ref (radio_menu_item->group->data);
	  
129
      for (slist = radio_menu_item->group; slist; slist = slist->next)
Elliot Lee's avatar
Elliot Lee committed
130
	{
131 132 133 134 135 136 137 138
	  GtkRadioMenuItem *tmp_item;
	  
	  tmp_item = slist->data;
	  
	  tmp_item->group = radio_menu_item->group;
	}
    }
  
139 140 141
  if (group && !group->next)
    new_group_singleton = g_object_ref (group->data);
  
142 143 144 145 146 147 148 149 150 151 152 153 154
  radio_menu_item->group = g_slist_prepend (group, radio_menu_item);
  
  if (group)
    {
      GSList *slist;
      
      for (slist = group; slist; slist = slist->next)
	{
	  GtkRadioMenuItem *tmp_item;
	  
	  tmp_item = slist->data;
	  
	  tmp_item->group = radio_menu_item->group;
Elliot Lee's avatar
Elliot Lee committed
155 156 157 158 159
	}
    }
  else
    {
      GTK_CHECK_MENU_ITEM (radio_menu_item)->active = TRUE;
160 161
      /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
       */
Elliot Lee's avatar
Elliot Lee committed
162
    }
163 164 165

  g_object_ref (radio_menu_item);

166
  g_object_notify (G_OBJECT (radio_menu_item), "group");
167 168 169 170 171 172 173 174 175 176 177 178 179
  g_signal_emit (radio_menu_item, group_changed_signal, 0);
  if (old_group_singleton)
    {
      g_signal_emit (old_group_singleton, group_changed_signal, 0);
      g_object_unref (old_group_singleton);
    }
  if (new_group_singleton)
    {
      g_signal_emit (new_group_singleton, group_changed_signal, 0);
      g_object_unref (new_group_singleton);
    }

  g_object_unref (radio_menu_item);
Elliot Lee's avatar
Elliot Lee committed
180 181 182 183 184 185 186
}

GtkWidget*
gtk_radio_menu_item_new_with_label (GSList *group,
				    const gchar *label)
{
  GtkWidget *radio_menu_item;
Tim Janik's avatar
Tim Janik committed
187
  GtkWidget *accel_label;
Elliot Lee's avatar
Elliot Lee committed
188 189

  radio_menu_item = gtk_radio_menu_item_new (group);
Tim Janik's avatar
Tim Janik committed
190 191 192 193 194
  accel_label = gtk_accel_label_new (label);
  gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
  gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
  gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
  gtk_widget_show (accel_label);
Elliot Lee's avatar
Elliot Lee committed
195 196 197 198

  return radio_menu_item;
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

/**
 * gtk_radio_menu_item_new_with_mnemonic:
 * @group: group the radio menu item is inside
 * @label: the text of the button, with an underscore in front of the
 *         mnemonic character
 * @returns: a new #GtkRadioMenuItem
 *
 * Creates a new #GtkRadioMenuItem containing a label. The label
 * will be created using gtk_label_new_with_mnemonic(), so underscores
 * in @label indicate the mnemonic for the menu item.
 **/
GtkWidget*
gtk_radio_menu_item_new_with_mnemonic (GSList *group,
				       const gchar *label)
{
  GtkWidget *radio_menu_item;
  GtkWidget *accel_label;

  radio_menu_item = gtk_radio_menu_item_new (group);
Manish Singh's avatar
Manish Singh committed
219
  accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
220 221 222 223 224 225 226 227 228 229
  gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
  gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);

  gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
  gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
  gtk_widget_show (accel_label);

  return radio_menu_item;
}

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
/**
 * gtk_radio_menu_item_new_from_widget:
 * @group: An existing #GtkRadioMenuItem
 * 
 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
 * 
 * Return value: The new #GtkRadioMenuItem
 * 
 * Since: 2.4
 **/
GtkWidget *
gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
{
  GSList *list = NULL;
  
  g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);

  if (group)
    list = gtk_radio_menu_item_get_group (group);
  
  return gtk_radio_menu_item_new (list);
}

/**
 * gtk_radio_menu_item_new_with_mnemonic_from_widget:
 * @group: An existing #GtkRadioMenuItem
 * @label: the text of the button, with an underscore in front of the
 *         mnemonic character
 *
 * Creates a new GtkRadioMenuItem containing a label. The label will be
 * created using gtk_label_new_with_mnemonic(), so underscores in label
 * indicate the mnemonic for the menu item.
 *
 * The new #GtkRadioMenuItem is added to the same group as @group.
 *
 * Return value: The new #GtkRadioMenuItem
 * 
 * Since: 2.4
 **/
GtkWidget *
gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
						   const gchar *label)
{
  GSList *list = NULL;

  g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);

  if (group)
    list = gtk_radio_menu_item_get_group (group);

  return gtk_radio_menu_item_new_with_mnemonic (list, label);
}

/**
 * gtk_radio_menu_item_new_with_label_from_widget:
 * @group: an existing #GtkRadioMenuItem 
 * @label: the text for the label
 *
 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
 * The new #GtkRadioMenuItem is added to the same group as @group.
 *
 * Return value: The new #GtkRadioMenuItem
 * 
 * Since: 2.4
 **/
GtkWidget *
gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
						const gchar *label)
{
  GSList *list = NULL;

  g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);

  if (group)
    list = gtk_radio_menu_item_get_group (group);

  return gtk_radio_menu_item_new_with_label (list, label);
}

Elliot Lee's avatar
Elliot Lee committed
309
GSList*
Owen Taylor's avatar
Owen Taylor committed
310
gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
Elliot Lee's avatar
Elliot Lee committed
311 312 313 314 315 316 317 318 319 320
{
  g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);

  return radio_menu_item->group;
}


static void
gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
{
321
  GObjectClass *gobject_class;  
322
  GtkObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
323 324
  GtkMenuItemClass *menu_item_class;

325 326 327
  gobject_class = G_OBJECT_CLASS (klass);
  object_class = GTK_OBJECT_CLASS (klass);
  menu_item_class = GTK_MENU_ITEM_CLASS (klass);
Elliot Lee's avatar
Elliot Lee committed
328

329 330 331
  gobject_class->set_property = gtk_radio_menu_item_set_property;
  gobject_class->get_property = gtk_radio_menu_item_get_property;

Matthias Clasen's avatar
Matthias Clasen committed
332 333 334 335 336 337 338
  /**
   * GtkRadioMenuItem:group:
   * 
   * The radio menu item whose group this widget belongs to.
   * 
   * Since: 2.8
   */
339 340 341 342
  g_object_class_install_property (gobject_class,
				   PROP_GROUP,
				   g_param_spec_object ("group",
							P_("Group"),
Matthias Clasen's avatar
Matthias Clasen committed
343
							P_("The radio menu item whose group this widget belongs to."),
344 345 346
							GTK_TYPE_RADIO_MENU_ITEM,
							GTK_PARAM_WRITABLE));

347 348
  object_class->destroy = gtk_radio_menu_item_destroy;

Elliot Lee's avatar
Elliot Lee committed
349
  menu_item_class->activate = gtk_radio_menu_item_activate;
350 351 352 353 354 355 356 357

  /**
   * GtkStyle::group-changed:
   * @style: the object which received the signal
   *
   * Emitted when the group of radio menu items that a radio menu item belongs
   * to changes. This is emitted when a radio menu item switches from
   * being alone to being part of a group of 2 or more menu items, or
Matthias Clasen's avatar
Matthias Clasen committed
358
   * vice-versa, and when a button is moved from one group of 2 or
359
   * more menu items ton a different one, but not when the composition
360
   * of the group that a menu item belongs to changes.
361 362
   *
   * Since: 2.4
363
   */
364
  group_changed_signal = g_signal_new (I_("group-changed"),
365 366 367 368 369 370
				       G_OBJECT_CLASS_TYPE (object_class),
				       G_SIGNAL_RUN_FIRST,
				       G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
				       NULL, NULL,
				       _gtk_marshal_VOID__VOID,
				       G_TYPE_NONE, 0);
Elliot Lee's avatar
Elliot Lee committed
371 372 373 374 375
}

static void
gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
{
376
  radio_menu_item->group = g_slist_prepend (NULL, radio_menu_item);
377
  gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
Elliot Lee's avatar
Elliot Lee committed
378 379
}

380 381 382
static void
gtk_radio_menu_item_destroy (GtkObject *object)
{
383
  GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (object);
384
  GtkWidget *old_group_singleton = NULL;
385 386
  GtkRadioMenuItem *tmp_menu_item;
  GSList *tmp_list;
387
  gboolean was_in_group;
388

389 390
  was_in_group = radio_menu_item->group && radio_menu_item->group->next;
  
391 392
  radio_menu_item->group = g_slist_remove (radio_menu_item->group,
					   radio_menu_item);
393 394 395
  if (radio_menu_item->group && !radio_menu_item->group->next)
    old_group_singleton = radio_menu_item->group->data;

396 397 398 399 400 401 402 403 404 405
  tmp_list = radio_menu_item->group;

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

      tmp_menu_item->group = radio_menu_item->group;
    }

406 407 408
  /* this radio menu item is no longer in the group */
  radio_menu_item->group = NULL;
  
409 410 411 412 413
  if (old_group_singleton)
    g_signal_emit (old_group_singleton, group_changed_signal, 0);
  if (was_in_group)
    g_signal_emit (radio_menu_item, group_changed_signal, 0);
  
Matthias Clasen's avatar
Matthias Clasen committed
414 415
  if (GTK_OBJECT_CLASS (gtk_radio_menu_item_parent_class)->destroy)
    (* GTK_OBJECT_CLASS (gtk_radio_menu_item_parent_class)->destroy) (object);
416 417
}

Elliot Lee's avatar
Elliot Lee committed
418 419 420
static void
gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
{
421 422
  GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
  GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
Elliot Lee's avatar
Elliot Lee committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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
  GtkCheckMenuItem *tmp_menu_item;
  GSList *tmp_list;
  gint toggled;

  toggled = FALSE;

  if (check_menu_item->active)
    {
      tmp_menu_item = NULL;
      tmp_list = radio_menu_item->group;

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

	  if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
	    break;

	  tmp_menu_item = NULL;
	}

      if (tmp_menu_item)
	{
	  toggled = TRUE;
	  check_menu_item->active = !check_menu_item->active;
	}
    }
  else
    {
      toggled = TRUE;
      check_menu_item->active = !check_menu_item->active;

      tmp_list = radio_menu_item->group;
      while (tmp_list)
	{
	  tmp_menu_item = tmp_list->data;
	  tmp_list = tmp_list->next;

	  if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
	    {
	      gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
	      break;
	    }
	}
    }

  if (toggled)
    gtk_check_menu_item_toggled (check_menu_item);
472

473
  gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));
Elliot Lee's avatar
Elliot Lee committed
474
}
475 476 477

#define __GTK_RADIO_MENU_ITEM_C__
#include "gtkaliasdef.c"