1
by Michael Terry
Import upstream version 2.91.3 |
1 |
/* testcombochange.c
|
2 |
* Copyright (C) 2004 Red Hat, Inc.
|
|
3 |
* Author: Owen Taylor
|
|
4 |
*
|
|
5 |
* This library is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU Library General Public
|
|
7 |
* License as published by the Free Software Foundation; either
|
|
8 |
* version 2 of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This library is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 |
* Library General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU Library General Public
|
|
1.1.19
by Sebastien Bacher
Import upstream version 3.4.2 |
16 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
1
by Michael Terry
Import upstream version 2.91.3 |
17 |
*/
|
18 |
||
19 |
#include "config.h" |
|
20 |
#include <gtk/gtk.h> |
|
21 |
#include <stdarg.h> |
|
22 |
||
23 |
GtkWidget *text_view; |
|
24 |
GtkListStore *model; |
|
25 |
GArray *contents; |
|
26 |
||
27 |
static char next_value = 'A'; |
|
28 |
||
29 |
static void |
|
30 |
test_init (void) |
|
31 |
{
|
|
1.2.2
by Sjoerd Simons
Import upstream version 3.2.1 |
32 |
if (g_file_test ("../modules/input/immodules.cache", G_FILE_TEST_EXISTS)) |
33 |
g_setenv ("GTK_IM_MODULE_FILE", "../modules/input/immodules.cache", TRUE); |
|
1
by Michael Terry
Import upstream version 2.91.3 |
34 |
}
|
35 |
||
36 |
static void |
|
37 |
combochange_log (const char *fmt, |
|
38 |
...)
|
|
39 |
{
|
|
40 |
GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)); |
|
41 |
GtkTextIter iter; |
|
42 |
va_list vap; |
|
43 |
char *msg; |
|
44 |
GString *order_string; |
|
45 |
GtkTextMark *tmp_mark; |
|
46 |
int i; |
|
47 |
||
48 |
va_start (vap, fmt); |
|
49 |
||
50 |
msg = g_strdup_vprintf (fmt, vap); |
|
51 |
||
52 |
gtk_text_buffer_get_end_iter (buffer, &iter); |
|
53 |
gtk_text_buffer_insert (buffer, &iter, msg, -1); |
|
54 |
||
55 |
order_string = g_string_new ("\n "); |
|
56 |
for (i = 0; i < contents->len; i++) |
|
57 |
{
|
|
58 |
if (i) |
|
59 |
g_string_append_c (order_string, ' '); |
|
60 |
g_string_append_c (order_string, g_array_index (contents, char, i)); |
|
61 |
}
|
|
62 |
g_string_append_c (order_string, '\n'); |
|
63 |
gtk_text_buffer_insert (buffer, &iter, order_string->str, -1); |
|
64 |
g_string_free (order_string, TRUE); |
|
65 |
||
66 |
tmp_mark = gtk_text_buffer_create_mark (buffer, NULL, &iter, FALSE); |
|
67 |
gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (text_view), tmp_mark); |
|
68 |
gtk_text_buffer_delete_mark (buffer, tmp_mark); |
|
69 |
||
70 |
g_free (msg); |
|
71 |
}
|
|
72 |
||
73 |
static GtkWidget * |
|
74 |
align_button_new (const char *text) |
|
75 |
{
|
|
76 |
GtkWidget *button = gtk_button_new (); |
|
77 |
GtkWidget *label = gtk_label_new (text); |
|
78 |
||
79 |
gtk_container_add (GTK_CONTAINER (button), label); |
|
80 |
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); |
|
81 |
||
82 |
return button; |
|
83 |
}
|
|
84 |
||
85 |
static GtkWidget * |
|
86 |
create_combo (const char *name, |
|
87 |
gboolean is_list) |
|
88 |
{
|
|
89 |
GtkCellRenderer *cell_renderer; |
|
90 |
GtkWidget *combo; |
|
1.1.3
by Robert Ancell
Import upstream version 2.91.6 |
91 |
GtkCssProvider *provider; |
92 |
GtkStyleContext *context; |
|
93 |
gchar *css_data; |
|
1
by Michael Terry
Import upstream version 2.91.3 |
94 |
|
95 |
combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model)); |
|
96 |
cell_renderer = gtk_cell_renderer_text_new (); |
|
97 |
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell_renderer, TRUE); |
|
98 |
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell_renderer, |
|
99 |
"text", 0, NULL); |
|
100 |
||
101 |
gtk_widget_set_name (combo, name); |
|
1.1.3
by Robert Ancell
Import upstream version 2.91.6 |
102 |
|
103 |
context = gtk_widget_get_style_context (combo); |
|
104 |
||
105 |
provider = gtk_css_provider_new (); |
|
106 |
css_data = g_strdup_printf ("#%s { -GtkComboBox-appears-as-list: %s }", |
|
107 |
name, is_list ? "true" : "false"); |
|
108 |
gtk_css_provider_load_from_data (provider, css_data, -1, NULL); |
|
109 |
g_free (css_data); |
|
110 |
||
111 |
gtk_style_context_add_provider (context, |
|
112 |
GTK_STYLE_PROVIDER (provider), |
|
113 |
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
|
114 |
||
1
by Michael Terry
Import upstream version 2.91.3 |
115 |
return combo; |
116 |
}
|
|
117 |
||
118 |
static void |
|
119 |
on_insert (void) |
|
120 |
{
|
|
121 |
GtkTreeIter iter; |
|
122 |
||
123 |
int insert_pos; |
|
124 |
char new_value[2]; |
|
125 |
||
126 |
new_value[0] = next_value++; |
|
127 |
new_value[1] = '\0'; |
|
128 |
||
129 |
if (next_value > 'Z') |
|
130 |
next_value = 'A'; |
|
131 |
||
132 |
if (contents->len) |
|
133 |
insert_pos = g_random_int_range (0, contents->len + 1); |
|
134 |
else
|
|
135 |
insert_pos = 0; |
|
136 |
||
137 |
gtk_list_store_insert (model, &iter, insert_pos); |
|
138 |
gtk_list_store_set (model, &iter, 0, new_value, -1); |
|
139 |
||
140 |
g_array_insert_val (contents, insert_pos, new_value); |
|
141 |
||
142 |
combochange_log ("Inserted '%c' at position %d", new_value[0], insert_pos); |
|
143 |
}
|
|
144 |
||
145 |
static void |
|
146 |
on_delete (void) |
|
147 |
{
|
|
148 |
GtkTreeIter iter; |
|
149 |
||
150 |
int delete_pos; |
|
151 |
char old_val; |
|
152 |
||
153 |
if (!contents->len) |
|
154 |
return; |
|
155 |
||
156 |
delete_pos = g_random_int_range (0, contents->len); |
|
157 |
gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (model), &iter, NULL, delete_pos); |
|
158 |
||
159 |
gtk_list_store_remove (model, &iter); |
|
160 |
||
161 |
old_val = g_array_index (contents, char, delete_pos); |
|
162 |
g_array_remove_index (contents, delete_pos); |
|
163 |
combochange_log ("Deleted '%c' from position %d", old_val, delete_pos); |
|
164 |
}
|
|
165 |
||
166 |
static void |
|
167 |
on_reorder (void) |
|
168 |
{
|
|
169 |
GArray *new_contents; |
|
170 |
gint *shuffle_array; |
|
171 |
gint i; |
|
172 |
||
173 |
shuffle_array = g_new (int, contents->len); |
|
174 |
||
175 |
for (i = 0; i < contents->len; i++) |
|
176 |
shuffle_array[i] = i; |
|
177 |
||
178 |
for (i = 0; i + 1 < contents->len; i++) |
|
179 |
{
|
|
180 |
gint pos = g_random_int_range (i, contents->len); |
|
181 |
gint tmp; |
|
182 |
||
183 |
tmp = shuffle_array[i]; |
|
184 |
shuffle_array[i] = shuffle_array[pos]; |
|
185 |
shuffle_array[pos] = tmp; |
|
186 |
}
|
|
187 |
||
188 |
gtk_list_store_reorder (model, shuffle_array); |
|
189 |
||
190 |
new_contents = g_array_new (FALSE, FALSE, sizeof (char)); |
|
191 |
for (i = 0; i < contents->len; i++) |
|
192 |
g_array_append_val (new_contents, |
|
193 |
g_array_index (contents, char, shuffle_array[i])); |
|
194 |
g_array_free (contents, TRUE); |
|
195 |
contents = new_contents; |
|
196 |
||
197 |
combochange_log ("Reordered array"); |
|
198 |
||
199 |
g_free (shuffle_array); |
|
200 |
}
|
|
201 |
||
202 |
static int n_animations = 0; |
|
203 |
static int timer = 0; |
|
204 |
||
205 |
static gint |
|
206 |
animation_timer (gpointer data) |
|
207 |
{
|
|
208 |
switch (g_random_int_range (0, 3)) |
|
209 |
{
|
|
210 |
case 0: |
|
211 |
on_insert (); |
|
212 |
break; |
|
213 |
case 1: |
|
214 |
on_delete (); |
|
215 |
break; |
|
216 |
case 2: |
|
217 |
on_reorder (); |
|
218 |
break; |
|
219 |
}
|
|
220 |
||
221 |
n_animations--; |
|
222 |
return n_animations > 0; |
|
223 |
}
|
|
224 |
||
225 |
static void |
|
226 |
on_animate (void) |
|
227 |
{
|
|
228 |
n_animations += 20; |
|
229 |
||
230 |
timer = gdk_threads_add_timeout (1000, (GSourceFunc) animation_timer, NULL); |
|
231 |
}
|
|
232 |
||
233 |
int
|
|
234 |
main (int argc, char **argv) |
|
235 |
{
|
|
236 |
GtkWidget *content_area; |
|
237 |
GtkWidget *dialog; |
|
238 |
GtkWidget *scrolled_window; |
|
239 |
GtkWidget *hbox; |
|
240 |
GtkWidget *button_vbox; |
|
241 |
GtkWidget *combo_vbox; |
|
242 |
GtkWidget *button; |
|
243 |
GtkWidget *menu_combo; |
|
244 |
GtkWidget *alignment; |
|
245 |
GtkWidget *label; |
|
246 |
GtkWidget *list_combo; |
|
247 |
||
248 |
test_init (); |
|
249 |
||
250 |
gtk_init (&argc, &argv); |
|
251 |
||
252 |
model = gtk_list_store_new (1, G_TYPE_STRING); |
|
253 |
contents = g_array_new (FALSE, FALSE, sizeof (char)); |
|
254 |
||
255 |
dialog = gtk_dialog_new_with_buttons ("GtkComboBox model changes", |
|
256 |
NULL, 0, |
|
257 |
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
|
258 |
NULL); |
|
259 |
||
260 |
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); |
|
261 |
||
262 |
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12); |
|
263 |
gtk_container_set_border_width (GTK_CONTAINER (hbox), 12); |
|
264 |
gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0); |
|
265 |
||
266 |
combo_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8); |
|
267 |
gtk_box_pack_start (GTK_BOX (hbox), combo_vbox, FALSE, FALSE, 0); |
|
268 |
||
269 |
label = gtk_label_new (NULL); |
|
270 |
gtk_label_set_markup (GTK_LABEL (label), "<b>Menu mode</b>"); |
|
271 |
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); |
|
272 |
gtk_box_pack_start (GTK_BOX (combo_vbox), label, FALSE, FALSE, 0); |
|
273 |
||
274 |
alignment = g_object_new (GTK_TYPE_ALIGNMENT, "left-padding", 12, NULL); |
|
275 |
gtk_box_pack_start (GTK_BOX (combo_vbox), alignment, FALSE, FALSE, 0); |
|
276 |
||
277 |
menu_combo = create_combo ("menu-combo", FALSE); |
|
278 |
gtk_container_add (GTK_CONTAINER (alignment), menu_combo); |
|
279 |
||
280 |
label = gtk_label_new (NULL); |
|
281 |
gtk_label_set_markup (GTK_LABEL (label), "<b>List mode</b>"); |
|
282 |
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); |
|
283 |
gtk_box_pack_start (GTK_BOX (combo_vbox), label, FALSE, FALSE, 0); |
|
284 |
||
285 |
alignment = g_object_new (GTK_TYPE_ALIGNMENT, "left-padding", 12, NULL); |
|
286 |
gtk_box_pack_start (GTK_BOX (combo_vbox), alignment, FALSE, FALSE, 0); |
|
287 |
||
288 |
list_combo = create_combo ("list-combo", TRUE); |
|
289 |
gtk_container_add (GTK_CONTAINER (alignment), list_combo); |
|
290 |
||
291 |
scrolled_window = gtk_scrolled_window_new (NULL, NULL); |
|
292 |
gtk_box_pack_start (GTK_BOX (hbox), scrolled_window, TRUE, TRUE, 0); |
|
293 |
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), |
|
294 |
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
|
295 |
||
296 |
text_view = gtk_text_view_new (); |
|
297 |
gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), FALSE); |
|
298 |
gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (text_view), FALSE); |
|
299 |
||
300 |
gtk_container_add (GTK_CONTAINER (scrolled_window), text_view); |
|
301 |
||
302 |
button_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8); |
|
303 |
gtk_box_pack_start (GTK_BOX (hbox), button_vbox, FALSE, FALSE, 0); |
|
304 |
||
305 |
gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 300); |
|
306 |
||
307 |
button = align_button_new ("Insert"); |
|
308 |
gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0); |
|
309 |
g_signal_connect (button, "clicked", G_CALLBACK (on_insert), NULL); |
|
310 |
||
311 |
button = align_button_new ("Delete"); |
|
312 |
gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0); |
|
313 |
g_signal_connect (button, "clicked", G_CALLBACK (on_delete), NULL); |
|
314 |
||
315 |
button = align_button_new ("Reorder"); |
|
316 |
gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0); |
|
317 |
g_signal_connect (button, "clicked", G_CALLBACK (on_reorder), NULL); |
|
318 |
||
319 |
button = align_button_new ("Animate"); |
|
320 |
gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0); |
|
321 |
g_signal_connect (button, "clicked", G_CALLBACK (on_animate), NULL); |
|
322 |
||
323 |
gtk_widget_show_all (dialog); |
|
324 |
gtk_dialog_run (GTK_DIALOG (dialog)); |
|
325 |
||
326 |
return 0; |
|
327 |
}
|