gtk/tests/testcombo.c

360 lines
12 KiB
C
Raw Normal View History

#include <config.h>
#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);
2003-11-16 23:20:23 +00:00
combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo),
cell, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
cell, "pixbuf", 0, NULL);
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);
g_object_unref (store);
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 ();
Support separators in combo boxes and more generally in tree views 2004-07-07 Matthias Clasen <mclasen@redhat.com> Support separators in combo boxes and more generally in tree views (#135873): * gtk/gtkcombobox.h: * gtk/gtkcombobox.c (gtk_combo_box_get_row_separator_column): * gtk/gtkcombobox.c (gtk_combo_box_set_row_separator_column): Add a ::row-separator-column property with getter and setter, which can indicate a boolean model column to determine which rows are separators. * gtk/gtkcombobox.c: Display separator rows as separator menu items in menu mode, and by using the new treeview separator functionality in list mode. * gtk/gtktreeview.h: * gtk/gtktreeview.c (gtk_tree_view_get_row_separator_func): * gtk/gtktreeview.c (gtk_tree_view_set_row_separator_func): Add a callback to determine whether a row is a separator. * gtk/gtktreeview.c (gtk_tree_view_bin_expose): * gtk/gtktreeview.c (gtk_tree_view_create_row_drag_icon): * gtk/gtktreeview.c (validate_row): Use the new callback to determine whether a row is a separator, and draw it as a separator then. Since separators should take up less vertical space than regular rows, this requires removing the redundant MAX(...,expander_size) calls which appear in many places. Instead, the MAX() is now only done in validate_row(), and only if the row is not a separator. To catch possible side effects of this intrusive change, I have left EXPANDER_MAX() calls in place of the MAX() calls which will emit a warning if something breaks. They should be removed before 2.6. * gtk/gtktreeselection.c (row_is_selectable): Don't let separator rows be selected. * tests/testcombo.c (create_blaat): Add a separator column.
2004-07-07 15:15:35 +00:00
store = gtk_list_store_new (3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN);
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",
Support separators in combo boxes and more generally in tree views 2004-07-07 Matthias Clasen <mclasen@redhat.com> Support separators in combo boxes and more generally in tree views (#135873): * gtk/gtkcombobox.h: * gtk/gtkcombobox.c (gtk_combo_box_get_row_separator_column): * gtk/gtkcombobox.c (gtk_combo_box_set_row_separator_column): Add a ::row-separator-column property with getter and setter, which can indicate a boolean model column to determine which rows are separators. * gtk/gtkcombobox.c: Display separator rows as separator menu items in menu mode, and by using the new treeview separator functionality in list mode. * gtk/gtktreeview.h: * gtk/gtktreeview.c (gtk_tree_view_get_row_separator_func): * gtk/gtktreeview.c (gtk_tree_view_set_row_separator_func): Add a callback to determine whether a row is a separator. * gtk/gtktreeview.c (gtk_tree_view_bin_expose): * gtk/gtktreeview.c (gtk_tree_view_create_row_drag_icon): * gtk/gtktreeview.c (validate_row): Use the new callback to determine whether a row is a separator, and draw it as a separator then. Since separators should take up less vertical space than regular rows, this requires removing the redundant MAX(...,expander_size) calls which appear in many places. Instead, the MAX() is now only done in validate_row(), and only if the row is not a separator. To catch possible side effects of this intrusive change, I have left EXPANDER_MAX() calls in place of the MAX() calls which will emit a warning if something breaks. They should be removed before 2.6. * gtk/gtktreeselection.c (row_is_selectable): Don't let separator rows be selected. * tests/testcombo.c (create_blaat): Add a separator column.
2004-07-07 15:15:35 +00:00
2, FALSE,
-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",
Support separators in combo boxes and more generally in tree views 2004-07-07 Matthias Clasen <mclasen@redhat.com> Support separators in combo boxes and more generally in tree views (#135873): * gtk/gtkcombobox.h: * gtk/gtkcombobox.c (gtk_combo_box_get_row_separator_column): * gtk/gtkcombobox.c (gtk_combo_box_set_row_separator_column): Add a ::row-separator-column property with getter and setter, which can indicate a boolean model column to determine which rows are separators. * gtk/gtkcombobox.c: Display separator rows as separator menu items in menu mode, and by using the new treeview separator functionality in list mode. * gtk/gtktreeview.h: * gtk/gtktreeview.c (gtk_tree_view_get_row_separator_func): * gtk/gtktreeview.c (gtk_tree_view_set_row_separator_func): Add a callback to determine whether a row is a separator. * gtk/gtktreeview.c (gtk_tree_view_bin_expose): * gtk/gtktreeview.c (gtk_tree_view_create_row_drag_icon): * gtk/gtktreeview.c (validate_row): Use the new callback to determine whether a row is a separator, and draw it as a separator then. Since separators should take up less vertical space than regular rows, this requires removing the redundant MAX(...,expander_size) calls which appear in many places. Instead, the MAX() is now only done in validate_row(), and only if the row is not a separator. To catch possible side effects of this intrusive change, I have left EXPANDER_MAX() calls in place of the MAX() calls which will emit a warning if something breaks. They should be removed before 2.6. * gtk/gtktreeselection.c (row_is_selectable): Don't let separator rows be selected. * tests/testcombo.c (create_blaat): Add a separator column.
2004-07-07 15:15:35 +00:00
2, FALSE,
-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",
Support separators in combo boxes and more generally in tree views 2004-07-07 Matthias Clasen <mclasen@redhat.com> Support separators in combo boxes and more generally in tree views (#135873): * gtk/gtkcombobox.h: * gtk/gtkcombobox.c (gtk_combo_box_get_row_separator_column): * gtk/gtkcombobox.c (gtk_combo_box_set_row_separator_column): Add a ::row-separator-column property with getter and setter, which can indicate a boolean model column to determine which rows are separators. * gtk/gtkcombobox.c: Display separator rows as separator menu items in menu mode, and by using the new treeview separator functionality in list mode. * gtk/gtktreeview.h: * gtk/gtktreeview.c (gtk_tree_view_get_row_separator_func): * gtk/gtktreeview.c (gtk_tree_view_set_row_separator_func): Add a callback to determine whether a row is a separator. * gtk/gtktreeview.c (gtk_tree_view_bin_expose): * gtk/gtktreeview.c (gtk_tree_view_create_row_drag_icon): * gtk/gtktreeview.c (validate_row): Use the new callback to determine whether a row is a separator, and draw it as a separator then. Since separators should take up less vertical space than regular rows, this requires removing the redundant MAX(...,expander_size) calls which appear in many places. Instead, the MAX() is now only done in validate_row(), and only if the row is not a separator. To catch possible side effects of this intrusive change, I have left EXPANDER_MAX() calls in place of the MAX() calls which will emit a warning if something breaks. They should be removed before 2.6. * gtk/gtktreeselection.c (row_is_selectable): Don't let separator rows be selected. * tests/testcombo.c (create_blaat): Add a separator column.
2004-07-07 15:15:35 +00:00
2, FALSE,
-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",
Support separators in combo boxes and more generally in tree views 2004-07-07 Matthias Clasen <mclasen@redhat.com> Support separators in combo boxes and more generally in tree views (#135873): * gtk/gtkcombobox.h: * gtk/gtkcombobox.c (gtk_combo_box_get_row_separator_column): * gtk/gtkcombobox.c (gtk_combo_box_set_row_separator_column): Add a ::row-separator-column property with getter and setter, which can indicate a boolean model column to determine which rows are separators. * gtk/gtkcombobox.c: Display separator rows as separator menu items in menu mode, and by using the new treeview separator functionality in list mode. * gtk/gtktreeview.h: * gtk/gtktreeview.c (gtk_tree_view_get_row_separator_func): * gtk/gtktreeview.c (gtk_tree_view_set_row_separator_func): Add a callback to determine whether a row is a separator. * gtk/gtktreeview.c (gtk_tree_view_bin_expose): * gtk/gtktreeview.c (gtk_tree_view_create_row_drag_icon): * gtk/gtktreeview.c (validate_row): Use the new callback to determine whether a row is a separator, and draw it as a separator then. Since separators should take up less vertical space than regular rows, this requires removing the redundant MAX(...,expander_size) calls which appear in many places. Instead, the MAX() is now only done in validate_row(), and only if the row is not a separator. To catch possible side effects of this intrusive change, I have left EXPANDER_MAX() calls in place of the MAX() calls which will emit a warning if something breaks. They should be removed before 2.6. * gtk/gtktreeselection.c (row_is_selectable): Don't let separator rows be selected. * tests/testcombo.c (create_blaat): Add a separator column.
2004-07-07 15:15:35 +00:00
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,
-1);
gtk_widget_destroy (cellview);
return GTK_TREE_MODEL (store);
}
static void
setup_combo_entry (GtkWidget *entry_box)
{
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");
}
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);
Support separators in combo boxes and more generally in tree views 2004-07-07 Matthias Clasen <mclasen@redhat.com> Support separators in combo boxes and more generally in tree views (#135873): * gtk/gtkcombobox.h: * gtk/gtkcombobox.c (gtk_combo_box_get_row_separator_column): * gtk/gtkcombobox.c (gtk_combo_box_set_row_separator_column): Add a ::row-separator-column property with getter and setter, which can indicate a boolean model column to determine which rows are separators. * gtk/gtkcombobox.c: Display separator rows as separator menu items in menu mode, and by using the new treeview separator functionality in list mode. * gtk/gtktreeview.h: * gtk/gtktreeview.c (gtk_tree_view_get_row_separator_func): * gtk/gtktreeview.c (gtk_tree_view_set_row_separator_func): Add a callback to determine whether a row is a separator. * gtk/gtktreeview.c (gtk_tree_view_bin_expose): * gtk/gtktreeview.c (gtk_tree_view_create_row_drag_icon): * gtk/gtktreeview.c (validate_row): Use the new callback to determine whether a row is a separator, and draw it as a separator then. Since separators should take up less vertical space than regular rows, this requires removing the redundant MAX(...,expander_size) calls which appear in many places. Instead, the MAX() is now only done in validate_row(), and only if the row is not a separator. To catch possible side effects of this intrusive change, I have left EXPANDER_MAX() calls in place of the MAX() calls which will emit a warning if something breaks. They should be removed before 2.6. * gtk/gtktreeselection.c (row_is_selectable): Don't let separator rows be selected. * tests/testcombo.c (create_blaat): Add a separator column.
2004-07-07 15:15:35 +00:00
sensitive = indices[0] != 1;
gtk_tree_path_free (path);
g_object_set (cell, "sensitive", sensitive, NULL);
}
int
main (int argc, char **argv)
{
GtkWidget *window, *cellview, *mainbox;
GtkWidget *combobox, *comboboxtext, *comboboxgrid;
GtkWidget *tmp, *boom;
GtkCellRenderer *renderer;
GdkPixbuf *pixbuf;
GtkTreeModel *model;
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, "destroy", 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);
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);
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);
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);
model = create_blaat ();
combobox = gtk_combo_box_new_with_model (model);
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);
Support separators in combo boxes and more generally in tree views 2004-07-07 Matthias Clasen <mclasen@redhat.com> Support separators in combo boxes and more generally in tree views (#135873): * gtk/gtkcombobox.h: * gtk/gtkcombobox.c (gtk_combo_box_get_row_separator_column): * gtk/gtkcombobox.c (gtk_combo_box_set_row_separator_column): Add a ::row-separator-column property with getter and setter, which can indicate a boolean model column to determine which rows are separators. * gtk/gtkcombobox.c: Display separator rows as separator menu items in menu mode, and by using the new treeview separator functionality in list mode. * gtk/gtktreeview.h: * gtk/gtktreeview.c (gtk_tree_view_get_row_separator_func): * gtk/gtktreeview.c (gtk_tree_view_set_row_separator_func): Add a callback to determine whether a row is a separator. * gtk/gtktreeview.c (gtk_tree_view_bin_expose): * gtk/gtktreeview.c (gtk_tree_view_create_row_drag_icon): * gtk/gtktreeview.c (validate_row): Use the new callback to determine whether a row is a separator, and draw it as a separator then. Since separators should take up less vertical space than regular rows, this requires removing the redundant MAX(...,expander_size) calls which appear in many places. Instead, the MAX() is now only done in validate_row(), and only if the row is not a separator. To catch possible side effects of this intrusive change, I have left EXPANDER_MAX() calls in place of the MAX() calls which will emit a warning if something breaks. They should be removed before 2.6. * gtk/gtktreeselection.c (row_is_selectable): Don't let separator rows be selected. * tests/testcombo.c (create_blaat): Add a separator column.
2004-07-07 15:15:35 +00:00
gtk_combo_box_set_row_separator_column (GTK_COMBO_BOX (combobox), 2);
gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
/* 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);
comboboxtext = gtk_combo_box_entry_new_text ();
setup_combo_entry (comboboxtext);
gtk_container_add (GTK_CONTAINER (boom), comboboxtext);
/* done */
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
/* vim:expandtab
*/