[amtk] tests: add test-headerbar.c



commit a12df2254501954c714d8cd47132c8b34df3c498
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Apr 12 15:42:45 2018 +0200

    tests: add test-headerbar.c
    
    I will expand Amtk to support GUIs with a GtkHeaderBar, to be used in
    Devhelp, to avoid information duplication.

 tests/Makefile.am      |    6 ++-
 tests/test-headerbar.c |  181 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 186 insertions(+), 1 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 1a00506..3a5f738 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -8,10 +8,14 @@ AM_LDFLAGS = $(WARN_LDFLAGS)
 LDADD = $(top_builddir)/amtk/libamtk-core.la   \
        $(AMTK_DEP_LIBS)
 
-noinst_PROGRAMS = $(TEST_PROGS)
 TEST_PROGS =
 
+TEST_PROGS += test-headerbar
+test_headerbar_SOURCES = test-headerbar.c
+
 TEST_PROGS += test-menu
 test_menu_SOURCES = test-menu.c
 
+noinst_PROGRAMS = $(TEST_PROGS)
+
 -include $(top_srcdir)/git.mk
diff --git a/tests/test-headerbar.c b/tests/test-headerbar.c
new file mode 100644
index 0000000..6d65c27
--- /dev/null
+++ b/tests/test-headerbar.c
@@ -0,0 +1,181 @@
+/*
+ * This file is part of Amtk - Actions, Menus and Toolbars Kit
+ *
+ * Copyright 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
+ * Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Amtk is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <amtk/amtk.h>
+
+/* This should be stored instead in the Private struct of the app GtkApplication
+ * subclass. But here to not make the code too complicated there is no
+ * GtkApplication subclass.
+ */
+static AmtkActionInfoStore *action_info_store = NULL;
+
+static void
+add_action_info_entries (void)
+{
+       const AmtkActionInfoEntry entries[] =
+       {
+               /* action, icon, label, accel */
+               { "win.show-side-panel", NULL, "_Side Panel", "F12" },
+               { "win.print", NULL, "_Print", "<Control>p" },
+               { NULL }
+       };
+
+       g_assert (action_info_store == NULL);
+       action_info_store = amtk_action_info_store_new ();
+
+       amtk_action_info_store_add_entries (action_info_store,
+                                           entries, -1,
+                                           NULL);
+}
+
+static void
+startup_cb (GApplication *g_app,
+           gpointer      user_data)
+{
+       add_action_info_entries ();
+}
+
+static void
+print_activate_cb (GSimpleAction *about_action,
+                  GVariant      *parameter,
+                  gpointer       user_data)
+{
+       g_print ("Print\n");
+}
+
+static void
+add_win_actions (GtkApplicationWindow *window,
+                GtkWidget            *side_panel)
+{
+       GPropertyAction *side_panel_action;
+
+       const GActionEntry entries[] =
+       {
+               { "print", print_activate_cb },
+               { NULL }
+       };
+
+       amtk_action_map_add_action_entries_check_dups (G_ACTION_MAP (window),
+                                                      entries, -1,
+                                                      NULL);
+
+       side_panel_action = g_property_action_new ("show-side-panel", side_panel, "visible");
+       g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (side_panel_action));
+       g_object_unref (side_panel_action);
+}
+
+static GMenuModel *
+create_window_menu (void)
+{
+       GMenu *menu;
+       GMenuItem *item;
+
+       menu = g_menu_new ();
+
+       /* TODO create and use Amtk factory function, plus a utility function to
+        * append+unref a GMenuItem to a GMenu.
+        */
+       item = g_menu_item_new ("_Side Panel", "win.show-side-panel");
+       g_menu_append_item (menu, item);
+       g_object_unref (item);
+
+       item = g_menu_item_new ("_Print", "win.print");
+       g_menu_append_item (menu, item);
+       g_object_unref (item);
+
+       g_menu_freeze (menu);
+
+       return G_MENU_MODEL (menu);
+}
+
+static GtkWidget *
+create_header_bar (void)
+{
+       GtkHeaderBar *header_bar;
+       GMenuModel *window_menu;
+       GtkMenuButton *menu_button;
+
+       header_bar = GTK_HEADER_BAR (gtk_header_bar_new ());
+       gtk_header_bar_set_title (header_bar, "Amtk test headerbar");
+       gtk_header_bar_set_show_close_button (header_bar, TRUE);
+
+       /* Menu */
+       menu_button = GTK_MENU_BUTTON (gtk_menu_button_new ());
+       gtk_menu_button_set_direction (menu_button, GTK_ARROW_NONE);
+
+       window_menu = create_window_menu ();
+       gtk_menu_button_set_menu_model (menu_button, window_menu);
+       g_object_unref (window_menu);
+
+       gtk_header_bar_pack_end (header_bar, GTK_WIDGET (menu_button));
+
+       return GTK_WIDGET (header_bar);
+}
+
+static void
+activate_cb (GApplication *g_app,
+            gpointer      user_data)
+{
+       GtkWidget *window;
+       GtkWidget *hgrid;
+       GtkWidget *side_panel;
+
+       window = gtk_application_window_new (GTK_APPLICATION (g_app));
+       gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
+       gtk_window_set_titlebar (GTK_WINDOW (window), create_header_bar ());
+
+       hgrid = gtk_grid_new ();
+       side_panel = gtk_label_new ("Side panel");
+       gtk_container_add (GTK_CONTAINER (hgrid), side_panel);
+       gtk_container_add (GTK_CONTAINER (hgrid), gtk_label_new ("Text view"));
+
+       add_win_actions (GTK_APPLICATION_WINDOW (window), side_panel);
+
+       gtk_container_add (GTK_CONTAINER (window), hgrid);
+       gtk_widget_show_all (window);
+}
+
+int
+main (int    argc,
+      char **argv)
+{
+       GtkApplication *app;
+       int status;
+
+       amtk_init ();
+
+       app = gtk_application_new ("org.gnome.amtk.test-headerbar", G_APPLICATION_FLAGS_NONE);
+
+       g_signal_connect (app,
+                         "startup",
+                         G_CALLBACK (startup_cb),
+                         NULL);
+
+       g_signal_connect (app,
+                         "activate",
+                         G_CALLBACK (activate_cb),
+                         NULL);
+
+       status = g_application_run (G_APPLICATION (app), argc, argv);
+
+       amtk_finalize ();
+       g_object_unref (app);
+       g_clear_object (&action_info_store);
+       return status;
+}


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