How to set GTKListStore / GTKComboBox background in GTK2?

I use this code to create a combo box with a color background / text:

GtkListStore *liststore;
GtkWidget *combo;
GtkCellRenderer *column;
liststore = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
for(int i=0; i<10; i++) {
    gtk_list_store_insert_with_values(liststore, NULL, -1, 0, "Default", 1, "white", 2, "black", -1);
}
combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(liststore));
g_object_unref(liststore);
column = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), column, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), column, "text", 0, "foreground", 1, "background", 2, NULL);

and it works. It looks like this: enter image description here

My question is: how to set the background in the list or in the combo box so that there are no spaces in the image? Thanks!

+6
source share
1 answer

I use the Numix theme, so the "border" is red. You can use css to override theme styles:

GtkCssProvider *provider;

provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, "menuitem { background: #000; } menuitem:hover { background: #FFF; } .combo { background: #000; }", -1, NULL);
gtk_style_context_add_provider (
  GTK_STYLE_CONTEXT (gtk_widget_get_style_context (GTK_WIDGET (combo))),
  GTK_STYLE_PROVIDER (provider),
  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

gtk_style_context_add_provider_for_screen (gtk_widget_get_screen (combo),
                                           GTK_STYLE_PROVIDER (provider),
                                           GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref (provider);

Result: screenshot

And here is the full source code: https://pastebin.com/wDeUpb8A

Also check out the GtkInspector , which is a handy tool for such purposes.

+2

All Articles