]> Pileus Git - ~andy/gtk/blobdiff - tests/testcombochange.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testcombochange.c
index 2b94b3062fb37b1cc7bd93d3a6dd34a62be7461f..6f254fe4a284b7e75adbe3ab9776610826db167e 100644 (file)
@@ -1,3 +1,22 @@
+/* testcombochange.c
+ * Copyright (C) 2004  Red Hat, Inc.
+ * Author: Owen Taylor
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
 #include <gtk/gtk.h>
 #include <stdarg.h>
 
@@ -8,18 +27,14 @@ GArray *contents;
 static char next_value = 'A';
 
 static void
-test_init ()
+test_init (void)
 {
-  if (g_file_test ("../gdk-pixbuf/libpixbufloader-pnm.la",
-                  G_FILE_TEST_EXISTS))
-    {
-      g_setenv ("GDK_PIXBUF_MODULE_FILE", "../gdk-pixbuf/gdk-pixbuf.loaders", TRUE);
-      g_setenv ("GTK_IM_MODULE_FILE", "../modules/input/gtk.immodules", TRUE);
-    }
+  if (g_file_test ("../modules/input/immodules.cache", G_FILE_TEST_EXISTS))
+    g_setenv ("GTK_IM_MODULE_FILE", "../modules/input/immodules.cache", TRUE);
 }
 
 static void
-log (const char *fmt,
+combochange_log (const char *fmt,
      ...)
 {
   GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
@@ -73,16 +88,9 @@ create_combo (const char *name,
 {
   GtkCellRenderer *cell_renderer;
   GtkWidget *combo;
-  char *rc_string;
-  
-  rc_string = g_strdup_printf ("style \"%s-style\" {\n"
-                              "  GtkComboBox::appears-as-list = %d\n"
-                              "}\n"
-                              "\n"
-                              "widget \"*.%s\" style \"%s-style\"",
-                              name, is_list, name, name);
-  gtk_rc_parse_string (rc_string);
-  g_free (rc_string);
+  GtkCssProvider *provider;
+  GtkStyleContext *context;
+  gchar *css_data;
 
   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model));
   cell_renderer = gtk_cell_renderer_text_new ();
@@ -91,7 +99,19 @@ create_combo (const char *name,
                                  "text", 0, NULL);
 
   gtk_widget_set_name (combo, name);
-  
+
+  context = gtk_widget_get_style_context (combo);
+
+  provider = gtk_css_provider_new ();
+  css_data = g_strdup_printf ("#%s { -GtkComboBox-appears-as-list: %s }",
+                              name, is_list ? "true" : "false");
+  gtk_css_provider_load_from_data (provider, css_data, -1, NULL);
+  g_free (css_data);
+
+  gtk_style_context_add_provider (context,
+                                  GTK_STYLE_PROVIDER (provider),
+                                  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+
   return combo;
 }
 
@@ -119,7 +139,7 @@ on_insert (void)
 
   g_array_insert_val (contents, insert_pos, new_value);
 
-  log ("Inserted '%c' at position %d", new_value[0], insert_pos);
+  combochange_log ("Inserted '%c' at position %d", new_value[0], insert_pos);
 }
 
 static void
@@ -140,25 +160,26 @@ on_delete (void)
 
   old_val = g_array_index (contents, char, delete_pos);
   g_array_remove_index (contents, delete_pos);
-  log ("Deleted '%c' from position %d", old_val, delete_pos);
+  combochange_log ("Deleted '%c' from position %d", old_val, delete_pos);
 }
 
 static void
 on_reorder (void)
 {
-  GArray *new_contents = g_array_new (FALSE, FALSE, sizeof (char));
-  gint *shuffle_array = g_new (int, contents->len);
-  gint *shuffle_array_inverse = g_new (int, contents->len);
+  GArray *new_contents;
+  gint *shuffle_array;
   gint i;
 
+  shuffle_array = g_new (int, contents->len);
+  
   for (i = 0; i < contents->len; i++)
     shuffle_array[i] = i;
 
-  for (i = 0; i < contents->len - 1; i++)
+  for (i = 0; i + 1 < contents->len; i++)
     {
       gint pos = g_random_int_range (i, contents->len);
       gint tmp;
-      
+
       tmp = shuffle_array[i];
       shuffle_array[i] = shuffle_array[pos];
       shuffle_array[pos] = tmp;
@@ -166,24 +187,53 @@ on_reorder (void)
 
   gtk_list_store_reorder (model, shuffle_array);
 
-  for (i = 0; i < contents->len; i++)
-    shuffle_array_inverse[shuffle_array[i]] = i;
-  
+  new_contents = g_array_new (FALSE, FALSE, sizeof (char));
   for (i = 0; i < contents->len; i++)
     g_array_append_val (new_contents,
-                       g_array_index (contents, char, shuffle_array_inverse[i]));
+                       g_array_index (contents, char, shuffle_array[i]));
   g_array_free (contents, TRUE);
   contents = new_contents;
 
-  log ("Reordered array");
+  combochange_log ("Reordered array");
     
   g_free (shuffle_array);
-  g_free (shuffle_array_inverse);
+}
+
+static int n_animations = 0;
+static int timer = 0;
+
+static gint
+animation_timer (gpointer data)
+{
+  switch (g_random_int_range (0, 3)) 
+    {
+    case 0: 
+      on_insert ();
+      break;
+    case 1:
+      on_delete ();
+      break;
+    case 2:
+      on_reorder ();
+      break;
+    }
+
+  n_animations--;
+  return n_animations > 0;
+}
+
+static void
+on_animate (void)
+{
+  n_animations += 20;
+  timer = gdk_threads_add_timeout (1000, (GSourceFunc) animation_timer, NULL);
 }
 
 int
 main (int argc, char **argv)
 {
+  GtkWidget *content_area;
   GtkWidget *dialog;
   GtkWidget *scrolled_window;
   GtkWidget *hbox;
@@ -203,15 +253,17 @@ main (int argc, char **argv)
   contents = g_array_new (FALSE, FALSE, sizeof (char));
   
   dialog = gtk_dialog_new_with_buttons ("GtkComboBox model changes",
-                                       NULL, GTK_DIALOG_NO_SEPARATOR,
+                                       NULL, 0,
                                        GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
                                        NULL);
-  
-  hbox = gtk_hbox_new (FALSE, 12);
+
+  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
+
+  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
   gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
-  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, TRUE, TRUE, 0);
+  gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
 
-  combo_vbox = gtk_vbox_new (FALSE, 8);
+  combo_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
   gtk_box_pack_start (GTK_BOX (hbox), combo_vbox, FALSE, FALSE, 0);
 
   label = gtk_label_new (NULL);
@@ -247,7 +299,7 @@ main (int argc, char **argv)
 
   gtk_container_add (GTK_CONTAINER (scrolled_window), text_view);
 
-  button_vbox = gtk_vbox_new (FALSE, 8);
+  button_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
   gtk_box_pack_start (GTK_BOX (hbox), button_vbox, FALSE, FALSE, 0);
   
   gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 300);
@@ -264,6 +316,10 @@ main (int argc, char **argv)
   gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
   g_signal_connect (button, "clicked", G_CALLBACK (on_reorder), NULL);
 
+  button = align_button_new ("Animate");
+  gtk_box_pack_start (GTK_BOX (button_vbox), button, FALSE, FALSE, 0);
+  g_signal_connect (button, "clicked", G_CALLBACK (on_animate), NULL);
+
   gtk_widget_show_all (dialog);
   gtk_dialog_run (GTK_DIALOG (dialog));