Gtk accel group error in C

I am a newbie in GTK+ and I do not understand what’s wrong with my callback function. Could you help me please?

I want to change my box orientation, but somehow my box pointer is wrong inside my callback function.

My code:

#include <gtk/gtk.h>
#include <stdio.h>

void flip_buttons(GtkWidget *window, gpointer user_data) {
  gtk_orientable_set_orientation(
				 (GtkOrientation *)user_data,
				 GTK_ORIENTATION_VERTICAL);
}

int main (int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *button;
    GtkWidget *button2;

    gtk_init (&argc,&argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);

    button = gtk_button_new_with_label ("Btn A");
    button2 = gtk_button_new_with_label ("Btn B");

    gtk_container_add (GTK_CONTAINER(window), box);

    gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (box), button2, TRUE, TRUE, 0);
    
    gtk_widget_show_all (window);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GClosure* closure_flip_buttons =
      g_cclosure_new(G_CALLBACK(flip_buttons), box, 0);

    // Set up the accelerator group.
    GtkAccelGroup* accel_group = gtk_accel_group_new();

    gtk_accel_group_connect(accel_group,
                            GDK_KEY_F,
                            GDK_CONTROL_MASK,
                            0,
                            closure_flip_buttons);
    
    gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);

    gtk_main();

    return 0;
}

The above code compiles, but then I have this error:

Gtk-CRITICAL **: 10:51:36.721: gtk_orientable_set_orientation: assertion 'GTK_IS_ORIENTABLE (orientable)' failed

Your callback should be something like this:

gboolean flip_buttons (GtkAccelGroup *accel_group,
                       GObject *acceleratable,
                       guint keyval,
                       GdkModifierType modifier,
                       gpointer user_data)
{
    GtkWidget *box = GTK_WIDGET (user_data);
    
    gtk_orientable_set_orientation (GTK_ORIENTABLE (box),
	                                GTK_ORIENTATION_VERTICAL);
    return TRUE;
}

The documentation for gtk_accel_group_connect() says:

The signature used for the closure is that of GtkAccelGroupActivate.

g_cclosure_new adds the user_data parameter at the end.

You were also casting the first parameter to gtk_orientable_set_orientation to GtkOrientation * instead of GtkOrientable *.

2 Likes

Thank you, It worked!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.