2 * Copyright (C) 2004 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
29 static char next_value
= 'A';
34 if (g_file_test ("../gdk-pixbuf/libpixbufloader-pnm.la",
37 g_setenv ("GDK_PIXBUF_MODULE_FILE", "../gdk-pixbuf/gdk-pixbuf.loaders", TRUE
);
38 g_setenv ("GTK_IM_MODULE_FILE", "../modules/input/gtk.immodules", TRUE
);
43 combochange_log (const char *fmt
,
46 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view
));
50 GString
*order_string
;
51 GtkTextMark
*tmp_mark
;
56 msg
= g_strdup_vprintf (fmt
, vap
);
58 gtk_text_buffer_get_end_iter (buffer
, &iter
);
59 gtk_text_buffer_insert (buffer
, &iter
, msg
, -1);
61 order_string
= g_string_new ("\n ");
62 for (i
= 0; i
< contents
->len
; i
++)
65 g_string_append_c (order_string
, ' ');
66 g_string_append_c (order_string
, g_array_index (contents
, char, i
));
68 g_string_append_c (order_string
, '\n');
69 gtk_text_buffer_insert (buffer
, &iter
, order_string
->str
, -1);
70 g_string_free (order_string
, TRUE
);
72 tmp_mark
= gtk_text_buffer_create_mark (buffer
, NULL
, &iter
, FALSE
);
73 gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (text_view
), tmp_mark
);
74 gtk_text_buffer_delete_mark (buffer
, tmp_mark
);
80 align_button_new (const char *text
)
82 GtkWidget
*button
= gtk_button_new ();
83 GtkWidget
*label
= gtk_label_new (text
);
85 gtk_container_add (GTK_CONTAINER (button
), label
);
86 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.5);
92 create_combo (const char *name
,
95 GtkCellRenderer
*cell_renderer
;
99 rc_string
= g_strdup_printf ("style \"%s-style\" {\n"
100 " GtkComboBox::appears-as-list = %d\n"
103 "widget \"*.%s\" style \"%s-style\"",
104 name
, is_list
, name
, name
);
105 gtk_rc_parse_string (rc_string
);
108 combo
= gtk_combo_box_new_with_model (GTK_TREE_MODEL (model
));
109 cell_renderer
= gtk_cell_renderer_text_new ();
110 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo
), cell_renderer
, TRUE
);
111 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo
), cell_renderer
,
114 gtk_widget_set_name (combo
, name
);
127 new_value
[0] = next_value
++;
130 if (next_value
> 'Z')
134 insert_pos
= g_random_int_range (0, contents
->len
+ 1);
138 gtk_list_store_insert (model
, &iter
, insert_pos
);
139 gtk_list_store_set (model
, &iter
, 0, new_value
, -1);
141 g_array_insert_val (contents
, insert_pos
, new_value
);
143 combochange_log ("Inserted '%c' at position %d", new_value
[0], insert_pos
);
157 delete_pos
= g_random_int_range (0, contents
->len
);
158 gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (model
), &iter
, NULL
, delete_pos
);
160 gtk_list_store_remove (model
, &iter
);
162 old_val
= g_array_index (contents
, char, delete_pos
);
163 g_array_remove_index (contents
, delete_pos
);
164 combochange_log ("Deleted '%c' from position %d", old_val
, delete_pos
);
170 GArray
*new_contents
;
174 shuffle_array
= g_new (int, contents
->len
);
176 for (i
= 0; i
< contents
->len
; i
++)
177 shuffle_array
[i
] = i
;
179 for (i
= 0; i
+ 1 < contents
->len
; i
++)
181 gint pos
= g_random_int_range (i
, contents
->len
);
184 tmp
= shuffle_array
[i
];
185 shuffle_array
[i
] = shuffle_array
[pos
];
186 shuffle_array
[pos
] = tmp
;
189 gtk_list_store_reorder (model
, shuffle_array
);
191 new_contents
= g_array_new (FALSE
, FALSE
, sizeof (char));
192 for (i
= 0; i
< contents
->len
; i
++)
193 g_array_append_val (new_contents
,
194 g_array_index (contents
, char, shuffle_array
[i
]));
195 g_array_free (contents
, TRUE
);
196 contents
= new_contents
;
198 combochange_log ("Reordered array");
200 g_free (shuffle_array
);
203 static int n_animations
= 0;
204 static int timer
= 0;
207 animation_timer (gpointer data
)
209 switch (g_random_int_range (0, 3))
223 return n_animations
> 0;
231 timer
= gdk_threads_add_timeout (1000, (GSourceFunc
) animation_timer
, NULL
);
235 main (int argc
, char **argv
)
238 GtkWidget
*scrolled_window
;
240 GtkWidget
*button_vbox
;
241 GtkWidget
*combo_vbox
;
243 GtkWidget
*menu_combo
;
244 GtkWidget
*alignment
;
246 GtkWidget
*list_combo
;
250 gtk_init (&argc
, &argv
);
252 model
= gtk_list_store_new (1, G_TYPE_STRING
);
253 contents
= g_array_new (FALSE
, FALSE
, sizeof (char));
255 dialog
= gtk_dialog_new_with_buttons ("GtkComboBox model changes",
256 NULL
, GTK_DIALOG_NO_SEPARATOR
,
257 GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
,
260 hbox
= gtk_hbox_new (FALSE
, 12);
261 gtk_container_set_border_width (GTK_CONTAINER (hbox
), 12);
262 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog
)->vbox
), hbox
, TRUE
, TRUE
, 0);
264 combo_vbox
= gtk_vbox_new (FALSE
, 8);
265 gtk_box_pack_start (GTK_BOX (hbox
), combo_vbox
, FALSE
, FALSE
, 0);
267 label
= gtk_label_new (NULL
);
268 gtk_label_set_markup (GTK_LABEL (label
), "<b>Menu mode</b>");
269 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.5);
270 gtk_box_pack_start (GTK_BOX (combo_vbox
), label
, FALSE
, FALSE
, 0);
272 alignment
= g_object_new (GTK_TYPE_ALIGNMENT
, "left-padding", 12, NULL
);
273 gtk_box_pack_start (GTK_BOX (combo_vbox
), alignment
, FALSE
, FALSE
, 0);
275 menu_combo
= create_combo ("menu-combo", FALSE
);
276 gtk_container_add (GTK_CONTAINER (alignment
), menu_combo
);
278 label
= gtk_label_new (NULL
);
279 gtk_label_set_markup (GTK_LABEL (label
), "<b>List mode</b>");
280 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.5);
281 gtk_box_pack_start (GTK_BOX (combo_vbox
), label
, FALSE
, FALSE
, 0);
283 alignment
= g_object_new (GTK_TYPE_ALIGNMENT
, "left-padding", 12, NULL
);
284 gtk_box_pack_start (GTK_BOX (combo_vbox
), alignment
, FALSE
, FALSE
, 0);
286 list_combo
= create_combo ("list-combo", TRUE
);
287 gtk_container_add (GTK_CONTAINER (alignment
), list_combo
);
289 scrolled_window
= gtk_scrolled_window_new (NULL
, NULL
);
290 gtk_box_pack_start (GTK_BOX (hbox
), scrolled_window
, TRUE
, TRUE
, 0);
291 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window
),
292 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
294 text_view
= gtk_text_view_new ();
295 gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view
), FALSE
);
296 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (text_view
), FALSE
);
298 gtk_container_add (GTK_CONTAINER (scrolled_window
), text_view
);
300 button_vbox
= gtk_vbox_new (FALSE
, 8);
301 gtk_box_pack_start (GTK_BOX (hbox
), button_vbox
, FALSE
, FALSE
, 0);
303 gtk_window_set_default_size (GTK_WINDOW (dialog
), 500, 300);
305 button
= align_button_new ("Insert");
306 gtk_box_pack_start (GTK_BOX (button_vbox
), button
, FALSE
, FALSE
, 0);
307 g_signal_connect (button
, "clicked", G_CALLBACK (on_insert
), NULL
);
309 button
= align_button_new ("Delete");
310 gtk_box_pack_start (GTK_BOX (button_vbox
), button
, FALSE
, FALSE
, 0);
311 g_signal_connect (button
, "clicked", G_CALLBACK (on_delete
), NULL
);
313 button
= align_button_new ("Reorder");
314 gtk_box_pack_start (GTK_BOX (button_vbox
), button
, FALSE
, FALSE
, 0);
315 g_signal_connect (button
, "clicked", G_CALLBACK (on_reorder
), NULL
);
317 button
= align_button_new ("Animate");
318 gtk_box_pack_start (GTK_BOX (button_vbox
), button
, FALSE
, FALSE
, 0);
319 g_signal_connect (button
, "clicked", G_CALLBACK (on_animate
), NULL
);
321 gtk_widget_show_all (dialog
);
322 gtk_dialog_run (GTK_DIALOG (dialog
));