Setting up the get_child_position signal of GtkOverlay

I am trying to catch the “get-child-position” of the GtkOverlay and I am not sure if I understand its documentation right.

For this I came with the following callback function:

gboolean pos_callback ( GtkOverlay *overlay, GtkWidget *widget, GdkRectangle *allocation )
{
    if ( GTK_IS_OVERLAY( overlay ) )
    {
        g_print( "Callback\n" );

        allocation->height = 100;
        allocation->width = 100;
        allocation->x     = 100;
        allocation->y     = 100;

        g_object_set( widget,
                     "halign",  GTK_ALIGN_END,
                     "valign",  GTK_ALIGN_CENTER,
                      NULL );

        return TRUE;
    }

    return FALSE;
}

and called from main with:

g_signal_connect_swapped( overlay, "get-child-position", G_CALLBACK( pos_callback ), overlay );

How exactly works this Signal?

GtkOverlay emits the signal when it needs to allocate the child, and if the signal handler returns TRUE, it will then proceed with the returned allocation.

With regard to the example code:

  • if you need the GTK_IS_OVERLAY check, something is wrong
  • do not change the widget from the handler

So the following would be an odd, but valid handler (assuming widget’s minimum size doesn’t exceed 100x100):

gboolean pos_callback ( GtkOverlay *overlay, GtkWidget *widget, GdkRectangle *allocation )
{
    g_print( "Callback\n" );
    allocation->height = 100;
    allocation->width = 100;
    allocation->x = 100;
    allocation->y = 100;

    return TRUE;
}

But note that the signal is very rarely needed.

You can place the overlay child on an edge, in a corner or in the center by setting appropriate h-align/v-align values:

    GtkWidget *top_left, *center, *bottom;

    top_left = foo_bar_new ();
    g_object_set (top_left,
                  "h-align", GTK_ALIGN_START,
                  "v-align", GTK_ALIGN_START,
                  NULL);
    gtk_overlay_add_overlay (overlay, top_left);

    center = foo_bar_new ();
    g_object_set (center,
                  "h-align", GTK_ALIGN_CENTER,
                  "v-align", GTK_ALIGN_CENTER,
                  NULL);
    gtk_overlay_add_overlay (overlay, center);

    bottom = foo_bar_new ();
    g_object_set (bottom,
                  "h-align", GTK_ALIGN_FILL,
                  "v-align", GTK_ALIGN_END,
                  NULL);
    gtk_overlay_add_overlay (overlay, bottom);

The above will work without setting up a get-child-position handler.

Well, g_signal_connect() expects for this signal (get-child-position) 4 parameters for its callback ( GtkOverlay *overlay, GtkWidget *widget, GdkRectangle *allocation, gpointer user_data ).

I am using instead g_signal_connect_swapped() to drop the usage of 4th parameter and instead to use only 3.

Now the use of GTK_IS_OVERLAY check is there to be sure that the last parameter of *swapped() is GtkOverlay.

More over, a function where you cast to void unused parameters is not a good approach and if I only use your example and I call g_signal_connect() it will look like (terrible) this:

gboolean pos_callback ( GtkOverlay *overlay, GtkWidget *widget, GdkRectangle *allocation, gpointer data )
{
    (void)overlay;
    (void)widget;
    (void)data;
    
    g_print( "Callback\n" );
    allocation->height = 100;
    allocation->width = 100;
    allocation->x = 100;
    allocation->y = 100;

    return TRUE;
}

If I was wrong, then whats the usage of GTK_IS_OVERLAY ?

g_signal_connect_swapped() doesn’t change the number of parameters, it swaps the first (“instance”) and last (“user data”) parameters. In your case you use the same object for instance and user data, so whether you use g_signal_connect() or g_signal_connect_swapped() makes absolutely no difference.

If the method is only called from the g_signal_connect_swapped() call you quoted, then what else would the parameter be?

If you want to avoid compiler warnings about unused parameters, mark them with G_GNUC_UNUSED. Don’t force yourself to use parameters you don’t need.

It’s main usage is to evaluate function parameters where you don’t control the callers. For example, all public GtkOverlay methods in GTK itself will start with something like

  g_return_if_fail (GTK_IS_OVERLAY (overlay));

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