gtkuimanager.c 69.7 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 <string.h>
34
#include "gtkalias.h"
35 36
#include "gtkintl.h"
#include "gtkmarshalers.h"
37 38
#include "gtkmenu.h"
#include "gtkmenubar.h"
39
#include "gtkmenushell.h"
40
#include "gtkseparatormenuitem.h"
41
#include "gtkseparatortoolitem.h"
42
#include "gtktearoffmenuitem.h"
43 44
#include "gtktoolbar.h"
#include "gtkuimanager.h"
45

Matthias Clasen's avatar
Matthias Clasen committed
46
#undef DEBUG_UI_MANAGER
47 48 49

typedef enum 
{
50 51 52 53 54 55 56 57 58 59 60
  NODE_TYPE_UNDECIDED,
  NODE_TYPE_ROOT,
  NODE_TYPE_MENUBAR,
  NODE_TYPE_MENU,
  NODE_TYPE_TOOLBAR,
  NODE_TYPE_MENU_PLACEHOLDER,
  NODE_TYPE_TOOLBAR_PLACEHOLDER,
  NODE_TYPE_POPUP,
  NODE_TYPE_MENUITEM,
  NODE_TYPE_TOOLITEM,
  NODE_TYPE_SEPARATOR,
61
  NODE_TYPE_ACCELERATOR
62
} NodeType;
63

64
typedef struct _Node Node;
65

66 67
struct _Node {
  NodeType type;
68

Matthias Clasen's avatar
Matthias Clasen committed
69
  gchar *name;
70 71 72 73

  GQuark action_name;
  GtkAction *action;
  GtkWidget *proxy;
74
  GtkWidget *extra; /* second separator for placeholders */
75 76 77 78 79 80

  GList *uifiles;

  guint dirty : 1;
};

81
#define GTK_UI_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_UI_MANAGER, GtkUIManagerPrivate))
82

83
struct _GtkUIManagerPrivate 
84 85 86 87 88 89 90 91 92
{
  GtkAccelGroup *accel_group;

  GNode *root_node;
  GList *action_groups;

  guint last_merge_id;

  guint update_tag;  
93 94

  gboolean add_tearoffs;
95 96
};

97
#define NODE_INFO(node) ((Node *)node->data)
98 99 100 101 102 103 104 105 106

typedef struct _NodeUIReference NodeUIReference;

struct _NodeUIReference 
{
  guint merge_id;
  GQuark action_quark;
};

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static void     gtk_ui_manager_class_init   (GtkUIManagerClass *class);
static void     gtk_ui_manager_init         (GtkUIManager      *self);
static void     gtk_ui_manager_finalize     (GObject           *object);
static void     gtk_ui_manager_set_property (GObject           *object,
					     guint              prop_id,
					     const GValue      *value,
					     GParamSpec        *pspec);
static void     gtk_ui_manager_get_property (GObject           *object,
					     guint              prop_id,
					     GValue            *value,
					     GParamSpec        *pspec);
static void     queue_update                (GtkUIManager      *self);
static void     dirty_all_nodes             (GtkUIManager      *self);
static GNode *  get_child_node              (GtkUIManager      *self,
					     GNode             *parent,
					     const gchar       *childname,
					     gint               childname_length,
					     NodeType           node_type,
					     gboolean           create,
					     gboolean           top);
static GNode *  get_node                    (GtkUIManager      *self,
					     const gchar       *path,
					     NodeType           node_type,
					     gboolean           create);
static gboolean free_node                   (GNode             *node);
static void     node_prepend_ui_reference   (Node              *node,
					     guint              merge_id,
					     GQuark             action_quark);
static void     node_remove_ui_reference    (Node              *node,
					     guint              merge_id);

138 139 140 141

enum 
{
  ADD_WIDGET,
142
  ACTIONS_CHANGED,
143 144 145 146
  CONNECT_PROXY,
  DISCONNECT_PROXY,
  PRE_ACTIVATE,
  POST_ACTIVATE,
147 148 149
  LAST_SIGNAL
};

150 151 152
enum
{
  PROP_0,
153 154
  PROP_ADD_TEAROFFS,
  PROP_UI
155 156
};

157
static GObjectClass *parent_class = NULL;
158
static guint ui_manager_signals[LAST_SIGNAL] = { 0 };
159 160 161 162

static GMemChunk *merge_node_chunk = NULL;

GType
163
gtk_ui_manager_get_type (void)
164 165 166 167 168 169 170
{
  static GtkType type = 0;

  if (!type)
    {
      static const GTypeInfo type_info =
      {
171
        sizeof (GtkUIManagerClass),
172 173
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
174
        (GClassInitFunc) gtk_ui_manager_class_init,
175 176 177
        (GClassFinalizeFunc) NULL,
        NULL,
        
178
        sizeof (GtkUIManager),
179
        0, /* n_preallocs */
180
        (GInstanceInitFunc) gtk_ui_manager_init,
181 182 183
      };

      type = g_type_register_static (G_TYPE_OBJECT,
184
				     "GtkUIManager",
185 186 187 188 189 190
				     &type_info, 0);
    }
  return type;
}

static void
191
gtk_ui_manager_class_init (GtkUIManagerClass *klass)
192 193 194
{
  GObjectClass *gobject_class;

195 196
  parent_class = g_type_class_peek_parent (klass);

197 198 199
  gobject_class = G_OBJECT_CLASS (klass);

  if (!merge_node_chunk)
200
    merge_node_chunk = g_mem_chunk_create (Node, 64,
201 202
					   G_ALLOC_AND_FREE);

203
  gobject_class->finalize = gtk_ui_manager_finalize;
204 205
  gobject_class->set_property = gtk_ui_manager_set_property;
  gobject_class->get_property = gtk_ui_manager_get_property;
206
  
207 208 209
  /**
   * GtkUIManager:add-tearoffs:
   *
210
   * The "add-tearoffs" property controls whether generated menus 
211 212 213 214 215 216 217 218 219 220
   * have tearoff menu items. 
   *
   * Note that this only affects regular menus. Generated popup 
   * menus never have tearoff menu items.   
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_ADD_TEAROFFS,
                                   g_param_spec_boolean ("add_tearoffs",
221 222
							 P_("Add tearoffs to menus"),
							 P_("Whether tearoff menu items should be added to menus"),
223
                                                         FALSE,
224 225 226 227 228
							 G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
				   PROP_UI,
				   g_param_spec_string ("ui",
229 230
 							P_("Merged UI definition"),
							P_("An XML string describing the merged UI"),
231 232 233 234
							NULL,
							G_PARAM_READABLE));


235 236 237 238 239 240 241 242 243 244 245
  /**
   * GtkUIManager::add-widget:
   * @merge: a #GtkUIManager
   * @widget: the added widget
   *
   * The add_widget signal is emitted for each generated menubar and toolbar.
   * It is not emitted for generated popup menus, which can be obtained by 
   * gtk_ui_manager_get_widget().
   *
   * Since: 2.4
   */
246
  ui_manager_signals[ADD_WIDGET] =
247 248 249
    g_signal_new ("add_widget",
		  G_OBJECT_CLASS_TYPE (klass),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
250 251
		  G_STRUCT_OFFSET (GtkUIManagerClass, add_widget),
		  NULL, NULL,
252
		  g_cclosure_marshal_VOID__OBJECT,
253
		  G_TYPE_NONE, 1, 
254
		  GTK_TYPE_WIDGET);
255

256
  /**
257
   * GtkUIManager::actions-changed:
258 259
   * @merge: a #GtkUIManager
   *
260 261
   * The "actions-changed" signal is emitted whenever the set of actions
   * changes.
262 263 264
   *
   * Since: 2.4
   */
265
  ui_manager_signals[ACTIONS_CHANGED] =
266
    g_signal_new ("actions_changed",
267 268
		  G_OBJECT_CLASS_TYPE (klass),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
269 270
		  G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),  
		  NULL, NULL,
271 272
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
273
  
274
  /**
275
   * GtkUIManager::connect-proxy:
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
   * @uimanager: the ui manager
   * @action: the action
   * @proxy: the proxy
   *
   * The connect_proxy signal is emitted after connecting a proxy to 
   * an action in the group. 
   *
   * This is intended for simple customizations for which a custom action
   * class would be too clumsy, e.g. showing tooltips for menuitems in the
   * statusbar.
   *
   * Since: 2.4
   */
  ui_manager_signals[CONNECT_PROXY] =
    g_signal_new ("connect_proxy",
		  G_OBJECT_CLASS_TYPE (klass),
292 293
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkUIManagerClass, connect_proxy),
294
		  NULL, NULL,
295 296
		  _gtk_marshal_VOID__OBJECT_OBJECT,
		  G_TYPE_NONE, 2, 
297 298
		  GTK_TYPE_ACTION,
		  GTK_TYPE_WIDGET);
299 300

  /**
301
   * GtkUIManager::disconnect-proxy:
302 303 304 305 306 307 308 309 310 311 312 313
   * @uimanager: the ui manager
   * @action: the action
   * @proxy: the proxy
   *
   * The disconnect_proxy signal is emitted after disconnecting a proxy 
   * from an action in the group. 
   *
   * Since: 2.4
   */
  ui_manager_signals[DISCONNECT_PROXY] =
    g_signal_new ("disconnect_proxy",
		  G_OBJECT_CLASS_TYPE (klass),
314 315 316
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkUIManagerClass, disconnect_proxy),
		  NULL, NULL,
317
		  _gtk_marshal_VOID__OBJECT_OBJECT,
318 319 320
		  G_TYPE_NONE, 2,
		  GTK_TYPE_ACTION,
		  GTK_TYPE_WIDGET);
321 322

  /**
323
   * GtkUIManager::pre-activate:
324 325 326 327 328 329 330 331 332 333 334 335 336 337
   * @uimanager: the ui manager
   * @action: the action
   *
   * The pre_activate signal is emitted just before the @action
   * is activated.
   *
   * This is intended for applications to get notification
   * just before any action is activated.
   *
   * Since: 2.4
   */
  ui_manager_signals[PRE_ACTIVATE] =
    g_signal_new ("pre_activate",
		  G_OBJECT_CLASS_TYPE (klass),
338 339 340
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkUIManagerClass, pre_activate),
		  NULL, NULL,
341
		  _gtk_marshal_VOID__OBJECT,
342
		  G_TYPE_NONE, 1,
343 344 345
		  GTK_TYPE_ACTION);

  /**
346
   * GtkUIManager::post-activate:
347 348 349 350 351 352 353 354 355 356 357 358 359 360
   * @uimanager: the ui manager
   * @action: the action
   *
   * The post_activate signal is emitted just after the @action
   * is activated.
   *
   * This is intended for applications to get notification
   * just after any action is activated.
   *
   * Since: 2.4
   */
  ui_manager_signals[POST_ACTIVATE] =
    g_signal_new ("post_activate",
		  G_OBJECT_CLASS_TYPE (klass),
361 362 363
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkUIManagerClass, post_activate),
		  NULL, NULL,
364
		  _gtk_marshal_VOID__OBJECT,
365
		  G_TYPE_NONE, 1,
366 367
		  GTK_TYPE_ACTION);

368 369 370 371 372 373 374
  klass->add_widget = NULL;
  klass->actions_changed = NULL;
  klass->connect_proxy = NULL;
  klass->disconnect_proxy = NULL;
  klass->pre_activate = NULL;
  klass->post_activate = NULL;

375
  g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
376 377 378 379
}


static void
380
gtk_ui_manager_init (GtkUIManager *self)
381 382 383 384
{
  guint merge_id;
  GNode *node;

385
  self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
386 387 388 389 390 391 392

  self->private_data->accel_group = gtk_accel_group_new ();

  self->private_data->root_node = NULL;
  self->private_data->action_groups = NULL;

  self->private_data->last_merge_id = 0;
393
  self->private_data->add_tearoffs = FALSE;
394

395
  merge_id = gtk_ui_manager_new_merge_id (self);
Matthias Clasen's avatar
Matthias Clasen committed
396 397
  node = get_child_node (self, NULL, "ui", 2,
			 NODE_TYPE_ROOT, TRUE, FALSE);
398
  node_prepend_ui_reference (NODE_INFO (node), merge_id, 0);
399 400
}

401 402 403 404 405 406 407 408 409 410
static void
gtk_ui_manager_finalize (GObject *object)
{
  GtkUIManager *self = GTK_UI_MANAGER (object);
  
  if (self->private_data->update_tag != 0)
    {
      g_source_remove (self->private_data->update_tag);
      self->private_data->update_tag = 0;
    }
Matthias Clasen's avatar
Matthias Clasen committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424
  
  g_node_traverse (self->private_data->root_node, 
		   G_POST_ORDER, G_TRAVERSE_ALL, -1,
		   (GNodeTraverseFunc)free_node, 0);
  g_node_destroy (self->private_data->root_node);
  self->private_data->root_node = NULL;
  
  g_list_foreach (self->private_data->action_groups,
                  (GFunc) g_object_unref, NULL);
  g_list_free (self->private_data->action_groups);
  self->private_data->action_groups = NULL;

  g_object_unref (self->private_data->accel_group);
  self->private_data->accel_group = NULL;
425 426

  G_OBJECT_CLASS (parent_class)->finalize (object);
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
static void
gtk_ui_manager_set_property (GObject         *object,
			     guint            prop_id,
			     const GValue    *value,
			     GParamSpec      *pspec)
{
  GtkUIManager *self = GTK_UI_MANAGER (object);
 
  switch (prop_id)
    {
    case PROP_ADD_TEAROFFS:
      gtk_ui_manager_set_add_tearoffs (self, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_ui_manager_get_property (GObject         *object,
			     guint            prop_id,
			     GValue          *value,
			     GParamSpec      *pspec)
{
  GtkUIManager *self = GTK_UI_MANAGER (object);

  switch (prop_id)
    {
    case PROP_ADD_TEAROFFS:
      g_value_set_boolean (value, self->private_data->add_tearoffs);
      break;
461 462 463
    case PROP_UI:
      g_value_set_string (value, gtk_ui_manager_get_ui (self));
      break;
464 465 466 467 468 469
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

470 471

/**
472
 * gtk_ui_manager_new:
473
 * 
Matthias Clasen's avatar
Matthias Clasen committed
474
 * Creates a new ui manager object.
475
 * 
Matthias Clasen's avatar
Matthias Clasen committed
476
 * Return value: a new ui manager object.
477 478 479
 *
 * Since: 2.4
 **/
480 481
GtkUIManager*
gtk_ui_manager_new (void)
482
{
483
  return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
484 485 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
/**
 * gtk_ui_manager_get_add_tearoffs:
 * @self: a #GtkUIManager
 * 
 * Returns whether menus generated by this #GtkUIManager
 * will have tearoff menu items. 
 * 
 * Return value: whether tearoff menu items are added
 *
 * Since: 2.4
 **/
gboolean 
gtk_ui_manager_get_add_tearoffs (GtkUIManager *self)
{
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), FALSE);
  
  return self->private_data->add_tearoffs;
}


/**
 * gtk_ui_manager_set_add_tearoffs:
 * @self: a #GtkUIManager
 * @add_tearoffs: whether tearoff menu items are added
 * 
512
 * Sets the "add_tearoffs" property, which controls whether menus 
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
 * generated by this #GtkUIManager will have tearoff menu items. 
 *
 * Note that this only affects regular menus. Generated popup 
 * menus never have tearoff menu items.
 *
 * Since: 2.4
 **/
void 
gtk_ui_manager_set_add_tearoffs (GtkUIManager *self,
				 gboolean      add_tearoffs)
{
  g_return_if_fail (GTK_IS_UI_MANAGER (self));

  add_tearoffs = add_tearoffs != FALSE;

  if (add_tearoffs != self->private_data->add_tearoffs)
    {
      self->private_data->add_tearoffs = add_tearoffs;
      
532
      dirty_all_nodes (self);
533 534 535 536 537

      g_object_notify (G_OBJECT (self), "add_tearoffs");
    }
}

538 539 540 541 542 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 570 571
static void
cb_proxy_connect_proxy (GtkActionGroup *group, 
                        GtkAction      *action,
                        GtkWidget      *proxy, 
                        GtkUIManager *self)
{
  g_signal_emit (self, ui_manager_signals[CONNECT_PROXY], 0, action, proxy);
}

static void
cb_proxy_disconnect_proxy (GtkActionGroup *group, 
                           GtkAction      *action,
                           GtkWidget      *proxy, 
                           GtkUIManager *self)
{
  g_signal_emit (self, ui_manager_signals[DISCONNECT_PROXY], 0, action, proxy);
}

static void
cb_proxy_pre_activate (GtkActionGroup *group, 
                       GtkAction      *action,
                       GtkUIManager   *self)
{
  g_signal_emit (self, ui_manager_signals[PRE_ACTIVATE], 0, action);
}

static void
cb_proxy_post_activate (GtkActionGroup *group, 
                        GtkAction      *action,
                        GtkUIManager   *self)
{
  g_signal_emit (self, ui_manager_signals[POST_ACTIVATE], 0, action);
}

572
/**
573 574
 * gtk_ui_manager_insert_action_group:
 * @self: a #GtkUIManager object
575
 * @action_group: the action group to be inserted
576
 * @pos: the position at which the group will be inserted.
577
 * 
578
 * Inserts an action group into the list of action groups associated 
579 580
 * with @self. Actions in earlier groups hide actions with the same 
 * name in later groups. 
581 582 583 584
 *
 * Since: 2.4
 **/
void
585
gtk_ui_manager_insert_action_group (GtkUIManager   *self,
586 587 588
				    GtkActionGroup *action_group, 
				    gint            pos)
{
589
  g_return_if_fail (GTK_IS_UI_MANAGER (self));
590
  g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
591 592
  g_return_if_fail (g_list_find (self->private_data->action_groups, 
				 action_group) == NULL);
593 594

  g_object_ref (action_group);
595 596
  self->private_data->action_groups = 
    g_list_insert (self->private_data->action_groups, action_group, pos);
597 598 599 600
  g_object_connect (action_group,
		    "object_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
		    "object_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
		    "object_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
601 602
		    "object_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self,
		    NULL);
603 604

  /* dirty all nodes, as action bindings may change */
605
  dirty_all_nodes (self);
606

607
  g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
608 609 610
}

/**
611 612
 * gtk_ui_manager_remove_action_group:
 * @self: a #GtkUIManager object
613 614
 * @action_group: the action group to be removed
 * 
615 616
 * Removes an action group from the list of action groups associated 
 * with @self.
617 618 619 620
 *
 * Since: 2.4
 **/
void
621
gtk_ui_manager_remove_action_group (GtkUIManager   *self,
622 623
				    GtkActionGroup *action_group)
{
624
  g_return_if_fail (GTK_IS_UI_MANAGER (self));
625 626 627 628 629 630
  g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
  g_return_if_fail (g_list_find (self->private_data->action_groups, 
				 action_group) != NULL);

  self->private_data->action_groups =
    g_list_remove (self->private_data->action_groups, action_group);
631 632 633 634 635 636

  g_object_disconnect (action_group,
                       "any_signal::connect_proxy", G_CALLBACK (cb_proxy_connect_proxy), self,
                       "any_signal::disconnect_proxy", G_CALLBACK (cb_proxy_disconnect_proxy), self,
                       "any_signal::pre_activate", G_CALLBACK (cb_proxy_pre_activate), self,
                       "any_signal::post_activate", G_CALLBACK (cb_proxy_post_activate), self, 
637
                       NULL);
638 639 640
  g_object_unref (action_group);

  /* dirty all nodes, as action bindings may change */
641
  dirty_all_nodes (self);
642

643
  g_signal_emit (self, ui_manager_signals[ACTIONS_CHANGED], 0);
644 645 646
}

/**
647 648
 * gtk_ui_manager_get_action_groups:
 * @self: a #GtkUIManager object
649 650 651 652 653 654 655 656 657
 * 
 * Returns the list of action groups associated with @self.
 *
 * Return value: a #GList of action groups. The list is owned by GTK+ 
 *   and should not be modified.
 *
 * Since: 2.4
 **/
GList *
Matthias Clasen's avatar
Matthias Clasen committed
658
gtk_ui_manager_get_action_groups (GtkUIManager *self)
659
{
660
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
661 662 663 664 665

  return self->private_data->action_groups;
}

/**
666 667
 * gtk_ui_manager_get_accel_group:
 * @self: a #GtkUIManager object
668 669 670 671 672 673 674 675
 * 
 * Returns the #GtkAccelGroup associated with @self.
 *
 * Return value: the #GtkAccelGroup.
 *
 * Since: 2.4
 **/
GtkAccelGroup *
Matthias Clasen's avatar
Matthias Clasen committed
676
gtk_ui_manager_get_accel_group (GtkUIManager *self)
677
{
678
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
679 680 681 682 683

  return self->private_data->accel_group;
}

/**
684 685
 * gtk_ui_manager_get_widget:
 * @self: a #GtkUIManager
686 687
 * @path: a path
 * 
Matthias Clasen's avatar
Matthias Clasen committed
688 689 690 691
 * Looks up a widget by following a path. 
 * The path consists of the names specified in the XML description of the UI. 
 * separated by '/'. Elements which don't have a name or action attribute in 
 * the XML (e.g. &lt;popup&gt;) can be addressed by their XML element name 
692
 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
693 694 695
 *
 * Note that the widget found by following a path that ends in a &lt;menu&gt;
 * element is the menuitem to which the menu is attached, not the menu itself.
696 697 698 699 700 701 702
 * 
 * Return value: the widget found by following the path, or %NULL if no widget
 *   was found.
 *
 * Since: 2.4
 **/
GtkWidget *
703
gtk_ui_manager_get_widget (GtkUIManager *self, 
704 705 706 707
			   const gchar  *path)
{
  GNode *node;

708
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
709
  g_return_val_if_fail (path != NULL, NULL);
710

711 712
  /* ensure that there are no pending updates before we get the
   * widget */
713
  gtk_ui_manager_ensure_update (self);
714

715
  node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
716 717 718 719

  if (node == NULL)
    return NULL;

720
  return NODE_INFO (node)->proxy;
721 722
}

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
static void
collect_toplevels (GNode   *node, 
		   gpointer user_data)
{
  struct {
    GtkUIManagerItemType types;
    GSList *list;
  } *data = user_data;

  switch (NODE_INFO (node)->type) {
  case NODE_TYPE_MENUBAR:
    if (data->types & GTK_UI_MANAGER_MENUBAR)
      data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
    break;
  case NODE_TYPE_TOOLBAR:
    if (data->types & GTK_UI_MANAGER_TOOLBAR)
      data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
    break;
  case NODE_TYPE_POPUP:
    if (data->types & GTK_UI_MANAGER_POPUP)
      data->list = g_slist_prepend (data->list, NODE_INFO (node)->proxy);
    break;
  default: ;
  }
}

/**
 * gtk_ui_manager_get_toplevels:
 * @self: a #GtkUIManager
 * @types: specifies the types of toplevel widgets to include. Allowed
 *   types are #GTK_UI_MANAGER_MENUBAR, #GTK_UI_MANAGER_TOOLBAR and
 *   #GTK_UI_MANAGER_POPUP.
 * 
 * Obtains a list of all toplevel widgets of the requested types.
 * 
758 759
 * Return value: a newly-allocated of all toplevel widgets of the requested 
 * types. 
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
 *
 * Since: 2.4
 **/
GSList *
gtk_ui_manager_get_toplevels (GtkUIManager         *self,
			      GtkUIManagerItemType  types)
{
  struct {
    GtkUIManagerItemType types;
    GSList *list;
  } data;

  g_return_val_if_fail ((~(GTK_UI_MANAGER_MENUBAR | 
			   GTK_UI_MANAGER_TOOLBAR |
			   GTK_UI_MANAGER_POPUP) & types) == 0, NULL);
  
      
  data.types = types;
  data.list = NULL;

  g_node_children_foreach (self->private_data->root_node, 
			   G_TRAVERSE_ALL, 
			   collect_toplevels, &data);

  return data.list;
}


788 789 790 791 792
/**
 * gtk_ui_manager_get_action:
 * @self: a #GtkUIManager
 * @path: a path
 * 
Matthias Clasen's avatar
Matthias Clasen committed
793 794
 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
 * for more information about paths.
795 796 797 798 799 800 801
 * 
 * Return value: the action whose proxy widget is found by following the path, 
 *     or %NULL if no widget was found.
 *
 * Since: 2.4
 **/
GtkAction *           
Matthias Clasen's avatar
Matthias Clasen committed
802 803
gtk_ui_manager_get_action (GtkUIManager *self,
			   const gchar  *path)
804 805 806 807
{
  GNode *node;

  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
808
  g_return_val_if_fail (path != NULL, NULL);
809 810 811 812 813
  
  /* ensure that there are no pending updates before we get
   * the action */
  gtk_ui_manager_ensure_update (self);
  
814
  node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
815 816 817 818 819 820 821

  if (node == NULL)
    return NULL;

  return NODE_INFO (node)->action;
}

822
static GNode *
Matthias Clasen's avatar
Matthias Clasen committed
823 824 825 826 827 828 829
get_child_node (GtkUIManager *self, 
		GNode        *parent,
		const gchar  *childname, 
		gint          childname_length,
		NodeType      node_type,
		gboolean      create, 
		gboolean      top)
830 831 832 833
{
  GNode *child = NULL;

  g_return_val_if_fail (parent == NULL ||
834 835
			(NODE_INFO (parent)->type != NODE_TYPE_MENUITEM &&
			 NODE_INFO (parent)->type != NODE_TYPE_TOOLITEM), 
836 837 838 839 840 841 842 843
			NULL);

  if (parent)
    {
      if (childname)
	{
	  for (child = parent->children; child != NULL; child = child->next)
	    {
844 845
	      if (NODE_INFO (child)->name &&
		  strlen (NODE_INFO (child)->name) == childname_length &&
846 847 848
		  !strncmp (NODE_INFO (child)->name, childname, childname_length))
		{
		  /* if undecided about node type, set it */
849
		  if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
850 851 852
		    NODE_INFO (child)->type = node_type;
		  
		  /* warn about type mismatch */
853 854
		  if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
		      node_type != NODE_TYPE_UNDECIDED &&
855 856 857 858 859 860 861 862 863 864 865 866
		      NODE_INFO (child)->type != node_type)
		    g_warning ("node type doesn't match %d (%s is type %d)",
			       node_type, 
			       NODE_INFO (child)->name,
			       NODE_INFO (child)->type);
		  
		  return child;
		}
	    }
	}
      if (!child && create)
	{
867
	  Node *mnode;
868
	  
869
	  mnode = g_chunk_new0 (Node, merge_node_chunk);
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	  mnode->type = node_type;
	  mnode->name = g_strndup (childname, childname_length);
	  mnode->dirty = TRUE;

	  if (top)
	    child = g_node_prepend_data (parent, mnode);
	  else
	    child = g_node_append_data (parent, mnode);
	}
    }
  else
    {
      /* handle root node */
      if (self->private_data->root_node)
	{
	  child = self->private_data->root_node;
	  if (strncmp (NODE_INFO (child)->name, childname, childname_length) != 0)
	    g_warning ("root node name '%s' doesn't match '%s'",
		       childname, NODE_INFO (child)->name);
889
	  if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
890 891 892 893
	    g_warning ("base element must be of type ROOT");
	}
      else if (create)
	{
894
	  Node *mnode;
895

896
	  mnode = g_chunk_new0 (Node, merge_node_chunk);
897 898 899 900 901 902 903 904 905 906 907 908
	  mnode->type = node_type;
	  mnode->name = g_strndup (childname, childname_length);
	  mnode->dirty = TRUE;
	  
	  child = self->private_data->root_node = g_node_new (mnode);
	}
    }

  return child;
}

static GNode *
Matthias Clasen's avatar
Matthias Clasen committed
909 910 911 912
get_node (GtkUIManager *self, 
	  const gchar  *path,
	  NodeType      node_type, 
	  gboolean      create)
913 914 915 916
{
  const gchar *pos, *end;
  GNode *parent, *node;
  
917 918 919
  if (strncmp ("/ui", path, 3) == 0)
    path += 3;
  
920 921 922 923 924 925 926 927 928 929 930 931 932 933
  end = path + strlen (path);
  pos = path;
  parent = node = NULL;
  while (pos < end)
    {
      const gchar *slash;
      gsize length;

      slash = strchr (pos, '/');
      if (slash)
	length = slash - pos;
      else
	length = strlen (pos);

934
      node = get_child_node (self, parent, pos, length, NODE_TYPE_UNDECIDED,
935 936 937
			     create, FALSE);
      if (!node)
	return NULL;
938

939 940 941 942
      pos += length + 1; /* move past the node name and the slash too */
      parent = node;
    }

943
  if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
944
    NODE_INFO (node)->type = node_type;
Matthias Clasen's avatar
Matthias Clasen committed
945

946 947 948
  return node;
}

Matthias Clasen's avatar
Matthias Clasen committed
949 950 951 952 953 954 955 956 957 958
static gboolean
free_node (GNode *node)
{
  Node *info = NODE_INFO (node);
  
  g_list_foreach (info->uifiles, (GFunc) g_free, NULL);
  g_list_free (info->uifiles);

  if (info->action)
    g_object_unref (info->action);
959 960 961 962
  if (info->proxy)
    g_object_unref (info->proxy);
  if (info->extra)
    g_object_unref (info->extra);
Matthias Clasen's avatar
Matthias Clasen committed
963 964 965 966 967 968
  g_free (info->name);
  g_chunk_free (info, merge_node_chunk);

  return FALSE;
}

969 970 971 972 973 974 975 976 977 978 979 980 981
/**
 * gtk_ui_manager_new_merge_id:
 * @self: a #GtkUIManager
 * 
 * Returns an unused merge id, suitable for use with 
 * gtk_ui_manager_add_ui().
 * 
 * Return value: an unused merge id.
 *
 * Since: 2.4
 **/
guint
gtk_ui_manager_new_merge_id (GtkUIManager *self)
982 983 984 985 986 987 988
{
  self->private_data->last_merge_id++;

  return self->private_data->last_merge_id;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
989 990 991
node_prepend_ui_reference (Node   *node,
			   guint   merge_id, 
			   GQuark  action_quark)
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
{
  NodeUIReference *reference;

  reference = g_new (NodeUIReference, 1);
  reference->action_quark = action_quark;
  reference->merge_id = merge_id;

  /* Prepend the reference */
  node->uifiles = g_list_prepend (node->uifiles, reference);

  node->dirty = TRUE;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1006 1007
node_remove_ui_reference (Node  *node,
			  guint  merge_id)
1008 1009 1010 1011 1012 1013 1014 1015 1016
{
  GList *p;
  
  for (p = node->uifiles; p != NULL; p = p->next)
    {
      NodeUIReference *reference = p->data;
      
      if (reference->merge_id == merge_id)
	{
1017
	  node->uifiles = g_list_delete_link (node->uifiles, p);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	  node->dirty = TRUE;
	  g_free (reference);

	  break;
	}
    }
}

/* -------------------- The UI file parser -------------------- */

typedef enum
{
  STATE_START,
  STATE_ROOT,
  STATE_MENU,
  STATE_TOOLBAR,
  STATE_MENUITEM,
  STATE_TOOLITEM,
1036
  STATE_ACCELERATOR,
1037 1038 1039 1040 1041 1042 1043 1044 1045
  STATE_END
} ParseState;

typedef struct _ParseContext ParseContext;
struct _ParseContext
{
  ParseState state;
  ParseState prev_state;

1046
  GtkUIManager *self;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

  GNode *current;

  guint merge_id;
};

static void
start_element_handler (GMarkupParseContext *context,
		       const gchar         *element_name,
		       const gchar        **attribute_names,
		       const gchar        **attribute_values,
		       gpointer             user_data,
		       GError             **error)
{
  ParseContext *ctx = user_data;
1062
  GtkUIManager *self = ctx->self;
1063 1064 1065

  gint i;
  const gchar *node_name;
1066
  const gchar *action;
1067
  GQuark action_quark;
1068
  gboolean top;
1069
  
1070 1071
  gboolean raise_error = TRUE;

1072 1073
  node_name = NULL;
  action = NULL;
1074
  action_quark = 0;
1075
  top = FALSE;
1076

1077 1078 1079 1080 1081 1082
  for (i = 0; attribute_names[i] != NULL; i++)
    {
      if (!strcmp (attribute_names[i], "name"))
	{
	  node_name = attribute_values[i];
	}
1083
      else if (!strcmp (attribute_names[i], "action"))
1084
	{
1085
	  action = attribute_values[i];
1086
	  action_quark = g_quark_from_string (attribute_values[i]);
1087
	}
1088
      else if (!strcmp (attribute_names[i], "position"))
1089 1090 1091
	{
	  top = !strcmp (attribute_values[i], "top");
	}
Matthias Clasen's avatar
Matthias Clasen committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
      else
	{
	  gint line_number, char_number;
	  
	  g_markup_parse_context_get_position (context,
					       &line_number, &char_number);
	  g_set_error (error,
		       G_MARKUP_ERROR,
		       G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
		       _("Unknown attribute '%s' on line %d char %d"),
		       attribute_names[i],
		       line_number, char_number);
	  return;
	}
1106
    }
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

  /* Work out a name for this node.  Either the name attribute, or
   * the action, or the element name */
  if (node_name == NULL) 
    {
      if (action != NULL)
	node_name = action;
      else 
	node_name = element_name;
    }
1117 1118 1119

  switch (element_name[0])
    {
1120 1121 1122
    case 'a':
      if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
	{
1123
	  ctx->state = STATE_ACCELERATOR;
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	  ctx->current = get_child_node (self, ctx->current,
					 node_name, strlen (node_name),
					 NODE_TYPE_ACCELERATOR,
					 TRUE, FALSE);
	  if (NODE_INFO (ctx->current)->action_name == 0)
	    NODE_INFO (ctx->current)->action_name = action_quark;

	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
	  NODE_INFO (ctx->current)->dirty = TRUE;

	  raise_error = FALSE;
	}
      break;
1138 1139
    case 'u':
      if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
1140 1141 1142 1143 1144
	{
	  ctx->state = STATE_ROOT;
	  ctx->current = self->private_data->root_node;
	  raise_error = FALSE;

1145 1146
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
1147 1148 1149
	}
      break;
    case 'm':
1150
      if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
1151 1152 1153 1154
	{
	  ctx->state = STATE_MENU;
	  ctx->current = get_child_node (self, ctx->current,
					 node_name, strlen (node_name),
1155
					 NODE_TYPE_MENUBAR,
1156 1157
					 TRUE, FALSE);
	  if (NODE_INFO (ctx->current)->action_name == 0)
1158
	    NODE_INFO (ctx->current)->action_name = action_quark;
1159

1160 1161
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
1162 1163
	  NODE_INFO (ctx->current)->dirty = TRUE;

1164 1165 1166 1167 1168 1169
	  raise_error = FALSE;
	}
      else if (ctx->state == STATE_MENU && !strcmp (element_name, "menu"))
	{
	  ctx->current = get_child_node (self, ctx->current,
					 node_name, strlen (node_name),
1170
					 NODE_TYPE_MENU,
1171 1172 1173 1174
					 TRUE, top);
	  if (NODE_INFO (ctx->current)->action_name == 0)
	    NODE_INFO (ctx->current)->action_name = action_quark;

1175 1176
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
1177 1178
	  NODE_INFO (ctx->current)->dirty = TRUE;
	  
1179 1180 1181 1182 1183 1184 1185 1186 1187
	  raise_error = FALSE;
	}
      else if (ctx->state == STATE_MENU && !strcmp (element_name, "menuitem"))
	{
	  GNode *node;

	  ctx->state = STATE_MENUITEM;
	  node = get_child_node (self, ctx->current,
				 node_name, strlen (node_name),
1188
				 NODE_TYPE_MENUITEM,
1189 1190
				 TRUE, top);
	  if (NODE_INFO (node)->action_name == 0)
1191
	    NODE_INFO (node)->action_name = action_quark;
1192
	  
1193 1194
	  node_prepend_ui_reference (NODE_INFO (node),
				     ctx->merge_id, action_quark);
1195 1196 1197 1198 1199 1200
	  NODE_INFO (node)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      break;
    case 'p':
1201
      if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
1202 1203 1204 1205
	{
	  ctx->state = STATE_MENU;
	  ctx->current = get_child_node (self, ctx->current,
					 node_name, strlen (node_name),
1206
					 NODE_TYPE_POPUP,
1207 1208
					 TRUE, FALSE);
	  if (NODE_INFO (ctx->current)->action_name == 0)
1209
	    NODE_INFO (ctx->current)->action_name = action_quark;
1210
	  
1211 1212
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
1213 1214 1215 1216 1217 1218 1219
	  NODE_INFO (ctx->current)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
	       !strcmp (element_name, "placeholder"))
	{
1220
	  if (ctx->state == STATE_TOOLBAR)
1221 1222
	    ctx->current = get_child_node (self, ctx->current,
					   node_name, strlen (node_name),
1223
					   NODE_TYPE_TOOLBAR_PLACEHOLDER,
1224 1225 1226 1227
					   TRUE, top);
	  else
	    ctx->current = get_child_node (self, ctx->current,
					   node_name, strlen (node_name),
1228
					   NODE_TYPE_MENU_PLACEHOLDER,
1229 1230
					   TRUE, top);
	  
1231 1232
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
1233 1234 1235 1236 1237 1238
	  NODE_INFO (ctx->current)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      break;
    case 's':
1239
      if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1240
	  !strcmp (element_name, "separator"))
1241 1242
	{
	  GNode *node;
1243
	  gint length;
1244

1245
	  if (ctx->state == STATE_TOOLBAR)
1246
	    ctx->state = STATE_TOOLITEM;
1247 1248
	  else
	    ctx->state = STATE_MENUITEM;
1249 1250 1251 1252 1253 1254 1255
	  if (!strcmp (node_name, "separator"))
	    {
	      node_name = NULL;
	      length = -1;
	    }
	  else
	    length = strlen (node_name);
1256
	  node = get_child_node (self, ctx->current,
1257
				 node_name, length,
1258
				 NODE_TYPE_SEPARATOR,
1259
				 TRUE, top);
1260

1261
	  if (NODE_INFO (node)->action_name == 0)
1262
	    NODE_INFO (node)->action_name = action_quark;
1263

1264 1265
	  node_prepend_ui_reference (NODE_INFO (node),
				     ctx->merge_id, action_quark);
1266 1267 1268 1269 1270 1271
	  NODE_INFO (node)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      break;
    case 't':
1272 1273 1274 1275 1276
      if (ctx->state == STATE_ROOT && !strcmp (element_name, "toolbar"))
	{
	  ctx->state = STATE_TOOLBAR;
	  ctx->current = get_child_node (self, ctx->current,
					 node_name, strlen (node_name),
1277
					 NODE_TYPE_TOOLBAR,
1278 1279 1280 1281
					 TRUE, FALSE);
	  if (NODE_INFO (ctx->current)->action_name == 0)
	    NODE_INFO (ctx->current)->action_name = action_quark;
	  
1282 1283
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
1284 1285 1286 1287 1288
	  NODE_INFO (ctx->current)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1289 1290 1291 1292 1293 1294
	{
	  GNode *node;

	  ctx->state = STATE_TOOLITEM;
	  node = get_child_node (self, ctx->current,
				node_name, strlen (node_name),
1295
				 NODE_TYPE_TOOLITEM,
1296 1297
				 TRUE, top);
	  if (NODE_INFO (node)->action_name == 0)
1298
	    NODE_INFO (node)->action_name = action_quark;
1299
	  
1300 1301
	  node_prepend_ui_reference (NODE_INFO (node),
				     ctx->merge_id, action_quark);
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
	  NODE_INFO (node)->dirty = TRUE;

	  raise_error = FALSE;
	}
      break;
    default:
      break;
    }
  if (raise_error)
    {
      gint line_number, char_number;
 
      g_markup_parse_context_get_position (context,
					   &line_number, &char_number);
Matthias Clasen's avatar
Matthias Clasen committed
1316 1317 1318 1319 1320 1321
      g_set_error (error,
		   G_MARKUP_ERROR,
		   G_MARKUP_ERROR_UNKNOWN_ELEMENT,
		   _("Unexpected start tag '%s' on line %d char %d"),
		   element_name,
		   line_number, char_number);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
    }
}

static void
end_element_handler (GMarkupParseContext *context,
		     const gchar         *element_name,
		     gpointer             user_data,
		     GError             **error)
{
  ParseContext *ctx = user_data;

  switch (ctx->state)
    {
    case STATE_START:
Matthias Clasen's avatar
Matthias Clasen committed
1336 1337
    case STATE_END:
      /* no need to GError here, GMarkup already catches this */
1338 1339 1340 1341 1342 1343
      break;
    case STATE_ROOT:
      ctx->current = NULL;
      ctx->state = STATE_END;
      break;
    case STATE_MENU:
Matthias Clasen's avatar
Matthias Clasen committed
1344
    case STATE_TOOLBAR:
1345
    case STATE_ACCELERATOR:
1346
      ctx->current = ctx->current->parent;
1347
      if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT) 
1348
	ctx->state = STATE_ROOT;
1349
      /* else, stay in same state */
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
      break;
    case STATE_MENUITEM:
      ctx->state = STATE_MENU;
      break;
    case STATE_TOOLITEM:
      ctx->state = STATE_TOOLBAR;
      break;
    }
}

static void
cleanup (GMarkupParseContext *context,
	 GError              *error,
	 gpointer             user_data)
{
  ParseContext *ctx = user_data;

  ctx->current = NULL;
  /* should also walk through the tree and get rid of nodes related to
   * this UI file's tag */

Matthias Clasen's avatar
Matthias Clasen committed
1371
  gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1372 1373
}

1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
static gboolean
xml_isspace (char c)
{
  return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

static void 
text_handler (GMarkupParseContext *context,
	      const gchar         *text,
	      gsize                text_len,  
	      gpointer             user_data,
	      GError             **error)
{
  const gchar *p;
  const gchar *end;

  p = text;
  end = text + text_len;
  while (p != end && xml_isspace (*p))
    ++p;
  
  if (p != end)
    {
      gint line_number, char_number;
      
      g_markup_parse_context_get_position (context,
					   &line_number, &char_number);
      g_set_error (error,
		   G_MARKUP_ERROR,
		   G_MARKUP_ERROR_INVALID_CONTENT,
		   _("Unexpected character data on line %d char %d"),
		   line_number, char_number);
    }
}


1410 1411 1412
static GMarkupParser ui_parser = {
  start_element_handler,
  end_element_handler,
1413
  text_handler,
1414 1415 1416 1417
  NULL,
  cleanup
};

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
static guint
add_ui_from_string (GtkUIManager *self,
		    const gchar  *buffer, 
		    gssize        length,
		    gboolean      needs_root,
		    GError      **error)
{
  ParseContext ctx = { 0 };
  GMarkupParseContext *context;

  ctx.state = STATE_START;
  ctx.self = self;
  ctx.current = NULL;
1431
  ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1432 1433 1434 1435 1436

  context = g_markup_parse_context_new (&ui_parser, 0, &ctx, NULL);

  if (needs_root)
    if (!g_markup_parse_context_parse (context, "<ui>", -1, error))
1437
      goto out;
1438 1439

  if (!g_markup_parse_context_parse (context, buffer, length, error))
1440
    goto out;
1441 1442 1443

  if (needs_root)
    if (!g_markup_parse_context_parse (context, "</ui>", -1, error))
1444
      goto out;
1445 1446

  if (!g_markup_parse_context_end_parse (context, error))
1447
    goto out;
1448 1449 1450

  g_markup_parse_context_free (context);

1451
  queue_update (self);
1452

1453
  g_object_notify (G_OBJECT (self), "ui");      
1454

1455 1456
  return ctx.merge_id;

1457
 out:
1458 1459 1460 1461 1462 1463

  g_markup_parse_context_free (context);

  return 0;
}

1464
/**
1465 1466
 * gtk_ui_manager_add_ui_from_string:
 * @self: a #GtkUIManager object
1467 1468 1469 1470
 * @buffer: the string to parse
 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
 * @error: return location for an error
 * 
1471 1472
 * Parses a string containing a <link linkend="XML-UI">UI definition</link> and 
 * merges it with the current contents of @self. An enclosing &lt;ui&gt; 
Matthias Clasen's avatar
Matthias Clasen committed
1473
 * element is added if it is missing.
1474 1475
 * 
 * Return value: The merge id for the merged UI. The merge id can be used
1476
 *   to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1477 1478 1479 1480 1481
 *   the return value is 0.
 *
 * Since: 2.4
 **/
guint
1482
gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1483
				   const gchar  *buffer, 
1484
				   gssize        length,
1485 1486
				   GError      **error)
{
1487 1488 1489
  gboolean needs_root = TRUE;
  const gchar *p;
  const gchar *end;
1490

1491 1492
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
  g_return_val_if_fail (buffer != NULL, 0);
1493 1494 1495 1496

  if (length < 0)
    length = strlen (buffer);

1497 1498 1499 1500
  p = buffer;
  end = buffer + length;
  while (p != end && xml_isspace (*p))
    ++p;
1501

1502 1503 1504 1505
  if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
    needs_root = FALSE;
  
  return add_ui_from_string (self, buffer, length, needs_root, error);
1506 1507 1508
}

/**
1509 1510
 * gtk_ui_manager_add_ui_from_file:
 * @self: a #GtkUIManager object
1511 1512 1513
 * @filename: the name of the file to parse 
 * @error: return location for an error
 * 
1514
 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and 
Matthias Clasen's avatar
Matthias Clasen committed
1515
 * merges it with the current contents of @self. 
1516 1517
 * 
 * Return value: The merge id for the merged UI. The merge id can be used
1518
 *   to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1519 1520 1521 1522 1523
 *   the return value is 0.
 *
 * Since: 2.4
 **/
guint
1524
gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1525 1526 1527 1528
				 const gchar  *filename,
				 GError      **error)
{
  gchar *buffer;
1529
  gsize length;
1530 1531
  guint res;

1532 1533
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);

1534 1535 1536
  if (!g_file_get_contents (filename, &buffer, &length, error))
    return 0;

1537
  res = add_ui_from_string (self, buffer, length, FALSE, error);
1538 1539 1540 1541 1542
  g_free (buffer);

  return res;
}

1543 1544 1545 1546 1547 1548 1549
/**
 * gtk_ui_manager_add_ui:
 * @self: a #GtkUIManager
 * @merge_id: the merge id for the merged UI, see gtk_ui_manager_new_merge_id()
 * @path: a path
 * @name: the name for the added UI element
 * @action: the name of the action to be proxied, or %NULL to add a separator
1550 1551 1552
 * @type: the type of UI element to add.
 * @top: if %TRUE, the UI element is added before its siblings, otherwise it 
 *   is added after its siblings.
1553
 * 
1554 1555 1556 1557 1558 1559
 * Adds a UI element to the current contents of @self. 
 *
 * If @type is %GTK_UI_MANAGER_AUTO, GTK+ inserts a menuitem, toolitem or 
 * separator if such an element can be inserted at the place determined by 
 * @path. Otherwise @type must indicate an element that can be inserted at 
 * the place determined by @path.
1560 1561 1562 1563
 * 
 * Since: 2.4
 **/
void
1564 1565 1566 1567 1568 1569 1570
gtk_ui_manager_add_ui (GtkUIManager        *self,
		       guint                merge_id,
		       const gchar         *path,
		       const gchar         *name,
		       const gchar         *action,
		       GtkUIManagerItemType type,
		       gboolean             top)
1571 1572 1573
{
  GNode *node;
  GNode *child;
1574
  NodeType node_type;
1575 1576 1577 1578
  GQuark action_quark = 0;

  g_return_if_fail (GTK_IS_UI_MANAGER (self));  
  g_return_if_fail (merge_id > 0);
1579
  g_return_if_fail (name != NULL);
1580

1581
  node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1582 1583 1584 1585
  
  if (node == NULL)
    return;

1586 1587
  node_type = NODE_TYPE_UNDECIDED;

1588 1589
  switch (NODE_INFO (node)->type) 
    {
1590
    case NODE_TYPE_MENUBAR:
1591 1592 1593
    case NODE_TYPE_MENU:
    case NODE_TYPE_POPUP:
    case NODE_TYPE_MENU_PLACEHOLDER:
1594 1595 1596 1597