knocte knocte - 3 years ago 195
CSS Question

Is there an easy way to get a textual representation of a GtkWidgetPath?

I haven't found any *_to_string() function for GtkWidgetPath, so how can I examine its content easily to debug an issue I'm having? Printing its text representation on the console would be grand.

Answer Source

If i understand you correct you want to put something like

<Widget>#<Name>: <NameOfObject>#<Name>
    The widget belongs to the following style classes: <listOfClasses>
    Complete Path: <Path>

for a specific widget on the screen.

I wrote the following two functions that do the job. The code should be easily translatable into other languages and is self explanatory if you have the reference at hand.

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


    return str;

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);
        g_print("<Widget>: %s\n", g_type_name(type));

    str = widget_path_get_style_classes (path);

    if (str != NULL) {
            ("\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");


If you call widget_path_dumper on the instantiated button1 widget of the attached ui file it puts

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

on the screen.

<?xml version="1.0" encoding="UTF-8"?>
  <!-- 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"/>
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
          <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>
              <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>
                  <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>
            <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>
            <property name="left_attach">1</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download