getting real widget sizes how and when



Hi all,

* second attempt ... first mail seems not to have come through *

I have this strange problem with resizing a viewport. Reducing it to the
elementary stuff, I'm doing and getting this:

- create a viewport
- put a vbox in it
- put (for the time being) a single widget in the vbox
- show everything
- reduce the viewport's height to conserve space if the widgets in the
vbox don't need as much space as the viewport's desired maximum height

let's say: If the vbox contains 10 widgets with a total 1000 pixels
height, set the viewport's height to a maximum of 400 pixels and scroll.
If the vbox contains just a single widget of 100 px height, make the
viewport adjust to that height.

Now, what I get is a viewport that is always some pixels (with my current
theme 8) too high.

After showing the dialog, I do:

// auto-resize the dialog
{
        GtkRequisition size, vp_size;
        int set_size, orig_vp_size;
        gint win_width;
        gint win_height;

        int a;

        // iterate over all children of the vbox2, though for testing we only use
one
        for ( a = 0; a < g_list_length( GTK_BOX( vbox2 )->children ); a++ ) {
                // get the nth's child from the list
                GtkBoxChild *child = g_list_nth_data( GTK_BOX( vbox2 )->children, a );
                // retrieve the widget from the box child
                        XvErrorItem *item = ((XvErrorItem*) child->widget );
                // the XvErrorItem uses an ebox to paint a white background, so this is
effectively the area of the complete widget
                        GtkWidget *ebox = ((GtkWidget*) item->ebox);

                gtk_widget_size_request( child->widget , &size );
                printf("gtk2_warning: child size: %i \n", size.height );

                gtk_widget_size_request( ebox , &size );
                printf("gtk2_warning: ebox size: %i \n", size.height );

        }

        // get the space the vbox uses
        gtk_widget_size_request(vbox2, &size);
        // the max size for the error list is 400 px
        set_size = ( size.height > 400 ? 400 : size.height );

        // get the viewport's current size to print it ... also we need to know
the amount by which we reduced the viewport's
        // height to reduce the window's height, too
        gtk_widget_size_request(viewport1, &vp_size);
        orig_vp_size = vp_size.height;
        printf("gtk2_warning: setting vp height to: %i ... old height: %i \n",
set_size, orig_vp_size);

        // resize the viewport
        gtk_widget_set_size_request (viewport1, -1, set_size );
        // check the viewport's size again
        gtk_widget_size_request(viewport1, &vp_size);
        printf("gtk2_warning: new vp height: %i \n", vp_size.height);

        // get the window's size
        gtk_window_get_size( GTK_WINDOW(warning), &win_width, &win_height);
         printf("gtk2_warning: the window height is: %i - needs to be
reduced by: %i to %i \n",
             win_height, ( orig_vp_size - vp_size.height ), win_height - (
orig_vp_size - vp_size.height ));
         gtk_window_resize ( GTK_WINDOW(warning), win_width, win_height - (
orig_vp_size - vp_size.height ) );
         gtk_widget_size_request(GTK_WIDGET(warning), &size);
         printf("gtk2_warning: the dialog's size request is: %i \n",
size.height);

        gtk_window_get_size( GTK_WINDOW(warning), &win_width, &win_height);
         printf("gtk2_warning: the window height is now: %i \n",
win_height);

         gtk_widget_size_request(viewport1, &vp_size);
         printf("gtk2_warning: new vp height: %i \n", vp_size.height);

   }


With that I get the following output:

gtk2_warning: child size: 173
gtk2_warning: ebox size: 173
gtk2_warning: setting vp height to: 173 ... old height: 404
gtk2_warning: new vp height: 173
gtk2_warning: the window height is: 570 - needs to be reduced by: 231 to 339
gtk2_warning: the dialog's size request is: 327
gtk2_warning: the window height is now: 570
gtk2_warning: new vp height: 173

This seems to suggest that the computation of the difference is somehow
going wrong. The size of the widget contained in the vbox is always
reported correctly (173). This can be verified from the screenshot here:
http://www.jarre-de-the.net/computing/Bildschirmfoto.png . The second
printout of the dialog's size is wrong due to the
nature of gtk_window_get_size. However, if I manually reduce the window
size by some more pixels, I get the desired effect (just not for all
possible themes). This leads me to the conclusion that while the viewport
requests 404 pixels (which is exactly what I set it to request) it
actually gets drawn a few pixels larger. So when I calculate based on
requests I don't get the correct amount by which to reduce the window's
height.
However, if I work with ...->allocation.height instead of
gtk_widget_size_request, it appears that the dialog is not rendered at the
point in time where I attempt the calculation (I get 130 for the child
size).
I've tried all sorts of things to make sure the dialog get's rendered
first (wait till the create_... function returns, add sleeps and stuff) to
no avail.

Will moving this stuff to an event handler for the realize signal help?

Thanks for any insights,

Karl.










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