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!

1 Answers1

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.

AndreLDM
  • 2,117
  • 1
  • 18
  • 27
  • 1
    Doesn't this only work for GTK3? Apologies if I'm misunderstanding something. –  Jun 05 '17 at 17:53
  • Please make it clear in the question that you're using a deprecated version of GTK, just a tag is easy to overlook. – AndreLDM Jun 05 '17 at 18:35
  • 1
    I tagged gtk2 in the question, but I'll add it to the body as well. –  Jun 05 '17 at 18:36
  • 3
    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. – José Fonte Jun 06 '17 at 00:03
  • 1
    @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. –  Jun 06 '17 at 15:11
  • 1
    @user7554160 Any reason to stick to gtk2? Check this links http://www.orford.org/gtk/ *** https://wiki.gnome.org/Attic/GnomeArt/Tutorials/GtkThemes *** https://developer.gnome.org/gtk3/stable/gtk-migrating-GtkStyleContext.html – José Fonte Jun 06 '17 at 15:24
  • @JoséFonte requirement for my school, I have no clue why!! Thanks for the links, I'll update my question when I understand more :) –  Jun 06 '17 at 15:29