Customizing GtkTextTag appearance thru GTK3 CSS stylesheets?

爷,独闯天下 提交于 2020-01-05 04:05:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/39466395/customizing-gtktexttag-appearance-thru-gtk3-css-stylesheets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!