Re: Overriding CSS style
- From: cecashon aol com
- To: zaghp outlook fr, gtk-app-devel-list gnome org
- Subject: Re: Overriding CSS style
- Date: Thu, 19 Apr 2018 17:06:14 -0400
Hi Yannick,
You have some options here. You can set a priority with
https://developer.gnome.org/gtk3/stable/GtkStyleContext.html#gtk-style-context-add-provider
with CSS.
If you want to just stick with drawing in C, connect your "draw" handler for the window. If you need a pixbuf
you can use
https://developer.gnome.org/gdk3/stable/gdk3-Cairo-Interaction.html#gdk-cairo-set-source-pixbuf
to add a pixbuf to the background.
Also, the CSS GTK+ recognizes is a little different between versions. I am still using GTK3.18 and it
understands a little different CSS string than GTK3.22. Something to keep in mind if you are using CSS
strings in code.
Eric
/*
gcc -Wall css1.c -o css1 `pkg-config --cflags --libs gtk+-3.0`
Tested with GTK3.18 and GTK3.22 on Ubuntu16.04
*/
#include<gtk/gtk.h>
static gboolean draw_window(GtkWidget *window, cairo_t *cr, gpointer user_data)
{
//A green window.
cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
cairo_paint(cr);
return FALSE;
}
int main(int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkWidget *window=gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "CSS");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
//Uncomment the following two line for drawing with cairo.
//gtk_widget_set_app_paintable(window, TRUE);
//g_signal_connect(window, "draw", G_CALLBACK(draw_window), NULL);
gtk_widget_show_all(window);
//Setup CSS for the program.
GError *css_error=NULL;
gint minor_version=gtk_get_minor_version();
gchar *css_string1=NULL;
//GTK CSS changed in 3.20.
if(minor_version>20)
{
css_string1=g_strdup("window{background: blue;}");
}
else
{
css_string1=g_strdup("GtkWindow{background: blue;}");
}
GtkCssProvider *provider=gtk_css_provider_new();
GdkDisplay *display=gdk_display_get_default();
GdkScreen *screen=gdk_display_get_default_screen(display);
gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_css_provider_load_from_data(provider, css_string1, -1, &css_error);
if(css_error!=NULL)
{
g_print("CSS loader error %s\n", css_error->message);
g_error_free(css_error);
}
g_object_unref(provider);
if(css_string1!=NULL) g_free(css_string1);
gtk_main();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]