summaryrefslogtreecommitdiff
path: root/tests/testlist2.c
blob: 6b176a0feae821f6ac2db4d82ec9ddf81a494301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <gtk/gtk.h>

static void
row_unrevealed (GObject *revealer, GParamSpec *pspec, gpointer data)
{
  GtkWidget *row, *list;

  row = gtk_widget_get_parent (GTK_WIDGET (revealer));
  list = gtk_widget_get_parent (row);

  gtk_container_remove (GTK_CONTAINER (list), row);
}

static void
remove_this_row (GtkButton *button, GtkWidget *child)
{
  GtkWidget *row, *revealer;

  row = gtk_widget_get_parent (child);
  revealer = gtk_revealer_new ();
  gtk_revealer_set_reveal_child (GTK_REVEALER (revealer), TRUE);
  gtk_widget_show (revealer);
  g_object_ref (child);
  gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (child)), child);
  gtk_revealer_set_child (GTK_REVEALER (revealer), child);
  g_object_unref (child);
  gtk_container_add (GTK_CONTAINER (row), revealer);
  g_signal_connect (revealer, "notify::child-revealed",
                    G_CALLBACK (row_unrevealed), NULL);
  gtk_revealer_set_reveal_child (GTK_REVEALER (revealer), FALSE);
}

static GtkWidget *create_row (const gchar *label);

static void
row_revealed (GObject *revealer, GParamSpec *pspec, gpointer data)
{
  GtkWidget *row, *child;

  row = gtk_widget_get_parent (GTK_WIDGET (revealer));
  child = gtk_revealer_get_child (GTK_REVEALER (revealer));
  g_object_ref (child);
  gtk_revealer_set_child (GTK_REVEALER (revealer), NULL);
  gtk_widget_destroy (GTK_WIDGET (revealer));
  gtk_container_add (GTK_CONTAINER (row), child);
  g_object_unref (child);
}

static void
add_row_below (GtkButton *button, GtkWidget *child)
{
  GtkWidget *revealer, *row, *list;
  gint index;

  row = gtk_widget_get_parent (child);
  index = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (row));
  list = gtk_widget_get_parent (row);
  row = create_row ("Extra row");
  revealer = gtk_revealer_new ();
  gtk_revealer_set_child (GTK_REVEALER (revealer), row);
  g_signal_connect (revealer, "notify::child-revealed",
                    G_CALLBACK (row_revealed), NULL);
  gtk_list_box_insert (GTK_LIST_BOX (list), revealer, index + 1);
  gtk_revealer_set_reveal_child (GTK_REVEALER (revealer), TRUE);
}

static void
add_separator (GtkListBoxRow *row, GtkListBoxRow *before, gpointer data)
{
  if (!before)
    return;

  gtk_list_box_row_set_header (row, gtk_separator_new (GTK_ORIENTATION_HORIZONTAL));
}

static GtkWidget *
create_row (const gchar *text)
{
  GtkWidget *row, *label, *button;

  row = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
  label = gtk_label_new (text);
  gtk_container_add (GTK_CONTAINER (row), label);
  button = gtk_button_new_with_label ("x");
  gtk_widget_set_hexpand (button, TRUE);
  gtk_widget_set_halign (button, GTK_ALIGN_END);
  gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
  gtk_container_add (GTK_CONTAINER (row), button);
  g_signal_connect (button, "clicked", G_CALLBACK (remove_this_row), row);
  button = gtk_button_new_with_label ("+");
  gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
  gtk_container_add (GTK_CONTAINER (row), button);
  g_signal_connect (button, "clicked", G_CALLBACK (add_row_below), row);

  return row;
}

static void
quit_cb (GtkWidget *widget,
         gpointer   data)
{
  gboolean *done = data;

  *done = TRUE;

  g_main_context_wakeup (NULL);
}

int main (int argc, char *argv[])
{
  GtkWidget *window, *list, *sw, *row;
  gint i;
  gchar *text;
  gboolean done = FALSE;

  gtk_init ();

  window = gtk_window_new ();
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);

  list = gtk_list_box_new ();
  gtk_list_box_set_selection_mode (GTK_LIST_BOX (list), GTK_SELECTION_NONE);
  gtk_list_box_set_header_func (GTK_LIST_BOX (list), add_separator, NULL, NULL);
  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_window_set_child (GTK_WINDOW (window), sw);
  gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);

  for (i = 0; i < 20; i++)
    {
      text = g_strdup_printf ("Row %d", i);
      row = create_row (text);
      gtk_list_box_insert (GTK_LIST_BOX (list), row, -1);
    }

  g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
  gtk_widget_show (window);

  while (!done)
    g_main_context_iteration (NULL, TRUE);

  return 0;
}