[gtk+] testtitlebar: more tests



commit 2f907810732e03bacdb1862f1372f5dd9386e181
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Dec 12 21:12:44 2013 -0500

    testtitlebar: more tests
    
    Turn this into a GtkApplication with an app menu.
    Allow to override the gtk-shell-shows-app-menu setting
    and the decoration-button-layout style property.

 tests/testtitlebar.c |  100 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 85 insertions(+), 15 deletions(-)
---
diff --git a/tests/testtitlebar.c b/tests/testtitlebar.c
index 3aa0309..40cb2c5 100644
--- a/tests/testtitlebar.c
+++ b/tests/testtitlebar.c
@@ -1,22 +1,65 @@
 #include <gtk/gtk.h>
 
-int
-main (int argc, char *argv[])
+static void
+on_text_changed (GtkEntry       *entry,
+                 GParamSpec     *pspec,
+                 GtkCssProvider *provider)
+{
+  const gchar *layout;
+  gchar *css;
+
+  layout = gtk_entry_get_text (entry);
+
+  css = g_strdup_printf ("GtkWindow {\n"
+                         "  -GtkWindow-decoration-button-layout: '%s';\n"
+                         "}", layout);
+
+  gtk_css_provider_load_from_data (provider, css, -1, NULL);
+  g_free (css);
+}
+
+static void
+activate (GApplication *gapp)
 {
+  GtkApplication *app = GTK_APPLICATION (gapp);
   GtkWidget *window;
   GtkWidget *header;
   GtkWidget *grid;
   GtkWidget *label;
   GtkWidget *entry;
   GtkWidget *check;
+  GtkBuilder *builder;
+  GMenuModel *menu;
+  GtkCssProvider *provider;
+  gchar *layout;
 
-  gtk_init (NULL, NULL);
+  g_action_map_add_action (G_ACTION_MAP (gapp), G_ACTION (g_simple_action_new ("test", NULL)));
+  builder = gtk_builder_new ();
+  gtk_builder_add_from_string (builder,
+                               "<interface>"
+                               "  <menu id='app-menu'>"
+                               "    <section>"
+                               "      <item>"
+                               "        <attribute name='label'>Test item</attribute>"
+                               "        <attribute name='action'>app.test</attribute>"
+                               "      </item>"
+                               "    </section>"
+                               "  </menu>"
+                               "</interface>", -1, NULL);
+  window = gtk_application_window_new (app);
 
-  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  menu = (GMenuModel*)gtk_builder_get_object (builder, "app-menu");
+  gtk_application_add_window (app, GTK_WINDOW (window));
+  gtk_application_set_app_menu (app, menu);
 
   header = gtk_header_bar_new ();
   gtk_window_set_titlebar (GTK_WINDOW (window), header);
 
+  provider = gtk_css_provider_new ();
+
+  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
+                                             GTK_STYLE_PROVIDER (provider), 600);
+
   grid = gtk_grid_new ();
   g_object_set (grid,
                 "halign", GTK_ALIGN_CENTER,
@@ -30,7 +73,7 @@ main (int argc, char *argv[])
   entry = gtk_entry_new ();
   g_object_bind_property (entry, "text",
                           header, "title",
-                          G_BINDING_SYNC_CREATE);
+                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
   gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
   gtk_grid_attach (GTK_GRID (grid), entry, 1, 0, 1, 1);
 
@@ -39,34 +82,61 @@ main (int argc, char *argv[])
   entry = gtk_entry_new ();
   g_object_bind_property (entry, "text",
                           header, "subtitle",
-                          G_BINDING_SYNC_CREATE);
+                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
   gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
   gtk_grid_attach (GTK_GRID (grid), entry, 1, 1, 1, 1);
 
+  label = gtk_label_new ("Layout");
+  gtk_widget_set_halign (label, GTK_ALIGN_END);
+  entry = gtk_entry_new ();
+
+  gtk_widget_style_get (window, "decoration-button-layout", &layout, NULL);
+  gtk_entry_set_text (GTK_ENTRY (entry), layout);
+  g_free (layout);
+
+  g_signal_connect (entry, "notify::text",
+                    G_CALLBACK (on_text_changed), provider);
+  gtk_grid_attach (GTK_GRID (grid), label, 0, 2, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), entry, 1, 2, 1, 1);
+
+  label = gtk_label_new ("Close Button");
+  gtk_widget_set_halign (label, GTK_ALIGN_END);
+  check = gtk_check_button_new ();
+  g_object_bind_property (check, "active",
+                          header, "show-close-button",
+                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
+  gtk_grid_attach (GTK_GRID (grid), label, 2, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), check, 3, 0, 1, 1);
+
   label = gtk_label_new ("Has Subtitle");
   gtk_widget_set_halign (label, GTK_ALIGN_END);
   check = gtk_check_button_new ();
   g_object_bind_property (check, "active",
                           header, "has-subtitle",
-                          G_BINDING_SYNC_CREATE);
+                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
   gtk_grid_attach (GTK_GRID (grid), label, 2, 1, 1, 1);
   gtk_grid_attach (GTK_GRID (grid), check, 3, 1, 1, 1);
 
-  label = gtk_label_new ("Close Button");
+  label = gtk_label_new ("Shell Shows Menu");
   gtk_widget_set_halign (label, GTK_ALIGN_END);
   check = gtk_check_button_new ();
   g_object_bind_property (check, "active",
-                          header, "show-close-button",
-                          G_BINDING_SYNC_CREATE);
-  gtk_grid_attach (GTK_GRID (grid), label, 2, 0, 1, 1);
-  gtk_grid_attach (GTK_GRID (grid), check, 3, 0, 1, 1);
+                          gtk_settings_get_default (), "gtk-shell-shows-app-menu",
+                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
+  gtk_grid_attach (GTK_GRID (grid), label, 2, 2, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), check, 3, 2, 1, 1);
 
   gtk_container_add (GTK_CONTAINER (window), grid);
   gtk_widget_show_all (window);
+}
 
-  gtk_main ();
+int
+main (int argc, char *argv[])
+{
+  GtkApplication *app;
 
-  gtk_widget_destroy (window);
+  app = gtk_application_new ("org.gtk.Test.titlebar", 0);
+  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
 
-  return 0;
+  return g_application_run (G_APPLICATION (app), argc, argv);
 }


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