1

I have some GtkTextTable*mom_tagtable; which is the tag table of a GtkTextBuffer *mom_obtextbuf; (which is shown in two text views GtkWidget *mom_tview1; & GtkWidget*mom_tview2;). I'm using GTK3.21 on Linux/Debian/Sid and it is for the same (free software, GPLv3) application than in this other question: the expjs branch (on github) of the MELT monitor. BTW all the GTK code is in one C99 file gui.c. I have a few dozen of text tags:

static GtkTextTag *mom_tag_toptitle;    // tag for top text
static GtkTextTag *mom_tag_objtitle;    // tag for object title line
static GtkTextTag *mom_tag_objsubtitle; // tag for object subtitle line
static GtkTextTag *mom_tag_idstart;     // tag for first characters of objids
/* etc .... */

I don't want to build my GUI using GtkBuilder (I prefer coding manually, because most of the code could be later generated by my application). I would like to customize the appearance of the tags and of all my app in the same textual file. It seems that GTK CSS stylesheets could be useful.

Actually, it looks that GTK3 has or had several ways to describe the look&feel and theme of an application in textual files (GtkBuilder which also defines the GUI and I don't want to have that, GTK-CSS stylesheets, obsolete Gtk resource files, obsolete GtkUIManager, etc...) and that it is transitioning to the GTK CSS framework but the transition did not complete in GTK3.20.

I'm currently initializing these tags using hard-coded look & feel:

    mom_tag_toptitle =
      gtk_text_buffer_create_tag (mom_obtextbuf,
                                  "toptitle",
                                  "justification", GTK_JUSTIFY_CENTER,
                                  "pixels-above-lines", 2,
                                  "pixels-below-lines", 4,
                                  "foreground", "navy",
                                  "paragraph-background", "lightyellow",
                                  "font", "Helvetica Bold", "scale", 1.5, NULL);
    mom_tag_objtitle =
      gtk_text_buffer_create_tag (mom_obtextbuf,
                                  "objtitle",
                                  "justification", GTK_JUSTIFY_CENTER,
                                  "pixels-above-lines", 4,
                                  "pixels-below-lines", 2,
                                  "foreground", "brown",
                                  "font", "Sans Bold",
                                  "paragraph-background", "lightcyan",
                                  "scale", 1.3, NULL);
    mom_tag_objsubtitle =
      gtk_text_buffer_create_tag (mom_obtextbuf,
                                  "objsubtitle",
                                  "justification", GTK_JUSTIFY_CENTER,
                                  "pixels-above-lines", 1,
                                  "pixels-below-lines", 1,
                                  "foreground", "chocolate4",
                                  "font", "Sans Bold",
                                  "paragraph-background", "lightgoldenrod",
                                  "scale", 1.15, NULL);
    mom_tag_idstart =
      gtk_text_buffer_create_tag (mom_obtextbuf,
                                  "idstart",
                                  "font", "Courier Bold",
                                  "foreground", "darkgreen", NULL);

I am not happy with that approach, and I would like these tags to have an appearance (font, size, colors) described in some external textual file (and likewise for the widgets around the textviews).

So my question is:

How to customize GtkTextTag-s thru GTK CSS stylesheets?

I was thinking of creating some "fake" GtkWidgetPath (should I create one such path per gtk text tag) and using it to retrieve the style information in the GTK CSS but I am not sure at all it is the right approach.

I also don't understand exactly how two different text views showing the same text buffers can have different styles for the same text (I hope it is not possible, but I am not sure why).

This might be a related question, but it looks quite different to mine.

PS. I am not specifically focused on GTK3 CSS, but I do want to have if possible one single text file describing the appearance of my entire GTK3 application.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    [GtkTextTagTable](https://developer.gnome.org/gtk3/stable/GtkTextTagTable.html) implements GtkBuildable, so you could define the tags in the GtkBuilder UI definition language, load it using [gtk_builder_new_from_file](https://developer.gnome.org/gtk3/stable/GtkBuilder.html#gtk-builder-new-from-file), pick just the GtkTextTagTable from it using [gtk_builder_get_object](https://developer.gnome.org/gtk3/stable/GtkBuilder.html#gtk-builder-new-from-file), and supply it to [gtk_text_buffer_new()](https://developer.gnome.org/gtk3/stable/GtkTextBuffer.html#gtk-text-buffer-new). That isn't CSS, though. – Nominal Animal Sep 13 '16 at 16:21
  • @NominalAnimal: Do you have an example of how to write that `.ui` file for GtkBuildable ? – Basile Starynkevitch Jun 10 '17 at 05:45

1 Answers1

0

To answer my own question, as suggested by Nominal Animal in a comment, for future reference, it is not possible to use CSS, but possible with UI builder (The actual working code is in C++ and using GtkMM libgtkmm & GTK 3.22, i.e. on my github melt-monitor master branch git commit d947aedc723913789515). Indeed using (the GtkMM equivalent of) gtk_builder_new_from_file (passing "browsermom.ui" which is the path of the XML GTK UI file below) to get a GtkBuilder, fetching the GtkTextTagTable from it using gtk_builder_get_object, supplying it to the GtkTextBuffer with gtk_text_buffer_new.

Here is the XML GTK UI file browsermom.ui that I am using

<?xml version="1.0" encoding="UTF-8"?>
<!-- file browsermom.ui for GtkBuilder describing tags in browsing textview -->
<!-- see also https://stackoverflow.com/q/39466395/841108 -->
<interface>
    <requires lib="gtk+" version="3.22"/>
    <object class='GtkTextTagTable' id='browsertagtable_id'>
      <!-- page_title_tag for the entire page -->
        <child type="tag">
            <object class='GtkTextTag'>
                <property name="name">page_title_tag</property>
                <property name="font">Arial Bold</property>
                <property name="scale">1.35</property>
                <property name="paragraph-background">lemonchiffon1</property>
                <property name="pixels-above-lines">3</property>
                <property name="pixels-below-lines">6</property>
                <property name="foreground">navyblue</property>
                <property name="justification">GTK_JUSTIFY_CENTER</property>
            </object>
        </child>
      <!-- object_title_tag for start of object -->
        <child type="tag">
            <object class='GtkTextTag'>
                <property name="name">object_title_tag</property>
                <property name="font">Cabin Medium</property>
                <property name="scale">1.27</property>
                <property name="paragraph-background">cornsilk</property>
                <property name="pixels-above-lines">2</property>
                <property name="pixels-below-lines">3</property>
                <property name="foreground">navyblue</property>
                <property name="justification">GTK_JUSTIFY_CENTER</property>
            </object>
        </child>
 <!-- etc.... --->
 </object>
</interface>

(the tricky part was to understand that the <child XML tags need a type="tag" XML attribute)

BTW, for some reason I don't understand, I'm getting GTk warnings like

(monimelt:25323): Gtk-WARNING **: Failed to set property gtkmm__GtkTextTag.scale to O.9: Could not parse double 'O.9'

even if 0.9 does not appear in that file.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547