有没有一种简单的方法来获取GtkWidgetPath的文本表示?

时间:2013-08-11 00:00:07

标签: css gtk gtk3

我没有为GtkWidgetPath找到任何* _to_string()函数,那么如何轻松检查其内容以调试我遇到的问题?在控制台上打印其文本表示将是宏伟的。

2 个答案:

答案 0 :(得分:1)

如果我理解你是正确的,你想要放置像

这样的东西
<Widget>#<Name>: <NameOfObject>#<Name>
    The widget belongs to the following style classes: <listOfClasses>
    Complete Path: <Path>

屏幕上的特定小部件。


我写了以下两个完成这项工作的功能。代码应该可以很容易地翻译成其他语言,如果您有手头的参考,则可以自我解释。

gchar *
widget_path_get_style_classes (GtkWidgetPath * path)
{
    GSList *list;
    gchar *str = NULL, *ptr;

    list = gtk_widget_path_iter_list_classes (path, -1);

    if (list == NULL)
        return NULL;

    do {

        ptr = str;
        str = g_strdup_printf (".%s", (gchar *) list->data);
        str = g_strjoin (", ", str, ptr, NULL);
        g_free (ptr);

    } while (list = g_slist_next (list));

    g_slist_free(list);

    return str;
}

void
widget_path_dumper (GtkWidget * widget)
{

    GtkWidgetPath *path;
    guint i, length;
    gchar *str;
    GType type;

    path = gtk_widget_get_path (widget);
    length = gtk_widget_path_length (path) - 1;

    type = gtk_widget_path_iter_get_object_type (path, length);
    str = (gchar *) gtk_widget_path_iter_get_name (path, length);

    if (str != NULL)
        g_print ("<Widget>#<Name>: %s#%s\n", g_type_name (type), str);
    else
        g_print("<Widget>: %s\n", g_type_name(type));

    str = widget_path_get_style_classes (path);

    if (str != NULL) {
        g_print
            ("\tThe widget belongs to the following style classes: %s\n", str);
        g_free (str);
    }

    g_print ("\tComplete Path: ");

    for (i = 0; i <= length; i++) {

        type = gtk_widget_path_iter_get_object_type (path, i);
        g_print ("/%s", g_type_name (type));

    }

    g_print ("\n");
}

示例:

如果您在附加的ui文件的实例化widget_path_dumper窗口小部件上调用button1

<Widget>#<Name>: GtkButton#myspecialbutton
    The widget belongs to the following style classes: .button
    Complete Path: /GtkWindow/GtkGrid/GtkFrame/GtkAlignment/GtkButton

在屏幕上。

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="border_width">10</property>
    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkFrame" id="frame1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label_xalign">0</property>
            <property name="shadow_type">out</property>
            <child>
              <object class="GtkAlignment" id="alignment1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="margin_right">10</property>
                <property name="margin_top">10</property>
                <property name="margin_bottom">10</property>
                <property name="left_padding">12</property>
                <child>
                  <object class="GtkButton" id="button1">
                    <property name="label" translatable="yes">button</property>
                    <property name="name">myspecialbutton</property>
                    <property name="use_action_appearance">False</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <property name="halign">center</property>
                    <property name="valign">center</property>
                    <property name="use_action_appearance">False</property>
                  </object>
                </child>
              </object>
            </child>
            <child type="label">
              <object class="GtkLabel" id="label1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">&lt;b&gt;frame1&lt;/b&gt;</property>
                <property name="use_markup">True</property>
              </object>
            </child>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

答案 1 :(得分:0)

由于gtk v3.2有gtk_widget_path_to_string,可以像这样简单地使用:

printf("%s\n",gtk_widget_path_to_string (path));

..但是,与user1146332的提案

相比,输出效果不佳