Je n'ai pas trouvé de fonction *_to_string() pour GtkWidgetPath Comment puis-je examiner facilement son contenu pour résoudre un problème que je rencontre ? Imprimer sa représentation textuelle sur la console serait formidable.
Réponses
Trop de publicités?Si je comprends bien, vous voulez mettre quelque chose comme
<Widget>#<Name>: <NameOfObject>#<Name>
The widget belongs to the following style classes: <listOfClasses>
Complete Path: <Path>
pour un widget spécifique sur l'écran.
J'ai écrit les deux fonctions suivantes qui font le travail. Le code devrait être facilement traduisible dans d'autres langues et est auto-explicatif si vous avez la référence en main.
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");
}
Exemple :
Si vous appelez widget_path_dumper
sur l'objet instancié button1
dans le widget du fichier ui ci-joint, il met
<Widget>#<Name>: GtkButton#myspecialbutton
The widget belongs to the following style classes: .button
Complete Path: /GtkWindow/GtkGrid/GtkFrame/GtkAlignment/GtkButton
sur l'écran.
<?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"><b>frame1</b></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>
Alex
Points
907