2

First time poster; long time admirer of the Stack Overflow angels.

I'm having an issue with colors in span text that are controlled by Pango.

Long Version:
I'm updating an old UI program which has C++ code guts with GTK, XML (written by Glade), and an RC stylesheet handling the graphics. Some of our colored markup text is hard-coded in the XML. Some of it is dynamically set in the C++ code. The problem is, that when the program runs on our older systems, the color referenced by span text as 'green' shows up as #00FF00. On our newer systems, 'green' is showing up as #008000. Example of code printing to a label widget:

    gtk_label_set_markup((GtkLabel *) TitleBarLabel, "<span color='green'>Orbital Cannon Positioning</span>");

I'm fairly certain that Pango is in control of the span text markup. I found that the difference between the greens is exactly the difference between X11 and W3C color lists (https://en.wikipedia.org/wiki/X11_color_names#Clashes_between_web_and_X11_colors). It seems that our old systems are using X11 and our new ones are using W3C, which makes sense.


I could just replace all instances of 'green' with '#00FF00' but if we wanted to change the colors in the future, we'd have to go through the whole thing again. I'd much rather have the colors changeable through a stylesheet instead of baked into the code.

C++ Code:

    GtkWidget * TitleBarLabel;
    TitleBarLabel = GTK_WIDGET (get_builder_object (builder, "TitleBarLabel"));
    gtk_label_set_markup((GtkLabel *) TitleBarLabel, "<span color='#00FF00'>Death Ray Power Status</span>");

I can create a GdkColor at run-time and gdk_color_parse it with values from a config file, and then use gtk_widget_modify_text() to apply the color to the label widgets. But then that doesn't work for all of the hard-coded span text in the XML. Also, we have pleanty of labels with bits of text colored differently inside the same line.

C++ Code:

    GdkColor  pass_color;
    gdk_color_parse("#00FF00", &pass_color);

    gtk_widget_modify_text(TitleBarLabel, GTK_STATE_NORMAL, &pass_color);

I can make a style in my RC file for each color and link every single label that would use that color at run-time. But we'd have to remove all markup coloring and add lots of code for grabbing widgets that we never bothered with before and code for setting names of widgets instead of just printing to them with new span text. It gets the desired result of having the colors changeable in a stylesheet but it's a massive undertaking and it's not intuitive for our veteran engineers who are used to using the color attributes.

RC File:

    style "pass_color"
    {
       fg[NORMAL] = #00FF00
    }
    widget "*TitleBarLabel_Pass" style "pass_color"

C++ Code:

    gtk_widget_set_name(TitleBarLabel, "TitleBarLabel_Pass");

Short Version:
Ideally, I would like to be able to make a new color at run-time that we can link with span text in such faction:

    <span color='MyNewColor'>Weather Manipulation Settings</span>

Or maybe even create a new tag that applies specific attributes, like:

    <span><MyNewColor>Shark Tank pH Balance</MyNewColor></span>

But I doubt that's possible.

I tried playing around with pango_attr_type_register(), pango_attr_foreground_new(), and friends, but I couldn't figure out how attributes work of if they could even do what I thought they did. After much research, it looks like an 'attribute' is just a one-time setting on a single string of text. And not a new value that can be called in line with span text, as I hoped.

Is anything like this remotely possible without rebuilding all of Pango?
Is there a different work around that would get me a stylesheet like setup?
At this point, I'm open to suggestions.

Version Specs:
Computers showing green as #00FF00
OS: Linux Slackware 13.37 and below
GTK: 2.24.4
Pango: 1.28.4

Computers showing green as #008000
OS: Linux Slackware 14.1
GTK: 2.24.20
Pango: 1.34.1

Sizz
  • 21
  • 3

1 Answers1

0

If you are able to use GTK 3.x, I would suggest doing that, where this is much easier to do using CSS. There is even a way to use multiple CSS styles for different regions in the same label, though it is awkward.

In GTK 2, as you noted, you can reference widgets by their name property in your RC file:

widget "shark-tank-ph-label" style "green-text"
style "green-text" {
    text[NORMAL] = #008000
}

I would recommend taking this approach even if it's not what you're used to. Refactoring once to remove the hardcoded colors from your labels will make it much easier the next time you have to change something like this, and will also make your code closer to how things would work in GTK 3.x should you decide to make a port in the future.

ptomato
  • 56,175
  • 13
  • 112
  • 165