Alex Eastman
Alex Eastman

Reputation: 395

How do I check the type of widget in GTK+3.0?

I saw this post but it was for Python so that doesn't help me too much. I'm programming in C++, working on a code-base that I didn't write. I see some checks like GTK_IS_ENTRY and GTK_IS_COMBO_BOX, but I'm not sure where this person found these or what other GTK_IS_... there are. Is there a reference to these somewhere?

Upvotes: 2

Views: 1855

Answers (2)

Holger
Holger

Reputation: 1010

In addition to the other answers, I would like to show here another possibility that has helped me many times.

    
#include<gtk/gtk.h>

static void view_object(GtkWidget *button, gpointer test_candidate)
{

    GObject *object = G_OBJECT(test_candidate);
    GType type = G_OBJECT_TYPE (object);
    const char *type_name = g_type_name(type);
    g_print ("\nThe name of the type is: %s\n", type_name);

    GObjectClass *object_class = G_OBJECT_GET_CLASS(object);
    GParamSpec **properties = g_object_class_list_properties(object_class, NULL);
    gint i, n = g_strv_length((gchar **)properties);

    for (i = 0; i < n; i++) {
        g_print("\nProperty %d: %s\n", i, g_param_spec_get_name(properties[i]));

        GValue value = G_VALUE_INIT;
        GValue target = G_VALUE_INIT;
        g_value_init(&target,G_TYPE_STRING);

        g_object_get_property(object,g_param_spec_get_name(properties[i]),&value);

        GType value_type = G_VALUE_TYPE(&value);
        const char *type_n = g_type_name(value_type);
        g_print("Property typ: %s\n", type_n);
    
        if (g_value_transform(&value, &target)) 
                printf("Property value: %s\n", g_value_get_string(&target));
    
        g_value_unset(&target);
        g_value_unset(&value);
    }

    g_free(properties);
}

static void activate (GtkApplication *app, gpointer user_data)
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *label;

    window = gtk_application_window_new(app);
    gtk_window_set_resizable (GTK_WINDOW( window),FALSE);
    
    label = gtk_label_new("(Label-Object to test)");  // Object to test
        button = gtk_button_new_with_label("Click to test");
    g_signal_connect(button, "clicked", G_CALLBACK(view_object),label); //Object to test
    gtk_window_set_child(GTK_WINDOW(window),GTK_WIDGET(button));

    gtk_window_present(GTK_WINDOW (window));
}

int main (int argc, char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new ("org.gtk.testing_tools", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "activate",G_CALLBACK(activate),NULL);
    status = g_application_run (G_APPLICATION(app), argc, argv);
    g_object_unref(app);
    return status;
}

If you are bothered by the fact that an error message appears for unreadable properties, you can also start the program with ./tool 2>/dev/null. For testing purposes, this shouldn't be a problem.

Regards

Upvotes: 1

ebassi
ebassi

Reputation: 8815

The type checks macros are typically part of the API contract for a GObject, and they are conventionally provided by the library, so they don't end up in the documentation. All they do is call G_TYPE_CHECK_INSTANCE_TYPE with the given GType macro, like GTK_TYPE_ENTRY or GTK_TYPE_COMBO_BOX.

Upvotes: 4

Related Questions