]> Pileus Git - ~andy/gtk/blob - tests/testbbox.c
39a61f03b2510154a93bead0b8e001f9f4159714
[~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, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  *
19  */
20
21 #include <gtk/gtk.h>
22
23 #define N_BUTTONS 3
24
25 GtkWidget *bbox = NULL;
26 GtkWidget *hbbox = NULL, *vbbox = NULL;
27
28 static const char* styles[] = { "GTK_BUTTONBOX_DEFAULT_STYLE",
29                                 "GTK_BUTTONBOX_SPREAD",
30                                 "GTK_BUTTONBOX_EDGE",
31                                 "GTK_BUTTONBOX_START",
32                                 "GTK_BUTTONBOX_END",
33                                 "GTK_BUTTONBOX_CENTER",
34                                 NULL};
35
36 static const char* types[] = { "GtkHButtonBox",
37                                "GtkVButtonBox",
38                                NULL};
39
40 static void
41 populate_combo_with (GtkComboBoxText *combo, const char** elements)
42 {
43   int i;
44   
45   for (i = 0; elements[i] != NULL; i++) {
46     gtk_combo_box_text_append_text (combo, elements[i]);
47   }
48   
49   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
50 }
51
52 static void
53 combo_changed_cb (GtkComboBoxText *combo,
54                   gpointer user_data)
55 {
56   char *text;
57   int i;
58   
59   text = gtk_combo_box_text_get_active_text (combo);
60   
61   for (i = 0; styles[i]; i++) {
62     if (g_str_equal (text, styles[i])) {
63       gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), (GtkButtonBoxStyle)i);
64     }
65   }
66 }
67
68 static void
69 reparent_widget (GtkWidget *widget,
70                  GtkWidget *old_parent,
71                  GtkWidget *new_parent)
72 {
73   g_object_ref (widget);
74   gtk_container_remove (GTK_CONTAINER (old_parent), widget);
75   gtk_container_add (GTK_CONTAINER (new_parent), widget);
76   g_object_unref (widget);
77 }
78
79 static void
80 combo_types_changed_cb (GtkComboBoxText *combo,
81                         GtkWidget **buttons)
82 {
83   int i;
84   char *text;
85   GtkWidget *old_parent, *new_parent;
86   GtkButtonBoxStyle style;
87   
88   text = gtk_combo_box_text_get_active_text (combo);
89   
90   if (g_str_equal (text, "GtkHButtonBox")) {
91     old_parent = vbbox;
92     new_parent = hbbox;
93   } else {
94     old_parent = hbbox;
95     new_parent = vbbox;
96   }
97   
98   bbox = new_parent;
99   
100   for (i = 0; i < N_BUTTONS; i++) {
101     reparent_widget (buttons[i], old_parent, new_parent);
102   }
103   
104   gtk_widget_hide (old_parent);
105   style = gtk_button_box_get_layout (GTK_BUTTON_BOX (old_parent));
106   gtk_button_box_set_layout (GTK_BUTTON_BOX (new_parent), style);
107   gtk_widget_show (new_parent);
108 }
109
110 static void
111 option_cb (GtkToggleButton *option,
112            GtkWidget *button)
113 {
114   gboolean active = gtk_toggle_button_get_active (option);
115   
116   gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (bbox),
117                                       button, active);
118 }
119
120 static const char* strings[] = { "Ok", "Cancel", "Help" };
121
122 int
123 main (int    argc,
124       char **argv)
125 {
126   GtkWidget *window, *buttons[N_BUTTONS];
127   GtkWidget *vbox, *hbox, *combo_styles, *combo_types, *option;
128   int i;
129   
130   gtk_init (&argc, &argv);
131   
132   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
133   g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (gtk_main_quit), NULL);
134   
135   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
136   gtk_container_add (GTK_CONTAINER (window), vbox);
137   
138   /* GtkHButtonBox */
139   
140   hbbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
141   gtk_box_pack_start (GTK_BOX (vbox), hbbox, TRUE, TRUE, 5);
142   
143   for (i = 0; i < N_BUTTONS; i++) {
144     buttons[i] = gtk_button_new_with_label (strings[i]);
145     gtk_container_add (GTK_CONTAINER (hbbox), buttons[i]);
146   }
147   
148   bbox = hbbox;
149   
150   /* GtkVButtonBox */
151   vbbox = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
152   gtk_box_pack_start (GTK_BOX (vbox), vbbox, TRUE, TRUE, 5);
153   
154   /* Options */
155   
156   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 0);
157   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
158   
159   combo_types = gtk_combo_box_text_new ();
160   populate_combo_with (GTK_COMBO_BOX_TEXT (combo_types), types);
161   g_signal_connect (G_OBJECT (combo_types), "changed", G_CALLBACK (combo_types_changed_cb), buttons);
162   gtk_box_pack_start (GTK_BOX (hbox), combo_types, TRUE, TRUE, 0);
163   
164   combo_styles = gtk_combo_box_text_new ();
165   populate_combo_with (GTK_COMBO_BOX_TEXT (combo_styles), styles);
166   g_signal_connect (G_OBJECT (combo_styles), "changed", G_CALLBACK (combo_changed_cb), NULL);
167   gtk_box_pack_start (GTK_BOX (hbox), combo_styles, TRUE, TRUE, 0);
168   
169   option = gtk_check_button_new_with_label ("Help is secondary");
170   g_signal_connect (G_OBJECT (option), "toggled", G_CALLBACK (option_cb), buttons[N_BUTTONS - 1]);
171   
172   gtk_box_pack_start (GTK_BOX (hbox), option, FALSE, FALSE, 0);
173   
174   gtk_widget_show_all (window);
175   gtk_widget_hide (vbbox);
176   
177   gtk_main ();
178   
179   return 0;
180 }