通过GTK3 CSS样式表自定义GtkTextTag外观? [英] Customizing GtkTextTag appearance thru GTK3 CSS stylesheets?

查看:85
本文介绍了通过GTK3 CSS样式表自定义GtkTextTag外观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些GtkTextTable*mom_tagtable;,它是GtkTextBuffer *mom_obtextbuf;的标签表(在两个文本视图GtkWidget *mom_tview1;GtkWidget*mom_tview2;中显示).我在Linux/Debian/Sid上使用GTK3.21,它用于的应用程序(免费软件,GPLv3)与其他问题: expjs分支(在github ).顺便说一句,所有GTK代码都在一个C99文件中 gui.c .我有几十个文本标签:

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 .... */

我不想使用GtkBuilder构建我的GUI(我更喜欢手动编码,因为大多数代码以后都可以由我的应用程序生成).我想在同一文本文件中自定义标签和所有应用程序的外观.似乎 GTK CSS样式表可能有用. >

实际上,GTK3似乎已经或曾经有几种方法可以在文本文件中描述应用程序的外观和主题( GtkUIManager 等),并且它正在过渡到GTK CSS框架,但过渡未在GTK3.20中完成.

我目前正在使用硬编码外观&初始化这些标签.感觉:

    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);

我对这种方法不满意,我希望这些标签具有在某些外部文本文件中描述的外观(字体,大小,颜色)(对于文本视图周围的小部件也是如此).

所以我的问题是:

如何通过GTK CSS样式表自定义GtkTextTag -s?

我当时正在考虑创建一些伪造的" GtkWidgetPath (我应该为每个gtk文本标记创建一个这样的路径),并使用它来检索GTK CSS中的样式信息,但是我不确定这是否是正确的方法.

我也不完全了解显示相同文本缓冲区的两个不同文本视图如何对同一文本具有不同的样式(我希望这是不可能的,但我不确定为什么)

可能是一个相关的问题,但与我的看起来完全不同.

PS.我并不是特别关注GTK3 CSS,但我确实希望有一个单一的文本文件来描述我的整个GTK3应用程序的外观.

解决方案

按照名义动物的建议回答我自己的问题在注释中,以供将来参考,无法使用CSS,但可以与UI构建器一起使用(实际的工作代码是C ++,并使用GtkMM libgtkmm& GTK 3.22,即在我的github 熔融监控器主分支git commit gtk_builder_new_from_file (传递下面的XML GTK UI文件的路径"browsermom.ui")来获取GtkBuilder,并使用

(棘手的部分是要了解<child XML标记需要type="tag" XML属性)

顺便说一句,由于某种原因,我不明白,我收到了类似GTk的警告

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

即使0.9没有出现在该文件中.

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.

解决方案

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.

这篇关于通过GTK3 CSS样式表自定义GtkTextTag外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆