gtktoggletoolbutton.c 13.5 KB
Newer Older
1
 /* gtktoggletoolbutton.c
2
 *
3
 * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
4 5 6 7 8 9 10 11 12 13 14 15 16
 * Copyright (C) 2002 James Henstridge <james@daa.com.au>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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
#include "config.h"
21 22 23 24 25 26
#include "gtktoggletoolbutton.h"
#include "gtkcheckmenuitem.h"
#include "gtklabel.h"
#include "gtktogglebutton.h"
#include "gtkstock.h"
#include "gtkintl.h"
27
#include "gtkradiotoolbutton.h"
28 29
#include "gtktoggleaction.h"
#include "gtkactivatable.h"
30
#include "gtkprivate.h"
31

32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * SECTION:gtktoggletoolbutton
 * @Short_description: A GtkToolItem containing a toggle button
 * @Title: GtkToggleToolButton
 * @See_also: #GtkToolbar, #GtkToolButton, #GtkSeparatorToolItem
 *
 * A #GtkToggleToolButton is a #GtkToolItem that contains a toggle
 * button.
 *
 * Use gtk_toggle_tool_button_new() to create a new
 * #GtkToggleToolButton. Use gtk_toggle_tool_button_new_from_stock() to
 * create a new #GtkToggleToolButton containing a stock item.
 */


48 49 50 51 52 53 54
#define MENU_ID "gtk-toggle-tool-button-menu-id"

enum {
  TOGGLED,
  LAST_SIGNAL
};

Matthias Clasen's avatar
Matthias Clasen committed
55 56 57 58 59 60
enum {
  PROP_0,
  PROP_ACTIVE
};


61 62 63 64 65
struct _GtkToggleToolButtonPrivate
{
  guint active : 1;
};
  
66

Matthias Clasen's avatar
Matthias Clasen committed
67 68 69 70 71 72 73 74 75
static void     gtk_toggle_tool_button_set_property        (GObject      *object,
							    guint         prop_id,
							    const GValue *value,
							    GParamSpec   *pspec);
static void     gtk_toggle_tool_button_get_property        (GObject      *object,
							    guint         prop_id,
							    GValue       *value,
							    GParamSpec   *pspec);

76 77 78 79 80 81 82 83
static gboolean gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *button);

static void button_toggled      (GtkWidget           *widget,
				 GtkToggleToolButton *button);
static void menu_item_activated (GtkWidget           *widget,
				 GtkToggleToolButton *button);


84
static void gtk_toggle_tool_button_activatable_interface_init (GtkActivatableIface  *iface);
85
static void gtk_toggle_tool_button_update                     (GtkActivatable       *activatable,
86 87
							       GtkAction            *action,
							       const gchar          *property_name);
88
static void gtk_toggle_tool_button_sync_action_properties     (GtkActivatable       *activatable,
89 90 91 92 93 94 95 96
							       GtkAction            *action);

static GtkActivatableIface *parent_activatable_iface;
static guint                toggle_signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE_WITH_CODE (GtkToggleToolButton, gtk_toggle_tool_button, GTK_TYPE_TOOL_BUTTON,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
						gtk_toggle_tool_button_activatable_interface_init))
97 98 99 100 101 102 103 104 105 106 107 108

static void
gtk_toggle_tool_button_class_init (GtkToggleToolButtonClass *klass)
{
  GObjectClass *object_class;
  GtkToolItemClass *toolitem_class;
  GtkToolButtonClass *toolbutton_class;

  object_class = (GObjectClass *)klass;
  toolitem_class = (GtkToolItemClass *)klass;
  toolbutton_class = (GtkToolButtonClass *)klass;

Matthias Clasen's avatar
Matthias Clasen committed
109 110 111
  object_class->set_property = gtk_toggle_tool_button_set_property;
  object_class->get_property = gtk_toggle_tool_button_get_property;

112 113
  toolitem_class->create_menu_proxy = gtk_toggle_tool_button_create_menu_proxy;
  toolbutton_class->button_type = GTK_TYPE_TOGGLE_BUTTON;
Matthias Clasen's avatar
Matthias Clasen committed
114 115 116 117

  /**
   * GtkToggleToolButton:active:
   *
118
   * If the toggle tool button should be pressed in.
Matthias Clasen's avatar
Matthias Clasen committed
119 120 121
   *
   * Since: 2.8
   */
122
  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
123 124 125
                                   PROP_ACTIVE,
                                   g_param_spec_boolean ("active",
							 P_("Active"),
126
							 P_("If the toggle button should be pressed in"),
Matthias Clasen's avatar
Matthias Clasen committed
127 128 129
							 FALSE,
							 GTK_PARAM_READWRITE));

130 131 132 133 134 135
/**
 * GtkToggleToolButton::toggled:
 * @toggle_tool_button: the object that emitted the signal
 *
 * Emitted whenever the toggle tool button changes state.
 **/
136
  toggle_signals[TOGGLED] =
137
    g_signal_new (I_("toggled"),
138 139 140 141 142 143
		  G_OBJECT_CLASS_TYPE (klass),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkToggleToolButtonClass, toggled),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
144 145

  g_type_class_add_private (object_class, sizeof (GtkToggleToolButtonPrivate));
146 147 148 149 150
}

static void
gtk_toggle_tool_button_init (GtkToggleToolButton *button)
{
151 152 153
  GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
  GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button));

154 155 156
  button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
                                              GTK_TYPE_TOGGLE_TOOL_BUTTON,
                                              GtkToggleToolButtonPrivate);
157 158 159 160 161 162 163

  /* If the real button is a radio button, it may have been
   * active at the time it was created.
   */
  button->priv->active = gtk_toggle_button_get_active (toggle_button);
    
  g_signal_connect_object (toggle_button,
164
			   "toggled", G_CALLBACK (button_toggled), button, 0);
165 166
}

Matthias Clasen's avatar
Matthias Clasen committed
167 168 169 170 171 172 173 174 175 176 177
static void
gtk_toggle_tool_button_set_property (GObject      *object,
				     guint         prop_id,
				     const GValue *value,
				     GParamSpec   *pspec)
{
  GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);

  switch (prop_id)
    {
      case PROP_ACTIVE:
178 179
	gtk_toggle_tool_button_set_active (button, 
					   g_value_get_boolean (value));
Matthias Clasen's avatar
Matthias Clasen committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gtk_toggle_tool_button_get_property (GObject    *object,
				     guint       prop_id,
				     GValue     *value,
				     GParamSpec *pspec)
{
  GtkToggleToolButton *button = GTK_TOGGLE_TOOL_BUTTON (object);

  switch (prop_id)
    {
      case PROP_ACTIVE:
        g_value_set_boolean (value, gtk_toggle_tool_button_get_active (button));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

208 209 210 211 212 213 214 215
static gboolean
gtk_toggle_tool_button_create_menu_proxy (GtkToolItem *item)
{
  GtkToolButton *tool_button = GTK_TOOL_BUTTON (item);
  GtkToggleToolButton *toggle_tool_button = GTK_TOGGLE_TOOL_BUTTON (item);
  GtkWidget *menu_item = NULL;
  GtkStockItem stock_item;
  gboolean use_mnemonic = TRUE;
216
  const char *label;
217 218 219
  GtkWidget *label_widget;
  const gchar *label_text;
  const gchar *stock_id;
220

221 222 223 224 225 226
  if (_gtk_tool_item_create_menu_proxy (item))
    return TRUE;

  label_widget = gtk_tool_button_get_label_widget (tool_button);
  label_text = gtk_tool_button_get_label (tool_button);
  stock_id = gtk_tool_button_get_stock_id (tool_button);
227

228
  if (GTK_IS_LABEL (label_widget))
229 230
    {
      label = gtk_label_get_label (GTK_LABEL (label_widget));
231
      use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (label_widget));
232 233
    }
  else if (label_text)
234
    {
235
      label = label_text;
236
      use_mnemonic = gtk_tool_button_get_use_underline (tool_button);
237
    }
238
  else if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
239 240 241 242 243 244 245
    {
      label = stock_item.label;
    }
  else
    {
      label = "";
    }
246 247 248 249 250 251 252
  
  if (use_mnemonic)
    menu_item = gtk_check_menu_item_new_with_mnemonic (label);
  else
    menu_item = gtk_check_menu_item_new_with_label (label);

  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
253
				  toggle_tool_button->priv->active);
254

255 256 257 258 259 260
  if (GTK_IS_RADIO_TOOL_BUTTON (toggle_tool_button))
    {
      gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (menu_item),
					     TRUE);
    }

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
  g_signal_connect_closure_by_id (menu_item,
				  g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
				  g_cclosure_new_object (G_CALLBACK (menu_item_activated),
							 G_OBJECT (toggle_tool_button)),
				  FALSE);

  gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
  
  return TRUE;
}

/* There are two activatable widgets, a toggle button and a menu item.
 *
 * If a widget is activated and the state of the tool button is the same as
 * the new state of the activated widget, then the other widget was the one
 * that was activated by the user and updated the tool button's state.
 *
 * If the state of the tool button is not the same as the new state of the
 * activated widget, then the activation was activated by the user, and the
 * widget needs to make sure the tool button is updated before the other
 * widget is activated. This will make sure the other widget a tool button
 * in a state that matches its own new state.
 */
static void
menu_item_activated (GtkWidget           *menu_item,
		     GtkToggleToolButton *toggle_tool_button)
{
  GtkToolButton *tool_button = GTK_TOOL_BUTTON (toggle_tool_button);
  gboolean menu_active = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menu_item));

291
  if (toggle_tool_button->priv->active != menu_active)
292
    {
293
      toggle_tool_button->priv->active = menu_active;
294

295 296
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)),
				    toggle_tool_button->priv->active);
297

Matthias Clasen's avatar
Matthias Clasen committed
298
      g_object_notify (G_OBJECT (toggle_tool_button), "active");
299
      g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
300 301 302 303 304 305 306
    }
}

static void
button_toggled (GtkWidget           *widget,
		GtkToggleToolButton *toggle_tool_button)
{
307 308 309
  gboolean toggle_active;

  toggle_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
310

311
  if (toggle_tool_button->priv->active != toggle_active)
312 313 314
    {
      GtkWidget *menu_item;
      
315
      toggle_tool_button->priv->active = toggle_active;
316 317 318 319 320
       
      if ((menu_item =
	   gtk_tool_item_get_proxy_menu_item (GTK_TOOL_ITEM (toggle_tool_button), MENU_ID)))
	{
	  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item),
321
					  toggle_tool_button->priv->active);
322 323
	}

Matthias Clasen's avatar
Matthias Clasen committed
324
      g_object_notify (G_OBJECT (toggle_tool_button), "active");
325
      g_signal_emit (toggle_tool_button, toggle_signals[TOGGLED], 0);
326 327 328
    }
}

329 330
static void
gtk_toggle_tool_button_activatable_interface_init (GtkActivatableIface *iface)
331 332
{
  parent_activatable_iface = g_type_interface_peek_parent (iface);
333 334
  iface->update = gtk_toggle_tool_button_update;
  iface->sync_action_properties = gtk_toggle_tool_button_sync_action_properties;
335 336 337
}

static void
338 339 340
gtk_toggle_tool_button_update (GtkActivatable *activatable,
			       GtkAction      *action,
			       const gchar    *property_name)
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
{
  GtkToggleToolButton *button;

  parent_activatable_iface->update (activatable, action, property_name);

  button = GTK_TOGGLE_TOOL_BUTTON (activatable);

  if (strcmp (property_name, "active") == 0)
    {
      gtk_action_block_activate (action);
      gtk_toggle_tool_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
      gtk_action_unblock_activate (action);
    }
}

static void
357 358
gtk_toggle_tool_button_sync_action_properties (GtkActivatable *activatable,
					       GtkAction      *action)
359 360 361
{
  GtkToggleToolButton *button;

362
  parent_activatable_iface->sync_action_properties (activatable, action);
363

364
  if (!GTK_IS_TOGGLE_ACTION (action))
365 366 367 368 369 370 371 372 373 374
    return;

  button = GTK_TOGGLE_TOOL_BUTTON (activatable);

  gtk_action_block_activate (action);
  gtk_toggle_tool_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
  gtk_action_unblock_activate (action);
}


375 376 377 378 379 380 381 382 383
/**
 * gtk_toggle_tool_button_new:
 * 
 * Returns a new #GtkToggleToolButton
 * 
 * Return value: a newly created #GtkToggleToolButton
 * 
 * Since: 2.4
 **/
384 385 386 387 388 389 390 391 392 393 394
GtkToolItem *
gtk_toggle_tool_button_new (void)
{
  GtkToolButton *button;

  button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
			 NULL);
  
  return GTK_TOOL_ITEM (button);
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408
/**
 * gtk_toggle_tool_button_new_from_stock:
 * @stock_id: the name of the stock item 
 *
 * Creates a new #GtkToggleToolButton containing the image and text from a
 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
 * and #GTK_STOCK_APPLY.
 *
 * It is an error if @stock_id is not a name of a stock item.
 * 
 * Return value: A new #GtkToggleToolButton
 * 
 * Since: 2.4
 **/
409 410 411 412 413 414 415 416
GtkToolItem *
gtk_toggle_tool_button_new_from_stock (const gchar *stock_id)
{
  GtkToolButton *button;

  g_return_val_if_fail (stock_id != NULL, NULL);
  
  button = g_object_new (GTK_TYPE_TOGGLE_TOOL_BUTTON,
417
			 "stock-id", stock_id,
418 419 420 421 422
			 NULL);
  
  return GTK_TOOL_ITEM (button);
}

423 424 425 426 427 428 429 430 431 432 433
/**
 * gtk_toggle_tool_button_set_active:
 * @button: a #GtkToggleToolButton
 * @is_active: whether @button should be active
 * 
 * Sets the status of the toggle tool button. Set to %TRUE if you
 * want the GtkToggleButton to be 'pressed in', and %FALSE to raise it.
 * This action causes the toggled signal to be emitted.
 * 
 * Since: 2.4
 **/
434 435 436 437 438 439 440 441
void
gtk_toggle_tool_button_set_active (GtkToggleToolButton *button,
				   gboolean is_active)
{
  g_return_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button));

  is_active = is_active != FALSE;

442 443
  if (button->priv->active != is_active)
    gtk_button_clicked (GTK_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button))));
444 445
}

446 447 448 449 450 451 452 453 454 455 456
/**
 * gtk_toggle_tool_button_get_active:
 * @button: a #GtkToggleToolButton
 * 
 * Queries a #GtkToggleToolButton and returns its current state.
 * Returns %TRUE if the toggle button is pressed in and %FALSE if it is raised.
 * 
 * Return value: %TRUE if the toggle tool button is pressed in, %FALSE if not
 * 
 * Since: 2.4
 **/
457 458 459 460 461
gboolean
gtk_toggle_tool_button_get_active (GtkToggleToolButton *button)
{
  g_return_val_if_fail (GTK_IS_TOGGLE_TOOL_BUTTON (button), FALSE);

462
  return button->priv->active;
463
}