gtkitemfactory.c 41.4 KB
Newer Older
Tim Janik's avatar
Tim Janik committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 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
 * 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 this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
22
23
24
25
26
27
28
29

/*
 * Modified by the GTK+ Team and others 1997-1999.  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/. 
 */

Tim Janik's avatar
Tim Janik committed
30
31
32
33
34
35
36
37
#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"
38
#include	"gtk/gtktearoffmenuitem.h"
Tim Janik's avatar
Tim Janik committed
39
#include	"gtk/gtkaccellabel.h"
40
#include        "gdk/gdkkeysyms.h"
Tim Janik's avatar
Tim Janik committed
41
42
43
44
#include	<string.h>
#include	<sys/stat.h>
#include	<fcntl.h>
#include	<unistd.h>
45
#include	<stdio.h>
Tim Janik's avatar
Tim Janik committed
46
47
48
49
50
51
52
53
54
55
56



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


/* --- structures --- */
typedef struct	_GtkIFCBData		GtkIFCBData;
typedef struct	_GtkIFActionLink	GtkIFActionLink;
57
typedef struct  _GtkIFDumpData		GtkIFDumpData;
Tim Janik's avatar
Tim Janik committed
58
59
struct _GtkIFCBData
{
60
61
62
63
  GtkItemFactoryCallback  func;
  guint			  callback_type;
  gpointer		  func_data;
  guint			  callback_action;
Tim Janik's avatar
Tim Janik committed
64
65
66
67
68
69
};
struct _GtkIFActionLink
{
  GtkWidget *widget;
  guint callback_action;
};
70
71
72
73
74
75
76
struct _GtkIFDumpData
{
  GtkPrintFunc	         print_func;
  gpointer	         func_data;
  guint		         modified_only : 1;
  GtkPatternSpec	*pspec;
};
Tim Janik's avatar
Tim Janik committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92


/* --- 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);
static void	gtk_item_factory_finalize		(GtkObject	      *object);


/* --- static variables --- */
static GtkItemFactoryClass *gtk_item_factory_class = NULL;
static GtkObjectClass	*parent_class = NULL;
static const gchar	*item_factory_string = "Gtk-<ItemFactory>";
static GMemChunk	*ifactory_item_chunks = NULL;
static GMemChunk	*ifactory_cb_data_chunks = NULL;
static const gchar	*key_popup_data = "GtkItemFactory-popup-data";
93
static GQuark		 quark_popup_data = 0;
Tim Janik's avatar
Tim Janik committed
94
static const gchar	*key_if_menu_pos = "GtkItemFactory-menu-position";
95
static GQuark		 quark_if_menu_pos = 0;
Tim Janik's avatar
Tim Janik committed
96
static const gchar	*key_item_factory = "GtkItemFactory";
97
static GQuark		 quark_item_factory = 0;
Tim Janik's avatar
Tim Janik committed
98
static const gchar	*key_item_factory_path = "GtkItemFactory-path";
99
static GQuark		 quark_item_factory_path = 0;
Tim Janik's avatar
Tim Janik committed
100
static const gchar	*key_type_item = "<Item>";
101
static GQuark		 quark_type_item = 0;
Tim Janik's avatar
Tim Janik committed
102
static const gchar	*key_type_title = "<Title>";
103
static GQuark		 quark_type_title = 0;
Tim Janik's avatar
Tim Janik committed
104
static const gchar	*key_type_radio_item = "<RadioItem>";
105
static GQuark		 quark_type_radio_item = 0;
Tim Janik's avatar
Tim Janik committed
106
static const gchar	*key_type_check_item = "<CheckItem>";
107
static GQuark		 quark_type_check_item = 0;
Tim Janik's avatar
Tim Janik committed
108
static const gchar	*key_type_toggle_item = "<ToggleItem>";
109
static GQuark		 quark_type_toggle_item = 0;
110
111
static const gchar	*key_type_tearoff_item = "<Tearoff>";
static GQuark		 quark_type_tearoff_item = 0;
Tim Janik's avatar
Tim Janik committed
112
static const gchar	*key_type_separator_item = "<Separator>";
113
static GQuark		 quark_type_separator_item = 0;
Tim Janik's avatar
Tim Janik committed
114
static const gchar	*key_type_branch = "<Branch>";
115
static GQuark		 quark_type_branch = 0;
Tim Janik's avatar
Tim Janik committed
116
static const gchar	*key_type_last_branch = "<LastBranch>";
117
static GQuark		 quark_type_last_branch = 0;
Tim Janik's avatar
Tim Janik committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
static	GScannerConfig	ifactory_scanner_config =
{
  (
   " \t\n"
   )			/* cset_skip_characters */,
  (
   G_CSET_a_2_z
   "_"
   G_CSET_A_2_Z
   )			/* cset_identifier_first */,
  (
   G_CSET_a_2_z
   "-+_0123456789"
   G_CSET_A_2_Z
   G_CSET_LATINS
   G_CSET_LATINC
   )			/* cset_identifier_nth */,
  ( ";\n" )		/* cpair_comment_single */,
  
  FALSE			/* case_sensitive */,
  
  TRUE			/* skip_comment_multi */,
  TRUE			/* skip_comment_single */,
  FALSE			/* scan_comment_multi */,
  TRUE			/* scan_identifier */,
  FALSE			/* scan_identifier_1char */,
  FALSE			/* scan_identifier_NULL */,
  TRUE			/* scan_symbols */,
  TRUE			/* scan_binary */,
  TRUE			/* scan_octal */,
  TRUE			/* scan_float */,
  TRUE			/* scan_hex */,
  FALSE			/* scan_hex_dollar */,
  TRUE			/* scan_string_sq */,
  TRUE			/* scan_string_dq */,
  TRUE			/* numbers_2_int */,
  FALSE			/* int_2_float */,
  FALSE			/* identifier_2_string */,
  TRUE			/* char_2_token */,
  FALSE			/* symbol_2_token */,
};


/* --- functions --- */
GtkType
gtk_item_factory_get_type (void)
{
  static GtkType item_factory_type = 0;
  
  if (!item_factory_type)
    {
169
      static const GtkTypeInfo item_factory_info =
Tim Janik's avatar
Tim Janik committed
170
171
172
173
174
175
      {
	"GtkItemFactory",
	sizeof (GtkItemFactory),
	sizeof (GtkItemFactoryClass),
	(GtkClassInitFunc) gtk_item_factory_class_init,
	(GtkObjectInitFunc) gtk_item_factory_init,
176
177
	/* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
178
        (GtkClassInitFunc) NULL,
Tim Janik's avatar
Tim Janik committed
179
180
      };
      
181
      item_factory_type = gtk_type_unique (GTK_TYPE_OBJECT, &item_factory_info);
Tim Janik's avatar
Tim Janik committed
182
183
184
185
186
187
188
189
190
191
192
193
    }
  
  return item_factory_type;
}

static void
gtk_item_factory_class_init (GtkItemFactoryClass  *class)
{
  GtkObjectClass *object_class;

  gtk_item_factory_class = class;

194
  parent_class = gtk_type_class (GTK_TYPE_OBJECT);
Tim Janik's avatar
Tim Janik committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

  object_class = (GtkObjectClass*) class;

  object_class->destroy = gtk_item_factory_destroy;
  object_class->finalize = gtk_item_factory_finalize;

  class->cpair_comment_single = g_strdup (";\n");

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

215
216
217
218
219
220
221
222
223
  quark_popup_data = g_quark_from_static_string (key_popup_data);
  quark_if_menu_pos = g_quark_from_static_string (key_if_menu_pos);
  quark_item_factory = g_quark_from_static_string (key_item_factory);
  quark_item_factory_path = g_quark_from_static_string (key_item_factory_path);
  quark_type_item = g_quark_from_static_string (key_type_item);
  quark_type_title = g_quark_from_static_string (key_type_title);
  quark_type_radio_item = g_quark_from_static_string (key_type_radio_item);
  quark_type_check_item = g_quark_from_static_string (key_type_check_item);
  quark_type_toggle_item = g_quark_from_static_string (key_type_toggle_item);
224
  quark_type_tearoff_item = g_quark_from_static_string (key_type_tearoff_item);
225
226
227
  quark_type_separator_item = g_quark_from_static_string (key_type_separator_item);
  quark_type_branch = g_quark_from_static_string (key_type_branch);
  quark_type_last_branch = g_quark_from_static_string (key_type_last_branch);
Tim Janik's avatar
Tim Janik committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
}

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

  object = GTK_OBJECT (ifactory);

  ifactory->path = NULL;
  ifactory->accel_group = NULL;
  ifactory->widget = NULL;
  ifactory->widgets_by_action = NULL;
}

GtkItemFactory*
gtk_item_factory_new (GtkType	     container_type,
		      const gchar   *path,
		      GtkAccelGroup *accel_group)
{
  GtkItemFactory *ifactory;

  g_return_val_if_fail (path != NULL, NULL);

252
  ifactory = gtk_type_new (GTK_TYPE_ITEM_FACTORY);
Tim Janik's avatar
Tim Janik committed
253
254
255
256
257
258
259
260
261
262
263
264
265
  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;

266
267
268
269
270
271
272
273
274
275
  if (data->callback_type == 1)
    {
      GtkItemFactoryCallback1 func1 = data->func;
      func1 (data->func_data, data->callback_action, widget);
    }
  else if (data->callback_type == 2)
    {
      GtkItemFactoryCallback2 func2 = data->func;
      func2 (widget, data->func_data, data->callback_action);
    }
Tim Janik's avatar
Tim Janik committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
}

static void
gtk_item_factory_propagate_accelerator (GtkItemFactoryItem *item,
					GtkWidget          *exclude)
{
  GSList *widget_list;
  GSList *slist;
  
  if (item->in_propagation)
    return;
  
  item->in_propagation = TRUE;
  
  widget_list = NULL;
  for (slist = item->widgets; slist; slist = slist->next)
    {
      GtkWidget *widget;
      
      widget = slist->data;
      
      if (widget != exclude)
	{
	  gtk_widget_ref (widget);
	  widget_list = g_slist_prepend (widget_list, widget);
	}
    }
  
  for (slist = widget_list; slist; slist = slist->next)
    {
      GtkWidget *widget;
      GtkItemFactory *ifactory;
      
      widget = slist->data;
      
      ifactory = gtk_item_factory_from_widget (widget);
      
      if (ifactory)
	{
	  guint signal_id;
	  
	  signal_id = gtk_signal_lookup ("activate", GTK_OBJECT_TYPE (widget));
	  if (signal_id)
	    {
	      if (item->accelerator_key)
		gtk_widget_add_accelerator (widget,
					    "activate",
					    ifactory->accel_group,
					    item->accelerator_key,
					    item->accelerator_mods,
					    GTK_ACCEL_VISIBLE);
	      else
		{
Tim Janik's avatar
Tim Janik committed
329
		  GSList *work;
Tim Janik's avatar
Tim Janik committed
330
		  
Tim Janik's avatar
Tim Janik committed
331
332
		  work = gtk_accel_group_entries_from_object (GTK_OBJECT (widget));
		  while (work)
Tim Janik's avatar
Tim Janik committed
333
334
335
		    {
		      GtkAccelEntry *ac_entry;
		      
Tim Janik's avatar
Tim Janik committed
336
337
		      ac_entry = work->data;
		      work = work->next;
Tim Janik's avatar
Tim Janik committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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
400
401
402
403
		      if (ac_entry->accel_flags & GTK_ACCEL_VISIBLE &&
			  ac_entry->accel_group == ifactory->accel_group &&
			  ac_entry->signal_id == signal_id)
			gtk_widget_remove_accelerator (GTK_WIDGET (widget),
						       ac_entry->accel_group,
						       ac_entry->accelerator_key,
						       ac_entry->accelerator_mods);
		    }
		}
	    }
	}
      gtk_widget_unref (widget);
    }
  g_slist_free (widget_list);
  
  item->in_propagation = FALSE;
}

static gint
gtk_item_factory_item_add_accelerator (GtkWidget	  *widget,
				       guint               accel_signal_id,
				       GtkAccelGroup      *accel_group,
				       guint               accel_key,
				       guint               accel_mods,
				       GtkAccelFlags       accel_flags,
				       GtkItemFactoryItem *item)
{
  if (!item->in_propagation &&
      g_slist_find (item->widgets, widget) &&
      accel_signal_id == gtk_signal_lookup ("activate", GTK_OBJECT_TYPE (widget)))
    {
      item->accelerator_key = accel_key;
      item->accelerator_mods = accel_mods;
      item->modified = TRUE;
      
      gtk_item_factory_propagate_accelerator (item, widget);
    }

  return TRUE;
}

static void
gtk_item_factory_item_remove_accelerator (GtkWidget	     *widget,
					  GtkAccelGroup      *accel_group,
					  guint               accel_key,
					  guint               accel_mods,
					  GtkItemFactoryItem *item)
{
  if (!item->in_propagation &&
      g_slist_find (item->widgets, widget) &&
      item->accelerator_key == accel_key &&
      item->accelerator_mods == accel_mods)
    {
      item->accelerator_key = 0;
      item->accelerator_mods = 0;
      item->modified = TRUE;
      
      gtk_item_factory_propagate_accelerator (item, widget);
    }
}

static void
gtk_item_factory_item_remove_widget (GtkWidget		*widget,
				     GtkItemFactoryItem *item)
{
  item->widgets = g_slist_remove (item->widgets, widget);
404
405
  gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_item_factory);
  gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_item_factory_path);
Tim Janik's avatar
Tim Janik committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
}

static void
ifactory_cb_data_free (gpointer mem)
{
  g_mem_chunk_free (ifactory_cb_data_chunks, mem);
}

static void
gtk_item_factory_add_item (GtkItemFactory		*ifactory,
			   const gchar			*path,
			   const gchar			*accelerator,
			   GtkItemFactoryCallback	callback,
			   guint			callback_action,
			   gpointer			callback_data,
421
			   guint			callback_type,
Tim Janik's avatar
Tim Janik committed
422
423
424
425
426
427
428
429
			   gchar			*item_type,
			   GtkWidget			*widget)
{
  GtkItemFactoryClass *class;
  GtkItemFactoryItem *item;
  gchar *fpath;
  
  g_return_if_fail (widget != NULL);
430
  g_return_if_fail (item_type != NULL);
Tim Janik's avatar
Tim Janik committed
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484

  class = GTK_ITEM_FACTORY_CLASS (GTK_OBJECT (ifactory)->klass);

  fpath = g_strconcat (ifactory->path, path, NULL);
  item = g_hash_table_lookup (class->item_ht, fpath);

  /* link the widget into its item-entry
   */
  if (!item)
    {
      guint keyval;
      guint mods;

      if (accelerator)
	gtk_accelerator_parse (accelerator, &keyval, &mods);
      else
	{
	  keyval = 0;
	  mods = 0;
	}

      item = g_chunk_new (GtkItemFactoryItem, ifactory_item_chunks);

      item->path = fpath;
      fpath = NULL;
      item->accelerator_key = keyval;
      item->accelerator_mods = mods;
      item->modified = FALSE;
      item->in_propagation = FALSE;
      item->item_type = NULL;
      item->widgets = NULL;
      
      g_hash_table_insert (class->item_ht, item->path, item);
    }
  g_free (fpath);

  if (item->item_type == NULL)
    {
      g_assert (item->widgets == NULL);
      
      if (item_type != ITEM_FACTORY_STRING)
	item->item_type = g_strdup (item_type);
      else
	item->item_type = ITEM_FACTORY_STRING;
    }

  item->widgets = g_slist_prepend (item->widgets, widget);
  gtk_signal_connect (GTK_OBJECT (widget),
		      "destroy",
		      GTK_SIGNAL_FUNC (gtk_item_factory_item_remove_widget),
		      item);

  /* set back pointers for the widget
   */
485
486
  gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_item_factory, ifactory);
  gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_item_factory_path, item->path);
Tim Janik's avatar
Tim Janik committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  gtk_widget_set_name (widget, item->path);

  /* set accelerator group on menu widgets
   */
  if (GTK_IS_MENU (widget))
    gtk_menu_set_accel_group ((GtkMenu*) widget, ifactory->accel_group);

  /* install defined accelerators
   */
  if (gtk_signal_lookup ("activate", GTK_OBJECT_TYPE (widget)))
    {
      if (item->accelerator_key)
	gtk_widget_add_accelerator (widget,
				    "activate",
				    ifactory->accel_group,
				    item->accelerator_key,
				    item->accelerator_mods,
				    GTK_ACCEL_VISIBLE);
      else
	gtk_widget_remove_accelerators (widget,
					"activate",
					TRUE);
    }

  /* keep track of accelerator changes
   */
  gtk_signal_connect_after (GTK_OBJECT (widget),
			    "add-accelerator",
			    GTK_SIGNAL_FUNC (gtk_item_factory_item_add_accelerator),
			    item);
  gtk_signal_connect_after (GTK_OBJECT (widget),
			    "remove-accelerator",
			    GTK_SIGNAL_FUNC (gtk_item_factory_item_remove_accelerator),
			    item);

  /* keep a per-action list of the widgets on the factory
   */
524
  if (callback)
Tim Janik's avatar
Tim Janik committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
    {
      GtkIFActionLink *link;

      link = g_new (GtkIFActionLink, 1);
      link->widget = widget;
      link->callback_action = callback_action;
      ifactory->widgets_by_action = g_slist_prepend (ifactory->widgets_by_action, link);
    }

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

      data = g_chunk_new (GtkIFCBData, ifactory_cb_data_chunks);
      data->func = callback;
542
      data->callback_type = callback_type;
Tim Janik's avatar
Tim Janik committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
      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);
    }
}

void
gtk_item_factory_construct (GtkItemFactory	*ifactory,
			    GtkType		 container_type,
			    const gchar		*path,
			    GtkAccelGroup	*accel_group)
{
562
  GtkAccelGroup *menu_group;
Tim Janik's avatar
Tim Janik committed
563
564
565
566
567
568
  guint len;

  g_return_if_fail (ifactory != NULL);
  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
  g_return_if_fail (ifactory->accel_group == NULL);
  g_return_if_fail (path != NULL);
569
570
  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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594

  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);
  ifactory->widget =
    gtk_widget_new (container_type,
		    "GtkObject::signal::destroy", gtk_widget_destroyed, &ifactory->widget,
		    NULL);
  gtk_object_ref (GTK_OBJECT (ifactory));
  gtk_object_sink (GTK_OBJECT (ifactory));
595
596
597
598

  menu_group = gtk_accel_group_new ();
  gtk_accel_group_attach (menu_group, GTK_OBJECT (ifactory->widget));
    
599
600
  /*
    gtk_signal_connect_object_while_alive (GTK_OBJECT (ifactory->widget),
Tim Janik's avatar
Tim Janik committed
601
					 "destroy",
602
					 GTK_SIGNAL_FUNC (gtk_object_destroy),
Tim Janik's avatar
Tim Janik committed
603
					 GTK_OBJECT (ifactory));
604
  */
Tim Janik's avatar
Tim Janik committed
605
606
  gtk_item_factory_add_item (ifactory,
			     "", NULL,
607
			     NULL, 0, NULL, 0,
Tim Janik's avatar
Tim Janik committed
608
609
610
611
			     ITEM_FACTORY_STRING,
			     ifactory->widget);
}

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
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);
639
640
641

  g_free (fname);

642
643
644
645
646
647
  if (item && item->widgets)
    return gtk_item_factory_from_widget (item->widgets->data);

  return NULL;
}

Tim Janik's avatar
Tim Janik committed
648
static void
Tim Janik's avatar
Tim Janik committed
649
gtk_item_factory_destroy (GtkObject *object)
Tim Janik's avatar
Tim Janik committed
650
651
652
653
654
655
656
657
658
659
660
{
  GtkItemFactory *ifactory;
  GSList *slist;

  g_return_if_fail (object != NULL);
  g_return_if_fail (GTK_IS_ITEM_FACTORY (object));

  ifactory = (GtkItemFactory*) object;

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

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

Tim Janik's avatar
Tim Janik committed
665
666
667
668
      gtk_object_ref (dobj);
      gtk_object_sink (dobj);
      gtk_object_destroy (dobj);
      gtk_object_unref (dobj);
Tim Janik's avatar
Tim Janik committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694

      ifactory->widget = NULL;
    }

  for (slist = ifactory->widgets_by_action; slist; slist = slist->next)
    g_free (slist->data);
  g_slist_free (ifactory->widgets_by_action);
  ifactory->widgets_by_action = NULL;

  parent_class->destroy (object);
}

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

  g_return_if_fail (object != NULL);
  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);

695
696
697
  if (ifactory->translate_data && ifactory->translate_notify)
    ifactory->translate_notify (ifactory->translate_data);
  
Tim Janik's avatar
Tim Janik committed
698
699
700
701
702
703
704
705
706
  parent_class->finalize (object);
}

GtkItemFactory*
gtk_item_factory_from_widget (GtkWidget	       *widget)
{
  g_return_val_if_fail (widget != NULL, NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

707
  return gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_item_factory);
Tim Janik's avatar
Tim Janik committed
708
709
710
711
712
713
714
715
}

gchar*
gtk_item_factory_path_from_widget (GtkWidget	    *widget)
{
  g_return_val_if_fail (widget != NULL, NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

716
  return gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_item_factory_path);
Tim Janik's avatar
Tim Janik committed
717
718
719
720
721
722
723
724
}

static void
gtk_item_factory_foreach (gpointer hash_key,
			  gpointer value,
			  gpointer user_data)
{
  GtkItemFactoryItem *item;
725
  GtkIFDumpData *data;
Tim Janik's avatar
Tim Janik committed
726
727
728
729
730
731
732
  gchar *string;
  gchar *name;
  gchar comment_prefix[2] = "\000\000";

  item = value;
  data = user_data;

733
  if (data->pspec && !gtk_pattern_match_string (data->pspec, item->path))
Tim Janik's avatar
Tim Janik committed
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
    return;

  comment_prefix[0] = gtk_item_factory_class->cpair_comment_single[0];

  name = gtk_accelerator_name (item->accelerator_key, item->accelerator_mods);
  string = g_strconcat (item->modified ? "" : comment_prefix,
			"(menu-path \"",
			hash_key,
			"\" \"",
			name,
			"\")",
			NULL);
  g_free (name);

  data->print_func (data->func_data, string);

  g_free (string);
}

void
754
755
756
757
gtk_item_factory_dump_items (GtkPatternSpec	 *path_pspec,
			     gboolean		  modified_only,
			     GtkPrintFunc	  print_func,
			     gpointer		  func_data)
Tim Janik's avatar
Tim Janik committed
758
{
759
  GtkIFDumpData data;
Tim Janik's avatar
Tim Janik committed
760
761
762
763
764
765
766
767
768

  g_return_if_fail (print_func != NULL);

  if (!gtk_item_factory_class)
    gtk_type_class (GTK_TYPE_ITEM_FACTORY);

  data.print_func = print_func;
  data.func_data = func_data;
  data.modified_only = (modified_only != FALSE);
769
  data.pspec = path_pspec;
Tim Janik's avatar
Tim Janik committed
770
771
772
773

  g_hash_table_foreach (gtk_item_factory_class->item_ht, gtk_item_factory_foreach, &data);
}

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
void
gtk_item_factory_print_func (gpointer FILE_pointer,
			     gchar   *string)
{
  FILE *f_out = FILE_pointer;

  g_return_if_fail (FILE_pointer != NULL);
  g_return_if_fail (string != NULL);
  
  fputs (string, f_out);
  fputc ('\n', f_out);
}

void
gtk_item_factory_dump_rc (const gchar            *file_name,
			  GtkPatternSpec         *path_pspec,
			  gboolean                modified_only)
{
  FILE *f_out;

  g_return_if_fail (file_name != NULL);

  f_out = fopen (file_name, "w");
  if (!f_out)
    return;

  fputs ("; ", f_out);
801
802
  if (g_get_prgname ())
    fputs (g_get_prgname (), f_out);
803
804
805
806
807
808
809
810
811
812
813
814
  fputs (" GtkItemFactory rc-file         -*- scheme -*-\n", f_out);
  fputs ("; this file is an automated menu-path dump\n", f_out);
  fputs (";\n", f_out);

  gtk_item_factory_dump_items (path_pspec,
			       modified_only,
			       gtk_item_factory_print_func,
			       f_out);

  fclose (f_out);
}

Tim Janik's avatar
Tim Janik committed
815
816
817
818
819
void
gtk_item_factory_create_items (GtkItemFactory	      *ifactory,
			       guint		       n_entries,
			       GtkItemFactoryEntry    *entries,
			       gpointer		       callback_data)
820
821
822
823
824
825
826
827
828
829
{
  gtk_item_factory_create_items_ac (ifactory, n_entries, entries, callback_data, 1);
}

void
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
830
831
832
833
834
{
  guint i;

  g_return_if_fail (ifactory != NULL);
  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
835
836
837
838
839
  g_return_if_fail (callback_type >= 1 && callback_type <= 2);

  if (n_entries == 0)
    return;

Tim Janik's avatar
Tim Janik committed
840
841
842
  g_return_if_fail (entries != NULL);

  for (i = 0; i < n_entries; i++)
843
    gtk_item_factory_create_item (ifactory, entries + i, callback_data, callback_type);
Tim Janik's avatar
Tim Janik committed
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
}

GtkWidget*
gtk_item_factory_get_widget (GtkItemFactory   *ifactory,
			     const gchar      *path)
{
  GtkItemFactoryClass *class;
  GtkItemFactoryItem *item;

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

  class = GTK_ITEM_FACTORY_CLASS (GTK_OBJECT (ifactory)->klass);

859
860
861
862
863
864
865
866
867
868
  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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905

  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;
}

GtkWidget*
gtk_item_factory_get_widget_by_action (GtkItemFactory   *ifactory,
				       guint	         action)
{
  GSList *slist;

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

  for (slist = ifactory->widgets_by_action; slist; slist = slist->next)
    {
      GtkIFActionLink *link;

      link = slist->data;

      if (link->callback_action == action)
	return link->widget;
    }

  return NULL;
}

906
static gboolean
907
908
909
910
911
gtk_item_factory_parse_path (GtkItemFactory *ifactory,
			     gchar          *str,
			     gchar         **path,
			     gchar         **parent_path,
			     gchar         **item)
912
{
913
  gchar *translation;
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
  gchar *p, *q;

  *path = g_strdup (str);

  p = q = *path;
  while (*p)
    {
      if (*p != '_')
	{
	  *q++ = *p;
	}
      p++;
    }
  *q = 0;

  *parent_path = g_strdup (*path);
  p = strrchr (*parent_path, '/');
  if (!p)
    {
      g_warning ("GtkItemFactory: invalid entry path `%s'", str);
      return FALSE;
    }
  *p = 0;

938
939
940
941
942
943
  if (ifactory->translate_func)
    translation = ifactory->translate_func (str, ifactory->translate_data);
  else
    translation = str;
			      
  p = strrchr (translation, '/');
944
945
946
947
948
949
950
  p++;

  *item = g_strdup (p);

  return TRUE;
}

Tim Janik's avatar
Tim Janik committed
951
952
953
void
gtk_item_factory_create_item (GtkItemFactory	     *ifactory,
			      GtkItemFactoryEntry    *entry,
954
955
			      gpointer		      callback_data,
			      guint		      callback_type)
Tim Janik's avatar
Tim Janik committed
956
957
958
959
{
  GtkWidget *parent;
  GtkWidget *widget;
  GSList *radio_group;
960
  gchar *name;
Tim Janik's avatar
Tim Janik committed
961
  gchar *parent_path;
962
963
  gchar *path;
  guint accel_key;
Tim Janik's avatar
Tim Janik committed
964
965
  guint type_id;
  GtkType type;
966
  gchar *item_type_path;
967
968
  GtkAccelGroup *parent_accel_group = NULL;
  GSList *tmp_list;
Tim Janik's avatar
Tim Janik committed
969
970
971
972
973

  g_return_if_fail (ifactory != NULL);
  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
  g_return_if_fail (entry != NULL);
  g_return_if_fail (entry->path != NULL);
974
  g_return_if_fail (entry->path[0] == '/');
975
  g_return_if_fail (callback_type >= 1 && callback_type <= 2);
Tim Janik's avatar
Tim Janik committed
976
977

  if (!entry->item_type ||
978
979
980
      entry->item_type[0] == 0)
    {
      item_type_path = (gpointer) key_type_item;
981
      type_id = quark_type_item;
982
    }
Tim Janik's avatar
Tim Janik committed
983
  else
984
985
986
987
    {
      item_type_path = entry->item_type;
      type_id = gtk_object_data_try_key (item_type_path);
    }
Tim Janik's avatar
Tim Janik committed
988
989

  radio_group = NULL;
990
  if (type_id == quark_type_item)
991
    type = GTK_TYPE_MENU_ITEM;
992
  else if (type_id == quark_type_title)
993
    type = GTK_TYPE_MENU_ITEM;
994
  else if (type_id == quark_type_radio_item)
995
    type = GTK_TYPE_RADIO_MENU_ITEM;
996
  else if (type_id == quark_type_check_item)
997
    type = GTK_TYPE_CHECK_MENU_ITEM;
998
999
  else if (type_id == quark_type_tearoff_item)
    type = GTK_TYPE_TEAROFF_MENU_ITEM;
1000
  else if (type_id == quark_type_toggle_item)
1001
    type = GTK_TYPE_CHECK_MENU_ITEM;
1002
  else if (type_id == quark_type_separator_item)
1003
    type = GTK_TYPE_MENU_ITEM;
1004
  else if (type_id == quark_type_branch)
1005
    type = GTK_TYPE_MENU_ITEM;
1006
  else if (type_id == quark_type_last_branch)
1007
    type = GTK_TYPE_MENU_ITEM;
Tim Janik's avatar
Tim Janik committed
1008
1009
1010
1011
  else
    {
      GtkWidget *radio_link;

1012
      radio_link = gtk_item_factory_get_widget (ifactory, item_type_path);
Tim Janik's avatar
Tim Janik committed
1013
1014
      if (radio_link && GTK_IS_RADIO_MENU_ITEM (radio_link))
	{
1015
	  type = GTK_TYPE_RADIO_MENU_ITEM;
Tim Janik's avatar
Tim Janik committed
1016
1017
1018
1019
1020
1021
	  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,
1022
		     item_type_path);
Tim Janik's avatar
Tim Janik committed
1023
1024
1025
1026
	  return;
	}
    }

1027
  if (!gtk_item_factory_parse_path (ifactory, entry->path, 
1028
1029
				    &path, &parent_path, &name))
    return;
1030

Tim Janik's avatar
Tim Janik committed
1031
1032
1033
1034
  parent = gtk_item_factory_get_widget (ifactory, parent_path);
  if (!parent)
    {
      GtkItemFactoryEntry pentry;
1035
      gchar *ppath, *p;
Tim Janik's avatar
Tim Janik committed
1036

1037
1038
1039
1040
1041
      ppath = g_strdup (entry->path);
      p = strrchr (ppath, '/');
      g_return_if_fail (p != NULL);
      *p = 0;
      pentry.path = ppath;
Tim Janik's avatar
Tim Janik committed
1042
1043
1044
1045
1046
      pentry.accelerator = NULL;
      pentry.callback = NULL;
      pentry.callback_action = 0;
      pentry.item_type = "<Branch>";

1047
      gtk_item_factory_create_item (ifactory, &pentry, NULL, 1);
1048
      g_free (ppath);
Tim Janik's avatar
Tim Janik committed
1049
1050

      parent = gtk_item_factory_get_widget (ifactory, parent_path);
1051
      g_return_if_fail (parent != NULL);
Tim Janik's avatar
Tim Janik committed
1052
1053
1054
1055
    }
  g_free (parent_path);

  g_return_if_fail (parent != NULL);
1056
1057
1058
1059

  tmp_list = gtk_accel_groups_from_object (GTK_OBJECT (parent));
  if (tmp_list)
    parent_accel_group = tmp_list->data;
Tim Janik's avatar
Tim Janik committed
1060
1061
1062
  
  widget = gtk_widget_new (type,
			   "GtkWidget::visible", TRUE,
1063
1064
			   "GtkWidget::sensitive", (type_id != quark_type_separator_item &&
						    type_id != quark_type_title),
Tim Janik's avatar
Tim Janik committed
1065
1066
1067
			   "GtkWidget::parent", parent,
			   NULL);

1068
  if (type == GTK_TYPE_RADIO_MENU_ITEM)
Tim Janik's avatar
Tim Janik committed
1069
1070
1071
1072
    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);
    
1073
1074
1075
  if ((type_id != quark_type_separator_item) && 
      (type_id != quark_type_tearoff_item) &&
      *name)
Tim Janik's avatar
Tim Janik committed
1076
1077
    {
      GtkWidget *label;
1078

Tim Janik's avatar
Tim Janik committed
1079
1080
1081
1082
1083
1084
1085
      label =
	gtk_widget_new (GTK_TYPE_ACCEL_LABEL,
			"GtkWidget::visible", TRUE,
			"GtkWidget::parent", widget,
			"GtkAccelLabel::accel_widget", widget,
			"GtkMisc::xalign", 0.0,
			NULL);
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105

      accel_key = gtk_label_parse_uline (GTK_LABEL (label), name);

      if ((accel_key != GDK_VoidSymbol) && GTK_IS_MENU_BAR (parent))
	{
	  gtk_widget_add_accelerator (widget,
				      "activate_item",
				      ifactory->accel_group,
				      accel_key, GDK_MOD1_MASK,
				      GTK_ACCEL_LOCKED);
	}

      if ((accel_key != GDK_VoidSymbol) && parent_accel_group)
	{
	  gtk_widget_add_accelerator (widget,
				      "activate_item",
				      parent_accel_group,
				      accel_key, 0,
				      GTK_ACCEL_LOCKED);
	}
Tim Janik's avatar
Tim Janik committed
1106
    }
1107
1108
1109

  g_free (name);

1110
1111
  if (type_id == quark_type_branch ||
      type_id == quark_type_last_branch)
Tim Janik's avatar
Tim Janik committed
1112
    {
1113
1114
      GtkAccelGroup *menu_group;

1115
1116
1117
1118
      if (entry->callback)
	g_warning ("gtk_item_factory_create_item(): Can't specify a callback on a branch: \"%s\"",
		   entry->path);
	
1119
1120
      menu_group = gtk_accel_group_new ();
      
1121
      if (type_id == quark_type_last_branch)
Tim Janik's avatar
Tim Janik committed
1122
1123
1124
1125
	gtk_menu_item_right_justify (GTK_MENU_ITEM (widget));
	
      parent = widget;
      widget =
1126
	gtk_widget_new (GTK_TYPE_MENU,
Tim Janik's avatar
Tim Janik committed
1127
			NULL);
1128
1129
      
      gtk_accel_group_attach (menu_group, GTK_OBJECT (widget));
Tim Janik's avatar
Tim Janik committed
1130
1131
1132
1133
      gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), widget);
    }	   
  
  gtk_item_factory_add_item (ifactory,
1134
			     path, entry->accelerator,
1135
			     (type_id == quark_type_branch ||
1136
1137
			      type_id == quark_type_last_branch) ?
			      (GtkItemFactoryCallback) NULL : entry->callback,
1138
			     entry->callback_action, callback_data,
1139
			     callback_type,
1140
			     item_type_path,
Tim Janik's avatar
Tim Janik committed
1141
			     widget);
1142
1143

  g_free (path);
Tim Janik's avatar
Tim Janik committed
1144
1145
1146
}

void
1147
1148
1149
gtk_item_factory_create_menu_entries (guint              n_entries,
				      GtkMenuEntry      *entries)
{
1150
1151
  static GtkPatternSpec pspec_separator = { 42, 0 };
  static GtkPatternSpec pspec_check = { 42, 0 };
1152
1153
1154
1155
1156
1157
  guint i;

  if (!n_entries)
    return;
  g_return_if_fail (entries != NULL);

1158
1159
1160
1161
1162
  if (pspec_separator.pattern_length == 0)
    {
      gtk_pattern_spec_init (&pspec_separator, "*<separator>*");
      gtk_pattern_spec_init (&pspec_check, "*<check>*");
    }
1163
1164
1165
1166
1167
1168

  for (i = 0; i < n_entries; i++)
    {
      GtkItemFactory *ifactory;
      GtkItemFactoryEntry entry;
      gchar *path;
1169
      gchar *cpath;
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183

      path = entries[i].path;
      ifactory = gtk_item_factory_from_path (path);
      if (!ifactory)
	{
	  g_warning ("gtk_item_factory_create_menu_entries(): "
		     "entry[%u] refers to unknown item factory: \"%s\"",
		     i, entries[i].path);
	  continue;
	}

      while (*path != '>')
	path++;
      path++;
1184
      cpath = NULL;
1185
1186
1187
1188
1189

      entry.path = path;
      entry.accelerator = entries[i].accelerator;
      entry.callback = entries[i].callback;
      entry.callback_action = 0;
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
      if (gtk_pattern_match_string (&pspec_separator, path))
	entry.item_type = (gpointer) key_type_separator_item;
      else if (!gtk_pattern_match_string (&pspec_check, path))
	entry.item_type = NULL;
      else
	{
	  gboolean in_brace = FALSE;
	  gchar *c;
	  
	  cpath = g_new (gchar, strlen (path));
	  c = cpath;
	  while (*path != 0)
	    {
	      if (*path == '<')
		in_brace = TRUE;
	      else if (*path == '>')
		in_brace = FALSE;
	      else if (!in_brace)
		*(c++) = *path;
	      path++;
	    }
	  *c = 0;
	  entry.item_type = (gpointer) key_type_toggle_item;
	  entry.path = cpath;
	}
      
1216
1217
      gtk_item_factory_create_item (ifactory, &entry, entries[i].callback_data, 2);
      entries[i].widget = gtk_item_factory_get_widget (ifactory, entries[i].path);
1218
      g_free (cpath);
1219
1220
1221
1222
1223
1224
    }
}

void
gtk_item_factories_path_delete (const gchar *ifactory_path,
				const gchar *path)
Tim Janik's avatar
Tim Janik committed
1225
1226
1227
1228
1229
1230
1231
1232
{
  GtkItemFactoryClass *class;
  GtkItemFactoryItem *item;

  g_return_if_fail (path != NULL);

  class = gtk_type_class (GTK_TYPE_ITEM_FACTORY);

1233
1234
1235
1236
1237
  if (path[0] == '<')
    item = g_hash_table_lookup (class->item_ht, (gpointer) path);
  else
    {
      gchar *fpath;
Tim Janik's avatar
Tim Janik committed
1238

1239
1240
1241
1242
1243
1244
1245
      g_return_if_fail (ifactory_path != NULL);
      
      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
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
  if (item)
    {
      GSList *widget_list;
      GSList *slist;

      widget_list = NULL;
      for (slist = item->widgets; slist; slist = slist->next)
	{
	  GtkWidget *widget;

	  widget = slist->data;
	  widget_list = g_slist_prepend (widget_list, widget);
	  gtk_widget_ref (widget);
	}

      for (slist = widget_list; slist; slist = slist->next)
	{
	  GtkWidget *widget;

	  widget = slist->data;
	  gtk_widget_destroy (widget);
	  gtk_widget_unref (widget);
	}
      g_slist_free (widget_list);
    }
}

void
gtk_item_factory_delete_item (GtkItemFactory         *ifactory,
			      const gchar            *path)
{
  GtkItemFactoryClass *class;
  GtkItemFactoryItem *item;
  gchar *fpath;

  g_return_if_fail (ifactory != NULL);
  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
  g_return_if_fail (path != NULL);

  class = GTK_ITEM_FACTORY_CLASS (GTK_OBJECT (ifactory)->klass);

  fpath = g_strconcat (ifactory->path, path, NULL);
  item = g_hash_table_lookup (class->item_ht, fpath);
  g_free (fpath);

  if (item)
    {
      GtkWidget *widget = NULL;
      GSList *slist;

      for (slist = item->widgets; slist; slist = slist->next)
	{
	  widget = slist->data;

	  if (gtk_item_factory_from_widget (widget) == ifactory)
	    break;
	}

      if (slist)
	gtk_widget_destroy (widget);
    }
}

void
gtk_item_factory_delete_entry (GtkItemFactory         *ifactory,
			       GtkItemFactoryEntry    *entry)
{
  g_return_if_fail (ifactory != NULL);
  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
  g_return_if_fail (entry != NULL);

  gtk_item_factory_delete_item (ifactory, entry->path);
}

void
gtk_item_factory_delete_entries (GtkItemFactory         *ifactory,
				 guint                   n_entries,
				 GtkItemFactoryEntry    *entries)
{
  guint i;

  g_return_if_fail (ifactory != NULL);
  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
  if (n_entries > 0)
    g_return_if_fail (entries != NULL);

  for (i = 0; i < n_entries; i++)
    gtk_item_factory_delete_item (ifactory, (entries + i)->path);
}

typedef struct
{
  guint x;
  guint y;
} MenuPos;

static void
gtk_item_factory_menu_pos (GtkMenu  *menu,
			   gint     *x,
			   gint     *y,
			   gpointer  func_data)
{
  MenuPos *mpos = func_data;

  *x = mpos->x;
  *y = mpos->y;
}

gpointer
gtk_item_factory_popup_data_from_widget (GtkWidget     *widget)
{
  GtkItemFactory *ifactory;
  
  g_return_val_if_fail (widget != NULL, NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

  ifactory = gtk_item_factory_from_widget (widget);
  if (ifactory)
1364
    return gtk_object_get_data_by_id (GTK_OBJECT (ifactory), quark_popup_data);
Tim Janik's avatar
Tim Janik committed
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374

  return NULL;
}

gpointer
gtk_item_factory_popup_data (GtkItemFactory *ifactory)
{
  g_return_val_if_fail (ifactory != NULL, NULL);
  g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);

1375
  return gtk_object_get_data_by_id (GTK_OBJECT (ifactory), quark_popup_data);
Tim Janik's avatar
Tim Janik committed
1376
1377
1378
1379
1380
1381
1382
1383
1384
}

static void
ifactory_delete_popup_data (GtkObject	   *object,
			    GtkItemFactory *ifactory)
{
  gtk_signal_disconnect_by_func (object,
				 GTK_SIGNAL_FUNC (ifactory_delete_popup_data),
				 ifactory);
1385
  gtk_object_remove_data_by_id (GTK_OBJECT (ifactory), quark_popup_data);
Tim Janik's avatar
Tim Janik committed
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
}

void
gtk_item_factory_popup (GtkItemFactory		*ifactory,
			guint			 x,
			guint			 y,
			guint			 mouse_button,
			guint32			 time)
{
  gtk_item_factory_popup_with_data (ifactory, NULL, NULL, x, y, mouse_button, time);
}

void
gtk_item_factory_popup_with_data (GtkItemFactory	*ifactory,
				  gpointer		 popup_data,
				  GtkDestroyNotify	 destroy,
				  guint			 x,
				  guint			 y,
				  guint			 mouse_button,
				  guint32		 time)
{
  g_return_if_fail (ifactory != NULL);
  g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
  g_return_if_fail (GTK_IS_MENU (ifactory->widget));

  if (!GTK_WIDGET_VISIBLE (ifactory->widget))
    {
      MenuPos *mpos;

1415
      mpos = gtk_object_get_data_by_id (GTK_OBJECT (ifactory->widget), quark_if_menu_pos);
Tim Janik's avatar
Tim Janik committed
1416
1417
1418
1419
1420

      if (!mpos)
	{
	  mpos = g_new0 (MenuPos, 1);
	  gtk_object_set_data_by_id_full (GTK_OBJECT (ifactory->widget),
1421
					  quark_if_menu_pos,
Tim Janik's avatar
Tim Janik committed
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
					  mpos,
					  g_free);
	}

      mpos->x = x;
      mpos->y = y;

      if (popup_data != NULL)
	{
	  gtk_object_set_data_by_id_full (GTK_OBJECT (ifactory),
1432
					  quark_popup_data,
Tim Janik's avatar
Tim Janik committed
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
					  popup_data,
					  destroy);
	  gtk_signal_connect (GTK_OBJECT (ifactory->widget),
			      "selection-done",
			      GTK_SIGNAL_FUNC (ifactory_delete_popup_data),
			      ifactory);
	}

      gtk_menu_popup (GTK_MENU (ifactory->widget),
		      NULL, NULL,
		      gtk_item_factory_menu_pos, mpos,
		      mouse_button, time);
    }
}

static guint
gtk_item_factory_parse_menu_path (GScanner            *scanner,
				  GtkItemFactoryClass *class)
{
  GtkItemFactoryItem *item;
  
  g_scanner_get_next_token (scanner);
  if (scanner->token != G_TOKEN_STRING)
    return G_TOKEN_STRING;

  g_scanner_peek_next_token (scanner);
  if (scanner->next_token != G_TOKEN_STRING)
    {
      g_scanner_get_next_token (scanner);
      return G_TOKEN_STRING;
    }

  item = g_hash_table_lookup (class->item_ht, scanner->value.v_string);
  if (!item)
    {
      item = g_chunk_new (GtkItemFactoryItem, ifactory_item_chunks);

      item->path = g_strdup (scanner->value.v_string);
      item->accelerator_key = 0;
      item->accelerator_mods = 0;
1473
      item->modified = TRUE;
Tim Janik's avatar
Tim Janik committed
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
      item->in_propagation = FALSE;
      item->item_type = NULL;
      item->widgets = NULL;

      g_hash_table_insert (class->item_ht, item->path, item);
    }
  g_scanner_get_next_token (scanner);
  
  if (!item->in_propagation)
    {
      guint old_keyval;
      guint old_mods;
      
      old_keyval = item->accelerator_key;
      old_mods = item->accelerator_mods;
      gtk_accelerator_parse (scanner->value.v_string,
			     &item->accelerator_key,
			     &item->accelerator_mods);
      if (old_keyval != item->accelerator_key ||
	  old_mods != item->accelerator_mods)
	{
	  item->modified = TRUE;
	  gtk_item_factory_propagate_accelerator (item, NULL);
	}
    }
  
  g_scanner_get_next_token (scanner);
  if (scanner->token != ')')
    return ')';
  else
    return G_TOKEN_NONE;
}

static void
gtk_item_factory_parse_statement (GScanner            *scanner,
				  GtkItemFactoryClass *class)
{
  guint expected_token;
  
  g_scanner_get_next_token (scanner);
  
  if (scanner->token == G_TOKEN_SYMBOL)
    {
      guint (*parser_func) (GScanner*, GtkItemFactoryClass*);

      parser_func = scanner->value.v_symbol;

1521
      /* check whether this is a GtkItemFactory symbol.
Tim Janik's avatar
Tim Janik committed
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
       */
      if (parser_func == gtk_item_factory_parse_menu_path)
	expected_token = parser_func (scanner, class);
      else
	expected_token = G_TOKEN_SYMBOL;
    }
  else
    expected_token = G_TOKEN_SYMBOL;

  /* skip rest of statement on errrors
   */
  if (expected_token != G_TOKEN_NONE)
    {
      register guint level;

      level = 1;
      if (scanner->token == ')')
	level--;
      if (scanner->token == '(')
	level++;
      
      while (!g_scanner_eof (scanner) && level > 0)
	{
	  g_scanner_get_next_token (scanner);
	  
	  if (scanner->token == '(')
	    level++;
	  else if (scanner->token == ')')
	    level--;
	}
    }
}

void
gtk_item_factory_parse_rc_string (const gchar	 *rc_string)
{
  GScanner *scanner;

  g_return_if_fail (rc_string != NULL);

1562
  ifactory_scanner_config.cpair_comment_single = gtk_item_factory_class->cpair_comment_single;
Tim Janik's avatar
Tim Janik committed
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
  scanner = g_scanner_new (&ifactory_scanner_config);

  g_scanner_input_text (scanner, rc_string, strlen (rc_string));

  gtk_item_factory_parse_rc_scanner (scanner);

  g_scanner_destroy (scanner);
}

void
gtk_item_factory_parse_rc_scanner (GScanner *scanner)
{
  gpointer saved_symbol;

  g_return_if_fail (scanner != NULL);

  if (!gtk_item_factory_class)
    gtk_type_class (GTK_TYPE_ITEM_FACTORY);

  saved_symbol = g_scanner_lookup_symbol (scanner, "menu-path");
  g_scanner_remove_symbol (scanner, "menu-path");
  g_scanner_add_symbol (scanner, "menu-path", gtk_item_factory_parse_menu_path);

  g_scanner_peek_next_token (scanner);

  while (scanner->next_token == '(')
    {
      g_scanner_get_next_token (scanner);

      gtk_item_factory_parse_statement (scanner, gtk_item_factory_class);

      g_scanner_peek_next_token (scanner);
    }

  g_scanner_remove_symbol (scanner, "menu-path");
  g_scanner_add_symbol (scanner, "menu-path", saved_symbol);
}

void
gtk_item_factory_parse_rc (const gchar	  *file_name)
{
  gint fd;
  GScanner *scanner;

  g_return_if_fail (file_name != NULL);

  if (!S_ISREG (g_scanner_stat_mode (file_name)))
    return;

Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
1612
#ifndef __EMX__
Tim Janik's avatar
Tim Janik committed
1613
  fd = open (file_name, O_RDONLY);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
1614
1615
1616
#else
  fd = open (file_name, O_RDONLY | O_TEXT);
#endif
Tim Janik's avatar
Tim Janik committed
1617
1618
1619
  if (fd < 0)
    return;

1620
  ifactory_scanner_config.cpair_comment_single = gtk_item_factory_class->cpair_comment_single;
Tim Janik's avatar
Tim Janik committed
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
  scanner = g_scanner_new (&ifactory_scanner_config);

  g_scanner_input_file (scanner, fd);

  gtk_item_factory_parse_rc_scanner (scanner);

  g_scanner_destroy (scanner);

  close (fd);
}
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646

void
gtk_item_factory_set_translate_func (GtkItemFactory      *ifactory,
				     GtkTranslateFunc     func,
				     gpointer             data,
				     GtkDestroyNotify     notify)
{
  g_return_if_fail (ifactory != NULL);
  
  if (ifactory->translate_data && ifactory->translate_notify)
    ifactory->translate_notify (ifactory->translate_data);
      
  ifactory->translate_func = func;
  ifactory->translate_data = data;
  ifactory->translate_notify = notify;
}