Codebase list amtk / 8af7cd4
Factory: add functions to create GMenuItem's And test it. Sébastien Wilmet 6 years ago
4 changed file(s) with 138 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
403403 }
404404
405405 static AmtkActionInfo *
406 common_create (AmtkFactory *factory,
407 const gchar *action_name,
408 AmtkFactoryFlags flags,
409 GtkWidget **widget)
406 common_create (AmtkFactory *factory,
407 const gchar *action_name,
408 AmtkFactoryFlags flags)
410409 {
411410 AmtkActionInfoCentralStore *central_store;
412411 AmtkActionInfo *action_info;
418417 {
419418 g_warning ("AmtkFactory create function: action name '%s' not found.",
420419 action_name);
421
420 return NULL;
421 }
422
423 if ((flags & AMTK_FACTORY_IGNORE_ACCELS) == 0 &&
424 (flags & AMTK_FACTORY_IGNORE_ACCELS_FOR_APP) == 0 &&
425 factory->priv->app != NULL)
426 {
427 const gchar * const *accels;
428
429 accels = amtk_action_info_get_accels (action_info);
430 gtk_application_set_accels_for_action (factory->priv->app, action_name, accels);
431 }
432
433 amtk_action_info_mark_as_used (action_info);
434
435 return action_info;
436 }
437
438 static AmtkActionInfo *
439 common_create_widget (AmtkFactory *factory,
440 const gchar *action_name,
441 AmtkFactoryFlags flags,
442 GtkWidget **widget)
443 {
444 AmtkActionInfo *action_info;
445
446 action_info = common_create (factory, action_name, flags);
447
448 if (action_info == NULL)
449 {
422450 g_object_ref_sink (*widget);
423451 g_object_unref (*widget);
424452 *widget = NULL;
429457 {
430458 gtk_actionable_set_action_name (GTK_ACTIONABLE (*widget), action_name);
431459 }
432
433 if ((flags & AMTK_FACTORY_IGNORE_ACCELS) == 0 &&
434 (flags & AMTK_FACTORY_IGNORE_ACCELS_FOR_APP) == 0 &&
435 factory->priv->app != NULL)
436 {
437 const gchar * const *accels;
438
439 accels = amtk_action_info_get_accels (action_info);
440 gtk_application_set_accels_for_action (factory->priv->app, action_name, accels);
441 }
442
443 amtk_action_info_mark_as_used (action_info);
444460
445461 return action_info;
446462 }
455471 const gchar * const *accels;
456472 const gchar *tooltip;
457473
458 action_info = common_create (factory, action_name, flags, (GtkWidget **)menu_item);
474 action_info = common_create_widget (factory, action_name, flags, (GtkWidget **)menu_item);
459475 if (action_info == NULL)
460476 {
461477 return NULL;
509525 const gchar *icon_name;
510526 const gchar *tooltip;
511527
512 action_info = common_create (factory, action_name, flags, (GtkWidget **)tool_button);
528 action_info = common_create_widget (factory, action_name, flags, (GtkWidget **)tool_button);
513529 if (action_info == NULL)
514530 {
515531 return NULL;
749765 }
750766
751767 /**
768 * amtk_factory_create_gmenu_item:
769 * @factory: an #AmtkFactory.
770 * @action_name: an action name.
771 *
772 * Calls amtk_factory_create_gmenu_item_full() with the
773 * #AmtkFactory:default-flags.
774 *
775 * Returns: (transfer full): a new #GMenuItem for @action_name.
776 * Since: 5.0
777 */
778 GMenuItem *
779 amtk_factory_create_gmenu_item (AmtkFactory *factory,
780 const gchar *action_name)
781 {
782 g_return_val_if_fail (AMTK_IS_FACTORY (factory), NULL);
783 g_return_val_if_fail (action_name != NULL, NULL);
784
785 return amtk_factory_create_gmenu_item_full (factory,
786 action_name,
787 factory->priv->default_flags);
788 }
789
790 /**
791 * amtk_factory_create_gmenu_item_full:
792 * @factory: an #AmtkFactory.
793 * @action_name: an action name.
794 * @flags: #AmtkFactoryFlags.
795 *
796 * This function ignores the #AmtkFactory:default-flags property and takes the
797 * @flags argument instead.
798 *
799 * Creates a new #GMenuItem for @action_name. It ignores the tooltip, i.e. the
800 * return value of amtk_action_info_get_tooltip().
801 *
802 * Returns: (transfer full): a new #GMenuItem for @action_name.
803 * Since: 5.0
804 */
805 GMenuItem *
806 amtk_factory_create_gmenu_item_full (AmtkFactory *factory,
807 const gchar *action_name,
808 AmtkFactoryFlags flags)
809 {
810 AmtkActionInfo *action_info;
811 const gchar *label = NULL;
812 const gchar *detailed_action = NULL;
813 GMenuItem *menu_item;
814 const gchar *icon_name;
815
816 g_return_val_if_fail (AMTK_IS_FACTORY (factory), NULL);
817 g_return_val_if_fail (action_name != NULL, NULL);
818
819 action_info = common_create (factory, action_name, flags);
820 if (action_info == NULL)
821 {
822 return NULL;
823 }
824
825 if ((flags & AMTK_FACTORY_IGNORE_LABEL) == 0)
826 {
827 label = amtk_action_info_get_label (action_info);
828 }
829
830 if ((flags & AMTK_FACTORY_IGNORE_GACTION) == 0)
831 {
832 detailed_action = action_name;
833 }
834
835 menu_item = g_menu_item_new (label, detailed_action);
836
837 icon_name = amtk_action_info_get_icon_name (action_info);
838 if ((flags & AMTK_FACTORY_IGNORE_ICON) == 0 &&
839 icon_name != NULL)
840 {
841 GIcon *icon;
842
843 icon = g_themed_icon_new (icon_name);
844 g_menu_item_set_icon (menu_item, icon);
845 g_object_unref (icon);
846 }
847
848 return menu_item;
849 }
850
851 /**
752852 * amtk_factory_create_tool_button:
753853 * @factory: an #AmtkFactory.
754854 * @action_name: an action name.
5555 /**
5656 * AmtkFactoryFlags:
5757 * @AMTK_FACTORY_FLAGS_NONE: No flags.
58 * @AMTK_FACTORY_IGNORE_GACTION: Do not call gtk_actionable_set_action_name().
58 * @AMTK_FACTORY_IGNORE_GACTION: Do not associate the created object with the
59 * #GAction. For example if the object to create is a #GtkActionable, do not
60 * call gtk_actionable_set_action_name().
5961 * @AMTK_FACTORY_IGNORE_ICON: Do not set an icon.
6062 * @AMTK_FACTORY_IGNORE_LABEL: Do not set a label/short description.
6163 * @AMTK_FACTORY_IGNORE_TOOLTIP: Do not set a tooltip/long description.
119121 gint n_entries,
120122 AmtkFactoryFlags flags);
121123
124 GMenuItem * amtk_factory_create_gmenu_item (AmtkFactory *factory,
125 const gchar *action_name);
126
127 GMenuItem * amtk_factory_create_gmenu_item_full (AmtkFactory *factory,
128 const gchar *action_name,
129 AmtkFactoryFlags flags);
130
122131 GtkToolItem * amtk_factory_create_tool_button (AmtkFactory *factory,
123132 const gchar *action_name);
124133
112112 amtk_factory_create_check_menu_item_full
113113 amtk_factory_create_simple_menu
114114 amtk_factory_create_simple_menu_full
115 amtk_factory_create_gmenu_item
116 amtk_factory_create_gmenu_item_full
115117 amtk_factory_create_tool_button
116118 amtk_factory_create_tool_button_full
117119 amtk_factory_create_menu_tool_button
8282 static GMenuModel *
8383 create_window_menu (void)
8484 {
85 AmtkFactory *factory;
8586 GMenu *menu;
8687 GMenuItem *item;
8788
8889 menu = g_menu_new ();
90 factory = amtk_factory_new_with_default_application ();
8991
90 /* TODO create and use Amtk factory function, plus a utility function to
91 * append+unref a GMenuItem to a GMenu.
92 /* TODO create a utility function to append+unref a GMenuItem to a
93 * GMenu.
9294 */
93 item = g_menu_item_new ("_Side Panel", "win.show-side-panel");
95 item = amtk_factory_create_gmenu_item (factory, "win.show-side-panel");
9496 g_menu_append_item (menu, item);
9597 g_object_unref (item);
9698
97 item = g_menu_item_new ("_Print", "win.print");
99 item = amtk_factory_create_gmenu_item (factory, "win.print");
98100 g_menu_append_item (menu, item);
99101 g_object_unref (item);
100102
103 g_object_unref (factory);
101104 g_menu_freeze (menu);
102105
103106 return G_MENU_MODEL (menu);