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

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

30 31
#include	"config.h"

Tim Janik's avatar
Tim Janik committed
32 33 34 35 36 37 38 39
#include	"gtkitemfactory.h"
#include	"gtk/gtksignal.h"
#include	"gtk/gtkoptionmenu.h"
#include	"gtk/gtkmenubar.h"
#include	"gtk/gtkmenu.h"
#include	"gtk/gtkmenuitem.h"
#include	"gtk/gtkradiomenuitem.h"
#include	"gtk/gtkcheckmenuitem.h"
40
#include	"gtk/gtkimagemenuitem.h"
41
#include	"gtk/gtktearoffmenuitem.h"
42
#include	"gtk/gtkaccelmap.h"
Tim Janik's avatar
Tim Janik committed
43
#include	"gtk/gtkaccellabel.h"
44
#include        "gdk/gdkkeysyms.h"
45 46 47
#include	"gtk/gtkimage.h"
#include	"gtk/gtkstock.h"
#include	"gtk/gtkiconfactory.h"
Tim Janik's avatar
Tim Janik committed
48 49
#include	<string.h>
#include	<fcntl.h>
50
#ifdef HAVE_UNISTD_H
Tim Janik's avatar
Tim Janik committed
51
#include	<unistd.h>
52
#endif
53
#include	<stdio.h>
Tim Janik's avatar
Tim Janik committed
54

Manish Singh's avatar
Manish Singh committed
55
#ifdef G_OS_WIN32
56 57 58 59 60 61
#include	<io.h>		/* For _open and _close */

#ifndef S_ISREG
#define S_ISREG(mode) ((mode)&_S_IFREG)
#endif
#endif
Tim Janik's avatar
Tim Janik committed
62 63 64 65 66 67 68 69 70


/* --- defines --- */
#define		ITEM_FACTORY_STRING	((gchar*) item_factory_string)
#define		ITEM_BLOCK_SIZE		(128)


/* --- structures --- */
typedef struct	_GtkIFCBData		GtkIFCBData;
71
typedef struct  _GtkIFDumpData		GtkIFDumpData;
Tim Janik's avatar
Tim Janik committed
72 73
struct _GtkIFCBData
{
74 75 76 77
  GtkItemFactoryCallback  func;
  guint			  callback_type;
  gpointer		  func_data;
  guint			  callback_action;
Tim Janik's avatar
Tim Janik committed
78 79 80 81 82 83 84
};


/* --- prototypes --- */
static void	gtk_item_factory_class_init		(GtkItemFactoryClass  *klass);
static void	gtk_item_factory_init			(GtkItemFactory	      *ifactory);
static void	gtk_item_factory_destroy		(GtkObject	      *object);
85
static void	gtk_item_factory_finalize		(GObject	      *object);
Tim Janik's avatar
Tim Janik committed
86 87 88 89


/* --- static variables --- */
static GtkItemFactoryClass *gtk_item_factory_class = NULL;
90
static gpointer          parent_class = NULL;
Tim Janik's avatar
Tim Janik committed
91 92 93
static const gchar	*item_factory_string = "Gtk-<ItemFactory>";
static GMemChunk	*ifactory_item_chunks = NULL;
static GMemChunk	*ifactory_cb_data_chunks = NULL;
94 95 96
static GQuark		 quark_popup_data = 0;
static GQuark		 quark_if_menu_pos = 0;
static GQuark		 quark_item_factory = 0;
97 98 99
static GQuark		 quark_item_path = 0;
static GQuark		 quark_action = 0;
static GQuark		 quark_accel_group = 0;
100 101 102 103 104
static GQuark		 quark_type_item = 0;
static GQuark		 quark_type_title = 0;
static GQuark		 quark_type_radio_item = 0;
static GQuark		 quark_type_check_item = 0;
static GQuark		 quark_type_toggle_item = 0;
105 106
static GQuark		 quark_type_image_item = 0;
static GQuark		 quark_type_stock_item = 0;
107
static GQuark		 quark_type_tearoff_item = 0;
108 109 110
static GQuark		 quark_type_separator_item = 0;
static GQuark		 quark_type_branch = 0;
static GQuark		 quark_type_last_branch = 0;
Tim Janik's avatar
Tim Janik committed
111 112 113 114 115 116 117 118 119 120


/* --- functions --- */
GtkType
gtk_item_factory_get_type (void)
{
  static GtkType item_factory_type = 0;
  
  if (!item_factory_type)
    {
121
      static const GtkTypeInfo item_factory_info =
Tim Janik's avatar
Tim Janik committed
122 123 124 125 126 127
      {
	"GtkItemFactory",
	sizeof (GtkItemFactory),
	sizeof (GtkItemFactoryClass),
	(GtkClassInitFunc) gtk_item_factory_class_init,
	(GtkObjectInitFunc) gtk_item_factory_init,
128 129
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
130
        (GtkClassInitFunc) NULL,
Tim Janik's avatar
Tim Janik committed
131 132
      };
      
133
      item_factory_type = gtk_type_unique (GTK_TYPE_OBJECT, &item_factory_info);
Tim Janik's avatar
Tim Janik committed
134 135 136 137 138 139 140 141
    }
  
  return item_factory_type;
}

static void
gtk_item_factory_class_init (GtkItemFactoryClass  *class)
{
142
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
143
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
Tim Janik's avatar
Tim Janik committed
144 145

  gtk_item_factory_class = class;
146
  parent_class = g_type_class_peek_parent (class);
Tim Janik's avatar
Tim Janik committed
147

148 149
  gobject_class->finalize = gtk_item_factory_finalize;

Tim Janik's avatar
Tim Janik committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163
  object_class->destroy = gtk_item_factory_destroy;

  class->item_ht = g_hash_table_new (g_str_hash, g_str_equal);
  ifactory_item_chunks =
    g_mem_chunk_new ("GtkItemFactoryItem",
		     sizeof (GtkItemFactoryItem),
		     sizeof (GtkItemFactoryItem) * ITEM_BLOCK_SIZE,
		     G_ALLOC_ONLY);
  ifactory_cb_data_chunks =
    g_mem_chunk_new ("GtkIFCBData",
		     sizeof (GtkIFCBData),
		     sizeof (GtkIFCBData) * ITEM_BLOCK_SIZE,
		     G_ALLOC_AND_FREE);

164 165 166 167 168 169 170 171 172 173 174
  quark_popup_data		= g_quark_from_static_string ("GtkItemFactory-popup-data");
  quark_if_menu_pos		= g_quark_from_static_string ("GtkItemFactory-menu-position");
  quark_item_factory		= g_quark_from_static_string ("GtkItemFactory");
  quark_item_path		= g_quark_from_static_string ("GtkItemFactory-path");
  quark_action			= g_quark_from_static_string ("GtkItemFactory-action");
  quark_accel_group		= g_quark_from_static_string ("GtkAccelGroup");
  quark_type_item		= g_quark_from_static_string ("<Item>");
  quark_type_title		= g_quark_from_static_string ("<Title>");
  quark_type_radio_item		= g_quark_from_static_string ("<RadioItem>");
  quark_type_check_item		= g_quark_from_static_string ("<CheckItem>");
  quark_type_toggle_item	= g_quark_from_static_string ("<ToggleItem>");
175 176
  quark_type_image_item         = g_quark_from_static_string ("<ImageItem>");
  quark_type_stock_item         = g_quark_from_static_string ("<StockItem>");
177
  quark_type_separator_item	= g_quark_from_static_string ("<Separator>");
178
  quark_type_tearoff_item	= g_quark_from_static_string ("<Tearoff>");
179 180
  quark_type_branch		= g_quark_from_static_string ("<Branch>");
  quark_type_last_branch	= g_quark_from_static_string ("<LastBranch>");
Tim Janik's avatar
Tim Janik committed
181 182 183 184 185 186 187 188 189 190 191 192
}

static void
gtk_item_factory_init (GtkItemFactory	    *ifactory)
{
  GtkObject *object;

  object = GTK_OBJECT (ifactory);

  ifactory->path = NULL;
  ifactory->accel_group = NULL;
  ifactory->widget = NULL;
193
  ifactory->items = NULL;
194 195 196
  ifactory->translate_func = NULL;
  ifactory->translate_data = NULL;
  ifactory->translate_notify = NULL;
Tim Janik's avatar
Tim Janik committed
197 198
}

Matthias Clasen's avatar
Matthias Clasen committed
199 200 201 202
/**
 * gtk_item_factory_new:
 * @container_type: the kind of menu to create; can be
 *    #GTK_TYPE_MENU_BAR, #GTK_TYPE_MENU or #GTK_TYPE_OPTION_MENU
Matthias Clasen's avatar
Matthias Clasen committed
203 204
 * @path: the factory path of the new item factory, a string of the form 
 *    <literal>"&lt;name&gt;"</literal>
Matthias Clasen's avatar
Matthias Clasen committed
205 206 207 208 209 210
 * @accel_group: a #GtkAccelGroup to which the accelerators for the
 *    menu items will be added, or %NULL to create a new one
 * @returns: a new #GtkItemFactory
 * 
 * Creates a new #GtkItemFactory.
 */
Tim Janik's avatar
Tim Janik committed
211 212 213 214 215 216 217 218 219
GtkItemFactory*
gtk_item_factory_new (GtkType	     container_type,
		      const gchar   *path,
		      GtkAccelGroup *accel_group)
{
  GtkItemFactory *ifactory;

  g_return_val_if_fail (path != NULL, NULL);

220
  ifactory = gtk_type_new (GTK_TYPE_ITEM_FACTORY);
Tim Janik's avatar
Tim Janik committed
221 222 223 224 225 226 227 228 229 230 231 232 233
  gtk_item_factory_construct (ifactory, container_type, path, accel_group);

  return ifactory;
}

static void
gtk_item_factory_callback_marshal (GtkWidget *widget,
				   gpointer   func_data)
{
  GtkIFCBData *data;

  data = func_data;

234 235
  if (data->callback_type == 1)
    {
236
      GtkItemFactoryCallback1 func1 = (GtkItemFactoryCallback1) data->func;
237 238 239 240
      func1 (data->func_data, data->callback_action, widget);
    }
  else if (data->callback_type == 2)
    {
241
      GtkItemFactoryCallback2 func2 = (GtkItemFactoryCallback2) data->func;
242 243
      func2 (widget, data->func_data, data->callback_action);
    }
Tim Janik's avatar
Tim Janik committed
244 245 246 247 248 249 250
}

static void
gtk_item_factory_item_remove_widget (GtkWidget		*widget,
				     GtkItemFactoryItem *item)
{
  item->widgets = g_slist_remove (item->widgets, widget);
251
  gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_item_factory);
252
  gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_item_path);
Tim Janik's avatar
Tim Janik committed
253 254
}

Matthias Clasen's avatar
Matthias Clasen committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/**
 * gtk_item_factory_add_foreign:
 * @accel_widget:     widget to install an accelerator on 
 * @full_path:	      the full path for the @accel_widget 
 * @accel_group:      the accelerator group to install the accelerator in
 * @accel_key:        key value of the accelerator
 * @accel_mods:       modifier combination of the accelerator
 *
 * Installs an accelerator for @accel_widget in @accel_group, that causes
 * the ::activate signal to be emitted if the accelerator is activated.
 * 
 * This function can be used to make widgets participate in the accel
 * saving/restoring functionality provided by gtk_accel_map_save() and
 * gtk_accel_map_load(), even if they haven't been created by an item
 * factory. 
 */
271 272 273 274
void
gtk_item_factory_add_foreign (GtkWidget      *accel_widget,
			      const gchar    *full_path,
			      GtkAccelGroup  *accel_group,
275 276
			      guint           accel_key,
			      GdkModifierType accel_mods)
Tim Janik's avatar
Tim Janik committed
277 278 279 280
{
  GtkItemFactoryClass *class;
  GtkItemFactoryItem *item;

281 282
  g_return_if_fail (GTK_IS_WIDGET (accel_widget));
  g_return_if_fail (full_path != NULL);
Tim Janik's avatar
Tim Janik committed
283

284
  class = gtk_type_class (GTK_TYPE_ITEM_FACTORY);
Tim Janik's avatar
Tim Janik committed
285

286
  accel_key = accel_key != GDK_VoidSymbol ? accel_key : 0;
287 288

  item = g_hash_table_lookup (class->item_ht, full_path);
Tim Janik's avatar
Tim Janik committed
289 290 291 292
  if (!item)
    {
      item = g_chunk_new (GtkItemFactoryItem, ifactory_item_chunks);

293
      item->path = g_strdup (full_path);
Tim Janik's avatar
Tim Janik committed
294 295 296 297 298
      item->widgets = NULL;
      
      g_hash_table_insert (class->item_ht, item->path, item);
    }

299 300
  item->widgets = g_slist_prepend (item->widgets, accel_widget);
  gtk_signal_connect (GTK_OBJECT (accel_widget),
Tim Janik's avatar
Tim Janik committed
301 302 303 304
		      "destroy",
		      GTK_SIGNAL_FUNC (gtk_item_factory_item_remove_widget),
		      item);

305
  /* set the item path for the widget
Tim Janik's avatar
Tim Janik committed
306
   */
307 308 309 310 311 312 313 314 315 316 317 318
  gtk_object_set_data_by_id (GTK_OBJECT (accel_widget), quark_item_path, item->path);
  gtk_widget_set_name (accel_widget, item->path);
  if (accel_group)
    {
      gtk_accel_group_ref (accel_group);
      gtk_object_set_data_by_id_full (GTK_OBJECT (accel_widget),
				      quark_accel_group,
				      accel_group,
				      (GtkDestroyNotify) gtk_accel_group_unref);
    }
  else
    gtk_object_set_data_by_id (GTK_OBJECT (accel_widget), quark_accel_group, NULL);
Tim Janik's avatar
Tim Janik committed
319 320 321

  /* install defined accelerators
   */
322
  if (gtk_signal_lookup ("activate", GTK_OBJECT_TYPE (accel_widget)))
Tim Janik's avatar
Tim Janik committed
323
    {
324
      if (accel_group)
325 326 327 328
	{
	  gtk_accel_map_add_entry (full_path, accel_key, accel_mods);
	  _gtk_widget_set_accel_path (accel_widget, full_path, accel_group);
	}
Tim Janik's avatar
Tim Janik committed
329
    }
330
}
Tim Janik's avatar
Tim Janik committed
331

332 333 334 335 336
static void
ifactory_cb_data_free (gpointer mem)
{
  g_mem_chunk_free (ifactory_cb_data_chunks, mem);
}
Tim Janik's avatar
Tim Janik committed
337

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
static void
gtk_item_factory_add_item (GtkItemFactory		*ifactory,
			   const gchar			*path,
			   const gchar			*accelerator,
			   GtkItemFactoryCallback	callback,
			   guint			callback_action,
			   gpointer			callback_data,
			   guint			callback_type,
			   gchar			*item_type,
			   GtkWidget			*widget)
{
  GtkItemFactoryClass *class;
  GtkItemFactoryItem *item;
  gchar *fpath;
  guint keyval, mods;
  
  g_return_if_fail (widget != NULL);
  g_return_if_fail (item_type != NULL);

357
  class = GTK_ITEM_FACTORY_GET_CLASS (ifactory);
358 359 360 361 362

  /* set accelerator group on menu widgets
   */
  if (GTK_IS_MENU (widget))
    gtk_menu_set_accel_group ((GtkMenu*) widget, ifactory->accel_group);
Tim Janik's avatar
Tim Janik committed
363 364 365 366 367 368 369 370 371

  /* connect callback if neccessary
   */
  if (callback)
    {
      GtkIFCBData *data;

      data = g_chunk_new (GtkIFCBData, ifactory_cb_data_chunks);
      data->func = callback;
372
      data->callback_type = callback_type;
Tim Janik's avatar
Tim Janik committed
373 374 375 376 377 378 379 380 381 382 383
      data->func_data = callback_data;
      data->callback_action = callback_action;

      gtk_object_weakref (GTK_OBJECT (widget),
			  ifactory_cb_data_free,
			  data);
      gtk_signal_connect (GTK_OBJECT (widget),
			  "activate",
			  GTK_SIGNAL_FUNC (gtk_item_factory_callback_marshal),
			  data);
    }
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

  /* link the widget into its item-entry
   * and keep back pointer on both the item factory and the widget
   */
  gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_action, GUINT_TO_POINTER (callback_action));
  gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_item_factory, ifactory);
  if (accelerator)
    gtk_accelerator_parse (accelerator, &keyval, &mods);
  else
    {
      keyval = 0;
      mods = 0;
    }
  fpath = g_strconcat (ifactory->path, path, NULL);
  gtk_item_factory_add_foreign (widget, fpath, ifactory->accel_group, keyval, mods);
  item = g_hash_table_lookup (class->item_ht, fpath);
  g_free (fpath);

  g_return_if_fail (item != NULL);

  if (!g_slist_find (ifactory->items, item))
    ifactory->items = g_slist_prepend (ifactory->items, item);
Tim Janik's avatar
Tim Janik committed
406 407
}

Matthias Clasen's avatar
Matthias Clasen committed
408 409 410 411 412
/**
 * gtk_item_factory_construct:
 * @ifactory: a #GtkItemFactory
 * @container_type: the kind of menu to create; can be
 *    #GTK_TYPE_MENU_BAR, #GTK_TYPE_MENU or #GTK_TYPE_OPTION_MENU
Matthias Clasen's avatar
Matthias Clasen committed
413 414
 * @path: the factory path of @ifactory, a string of the form 
 *    <literal>"&lt;name&gt;"</literal>
Matthias Clasen's avatar
Matthias Clasen committed
415 416 417 418 419
 * @accel_group: a #GtkAccelGroup to which the accelerators for the
 *    menu items will be added, or %NULL to create a new one
 * 
 * Initializes an item factory.
 */  
Tim Janik's avatar
Tim Janik committed
420 421 422 423 424 425 426 427 428 429 430
void
gtk_item_factory_construct (GtkItemFactory	*ifactory,
			    GtkType		 container_type,
			    const gchar		*path,
			    GtkAccelGroup	*accel_group)
{
  guint len;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
  g_return_if_fail (ifactory->accel_group == NULL);
  g_return_if_fail (path != NULL);
431 432
  if (!gtk_type_is_a (container_type, GTK_TYPE_OPTION_MENU))
    g_return_if_fail (gtk_type_is_a (container_type, GTK_TYPE_MENU_SHELL));
Tim Janik's avatar
Tim Janik committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

  len = strlen (path);

  if (path[0] != '<' && path[len - 1] != '>')
    {
      g_warning ("GtkItemFactory: invalid factory path `%s'", path);
      return;
    }

  if (accel_group)
    {
      ifactory->accel_group = accel_group;
      gtk_accel_group_ref (ifactory->accel_group);
    }
  else
    ifactory->accel_group = gtk_accel_group_new ();

  ifactory->path = g_strdup (path);
Tim Janik's avatar
Tim Janik committed
451 452 453
  ifactory->widget = g_object_connect (gtk_widget_new (container_type, NULL),
				       "signal::destroy", gtk_widget_destroyed, &ifactory->widget,
				       NULL);
Tim Janik's avatar
Tim Janik committed
454 455
  gtk_object_ref (GTK_OBJECT (ifactory));
  gtk_object_sink (GTK_OBJECT (ifactory));
456

Tim Janik's avatar
Tim Janik committed
457 458
  gtk_item_factory_add_item (ifactory,
			     "", NULL,
459
			     NULL, 0, NULL, 0,
Tim Janik's avatar
Tim Janik committed
460 461 462 463
			     ITEM_FACTORY_STRING,
			     ifactory->widget);
}

Matthias Clasen's avatar
Matthias Clasen committed
464 465
/**
 * gtk_item_factory_from_path:
Matthias Clasen's avatar
Matthias Clasen committed
466 467
 * @path: a string starting with a factory path of the form 
 *   <literal>"&lt;name&gt;"</literal>
Matthias Clasen's avatar
Matthias Clasen committed
468 469
 * @returns: the #GtkItemFactory created for the given factory path, or %NULL 
 *
Matthias Clasen's avatar
Matthias Clasen committed
470 471 472
 * Finds an item factory which has been constructed using the 
 * <literal>"&lt;name&gt;"</literal> prefix of @path as the @path argument 
 * for gtk_item_factory_new().
Matthias Clasen's avatar
Matthias Clasen committed
473
 */
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
GtkItemFactory*
gtk_item_factory_from_path (const gchar      *path)
{
  GtkItemFactoryClass *class;
  GtkItemFactoryItem *item;
  gchar *fname;
  guint i;

  g_return_val_if_fail (path != NULL, NULL);
  g_return_val_if_fail (path[0] == '<', NULL);

  class = gtk_type_class (GTK_TYPE_ITEM_FACTORY);

  i = 0;
  while (path[i] && path[i] != '>')
    i++;
  if (path[i] != '>')
    {
      g_warning ("gtk_item_factory_from_path(): invalid factory path \"%s\"",
		 path);
      return NULL;
    }
  fname = g_new (gchar, i + 2);
  g_memmove (fname, path, i + 1);
  fname[i + 1] = 0;

  item = g_hash_table_lookup (class->item_ht, fname);
501 502 503

  g_free (fname);

504 505 506 507 508 509
  if (item && item->widgets)
    return gtk_item_factory_from_widget (item->widgets->data);

  return NULL;
}

Tim Janik's avatar
Tim Janik committed
510
static void
Tim Janik's avatar
Tim Janik committed
511
gtk_item_factory_destroy (GtkObject *object)
Tim Janik's avatar
Tim Janik committed
512 513 514 515 516 517 518 519 520 521
{
  GtkItemFactory *ifactory;
  GSList *slist;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (object));

  ifactory = (GtkItemFactory*) object;

  if (ifactory->widget)
    {
Tim Janik's avatar
Tim Janik committed
522
      GtkObject *dobj;
Tim Janik's avatar
Tim Janik committed
523

Tim Janik's avatar
Tim Janik committed
524
      dobj = GTK_OBJECT (ifactory->widget);
Tim Janik's avatar
Tim Janik committed
525

Tim Janik's avatar
Tim Janik committed
526 527 528 529
      gtk_object_ref (dobj);
      gtk_object_sink (dobj);
      gtk_object_destroy (dobj);
      gtk_object_unref (dobj);
Tim Janik's avatar
Tim Janik committed
530 531 532 533

      ifactory->widget = NULL;
    }

534 535 536 537 538 539
  for (slist = ifactory->items; slist; slist = slist->next)
    {
      GtkItemFactoryItem *item = slist->data;
      GSList *link;
      
      for (link = item->widgets; link; link = link->next)
540
	if (gtk_object_get_data_by_id (link->data, quark_item_factory) == ifactory)
541 542 543 544
	  gtk_object_remove_data_by_id (link->data, quark_item_factory);
    }
  g_slist_free (ifactory->items);
  ifactory->items = NULL;
Tim Janik's avatar
Tim Janik committed
545

546
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
Tim Janik's avatar
Tim Janik committed
547 548 549
}

static void
550
gtk_item_factory_finalize (GObject *object)
Tim Janik's avatar
Tim Janik committed
551 552 553 554 555 556 557 558 559 560 561
{
  GtkItemFactory *ifactory;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (object));

  ifactory = GTK_ITEM_FACTORY (object);

  gtk_accel_group_unref (ifactory->accel_group);
  g_free (ifactory->path);
  g_assert (ifactory->widget == NULL);

562
  if (ifactory->translate_notify)
563 564
    ifactory->translate_notify (ifactory->translate_data);
  
565
  G_OBJECT_CLASS (parent_class)->finalize (object);
Tim Janik's avatar
Tim Janik committed
566 567
}

Matthias Clasen's avatar
Matthias Clasen committed
568 569 570 571 572 573 574
/**
 * gtk_item_factory_from_widget:
 * @widget: a widget
 * @returns: the item factory from which @widget was created, or %NULL
 *
 * Obtains the item factory from which a widget was created.
 */
Tim Janik's avatar
Tim Janik committed
575 576 577 578 579
GtkItemFactory*
gtk_item_factory_from_widget (GtkWidget	       *widget)
{
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

580
  return gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_item_factory);
Tim Janik's avatar
Tim Janik committed
581 582
}

Matthias Clasen's avatar
Matthias Clasen committed
583 584 585 586 587 588 589 590 591 592 593
/**
 * gtk_item_factory_path_from_widget:
 * @widget: a widget
 * @returns: the full path to @widget if it been created by an item factory, 
 *   %NULL otherwise 
 * 
 * If @widget has been created by an item factory, returns the full path
 * to it. (The full path of a widget is the concatenation of the factory 
 * path specified in gtk_item_factory_new() with the path specified in the 
 * #GtkItemFactoryEntry from which the widget was created.)
 */
Tim Janik's avatar
Tim Janik committed
594 595 596 597 598
gchar*
gtk_item_factory_path_from_widget (GtkWidget	    *widget)
{
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

599
  return gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_item_path);
Tim Janik's avatar
Tim Janik committed
600 601
}

Matthias Clasen's avatar
Matthias Clasen committed
602 603 604 605 606 607 608 609 610 611
/**
 * gtk_item_factory_create_items:
 * @ifactory: a #GtkItemFactory
 * @n_entries: the length of @entries
 * @entries: an array of #GtkItemFactoryEntry<!>s whose @callback members
 *    must by of type #GtkItemFactoryCallback1
 * @callback_data: data passed to the callback functions of all entries
 *
 * Creates the menu items from the @entries.
 */
Tim Janik's avatar
Tim Janik committed
612
void
613 614 615 616
gtk_item_factory_create_items (GtkItemFactory	   *ifactory,
			       guint		    n_entries,
			       GtkItemFactoryEntry *entries,
			       gpointer		    callback_data)
617 618 619 620
{
  gtk_item_factory_create_items_ac (ifactory, n_entries, entries, callback_data, 1);
}

Matthias Clasen's avatar
Matthias Clasen committed
621 622 623 624 625 626 627 628 629 630 631
/**
 * gtk_item_factory_create_items_ac:
 * @ifactory: a #GtkItemFactory
 * @n_entries: the length of @entries
 * @entries: an array of #GtkItemFactoryEntry<!>s 
 * @callback_data: data passed to the callback functions of all entries
 * @callback_data: 1 if the callback functions in @entries are of type
 *    #GtkItemFactoryCallback1, 2 if they are of type #GtkItemFactoryCallback2 
 *
 * Creates the menu items from the @entries.
 */
632
void
633 634 635 636 637
gtk_item_factory_create_items_ac (GtkItemFactory      *ifactory,
				  guint		       n_entries,
				  GtkItemFactoryEntry *entries,
				  gpointer	       callback_data,
				  guint		       callback_type)
Tim Janik's avatar
Tim Janik committed
638 639 640 641
{
  guint i;

  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
642 643 644 645 646
  g_return_if_fail (callback_type >= 1 && callback_type <= 2);

  if (n_entries == 0)
    return;

Tim Janik's avatar
Tim Janik committed
647 648 649
  g_return_if_fail (entries != NULL);

  for (i = 0; i < n_entries; i++)
650
    gtk_item_factory_create_item (ifactory, entries + i, callback_data, callback_type);
Tim Janik's avatar
Tim Janik committed
651 652
}

Matthias Clasen's avatar
Matthias Clasen committed
653 654 655 656 657 658 659 660 661 662 663 664 665
/**
 * gtk_item_factory_get_widget:
 * @ifactory: a #GtkItemFactory
 * @path: the path to the widget
 * @returns: the widget for the given path, or %NULL if @path doesn't lead
 *   to a widget
 *
 * Obtains the widget which corresponds to @path. 
 *
 * If the widget corresponding to @path is a menu item which opens a 
 * submenu, then the submenu is returned. If you are interested in the menu 
 * item, use gtk_item_factory_get_item() instead.
 */
Tim Janik's avatar
Tim Janik committed
666
GtkWidget*
667 668
gtk_item_factory_get_widget (GtkItemFactory *ifactory,
			     const gchar    *path)
Tim Janik's avatar
Tim Janik committed
669 670 671 672 673 674 675
{
  GtkItemFactoryClass *class;
  GtkItemFactoryItem *item;

  g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);
  g_return_val_if_fail (path != NULL, NULL);

676
  class = GTK_ITEM_FACTORY_GET_CLASS (ifactory);
Tim Janik's avatar
Tim Janik committed
677

678 679 680 681 682 683 684 685 686 687
  if (path[0] == '<')
    item = g_hash_table_lookup (class->item_ht, (gpointer) path);
  else
    {
      gchar *fpath;

      fpath = g_strconcat (ifactory->path, path, NULL);
      item = g_hash_table_lookup (class->item_ht, fpath);
      g_free (fpath);
    }
Tim Janik's avatar
Tim Janik committed
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

  if (item)
    {
      GSList *slist;

      for (slist = item->widgets; slist; slist = slist->next)
	{
	  if (gtk_item_factory_from_widget (slist->data) == ifactory)
	    return slist->data;
	}
    }

  return NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
703 704 705 706 707 708 709 710 711 712 713
/**
 * gtk_item_factory_get_widget_by_action:
 * @ifactory: a #GtkItemFactory
 * @action: an action as specified in the @callback_action field
 *   of #GtkItemFactoryEntry
 * @returns: the widget which corresponds to the given action, or %NULL
 *   if no widget was found
 *
 * Obtains the widget which was constructed from the first #GtkItemFactoryEntry
 * with the given @action.
 */
Tim Janik's avatar
Tim Janik committed
714
GtkWidget*
715 716
gtk_item_factory_get_widget_by_action (GtkItemFactory *ifactory,
				       guint	       action)
Tim Janik's avatar
Tim Janik committed
717 718 719 720 721
{
  GSList *slist;

  g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);

722
  for (slist = ifactory->items; slist; slist = slist->next)
Tim Janik's avatar
Tim Janik committed
723
    {
724 725
      GtkItemFactoryItem *item = slist->data;
      GSList *link;
Tim Janik's avatar
Tim Janik committed
726

727 728 729 730
      for (link = item->widgets; link; link = link->next)
	if (gtk_object_get_data_by_id (link->data, quark_item_factory) == ifactory &&
	    gtk_object_get_data_by_id (link->data, quark_action) == GUINT_TO_POINTER (action))
	  return link->data;
Tim Janik's avatar
Tim Janik committed
731 732 733 734 735
    }

  return NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
736 737 738 739 740 741 742 743 744 745 746 747 748
/** 
 * gtk_item_factory_get_item:
 * @ifactory: a #GtkItemFactory
 * @path: the path to the menu item
 * @returns: the menu item for the given path, or %NULL if @path doesn't
 *   lead to a menu item
 *
 * Obtains the menu item which corresponds to @path. 
 *
 * If the widget corresponding to @path is a menu item which opens a 
 * submenu, then the item is returned. If you are interested in the submenu, 
 * use gtk_item_factory_get_widget() instead.
 */
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
GtkWidget*
gtk_item_factory_get_item (GtkItemFactory *ifactory,
			   const gchar    *path)
{
  GtkWidget *widget;

  g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  widget = gtk_item_factory_get_widget (ifactory, path);

  if (GTK_IS_MENU (widget))
    widget = gtk_menu_get_attach_widget (GTK_MENU (widget));

  return GTK_IS_ITEM (widget) ? widget : NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
766 767 768 769 770 771 772 773 774 775 776 777

/**
 * gtk_item_factory_get_item_by_action:
 * @ifactory: a #GtkItemFactory
 * @action: an action as specified in the @callback_action field
 *   of #GtkItemFactoryEntry
 * @returns: the menu item which corresponds to the given action, or %NULL
 *   if no menu item was found
 *
 * Obtains the menu item which was constructed from the first 
 * #GtkItemFactoryEntry with the given @action.
 */
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
GtkWidget*
gtk_item_factory_get_item_by_action (GtkItemFactory *ifactory,
				     guint	     action)
{
  GtkWidget *widget;

  g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);

  widget = gtk_item_factory_get_widget_by_action (ifactory, action);

  if (GTK_IS_MENU (widget))
    widget = gtk_menu_get_attach_widget (GTK_MENU (widget));

  return GTK_IS_ITEM (widget) ? widget : NULL;
}

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
static char *
item_factory_find_separator_r (char *path)
{
  gchar *result = NULL;
  gboolean escaped = FALSE;

  while (*path)
    {
      if (escaped)
	escaped = FALSE;
      else
	{
	  if (*path == '\\')
	    escaped = TRUE;
	  else if (*path == '/')
	    result = path;
	}
      
      path++;
    }

  return result;
}

static char *
item_factory_unescape_label (const char *label)
{
  char *new = g_malloc (strlen (label) + 1);
  char *p = new;
  gboolean escaped = FALSE;
  
  while (*label)
    {
      if (escaped)
	{
	  *p++ = *label;
	  escaped = FALSE;
	}
      else
	{
	  if (*label == '\\')
	    escaped = TRUE;
	  else
	    *p++ = *label;
	}
      
      label++;
    }

  *p = '\0';

  return new;
}

848
static gboolean
849 850 851 852 853
gtk_item_factory_parse_path (GtkItemFactory *ifactory,
			     gchar          *str,
			     gchar         **path,
			     gchar         **parent_path,
			     gchar         **item)
854
{
855
  gchar *translation;
856
  gchar *p, *q;
857
  
858 859 860 861 862
  *path = g_strdup (str);

  p = q = *path;
  while (*p)
    {
863 864 865 866 867 868 869 870 871
      if (*p == '_')
	{
	  if (p[1] == '_')
	    {
	      p++;
	      *q++ = '_';
	    }
	}
      else
872 873 874 875 876 877 878 879
	{
	  *q++ = *p;
	}
      p++;
    }
  *q = 0;

  *parent_path = g_strdup (*path);
880
  p = item_factory_find_separator_r (*parent_path);
881 882 883 884 885 886 887
  if (!p)
    {
      g_warning ("GtkItemFactory: invalid entry path `%s'", str);
      return FALSE;
    }
  *p = 0;

888 889 890 891 892
  if (ifactory->translate_func)
    translation = ifactory->translate_func (str, ifactory->translate_data);
  else
    translation = str;
			      
893
  p = item_factory_find_separator_r (translation);
894 895 896 897
  if (p)
    p++;
  else
    p = translation;
898

899
  *item = item_factory_unescape_label (p);
900 901 902 903

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
904 905 906 907 908 909 910 911 912 913
/**
 * gtk_item_factory_create_item:
 * @ifactory: a #GtkItemFactory
 * @entry: the #GtkItemFactoryEntry to create an item for
 * @callback_data: data passed to the callback function of @entry
 * @callback_data: 1 if the callback function of @entry is of type
 *    #GtkItemFactoryCallback1, 2 if it is of type #GtkItemFactoryCallback2 
 *
 * Creates an item for @entry.
 */
Tim Janik's avatar
Tim Janik committed
914 915 916
void
gtk_item_factory_create_item (GtkItemFactory	     *ifactory,
			      GtkItemFactoryEntry    *entry,
917 918
			      gpointer		      callback_data,
			      guint		      callback_type)
Tim Janik's avatar
Tim Janik committed
919
{
920
  GtkOptionMenu *option_menu = NULL;
Tim Janik's avatar
Tim Janik committed
921 922
  GtkWidget *parent;
  GtkWidget *widget;
923
  GtkWidget *image;
Tim Janik's avatar
Tim Janik committed
924
  GSList *radio_group;
925
  gchar *name;
Tim Janik's avatar
Tim Janik committed
926
  gchar *parent_path;
927
  gchar *path;
928
  gchar *accelerator;
Tim Janik's avatar
Tim Janik committed
929 930
  guint type_id;
  GtkType type;
931
  gchar *item_type_path;
932 933
  GtkStockItem stock_item;
      
Tim Janik's avatar
Tim Janik committed
934 935 936
  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
  g_return_if_fail (entry != NULL);
  g_return_if_fail (entry->path != NULL);
937
  g_return_if_fail (entry->path[0] == '/');
938
  g_return_if_fail (callback_type >= 1 && callback_type <= 2);
Tim Janik's avatar
Tim Janik committed
939 940

  if (!entry->item_type ||
941 942
      entry->item_type[0] == 0)
    {
943
      item_type_path = "<Item>";
944
      type_id = quark_type_item;
945
    }
Tim Janik's avatar
Tim Janik committed
946
  else
947 948 949 950
    {
      item_type_path = entry->item_type;
      type_id = gtk_object_data_try_key (item_type_path);
    }
Tim Janik's avatar
Tim Janik committed
951 952

  radio_group = NULL;
953
  if (type_id == quark_type_item)
954
    type = GTK_TYPE_MENU_ITEM;
955
  else if (type_id == quark_type_title)
956
    type = GTK_TYPE_MENU_ITEM;
957
  else if (type_id == quark_type_radio_item)
958
    type = GTK_TYPE_RADIO_MENU_ITEM;
959
  else if (type_id == quark_type_check_item)
960
    type = GTK_TYPE_CHECK_MENU_ITEM;
961 962 963 964
  else if (type_id == quark_type_image_item)
    type = GTK_TYPE_IMAGE_MENU_ITEM;
  else if (type_id == quark_type_stock_item)
    type = GTK_TYPE_IMAGE_MENU_ITEM;
965 966
  else if (type_id == quark_type_tearoff_item)
    type = GTK_TYPE_TEAROFF_MENU_ITEM;
967
  else if (type_id == quark_type_toggle_item)
968
    type = GTK_TYPE_CHECK_MENU_ITEM;
969
  else if (type_id == quark_type_separator_item)
970
    type = GTK_TYPE_MENU_ITEM;
971
  else if (type_id == quark_type_branch)
972
    type = GTK_TYPE_MENU_ITEM;
973
  else if (type_id == quark_type_last_branch)
974
    type = GTK_TYPE_MENU_ITEM;
Tim Janik's avatar
Tim Janik committed
975 976 977 978
  else
    {
      GtkWidget *radio_link;

979
      radio_link = gtk_item_factory_get_widget (ifactory, item_type_path);
Tim Janik's avatar
Tim Janik committed
980 981
      if (radio_link && GTK_IS_RADIO_MENU_ITEM (radio_link))
	{
982
	  type = GTK_TYPE_RADIO_MENU_ITEM;
Tim Janik's avatar
Tim Janik committed
983 984 985 986 987 988
	  radio_group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (radio_link));
	}
      else
	{
	  g_warning ("GtkItemFactory: entry path `%s' has invalid type `%s'",
		     entry->path,
989
		     item_type_path);
Tim Janik's avatar
Tim Janik committed
990 991 992 993
	  return;
	}
    }

994
  if (!gtk_item_factory_parse_path (ifactory, entry->path, 
995 996
				    &path, &parent_path, &name))
    return;
997

Tim Janik's avatar
Tim Janik committed
998 999 1000 1001
  parent = gtk_item_factory_get_widget (ifactory, parent_path);
  if (!parent)
    {
      GtkItemFactoryEntry pentry;
1002
      gchar *ppath, *p;
Tim Janik's avatar
Tim Janik committed
1003

1004
      ppath = g_strdup (entry->path);
1005
      p = item_factory_find_separator_r (ppath);
1006 1007 1008
      g_return_if_fail (p != NULL);
      *p = 0;
      pentry.path = ppath;
Tim Janik's avatar
Tim Janik committed
1009 1010 1011 1012 1013
      pentry.accelerator = NULL;
      pentry.callback = NULL;
      pentry.callback_action = 0;
      pentry.item_type = "<Branch>";

1014
      gtk_item_factory_create_item (ifactory, &pentry, NULL, 1);
1015
      g_free (ppath);
Tim Janik's avatar
Tim Janik committed
1016 1017

      parent = gtk_item_factory_get_widget (ifactory, parent_path);
1018
      g_return_if_fail (parent != NULL);
Tim Janik's avatar
Tim Janik committed
1019 1020
    }

1021 1022 1023 1024
  if (GTK_IS_OPTION_MENU (parent))
    {
      option_menu = GTK_OPTION_MENU (parent);
      if (!option_menu->menu)
1025 1026 1027 1028 1029 1030 1031 1032
	{
	  GtkWidget *menu = g_object_new (GTK_TYPE_MENU, NULL);
	  gchar *p = g_strconcat (ifactory->path, parent_path, NULL);

	  gtk_menu_set_accel_path (GTK_MENU (menu), p);
	  g_free (p);
	  gtk_option_menu_set_menu (option_menu, menu);
	}
1033 1034
      parent = option_menu->menu;
    }
1035
  g_free (parent_path);
1036
			      
1037
  g_return_if_fail (GTK_IS_CONTAINER (parent));
1038

1039 1040
  accelerator = entry->accelerator;
  
Tim Janik's avatar
Tim Janik committed
1041
  widget = gtk_widget_new (type,
1042 1043 1044 1045
			   "visible", TRUE,
			   "sensitive", (type_id != quark_type_separator_item &&
					 type_id != quark_type_title),
			   "parent", parent,
Tim Janik's avatar
Tim Janik committed
1046
			   NULL);
1047 1048
  if (option_menu && !option_menu->menu_item)
    gtk_option_menu_set_history (option_menu, 0);
Tim Janik's avatar
Tim Janik committed
1049

1050
  if (type == GTK_TYPE_RADIO_MENU_ITEM)
Tim Janik's avatar
Tim Janik committed
1051 1052 1053
    gtk_radio_menu_item_set_group (GTK_RADIO_MENU_ITEM (widget), radio_group);
  if (GTK_IS_CHECK_MENU_ITEM (widget))
    gtk_check_menu_item_set_show_toggle (GTK_CHECK_MENU_ITEM (widget), TRUE);
1054
  if (type_id == quark_type_image_item)
1055 1056 1057
    {
      GdkPixbuf *pixbuf = NULL;

1058
      image = NULL;
1059
      pixbuf = gdk_pixbuf_new_from_inline (-1,
1060
					   entry->extra_data,
1061 1062 1063 1064 1065
					   FALSE,
					   NULL);
      if (pixbuf)
	image = gtk_image_new_from_pixbuf (pixbuf);
      if (image)
1066 1067 1068 1069
	{
	  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (widget), image);
	  gtk_widget_show (image);
	}
1070 1071 1072 1073 1074 1075
      if (pixbuf)
	g_object_unref (G_OBJECT (pixbuf));
    }
  if (type_id == quark_type_stock_item)
    {
      image = gtk_image_new_from_stock (entry->extra_data, GTK_ICON_SIZE_MENU);
1076
      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (widget), image);
1077 1078
      gtk_widget_show (image);

1079 1080 1081 1082 1083 1084
      if (gtk_stock_lookup (entry->extra_data, &stock_item))
	{
	  if (!accelerator)
	    accelerator = gtk_accelerator_name (stock_item.keyval, stock_item.modifier);
	}
    }
1085 1086 1087 1088 1089

  /* install underline accelerators for this item
   */
  if (type_id != quark_type_separator_item && 
      type_id != quark_type_tearoff_item &&
1090
      *name)
Tim Janik's avatar
Tim Janik committed
1091 1092
    {
      GtkWidget *label;
1093 1094
      
      label = gtk_widget_new (GTK_TYPE_ACCEL_LABEL,
1095 1096 1097 1098
			      "visible", TRUE,
			      "parent", widget,
			      "accel_widget", widget,
			      "xalign", 0.0,
1099
			      NULL);
1100
      gtk_label_set_text_with_mnemonic (GTK_LABEL (label), name);
Tim Janik's avatar
Tim Janik committed
1101
    }
1102
  
1103
  g_free (name);
1104
  
1105 1106
  if (type_id == quark_type_branch ||
      type_id == quark_type_last_branch)
Tim Janik's avatar
Tim Janik committed
1107
    {
1108 1109
      gchar *p;

1110 1111 1112
      if (entry->callback)
	g_warning ("gtk_item_factory_create_item(): Can't specify a callback on a branch: \"%s\"",
		   entry->path);
1113
      if (type_id == quark_type_last_branch)
1114
	gtk_menu_item_set_right_justified (GTK_MENU_ITEM (widget), TRUE);
1115