testcombo.c 11.6 KB
Newer Older
1
#include <config.h>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <gtk/gtk.h>
#include <gtk/gtkcellview.h>

#include <string.h>
#include <stdio.h>

/**
 * oh yes, this test app surely has a lot of ugly code
 */

/* grid combo demo */
static GdkPixbuf *
create_color_pixbuf (const char *color)
{
        GdkPixbuf *pixbuf;
        GdkColor col;

        int x;
        int num;
        int rowstride;
        guchar *pixels, *p;

        if (!gdk_color_parse (color, &col))
                return NULL;

        pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
                                 FALSE, 8,
                                 16, 16);

        rowstride = gdk_pixbuf_get_rowstride (pixbuf);
        p = pixels = gdk_pixbuf_get_pixels (pixbuf);

        num = gdk_pixbuf_get_width (pixbuf) *
                gdk_pixbuf_get_height (pixbuf);

        for (x = 0; x < num; x++) {
                p[0] = col.red / 65535 * 255;
                p[1] = col.green / 65535 * 255;
                p[2] = col.blue / 65535 * 255;
                p += 3;
        }

        return pixbuf;
}

static GtkWidget *
create_combo_box_grid_demo ()
{
        GtkWidget *combo;
        GtkTreeIter iter;
        GtkCellRenderer *cell = gtk_cell_renderer_pixbuf_new ();
        GtkListStore *store;

        store = gtk_list_store_new (1, GDK_TYPE_PIXBUF);

Kristian Rietveld's avatar
Kristian Rietveld committed
57
        combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
58 59 60 61
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo),
                                    cell, TRUE);
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
                                        cell, "pixbuf", 0, NULL);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        gtk_combo_box_set_wrap_width (GTK_COMBO_BOX (combo),
                                      3);

        /* first row */
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("red"),
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("green"),
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("blue"),
                            -1);

        /* second row */
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("yellow"),
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("black"),
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("white"),
                            -1);

        /* third row */
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("gray"),
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("snow"),
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, create_color_pixbuf ("magenta"),
                            -1);

        gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);

        return combo;
}

/* blaat */
static GtkTreeModel *
create_blaat ()
{
        GdkPixbuf *pixbuf;
        GtkWidget *cellview;
        GtkTreeIter iter;
        GtkListStore *store;

        cellview = gtk_cell_view_new ();

129
        store = gtk_list_store_new (3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN);
130 131 132 133 134 135 136

        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",
137
			    2, FALSE,
138 139 140 141 142 143 144 145
                            -1);

        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",
146
			    2, FALSE,
147 148 149 150 151 152 153 154
                            -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",
155
			    2, FALSE,
156 157 158 159 160 161 162 163
                            -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",
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
			    2, FALSE,
                            -1);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, NULL,
                            1, "separator",
			    2, TRUE,
                            -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",
			    2, FALSE,
181 182 183 184 185 186 187
                            -1);

        gtk_widget_destroy (cellview);

        return GTK_TREE_MODEL (store);
}

188 189
static void
setup_combo_entry (GtkWidget *entry_box)
190
{
191 192 193 194 195 196 197 198 199 200 201 202
	gtk_combo_box_append_text (GTK_COMBO_BOX (entry_box),
				   "dum de dum");
	gtk_combo_box_append_text (GTK_COMBO_BOX (entry_box),
				   "la la la");
	gtk_combo_box_append_text (GTK_COMBO_BOX (entry_box),
				   "la la la dum de dum la la la la la la boom de da la la");
	gtk_combo_box_append_text (GTK_COMBO_BOX (entry_box),
				   "bloop");
	gtk_combo_box_append_text (GTK_COMBO_BOX (entry_box),
				   "bleep");
	gtk_combo_box_append_text (GTK_COMBO_BOX (entry_box),
				   "klaas");
203 204
}

205 206 207 208 209 210 211 212 213 214 215 216 217
static void
set_sensitive (GtkCellLayout   *cell_layout,
	       GtkCellRenderer *cell,
	       GtkTreeModel    *tree_model,
	       GtkTreeIter     *iter,
	       gpointer         data)
{
  GtkTreePath *path;
  gint *indices;
  gboolean sensitive;

  path = gtk_tree_model_get_path (tree_model, iter);
  indices = gtk_tree_path_get_indices (path);
218
  sensitive = indices[0] != 1;
219 220 221 222 223
  gtk_tree_path_free (path);

  g_object_set (cell, "sensitive", sensitive, NULL);
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
int
main (int argc, char **argv)
{
        GtkWidget *window, *cellview, *mainbox;
        GtkWidget *combobox, *comboboxtext, *comboboxgrid;
        GtkWidget *tmp, *boom;
        GtkCellRenderer *renderer;
        GdkPixbuf *pixbuf;
        GValue value = {0, };

        gtk_init (&argc, &argv);

        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width (GTK_CONTAINER (window), 5);
        g_signal_connect (window, "delete_event", gtk_main_quit, NULL);

        mainbox = gtk_vbox_new (FALSE, 2);
        gtk_container_add (GTK_CONTAINER (window), mainbox);


        /* GtkCellView */
        tmp = gtk_frame_new ("GtkCellView");
        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);

        cellview = gtk_cell_view_new ();
        renderer = gtk_cell_renderer_pixbuf_new ();
        pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_DIALOG_WARNING,
                                         GTK_ICON_SIZE_BUTTON, NULL);

257 258 259
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
                                    renderer,
                                    FALSE);
260 261 262 263 264 265 266 267 268
        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);

        renderer = gtk_cell_renderer_text_new ();
269 270 271
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
                                    renderer,
                                    TRUE);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        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);
        gtk_container_add (GTK_CONTAINER (boom), cellview);


        /* GtkComboBox */
        tmp = gtk_frame_new ("GtkComboBox");
        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);

Kristian Rietveld's avatar
Kristian Rietveld committed
290
        combobox = gtk_combo_box_new_with_model (create_blaat ());
291 292 293
        gtk_container_add (GTK_CONTAINER (boom), combobox);

        renderer = gtk_cell_renderer_pixbuf_new ();
294 295 296 297 298 299
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox),
                                    renderer,
                                    FALSE);
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
                                        "pixbuf", 0,
                                        NULL);
300 301 302 303
	gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combobox),
					    renderer,
					    set_sensitive,
					    NULL, NULL);
304 305

        renderer = gtk_cell_renderer_text_new ();
306 307 308 309 310 311
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox),
                                    renderer,
                                    TRUE);
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
                                        "text", 1,
                                        NULL);
312 313 314 315
	gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combobox),
					    renderer,
					    set_sensitive,
					    NULL, NULL);
316 317 318
	gtk_combo_box_set_row_separator_column (GTK_COMBO_BOX (combobox), 2);
						
        gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340


        /* GtkComboBox (grid mode) */
        tmp = gtk_frame_new ("GtkComboBox (grid mode)");
        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);

        comboboxgrid = create_combo_box_grid_demo ();
        gtk_box_pack_start (GTK_BOX (boom), comboboxgrid, FALSE, FALSE, 0);


        /* GtkComboBoxEntry */
        tmp = gtk_frame_new ("GtkComboBoxEntry");
        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);

341 342
        comboboxtext = gtk_combo_box_entry_new_text ();
	setup_combo_entry (comboboxtext);
343 344 345 346 347 348 349 350 351 352 353 354
        gtk_container_add (GTK_CONTAINER (boom), comboboxtext);

        /* done */
        gtk_widget_show_all (window);

        gtk_main ();

        return 0;
}

/* vim:expandtab
 */