GTK+3 CSS style function questions




Hi all,

My GTK+3 app includes several GtkButton widgets which I am setting to specific background and foreground (i.e., label) colors. Furthermore, the colors of each of these buttons are changed over time in response to user activity. My current code seems to work OK but I have a few questions re: optimization vs. safety in my use of the GTK style routines.

Here is a condensed version of the relevant code (questions follow below):


GtkWidget *button;
GtkCssProvider *gcp;
GtkStyleContext *gsc;
char fgrgbtext[10], bgrgbtext[10], cssbuf[512];

button= gtk_button_new_with_label((gchar *) "yadayada");
gsc= gtk_widget_get_style_context(button);

/* set arbitrary hex color strings */
(void) strcpy(fgrgbtext, "#000000");
(void) strcpy(bgrgbtext, "#d264ff");
(void) sprintf(cssbuf, "GtkButton {\ncolor: %s;\nbackground-color: %s;\nbackground-image: -gtk-gradient(linear, center top, center bottom, from(%s), to(%s));\n}\n", fgrgbtext, bgrgbtext, bgrgbtext, bgrgbtext);

gcp= gtk_css_provider_new();
gtk_css_provider_load_from_data(gcp, cssbuf, -1, 0);
gtk_style_context_add_provider(gsc, GTK_STYLE_PROVIDER(gcp), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);


Keeping in mind that I am repeatedly changing the colors of several of these buttons over and over again under user control, my questions are:

(1) It seemed safest to statically allocate and maintain a distinct GtkCssProvider object for each of my buttons, otherwise multiple buttons might somehow wind up referencing the same style setting, i.e., colors. Or is this unnecessary, i.e., perhaps gtk_style_context_add_provider() copies the GtkCssProvider's contents into the widget and then the latter never references the actual GtkCssProvider object again?

(2) The GTK manual says a widget's GtkStyleContext is owned by the widget and should not be freed. Will the pointer to a particular widget's GtkStyleContext returned by gtk_widget_get_style_context() be unchanging over the life of the app or is it possible that it may change over time for whatever reason (e.g., by GTK internally destroying and recreating the widget's style context)? Is it safe for me to maintain my own static pointers to my various buttons' style contexts, or should I always retrieve the necessary context via gtk_widget_get_style_context() just prior to altering a particular button's style?

(3) Do I need to call gtk_style_context_add_provider() every time I want to change the same button's color, or should (must?) I only call that function once the very first time I set the button's colors, and then just call gtk_css_provider_load_from_data() with my updated cssbuf style description string containing the new colors every time I want to subsequently alter the colors? Will a bad thing happen if I repeatedly call gtk_style_context_add_provider() with the very same GtkStyleContext/GtkCssProvider object pair?

(4) Is there an authoritative reference on the syntax of style description strings created for use with gtk_css_provider_load_from_data()? Is there somewhere I can find a complete listing of every single property substring that might impact the appearance of a GtkButton, for example? I had to puzzle this stuff out from a half-dozen blog posts in various places, which was quite a pain in the ass. (Although I am infinitely grateful to the bloggers, of course!) I can never find my own phone/glasses/keys/whatever, though, so I freely admit the possibility that I'm missing the obvious somewhere in the existing GTK+3 manual.

Thanks!

Roger Davis
Univ. of Hawaii



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]