]> Pileus Git - ~andy/gtk/blobdiff - tests/testcombo.c
Updated Korean translation.
[~andy/gtk] / tests / testcombo.c
index f2b7c7401dc27f068132aacbde26bf881d30ce7d..9f9f1641cf444ca29cb516fb72e48492bd77737a 100644 (file)
@@ -1,3 +1,21 @@
+/* testcombo.c
+ * Copyright (C) 2003  Kristian Rietveld
+ *
+ * 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, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
 #include <config.h>
 #include <gtk/gtk.h>
 
@@ -44,10 +62,11 @@ create_color_pixbuf (const char *color)
 }
 
 static GtkWidget *
-create_combo_box_grid_demo ()
+create_combo_box_grid_demo (void)
 {
         GtkWidget *combo;
         GtkTreeIter iter;
+        GdkPixbuf *pixbuf;
         GtkCellRenderer *cell = gtk_cell_renderer_pixbuf_new ();
         GtkListStore *store;
 
@@ -62,52 +81,70 @@ create_combo_box_grid_demo ()
                                       3);
 
         /* first row */
+        pixbuf = create_color_pixbuf ("red");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("red"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
+        pixbuf = create_color_pixbuf ("green");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("green"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
+        pixbuf = create_color_pixbuf ("blue");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("blue"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
         /* second row */
+        pixbuf = create_color_pixbuf ("yellow");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("yellow"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
+        pixbuf = create_color_pixbuf ("black");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("black"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
+        pixbuf = create_color_pixbuf ("white");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("white"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
         /* third row */
+        pixbuf = create_color_pixbuf ("gray");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("gray"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
+        pixbuf = create_color_pixbuf ("snow");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("snow"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
+        pixbuf = create_color_pixbuf ("magenta");
         gtk_list_store_append (store, &iter);
         gtk_list_store_set (store, &iter,
-                            0, create_color_pixbuf ("magenta"),
+                            0, pixbuf,
                             -1);
+        g_object_unref (pixbuf);
 
         g_object_unref (store);
 
@@ -118,7 +155,7 @@ create_combo_box_grid_demo ()
 
 /* blaat */
 static GtkTreeModel *
-create_tree_blaat ()
+create_tree_blaat (void)
 {
         GdkPixbuf *pixbuf;
         GtkWidget *cellview;
@@ -189,7 +226,91 @@ create_tree_blaat ()
 }
 
 static GtkTreeModel *
-create_list_blaat ()
+create_empty_list_blaat (void)
+{
+        GdkPixbuf *pixbuf;
+        GtkWidget *cellview;
+        GtkTreeIter iter;
+        GtkListStore *store;
+
+        cellview = gtk_cell_view_new ();
+
+        store = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
+
+        pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_DIALOG_WARNING,
+                                         GTK_ICON_SIZE_BUTTON, NULL);
+        gtk_list_store_append (store, &iter);
+        gtk_list_store_set (store, &iter,
+                            0, pixbuf,
+                            1, "gtk-stock-dialog-warning",
+                            -1);
+
+        gtk_widget_destroy (cellview);
+
+        return GTK_TREE_MODEL (store);
+}
+
+static void
+populate_list_blaat (gpointer data)
+{
+  GtkComboBox *combo_box = GTK_COMBO_BOX (data);
+  GtkListStore *store;
+  GdkPixbuf *pixbuf;
+  GtkWidget *cellview;
+  GtkTreeIter iter;
+  
+  store = GTK_LIST_STORE (gtk_combo_box_get_model (combo_box));
+
+  gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
+
+  if (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter))
+    return;
+
+  cellview = gtk_cell_view_new ();
+  
+  pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_STOP,
+                                  GTK_ICON_SIZE_BUTTON, NULL);
+  gtk_list_store_append (store, &iter);                               
+  gtk_list_store_set (store, &iter,
+                     0, pixbuf,
+                     1, "gtk-stock-stop",
+                     -1);
+  
+  pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_NEW,
+                                  GTK_ICON_SIZE_BUTTON, NULL);
+  gtk_list_store_append (store, &iter);                               
+  gtk_list_store_set (store, &iter,
+                     0, pixbuf,
+                     1, "gtk-stock-new",
+                     -1);
+  
+  pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_CLEAR,
+                                  GTK_ICON_SIZE_BUTTON, NULL);
+  gtk_list_store_append (store, &iter);
+  gtk_list_store_set (store, &iter,
+                     0, pixbuf,
+                     1, "gtk-stock-clear",
+                     -1);
+  
+  gtk_list_store_append (store, &iter);
+  gtk_list_store_set (store, &iter,
+                     0, NULL,
+                     1, "separator",
+                     -1);
+  
+  pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_OPEN,
+                                  GTK_ICON_SIZE_BUTTON, NULL);
+  gtk_list_store_append (store, &iter);
+  gtk_list_store_set (store, &iter,
+                     0, pixbuf,
+                     1, "gtk-stock-open",
+                     -1);
+  
+  gtk_widget_destroy (cellview);  
+}
+
+static GtkTreeModel *
+create_list_blaat (void)
 {
         GdkPixbuf *pixbuf;
         GtkWidget *cellview;
@@ -253,7 +374,7 @@ create_list_blaat ()
 
 /* blaat */
 static GtkTreeModel *
-create_phylogenetic_tree ()
+create_phylogenetic_tree (void)
 {
         GtkTreeIter iter, iter2, iter3;
         GtkTreeStore *store;
@@ -506,7 +627,7 @@ create_phylogenetic_tree ()
 
 /* blaat */
 static GtkTreeModel *
-create_capital_tree ()
+create_capital_tree (void)
 {
         GtkTreeIter iter, iter2;
         GtkTreeStore *store;
@@ -875,6 +996,19 @@ is_separator (GtkTreeModel *model,
   
 }
 
+static void
+displayed_row_changed (GtkComboBox *combo,
+                       GtkCellView *cell)
+{
+  gint row;
+  GtkTreePath *path;
+
+  row = gtk_combo_box_get_active (combo);
+  path = gtk_tree_path_new_from_indices (row, -1);
+  gtk_cell_view_set_displayed_row (cell, path);
+  gtk_tree_path_free (path);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -884,9 +1018,9 @@ main (int argc, char **argv)
         GtkCellRenderer *renderer;
         GdkPixbuf *pixbuf;
         GtkTreeModel *model;
-        GValue value = {0, };
        GtkTreePath *path;
        GtkTreeIter iter;
+        GdkColor color;
        
         gtk_init (&argc, &argv);
 
@@ -916,25 +1050,13 @@ main (int argc, char **argv)
         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
                                     renderer,
                                     FALSE);
-        g_value_init (&value, GDK_TYPE_PIXBUF);
-        g_value_set_instance (&value, pixbuf);
-        gtk_cell_view_set_values (GTK_CELL_VIEW (cellview),
-                                  renderer,
-                                  "pixbuf", &value,
-                                  NULL);
-        g_value_unset (&value);
+        g_object_set (renderer, "pixbuf", pixbuf, NULL);
 
         renderer = gtk_cell_renderer_text_new ();
         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
                                     renderer,
                                     TRUE);
-        g_value_init (&value, G_TYPE_STRING);
-        g_value_set_string (&value, "la la la");
-        gtk_cell_view_set_values (GTK_CELL_VIEW (cellview),
-                                  renderer,
-                                  "text", &value,
-                                  NULL);
-        g_value_unset (&value);
+        g_object_set (renderer, "text", "la la la", NULL);
         gtk_container_add (GTK_CONTAINER (boom), cellview);
 
         /* GtkComboBox list */
@@ -979,6 +1101,112 @@ main (int argc, char **argv)
                                                
         gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
 
+        /* GtkComboBox dynamic list */
+        tmp = gtk_frame_new ("GtkComboBox (dynamic list)");
+        gtk_box_pack_start (GTK_BOX (mainbox), tmp, FALSE, FALSE, 0);
+
+        boom = gtk_vbox_new (FALSE, 0);
+        gtk_container_set_border_width (GTK_CONTAINER (boom), 5);
+        gtk_container_add (GTK_CONTAINER (tmp), boom);
+
+        model = create_empty_list_blaat ();
+        combobox = gtk_combo_box_new_with_model (model);
+       g_signal_connect (combobox, "popup-show", 
+                         G_CALLBACK (populate_list_blaat), combobox);
+
+       gtk_combo_box_set_add_tearoffs (GTK_COMBO_BOX (combobox), TRUE);
+        g_object_unref (model);
+        gtk_container_add (GTK_CONTAINER (boom), combobox);
+
+        renderer = gtk_cell_renderer_pixbuf_new ();
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox),
+                                    renderer,
+                                    FALSE);
+        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
+                                        "pixbuf", 0,
+                                        NULL);
+       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combobox),
+                                           renderer,
+                                           set_sensitive,
+                                           NULL, NULL);
+
+        renderer = gtk_cell_renderer_text_new ();
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox),
+                                    renderer,
+                                    TRUE);
+        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
+                                        "text", 1,
+                                        NULL);
+       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combobox),
+                                           renderer,
+                                           set_sensitive,
+                                           NULL, NULL);
+       gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combobox), 
+                                             is_separator, NULL, NULL);
+                                               
+        gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
+       gtk_combo_box_set_title (GTK_COMBO_BOX (combobox), "Dynamic list");
+
+        /* GtkComboBox custom entry */
+        tmp = gtk_frame_new ("GtkComboBox (custom)");
+        gtk_box_pack_start (GTK_BOX (mainbox), tmp, FALSE, FALSE, 0);
+
+        boom = gtk_vbox_new (FALSE, 0);
+        gtk_container_set_border_width (GTK_CONTAINER (boom), 5);
+        gtk_container_add (GTK_CONTAINER (tmp), boom);
+
+        model = create_list_blaat ();
+        combobox = gtk_combo_box_new_with_model (model);
+       gtk_combo_box_set_add_tearoffs (GTK_COMBO_BOX (combobox), TRUE);
+        g_object_unref (model);
+        gtk_container_add (GTK_CONTAINER (boom), combobox);
+
+        renderer = gtk_cell_renderer_pixbuf_new ();
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox),
+                                    renderer,
+                                    FALSE);
+        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
+                                        "pixbuf", 0,
+                                        NULL);
+       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combobox),
+                                           renderer,
+                                           set_sensitive,
+                                           NULL, NULL);
+
+        renderer = gtk_cell_renderer_text_new ();
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox),
+                                    renderer,
+                                    TRUE);
+        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
+                                        "text", 1,
+                                        NULL);
+       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combobox),
+                                           renderer,
+                                           set_sensitive,
+                                           NULL, NULL);
+       gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combobox), 
+                                             is_separator, NULL, NULL);
+                                               
+        gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
+
+        tmp = gtk_cell_view_new ();
+        gtk_widget_show (tmp);
+        gtk_cell_view_set_model (GTK_CELL_VIEW (tmp), model);
+
+        renderer = gtk_cell_renderer_text_new ();
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tmp), renderer, TRUE);
+        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (tmp), renderer,
+                                        "text", 1,
+                                        NULL);
+        color.red = 0xffff;
+        color.blue = 0xffff;
+        color.green = 0;
+        gtk_cell_view_set_background_color (GTK_CELL_VIEW (tmp), &color);
+        displayed_row_changed (GTK_COMBO_BOX (combobox), GTK_CELL_VIEW (tmp));
+        g_signal_connect (combobox, "changed", G_CALLBACK (displayed_row_changed), tmp); 
+           
+        gtk_container_add (GTK_CONTAINER (combobox), tmp);
+
         /* GtkComboBox tree */
         tmp = gtk_frame_new ("GtkComboBox (tree)");
         gtk_box_pack_start (GTK_BOX (mainbox), tmp, FALSE, FALSE, 0);
@@ -1083,7 +1311,7 @@ main (int argc, char **argv)
         gtk_container_add (GTK_CONTAINER (tmp), boom);
 
         model = create_capital_tree ();
-        combobox = gtk_combo_box_new_with_model (model);
+       combobox = gtk_combo_box_new_with_model (model);
        gtk_combo_box_set_add_tearoffs (GTK_COMBO_BOX (combobox), TRUE);
         g_object_unref (model);
         gtk_container_add (GTK_CONTAINER (boom), combobox);