]> Pileus Git - ~andy/gtk/blobdiff - tests/testentrycompletion.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testentrycompletion.c
index 0a07be9aa8c6264a12284304985c205001a99083..898b8d3bb18f9688950df4313e8399ec8cf54c44 100644 (file)
@@ -1,9 +1,28 @@
+/* testentrycompletion.c
+ * Copyright (C) 2004  Red Hat, Inc.
+ * Author: Matthias Clasen
+ *
+ * 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 "config.h"
 #include <stdlib.h>
 #include <string.h>
 #include <gtk/gtk.h>
 
+#include "prop-editor.h"
+
 /* Don't copy this bad example; inline RGB data is always a better
  * idea than inline XPMs.
  */
@@ -92,6 +111,12 @@ create_simple_completion_model (void)
   gtk_list_store_set (store, &iter, 0, "Totipalmi", -1);
   gtk_list_store_append (store, &iter);
   gtk_list_store_set (store, &iter, 0, "zombie", -1);
+  gtk_list_store_append (store, &iter);
+  gtk_list_store_set (store, &iter, 0, "a\303\246x", -1);
+  gtk_list_store_append (store, &iter);
+  gtk_list_store_set (store, &iter, 0, "a\303\246y", -1);
+  gtk_list_store_append (store, &iter);
+  gtk_list_store_set (store, &iter, 0, "a\303\246z", -1);
  
   return GTK_TREE_MODEL (store);
 }
@@ -201,26 +226,89 @@ animation_timer (GtkEntryCompletion *completion)
   GtkTreeIter iter;
   gint n_completions = G_N_ELEMENTS (dynamic_completions);
   gint n;
-
+  static GtkListStore *old_store = NULL;
   GtkListStore *store = GTK_LIST_STORE (gtk_entry_completion_get_model (completion));
 
-  if ((timer_count / n_completions) % 2 == 0)
+  if (timer_count % 10 == 0)
     {
-      n = timer_count % n_completions;
-      gtk_list_store_append (store, &iter);
-      gtk_list_store_set (store, &iter, 0, dynamic_completions[n], -1);
-      
+      if (!old_store)
+       {
+         g_print ("removing model!\n");
+
+         old_store = g_object_ref (gtk_entry_completion_get_model (completion));
+         gtk_entry_completion_set_model (completion, NULL);
+       }
+      else
+       {
+         g_print ("readding model!\n");
+         
+         gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (old_store));
+         g_object_unref (old_store);
+         old_store = NULL;
+       }
+
+      timer_count ++;
+      return TRUE;
     }
-  else
+
+  if (!old_store)
     {
-      gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
-      gtk_list_store_remove (store, &iter);
+      if ((timer_count / n_completions) % 2 == 0)
+       {
+         n = timer_count % n_completions;
+         gtk_list_store_append (store, &iter);
+         gtk_list_store_set (store, &iter, 0, dynamic_completions[n], -1);
+         
+       }
+      else
+       {
+         if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter))
+           gtk_list_store_remove (store, &iter);
+       }
     }
   
   timer_count++;
   return TRUE;
 }
 
+gboolean 
+match_selected_cb (GtkEntryCompletion *completion,
+                  GtkTreeModel       *model,
+                  GtkTreeIter        *iter)
+{
+  gchar *str;
+  GtkWidget *entry;
+
+  entry = gtk_entry_completion_get_entry (completion);
+  gtk_tree_model_get (GTK_TREE_MODEL (model), iter, 1, &str, -1);
+  gtk_entry_set_text (GTK_ENTRY (entry), str);
+  gtk_editable_set_position (GTK_EDITABLE (entry), -1);
+  g_free (str);
+
+  return TRUE;
+}
+
+static void
+new_prop_editor (GObject *object)
+{
+       gtk_widget_show (create_prop_editor (object, G_OBJECT_TYPE (object)));
+}
+
+static void
+add_with_prop_edit_button (GtkWidget *vbox, GtkWidget *entry, GtkEntryCompletion *completion)
+{
+       GtkWidget *hbox, *button;
+
+       hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
+       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
+
+       gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
+
+       button = gtk_button_new_with_label ("Properties");
+       g_signal_connect_swapped (button, "clicked", G_CALLBACK (new_prop_editor), completion);
+       gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
+}
+
 int 
 main (int argc, char *argv[])
 {
@@ -237,26 +325,29 @@ main (int argc, char *argv[])
   gtk_container_set_border_width (GTK_CONTAINER (window), 5);
   g_signal_connect (window, "delete_event", gtk_main_quit, NULL);
   
-  vbox = gtk_vbox_new (FALSE, 2);
+  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
   gtk_container_add (GTK_CONTAINER (window), vbox);
     
   gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
   
   label = gtk_label_new (NULL);
+
   gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
 
   /* Create our first entry */
   entry = gtk_entry_new ();
-  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
   
   /* Create the completion object */
   completion = gtk_entry_completion_new ();
+  gtk_entry_completion_set_inline_completion (completion, TRUE);
   
   /* Assign the completion to the entry */
   gtk_entry_set_completion (GTK_ENTRY (entry), completion);
   g_object_unref (completion);
   
+  add_with_prop_edit_button (vbox, entry, completion);
+
   /* Create a tree model and use it as the completion model */
   completion_model = create_simple_completion_model ();
   gtk_entry_completion_set_model (completion, completion_model);
@@ -267,7 +358,6 @@ main (int argc, char *argv[])
 
   /* Create our second entry */
   entry = gtk_entry_new ();
-  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
 
   /* Create the completion object */
   completion = gtk_entry_completion_new ();
@@ -276,6 +366,8 @@ main (int argc, char *argv[])
   gtk_entry_set_completion (GTK_ENTRY (entry), completion);
   g_object_unref (completion);
   
+  add_with_prop_edit_button (vbox, entry, completion);
+
   /* Create a tree model and use it as the completion model */
   completion_model = create_completion_model ();
   gtk_entry_completion_set_model (completion, completion_model);
@@ -294,14 +386,15 @@ main (int argc, char *argv[])
                                  "text", 1, NULL); 
   
   gtk_entry_completion_set_match_func (completion, match_func, NULL, NULL);
+  g_signal_connect (completion, "match-selected", 
+                   G_CALLBACK (match_selected_cb), NULL);
 
   gtk_entry_completion_insert_action_text (completion, 100, "action!");
   gtk_entry_completion_insert_action_text (completion, 101, "'nother action!");
-  g_signal_connect (completion, "action_activated", G_CALLBACK (activated_cb), 0);
+  g_signal_connect (completion, "action_activated", G_CALLBACK (activated_cb), NULL);
 
   /* Create our third entry */
   entry = gtk_entry_new ();
-  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
 
   /* Create the completion object */
   completion = gtk_entry_completion_new ();
@@ -310,8 +403,10 @@ main (int argc, char *argv[])
   gtk_entry_set_completion (GTK_ENTRY (entry), completion);
   g_object_unref (completion);
   
+  add_with_prop_edit_button (vbox, entry, completion);
+
   /* Create a tree model and use it as the completion model */
-  completion_model = gtk_list_store_new (1, G_TYPE_STRING);
+  completion_model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
 
   gtk_entry_completion_set_model (completion, completion_model);
   g_object_unref (completion_model);
@@ -320,7 +415,21 @@ main (int argc, char *argv[])
   gtk_entry_completion_set_text_column (completion, 0);
 
   /* Fill the completion dynamically */
-  g_timeout_add (1000, (GSourceFunc) animation_timer, completion);
+  gdk_threads_add_timeout (1000, (GSourceFunc) animation_timer, completion);
+
+  /* Fourth entry */
+  gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Model-less entry completion"), FALSE, FALSE, 0);
+
+  entry = gtk_entry_new ();
+
+  /* Create the completion object */
+  completion = gtk_entry_completion_new ();
+  
+  /* Assign the completion to the entry */
+  gtk_entry_set_completion (GTK_ENTRY (entry), completion);
+  g_object_unref (completion);
+  
+  add_with_prop_edit_button (vbox, entry, completion);
 
   gtk_widget_show_all (window);