Re: How to change GTK+3 label color dynamically



From: R?ben Rodrigues <ruben_gr live com pt>

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


Hi,

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


I have this function (for any widget type):

void set_background_color(GtkWidget *w, gchar *color)
{
GtkCssProvider *gcp;
GtkStyleContext *gsc;
gsc = gtk_widget_get_style_context(w);
const gchar *type = g_type_name (G_TYPE_FROM_INSTANCE (w));
gchar *str = g_strdup_printf ("%s {background-color: %s;}", type,
color);
gcp= gtk_css_provider_new();
gtk_css_provider_load_from_data(gcp, str, -1, 0);
g_free (str);
gtk_style_context_add_provider(gsc, GTK_STYLE_PROVIDER(gcp), 
    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}

I don't know if there is a memory leak here though...

HTH

Richard Shann



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