teststack.c 8.38 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1 2 3 4
#include <gtk/gtk.h>

GtkWidget *stack;
GtkWidget *switcher;
Ikey Doherty's avatar
Ikey Doherty committed
5
GtkWidget *sidebar;
Matthias Clasen's avatar
Matthias Clasen committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
GtkWidget *w1;

static void
set_visible_child (GtkWidget *button, gpointer data)
{
  gtk_stack_set_visible_child (GTK_STACK (stack), GTK_WIDGET (data));
}

static void
set_visible_child_name (GtkWidget *button, gpointer data)
{
  gtk_stack_set_visible_child_name (GTK_STACK (stack), (const char *)data);
}

static void
21
toggle_hhomogeneous (GtkWidget *button, gpointer data)
Matthias Clasen's avatar
Matthias Clasen committed
22 23
{
  gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
24 25 26 27 28 29 30 31
  gtk_stack_set_hhomogeneous (GTK_STACK (stack), active);
}

static void
toggle_vhomogeneous (GtkWidget *button, gpointer data)
{
  gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
  gtk_stack_set_vhomogeneous (GTK_STACK (stack), active);
Matthias Clasen's avatar
Matthias Clasen committed
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
}

static void
toggle_icon_name (GtkWidget *button, gpointer data)
{
  gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
  gtk_container_child_set (GTK_CONTAINER (stack), w1,
			   "icon-name", active ? "edit-find-symbolic" : NULL,
			   NULL);
}

static void
toggle_transitions (GtkWidget *combo, gpointer data)
{
  int id = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
  gtk_stack_set_transition_type (GTK_STACK (stack), id);
}

static void
on_back_button_clicked (GtkButton *button, GtkStack *stack)
{
  const gchar *seq[] = { "1", "2", "3" };
  const gchar *vis;
  gint i;

  vis = gtk_stack_get_visible_child_name (stack);

  for (i = 1; i < G_N_ELEMENTS (seq); i++)
    {
61
      if (g_strcmp0 (vis, seq[i]) == 0)
Matthias Clasen's avatar
Matthias Clasen committed
62
        {
63
          gtk_stack_set_visible_child_full (stack, seq[i - 1], GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
Matthias Clasen's avatar
Matthias Clasen committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
          break;
        }
    }
}

static void
on_forward_button_clicked (GtkButton *button, GtkStack *stack)
{
  const gchar *seq[] = { "1", "2", "3" };
  const gchar *vis;
  gint i;

  vis = gtk_stack_get_visible_child_name (stack);

  for (i = 0; i < G_N_ELEMENTS (seq) - 1; i++)
    {
80
      if (g_strcmp0 (vis, seq[i]) == 0)
Matthias Clasen's avatar
Matthias Clasen committed
81
        {
82
          gtk_stack_set_visible_child_full (stack, seq[i + 1], GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT);
Matthias Clasen's avatar
Matthias Clasen committed
83 84 85 86 87 88 89 90 91 92 93
          break;
        }
    }
}

static void
update_back_button_sensitivity (GtkStack *stack, GParamSpec *pspec, GtkWidget *button)
{
  const gchar *vis;

  vis = gtk_stack_get_visible_child_name (stack);
94
  gtk_widget_set_sensitive (button, g_strcmp0 (vis, "1") != 0);
Matthias Clasen's avatar
Matthias Clasen committed
95 96 97 98 99 100 101 102
}

static void
update_forward_button_sensitivity (GtkStack *stack, GParamSpec *pspec, GtkWidget *button)
{
  const gchar *vis;

  vis = gtk_stack_get_visible_child_name (stack);
103
  gtk_widget_set_sensitive (button, g_strcmp0 (vis, "3") != 0);
Matthias Clasen's avatar
Matthias Clasen committed
104 105 106 107 108 109
}

gint
main (gint argc,
      gchar ** argv)
{
Ikey Doherty's avatar
Ikey Doherty committed
110
  GtkWidget *window, *box, *button, *hbox, *combo, *layout;
Matthias Clasen's avatar
Matthias Clasen committed
111 112 113 114 115 116 117 118
  GtkWidget *w2, *w3;
  GtkListStore* store;
  GtkWidget *tree_view;
  GtkTreeViewColumn *column;
  GtkCellRenderer *renderer;
  GtkWidget *scrolled_win;
  int i;
  GtkTreeIter iter;
119
  GEnumClass *class;
Matthias Clasen's avatar
Matthias Clasen committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request (window, 300, 300);

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add (GTK_CONTAINER (window), box);

  switcher = gtk_stack_switcher_new ();
  gtk_box_pack_start (GTK_BOX (box), switcher, FALSE, FALSE, 0);

  stack = gtk_stack_new ();

  /* Make transitions longer so we can see that they work */
135
  gtk_stack_set_transition_duration (GTK_STACK (stack), 1500);
Matthias Clasen's avatar
Matthias Clasen committed
136 137

  gtk_widget_set_halign (stack, GTK_ALIGN_START);
Benjamin Otte's avatar
Benjamin Otte committed
138
  gtk_widget_set_vexpand (stack, TRUE);
Ikey Doherty's avatar
Ikey Doherty committed
139 140

  /* Add sidebar before stack */
141 142
  sidebar = gtk_stack_sidebar_new ();
  gtk_stack_sidebar_set_stack (GTK_STACK_SIDEBAR (sidebar), GTK_STACK (stack));
Ikey Doherty's avatar
Ikey Doherty committed
143 144 145 146 147
  layout = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_box_pack_start (GTK_BOX (layout), sidebar, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (layout), stack, TRUE, TRUE, 0);

  gtk_container_add (GTK_CONTAINER (box), layout);
Matthias Clasen's avatar
Matthias Clasen committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

  gtk_stack_switcher_set_stack (GTK_STACK_SWITCHER (switcher), GTK_STACK (stack));

  w1 = gtk_text_view_new ();
  gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (w1)),
			    "This is a\nTest\nBalh!", -1);

  gtk_container_add_with_properties (GTK_CONTAINER (stack), w1,
				     "name", "1",
				     "title", "1",
				     NULL);

  w2 = gtk_button_new_with_label ("Gazoooooooooooooooonk");
  gtk_container_add (GTK_CONTAINER (stack), w2);
  gtk_container_child_set (GTK_CONTAINER (stack), w2,
			   "name", "2",
			   "title", "2",
165
                           "needs-attention", TRUE,
Matthias Clasen's avatar
Matthias Clasen committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
			   NULL);


  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_AUTOMATIC);
  gtk_widget_set_size_request (scrolled_win, 100, 200);


  store = gtk_list_store_new (1, G_TYPE_STRING);

  for (i = 0; i < 40; i++)
    gtk_list_store_insert_with_values (store, &iter, i, 0,  "Testvalule", -1);

  tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));

  gtk_container_add (GTK_CONTAINER (scrolled_win), tree_view);
  w3 = scrolled_win;

  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Target", renderer,
						     "text", 0, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);

  gtk_stack_add_titled (GTK_STACK (stack), w3, "3", "3");

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_container_add (GTK_CONTAINER (box), hbox);

  button = gtk_button_new_with_label ("1");
  gtk_container_add (GTK_CONTAINER (hbox), button);
  g_signal_connect (button, "clicked", (GCallback) set_visible_child, w1);

  button = gtk_button_new_with_label ("2");
  gtk_container_add (GTK_CONTAINER (hbox), button);
  g_signal_connect (button, "clicked", (GCallback) set_visible_child, w2);

  button = gtk_button_new_with_label ("3");
  gtk_container_add (GTK_CONTAINER (hbox), button);
  g_signal_connect (button, "clicked", (GCallback) set_visible_child, w3);

  button = gtk_button_new_with_label ("1");
  gtk_container_add (GTK_CONTAINER (hbox), button);
  g_signal_connect (button, "clicked", (GCallback) set_visible_child_name, (gpointer) "1");

  button = gtk_button_new_with_label ("2");
  gtk_container_add (GTK_CONTAINER (hbox), button);
  g_signal_connect (button, "clicked", (GCallback) set_visible_child_name, (gpointer) "2");

  button = gtk_button_new_with_label ("3");
  gtk_container_add (GTK_CONTAINER (hbox), button);
  g_signal_connect (button, "clicked", (GCallback) set_visible_child_name, (gpointer) "3");

220 221 222 223 224 225
  button = gtk_check_button_new ();
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
				gtk_stack_get_hhomogeneous (GTK_STACK (stack)));
  gtk_container_add (GTK_CONTAINER (hbox), button);
  g_signal_connect (button, "clicked", (GCallback) toggle_hhomogeneous, NULL);

Matthias Clasen's avatar
Matthias Clasen committed
226 227
  button = gtk_check_button_new_with_label ("homogeneous");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
228
				gtk_stack_get_vhomogeneous (GTK_STACK (stack)));
Matthias Clasen's avatar
Matthias Clasen committed
229
  gtk_container_add (GTK_CONTAINER (hbox), button);
230
  g_signal_connect (button, "clicked", (GCallback) toggle_vhomogeneous, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
231 232 233 234 235 236

  button = gtk_toggle_button_new_with_label ("Add icon");
  g_signal_connect (button, "toggled", (GCallback) toggle_icon_name, NULL);
  gtk_container_add (GTK_CONTAINER (hbox), button);

  combo = gtk_combo_box_text_new ();
237 238 239 240
  class = g_type_class_ref (GTK_TYPE_STACK_TRANSITION_TYPE);
  for (i = 0; i < class->n_values; i++)
    gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), class->values[i].value_nick);
  g_type_class_unref (class);
Matthias Clasen's avatar
Matthias Clasen committed
241 242 243

  gtk_container_add (GTK_CONTAINER (hbox), combo);
  g_signal_connect (combo, "changed", (GCallback) toggle_transitions, NULL);
244
  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
Matthias Clasen's avatar
Matthias Clasen committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_container_add (GTK_CONTAINER (box), hbox);

  button = gtk_button_new_with_label ("<");
  g_signal_connect (button, "clicked", (GCallback) on_back_button_clicked, stack);
  g_signal_connect (stack, "notify::visible-child-name",
                    (GCallback)update_back_button_sensitivity, button);
  gtk_container_add (GTK_CONTAINER (hbox), button);

  button = gtk_button_new_with_label (">");
  gtk_container_add (GTK_CONTAINER (hbox), button);
  g_signal_connect (button, "clicked", (GCallback) on_forward_button_clicked, stack);
  g_signal_connect (stack, "notify::visible-child-name",
                    (GCallback)update_forward_button_sensitivity, button);


  gtk_widget_show_all (window);
  gtk_main ();

  gtk_widget_destroy (window);

  return 0;
}