]> Pileus Git - ~andy/gtk/blob - tests/testbbox.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testbbox.c
1 /*
2  * Copyright (C) 2006 Nokia Corporation.
3  * Author: Xan Lopez <xan.lopez@nokia.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * version 2.1 as published by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <gtk/gtk.h>
20
21 #define N_BUTTONS 3
22
23 GtkWidget *bbox = NULL;
24 GtkWidget *hbbox = NULL, *vbbox = NULL;
25
26 static const char* styles[] = { "GTK_BUTTONBOX_DEFAULT_STYLE",
27                                 "GTK_BUTTONBOX_SPREAD",
28                                 "GTK_BUTTONBOX_EDGE",
29                                 "GTK_BUTTONBOX_START",
30                                 "GTK_BUTTONBOX_END",
31                                 "GTK_BUTTONBOX_CENTER",
32                                 NULL};
33
34 static const char* types[] = { "GtkHButtonBox",
35                                "GtkVButtonBox",
36                                NULL};
37
38 static void
39 populate_combo_with (GtkComboBoxText *combo, const char** elements)
40 {
41   int i;
42   
43   for (i = 0; elements[i] != NULL; i++) {
44     gtk_combo_box_text_append_text (combo, elements[i]);
45   }
46   
47   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
48 }
49
50 static void
51 combo_changed_cb (GtkComboBoxText *combo,
52                   gpointer user_data)
53 {
54   char *text;
55   int i;
56   
57   text = gtk_combo_box_text_get_active_text (combo);
58   
59   for (i = 0; styles[i]; i++) {
60     if (g_str_equal (text, styles[i])) {
61       gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), (GtkButtonBoxStyle)i);
62     }
63   }
64 }
65
66 static void
67 reparent_widget (GtkWidget *widget,
68                  GtkWidget *old_parent,
69                  GtkWidget *new_parent)
70 {
71   g_object_ref (widget);
72   gtk_container_remove (GTK_CONTAINER (old_parent), widget);
73   gtk_container_add (GTK_CONTAINER (new_parent), widget);
74   g_object_unref (widget);
75 }
76
77 static void
78 combo_types_changed_cb (GtkComboBoxText *combo,
79                         GtkWidget **buttons)
80 {
81   int i;
82   char *text;
83   GtkWidget *old_parent, *new_parent;
84   GtkButtonBoxStyle style;
85   
86   text = gtk_combo_box_text_get_active_text (combo);
87   
88   if (g_str_equal (text, "GtkHButtonBox")) {
89     old_parent = vbbox;
90     new_parent = hbbox;
91   } else {
92     old_parent = hbbox;
93     new_parent = vbbox;
94   }
95   
96   bbox = new_parent;
97   
98   for (i = 0; i < N_BUTTONS; i++) {
99     reparent_widget (buttons[i], old_parent, new_parent);
100   }
101   
102   gtk_widget_hide (old_parent);
103   style = gtk_button_box_get_layout (GTK_BUTTON_BOX (old_parent));
104   gtk_button_box_set_layout (GTK_BUTTON_BOX (new_parent), style);
105   gtk_widget_show (new_parent);
106 }
107
108 static void
109 option_cb (GtkToggleButton *option,
110            GtkWidget *button)
111 {
112   gboolean active = gtk_toggle_button_get_active (option);
113   
114   gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (bbox),
115                                       button, active);
116 }
117
118 static const char* strings[] = { "Ok", "Cancel", "Help" };
119
120 int
121 main (int    argc,
122       char **argv)
123 {
124   GtkWidget *window, *buttons[N_BUTTONS];
125   GtkWidget *vbox, *hbox, *combo_styles, *combo_types, *option;
126   int i;
127   
128   gtk_init (&argc, &argv);
129   
130   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
131   g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (gtk_main_quit), NULL);
132   
133   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
134   gtk_container_add (GTK_CONTAINER (window), vbox);
135   
136   /* GtkHButtonBox */
137   
138   hbbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
139   gtk_box_pack_start (GTK_BOX (vbox), hbbox, TRUE, TRUE, 5);
140   
141   for (i = 0; i < N_BUTTONS; i++) {
142     buttons[i] = gtk_button_new_with_label (strings[i]);
143     gtk_container_add (GTK_CONTAINER (hbbox), buttons[i]);
144   }
145   
146   bbox = hbbox;
147   
148   /* GtkVButtonBox */
149   vbbox = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
150   gtk_box_pack_start (GTK_BOX (vbox), vbbox, TRUE, TRUE, 5);
151   
152   /* Options */
153   
154   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
155   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
156   
157   combo_types = gtk_combo_box_text_new ();
158   populate_combo_with (GTK_COMBO_BOX_TEXT (combo_types), types);
159   g_signal_connect (G_OBJECT (combo_types), "changed", G_CALLBACK (combo_types_changed_cb), buttons);
160   gtk_box_pack_start (GTK_BOX (hbox), combo_types, TRUE, TRUE, 0);
161   
162   combo_styles = gtk_combo_box_text_new ();
163   populate_combo_with (GTK_COMBO_BOX_TEXT (combo_styles), styles);
164   g_signal_connect (G_OBJECT (combo_styles), "changed", G_CALLBACK (combo_changed_cb), NULL);
165   gtk_box_pack_start (GTK_BOX (hbox), combo_styles, TRUE, TRUE, 0);
166   
167   option = gtk_check_button_new_with_label ("Help is secondary");
168   g_signal_connect (G_OBJECT (option), "toggled", G_CALLBACK (option_cb), buttons[N_BUTTONS - 1]);
169   
170   gtk_box_pack_start (GTK_BOX (hbox), option, FALSE, FALSE, 0);
171   
172   gtk_widget_show_all (window);
173   gtk_widget_hide (vbbox);
174   
175   gtk_main ();
176   
177   return 0;
178 }