testtitlebar.c 6.51 KB
Newer Older
1 2
#include <gtk/gtk.h>

3 4 5
static void
on_text_changed (GtkEntry       *entry,
                 GParamSpec     *pspec,
6
                 GtkHeaderBar   *bar)
7 8 9 10 11
{
  const gchar *layout;

  layout = gtk_entry_get_text (entry);

12
  gtk_header_bar_set_decoration_layout (bar, layout);
13 14
}

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static void
create_widgets (GtkHeaderBar *bar,
                GtkPackType   pack_type,
                gint          n)
{
  GList *children, *l;
  GtkWidget *child;
  gint i;
  gchar *label;

  children = gtk_container_get_children (GTK_CONTAINER (bar));
  for (l = children; l; l = l->next)
    {
      GtkPackType type;

      child = l->data;
      gtk_container_child_get (GTK_CONTAINER (bar), child, "pack-type", &type, NULL);
      if (type == pack_type)
        gtk_container_remove (GTK_CONTAINER (bar), child);
    }
  g_list_free (children);

  for (i = 0; i < n; i++)
    {
      label = g_strdup_printf ("%d", i);
      child = gtk_button_new_with_label (label);
      g_free (label);

      gtk_widget_show (child);
      if (pack_type == GTK_PACK_START)
        gtk_header_bar_pack_start (bar, child);
      else
        gtk_header_bar_pack_end (bar, child);
    }
}

static void
change_start (GtkSpinButton *button,
              GParamSpec    *pspec,
              GtkHeaderBar  *bar)
{
  create_widgets (bar,
                  GTK_PACK_START,
                  gtk_spin_button_get_value_as_int (button));
}

static void
change_end (GtkSpinButton *button,
            GParamSpec    *pspec,
            GtkHeaderBar  *bar)
{
  create_widgets (bar,
                  GTK_PACK_END,
                  gtk_spin_button_get_value_as_int (button));
}

71 72
static void
activate (GApplication *gapp)
73
{
74
  GtkApplication *app = GTK_APPLICATION (gapp);
75 76
  GtkWidget *window;
  GtkWidget *header;
77 78 79 80
  GtkWidget *grid;
  GtkWidget *label;
  GtkWidget *entry;
  GtkWidget *check;
81
  GtkWidget *spin;
82 83 84
  GtkBuilder *builder;
  GMenuModel *menu;
  gchar *layout;
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99
  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);
100
  gtk_window_set_icon_name (GTK_WINDOW (window), "preferences-desktop-font");
101

102 103 104
  menu = (GMenuModel*)gtk_builder_get_object (builder, "app-menu");
  gtk_application_add_window (app, GTK_WINDOW (window));
  gtk_application_set_app_menu (app, menu);
105 106 107 108

  header = gtk_header_bar_new ();
  gtk_window_set_titlebar (GTK_WINDOW (window), header);

109 110 111 112 113 114 115 116 117 118 119
  grid = gtk_grid_new ();
  g_object_set (grid,
                "halign", GTK_ALIGN_CENTER,
                "margin", 20,
                "row-spacing", 12,
                "column-spacing", 12,
                NULL);

  label = gtk_label_new ("Title");
  gtk_widget_set_halign (label, GTK_ALIGN_END);
  entry = gtk_entry_new ();
120 121
  g_object_bind_property (header, "title",
                          entry, "text",
122
                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
123 124 125 126 127 128
  gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 0, 1, 1);

  label = gtk_label_new ("Subtitle");
  gtk_widget_set_halign (label, GTK_ALIGN_END);
  entry = gtk_entry_new ();
129 130
  g_object_bind_property (header, "subtitle",
                          entry, "text",
131
                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
132 133 134
  gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 1, 1, 1);

135 136 137 138
  label = gtk_label_new ("Layout");
  gtk_widget_set_halign (label, GTK_ALIGN_END);
  entry = gtk_entry_new ();

139
  g_object_get (gtk_widget_get_settings (window), "gtk-decoration-layout", &layout, NULL);
140 141 142 143
  gtk_entry_set_text (GTK_ENTRY (entry), layout);
  g_free (layout);

  g_signal_connect (entry, "notify::text",
144
                    G_CALLBACK (on_text_changed), header);
145 146 147
  gtk_grid_attach (GTK_GRID (grid), label, 0, 2, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), entry, 1, 2, 1, 1);

148
  label = gtk_label_new ("Decorations");
149 150
  gtk_widget_set_halign (label, GTK_ALIGN_END);
  check = gtk_check_button_new ();
151 152
  g_object_bind_property (header, "show-close-button",
                          check, "active",
153 154 155 156
                          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);

157 158 159
  label = gtk_label_new ("Has Subtitle");
  gtk_widget_set_halign (label, GTK_ALIGN_END);
  check = gtk_check_button_new ();
160 161
  g_object_bind_property (header, "has-subtitle",
                          check, "active",
162
                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
163 164 165
  gtk_grid_attach (GTK_GRID (grid), label, 2, 1, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), check, 3, 1, 1, 1);

166
  label = gtk_label_new ("Shell Shows Menu");
167 168
  gtk_widget_set_halign (label, GTK_ALIGN_END);
  check = gtk_check_button_new ();
169 170
  g_object_bind_property (gtk_settings_get_default (), "gtk-shell-shows-app-menu",
                          check, "active",
171 172 173
                          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);
174

175 176 177 178 179 180 181 182 183 184 185 186
  label = gtk_label_new ("Custom");
  gtk_widget_set_halign (label, GTK_ALIGN_END);
  spin = gtk_spin_button_new_with_range (0, 10, 1);
  g_signal_connect (spin, "notify::value",
                    G_CALLBACK (change_start), header);
  gtk_grid_attach (GTK_GRID (grid), label, 0, 3, 1, 1);
  gtk_grid_attach (GTK_GRID (grid), spin, 1, 3, 1, 1);
  spin = gtk_spin_button_new_with_range (0, 10, 1);
  g_signal_connect (spin, "notify::value",
                    G_CALLBACK (change_end), header);
  gtk_grid_attach (GTK_GRID (grid), spin, 2, 3, 2, 1);
  
187
  gtk_container_add (GTK_CONTAINER (window), grid);
188
  gtk_widget_show_all (window);
189
}
190

191 192 193 194
int
main (int argc, char *argv[])
{
  GtkApplication *app;
195

196 197
  app = gtk_application_new ("org.gtk.Test.titlebar", 0);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
198

199
  return g_application_run (G_APPLICATION (app), argc, argv);
200
}