docs: Fix GApplicationCommandLine typo
[glib.git] / gio / gmenu.c
blobc80b6bf00b8f021b252c01821bc95bbab4acc7fc
1 /*
2 * Copyright © 2011 Canonical Ltd.
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "config.h"
24 #include "gmenu.h"
26 #include <string.h>
28 /**
29 * SECTION:gmenu
30 * @title: GMenu
31 * @short_description: A simple implementation of GMenuModel
33 * #GMenu is a simple implementation of #GMenuModel.
34 * You populate a #GMenu by adding #GMenuItem instances to it.
36 * There are some convenience functions to allow you to directly
37 * add items (avoiding #GMenuItem) for the common cases. To add
38 * a regular item, use g_menu_insert(). To add a section, use
39 * g_menu_insert_section(). To add a submenu, use
40 * g_menu_insert_submenu().
43 /**
44 * GMenu:
46 * #GMenu is an opaque structure type. You must access it using the
47 * functions below.
49 * Since: 2.32
52 /**
53 * GMenuItem:
55 * #GMenuItem is an opaque structure type. You must access it using the
56 * functions below.
58 * Since: 2.32
61 struct _GMenuItem
63 GObject parent_instance;
65 GHashTable *attributes;
66 GHashTable *links;
67 gboolean cow;
70 typedef GObjectClass GMenuItemClass;
72 struct _GMenu
74 GMenuModel parent_instance;
76 GArray *items;
77 gboolean mutable;
80 typedef GMenuModelClass GMenuClass;
82 G_DEFINE_TYPE (GMenu, g_menu, G_TYPE_MENU_MODEL)
83 G_DEFINE_TYPE (GMenuItem, g_menu_item, G_TYPE_OBJECT)
85 struct item
87 GHashTable *attributes;
88 GHashTable *links;
91 static gboolean
92 g_menu_is_mutable (GMenuModel *model)
94 GMenu *menu = G_MENU (model);
96 return menu->mutable;
99 static gint
100 g_menu_get_n_items (GMenuModel *model)
102 GMenu *menu = G_MENU (model);
104 return menu->items->len;
107 static void
108 g_menu_get_item_attributes (GMenuModel *model,
109 gint position,
110 GHashTable **table)
112 GMenu *menu = G_MENU (model);
114 *table = g_hash_table_ref (g_array_index (menu->items, struct item, position).attributes);
117 static void
118 g_menu_get_item_links (GMenuModel *model,
119 gint position,
120 GHashTable **table)
122 GMenu *menu = G_MENU (model);
124 *table = g_hash_table_ref (g_array_index (menu->items, struct item, position).links);
128 * g_menu_insert_item:
129 * @menu: a #GMenu
130 * @position: the position at which to insert the item
131 * @item: the #GMenuItem to insert
133 * Inserts @item into @menu.
135 * The "insertion" is actually done by copying all of the attribute and
136 * link values of @item and using them to form a new item within @menu.
137 * As such, @item itself is not really inserted, but rather, a menu item
138 * that is exactly the same as the one presently described by @item.
140 * This means that @item is essentially useless after the insertion
141 * occurs. Any changes you make to it are ignored unless it is inserted
142 * again (at which point its updated values will be copied).
144 * You should probably just free @item once you're done.
146 * There are many convenience functions to take care of common cases.
147 * See g_menu_insert(), g_menu_insert_section() and
148 * g_menu_insert_submenu() as well as "prepend" and "append" variants of
149 * each of these functions.
151 * Since: 2.32
153 void
154 g_menu_insert_item (GMenu *menu,
155 gint position,
156 GMenuItem *item)
158 struct item new_item;
160 g_return_if_fail (G_IS_MENU (menu));
161 g_return_if_fail (G_IS_MENU_ITEM (item));
163 if (position < 0 || position > menu->items->len)
164 position = menu->items->len;
166 new_item.attributes = g_hash_table_ref (item->attributes);
167 new_item.links = g_hash_table_ref (item->links);
168 item->cow = TRUE;
170 g_array_insert_val (menu->items, position, new_item);
171 g_menu_model_items_changed (G_MENU_MODEL (menu), position, 0, 1);
175 * g_menu_prepend_item:
176 * @menu: a #GMenu
177 * @item: a #GMenuItem to prepend
179 * Prepends @item to the start of @menu.
181 * See g_menu_insert_item() for more information.
183 * Since: 2.32
185 void
186 g_menu_prepend_item (GMenu *menu,
187 GMenuItem *item)
189 g_menu_insert_item (menu, 0, item);
193 * g_menu_append_item:
194 * @menu: a #GMenu
195 * @item: a #GMenuItem to append
197 * Appends @item to the end of @menu.
199 * See g_menu_insert_item() for more information.
201 * Since: 2.32
203 void
204 g_menu_append_item (GMenu *menu,
205 GMenuItem *item)
207 g_menu_insert_item (menu, -1, item);
211 * g_menu_freeze:
212 * @menu: a #GMenu
214 * Marks @menu as frozen.
216 * After the menu is frozen, it is an error to attempt to make any
217 * changes to it. In effect this means that the #GMenu API must no
218 * longer be used.
220 * This function causes g_menu_model_is_mutable() to begin returning
221 * %FALSE, which has some positive performance implications.
223 * Since: 2.32
225 void
226 g_menu_freeze (GMenu *menu)
228 g_return_if_fail (G_IS_MENU (menu));
230 menu->mutable = FALSE;
234 * g_menu_new:
236 * Creates a new #GMenu.
238 * The new menu has no items.
240 * Returns: a new #GMenu
242 * Since: 2.32
244 GMenu *
245 g_menu_new (void)
247 return g_object_new (G_TYPE_MENU, NULL);
251 * g_menu_insert:
252 * @menu: a #GMenu
253 * @position: the position at which to insert the item
254 * @label: (allow-none): the section label, or %NULL
255 * @detailed_action: (allow-none): the detailed action string, or %NULL
257 * Convenience function for inserting a normal menu item into @menu.
258 * Combine g_menu_new() and g_menu_insert_item() for a more flexible
259 * alternative.
261 * Since: 2.32
263 void
264 g_menu_insert (GMenu *menu,
265 gint position,
266 const gchar *label,
267 const gchar *detailed_action)
269 GMenuItem *menu_item;
271 menu_item = g_menu_item_new (label, detailed_action);
272 g_menu_insert_item (menu, position, menu_item);
273 g_object_unref (menu_item);
277 * g_menu_prepend:
278 * @menu: a #GMenu
279 * @label: (allow-none): the section label, or %NULL
280 * @detailed_action: (allow-none): the detailed action string, or %NULL
282 * Convenience function for prepending a normal menu item to the start
283 * of @menu. Combine g_menu_new() and g_menu_insert_item() for a more
284 * flexible alternative.
286 * Since: 2.32
288 void
289 g_menu_prepend (GMenu *menu,
290 const gchar *label,
291 const gchar *detailed_action)
293 g_menu_insert (menu, 0, label, detailed_action);
297 * g_menu_append:
298 * @menu: a #GMenu
299 * @label: (allow-none): the section label, or %NULL
300 * @detailed_action: (allow-none): the detailed action string, or %NULL
302 * Convenience function for appending a normal menu item to the end of
303 * @menu. Combine g_menu_new() and g_menu_insert_item() for a more
304 * flexible alternative.
306 * Since: 2.32
308 void
309 g_menu_append (GMenu *menu,
310 const gchar *label,
311 const gchar *detailed_action)
313 g_menu_insert (menu, -1, label, detailed_action);
317 * g_menu_insert_section:
318 * @menu: a #GMenu
319 * @position: the position at which to insert the item
320 * @label: (allow-none): the section label, or %NULL
321 * @section: a #GMenuModel with the items of the section
323 * Convenience function for inserting a section menu item into @menu.
324 * Combine g_menu_item_new_section() and g_menu_insert_item() for a more
325 * flexible alternative.
327 * Since: 2.32
329 void
330 g_menu_insert_section (GMenu *menu,
331 gint position,
332 const gchar *label,
333 GMenuModel *section)
335 GMenuItem *menu_item;
337 menu_item = g_menu_item_new_section (label, section);
338 g_menu_insert_item (menu, position, menu_item);
339 g_object_unref (menu_item);
344 * g_menu_prepend_section:
345 * @menu: a #GMenu
346 * @label: (allow-none): the section label, or %NULL
347 * @section: a #GMenuModel with the items of the section
349 * Convenience function for prepending a section menu item to the start
350 * of @menu. Combine g_menu_item_new_section() and g_menu_insert_item() for
351 * a more flexible alternative.
353 * Since: 2.32
355 void
356 g_menu_prepend_section (GMenu *menu,
357 const gchar *label,
358 GMenuModel *section)
360 g_menu_insert_section (menu, 0, label, section);
364 * g_menu_append_section:
365 * @menu: a #GMenu
366 * @label: (allow-none): the section label, or %NULL
367 * @section: a #GMenuModel with the items of the section
369 * Convenience function for appending a section menu item to the end of
370 * @menu. Combine g_menu_item_new_section() and g_menu_insert_item() for a
371 * more flexible alternative.
373 * Since: 2.32
375 void
376 g_menu_append_section (GMenu *menu,
377 const gchar *label,
378 GMenuModel *section)
380 g_menu_insert_section (menu, -1, label, section);
384 * g_menu_insert_submenu:
385 * @menu: a #GMenu
386 * @position: the position at which to insert the item
387 * @label: (allow-none): the section label, or %NULL
388 * @submenu: a #GMenuModel with the items of the submenu
390 * Convenience function for inserting a submenu menu item into @menu.
391 * Combine g_menu_item_new_submenu() and g_menu_insert_item() for a more
392 * flexible alternative.
394 * Since: 2.32
396 void
397 g_menu_insert_submenu (GMenu *menu,
398 gint position,
399 const gchar *label,
400 GMenuModel *submenu)
402 GMenuItem *menu_item;
404 menu_item = g_menu_item_new_submenu (label, submenu);
405 g_menu_insert_item (menu, position, menu_item);
406 g_object_unref (menu_item);
410 * g_menu_prepend_submenu:
411 * @menu: a #GMenu
412 * @label: (allow-none): the section label, or %NULL
413 * @submenu: a #GMenuModel with the items of the submenu
415 * Convenience function for prepending a submenu menu item to the start
416 * of @menu. Combine g_menu_item_new_submenu() and g_menu_insert_item() for
417 * a more flexible alternative.
419 * Since: 2.32
421 void
422 g_menu_prepend_submenu (GMenu *menu,
423 const gchar *label,
424 GMenuModel *submenu)
426 g_menu_insert_submenu (menu, 0, label, submenu);
430 * g_menu_append_submenu:
431 * @menu: a #GMenu
432 * @label: (allow-none): the section label, or %NULL
433 * @submenu: a #GMenuModel with the items of the submenu
435 * Convenience function for appending a submenu menu item to the end of
436 * @menu. Combine g_menu_item_new_submenu() and g_menu_insert_item() for a
437 * more flexible alternative.
439 * Since: 2.32
441 void
442 g_menu_append_submenu (GMenu *menu,
443 const gchar *label,
444 GMenuModel *submenu)
446 g_menu_insert_submenu (menu, -1, label, submenu);
449 static void
450 g_menu_clear_item (struct item *item)
452 if (item->attributes != NULL)
453 g_hash_table_unref (item->attributes);
454 if (item->links != NULL);
455 g_hash_table_unref (item->links);
459 * g_menu_remove:
460 * @menu: a #GMenu
461 * @position: the position of the item to remove
463 * Removes an item from the menu.
465 * @position gives the index of the item to remove.
467 * It is an error if position is not in range the range from 0 to one
468 * less than the number of items in the menu.
470 * It is not possible to remove items by identity since items are added
471 * to the menu simply by copying their links and attributes (ie:
472 * identity of the item itself is not preserved).
474 * Since: 2.32
476 void
477 g_menu_remove (GMenu *menu,
478 gint position)
480 g_return_if_fail (G_IS_MENU (menu));
481 g_return_if_fail (0 <= position && position < menu->items->len);
483 g_menu_clear_item (&g_array_index (menu->items, struct item, position));
484 g_array_remove_index (menu->items, position);
485 g_menu_model_items_changed (G_MENU_MODEL (menu), position, 1, 0);
488 static void
489 g_menu_finalize (GObject *object)
491 GMenu *menu = G_MENU (object);
492 struct item *items;
493 gint n_items;
494 gint i;
496 n_items = menu->items->len;
497 items = (struct item *) g_array_free (menu->items, FALSE);
498 for (i = 0; i < n_items; i++)
499 g_menu_clear_item (&items[i]);
500 g_free (items);
502 G_OBJECT_CLASS (g_menu_parent_class)
503 ->finalize (object);
506 static void
507 g_menu_init (GMenu *menu)
509 menu->items = g_array_new (FALSE, FALSE, sizeof (struct item));
510 menu->mutable = TRUE;
513 static void
514 g_menu_class_init (GMenuClass *class)
516 GMenuModelClass *model_class = G_MENU_MODEL_CLASS (class);
517 GObjectClass *object_class = G_OBJECT_CLASS (class);
519 object_class->finalize = g_menu_finalize;
521 model_class->is_mutable = g_menu_is_mutable;
522 model_class->get_n_items = g_menu_get_n_items;
523 model_class->get_item_attributes = g_menu_get_item_attributes;
524 model_class->get_item_links = g_menu_get_item_links;
528 static void
529 g_menu_item_clear_cow (GMenuItem *menu_item)
531 if (menu_item->cow)
533 GHashTableIter iter;
534 GHashTable *new;
535 gpointer key;
536 gpointer val;
538 new = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
539 g_hash_table_iter_init (&iter, menu_item->attributes);
540 while (g_hash_table_iter_next (&iter, &key, &val))
541 g_hash_table_insert (new, g_strdup (key), g_variant_ref (val));
542 g_hash_table_unref (menu_item->attributes);
543 menu_item->attributes = new;
545 new = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_object_unref);
546 g_hash_table_iter_init (&iter, menu_item->links);
547 while (g_hash_table_iter_next (&iter, &key, &val))
548 g_hash_table_insert (new, g_strdup (key), g_object_ref (val));
549 g_hash_table_unref (menu_item->links);
550 menu_item->links = new;
552 menu_item->cow = FALSE;
556 static void
557 g_menu_item_finalize (GObject *object)
559 GMenuItem *menu_item = G_MENU_ITEM (object);
561 g_hash_table_unref (menu_item->attributes);
562 g_hash_table_unref (menu_item->links);
564 G_OBJECT_CLASS (g_menu_item_parent_class)
565 ->finalize (object);
568 static void
569 g_menu_item_init (GMenuItem *menu_item)
571 menu_item->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
572 menu_item->links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
573 menu_item->cow = FALSE;
576 static void
577 g_menu_item_class_init (GMenuItemClass *class)
579 class->finalize = g_menu_item_finalize;
582 /* We treat attribute names the same as GSettings keys:
583 * - only lowercase ascii, digits and '-'
584 * - must start with lowercase
585 * - must not end with '-'
586 * - no consecutive '-'
587 * - not longer than 1024 chars
589 static gboolean
590 valid_attribute_name (const gchar *name)
592 gint i;
594 if (!g_ascii_islower (name[0]))
595 return FALSE;
597 for (i = 1; name[i]; i++)
599 if (name[i] != '-' &&
600 !g_ascii_islower (name[i]) &&
601 !g_ascii_isdigit (name[i]))
602 return FALSE;
604 if (name[i] == '-' && name[i + 1] == '-')
605 return FALSE;
608 if (name[i - 1] == '-')
609 return FALSE;
611 if (i > 1024)
612 return FALSE;
614 return TRUE;
618 * g_menu_item_set_attribute_value:
619 * @menu_item: a #GMenuItem
620 * @attribute: the attribute to set
621 * @value: (allow-none): a #GVariant to use as the value, or %NULL
623 * Sets or unsets an attribute on @menu_item.
625 * The attribute to set or unset is specified by @attribute. This
626 * can be one of the standard attribute names %G_MENU_ATTRIBUTE_LABEL,
627 * %G_MENU_ATTRIBUTE_ACTION, %G_MENU_ATTRIBUTE_TARGET, or a custom
628 * attribute name.
629 * Attribute names are restricted to lowercase characters, numbers
630 * and '-'. Furthermore, the names must begin with a lowercase character,
631 * must not end with a '-', and must not contain consecutive dashes.
633 * must consist only of lowercase
634 * ASCII characters, digits and '-'.
636 * If @value is non-%NULL then it is used as the new value for the
637 * attribute. If @value is %NULL then the attribute is unset. If
638 * the @value #GVariant is floating, it is consumed.
640 * See also g_menu_item_set_attribute() for a more convenient way to do
641 * the same.
643 * Since: 2.32
645 void
646 g_menu_item_set_attribute_value (GMenuItem *menu_item,
647 const gchar *attribute,
648 GVariant *value)
650 g_return_if_fail (G_IS_MENU_ITEM (menu_item));
651 g_return_if_fail (attribute != NULL);
652 g_return_if_fail (valid_attribute_name (attribute));
654 g_menu_item_clear_cow (menu_item);
656 if (value != NULL)
657 g_hash_table_insert (menu_item->attributes, g_strdup (attribute), g_variant_ref_sink (value));
658 else
659 g_hash_table_remove (menu_item->attributes, attribute);
663 * g_menu_item_set_attribute:
664 * @menu_item: a #GMenuItem
665 * @attribute: the attribute to set
666 * @format_string: (allow-none): a #GVariant format string, or %NULL
667 * @...: positional parameters, as per @format_string
669 * Sets or unsets an attribute on @menu_item.
671 * The attribute to set or unset is specified by @attribute. This
672 * can be one of the standard attribute names %G_MENU_ATTRIBUTE_LABEL,
673 * %G_MENU_ATTRIBUTE_ACTION, %G_MENU_ATTRIBUTE_TARGET, or a custom
674 * attribute name.
675 * Attribute names are restricted to lowercase characters, numbers
676 * and '-'. Furthermore, the names must begin with a lowercase character,
677 * must not end with a '-', and must not contain consecutive dashes.
679 * If @format_string is non-%NULL then the proper position parameters
680 * are collected to create a #GVariant instance to use as the attribute
681 * value. If it is %NULL then the positional parameterrs are ignored
682 * and the named attribute is unset.
684 * See also g_menu_item_set_attribute_value() for an equivalent call
685 * that directly accepts a #GVariant.
687 * Since: 2.32
689 void
690 g_menu_item_set_attribute (GMenuItem *menu_item,
691 const gchar *attribute,
692 const gchar *format_string,
693 ...)
695 GVariant *value;
697 if (format_string != NULL)
699 va_list ap;
701 va_start (ap, format_string);
702 value = g_variant_new_va (format_string, NULL, &ap);
703 va_end (ap);
705 else
706 value = NULL;
708 g_menu_item_set_attribute_value (menu_item, attribute, value);
712 * g_menu_item_set_link:
713 * @menu_item: a #GMenuItem
714 * @link: type of link to establish or unset
715 * @model: (allow-none): the #GMenuModel to link to (or %NULL to unset)
717 * Creates a link from @menu_item to @model if non-%NULL, or unsets it.
719 * Links are used to establish a relationship between a particular menu
720 * item and another menu. For example, %G_MENU_LINK_SUBMENU is used to
721 * associate a submenu with a particular menu item, and %G_MENU_LINK_SECTION
722 * is used to create a section. Other types of link can be used, but there
723 * is no guarantee that clients will be able to make sense of them.
724 * Link types are restricted to lowercase characters, numbers
725 * and '-'. Furthermore, the names must begin with a lowercase character,
726 * must not end with a '-', and must not contain consecutive dashes.
728 * Since: 2.32
730 void
731 g_menu_item_set_link (GMenuItem *menu_item,
732 const gchar *link,
733 GMenuModel *model)
735 g_return_if_fail (G_IS_MENU_ITEM (menu_item));
736 g_return_if_fail (link != NULL);
737 g_return_if_fail (valid_attribute_name (link));
739 g_menu_item_clear_cow (menu_item);
741 if (model != NULL)
742 g_hash_table_insert (menu_item->links, g_strdup (link), g_object_ref (model));
743 else
744 g_hash_table_remove (menu_item->links, link);
748 * g_menu_item_set_label:
749 * @menu_item: a #GMenuItem
750 * @label: (allow-none): the label to set, or %NULL to unset
752 * Sets or unsets the "label" attribute of @menu_item.
754 * If @label is non-%NULL it is used as the label for the menu item. If
755 * it is %NULL then the label attribute is unset.
757 * Since: 2.32
759 void
760 g_menu_item_set_label (GMenuItem *menu_item,
761 const gchar *label)
763 GVariant *value;
765 if (label != NULL)
766 value = g_variant_new_string (label);
767 else
768 value = NULL;
770 g_menu_item_set_attribute_value (menu_item, G_MENU_ATTRIBUTE_LABEL, value);
774 * g_menu_item_set_submenu:
775 * @menu_item: a #GMenuItem
776 * @submenu: (allow-none): a #GMenuModel, or %NULL
778 * Sets or unsets the "submenu" link of @menu_item to @submenu.
780 * If @submenu is non-%NULL, it is linked to. If it is %NULL then the
781 * link is unset.
783 * The effect of having one menu appear as a submenu of another is
784 * exactly as it sounds.
786 * Since: 2.32
788 void
789 g_menu_item_set_submenu (GMenuItem *menu_item,
790 GMenuModel *submenu)
792 g_menu_item_set_link (menu_item, G_MENU_LINK_SUBMENU, submenu);
796 * g_menu_item_set_section:
797 * @menu_item: a #GMenuItem
798 * @section: (allow-none): a #GMenuModel, or %NULL
800 * Sets or unsets the "section" link of @menu_item to @section.
802 * The effect of having one menu appear as a section of another is
803 * exactly as it sounds: the items from @section become a direct part of
804 * the menu that @menu_item is added to. See g_menu_item_new_section()
805 * for more information about what it means for a menu item to be a
806 * section.
808 * Since: 2.32
810 void
811 g_menu_item_set_section (GMenuItem *menu_item,
812 GMenuModel *section)
814 g_menu_item_set_link (menu_item, G_MENU_LINK_SECTION, section);
818 * g_menu_item_set_action_and_target_value:
819 * @menu_item: a #GMenuItem
820 * @action: (allow-none): the name of the action for this item
821 * @target_value: (allow-none): a #GVariant to use as the action target
823 * Sets or unsets the "action" and "target" attributes of @menu_item.
825 * If @action is %NULL then both the "action" and "target" attributes
826 * are unset (and @target_value is ignored).
828 * If @action is non-%NULL then the "action" attribute is set. The
829 * "target" attribute is then set to the value of @target_value if it is
830 * non-%NULL or unset otherwise.
832 * Normal menu items (ie: not submenu, section or other custom item
833 * types) are expected to have the "action" attribute set to identify
834 * the action that they are associated with. The state type of the
835 * action help to determine the disposition of the menu item. See
836 * #GAction and #GActionGroup for an overview of actions.
838 * In general, clicking on the menu item will result in activation of
839 * the named action with the "target" attribute given as the parameter
840 * to the action invocation. If the "target" attribute is not set then
841 * the action is invoked with no parameter.
843 * If the action has no state then the menu item is usually drawn as a
844 * plain menu item (ie: with no additional decoration).
846 * If the action has a boolean state then the menu item is usually drawn
847 * as a toggle menu item (ie: with a checkmark or equivalent
848 * indication). The item should be marked as 'toggled' or 'checked'
849 * when the boolean state is %TRUE.
851 * If the action has a string state then the menu item is usually drawn
852 * as a radio menu item (ie: with a radio bullet or equivalent
853 * indication). The item should be marked as 'selected' when the string
854 * state is equal to the value of the @target property.
856 * See g_menu_item_set_action_and_target() or
857 * g_menu_item_set_detailed_action() for two equivalent calls that are
858 * probably more convenient for most uses.
860 * Since: 2.32
862 void
863 g_menu_item_set_action_and_target_value (GMenuItem *menu_item,
864 const gchar *action,
865 GVariant *target_value)
867 GVariant *action_value;
869 if (action != NULL)
871 action_value = g_variant_new_string (action);
873 else
875 action_value = NULL;
876 target_value = NULL;
879 g_menu_item_set_attribute_value (menu_item, G_MENU_ATTRIBUTE_ACTION, action_value);
880 g_menu_item_set_attribute_value (menu_item, G_MENU_ATTRIBUTE_TARGET, target_value);
884 * g_menu_item_set_action_and_target:
885 * @menu_item: a #GMenuItem
886 * @action: (allow-none): the name of the action for this item
887 * @format_string: (allow-none): a GVariant format string
888 * @...: positional parameters, as per @format_string
890 * Sets or unsets the "action" and "target" attributes of @menu_item.
892 * If @action is %NULL then both the "action" and "target" attributes
893 * are unset (and @format_string is ignored along with the positional
894 * parameters).
896 * If @action is non-%NULL then the "action" attribute is set.
897 * @format_string is then inspected. If it is non-%NULL then the proper
898 * position parameters are collected to create a #GVariant instance to
899 * use as the target value. If it is %NULL then the positional
900 * parameters are ignored and the "target" attribute is unset.
902 * See also g_menu_item_set_action_and_target_value() for an equivalent
903 * call that directly accepts a #GVariant. See
904 * g_menu_item_set_detailed_action() for a more convenient version that
905 * works with string-typed targets.
907 * See also g_menu_item_set_action_and_target_value() for a
908 * description of the semantics of the action and target attributes.
910 * Since: 2.32
912 void
913 g_menu_item_set_action_and_target (GMenuItem *menu_item,
914 const gchar *action,
915 const gchar *format_string,
916 ...)
918 GVariant *value;
920 if (format_string != NULL)
922 va_list ap;
924 va_start (ap, format_string);
925 value = g_variant_new_va (format_string, NULL, &ap);
926 va_end (ap);
928 else
929 value = NULL;
931 g_menu_item_set_action_and_target_value (menu_item, action, value);
935 * g_menu_item_set_detailed_action:
936 * @menu_item: a #GMenuItem
937 * @detailed_action: the "detailed" action string
939 * Sets the "action" and possibly the "target" attribute of @menu_item.
941 * If @detailed_action contains a double colon ("::") then it is used as
942 * a separator between an action name and a target string. In this
943 * case, this call is equivalent to calling
944 * g_menu_item_set_action_and_target() with the part before the "::" and
945 * with a string-type #GVariant containing the part following the "::".
947 * If @detailed_action doesn't contain "::" then the action is set to
948 * the given string (verbatim) and the target value is unset.
950 * See g_menu_item_set_action_and_target() or
951 * g_menu_item_set_action_and_target_value() for more flexible (but
952 * slightly less convenient) alternatives.
954 * See also g_menu_item_set_action_and_target_value() for a description of
955 * the semantics of the action and target attributes.
957 * Since: 2.32
959 void
960 g_menu_item_set_detailed_action (GMenuItem *menu_item,
961 const gchar *detailed_action)
963 const gchar *sep;
965 sep = strstr (detailed_action, "::");
967 if (sep != NULL)
969 gchar *action;
971 action = g_strndup (detailed_action, sep - detailed_action);
972 g_menu_item_set_action_and_target (menu_item, action, "s", sep + 2);
973 g_free (action);
976 else
977 g_menu_item_set_action_and_target_value (menu_item, detailed_action, NULL);
981 * g_menu_item_new:
982 * @label: (allow-none): the section label, or %NULL
983 * @detailed_action: (allow-none): the detailed action string, or %NULL
985 * Creates a new #GMenuItem.
987 * If @label is non-%NULL it is used to set the "label" attribute of the
988 * new item.
990 * If @detailed_action is non-%NULL it is used to set the "action" and
991 * possibly the "target" attribute of the new item. See
992 * g_menu_item_set_detailed_action() for more information.
994 * Returns: a new #GMenuItem
996 * Since: 2.32
998 GMenuItem *
999 g_menu_item_new (const gchar *label,
1000 const gchar *detailed_action)
1002 GMenuItem *menu_item;
1004 menu_item = g_object_new (G_TYPE_MENU_ITEM, NULL);
1006 if (label != NULL)
1007 g_menu_item_set_label (menu_item, label);
1009 if (detailed_action != NULL)
1010 g_menu_item_set_detailed_action (menu_item, detailed_action);
1012 return menu_item;
1016 * g_menu_item_new_submenu:
1017 * @label: (allow-none): the section label, or %NULL
1018 * @submenu: a #GMenuModel with the items of the submenu
1020 * Creates a new #GMenuItem representing a submenu.
1022 * This is a convenience API around g_menu_item_new() and
1023 * g_menu_item_set_submenu().
1025 * Returns: a new #GMenuItem
1027 * Since: 2.32
1029 GMenuItem *
1030 g_menu_item_new_submenu (const gchar *label,
1031 GMenuModel *submenu)
1033 GMenuItem *menu_item;
1035 menu_item = g_object_new (G_TYPE_MENU_ITEM, NULL);
1037 if (label != NULL)
1038 g_menu_item_set_label (menu_item, label);
1040 g_menu_item_set_submenu (menu_item, submenu);
1042 return menu_item;
1046 * g_menu_item_new_section:
1047 * @label: (allow-none): the section label, or %NULL
1048 * @section: a #GMenuModel with the items of the section
1050 * Creates a new #GMenuItem representing a section.
1052 * This is a convenience API around g_menu_item_new() and
1053 * g_menu_item_set_section().
1055 * The effect of having one menu appear as a section of another is
1056 * exactly as it sounds: the items from @section become a direct part of
1057 * the menu that @menu_item is added to.
1059 * Visual separation is typically displayed between two non-empty
1060 * sections. If @label is non-%NULL then it will be encorporated into
1061 * this visual indication. This allows for labeled subsections of a
1062 * menu.
1064 * As a simple example, consider a typical "Edit" menu from a simple
1065 * program. It probably contains an "Undo" and "Redo" item, followed by
1066 * a separator, followed by "Cut", "Copy" and "Paste".
1068 * This would be accomplished by creating three #GMenu instances. The
1069 * first would be populated with the "Undo" and "Redo" items, and the
1070 * second with the "Cut", "Copy" and "Paste" items. The first and
1071 * second menus would then be added as submenus of the third. In XML
1072 * format, this would look something like the following:
1074 * <informalexample><programlisting><![CDATA[
1075 * <menu id='edit-menu'>
1076 * <section>
1077 * <item label='Undo'/>
1078 * <item label='Redo'/>
1079 * </section>
1080 * <section>
1081 * <item label='Cut'/>
1082 * <item label='Copy'/>
1083 * <item label='Paste'/>
1084 * </section>
1085 * </menu>
1086 * ]]></programlisting></informalexample>
1088 * The following example is exactly equivalent. It is more illustrative
1089 * of the exact relationship between the menus and items (keeping in
1090 * mind that the 'link' element defines a new menu that is linked to the
1091 * containing one). The style of the second example is more verbose and
1092 * difficult to read (and therefore not recommended except for the
1093 * purpose of understanding what is really going on).
1095 * <informalexample><programlisting><![CDATA[
1096 * <menu id='edit-menu'>
1097 * <item>
1098 * <link name='section'>
1099 * <item label='Undo'/>
1100 * <item label='Redo'/>
1101 * </link>
1102 * </item>
1103 * <item>
1104 * <link name='section'>
1105 * <item label='Cut'/>
1106 * <item label='Copy'/>
1107 * <item label='Paste'/>
1108 * </link>
1109 * </item>
1110 * </menu>
1111 * ]]></programlisting></informalexample>
1113 * Returns: a new #GMenuItem
1115 * Since: 2.32
1117 GMenuItem *
1118 g_menu_item_new_section (const gchar *label,
1119 GMenuModel *section)
1121 GMenuItem *menu_item;
1123 menu_item = g_object_new (G_TYPE_MENU_ITEM, NULL);
1125 if (label != NULL)
1126 g_menu_item_set_label (menu_item, label);
1128 g_menu_item_set_section (menu_item, section);
1130 return menu_item;