gtkaction.c 44.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * GTK - The GIMP Toolkit
 * 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
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * 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/. 
 */

#include <config.h>

#include "gtkaction.h"
34
#include "gtkactiongroup.h"
Matthias Clasen's avatar
Matthias Clasen committed
35
#include "gtkaccellabel.h"
36
#include "gtkbutton.h"
37
#include "gtkimage.h"
Matthias Clasen's avatar
Matthias Clasen committed
38
#include "gtkimagemenuitem.h"
39
#include "gtkintl.h"
Matthias Clasen's avatar
Matthias Clasen committed
40
#include "gtklabel.h"
41
#include "gtkmarshalers.h"
Matthias Clasen's avatar
Matthias Clasen committed
42 43
#include "gtkmenuitem.h"
#include "gtkstock.h"
44
#include "gtktearoffmenuitem.h"
Matthias Clasen's avatar
Matthias Clasen committed
45 46
#include "gtktoolbutton.h"
#include "gtktoolbar.h"
47
#include "gtkprivate.h"
48
#include "gtkalias.h"
49 50 51 52 53 54 55 56 57 58 59 60


#define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))

struct _GtkActionPrivate 
{
  gchar *name;
  gchar *label;
  gchar *short_label;
  gchar *tooltip;
  gchar *stock_id; /* icon */

61 62 63 64
  guint sensitive          : 1;
  guint visible            : 1;
  guint label_set          : 1; /* these two used so we can set label */
  guint short_label_set    : 1; /* based on stock id */
65 66
  guint visible_horizontal : 1;
  guint visible_vertical   : 1;
67 68 69
  guint is_important       : 1;
  guint hide_if_empty      : 1;
  guint visible_overflown  : 1;
70 71

  /* accelerator */
72 73 74 75
  guint          accel_count;
  GtkAccelGroup *accel_group;
  GClosure      *accel_closure;
  GQuark         accel_quark;
76

77 78
  GtkActionGroup *action_group;

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  /* list of proxy widgets */
  GSList *proxies;
};

enum 
{
  ACTIVATE,
  LAST_SIGNAL
};

enum 
{
  PROP_0,
  PROP_NAME,
  PROP_LABEL,
  PROP_SHORT_LABEL,
  PROP_TOOLTIP,
  PROP_STOCK_ID,
97 98
  PROP_VISIBLE_HORIZONTAL,
  PROP_VISIBLE_VERTICAL,
99
  PROP_VISIBLE_OVERFLOWN,
100
  PROP_IS_IMPORTANT,
101
  PROP_HIDE_IF_EMPTY,
102
  PROP_SENSITIVE,
103 104
  PROP_VISIBLE,
  PROP_ACTION_GROUP
105 106 107 108 109
};

static void gtk_action_init       (GtkAction *action);
static void gtk_action_class_init (GtkActionClass *class);

110 111
static GQuark      accel_path_id  = 0;
static GQuark      quark_gtk_action_proxy  = 0;
112
static const gchar accel_path_key[] = "GtkAction::accel_path";
113
static const gchar gtk_action_proxy_key[] = "gtk-action";
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

GType
gtk_action_get_type (void)
{
  static GtkType type = 0;

  if (!type)
    {
      static const GTypeInfo type_info =
      {
        sizeof (GtkActionClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gtk_action_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,
        
        sizeof (GtkAction),
        0, /* n_preallocs */
        (GInstanceInitFunc) gtk_action_init,
      };

      type = g_type_register_static (G_TYPE_OBJECT,
Matthias Clasen's avatar
Matthias Clasen committed
137
				     I_("GtkAction"),
138 139 140 141 142 143 144 145 146 147 148 149 150 151
				     &type_info, 0);
    }
  return type;
}

static void gtk_action_finalize     (GObject *object);
static void gtk_action_set_property (GObject         *object,
				     guint            prop_id,
				     const GValue    *value,
				     GParamSpec      *pspec);
static void gtk_action_get_property (GObject         *object,
				     guint            prop_id,
				     GValue          *value,
				     GParamSpec      *pspec);
152
static void gtk_action_set_action_group (GtkAction	*action,
153
					 GtkActionGroup *action_group);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
static void gtk_action_set_is_important (GtkAction	*action,
					 gboolean        is_important);
static void gtk_action_set_label        (GtkAction	*action,
					 const gchar    *label);
static void gtk_action_set_short_label  (GtkAction	*action,
					 const gchar    *label);
static void gtk_action_set_visible_horizontal (GtkAction *action,
					       gboolean   visible_horizontal);
static void gtk_action_set_visible_vertical   (GtkAction *action,
					       gboolean   visible_vertical);
static void gtk_action_set_tooltip      (GtkAction	*action,
					 const gchar    *tooltip);
static void gtk_action_set_stock_id     (GtkAction	*action,
					 const gchar    *stock_id);
static void gtk_action_sync_tooltip     (GtkAction      *action,
					 GtkWidget      *proxy);
170 171 172

static GtkWidget *create_menu_item    (GtkAction *action);
static GtkWidget *create_tool_item    (GtkAction *action);
173 174
static void       connect_proxy       (GtkAction     *action,
				       GtkWidget     *proxy);
175 176
static void       disconnect_proxy    (GtkAction *action,
				       GtkWidget *proxy);
177 178 179 180 181 182
static void       closure_accel_activate (GClosure     *closure,
					  GValue       *return_value,
					  guint         n_param_values,
					  const GValue *param_values,
					  gpointer      invocation_hint,
					  gpointer      marshal_data);
183 184 185 186 187 188 189 190 191 192 193

static GObjectClass *parent_class = NULL;
static guint         action_signals[LAST_SIGNAL] = { 0 };


static void
gtk_action_class_init (GtkActionClass *klass)
{
  GObjectClass *gobject_class;

  accel_path_id = g_quark_from_static_string (accel_path_key);
194
  quark_gtk_action_proxy = g_quark_from_static_string (gtk_action_proxy_key);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

  parent_class = g_type_class_peek_parent (klass);
  gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize     = gtk_action_finalize;
  gobject_class->set_property = gtk_action_set_property;
  gobject_class->get_property = gtk_action_get_property;

  klass->activate = NULL;

  klass->create_menu_item = create_menu_item;
  klass->create_tool_item = create_tool_item;
  klass->connect_proxy = connect_proxy;
  klass->disconnect_proxy = disconnect_proxy;

  klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
  klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;

  g_object_class_install_property (gobject_class,
				   PROP_NAME,
				   g_param_spec_string ("name",
216 217
							P_("Name"),
							P_("A unique name for the action."),
218
							NULL,
219 220
							GTK_PARAM_READWRITE | 
							G_PARAM_CONSTRUCT_ONLY));
221 222 223
  g_object_class_install_property (gobject_class,
				   PROP_LABEL,
				   g_param_spec_string ("label",
224
							P_("Label"),
225 226
							P_("The label used for menu items and buttons "
							   "that activate this action."),
227
							NULL,
228
							GTK_PARAM_READWRITE));
229 230
  g_object_class_install_property (gobject_class,
				   PROP_SHORT_LABEL,
231
				   g_param_spec_string ("short-label",
232 233
							P_("Short label"),
							P_("A shorter label that may be used on toolbar buttons."),
234
							NULL,
235
							GTK_PARAM_READWRITE));
236 237 238
  g_object_class_install_property (gobject_class,
				   PROP_TOOLTIP,
				   g_param_spec_string ("tooltip",
239 240
							P_("Tooltip"),
							P_("A tooltip for this action."),
241
							NULL,
242
							GTK_PARAM_READWRITE));
243 244
  g_object_class_install_property (gobject_class,
				   PROP_STOCK_ID,
245
				   g_param_spec_string ("stock-id",
246
							P_("Stock Icon"),
247 248
							P_("The stock icon displayed in widgets representing "
							   "this action."),
249
							NULL,
250
							GTK_PARAM_READWRITE));
251 252
  g_object_class_install_property (gobject_class,
				   PROP_VISIBLE_HORIZONTAL,
253
				   g_param_spec_boolean ("visible-horizontal",
254
							 P_("Visible when horizontal"),
255 256 257
							 P_("Whether the toolbar item is visible when the toolbar "
							    "is in a horizontal orientation."),
							 TRUE,
258
							 GTK_PARAM_READWRITE));
259 260 261 262 263 264 265 266 267 268
  /**
   * GtkAction:visible-overflown:
   *
   * When %TRUE, toolitem proxies for this action are represented in the 
   * toolbar overflow menu.
   *
   * Since: 2.6
   */
  g_object_class_install_property (gobject_class,
				   PROP_VISIBLE_OVERFLOWN,
269
				   g_param_spec_boolean ("visible-overflown",
270 271 272
							 P_("Visible when overflown"),
							 P_("When TRUE, toolitem proxies for this action "
							    "are represented in the toolbar overflow menu."),
273
							 TRUE,
274
							 GTK_PARAM_READWRITE));
275 276
  g_object_class_install_property (gobject_class,
				   PROP_VISIBLE_VERTICAL,
277
				   g_param_spec_boolean ("visible-vertical",
278
							 P_("Visible when vertical"),
279 280
							 P_("Whether the toolbar item is visible when the toolbar "
							    "is in a vertical orientation."),
281
							 TRUE,
282
							 GTK_PARAM_READWRITE));
283 284
  g_object_class_install_property (gobject_class,
				   PROP_IS_IMPORTANT,
285
				   g_param_spec_boolean ("is-important",
286
							 P_("Is important"),
287 288 289
							 P_("Whether the action is considered important. "
							    "When TRUE, toolitem proxies for this action "
							    "show text in GTK_TOOLBAR_BOTH_HORIZ mode."),
290
							 FALSE,
291
							 GTK_PARAM_READWRITE));
292 293
  g_object_class_install_property (gobject_class,
				   PROP_HIDE_IF_EMPTY,
294
				   g_param_spec_boolean ("hide-if-empty",
295 296
							 P_("Hide if empty"),
							 P_("When TRUE, empty menu proxies for this action are hidden."),
297
							 TRUE,
298
							 GTK_PARAM_READWRITE));
299 300 301
  g_object_class_install_property (gobject_class,
				   PROP_SENSITIVE,
				   g_param_spec_boolean ("sensitive",
302 303
							 P_("Sensitive"),
							 P_("Whether the action is enabled."),
304
							 TRUE,
305
							 GTK_PARAM_READWRITE));
306 307 308
  g_object_class_install_property (gobject_class,
				   PROP_VISIBLE,
				   g_param_spec_boolean ("visible",
309 310
							 P_("Visible"),
							 P_("Whether the action is visible."),
311
							 TRUE,
312
							 GTK_PARAM_READWRITE));
313 314
  g_object_class_install_property (gobject_class,
				   PROP_ACTION_GROUP,
315
				   g_param_spec_object ("action-group",
316 317
							 P_("Action Group"),
							 P_("The GtkActionGroup this GtkAction is associated with, or NULL (for internal use)."),
318
							 GTK_TYPE_ACTION_GROUP,
319
							 GTK_PARAM_READWRITE));
320

Matthias Clasen's avatar
Matthias Clasen committed
321 322 323 324 325 326 327 328
  /**
   * GtkAction::activate:
   * @action: the #GtkAction
   *
   * The "activate" signal is emitted when the action is activated.
   *
   * Since: 2.4
   */
329
  action_signals[ACTIVATE] =
Matthias Clasen's avatar
Matthias Clasen committed
330
    g_signal_new (I_("activate"),
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
		  G_OBJECT_CLASS_TYPE (klass),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkActionClass, activate),  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
}


static void
gtk_action_init (GtkAction *action)
{
  action->private_data = GTK_ACTION_GET_PRIVATE (action);

  action->private_data->name = NULL;
  action->private_data->label = NULL;
  action->private_data->short_label = NULL;
  action->private_data->tooltip = NULL;
  action->private_data->stock_id = NULL;
351 352
  action->private_data->visible_horizontal = TRUE;
  action->private_data->visible_vertical   = TRUE;
353
  action->private_data->visible_overflown  = TRUE;
354
  action->private_data->is_important = FALSE;
355
  action->private_data->hide_if_empty = TRUE;
356 357 358 359 360 361 362

  action->private_data->sensitive = TRUE;
  action->private_data->visible = TRUE;

  action->private_data->label_set = FALSE;
  action->private_data->short_label_set = FALSE;

363
  action->private_data->accel_count = 0;
364 365
  action->private_data->accel_group = NULL;
  action->private_data->accel_quark = 0;
366 367 368 369 370 371 372
  action->private_data->accel_closure = 
    g_closure_new_object (sizeof (GClosure), G_OBJECT (action));
  g_closure_set_marshal (action->private_data->accel_closure, 
			 closure_accel_activate);
  g_closure_ref (action->private_data->accel_closure);
  g_closure_sink (action->private_data->accel_closure);

373 374
  action->private_data->action_group = NULL;

375 376 377
  action->private_data->proxies = NULL;
}

378 379 380 381 382 383 384 385 386 387
/**
 * gtk_action_new:
 * @name: A unique name for the action
 * @label: the label displayed in menu items and on buttons
 * @tooltip: a tooltip for the action
 * @stock_id: the stock icon to display in widgets representing the action
 *
 * Creates a new #GtkAction object. To add the action to a
 * #GtkActionGroup and set the accelerator for the action,
 * call gtk_action_group_add_action_with_accel().
Matthias Clasen's avatar
Matthias Clasen committed
388
 * See <xref linkend="XML-UI"/> for information on allowed action
389
 * names.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
 *
 * Return value: a new #GtkAction
 *
 * Since: 2.4
 */
GtkAction *
gtk_action_new (const gchar *name,
		const gchar *label,
		const gchar *tooltip,
		const gchar *stock_id)
{
  GtkAction *action;

  action = g_object_new (GTK_TYPE_ACTION,
			 "name", name,
			 "label", label,
			 "tooltip", tooltip,
			 "stock_id", stock_id,
			 NULL);

  return action;
}

413 414 415 416 417 418 419 420 421 422 423
static void
gtk_action_finalize (GObject *object)
{
  GtkAction *action;
  action = GTK_ACTION (object);

  g_free (action->private_data->name);
  g_free (action->private_data->label);
  g_free (action->private_data->short_label);
  g_free (action->private_data->tooltip);
  g_free (action->private_data->stock_id);
424

425
  g_closure_unref (action->private_data->accel_closure);
426 427
  if (action->private_data->accel_group)
    g_object_unref (action->private_data->accel_group);
428 429

  G_OBJECT_CLASS (parent_class)->finalize (object);  
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
}

static void
gtk_action_set_property (GObject         *object,
			 guint            prop_id,
			 const GValue    *value,
			 GParamSpec      *pspec)
{
  GtkAction *action;
  gchar *tmp;
  
  action = GTK_ACTION (object);

  switch (prop_id)
    {
    case PROP_NAME:
      tmp = action->private_data->name;
      action->private_data->name = g_value_dup_string (value);
      g_free (tmp);
      break;
    case PROP_LABEL:
451
      gtk_action_set_label (action, g_value_get_string (value));
452 453
      break;
    case PROP_SHORT_LABEL:
454
      gtk_action_set_short_label (action, g_value_get_string (value));
455 456
      break;
    case PROP_TOOLTIP:
457
      gtk_action_set_tooltip (action, g_value_get_string (value));
458 459
      break;
    case PROP_STOCK_ID:
460
      gtk_action_set_stock_id (action, g_value_get_string (value));
461
      break;
462
    case PROP_VISIBLE_HORIZONTAL:
463
      gtk_action_set_visible_horizontal (action, g_value_get_boolean (value));
464 465
      break;
    case PROP_VISIBLE_VERTICAL:
466
      gtk_action_set_visible_vertical (action, g_value_get_boolean (value));
467
      break;
468 469 470
    case PROP_VISIBLE_OVERFLOWN:
      action->private_data->visible_overflown = g_value_get_boolean (value);
      break;
471
    case PROP_IS_IMPORTANT:
472
      gtk_action_set_is_important (action, g_value_get_boolean (value));
473
      break;
474 475 476
    case PROP_HIDE_IF_EMPTY:
      action->private_data->hide_if_empty = g_value_get_boolean (value);
      break;
477
    case PROP_SENSITIVE:
478
      gtk_action_set_sensitive (action, g_value_get_boolean (value));
479 480
      break;
    case PROP_VISIBLE:
481
      gtk_action_set_visible (action, g_value_get_boolean (value));
482
      break;
483 484 485
    case PROP_ACTION_GROUP:
      gtk_action_set_action_group (action, g_value_get_object (value));
      break;
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_action_get_property (GObject    *object,
			 guint       prop_id,
			 GValue     *value,
			 GParamSpec *pspec)
{
  GtkAction *action;

  action = GTK_ACTION (object);

  switch (prop_id)
    {
    case PROP_NAME:
      g_value_set_string (value, action->private_data->name);
      break;
    case PROP_LABEL:
      g_value_set_string (value, action->private_data->label);
      break;
    case PROP_SHORT_LABEL:
      g_value_set_string (value, action->private_data->short_label);
      break;
    case PROP_TOOLTIP:
      g_value_set_string (value, action->private_data->tooltip);
      break;
    case PROP_STOCK_ID:
      g_value_set_string (value, action->private_data->stock_id);
      break;
519 520 521 522 523 524
    case PROP_VISIBLE_HORIZONTAL:
      g_value_set_boolean (value, action->private_data->visible_horizontal);
      break;
    case PROP_VISIBLE_VERTICAL:
      g_value_set_boolean (value, action->private_data->visible_vertical);
      break;
525 526 527
    case PROP_VISIBLE_OVERFLOWN:
      g_value_set_boolean (value, action->private_data->visible_overflown);
      break;
528 529 530
    case PROP_IS_IMPORTANT:
      g_value_set_boolean (value, action->private_data->is_important);
      break;
531 532 533
    case PROP_HIDE_IF_EMPTY:
      g_value_set_boolean (value, action->private_data->hide_if_empty);
      break;
534 535 536 537 538 539
    case PROP_SENSITIVE:
      g_value_set_boolean (value, action->private_data->sensitive);
      break;
    case PROP_VISIBLE:
      g_value_set_boolean (value, action->private_data->visible);
      break;
540 541 542
    case PROP_ACTION_GROUP:
      g_value_set_object (value, action->private_data->action_group);
      break;
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static GtkWidget *
create_menu_item (GtkAction *action)
{
  GType menu_item_type;

  menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;

  return g_object_new (menu_item_type, NULL);
}

static GtkWidget *
create_tool_item (GtkAction *action)
{
  GType toolbar_item_type;

  toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;

  return g_object_new (toolbar_item_type, NULL);
}

static void
570 571
remove_proxy (GtkAction *action,
	      GtkWidget *proxy)
572
{
573
  if (GTK_IS_MENU_ITEM (proxy))
574
    gtk_action_disconnect_accelerator (action);
575
  
576
  action->private_data->proxies = g_slist_remove (action->private_data->proxies, proxy);
577 578
}

579 580 581 582 583 584 585 586 587 588 589
/**
 * _gtk_action_sync_menu_visible:
 * @action: a #GtkAction, or %NULL to determine the action from @proxy
 * @proxy: a proxy menu item
 * @empty: whether the submenu attached to @proxy is empty
 * 
 * Updates the visibility of @proxy from the visibility of @action
 * according to the following rules:
 * <itemizedlist>
 * <listitem><para>if @action is invisible, @proxy is too
 * </para></listitem>
590 591
 * <listitem><para>if @empty is %TRUE, hide @proxy unless the "hide-if-empty" 
 *   property of @action indicates otherwise
592 593 594 595 596 597 598 599 600 601
 * </para></listitem>
 * </itemizedlist>
 * 
 * This function is used in the implementation of #GtkUIManager.
 **/
void
_gtk_action_sync_menu_visible (GtkAction *action,
			       GtkWidget *proxy,
			       gboolean   empty)
{
602
  gboolean visible, hide_if_empty;
603 604 605

  g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
  g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
606

607
  if (action == NULL)
608
    action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
609

610
  visible = gtk_action_is_visible (action);
611
  hide_if_empty = action->private_data->hide_if_empty;
612

613 614 615 616
  if (visible && !(empty && hide_if_empty))
    gtk_widget_show (proxy);
  else
    gtk_widget_hide (proxy);
617 618 619 620
}

gboolean _gtk_menu_is_empty (GtkWidget *menu);

621 622 623 624
static gboolean
gtk_action_create_menu_proxy (GtkToolItem *tool_item, 
			      GtkAction   *action)
{
625
  GtkWidget *menu_item;
626
  
627 628 629 630
  if (action->private_data->visible_overflown)
    {
      menu_item = gtk_action_create_menu_item (action);

631
      g_object_ref_sink (menu_item);
632 633 634 635 636 637 638 639
      
      gtk_tool_item_set_proxy_menu_item (tool_item, 
					 "gtk-action-menu-item", menu_item);
      g_object_unref (menu_item);
    }
  else
    gtk_tool_item_set_proxy_menu_item (tool_item, 
				       "gtk-action-menu-item", NULL);
640 641 642 643 644

  return TRUE;
}

static void
645 646
connect_proxy (GtkAction     *action, 
	       GtkWidget     *proxy)
647 648
{
  g_object_ref (action);
649 650
  g_object_set_qdata_full (G_OBJECT (proxy), quark_gtk_action_proxy, action,
			   g_object_unref);
651 652 653

  /* add this widget to the list of proxies */
  action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
654 655
  g_object_weak_ref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
  
656
  gtk_widget_set_sensitive (proxy, gtk_action_is_sensitive (action));
657
  if (gtk_action_is_visible (action))
658 659 660
    gtk_widget_show (proxy);
  else
    gtk_widget_hide (proxy);
661
  gtk_widget_set_no_show_all (proxy, TRUE);
662 663 664 665 666 667

  if (GTK_IS_MENU_ITEM (proxy))
    {
      GtkWidget *label;
      /* menu item specific synchronisers ... */
      
668 669 670 671 672 673 674
      if (action->private_data->accel_quark)
 	{
	  gtk_action_connect_accelerator (action);
 	  gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
 					g_quark_to_string (action->private_data->accel_quark));
 	}
      
675 676 677 678 679 680 681 682
      label = GTK_BIN (proxy)->child;

      /* make sure label is a label */
      if (label && !GTK_IS_LABEL (label))
	{
	  gtk_container_remove (GTK_CONTAINER (proxy), label);
	  label = NULL;
	}
683

684
      if (!label)
685 686 687 688 689 690 691 692
	label = g_object_new (GTK_TYPE_ACCEL_LABEL,
			      "use_underline", TRUE,
			      "xalign", 0.0,
			      "visible", TRUE,
			      "parent", proxy,
			      NULL);
      
      if (GTK_IS_ACCEL_LABEL (label) && action->private_data->accel_quark)
693
	g_object_set (label,
694 695 696
		      "accel_closure", action->private_data->accel_closure,
		      NULL);

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
      gtk_label_set_label (GTK_LABEL (label), action->private_data->label);

      if (GTK_IS_IMAGE_MENU_ITEM (proxy))
	{
	  GtkWidget *image;

	  image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
	  if (image && !GTK_IS_IMAGE (image))
	    {
	      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
	      image = NULL;
	    }
	  if (!image)
	    {
	      image = gtk_image_new_from_stock (NULL,
						GTK_ICON_SIZE_MENU);
	      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
					     image);
	      gtk_widget_show (image);
	    }
	  gtk_image_set_from_stock (GTK_IMAGE (image),
				    action->private_data->stock_id, GTK_ICON_SIZE_MENU);
	}

721
      if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy)) == NULL)
722 723 724
	g_signal_connect_object (proxy, "activate",
				 G_CALLBACK (gtk_action_activate), action,
				 G_CONNECT_SWAPPED);
725

726
    }
727
  else if (GTK_IS_TOOL_ITEM (proxy))
728
    {
729
      /* toolbar item specific synchronisers ... */
730

731
      g_object_set (proxy,
732
		    "visible_horizontal", action->private_data->visible_horizontal,
733
		    "visible_vertical",	action->private_data->visible_vertical,
734
		    "is_important", action->private_data->is_important,
735
		    NULL);
736

737
      gtk_action_sync_tooltip (action, proxy);
738

739 740 741 742
      g_signal_connect_object (proxy, "create_menu_proxy",
			       G_CALLBACK (gtk_action_create_menu_proxy),
			       action, 0);

743 744
      gtk_tool_item_rebuild_menu (GTK_TOOL_ITEM (proxy));

745 746 747
      /* toolbar button specific synchronisers ... */
      if (GTK_IS_TOOL_BUTTON (proxy))
	{
748
	  g_object_set (proxy,
749 750 751 752
			"label", action->private_data->short_label,
			"use_underline", TRUE,
			"stock_id", action->private_data->stock_id,
			NULL);
753

754 755 756 757
	  g_signal_connect_object (proxy, "clicked",
				   G_CALLBACK (gtk_action_activate), action,
				   G_CONNECT_SWAPPED);
	}
758
    }
759 760 761
  else if (GTK_IS_BUTTON (proxy))
    {
      /* button specific synchronisers ... */
762 763 764 765
      if (gtk_button_get_use_stock (GTK_BUTTON (proxy)))
	{
	  /* synchronise stock-id */
	  g_object_set (proxy,
766
			"label", action->private_data->stock_id,
767 768
			NULL);
	}
769 770
      else if (GTK_BIN (proxy)->child == NULL || 
	       GTK_IS_LABEL (GTK_BIN (proxy)->child))
771 772 773 774 775 776 777
	{
	  /* synchronise the label */
	  g_object_set (proxy,
			"label", action->private_data->short_label,
			"use_underline", TRUE,
			NULL);
	}
778
      
779
      /* we leave the button alone if there is a custom child */
780 781 782 783
      g_signal_connect_object (proxy, "clicked",
			       G_CALLBACK (gtk_action_activate), action,
			       G_CONNECT_SWAPPED);
    }
784

785 786
  if (action->private_data->action_group)
    _gtk_action_group_emit_connect_proxy (action->private_data->action_group, action, proxy);
787 788 789 790 791 792
}

static void
disconnect_proxy (GtkAction *action, 
		  GtkWidget *proxy)
{
793
  g_object_set_qdata (G_OBJECT (proxy), quark_gtk_action_proxy, NULL);
794

Matthias Clasen's avatar
Matthias Clasen committed
795
  g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)remove_proxy, action);
796
  remove_proxy (action, proxy);
797 798 799 800 801 802 803 804 805 806

  /* disconnect the activate handler */
  g_signal_handlers_disconnect_by_func (proxy,
					G_CALLBACK (gtk_action_activate),
					action);

  /* toolbar button specific synchronisers ... */
  g_signal_handlers_disconnect_by_func (proxy,
					G_CALLBACK (gtk_action_create_menu_proxy),
					action);
807

808 809
  if (action->private_data->action_group)
    _gtk_action_group_emit_disconnect_proxy (action->private_data->action_group, action, proxy);
810 811
}

812 813 814
void
_gtk_action_emit_activate (GtkAction *action)
{
815 816
  GtkActionGroup *group = action->private_data->action_group;

Matthias Clasen's avatar
Matthias Clasen committed
817 818 819 820 821
  if (group != NULL) 
    {
      g_object_ref (group);
      _gtk_action_group_emit_pre_activate (group, action);
    }
822

823
    g_signal_emit (action, action_signals[ACTIVATE], 0);
824

Matthias Clasen's avatar
Matthias Clasen committed
825 826 827 828 829
  if (group != NULL) 
    {
      _gtk_action_group_emit_post_activate (group, action);
      g_object_unref (group);
    }
830 831
}

832 833 834 835
/**
 * gtk_action_activate:
 * @action: the action object
 *
836 837 838
 * Emits the "activate" signal on the specified action, if it isn't 
 * insensitive. This gets called by the proxy widgets when they get 
 * activated.
839 840 841 842 843 844 845 846
 *
 * It can also be used to manually activate an action.
 *
 * Since: 2.4
 */
void
gtk_action_activate (GtkAction *action)
{
847 848
  g_return_if_fail (GTK_IS_ACTION (action));
  
849
  if (gtk_action_is_sensitive (action))
850
    _gtk_action_emit_activate (action);
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
}

/**
 * gtk_action_create_icon:
 * @action: the action object
 * @icon_size: the size of the icon that should be created.
 *
 * This function is intended for use by action implementations to
 * create icons displayed in the proxy widgets.
 *
 * Returns: a widget that displays the icon for this action.
 *
 * Since: 2.4
 */
GtkWidget *
gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
{
  g_return_val_if_fail (GTK_IS_ACTION (action), NULL);

  if (action->private_data->stock_id)
    return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
  else
    return NULL;
}

/**
 * gtk_action_create_menu_item:
 * @action: the action object
 *
 * Creates a menu item widget that proxies for the given action.
 *
 * Returns: a menu item connected to the action.
 *
 * Since: 2.4
 */
GtkWidget *
gtk_action_create_menu_item (GtkAction *action)
{
  GtkWidget *menu_item;

  g_return_val_if_fail (GTK_IS_ACTION (action), NULL);

  menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);

  (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);

  return menu_item;
}

/**
 * gtk_action_create_tool_item:
 * @action: the action object
 *
 * Creates a toolbar item widget that proxies for the given action.
 *
 * Returns: a toolbar item connected to the action.
 *
 * Since: 2.4
 */
GtkWidget *
gtk_action_create_tool_item (GtkAction *action)
{
  GtkWidget *button;

  g_return_val_if_fail (GTK_IS_ACTION (action), NULL);

  button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);

  (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);

  return button;
}

/**
 * gtk_action_connect_proxy:
 * @action: the action object
 * @proxy: the proxy widget
 *
 * Connects a widget to an action object as a proxy.  Synchronises 
 * various properties of the action with the widget (such as label 
 * text, icon, tooltip, etc), and attaches a callback so that the 
 * action gets activated when the proxy widget does.
 *
 * If the widget is already connected to an action, it is disconnected
 * first.
 *
 * Since: 2.4
 */
void
gtk_action_connect_proxy (GtkAction *action,
			  GtkWidget *proxy)
{
  GtkAction *prev_action;

  g_return_if_fail (GTK_IS_ACTION (action));
  g_return_if_fail (GTK_IS_WIDGET (proxy));

948
  prev_action = g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy);
949 950

  if (prev_action)
951
    (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (prev_action, proxy);  
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

  (* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
}

/**
 * gtk_action_disconnect_proxy:
 * @action: the action object
 * @proxy: the proxy widget
 *
 * Disconnects a proxy widget from an action.  
 * Does <emphasis>not</emphasis> destroy the widget, however.
 *
 * Since: 2.4
 */
void
gtk_action_disconnect_proxy (GtkAction *action,
			     GtkWidget *proxy)
{
  g_return_if_fail (GTK_IS_ACTION (action));
  g_return_if_fail (GTK_IS_WIDGET (proxy));

973
  g_return_if_fail (g_object_get_qdata (G_OBJECT (proxy), quark_gtk_action_proxy) == action);
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

  (* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);  
}

/**
 * gtk_action_get_proxies:
 * @action: the action object
 * 
 * Returns the proxy widgets for an action.
 * 
 * Return value: a #GSList of proxy widgets. The list is owned by the action and
 * must not be modified.
 *
 * Since: 2.4
 **/
GSList*
gtk_action_get_proxies (GtkAction *action)
{
  g_return_val_if_fail (GTK_IS_ACTION (action), NULL);

  return action->private_data->proxies;
}


/**
 * gtk_action_get_name:
 * @action: the action object
 * 
 * Returns the name of the action.
 * 
 * Return value: the name of the action. The string belongs to GTK+ and should not
 *   be freed.
 *
 * Since: 2.4
 **/
1009
G_CONST_RETURN gchar *
1010 1011 1012 1013 1014 1015 1016
gtk_action_get_name (GtkAction *action)
{
  g_return_val_if_fail (GTK_IS_ACTION (action), NULL);

  return action->private_data->name;
}

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
/**
 * gtk_action_is_sensitive:
 * @action: the action object
 * 
 * Returns whether the action is effectively sensitive.
 *
 * Return value: %TRUE if the action and its associated action group 
 * are both sensitive.
 *
 * Since: 2.4
 **/
gboolean
gtk_action_is_sensitive (GtkAction *action)
{
  GtkActionPrivate *priv;
  g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);

  priv = action->private_data;
  return priv->sensitive &&
    (priv->action_group == NULL ||
     gtk_action_group_get_sensitive (priv->action_group));
}

/**
 * gtk_action_get_sensitive:
 * @action: the action object
 * 
 * Returns whether the action itself is sensitive. Note that this doesn't 
 * necessarily mean effective sensitivity. See gtk_action_is_sensitive() 
 * for that.
 *
 * Return value: %TRUE if the action itself is sensitive.
 *
 * Since: 2.4
 **/
gboolean
gtk_action_get_sensitive (GtkAction *action)
{
  g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);

  return action->private_data->sensitive;
}

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
void
_gtk_action_sync_sensitive (GtkAction *action)
{
  GSList *p;
  GtkWidget *proxy;
  gboolean sensitive;

  sensitive = gtk_action_is_sensitive (action);

  for (p = action->private_data->proxies; p; p = p->next)
    {
      proxy = (GtkWidget *)p->data;
      gtk_widget_set_sensitive (proxy, sensitive);
    }      
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
/**
 * gtk_action_set_sensitive:
 * @action: the action object
 * @sensitive: %TRUE to make the action sensitive
 * 
 * Sets the ::sensitive property of the action to @sensitive. Note that 
 * this doesn't necessarily mean effective sensitivity. See 
 * gtk_action_is_sensitive() 
 * for that.
 *
 * Since: 2.6
 **/
void
gtk_action_set_sensitive (GtkAction *action,
			  gboolean   sensitive)
{
  g_return_if_fail (GTK_IS_ACTION (action));

  sensitive = sensitive != FALSE;
  
  if (action->private_data->sensitive != sensitive)
    {
      action->private_data->sensitive = sensitive;

1100 1101
      _gtk_action_sync_sensitive (action);

1102 1103 1104 1105
      g_object_notify (G_OBJECT (action), "sensitive");
    }
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
/**
 * gtk_action_is_visible:
 * @action: the action object
 * 
 * Returns whether the action is effectively visible.
 *
 * Return value: %TRUE if the action and its associated action group 
 * are both visible.
 *
 * Since: 2.4
 **/
gboolean
gtk_action_is_visible (GtkAction *action)
{
  GtkActionPrivate *priv;
  g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);

  priv = action->private_data;
  return priv->visible &&
    (priv->action_group == NULL ||
     gtk_action_group_get_visible (priv->action_group));
}

/**
 * gtk_action_get_visible:
 * @action: the action object
 * 
 * Returns whether the action itself is visible. Note that this doesn't 
 * necessarily mean effective visibility. See gtk_action_is_sensitive() 
 * for that.
 *
 * Return value: %TRUE if the action itself is visible.
 *
 * Since: 2.4
 **/
gboolean
gtk_action_get_visible (GtkAction *action)
{
  g_return_val_if_fail (GTK_IS_ACTION (action), FALSE);

  return action->private_data->visible;
}

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
void
_gtk_action_sync_visible (GtkAction *action)
{
  GSList *p;
  GtkWidget *proxy;
  GtkWidget *menu;
  gboolean visible;

  visible = gtk_action_is_visible (action);
    
  for (p = action->private_data->proxies; p; p = p->next)
    {
      proxy = (GtkWidget *)p->data;

      if (GTK_IS_MENU_ITEM (proxy))
	{
	  menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (proxy));
	  
	  _gtk_action_sync_menu_visible (action, proxy, _gtk_menu_is_empty (menu));
	}
      else
	{
	  if (visible)
	    gtk_widget_show (proxy);
	  else
	    gtk_widget_hide (proxy);
	}
    } 
}

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
/**
 * gtk_action_set_visible:
 * @action: the action object
 * @visible: %TRUE to make the action visible
 * 
 * Sets the ::visible property of the action to @visible. Note that 
 * this doesn't necessarily mean effective visibility. See 
 * gtk_action_is_visible() 
 * for that.
 *
 * Since: 2.6
 **/
void
gtk_action_set_visible (GtkAction *action,
			gboolean   visible)
{
  g_return_if_fail (GTK_IS_ACTION (action));

  visible = visible != FALSE;
  
  if (action->private_data->visible != visible)
    {
      action->private_data->visible = visible;

1203
      _gtk_action_sync_visible (action);
1204

1205 1206 1207 1208
      g_object_notify (G_OBJECT (action), "visible");
    }
}

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249