9

can someone type up a simple example for styling a GTK+ widget with css? I could not figure out how to do it looking at the docs:

#include <gtk/gtk.h>
int main(int argc,char *argv[])
{
    gtk_init(&argc,&argv);
    GtkWidget *window;
    GtkWidget *button;
    GtkCssProvider *cssProvider;

    gtk_css_provider_load_from_path(cssProvider,"./gtkExample2.css",NULL);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label("GTK Button");

    gtk_style_context_add_provider(gtk_widget_get_style_context(window),cssProvider,GTK_STYLE_PROVIDER_PRIORITY_USER);

    g_signal_connect_swapped(window,"delete-event",G_CALLBACK(gtk_widget_destroy),window);

    gtk_container_set_border_width(GTK_CONTAINER(window),20);
    gtk_container_add(GTK_CONTAINER(window),button);

    gtk_widget_show(window);
    gtk_widget_show(button);

    gtk_main();
    return 1;
}
marcio
  • 10,002
  • 11
  • 54
  • 83
user1502482
  • 139
  • 1
  • 1
  • 4
  • I've been making attempts, I'll add what I have currently – user1502482 Jan 19 '13 at 03:35
  • What problem(s) are you having with that code? – Andrew Barber Jan 19 '13 at 03:38
  • Compiling wise, argument 2 passed into gtk_style_context_add_provider is not correct. It requires GtkStyleProvider and i provided GtkCssProvider and I have no idea how to get a GtkStyleProvider – user1502482 Jan 19 '13 at 03:47
  • What does the documentation tell you? –  Jan 19 '13 at 03:48
  • That's another thing, this is my first day working with GTK and reading the docs. It's hard to follow them and I've been reading and looking for hours now – user1502482 Jan 19 '13 at 03:51
  • 4
    I have the exact same question, and have no idea why this question was closed :/. I'd still very much like it to be answered (and could have used the answer if it had not been closed). – Carlo Wood Mar 29 '15 at 01:25
  • Here's a similar question that has a working example: https://stackoverflow.com/questions/30791670/how-to-style-a-gtklabel-with-css – gsgx Jun 13 '15 at 02:01
  • 1
    You're passing an uninitialized pointer (cssProvider) to gtk_css_provider_load_from_path. You should create a cssProvider first: cssProvider = gtk_css_provider_new(); – LRMAAX Nov 14 '15 at 05:11

0 Answers0