Computing border size of GtkFrame



I need to compute the border sizes of a GtkFrame before the window containing
that GtkFrame has been realized. Precisely, I need the distances between the
GtkFrame container widget and its child. AFAICS, this isn't possible to get
before the GtkFrame has been realized. So I thought that I could use a workaround
that creates a top-level window, adds a GtkFrame with a GtkButton and computes
the size and destroys everything again. Here is the code:

void GTKFrameGetBorders(int *borderTop, int *borderOther)
{
    GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    GtkWidget* labelwidget = gtk_label_new_with_mnemonic("Foo");
    gtk_widget_show(labelwidget); 

    GtkWidget* framewidget = gtk_frame_new(NULL);
    gtk_frame_set_label_widget(GTK_FRAME(framewidget), labelwidget);

    GtkWidget *btn = gtk_button_new();
    gtk_widget_show(btn);
    
    gtk_container_add(GTK_CONTAINER(framewidget), btn);
    gtk_container_add(GTK_CONTAINER(wnd), framewidget);

    gtk_widget_show(framewidget);

    // !!! THIS CALL IS NECESSARY TO GET CORRECT VALUES !!!
    gtk_widget_realize(labelwidget);
    
    GtkAllocation alloc, child_alloc;
    gtk_widget_get_allocation(framewidget, &alloc);

    GTK_FRAME_GET_CLASS(framewidget)->compute_child_allocation(GTK_FRAME(framewidget), &child_alloc);

    *borderTop = child_alloc.y - alloc.y;
    *borderOther = child_alloc.x - alloc.x;    

    gtk_widget_destroy(wnd);
}

This seems to do the job and returns the correct values but of course it's a
really hackish solution. So I've got two questions:

1) Is there a better/cleaner solution? :)

2) Is it allowed to call gtk_widget_realize() on the "labelwidget" as done above? The
docs say that gtk_widget_realize() is propagated upwards so does this mean that
gtk_widget_realize() will end up being called on the top-level window which means
that the top-level window will be shown very briefly? That's of course something
I'd like to avoid...

I'm targetting GTK+ 2.

Thanks for any suggestions!

-- 
Best regards,
 Andreas Falkenhahn                          mailto:andreas falkenhahn com



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]