[amtk] Factory: add functions to create GMenuItem's
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [amtk] Factory: add functions to create GMenuItem's
- Date: Thu, 12 Apr 2018 17:20:11 +0000 (UTC)
commit 8af7cd417a8808c15649dad27864a95ff942f820
Author: Sébastien Wilmet <swilmet gnome org>
Date: Thu Apr 12 18:49:12 2018 +0200
Factory: add functions to create GMenuItem's
And test it.
amtk/amtk-factory.c | 130 ++++++++++++++++++++++++++++++----
amtk/amtk-factory.h | 11 +++-
docs/reference/amtk-5.0-sections.txt | 2 +
tests/test-headerbar.c | 11 ++-
4 files changed, 134 insertions(+), 20 deletions(-)
---
diff --git a/amtk/amtk-factory.c b/amtk/amtk-factory.c
index 84a26bb..c8e3b05 100644
--- a/amtk/amtk-factory.c
+++ b/amtk/amtk-factory.c
@@ -404,10 +404,9 @@ amtk_factory_set_default_flags (AmtkFactory *factory,
}
static AmtkActionInfo *
-common_create (AmtkFactory *factory,
- const gchar *action_name,
- AmtkFactoryFlags flags,
- GtkWidget **widget)
+common_create (AmtkFactory *factory,
+ const gchar *action_name,
+ AmtkFactoryFlags flags)
{
AmtkActionInfoCentralStore *central_store;
AmtkActionInfo *action_info;
@@ -419,18 +418,9 @@ common_create (AmtkFactory *factory,
{
g_warning ("AmtkFactory create function: action name '%s' not found.",
action_name);
-
- g_object_ref_sink (*widget);
- g_object_unref (*widget);
- *widget = NULL;
return NULL;
}
- if ((flags & AMTK_FACTORY_IGNORE_GACTION) == 0)
- {
- gtk_actionable_set_action_name (GTK_ACTIONABLE (*widget), action_name);
- }
-
if ((flags & AMTK_FACTORY_IGNORE_ACCELS) == 0 &&
(flags & AMTK_FACTORY_IGNORE_ACCELS_FOR_APP) == 0 &&
factory->priv->app != NULL)
@@ -447,6 +437,32 @@ common_create (AmtkFactory *factory,
}
static AmtkActionInfo *
+common_create_widget (AmtkFactory *factory,
+ const gchar *action_name,
+ AmtkFactoryFlags flags,
+ GtkWidget **widget)
+{
+ AmtkActionInfo *action_info;
+
+ action_info = common_create (factory, action_name, flags);
+
+ if (action_info == NULL)
+ {
+ g_object_ref_sink (*widget);
+ g_object_unref (*widget);
+ *widget = NULL;
+ return NULL;
+ }
+
+ if ((flags & AMTK_FACTORY_IGNORE_GACTION) == 0)
+ {
+ gtk_actionable_set_action_name (GTK_ACTIONABLE (*widget), action_name);
+ }
+
+ return action_info;
+}
+
+static AmtkActionInfo *
common_create_menu_item (AmtkFactory *factory,
const gchar *action_name,
AmtkFactoryFlags flags,
@@ -456,7 +472,7 @@ common_create_menu_item (AmtkFactory *factory,
const gchar * const *accels;
const gchar *tooltip;
- action_info = common_create (factory, action_name, flags, (GtkWidget **)menu_item);
+ action_info = common_create_widget (factory, action_name, flags, (GtkWidget **)menu_item);
if (action_info == NULL)
{
return NULL;
@@ -510,7 +526,7 @@ common_create_tool_button (AmtkFactory *factory,
const gchar *icon_name;
const gchar *tooltip;
- action_info = common_create (factory, action_name, flags, (GtkWidget **)tool_button);
+ action_info = common_create_widget (factory, action_name, flags, (GtkWidget **)tool_button);
if (action_info == NULL)
{
return NULL;
@@ -750,6 +766,90 @@ amtk_factory_create_simple_menu_full (AmtkFactory *factory,
}
/**
+ * amtk_factory_create_gmenu_item:
+ * @factory: an #AmtkFactory.
+ * @action_name: an action name.
+ *
+ * Calls amtk_factory_create_gmenu_item_full() with the
+ * #AmtkFactory:default-flags.
+ *
+ * Returns: (transfer full): a new #GMenuItem for @action_name.
+ * Since: 5.0
+ */
+GMenuItem *
+amtk_factory_create_gmenu_item (AmtkFactory *factory,
+ const gchar *action_name)
+{
+ g_return_val_if_fail (AMTK_IS_FACTORY (factory), NULL);
+ g_return_val_if_fail (action_name != NULL, NULL);
+
+ return amtk_factory_create_gmenu_item_full (factory,
+ action_name,
+ factory->priv->default_flags);
+}
+
+/**
+ * amtk_factory_create_gmenu_item_full:
+ * @factory: an #AmtkFactory.
+ * @action_name: an action name.
+ * @flags: #AmtkFactoryFlags.
+ *
+ * This function ignores the #AmtkFactory:default-flags property and takes the
+ * @flags argument instead.
+ *
+ * Creates a new #GMenuItem for @action_name. It ignores the tooltip, i.e. the
+ * return value of amtk_action_info_get_tooltip().
+ *
+ * Returns: (transfer full): a new #GMenuItem for @action_name.
+ * Since: 5.0
+ */
+GMenuItem *
+amtk_factory_create_gmenu_item_full (AmtkFactory *factory,
+ const gchar *action_name,
+ AmtkFactoryFlags flags)
+{
+ AmtkActionInfo *action_info;
+ const gchar *label = NULL;
+ const gchar *detailed_action = NULL;
+ GMenuItem *menu_item;
+ const gchar *icon_name;
+
+ g_return_val_if_fail (AMTK_IS_FACTORY (factory), NULL);
+ g_return_val_if_fail (action_name != NULL, NULL);
+
+ action_info = common_create (factory, action_name, flags);
+ if (action_info == NULL)
+ {
+ return NULL;
+ }
+
+ if ((flags & AMTK_FACTORY_IGNORE_LABEL) == 0)
+ {
+ label = amtk_action_info_get_label (action_info);
+ }
+
+ if ((flags & AMTK_FACTORY_IGNORE_GACTION) == 0)
+ {
+ detailed_action = action_name;
+ }
+
+ menu_item = g_menu_item_new (label, detailed_action);
+
+ icon_name = amtk_action_info_get_icon_name (action_info);
+ if ((flags & AMTK_FACTORY_IGNORE_ICON) == 0 &&
+ icon_name != NULL)
+ {
+ GIcon *icon;
+
+ icon = g_themed_icon_new (icon_name);
+ g_menu_item_set_icon (menu_item, icon);
+ g_object_unref (icon);
+ }
+
+ return menu_item;
+}
+
+/**
* amtk_factory_create_tool_button:
* @factory: an #AmtkFactory.
* @action_name: an action name.
diff --git a/amtk/amtk-factory.h b/amtk/amtk-factory.h
index e8ca297..b7e2972 100644
--- a/amtk/amtk-factory.h
+++ b/amtk/amtk-factory.h
@@ -56,7 +56,9 @@ struct _AmtkFactoryClass
/**
* AmtkFactoryFlags:
* @AMTK_FACTORY_FLAGS_NONE: No flags.
- * @AMTK_FACTORY_IGNORE_GACTION: Do not call gtk_actionable_set_action_name().
+ * @AMTK_FACTORY_IGNORE_GACTION: Do not associate the created object with the
+ * #GAction. For example if the object to create is a #GtkActionable, do not
+ * call gtk_actionable_set_action_name().
* @AMTK_FACTORY_IGNORE_ICON: Do not set an icon.
* @AMTK_FACTORY_IGNORE_LABEL: Do not set a label/short description.
* @AMTK_FACTORY_IGNORE_TOOLTIP: Do not set a tooltip/long description.
@@ -120,6 +122,13 @@ GtkWidget * amtk_factory_create_simple_menu_full (AmtkFactory
*f
gint n_entries,
AmtkFactoryFlags flags);
+GMenuItem * amtk_factory_create_gmenu_item (AmtkFactory *factory,
+ const gchar *action_name);
+
+GMenuItem * amtk_factory_create_gmenu_item_full (AmtkFactory *factory,
+ const gchar *action_name,
+ AmtkFactoryFlags flags);
+
GtkToolItem * amtk_factory_create_tool_button (AmtkFactory *factory,
const gchar *action_name);
diff --git a/docs/reference/amtk-5.0-sections.txt b/docs/reference/amtk-5.0-sections.txt
index a548c28..745f8f2 100644
--- a/docs/reference/amtk-5.0-sections.txt
+++ b/docs/reference/amtk-5.0-sections.txt
@@ -113,6 +113,8 @@ amtk_factory_create_check_menu_item
amtk_factory_create_check_menu_item_full
amtk_factory_create_simple_menu
amtk_factory_create_simple_menu_full
+amtk_factory_create_gmenu_item
+amtk_factory_create_gmenu_item_full
amtk_factory_create_tool_button
amtk_factory_create_tool_button_full
amtk_factory_create_menu_tool_button
diff --git a/tests/test-headerbar.c b/tests/test-headerbar.c
index 6d65c27..09af4c2 100644
--- a/tests/test-headerbar.c
+++ b/tests/test-headerbar.c
@@ -83,22 +83,25 @@ add_win_actions (GtkApplicationWindow *window,
static GMenuModel *
create_window_menu (void)
{
+ AmtkFactory *factory;
GMenu *menu;
GMenuItem *item;
menu = g_menu_new ();
+ factory = amtk_factory_new_with_default_application ();
- /* TODO create and use Amtk factory function, plus a utility function to
- * append+unref a GMenuItem to a GMenu.
+ /* TODO create a utility function to append+unref a GMenuItem to a
+ * GMenu.
*/
- item = g_menu_item_new ("_Side Panel", "win.show-side-panel");
+ item = amtk_factory_create_gmenu_item (factory, "win.show-side-panel");
g_menu_append_item (menu, item);
g_object_unref (item);
- item = g_menu_item_new ("_Print", "win.print");
+ item = amtk_factory_create_gmenu_item (factory, "win.print");
g_menu_append_item (menu, item);
g_object_unref (item);
+ g_object_unref (factory);
g_menu_freeze (menu);
return G_MENU_MODEL (menu);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]