I am trying to customize GtkCellRendererCombo so that the items in the
combo box have different colors.
Documentation indicates that this is supposed to be done through
"editing-started" signal handler, but I failed to do it.
My sample is given below.
When gtk_cell_layout_set_cell_data_func or gtk_cell_layout_add_attribute
call is being made, GTK issues error:
"gtk_combo_box_cell_layout_set_cell_data_func: assertion `info != NULL'
failed".
And the coloring doesn't work.
Any tips?
Regards,
Miroslav
PS. Code:
#include <gtk/gtk.h>
GtkWidget *window1;
GtkListStore *store;
void TaskStatusComboCellData(GtkCellLayout *cell_layout, GtkCellRenderer
*cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
{
//get item index
GtkTreePath *path = gtk_tree_model_get_path(tree_model, iter);
gint* arrIndices = gtk_tree_path_get_indices(path);
int nIdx = arrIndices[0];
gtk_tree_path_free(path);
//now set correct color for each item
switch (nIdx){
case 0:
g_object_set(G_OBJECT(cell), "foreground", "red", NULL);
break;
case 1:
g_object_set(G_OBJECT(cell), "foreground", "blue", NULL);
break;
}
}
void on_tree_cell_combo_editing_start(GtkCellRenderer *renderer,
GtkCellEditable *editable, gchar *path, gpointer user_data)
{
//set colors for combo box entries
if (GTK_IS_COMBO_BOX(editable))
{
//refresh latest colors
GtkComboBox *combo = GTK_COMBO_BOX(editable);
//TOFIX
gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo),
renderer, TaskStatusComboCellData, NULL, NULL);
//gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(combo),
renderer, "foreground", 2);
}
}
GtkWidget* create_main_win ()
{
GtkWidget *vbox1;
GtkWidget *scrolledwindow1;
GtkWidget *treeview4;
window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window1), "Test");
gtk_window_set_default_size (GTK_WINDOW (window1), 500, 350);
vbox1 = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox1);
gtk_container_add (GTK_CONTAINER (window1), vbox1);
scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolledwindow1);
gtk_container_add (GTK_CONTAINER (vbox1), scrolledwindow1);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW
(scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
//create tree view
store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
treeview4 = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
g_object_unref (G_OBJECT (store)); //tree now holds reference
gtk_widget_set_size_request(treeview4, -1, 150);
//create list view columns
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview4), TRUE);
gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(treeview4), TRUE);
gtk_tree_view_set_enable_search(GTK_TREE_VIEW(treeview4),FALSE);
gtk_widget_show (treeview4);
gtk_container_add (GTK_CONTAINER (scrolledwindow1), treeview4);
GtkTreeViewColumn *col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Title");
gtk_tree_view_column_set_clickable(col, TRUE); //allow column
header clicks
gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_resizable(col, TRUE);
gtk_tree_view_column_set_fixed_width(col, 100);
GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(col, (GtkCellRenderer *)renderer,
TRUE);
gtk_tree_view_column_add_attribute(col, renderer, "text", 0);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview4), col);
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Type");
gtk_tree_view_column_set_clickable(col, TRUE); //allow column
header clicks
gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_resizable(col, TRUE);
gtk_tree_view_column_set_fixed_width(col, 150);
renderer = gtk_cell_renderer_combo_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_add_attribute(col, renderer, "text", 1);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview4), col);
//add combo with values
GtkListStore *store1 = gtk_list_store_new(2, G_TYPE_STRING,
G_TYPE_STRING); //text, color
GtkTreeIter iter;
gtk_list_store_append(store1, &iter);
gtk_list_store_set(store1, &iter, 0, "Type 1 (red)", -1);
gtk_list_store_append(store1, &iter);
gtk_list_store_set(store1, &iter, 0, "Type 2 (blue)", -1);
gtk_list_store_append(store1, &iter);
gtk_list_store_set(store1, &iter, 0, "Type 3 (green)", -1);
g_object_set(renderer, "model", store1, "text-column", 0,
"has-entry", FALSE, "editable", TRUE, (char *)NULL);
g_signal_connect(renderer, "editing-started",
G_CALLBACK(on_tree_cell_combo_editing_start), NULL);
gtk_widget_show_all(window1);
g_signal_connect(window1, "destroy", G_CALLBACK(gtk_main_quit), NULL);
return window1;
}
void fill_sample_data()
{
GtkTreeIter iter;
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, "Item1", 1, "Item1 type", -1);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, "Item2", 1, "Item2 type", -1);
}
void GtkLogHandler(const gchar *log_domain, GLogLevelFlags log_level,
const gchar *message, gpointer user_data)
{
printf("GTK [%d]: %s\n", log_level, message);
}
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
//register logging
GLogLevelFlags logFlags = (GLogLevelFlags)(G_LOG_LEVEL_MASK); //all
g_log_set_handler(G_LOG_DOMAIN, logFlags, GtkLogHandler, NULL);
g_log_set_handler("Gtk", logFlags, GtkLogHandler, NULL);
g_log_set_handler("Gdk", logFlags, GtkLogHandler, NULL);
g_log_set_handler("GLib", logFlags, GtkLogHandler, NULL);
window1 = create_main_win ();
fill_sample_data();
gtk_main ();
return 0;
}
#ifdef _WIN32
#include <windows.h>
int APIENTRY WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return main( __argc, __argv );
}
#endif
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list