[amtk] Factory: add functions to create simple GtkMenu



commit 5031880344ec25962bee37e029449179f4d66a77
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Apr 11 18:24:58 2018 +0200

    Factory: add functions to create simple GtkMenu
    
    And test it.

 amtk/amtk-factory.c                  |   81 +++++++++++++++++++++++++++++++++-
 amtk/amtk-factory.h                  |   11 ++++-
 docs/reference/amtk-5.0-sections.txt |    2 +
 tests/test-menu.c                    |   39 ++++++++--------
 4 files changed, 112 insertions(+), 21 deletions(-)
---
diff --git a/amtk/amtk-factory.c b/amtk/amtk-factory.c
index 2ad8cff..84a26bb 100644
--- a/amtk/amtk-factory.c
+++ b/amtk/amtk-factory.c
@@ -1,7 +1,7 @@
 /*
  * This file is part of Amtk - Actions, Menus and Toolbars Kit
  *
- * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2017, 2018 - Sébastien Wilmet <swilmet gnome org>
  *
  * Amtk is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
@@ -671,6 +671,85 @@ amtk_factory_create_check_menu_item_full (AmtkFactory      *factory,
 }
 
 /**
+ * amtk_factory_create_simple_menu:
+ * @factory: an #AmtkFactory.
+ * @entries: (array length=n_entries) (element-type AmtkActionInfoEntry): a
+ *   pointer to the first item in an array of #AmtkActionInfoEntry structs.
+ * @n_entries: the length of @entries, or -1 if @entries is %NULL-terminated.
+ *
+ * Calls amtk_factory_create_simple_menu_full() with the
+ * #AmtkFactory:default-flags.
+ *
+ * Returns: (transfer floating): a new simple #GtkMenu for @entries.
+ * Since: 5.0
+ */
+GtkWidget *
+amtk_factory_create_simple_menu (AmtkFactory               *factory,
+                                const AmtkActionInfoEntry *entries,
+                                gint                       n_entries)
+{
+       g_return_val_if_fail (AMTK_IS_FACTORY (factory), NULL);
+       g_return_val_if_fail (n_entries >= -1, NULL);
+       g_return_val_if_fail (entries != NULL || n_entries == 0, NULL);
+
+       return amtk_factory_create_simple_menu_full (factory,
+                                                    entries,
+                                                    n_entries,
+                                                    factory->priv->default_flags);
+}
+
+/**
+ * amtk_factory_create_simple_menu_full:
+ * @factory: an #AmtkFactory.
+ * @entries: (array length=n_entries) (element-type AmtkActionInfoEntry): a
+ *   pointer to the first item in an array of #AmtkActionInfoEntry structs.
+ * @n_entries: the length of @entries, or -1 if @entries is %NULL-terminated.
+ * @flags: #AmtkFactoryFlags.
+ *
+ * This function ignores the #AmtkFactory:default-flags property and takes the
+ * @flags argument instead.
+ *
+ * This function:
+ * - Creates a #GtkMenu;
+ * - For each #AmtkActionInfoEntry action name from @entries, creates a
+ *   #GtkMenuItem with amtk_factory_create_menu_item_full() with the same @flags
+ *   as passed in to this function, and appends it to the #GtkMenu, in the same
+ *   order as provided by the @entries array.
+ *
+ * So this function is useful only if the #GtkMenu contains only simple
+ * #GtkMenuItem's, not #GtkCheckMenuItem's nor #GtkRadioMenuItem's.
+ *
+ * Returns: (transfer floating): a new simple #GtkMenu for @entries.
+ * Since: 5.0
+ */
+GtkWidget *
+amtk_factory_create_simple_menu_full (AmtkFactory               *factory,
+                                     const AmtkActionInfoEntry *entries,
+                                     gint                       n_entries,
+                                     AmtkFactoryFlags           flags)
+{
+       GtkMenuShell *menu;
+       gint i;
+
+       g_return_val_if_fail (AMTK_IS_FACTORY (factory), NULL);
+       g_return_val_if_fail (n_entries >= -1, NULL);
+       g_return_val_if_fail (entries != NULL || n_entries == 0, NULL);
+
+       menu = GTK_MENU_SHELL (gtk_menu_new ());
+
+       for (i = 0; n_entries == -1 ? entries[i].action_name != NULL : i < n_entries; i++)
+       {
+               const AmtkActionInfoEntry *cur_entry = &entries[i];
+               GtkWidget *menu_item;
+
+               menu_item = amtk_factory_create_menu_item_full (factory, cur_entry->action_name, flags);
+               gtk_menu_shell_append (menu, menu_item);
+       }
+
+       return GTK_WIDGET (menu);
+}
+
+/**
  * 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 bf2c4a8..e8ca297 100644
--- a/amtk/amtk-factory.h
+++ b/amtk/amtk-factory.h
@@ -1,7 +1,7 @@
 /*
  * This file is part of Amtk - Actions, Menus and Toolbars Kit
  *
- * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2017, 2018 - Sébastien Wilmet <swilmet gnome org>
  *
  * Amtk is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
@@ -111,6 +111,15 @@ GtkWidget *                amtk_factory_create_check_menu_item_full        (AmtkFactory  
    *factory
                                                                         const gchar      *action_name,
                                                                         AmtkFactoryFlags  flags);
 
+GtkWidget *            amtk_factory_create_simple_menu                 (AmtkFactory               *factory,
+                                                                        const AmtkActionInfoEntry *entries,
+                                                                        gint                       
n_entries);
+
+GtkWidget *            amtk_factory_create_simple_menu_full            (AmtkFactory               *factory,
+                                                                        const AmtkActionInfoEntry *entries,
+                                                                        gint                       n_entries,
+                                                                        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 4f377c4..a548c28 100644
--- a/docs/reference/amtk-5.0-sections.txt
+++ b/docs/reference/amtk-5.0-sections.txt
@@ -111,6 +111,8 @@ amtk_factory_create_menu_item
 amtk_factory_create_menu_item_full
 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_tool_button
 amtk_factory_create_tool_button_full
 amtk_factory_create_menu_tool_button
diff --git a/tests/test-menu.c b/tests/test-menu.c
index 9262502..ac77239 100644
--- a/tests/test-menu.c
+++ b/tests/test-menu.c
@@ -25,6 +25,17 @@
  */
 static AmtkActionInfoStore *action_info_store = NULL;
 
+static AmtkActionInfoEntry file_action_info_entries[] =
+{
+       { "win.open", "document-open", "_Open", "<Control>o",
+         "Open a file" },
+
+       { "app.quit", "application-exit", "_Quit", "<Control>q",
+         "Quit the application" },
+
+       { NULL }
+};
+
 static void
 add_action_info_entries (void)
 {
@@ -32,9 +43,6 @@ add_action_info_entries (void)
        {
                /* action, icon, label, accel, tooltip */
 
-               { "app.quit", "application-exit", "_Quit", "<Control>q",
-                 "Quit the application" },
-
                { "app.about", "help-about", "_About", NULL,
                  "About this application" },
 
@@ -49,6 +57,10 @@ add_action_info_entries (void)
                                            entries,
                                            G_N_ELEMENTS (entries),
                                            NULL);
+
+       amtk_action_info_store_add_entries (action_info_store,
+                                           file_action_info_entries, -1,
+                                           NULL);
 }
 
 static void
@@ -91,21 +103,6 @@ startup_cb (GApplication *g_app,
 }
 
 static GtkWidget *
-create_file_submenu (void)
-{
-       GtkMenuShell *file_submenu;
-       AmtkFactory *factory;
-
-       file_submenu = GTK_MENU_SHELL (gtk_menu_new ());
-
-       factory = amtk_factory_new_with_default_application ();
-       gtk_menu_shell_append (file_submenu, amtk_factory_create_menu_item (factory, "app.quit"));
-       g_object_unref (factory);
-
-       return GTK_WIDGET (file_submenu);
-}
-
-static GtkWidget *
 create_view_submenu (void)
 {
        GtkMenuShell *view_submenu;
@@ -138,14 +135,17 @@ create_help_submenu (void)
 static GtkMenuBar *
 create_menu_bar (void)
 {
+       AmtkFactory *factory;
        GtkWidget *file_menu_item;
        GtkWidget *view_menu_item;
        GtkWidget *help_menu_item;
        GtkMenuBar *menu_bar;
 
+       factory = amtk_factory_new_with_default_application ();
+
        file_menu_item = gtk_menu_item_new_with_mnemonic ("_File");
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (file_menu_item),
-                                  create_file_submenu ());
+                                  amtk_factory_create_simple_menu (factory, file_action_info_entries, -1));
 
        view_menu_item = gtk_menu_item_new_with_mnemonic ("_View");
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (view_menu_item),
@@ -162,6 +162,7 @@ create_menu_bar (void)
 
        amtk_action_info_store_check_all_used (action_info_store);
 
+       g_object_unref (factory);
        return menu_bar;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]