2

I've written a simple function which aim is to change text & color of a GtkLabel. Trouble is (off course) : it doesn't work. Text contained in "status" is set correctly but color is not.

I use the Pango Attribute to set it but no way. Call is like : _SetStatus(context, "Running", "green");

GUI_ERR_HDL _SetStatus(GuiContext *context, const gchar *status, const gchar *color) {
GtkLabel *lbl;
PangoAttrList *pngList;
PangoAttribute *pngFgColor;
GdkRGBA rgba;
guint16 r;
guint16 g;
guint16 b;

lbl=GTK_LABEL(gtk_builder_get_object(context->builder, 
    (gchar*)OPENSESSION_LBL_STATUS));

pngList=gtk_label_get_attributes(lbl);

gdk_rgba_parse(&rgba, color);

r=(guint16)rgba.red*255;
g=(guint16)rgba.green*255;
b=(guint16)rgba.blue*255;

pngFgColor=pango_attr_foreground_new(r,g,b);
pango_attr_list_change(pngList, pngFgColor);

gtk_label_set_attributes(lbl, pngList);
gtk_label_set_label(lbl, status);

return NO_ERR;
}

Any idea ?

Thanks in advance.

Vincent.

Chouchenn
  • 63
  • 9

2 Answers2

2

I'd recommend using gtk_label_set_markup (markup is described here).

Another approach (more complicated) is described here.

In both cases you should understand, that user can use third-party theme and changing color can make things unreadable

Alexander Dmitriev
  • 2,483
  • 1
  • 15
  • 20
0

In my applications I do it this way:

const static GdkRGBA rgba_color = {.red = 0.0, .green = 1.0, .blue = 0.0, .alpha = 1.0};
gtk_widget_override_color(label_xyz, GTK_STATE_FLAG_NORMAL, &rgba_color);

I don't use any Pango functions in my applications.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • Sadly gtk_widget_override_color has been deprecated. Any other solution ? – Chouchenn Jun 22 '18 at 14:00
  • Ouch, I didn't touch these applications for quite a while. If you need all this Pango stuff as a replacement, I'm not keen on updating too soon. :( – Gerhardh Jun 22 '18 at 14:05