Change text color of label in GTK in C

user907810 picture user907810 · May 22, 2012 · Viewed 8.6k times · Source

I am using Gtk 2.0.

I am trying to change the text color/font color of the label.

How difficult can it get? I am just trying things like gtk_widget_modify_text etc to no avail. I want to go the "android" or "Qt" way by say adding a simple resource file with all the styles. Where and as what(.rc?) should I add this file? How to parse this file?

I already wrote my App with a lot of widgets and I do not want to change/redo them all. Can somebody show me a simple example?

I even tried a Pango example from the web but the problem is the text in my label keeps changing and therefore I could not follow this. Please help. Here is what I have tried so far and without success.

    GtkWidget *label1;    
    label1= gtk_label_new(" ");
    gtk_box_pack_start (GTK_BOX(box1), label1,TRUE,TRUE, 0);

    GdkColor color;

    gdk_color_parse ("white", &color);


    gtk_widget_modify_text ( GTK_WIDGET(label1), GTK_STATE_NORMAL, &color);

    gchar *stringMarkupText = "<span foreground=\"white\">   <b>Bold</b></span>"; //white color and bold--> the length of this text is fixed by number of spaces. I need to call a function that would set this text on a g_signal_connect so do not want to fix spaces!!
    gchar *stringPlainText;

    PangoAttrList  *attrList;

    pango_parse_markup(stringMarkupText, -1, 0, &attrList, &stringPlainText, NULL, NULL);
    gtk_label_set_attributes(GTK_LABEL(label1), attrList);

Answer

fabregaszy picture fabregaszy · May 22, 2012

From my point of view, you may use GTK resource file(rc file) in this case.

You can use the gtk_rc_parse function to load your rc file.

void gtk_rc_parse(const gchar *filename);