1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GtkItemFactory: Flexible item factory with automatic rc handling
5 * Copyright (C) 1998 Tim Janik
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkitemfactory.h"
33 #include "gtk/gtkmenubar.h"
34 #include "gtk/gtkmenu.h"
35 #include "gtk/gtkmenuitem.h"
36 #include "gtk/gtkradiomenuitem.h"
37 #include "gtk/gtkcheckmenuitem.h"
38 #include "gtk/gtkimagemenuitem.h"
39 #include "gtk/gtktearoffmenuitem.h"
40 #include "gtk/gtkaccelmap.h"
41 #include "gtk/gtkaccellabel.h"
42 #include "gdk/gdkkeysyms.h"
43 #include "gtk/gtkimage.h"
44 #include "gtk/gtkstock.h"
45 #include "gtk/gtkiconfactory.h"
53 #undef GTK_DISABLE_DEPRECATED
54 #include "gtk/gtkoptionmenu.h"
55 #define GTK_DISABLE_DEPRECATED
58 #define ITEM_FACTORY_STRING ((gchar*) item_factory_string)
59 #define ITEM_BLOCK_SIZE (128)
62 /* --- structures --- */
63 typedef struct _GtkIFCBData GtkIFCBData;
64 typedef struct _GtkIFDumpData GtkIFDumpData;
67 GtkItemFactoryCallback func;
70 guint callback_action;
74 /* --- prototypes --- */
75 static void gtk_item_factory_class_init (GtkItemFactoryClass *klass);
76 static void gtk_item_factory_init (GtkItemFactory *ifactory);
77 static void gtk_item_factory_destroy (GtkObject *object);
78 static void gtk_item_factory_finalize (GObject *object);
81 /* --- static variables --- */
82 static GtkItemFactoryClass *gtk_item_factory_class = NULL;
83 static gpointer parent_class = NULL;
84 static const gchar *item_factory_string = "Gtk-<ItemFactory>";
85 static GMemChunk *ifactory_item_chunks = NULL;
86 static GMemChunk *ifactory_cb_data_chunks = NULL;
87 static GQuark quark_popup_data = 0;
88 static GQuark quark_if_menu_pos = 0;
89 static GQuark quark_item_factory = 0;
90 static GQuark quark_item_path = 0;
91 static GQuark quark_action = 0;
92 static GQuark quark_accel_group = 0;
93 static GQuark quark_type_item = 0;
94 static GQuark quark_type_title = 0;
95 static GQuark quark_type_radio_item = 0;
96 static GQuark quark_type_check_item = 0;
97 static GQuark quark_type_toggle_item = 0;
98 static GQuark quark_type_image_item = 0;
99 static GQuark quark_type_stock_item = 0;
100 static GQuark quark_type_tearoff_item = 0;
101 static GQuark quark_type_separator_item = 0;
102 static GQuark quark_type_branch = 0;
103 static GQuark quark_type_last_branch = 0;
106 /* --- functions --- */
108 gtk_item_factory_get_type (void)
110 static GType item_factory_type = 0;
112 if (!item_factory_type)
114 static const GTypeInfo item_factory_info =
116 sizeof (GtkItemFactoryClass),
117 NULL, /* base_init */
118 NULL, /* base_finalize */
119 (GClassInitFunc) gtk_item_factory_class_init,
120 NULL, /* class_finalize */
121 NULL, /* class_data */
122 sizeof (GtkItemFactory),
124 (GInstanceInitFunc) gtk_item_factory_init,
128 g_type_register_static (GTK_TYPE_OBJECT, "GtkItemFactory",
129 &item_factory_info, 0);
132 return item_factory_type;
136 gtk_item_factory_class_init (GtkItemFactoryClass *class)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
139 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
141 gtk_item_factory_class = class;
142 parent_class = g_type_class_peek_parent (class);
144 gobject_class->finalize = gtk_item_factory_finalize;
146 object_class->destroy = gtk_item_factory_destroy;
148 class->item_ht = g_hash_table_new (g_str_hash, g_str_equal);
149 ifactory_item_chunks =
150 g_mem_chunk_new ("GtkItemFactoryItem",
151 sizeof (GtkItemFactoryItem),
152 sizeof (GtkItemFactoryItem) * ITEM_BLOCK_SIZE,
154 ifactory_cb_data_chunks =
155 g_mem_chunk_new ("GtkIFCBData",
156 sizeof (GtkIFCBData),
157 sizeof (GtkIFCBData) * ITEM_BLOCK_SIZE,
160 quark_popup_data = g_quark_from_static_string ("GtkItemFactory-popup-data");
161 quark_if_menu_pos = g_quark_from_static_string ("GtkItemFactory-menu-position");
162 quark_item_factory = g_quark_from_static_string ("GtkItemFactory");
163 quark_item_path = g_quark_from_static_string ("GtkItemFactory-path");
164 quark_action = g_quark_from_static_string ("GtkItemFactory-action");
165 quark_accel_group = g_quark_from_static_string ("GtkAccelGroup");
166 quark_type_item = g_quark_from_static_string ("<Item>");
167 quark_type_title = g_quark_from_static_string ("<Title>");
168 quark_type_radio_item = g_quark_from_static_string ("<RadioItem>");
169 quark_type_check_item = g_quark_from_static_string ("<CheckItem>");
170 quark_type_toggle_item = g_quark_from_static_string ("<ToggleItem>");
171 quark_type_image_item = g_quark_from_static_string ("<ImageItem>");
172 quark_type_stock_item = g_quark_from_static_string ("<StockItem>");
173 quark_type_separator_item = g_quark_from_static_string ("<Separator>");
174 quark_type_tearoff_item = g_quark_from_static_string ("<Tearoff>");
175 quark_type_branch = g_quark_from_static_string ("<Branch>");
176 quark_type_last_branch = g_quark_from_static_string ("<LastBranch>");
180 gtk_item_factory_init (GtkItemFactory *ifactory)
184 object = GTK_OBJECT (ifactory);
186 ifactory->path = NULL;
187 ifactory->accel_group = NULL;
188 ifactory->widget = NULL;
189 ifactory->items = NULL;
190 ifactory->translate_func = NULL;
191 ifactory->translate_data = NULL;
192 ifactory->translate_notify = NULL;
196 * gtk_item_factory_new:
197 * @container_type: the kind of menu to create; can be
198 * #GTK_TYPE_MENU_BAR, #GTK_TYPE_MENU or #GTK_TYPE_OPTION_MENU
199 * @path: the factory path of the new item factory, a string of the form
200 * <literal>"<name>"</literal>
201 * @accel_group: a #GtkAccelGroup to which the accelerators for the
202 * menu items will be added, or %NULL to create a new one
203 * @returns: a new #GtkItemFactory
205 * Creates a new #GtkItemFactory.
207 * Beware that the returned object does not have a floating reference.
210 gtk_item_factory_new (GType container_type,
212 GtkAccelGroup *accel_group)
214 GtkItemFactory *ifactory;
216 g_return_val_if_fail (path != NULL, NULL);
218 ifactory = g_object_new (GTK_TYPE_ITEM_FACTORY, NULL);
219 gtk_item_factory_construct (ifactory, container_type, path, accel_group);
225 gtk_item_factory_callback_marshal (GtkWidget *widget,
232 if (data->callback_type == 1)
234 GtkItemFactoryCallback1 func1 = (GtkItemFactoryCallback1) data->func;
235 func1 (data->func_data, data->callback_action, widget);
237 else if (data->callback_type == 2)
239 GtkItemFactoryCallback2 func2 = (GtkItemFactoryCallback2) data->func;
240 func2 (widget, data->func_data, data->callback_action);
245 gtk_item_factory_item_remove_widget (GtkWidget *widget,
246 GtkItemFactoryItem *item)
248 item->widgets = g_slist_remove (item->widgets, widget);
249 g_object_set_qdata (G_OBJECT (widget), quark_item_factory, NULL);
250 g_object_set_qdata (G_OBJECT (widget), quark_item_path, NULL);
254 * gtk_item_factory_add_foreign:
255 * @accel_widget: widget to install an accelerator on
256 * @full_path: the full path for the @accel_widget
257 * @accel_group: the accelerator group to install the accelerator in
258 * @keyval: key value of the accelerator
259 * @modifiers: modifier combination of the accelerator
261 * Installs an accelerator for @accel_widget in @accel_group, that causes
262 * the ::activate signal to be emitted if the accelerator is activated.
264 * This function can be used to make widgets participate in the accel
265 * saving/restoring functionality provided by gtk_accel_map_save() and
266 * gtk_accel_map_load(), even if they haven't been created by an item
269 * Deprecated: The recommended API for this purpose are the functions
270 * gtk_menu_item_set_accel_path() and gtk_widget_set_accel_path(); don't
271 * use gtk_item_factory_add_foreign() in new code, since it is likely to
272 * be removed in the future.
275 gtk_item_factory_add_foreign (GtkWidget *accel_widget,
276 const gchar *full_path,
277 GtkAccelGroup *accel_group,
279 GdkModifierType modifiers)
281 GtkItemFactoryClass *class;
282 GtkItemFactoryItem *item;
284 g_return_if_fail (GTK_IS_WIDGET (accel_widget));
285 g_return_if_fail (full_path != NULL);
287 class = gtk_type_class (GTK_TYPE_ITEM_FACTORY);
289 keyval = keyval != GDK_VoidSymbol ? keyval : 0;
291 item = g_hash_table_lookup (class->item_ht, full_path);
294 item = g_chunk_new (GtkItemFactoryItem, ifactory_item_chunks);
296 item->path = g_strdup (full_path);
297 item->widgets = NULL;
299 g_hash_table_insert (class->item_ht, item->path, item);
302 item->widgets = g_slist_prepend (item->widgets, accel_widget);
303 g_signal_connect (accel_widget,
305 G_CALLBACK (gtk_item_factory_item_remove_widget),
308 /* set the item path for the widget
310 g_object_set_qdata (G_OBJECT (accel_widget), quark_item_path, item->path);
311 gtk_widget_set_name (accel_widget, item->path);
314 g_object_ref (accel_group);
315 g_object_set_qdata_full (G_OBJECT (accel_widget),
321 g_object_set_qdata (G_OBJECT (accel_widget), quark_accel_group, NULL);
323 /* install defined accelerators
325 if (g_signal_lookup ("activate", G_TYPE_FROM_INSTANCE (accel_widget)))
329 gtk_accel_map_add_entry (full_path, keyval, modifiers);
330 gtk_widget_set_accel_path (accel_widget, full_path, accel_group);
336 ifactory_cb_data_free (gpointer mem)
338 g_mem_chunk_free (ifactory_cb_data_chunks, mem);
342 gtk_item_factory_add_item (GtkItemFactory *ifactory,
344 const gchar *accelerator,
345 GtkItemFactoryCallback callback,
346 guint callback_action,
347 gpointer callback_data,
352 GtkItemFactoryClass *class;
353 GtkItemFactoryItem *item;
356 GdkModifierType mods;
358 g_return_if_fail (widget != NULL);
359 g_return_if_fail (item_type != NULL);
361 class = GTK_ITEM_FACTORY_GET_CLASS (ifactory);
363 /* set accelerator group on menu widgets
365 if (GTK_IS_MENU (widget))
366 gtk_menu_set_accel_group ((GtkMenu*) widget, ifactory->accel_group);
368 /* connect callback if necessary
374 data = g_chunk_new (GtkIFCBData, ifactory_cb_data_chunks);
375 data->func = callback;
376 data->callback_type = callback_type;
377 data->func_data = callback_data;
378 data->callback_action = callback_action;
380 g_object_weak_ref (G_OBJECT (widget),
381 (GWeakNotify) ifactory_cb_data_free,
383 g_signal_connect (widget,
385 G_CALLBACK (gtk_item_factory_callback_marshal),
389 /* link the widget into its item-entry
390 * and keep back pointer on both the item factory and the widget
392 g_object_set_qdata (G_OBJECT (widget), quark_action, GUINT_TO_POINTER (callback_action));
393 g_object_set_qdata (G_OBJECT (widget), quark_item_factory, ifactory);
395 gtk_accelerator_parse (accelerator, &keyval, &mods);
401 fpath = g_strconcat (ifactory->path, path, NULL);
402 gtk_item_factory_add_foreign (widget, fpath, ifactory->accel_group, keyval, mods);
403 item = g_hash_table_lookup (class->item_ht, fpath);
406 g_return_if_fail (item != NULL);
408 if (!g_slist_find (ifactory->items, item))
409 ifactory->items = g_slist_prepend (ifactory->items, item);
413 * gtk_item_factory_construct:
414 * @ifactory: a #GtkItemFactory
415 * @container_type: the kind of menu to create; can be
416 * #GTK_TYPE_MENU_BAR, #GTK_TYPE_MENU or #GTK_TYPE_OPTION_MENU
417 * @path: the factory path of @ifactory, a string of the form
418 * <literal>"<name>"</literal>
419 * @accel_group: a #GtkAccelGroup to which the accelerators for the
420 * menu items will be added, or %NULL to create a new one
422 * Initializes an item factory.
425 gtk_item_factory_construct (GtkItemFactory *ifactory,
426 GType container_type,
428 GtkAccelGroup *accel_group)
432 g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
433 g_return_if_fail (ifactory->accel_group == NULL);
434 g_return_if_fail (path != NULL);
435 if (!g_type_is_a (container_type, GTK_TYPE_OPTION_MENU))
436 g_return_if_fail (g_type_is_a (container_type, GTK_TYPE_MENU_SHELL));
440 if (path[0] != '<' && path[len - 1] != '>')
442 g_warning ("GtkItemFactory: invalid factory path `%s'", path);
448 ifactory->accel_group = accel_group;
449 g_object_ref (ifactory->accel_group);
452 ifactory->accel_group = gtk_accel_group_new ();
454 ifactory->path = g_strdup (path);
455 ifactory->widget = g_object_connect (gtk_widget_new (container_type, NULL),
456 "signal::destroy", gtk_widget_destroyed, &ifactory->widget,
458 g_object_ref (ifactory);
459 gtk_object_sink (GTK_OBJECT (ifactory));
461 gtk_item_factory_add_item (ifactory,
469 * gtk_item_factory_from_path:
470 * @path: a string starting with a factory path of the form
471 * <literal>"<name>"</literal>
472 * @returns: the #GtkItemFactory created for the given factory path, or %NULL
474 * Finds an item factory which has been constructed using the
475 * <literal>"<name>"</literal> prefix of @path as the @path argument
476 * for gtk_item_factory_new().
479 gtk_item_factory_from_path (const gchar *path)
481 GtkItemFactoryClass *class;
482 GtkItemFactoryItem *item;
486 g_return_val_if_fail (path != NULL, NULL);
487 g_return_val_if_fail (path[0] == '<', NULL);
489 class = gtk_type_class (GTK_TYPE_ITEM_FACTORY);
492 while (path[i] && path[i] != '>')
496 g_warning ("gtk_item_factory_from_path(): invalid factory path \"%s\"",
500 fname = g_new (gchar, i + 2);
501 g_memmove (fname, path, i + 1);
504 item = g_hash_table_lookup (class->item_ht, fname);
508 if (item && item->widgets)
509 return gtk_item_factory_from_widget (item->widgets->data);
515 gtk_item_factory_destroy (GtkObject *object)
517 GtkItemFactory *ifactory;
520 g_return_if_fail (GTK_IS_ITEM_FACTORY (object));
522 ifactory = (GtkItemFactory*) object;
524 if (ifactory->widget)
528 dobj = GTK_OBJECT (ifactory->widget);
531 gtk_object_sink (dobj);
532 gtk_object_destroy (dobj);
533 g_object_unref (dobj);
535 ifactory->widget = NULL;
538 for (slist = ifactory->items; slist; slist = slist->next)
540 GtkItemFactoryItem *item = slist->data;
543 for (link = item->widgets; link; link = link->next)
544 if (g_object_get_qdata (link->data, quark_item_factory) == ifactory)
545 g_object_set_qdata (link->data, quark_item_factory, NULL);
547 g_slist_free (ifactory->items);
548 ifactory->items = NULL;
550 GTK_OBJECT_CLASS (parent_class)->destroy (object);
554 gtk_item_factory_finalize (GObject *object)
556 GtkItemFactory *ifactory;
558 g_return_if_fail (GTK_IS_ITEM_FACTORY (object));
560 ifactory = GTK_ITEM_FACTORY (object);
562 g_object_unref (ifactory->accel_group);
563 g_free (ifactory->path);
564 g_assert (ifactory->widget == NULL);
566 if (ifactory->translate_notify)
567 ifactory->translate_notify (ifactory->translate_data);
569 G_OBJECT_CLASS (parent_class)->finalize (object);
573 * gtk_item_factory_from_widget:
575 * @returns: the item factory from which @widget was created, or %NULL
577 * Obtains the item factory from which a widget was created.
580 gtk_item_factory_from_widget (GtkWidget *widget)
582 GtkItemFactory *ifactory;
584 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
586 ifactory = g_object_get_qdata (G_OBJECT (widget), quark_item_factory);
588 if (ifactory == NULL && GTK_IS_MENU_ITEM (widget) &&
589 GTK_MENU_ITEM (widget)->submenu != NULL)
591 GtkWidget *menu = GTK_MENU_ITEM (widget)->submenu;
592 ifactory = g_object_get_qdata (G_OBJECT (menu), quark_item_factory);
599 * gtk_item_factory_path_from_widget:
601 * @returns: the full path to @widget if it has been created by an item
602 * factory, %NULL otherwise. This value is owned by GTK+ and must not be
605 * If @widget has been created by an item factory, returns the full path
606 * to it. (The full path of a widget is the concatenation of the factory
607 * path specified in gtk_item_factory_new() with the path specified in the
608 * #GtkItemFactoryEntry from which the widget was created.)
610 G_CONST_RETURN gchar*
611 gtk_item_factory_path_from_widget (GtkWidget *widget)
615 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
617 path = g_object_get_qdata (G_OBJECT (widget), quark_item_path);
619 if (path == NULL && GTK_IS_MENU_ITEM (widget) &&
620 GTK_MENU_ITEM (widget)->submenu != NULL)
622 GtkWidget *menu = GTK_MENU_ITEM (widget)->submenu;
623 path = g_object_get_qdata (G_OBJECT (menu), quark_item_path);
630 * gtk_item_factory_create_items:
631 * @ifactory: a #GtkItemFactory
632 * @n_entries: the length of @entries
633 * @entries: an array of #GtkItemFactoryEntry<!-- -->s whose @callback members
634 * must by of type #GtkItemFactoryCallback1
635 * @callback_data: data passed to the callback functions of all entries
637 * Creates the menu items from the @entries.
640 gtk_item_factory_create_items (GtkItemFactory *ifactory,
642 GtkItemFactoryEntry *entries,
643 gpointer callback_data)
645 gtk_item_factory_create_items_ac (ifactory, n_entries, entries, callback_data, 1);
649 * gtk_item_factory_create_items_ac:
650 * @ifactory: a #GtkItemFactory
651 * @n_entries: the length of @entries
652 * @entries: an array of #GtkItemFactoryEntry<!-- -->s
653 * @callback_data: data passed to the callback functions of all entries
654 * @callback_type: 1 if the callback functions in @entries are of type
655 * #GtkItemFactoryCallback1, 2 if they are of type #GtkItemFactoryCallback2
657 * Creates the menu items from the @entries.
660 gtk_item_factory_create_items_ac (GtkItemFactory *ifactory,
662 GtkItemFactoryEntry *entries,
663 gpointer callback_data,
668 g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
669 g_return_if_fail (callback_type >= 1 && callback_type <= 2);
674 g_return_if_fail (entries != NULL);
676 for (i = 0; i < n_entries; i++)
677 gtk_item_factory_create_item (ifactory, entries + i, callback_data, callback_type);
681 * gtk_item_factory_get_widget:
682 * @ifactory: a #GtkItemFactory
683 * @path: the path to the widget
684 * @returns: the widget for the given path, or %NULL if @path doesn't lead
687 * Obtains the widget which corresponds to @path.
689 * If the widget corresponding to @path is a menu item which opens a
690 * submenu, then the submenu is returned. If you are interested in the menu
691 * item, use gtk_item_factory_get_item() instead.
694 gtk_item_factory_get_widget (GtkItemFactory *ifactory,
697 GtkItemFactoryClass *class;
698 GtkItemFactoryItem *item;
700 g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);
701 g_return_val_if_fail (path != NULL, NULL);
703 class = GTK_ITEM_FACTORY_GET_CLASS (ifactory);
706 item = g_hash_table_lookup (class->item_ht, (gpointer) path);
711 fpath = g_strconcat (ifactory->path, path, NULL);
712 item = g_hash_table_lookup (class->item_ht, fpath);
720 for (slist = item->widgets; slist; slist = slist->next)
722 if (gtk_item_factory_from_widget (slist->data) == ifactory)
731 * gtk_item_factory_get_widget_by_action:
732 * @ifactory: a #GtkItemFactory
733 * @action: an action as specified in the @callback_action field
734 * of #GtkItemFactoryEntry
735 * @returns: the widget which corresponds to the given action, or %NULL
736 * if no widget was found
738 * Obtains the widget which was constructed from the #GtkItemFactoryEntry
739 * with the given @action.
741 * If there are multiple items with the same action, the result is
745 gtk_item_factory_get_widget_by_action (GtkItemFactory *ifactory,
750 g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);
752 for (slist = ifactory->items; slist; slist = slist->next)
754 GtkItemFactoryItem *item = slist->data;
757 for (link = item->widgets; link; link = link->next)
758 if (g_object_get_qdata (link->data, quark_item_factory) == ifactory &&
759 g_object_get_qdata (link->data, quark_action) == GUINT_TO_POINTER (action))
767 * gtk_item_factory_get_item:
768 * @ifactory: a #GtkItemFactory
769 * @path: the path to the menu item
770 * @returns: the menu item for the given path, or %NULL if @path doesn't
771 * lead to a menu item
773 * Obtains the menu item which corresponds to @path.
775 * If the widget corresponding to @path is a menu item which opens a
776 * submenu, then the item is returned. If you are interested in the submenu,
777 * use gtk_item_factory_get_widget() instead.
780 gtk_item_factory_get_item (GtkItemFactory *ifactory,
785 g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);
786 g_return_val_if_fail (path != NULL, NULL);
788 widget = gtk_item_factory_get_widget (ifactory, path);
790 if (GTK_IS_MENU (widget))
791 widget = gtk_menu_get_attach_widget (GTK_MENU (widget));
793 return GTK_IS_ITEM (widget) ? widget : NULL;
798 * gtk_item_factory_get_item_by_action:
799 * @ifactory: a #GtkItemFactory
800 * @action: an action as specified in the @callback_action field
801 * of #GtkItemFactoryEntry
802 * @returns: the menu item which corresponds to the given action, or %NULL
803 * if no menu item was found
805 * Obtains the menu item which was constructed from the first
806 * #GtkItemFactoryEntry with the given @action.
809 gtk_item_factory_get_item_by_action (GtkItemFactory *ifactory,
814 g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);
816 widget = gtk_item_factory_get_widget_by_action (ifactory, action);
818 if (GTK_IS_MENU (widget))
819 widget = gtk_menu_get_attach_widget (GTK_MENU (widget));
821 return GTK_IS_ITEM (widget) ? widget : NULL;
825 item_factory_find_separator_r (char *path)
827 gchar *result = NULL;
828 gboolean escaped = FALSE;
838 else if (*path == '/')
849 item_factory_unescape_label (const char *label)
851 char *new = g_malloc (strlen (label) + 1);
853 gboolean escaped = FALSE;
879 gtk_item_factory_parse_path (GtkItemFactory *ifactory,
888 *path = g_strdup (str);
909 *parent_path = g_strdup (*path);
910 p = item_factory_find_separator_r (*parent_path);
913 g_warning ("GtkItemFactory: invalid entry path `%s'", str);
918 if (ifactory->translate_func)
919 translation = ifactory->translate_func (str, ifactory->translate_data);
923 p = item_factory_find_separator_r (translation);
929 *item = item_factory_unescape_label (p);
935 * gtk_item_factory_create_item:
936 * @ifactory: a #GtkItemFactory
937 * @entry: the #GtkItemFactoryEntry to create an item for
938 * @callback_data: data passed to the callback function of @entry
939 * @callback_type: 1 if the callback function of @entry is of type
940 * #GtkItemFactoryCallback1, 2 if it is of type #GtkItemFactoryCallback2
942 * Creates an item for @entry.
945 gtk_item_factory_create_item (GtkItemFactory *ifactory,
946 GtkItemFactoryEntry *entry,
947 gpointer callback_data,
950 GtkOptionMenu *option_menu = NULL;
961 gchar *item_type_path;
962 GtkStockItem stock_item;
964 g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
965 g_return_if_fail (entry != NULL);
966 g_return_if_fail (entry->path != NULL);
967 g_return_if_fail (entry->path[0] == '/');
968 g_return_if_fail (callback_type >= 1 && callback_type <= 2);
970 if (!entry->item_type ||
971 entry->item_type[0] == 0)
973 item_type_path = "<Item>";
974 type_id = quark_type_item;
978 item_type_path = entry->item_type;
979 type_id = g_quark_try_string (item_type_path);
983 if (type_id == quark_type_item)
984 type = GTK_TYPE_MENU_ITEM;
985 else if (type_id == quark_type_title)
986 type = GTK_TYPE_MENU_ITEM;
987 else if (type_id == quark_type_radio_item)
988 type = GTK_TYPE_RADIO_MENU_ITEM;
989 else if (type_id == quark_type_check_item)
990 type = GTK_TYPE_CHECK_MENU_ITEM;
991 else if (type_id == quark_type_image_item)
992 type = GTK_TYPE_IMAGE_MENU_ITEM;
993 else if (type_id == quark_type_stock_item)
994 type = GTK_TYPE_IMAGE_MENU_ITEM;
995 else if (type_id == quark_type_tearoff_item)
996 type = GTK_TYPE_TEAROFF_MENU_ITEM;
997 else if (type_id == quark_type_toggle_item)
998 type = GTK_TYPE_CHECK_MENU_ITEM;
999 else if (type_id == quark_type_separator_item)
1000 type = GTK_TYPE_MENU_ITEM;
1001 else if (type_id == quark_type_branch)
1002 type = GTK_TYPE_MENU_ITEM;
1003 else if (type_id == quark_type_last_branch)
1004 type = GTK_TYPE_MENU_ITEM;
1007 GtkWidget *radio_link;
1009 radio_link = gtk_item_factory_get_widget (ifactory, item_type_path);
1010 if (radio_link && GTK_IS_RADIO_MENU_ITEM (radio_link))
1012 type = GTK_TYPE_RADIO_MENU_ITEM;
1013 radio_group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (radio_link));
1017 g_warning ("GtkItemFactory: entry path `%s' has invalid type `%s'",
1024 if (!gtk_item_factory_parse_path (ifactory, entry->path,
1025 &path, &parent_path, &name))
1028 parent = gtk_item_factory_get_widget (ifactory, parent_path);
1031 GtkItemFactoryEntry pentry;
1034 ppath = g_strdup (entry->path);
1035 p = item_factory_find_separator_r (ppath);
1036 g_return_if_fail (p != NULL);
1038 pentry.path = ppath;
1039 pentry.accelerator = NULL;
1040 pentry.callback = NULL;
1041 pentry.callback_action = 0;
1042 pentry.item_type = "<Branch>";
1044 gtk_item_factory_create_item (ifactory, &pentry, NULL, 1);
1047 parent = gtk_item_factory_get_widget (ifactory, parent_path);
1048 g_return_if_fail (parent != NULL);
1051 if (GTK_IS_OPTION_MENU (parent))
1053 option_menu = GTK_OPTION_MENU (parent);
1054 if (!option_menu->menu)
1056 GtkWidget *menu = g_object_new (GTK_TYPE_MENU, NULL);
1057 gchar *p = g_strconcat (ifactory->path, parent_path, NULL);
1059 gtk_menu_set_accel_path (GTK_MENU (menu), p);
1061 gtk_option_menu_set_menu (option_menu, menu);
1063 parent = option_menu->menu;
1065 g_free (parent_path);
1067 g_return_if_fail (GTK_IS_CONTAINER (parent));
1069 accelerator = entry->accelerator;
1071 widget = gtk_widget_new (type,
1073 "sensitive", (type_id != quark_type_separator_item &&
1074 type_id != quark_type_title),
1077 if (option_menu && !option_menu->menu_item)
1078 gtk_option_menu_set_history (option_menu, 0);
1080 if (GTK_IS_RADIO_MENU_ITEM (widget))
1081 gtk_radio_menu_item_set_group (GTK_RADIO_MENU_ITEM (widget), radio_group);
1082 if (type_id == quark_type_image_item)
1084 GdkPixbuf *pixbuf = NULL;
1086 if (entry->extra_data)
1088 pixbuf = gdk_pixbuf_new_from_inline (-1,
1093 image = gtk_image_new_from_pixbuf (pixbuf);
1097 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (widget), image);
1098 gtk_widget_show (image);
1101 g_object_unref (pixbuf);
1103 if (type_id == quark_type_stock_item)
1105 image = gtk_image_new_from_stock (entry->extra_data, GTK_ICON_SIZE_MENU);
1106 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (widget), image);
1107 gtk_widget_show (image);
1109 if (gtk_stock_lookup (entry->extra_data, &stock_item))
1112 accelerator = gtk_accelerator_name (stock_item.keyval, stock_item.modifier);
1116 /* install underline accelerators for this item
1118 if (type_id != quark_type_separator_item &&
1119 type_id != quark_type_tearoff_item &&
1124 label = gtk_widget_new (GTK_TYPE_ACCEL_LABEL,
1127 "accel_widget", widget,
1130 gtk_label_set_text_with_mnemonic (GTK_LABEL (label), name);
1135 if (type_id == quark_type_branch ||
1136 type_id == quark_type_last_branch)
1140 if (entry->callback)
1141 g_warning ("gtk_item_factory_create_item(): Can't specify a callback on a branch: \"%s\"",
1143 if (type_id == quark_type_last_branch)
1144 gtk_menu_item_set_right_justified (GTK_MENU_ITEM (widget), TRUE);
1147 widget = gtk_widget_new (GTK_TYPE_MENU, NULL);
1148 p = g_strconcat (ifactory->path, path, NULL);
1149 gtk_menu_set_accel_path (GTK_MENU (widget), p);
1152 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), widget);
1155 gtk_item_factory_add_item (ifactory,
1157 (type_id == quark_type_branch ||
1158 type_id == quark_type_last_branch) ?
1159 (GtkItemFactoryCallback) NULL : entry->callback,
1160 entry->callback_action, callback_data,
1164 if (accelerator != entry->accelerator)
1165 g_free (accelerator);
1170 * gtk_item_factory_create_menu_entries:
1171 * @n_entries: the length of @entries
1172 * @entries: an array of #GtkMenuEntry<!-- -->s
1174 * Creates the menu items from the @entries.
1177 gtk_item_factory_create_menu_entries (guint n_entries,
1178 GtkMenuEntry *entries)
1180 static GPatternSpec *pspec_separator = NULL;
1181 static GPatternSpec *pspec_check = NULL;
1186 g_return_if_fail (entries != NULL);
1188 if (!pspec_separator)
1190 pspec_separator = g_pattern_spec_new ("*<separator>*");
1191 pspec_check = g_pattern_spec_new ("*<check>*");
1194 for (i = 0; i < n_entries; i++)
1196 GtkItemFactory *ifactory;
1197 GtkItemFactoryEntry entry;
1201 path = entries[i].path;
1202 ifactory = gtk_item_factory_from_path (path);
1205 g_warning ("gtk_item_factory_create_menu_entries(): "
1206 "entry[%u] refers to unknown item factory: \"%s\"",
1207 i, entries[i].path);
1211 while (*path != '>')
1217 entry.accelerator = entries[i].accelerator;
1218 entry.callback = entries[i].callback;
1219 entry.callback_action = 0;
1220 if (g_pattern_match_string (pspec_separator, path))
1221 entry.item_type = "<Separator>";
1222 else if (!g_pattern_match_string (pspec_check, path))
1223 entry.item_type = NULL;
1226 gboolean in_brace = FALSE;
1229 cpath = g_new (gchar, strlen (path));
1235 else if (*path == '>')
1242 entry.item_type = "<ToggleItem>";
1246 gtk_item_factory_create_item (ifactory, &entry, entries[i].callback_data, 2);
1247 entries[i].widget = gtk_item_factory_get_widget (ifactory, entries[i].path);
1253 * gtk_item_factories_path_delete:
1254 * @ifactory_path: a factory path to prepend to @path. May be %NULL if @path
1255 * starts with a factory path
1258 * Deletes all widgets constructed from the specified path.
1261 gtk_item_factories_path_delete (const gchar *ifactory_path,
1264 GtkItemFactoryClass *class;
1265 GtkItemFactoryItem *item;
1267 g_return_if_fail (path != NULL);
1269 class = gtk_type_class (GTK_TYPE_ITEM_FACTORY);
1272 item = g_hash_table_lookup (class->item_ht, (gpointer) path);
1277 g_return_if_fail (ifactory_path != NULL);
1279 fpath = g_strconcat (ifactory_path, path, NULL);
1280 item = g_hash_table_lookup (class->item_ht, fpath);
1286 GSList *widget_list;
1290 for (slist = item->widgets; slist; slist = slist->next)
1294 widget = slist->data;
1295 widget_list = g_slist_prepend (widget_list, widget);
1296 g_object_ref (widget);
1299 for (slist = widget_list; slist; slist = slist->next)
1303 widget = slist->data;
1304 gtk_widget_destroy (widget);
1305 g_object_unref (widget);
1307 g_slist_free (widget_list);
1312 * gtk_item_factory_delete_item:
1313 * @ifactory: a #GtkItemFactory
1316 * Deletes the menu item which was created for @path by the given
1320 gtk_item_factory_delete_item (GtkItemFactory *ifactory,
1323 GtkItemFactoryClass *class;
1326 g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
1327 g_return_if_fail (path != NULL);
1329 class = GTK_ITEM_FACTORY_GET_CLASS (ifactory);
1331 widget = gtk_item_factory_get_widget (ifactory, path);
1335 if (GTK_IS_MENU (widget))
1336 widget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1338 gtk_widget_destroy (widget);
1343 * gtk_item_factory_delete_entry:
1344 * @ifactory: a #GtkItemFactory
1345 * @entry: a #GtkItemFactoryEntry
1347 * Deletes the menu item which was created from @entry by the given
1351 gtk_item_factory_delete_entry (GtkItemFactory *ifactory,
1352 GtkItemFactoryEntry *entry)
1358 g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
1359 g_return_if_fail (entry != NULL);
1360 g_return_if_fail (entry->path != NULL);
1361 g_return_if_fail (entry->path[0] == '/');
1363 if (!gtk_item_factory_parse_path (ifactory, entry->path,
1364 &path, &parent_path, &name))
1367 gtk_item_factory_delete_item (ifactory, path);
1370 g_free (parent_path);
1375 * gtk_item_factory_delete_entries:
1376 * @ifactory: a #GtkItemFactory
1377 * @n_entries: the length of @entries
1378 * @entries: an array of #GtkItemFactoryEntry<!-- -->s
1380 * Deletes the menu items which were created from the @entries by the given
1384 gtk_item_factory_delete_entries (GtkItemFactory *ifactory,
1386 GtkItemFactoryEntry *entries)
1390 g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
1392 g_return_if_fail (entries != NULL);
1394 for (i = 0; i < n_entries; i++)
1395 gtk_item_factory_delete_entry (ifactory, entries + i);
1405 gtk_item_factory_menu_pos (GtkMenu *menu,
1411 MenuPos *mpos = func_data;
1418 * gtk_item_factory_popup_data_from_widget:
1420 * @returns: @popup_data associated with the item factory from
1421 * which @widget was created, or %NULL if @widget wasn't created
1422 * by an item factory
1424 * Obtains the @popup_data which was passed to
1425 * gtk_item_factory_popup_with_data(). This data is available until the menu
1426 * is popped down again.
1429 gtk_item_factory_popup_data_from_widget (GtkWidget *widget)
1431 GtkItemFactory *ifactory;
1433 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1435 ifactory = gtk_item_factory_from_widget (widget);
1437 return g_object_get_qdata (G_OBJECT (ifactory), quark_popup_data);
1443 * gtk_item_factory_popup_data:
1444 * @ifactory: a #GtkItemFactory
1445 * @returns: @popup_data associated with @ifactory
1447 * Obtains the @popup_data which was passed to
1448 * gtk_item_factory_popup_with_data(). This data is available until the menu
1449 * is popped down again.
1452 gtk_item_factory_popup_data (GtkItemFactory *ifactory)
1454 g_return_val_if_fail (GTK_IS_ITEM_FACTORY (ifactory), NULL);
1456 return g_object_get_qdata (G_OBJECT (ifactory), quark_popup_data);
1460 ifactory_delete_popup_data (GtkObject *object,
1461 GtkItemFactory *ifactory)
1463 g_signal_handlers_disconnect_by_func (object,
1464 ifactory_delete_popup_data,
1466 g_object_set_qdata (G_OBJECT (ifactory), quark_popup_data, NULL);
1470 * gtk_item_factory_popup:
1471 * @ifactory: a #GtkItemFactory of type #GTK_TYPE_MENU (see gtk_item_factory_new())
1472 * @x: the x position
1473 * @y: the y position
1474 * @mouse_button: the mouse button which was pressed to initiate the popup
1475 * @time_: the time at which the activation event occurred
1477 * Pops up the menu constructed from the item factory at (@x, @y).
1479 * The @mouse_button parameter should be the mouse button pressed to initiate
1480 * the menu popup. If the menu popup was initiated by something other than
1481 * a mouse button press, such as a mouse button release or a keypress,
1482 * @mouse_button should be 0.
1484 * The @time_ parameter should be the time stamp of the event that
1485 * initiated the popup. If such an event is not available, use
1486 * gtk_get_current_event_time() instead.
1488 * The operation of the @mouse_button and the @time_ parameter is the same
1489 * as the @button and @activation_time parameters for gtk_menu_popup().
1492 gtk_item_factory_popup (GtkItemFactory *ifactory,
1498 gtk_item_factory_popup_with_data (ifactory, NULL, NULL, x, y, mouse_button, time);
1502 * gtk_item_factory_popup_with_data:
1503 * @ifactory: a #GtkItemFactory of type #GTK_TYPE_MENU (see gtk_item_factory_new())
1504 * @popup_data: data available for callbacks while the menu is posted
1505 * @destroy: a #GtkDestroyNotify function to be called on @popup_data when
1506 * the menu is unposted
1507 * @x: the x position
1508 * @y: the y position
1509 * @mouse_button: the mouse button which was pressed to initiate the popup
1510 * @time_: the time at which the activation event occurred
1512 * Pops up the menu constructed from the item factory at (@x, @y). Callbacks
1513 * can access the @popup_data while the menu is posted via
1514 * gtk_item_factory_popup_data() and gtk_item_factory_popup_data_from_widget().
1516 * The @mouse_button parameter should be the mouse button pressed to initiate
1517 * the menu popup. If the menu popup was initiated by something other than
1518 * a mouse button press, such as a mouse button release or a keypress,
1519 * @mouse_button should be 0.
1521 * The @time_ parameter should be the time stamp of the event that
1522 * initiated the popup. If such an event is not available, use
1523 * gtk_get_current_event_time() instead.
1525 * The operation of the @mouse_button and the @time_ parameters is the same
1526 * as the @button and @activation_time parameters for gtk_menu_popup().
1529 gtk_item_factory_popup_with_data (GtkItemFactory *ifactory,
1530 gpointer popup_data,
1531 GtkDestroyNotify destroy,
1539 g_return_if_fail (GTK_IS_ITEM_FACTORY (ifactory));
1540 g_return_if_fail (GTK_IS_MENU (ifactory->widget));
1542 mpos = g_object_get_qdata (G_OBJECT (ifactory->widget), quark_if_menu_pos);
1546 mpos = g_new0 (MenuPos, 1);
1547 g_object_set_qdata_full (G_OBJECT (ifactory->widget),
1556 if (popup_data != NULL)
1558 g_object_set_qdata_full (G_OBJECT (ifactory),
1562 g_signal_connect (ifactory->widget,
1564 G_CALLBACK (ifactory_delete_popup_data),
1568 gtk_menu_popup (GTK_MENU (ifactory->widget),
1570 gtk_item_factory_menu_pos, mpos,
1571 mouse_button, time);
1575 * gtk_item_factory_set_translate_func:
1576 * @ifactory: a #GtkItemFactory
1577 * @func: the #GtkTranslateFunc function to be used to translate path elements
1578 * @data: data to pass to @func and @notify
1579 * @notify: a #GtkDestroyNotify function to be called when @ifactory is
1580 * destroyed and when the translation function is changed again
1582 * Sets a function to be used for translating the path elements before they
1586 gtk_item_factory_set_translate_func (GtkItemFactory *ifactory,
1587 GtkTranslateFunc func,
1589 GtkDestroyNotify notify)
1591 g_return_if_fail (ifactory != NULL);
1593 if (ifactory->translate_notify)
1594 ifactory->translate_notify (ifactory->translate_data);
1596 ifactory->translate_func = func;
1597 ifactory->translate_data = data;
1598 ifactory->translate_notify = notify;