How can I set the background of a GTKListStore/GTKComboBox in GTK2?
Asked Answered
W

1

6

I'm using this code to create a combo box with colored 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 can I set the background of the liststore or the combo box so that there is no whitespace as seen in the picture? Thanks!

Wiggle answered 5/6, 2017 at 15:14 Comment(0)
A
2

I'm using 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);

Here's result: screenshot

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

Also take a look at GtkInspector, it's a handy tool for such purposes.

Avestan answered 5/6, 2017 at 17:41 Comment(7)
Doesn't this only work for GTK3? Apologies if I'm misunderstanding something.Wiggle
Please make it clear in the question that you're using a deprecated version of GTK, just a tag is easy to overlook.Avestan
I tagged gtk2 in the question, but I'll add it to the body as well.Wiggle
I upvoted the question as the answer still relevant for Gtk users. Gtk 2 does not use CSS but it's deprecated and newly written code should use Gtk 3. Gtk 2 uses rc files to set styles and also some functions and compiled theme engines. Thanks for effort @AndreLDM.Chicanery
@JoséFonte do you know of a good tutorial/explanation of rc files? I just posted a question because even a simple example isn't working for me. I am also on gtk2.Necolenecro
@Necolenecro Any reason to stick to gtk2? Check this links orford.org/gtk *** wiki.gnome.org/Attic/GnomeArt/Tutorials/GtkThemes *** developer.gnome.org/gtk3/stable/…Chicanery
@JoséFonte requirement for my school, I have no clue why!! Thanks for the links, I'll update my question when I understand more :)Necolenecro

© 2022 - 2024 — McMap. All rights reserved.