Re: How to change GTK+3 label color dynamically



Hi,

This is what i have, and don't works...


On 16-03-2017 11:20, Tilo Villwock wrote:
Am Donnerstag, den 16.03.2017, 09:55 +0000 schrieb Rúben Rodrigues:
Hi,

THanks. This is dynamically? I need in c language if possible :S
Dynamic version:

...

static GtkCssProvider* provider = NULL;

static void
set_label_color(GtkWidget* label, const char* color)
{
     const char* format = "label { color: %s; }";
     size_t length = strlen(format) - 2 + 1;
     char style[length];
     sprintf(style, format, color);

     if (provider == NULL) {
         /* only create and add the provider the first time */
         provider = gtk_css_provider_new();
         gtk_style_context_add_provider(
             gtk_widget_get_style_context(label),
             GTK_STYLE_PROVIDER(provider),
             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
         g_object_unref(provider);
     }

     gtk_css_provider_load_from_data(provider, style, -1, NULL);
}

...

This might not be ideal in all situations. Just make sure you don't
create a new provider everytime you set a new color. Hope that helps.

--Tilo



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