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

45
#undef DEBUG_UI_MANAGER
46 47 48

typedef enum 
{
49 50 51 52 53 54 55 56 57 58 59
  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,
60
  NODE_TYPE_ACCELERATOR
61
} NodeType;
62

63
typedef struct _Node Node;
64

65 66
struct _Node {
  NodeType type;
67

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

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

  GList *uifiles;

  guint dirty : 1;
};

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

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

  GNode *root_node;
  GList *action_groups;

  guint last_merge_id;

  guint update_tag;  
92 93

  gboolean add_tearoffs;
94 95
};

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

typedef struct _NodeUIReference NodeUIReference;

struct _NodeUIReference 
{
  guint merge_id;
  GQuark action_quark;
};

106 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
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);

137 138 139 140

enum 
{
  ADD_WIDGET,
141
  ACTIONS_CHANGED,
142 143 144
  LAST_SIGNAL
};

145 146 147
enum
{
  PROP_0,
148 149
  PROP_ADD_TEAROFFS,
  PROP_UI
150 151
};

152 153 154 155 156
static guint merge_signals[LAST_SIGNAL] = { 0 };

static GMemChunk *merge_node_chunk = NULL;

GType
157
gtk_ui_manager_get_type (void)
158 159 160 161 162 163 164
{
  static GtkType type = 0;

  if (!type)
    {
      static const GTypeInfo type_info =
      {
165
        sizeof (GtkUIManagerClass),
166 167
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
168
        (GClassInitFunc) gtk_ui_manager_class_init,
169 170 171
        (GClassFinalizeFunc) NULL,
        NULL,
        
172
        sizeof (GtkUIManager),
173
        0, /* n_preallocs */
174
        (GInstanceInitFunc) gtk_ui_manager_init,
175 176 177
      };

      type = g_type_register_static (G_TYPE_OBJECT,
178
				     "GtkUIManager",
179 180 181 182 183 184
				     &type_info, 0);
    }
  return type;
}

static void
185
gtk_ui_manager_class_init (GtkUIManagerClass *klass)
186 187 188 189 190 191
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);

  if (!merge_node_chunk)
192
    merge_node_chunk = g_mem_chunk_create (Node, 64,
193 194
					   G_ALLOC_AND_FREE);

195
  gobject_class->finalize = gtk_ui_manager_finalize;
196 197
  gobject_class->set_property = gtk_ui_manager_set_property;
  gobject_class->get_property = gtk_ui_manager_get_property;
198
  
199 200 201
  /**
   * GtkUIManager:add-tearoffs:
   *
202
   * The "add-tearoffs" property controls whether generated menus 
203 204 205 206 207 208 209 210 211 212 213 214 215
   * 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",
							 _("Add tearoffs to menus"),
							 _("Whether tearoff menu items should be added to menus"),
                                                         FALSE,
216 217 218 219 220 221 222 223 224 225 226
							 G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
				   PROP_UI,
				   g_param_spec_string ("ui",
 							_("Merged UI definition"),
							_("An XML string describing the merged UI"),
							NULL,
							G_PARAM_READABLE));


227 228 229 230 231 232 233 234 235 236 237
  /**
   * 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
   */
238 239 240 241
  merge_signals[ADD_WIDGET] =
    g_signal_new ("add_widget",
		  G_OBJECT_CLASS_TYPE (klass),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
242
		  G_STRUCT_OFFSET (GtkUIManagerClass, add_widget), NULL, NULL,
243
		  g_cclosure_marshal_VOID__OBJECT,
244
		  G_TYPE_NONE, 1, 
245
		  GTK_TYPE_WIDGET);
246

247
  /**
248
   * GtkUIManager::actions-changed:
249 250
   * @merge: a #GtkUIManager
   *
251 252
   * The "actions-changed" signal is emitted whenever the set of actions
   * changes.
253 254 255
   *
   * Since: 2.4
   */
256 257
  merge_signals[ACTIONS_CHANGED] =
    g_signal_new ("actions_changed",
258 259
		  G_OBJECT_CLASS_TYPE (klass),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
260 261
		  G_STRUCT_OFFSET (GtkUIManagerClass, actions_changed),  
		  NULL, NULL,
262 263
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
264
  
265
  g_type_class_add_private (gobject_class, sizeof (GtkUIManagerPrivate));
266 267 268 269
}


static void
270
gtk_ui_manager_init (GtkUIManager *self)
271 272 273 274
{
  guint merge_id;
  GNode *node;

275
  self->private_data = GTK_UI_MANAGER_GET_PRIVATE (self);
276 277 278 279 280 281 282

  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;
283
  self->private_data->add_tearoffs = FALSE;
284

285
  merge_id = gtk_ui_manager_new_merge_id (self);
Matthias Clasen's avatar
Matthias Clasen committed
286 287
  node = get_child_node (self, NULL, "ui", 2,
			 NODE_TYPE_ROOT, TRUE, FALSE);
288
  node_prepend_ui_reference (NODE_INFO (node), merge_id, 0);
289 290
}

291 292 293 294 295 296 297 298 299 300
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
301 302 303 304 305 306 307 308 309 310 311 312 313 314
  
  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;
315 316
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
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;
349 350 351
    case PROP_UI:
      g_value_set_string (value, gtk_ui_manager_get_ui (self));
      break;
352 353 354 355 356 357
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

358 359

/**
360
 * gtk_ui_manager_new:
361
 * 
Matthias Clasen's avatar
Matthias Clasen committed
362
 * Creates a new ui manager object.
363
 * 
Matthias Clasen's avatar
Matthias Clasen committed
364
 * Return value: a new ui manager object.
365 366 367
 *
 * Since: 2.4
 **/
368 369
GtkUIManager*
gtk_ui_manager_new (void)
370
{
371
  return g_object_new (GTK_TYPE_UI_MANAGER, NULL);
372 373 374
}


375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
/**
 * 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
 * 
400
 * Sets the "add_tearoffs" property, which controls whether menus 
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
 * 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;
      
420
      dirty_all_nodes (self);
421 422 423 424 425

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

426
/**
427 428
 * gtk_ui_manager_insert_action_group:
 * @self: a #GtkUIManager object
429
 * @action_group: the action group to be inserted
430
 * @pos: the position at which the group will be inserted.
431
 * 
432
 * Inserts an action group into the list of action groups associated 
433 434
 * with @self. Actions in earlier groups hide actions with the same 
 * name in later groups. 
435 436 437 438
 *
 * Since: 2.4
 **/
void
439
gtk_ui_manager_insert_action_group (GtkUIManager   *self,
440 441 442
				    GtkActionGroup *action_group, 
				    gint            pos)
{
443
  g_return_if_fail (GTK_IS_UI_MANAGER (self));
444
  g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
445 446
  g_return_if_fail (g_list_find (self->private_data->action_groups, 
				 action_group) == NULL);
447 448

  g_object_ref (action_group);
449 450
  self->private_data->action_groups = 
    g_list_insert (self->private_data->action_groups, action_group, pos);
451 452

  /* dirty all nodes, as action bindings may change */
453
  dirty_all_nodes (self);
454 455

  g_signal_emit (self, merge_signals[ACTIONS_CHANGED], 0);
456 457 458
}

/**
459 460
 * gtk_ui_manager_remove_action_group:
 * @self: a #GtkUIManager object
461 462
 * @action_group: the action group to be removed
 * 
463 464
 * Removes an action group from the list of action groups associated 
 * with @self.
465 466 467 468
 *
 * Since: 2.4
 **/
void
469
gtk_ui_manager_remove_action_group (GtkUIManager   *self,
470 471
				    GtkActionGroup *action_group)
{
472
  g_return_if_fail (GTK_IS_UI_MANAGER (self));
473 474 475 476 477 478 479 480 481
  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);
  g_object_unref (action_group);

  /* dirty all nodes, as action bindings may change */
482
  dirty_all_nodes (self);
483 484

  g_signal_emit (self, merge_signals[ACTIONS_CHANGED], 0);
485 486 487
}

/**
488 489
 * gtk_ui_manager_get_action_groups:
 * @self: a #GtkUIManager object
490 491 492 493 494 495 496 497 498
 * 
 * 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
499
gtk_ui_manager_get_action_groups (GtkUIManager *self)
500
{
501
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
502 503 504 505 506

  return self->private_data->action_groups;
}

/**
507 508
 * gtk_ui_manager_get_accel_group:
 * @self: a #GtkUIManager object
509 510 511 512 513 514 515 516
 * 
 * Returns the #GtkAccelGroup associated with @self.
 *
 * Return value: the #GtkAccelGroup.
 *
 * Since: 2.4
 **/
GtkAccelGroup *
Matthias Clasen's avatar
Matthias Clasen committed
517
gtk_ui_manager_get_accel_group (GtkUIManager *self)
518
{
519
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
520 521 522 523 524

  return self->private_data->accel_group;
}

/**
525 526
 * gtk_ui_manager_get_widget:
 * @self: a #GtkUIManager
527 528
 * @path: a path
 * 
Matthias Clasen's avatar
Matthias Clasen committed
529 530 531 532
 * 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 
533
 * (e.g. "popup"). The root element ("/ui") can be omitted in the path.
534 535 536 537 538 539 540
 * 
 * Return value: the widget found by following the path, or %NULL if no widget
 *   was found.
 *
 * Since: 2.4
 **/
GtkWidget *
541
gtk_ui_manager_get_widget (GtkUIManager *self, 
542 543 544 545
			   const gchar  *path)
{
  GNode *node;

546
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
547
  g_return_val_if_fail (path != NULL, NULL);
548

549 550
  /* ensure that there are no pending updates before we get the
   * widget */
551
  gtk_ui_manager_ensure_update (self);
552

553
  node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
554 555 556 557 558 559 560

  if (node == NULL)
    return NULL;

  return NODE_INFO (node)->proxy;
}

561 562 563 564 565
/**
 * gtk_ui_manager_get_action:
 * @self: a #GtkUIManager
 * @path: a path
 * 
Matthias Clasen's avatar
Matthias Clasen committed
566 567
 * Looks up an action by following a path. See gtk_ui_manager_get_widget()
 * for more information about paths.
568 569 570 571 572 573 574
 * 
 * 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
575 576
gtk_ui_manager_get_action (GtkUIManager *self,
			   const gchar  *path)
577 578 579 580
{
  GNode *node;

  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), NULL);
581
  g_return_val_if_fail (path != NULL, NULL);
582 583 584 585 586
  
  /* ensure that there are no pending updates before we get
   * the action */
  gtk_ui_manager_ensure_update (self);
  
587
  node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
588 589 590 591 592 593 594

  if (node == NULL)
    return NULL;

  return NODE_INFO (node)->action;
}

595
static GNode *
Matthias Clasen's avatar
Matthias Clasen committed
596 597 598 599 600 601 602
get_child_node (GtkUIManager *self, 
		GNode        *parent,
		const gchar  *childname, 
		gint          childname_length,
		NodeType      node_type,
		gboolean      create, 
		gboolean      top)
603 604 605 606
{
  GNode *child = NULL;

  g_return_val_if_fail (parent == NULL ||
607 608
			(NODE_INFO (parent)->type != NODE_TYPE_MENUITEM &&
			 NODE_INFO (parent)->type != NODE_TYPE_TOOLITEM), 
609 610 611 612 613 614 615 616 617 618 619 620
			NULL);

  if (parent)
    {
      if (childname)
	{
	  for (child = parent->children; child != NULL; child = child->next)
	    {
	      if (strlen (NODE_INFO (child)->name) == childname_length &&
		  !strncmp (NODE_INFO (child)->name, childname, childname_length))
		{
		  /* if undecided about node type, set it */
621
		  if (NODE_INFO (child)->type == NODE_TYPE_UNDECIDED)
622 623 624
		    NODE_INFO (child)->type = node_type;
		  
		  /* warn about type mismatch */
625 626
		  if (NODE_INFO (child)->type != NODE_TYPE_UNDECIDED &&
		      node_type != NODE_TYPE_UNDECIDED &&
627 628 629 630 631 632 633 634 635 636 637 638
		      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)
	{
639
	  Node *mnode;
640
	  
641
	  mnode = g_chunk_new0 (Node, merge_node_chunk);
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
	  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);
661
	  if (NODE_INFO (child)->type != NODE_TYPE_ROOT)
662 663 664 665
	    g_warning ("base element must be of type ROOT");
	}
      else if (create)
	{
666
	  Node *mnode;
667

668
	  mnode = g_chunk_new0 (Node, merge_node_chunk);
669 670 671 672 673 674 675 676 677 678 679 680
	  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
681 682 683 684
get_node (GtkUIManager *self, 
	  const gchar  *path,
	  NodeType      node_type, 
	  gboolean      create)
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
{
  const gchar *pos, *end;
  GNode *parent, *node;
  
  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);

703
      node = get_child_node (self, parent, pos, length, NODE_TYPE_UNDECIDED,
704 705 706 707 708 709 710 711
			     create, FALSE);
      if (!node)
	return NULL;
      
      pos += length + 1; /* move past the node name and the slash too */
      parent = node;
    }

712
  if (node != NULL && NODE_INFO (node)->type == NODE_TYPE_UNDECIDED)
713
    NODE_INFO (node)->type = node_type;
Matthias Clasen's avatar
Matthias Clasen committed
714

715 716 717
  return node;
}

Matthias Clasen's avatar
Matthias Clasen committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
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);
  g_free (info->name);
  g_chunk_free (info, merge_node_chunk);

  return FALSE;
}

734 735 736 737 738 739 740 741 742 743 744 745 746
/**
 * 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)
747 748 749 750 751 752 753
{
  self->private_data->last_merge_id++;

  return self->private_data->last_merge_id;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
754 755 756
node_prepend_ui_reference (Node   *node,
			   guint   merge_id, 
			   GQuark  action_quark)
757 758 759 760 761 762 763 764 765 766 767 768 769 770
{
  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
771 772
node_remove_ui_reference (Node  *node,
			  guint  merge_id)
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
{
  GList *p;
  
  for (p = node->uifiles; p != NULL; p = p->next)
    {
      NodeUIReference *reference = p->data;
      
      if (reference->merge_id == merge_id)
	{
	  node->uifiles = g_list_remove_link (node->uifiles, p);
	  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,
  STATE_END
} ParseState;

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

810
  GtkUIManager *self;
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825

  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;
826
  GtkUIManager *self = ctx->self;
827 828 829

  gint i;
  const gchar *node_name;
830
  const gchar *action;
831
  GQuark action_quark;
832
  gboolean top;
833
  
834 835
  gboolean raise_error = TRUE;

836 837
  node_name = NULL;
  action = NULL;
838
  action_quark = 0;
839
  top = FALSE;
840

841 842 843 844 845 846
  for (i = 0; attribute_names[i] != NULL; i++)
    {
      if (!strcmp (attribute_names[i], "name"))
	{
	  node_name = attribute_values[i];
	}
847
      else if (!strcmp (attribute_names[i], "action"))
848
	{
849
	  action = attribute_values[i];
850
	  action_quark = g_quark_from_string (attribute_values[i]);
851
	}
852
      else if (!strcmp (attribute_names[i], "position"))
853 854 855
	{
	  top = !strcmp (attribute_values[i], "top");
	}
Matthias Clasen's avatar
Matthias Clasen committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869
      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;
	}
870
    }
871 872 873 874 875 876 877 878 879 880

  /* 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;
    }
881 882 883

  switch (element_name[0])
    {
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
    case 'a':
      if (ctx->state == STATE_ROOT && !strcmp (element_name, "accelerator"))
	{
	  ctx->state = STATE_ROOT;
	  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;
902 903
    case 'u':
      if (ctx->state == STATE_START && !strcmp (element_name, "ui"))
904 905 906 907 908
	{
	  ctx->state = STATE_ROOT;
	  ctx->current = self->private_data->root_node;
	  raise_error = FALSE;

909 910
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
911 912 913
	}
      break;
    case 'm':
914
      if (ctx->state == STATE_ROOT && !strcmp (element_name, "menubar"))
915 916 917 918
	{
	  ctx->state = STATE_MENU;
	  ctx->current = get_child_node (self, ctx->current,
					 node_name, strlen (node_name),
919
					 NODE_TYPE_MENUBAR,
920 921
					 TRUE, FALSE);
	  if (NODE_INFO (ctx->current)->action_name == 0)
922
	    NODE_INFO (ctx->current)->action_name = action_quark;
923

924 925
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
926 927
	  NODE_INFO (ctx->current)->dirty = TRUE;

928 929 930 931 932 933
	  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),
934
					 NODE_TYPE_MENU,
935 936 937 938
					 TRUE, top);
	  if (NODE_INFO (ctx->current)->action_name == 0)
	    NODE_INFO (ctx->current)->action_name = action_quark;

939 940
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
941 942
	  NODE_INFO (ctx->current)->dirty = TRUE;
	  
943 944 945 946 947 948 949 950 951
	  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),
952
				 NODE_TYPE_MENUITEM,
953 954
				 TRUE, top);
	  if (NODE_INFO (node)->action_name == 0)
955
	    NODE_INFO (node)->action_name = action_quark;
956
	  
957 958
	  node_prepend_ui_reference (NODE_INFO (node),
				     ctx->merge_id, action_quark);
959 960 961 962 963 964
	  NODE_INFO (node)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      break;
    case 'p':
965
      if (ctx->state == STATE_ROOT && !strcmp (element_name, "popup"))
966 967 968 969
	{
	  ctx->state = STATE_MENU;
	  ctx->current = get_child_node (self, ctx->current,
					 node_name, strlen (node_name),
970
					 NODE_TYPE_POPUP,
971 972
					 TRUE, FALSE);
	  if (NODE_INFO (ctx->current)->action_name == 0)
973
	    NODE_INFO (ctx->current)->action_name = action_quark;
974
	  
975 976
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
977 978 979 980 981 982 983
	  NODE_INFO (ctx->current)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      else if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
	       !strcmp (element_name, "placeholder"))
	{
984
	  if (ctx->state == STATE_TOOLBAR)
985 986
	    ctx->current = get_child_node (self, ctx->current,
					   node_name, strlen (node_name),
987
					   NODE_TYPE_TOOLBAR_PLACEHOLDER,
988 989 990 991
					   TRUE, top);
	  else
	    ctx->current = get_child_node (self, ctx->current,
					   node_name, strlen (node_name),
992
					   NODE_TYPE_MENU_PLACEHOLDER,
993 994
					   TRUE, top);
	  
995 996
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
997 998 999 1000 1001 1002
	  NODE_INFO (ctx->current)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      break;
    case 's':
1003
      if ((ctx->state == STATE_MENU || ctx->state == STATE_TOOLBAR) &&
1004
	  !strcmp (element_name, "separator"))
1005 1006 1007
	{
	  GNode *node;

1008
	  if (ctx->state == STATE_TOOLBAR)
1009
	    ctx->state = STATE_TOOLITEM;
1010 1011
	  else
	    ctx->state = STATE_MENUITEM;
1012 1013
	  node = get_child_node (self, ctx->current,
				 node_name, strlen (node_name),
1014
				 NODE_TYPE_SEPARATOR,
1015 1016
				 TRUE, top);
	  if (NODE_INFO (node)->action_name == 0)
1017
	    NODE_INFO (node)->action_name = action_quark;
1018

1019 1020
	  node_prepend_ui_reference (NODE_INFO (node),
				     ctx->merge_id, action_quark);
1021 1022 1023 1024 1025 1026
	  NODE_INFO (node)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      break;
    case 't':
1027 1028 1029 1030 1031
      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),
1032
					 NODE_TYPE_TOOLBAR,
1033 1034 1035 1036
					 TRUE, FALSE);
	  if (NODE_INFO (ctx->current)->action_name == 0)
	    NODE_INFO (ctx->current)->action_name = action_quark;
	  
1037 1038
	  node_prepend_ui_reference (NODE_INFO (ctx->current),
				     ctx->merge_id, action_quark);
1039 1040 1041 1042 1043
	  NODE_INFO (ctx->current)->dirty = TRUE;
	  
	  raise_error = FALSE;
	}
      else if (ctx->state == STATE_TOOLBAR && !strcmp (element_name, "toolitem"))
1044 1045 1046 1047 1048 1049
	{
	  GNode *node;

	  ctx->state = STATE_TOOLITEM;
	  node = get_child_node (self, ctx->current,
				node_name, strlen (node_name),
1050
				 NODE_TYPE_TOOLITEM,
1051 1052
				 TRUE, top);
	  if (NODE_INFO (node)->action_name == 0)
1053
	    NODE_INFO (node)->action_name = action_quark;
1054
	  
1055 1056
	  node_prepend_ui_reference (NODE_INFO (node),
				     ctx->merge_id, action_quark);
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	  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
1071 1072 1073 1074 1075 1076
      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);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    }
}

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
1091 1092
    case STATE_END:
      /* no need to GError here, GMarkup already catches this */
1093 1094 1095 1096 1097 1098
      break;
    case STATE_ROOT:
      ctx->current = NULL;
      ctx->state = STATE_END;
      break;
    case STATE_MENU:
Matthias Clasen's avatar
Matthias Clasen committed
1099
    case STATE_TOOLBAR:
1100
      ctx->current = ctx->current->parent;
1101
      if (NODE_INFO (ctx->current)->type == NODE_TYPE_ROOT) 
1102
	ctx->state = STATE_ROOT;
1103
      /* else, stay in same state */
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
      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
1125
  gtk_ui_manager_remove_ui (ctx->self, ctx->merge_id);
1126 1127
}

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
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);
    }
}


1164 1165 1166
static GMarkupParser ui_parser = {
  start_element_handler,
  end_element_handler,
1167
  text_handler,
1168 1169 1170 1171
  NULL,
  cleanup
};

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
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;
1185
  ctx.merge_id = gtk_ui_manager_new_merge_id (self);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

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

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

  if (!g_markup_parse_context_parse (context, buffer, length, error))
    goto error;

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

  if (!g_markup_parse_context_end_parse (context, error))
    goto error;

  g_markup_parse_context_free (context);

1205
  queue_update (self);
1206

1207
  g_object_notify (G_OBJECT (self), "ui");      
1208

1209 1210 1211 1212 1213 1214 1215 1216 1217
  return ctx.merge_id;

 error:

  g_markup_parse_context_free (context);

  return 0;
}

1218
/**
1219 1220
 * gtk_ui_manager_add_ui_from_string:
 * @self: a #GtkUIManager object
1221 1222 1223 1224
 * @buffer: the string to parse
 * @length: the length of @buffer (may be -1 if @buffer is nul-terminated)
 * @error: return location for an error
 * 
1225 1226
 * 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
1227
 * element is added if it is missing.
1228 1229
 * 
 * Return value: The merge id for the merged UI. The merge id can be used
1230
 *   to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1231 1232 1233 1234 1235
 *   the return value is 0.
 *
 * Since: 2.4
 **/
guint
1236
gtk_ui_manager_add_ui_from_string (GtkUIManager *self,
1237
				   const gchar  *buffer, 
1238
				   gssize        length,
1239 1240
				   GError      **error)
{
1241 1242 1243
  gboolean needs_root = TRUE;
  const gchar *p;
  const gchar *end;
1244

1245 1246
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);
  g_return_val_if_fail (buffer != NULL, 0);
1247 1248 1249 1250

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

1251 1252 1253 1254
  p = buffer;
  end = buffer + length;
  while (p != end && xml_isspace (*p))
    ++p;
1255

1256 1257 1258 1259
  if (end - p >= 4 && strncmp (p, "<ui>", 4) == 0)
    needs_root = FALSE;
  
  return add_ui_from_string (self, buffer, length, needs_root, error);
1260 1261 1262
}

/**
1263 1264
 * gtk_ui_manager_add_ui_from_file:
 * @self: a #GtkUIManager object
1265 1266 1267
 * @filename: the name of the file to parse 
 * @error: return location for an error
 * 
1268
 * Parses a file containing a <link linkend="XML-UI">UI definition</link> and 
Matthias Clasen's avatar
Matthias Clasen committed
1269
 * merges it with the current contents of @self. 
1270 1271
 * 
 * Return value: The merge id for the merged UI. The merge id can be used
1272
 *   to unmerge the UI with gtk_ui_manager_remove_ui(). If an error occurred,
1273 1274 1275 1276 1277
 *   the return value is 0.
 *
 * Since: 2.4
 **/
guint
1278
gtk_ui_manager_add_ui_from_file (GtkUIManager *self,
1279 1280 1281 1282 1283 1284 1285
				 const gchar  *filename,
				 GError      **error)
{
  gchar *buffer;
  gint length;
  guint res;

1286 1287
  g_return_val_if_fail (GTK_IS_UI_MANAGER (self), 0);

1288 1289 1290
  if (!g_file_get_contents (filename, &buffer, &length, error))
    return 0;

1291
  res = add_ui_from_string (self, buffer, length, FALSE, error);
1292 1293 1294 1295 1296
  g_free (buffer);

  return res;
}

1297 1298 1299 1300 1301 1302 1303
/**
 * 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
1304 1305 1306
 * @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.
1307
 * 
1308 1309 1310 1311 1312 1313
 * 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.
1314 1315 1316 1317
 * 
 * Since: 2.4
 **/
void
1318 1319 1320 1321 1322 1323 1324
gtk_ui_manager_add_ui (GtkUIManager        *self,
		       guint                merge_id,
		       const gchar         *path,
		       const gchar         *name,
		       const gchar         *action,
		       GtkUIManagerItemType type,
		       gboolean             top)
1325 1326 1327
{
  GNode *node;
  GNode *child;
1328
  NodeType node_type;
1329 1330 1331 1332
  GQuark action_quark = 0;

  g_return_if_fail (GTK_IS_UI_MANAGER (self));  
  g_return_if_fail (merge_id > 0);
1333
  g_return_if_fail (name != NULL);
1334

1335
  node = get_node (self, path, NODE_TYPE_UNDECIDED, FALSE);
1336 1337 1338 1339
  
  if (node == NULL)
    return;

1340 1341
  node_type = NODE_TYPE_UNDECIDED;

1342 1343
  switch (NODE_INFO (node)->type) 
    {
1344
    case NODE_TYPE_MENUBAR:
1345 1346 1347
    case NODE_TYPE_MENU:
    case NODE_TYPE_POPUP:
    case NODE_TYPE_MENU_PLACEHOLDER:
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
      switch (type) 
	{
	case GTK_UI_MANAGER_AUTO:
	  if (action != NULL)
	      node_type = NODE_TYPE_MENUITEM;
	  else
	      node_type = NODE_TYPE_SEPARATOR;
	  break;
	case GTK_UI_MANAGER_MENU:
	  node_type = NODE_TYPE_MENU;
	  break;
	case GTK_UI_MANAGER_MENUITEM:
	  node_type = NODE_TYPE_MENUITEM;
	  break;
	case GTK_UI_MANAGER_SEPARATOR:
	  node_type = NODE_TYPE_SEPARATOR;
	  break;
	case GTK_UI_MANAGER_PLACEHOLDER:
	  node_type = NODE_TYPE_MENU_PLACEHOLDER;
	  break;
	default: ;
	  /* do nothing */
	}
1371
      break;
1372 1373
    case NODE_TYPE_TOOLBAR:
    case NODE_TYPE_TOOLBAR_PLACEHOLDER:
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
      switch (type) 
	{
	case GTK_UI_MANAGER_AUTO:
	  if (action != NULL)
	      node_type = NODE_TYPE_TOOLITEM;
	  else
	      node_type = NODE_TYPE_SEPARATOR;
	  break;
	case GTK_UI_MANAGER_TOOLITEM:
	  node_type = NODE_TYPE_TOOLITEM;
	  break;
	case GTK_UI_MANAGER_SEPARATOR:
	  node_type = NODE_TYPE_SEPARATOR;
	  break;
	case GTK_UI_MANAGER_PLACEHOLDER:
	  node_type = NODE_TYPE_MENU_PLACEHOLDER;
	  break;
	default: ;
	  /* do nothing */
	}
1394
      break;
1395 1396
    case NODE_TYPE_ROOT:
      switch (type) 
1397
	{
1398 1399
	case GTK_UI_MANAGER_MENUBAR:
	  node_type = NODE_TYPE_MENUBAR;
1400
	  break;
1401 1402
	case GTK_UI_MANAGER_TOOLBAR:
	  node_type = NODE_TYPE_TOOLBAR;
1403
	  break;
1404 1405
	case GTK_UI_MANAGER_POPUP:
	  node_type = NODE_TYPE_POPUP;
1406
	  break;
1407 1408 1409
	case GTK_UI_MANAGER_ACCELERATOR:
	  node_type = NODE_TYPE_ACCELERATOR;
	  break;
1410 1411
	default: ;
	  /* do nothing */
1412
	}
1413 1414 1415
      break;
    default: ;
      /* do nothing */
1416
    }
1417 1418 1419 1420

  if (node_type == NODE_TYPE_UNDECIDED)
    return;
   
1421 1422
  child = get_child_node (self, node,
			  name, strlen (name),
1423
			  node_type, TRUE, top);
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436

  if (action != NULL)
    action_quark = g_quark_from_string (action);

  node_prepend_ui_reference (NODE_INFO (child), 
			     merge_id, action_quark);

  if (NODE_INFO (node)->action_name == 0)
    NODE_INFO (child)->action_name = action_quark;

  NODE_INFO (child)->dirty = TRUE;

  queue_update (self);
1437

1438
  g_object_notify (G_OBJECT (self), "ui");      
1439 1440
}