Re: Centering widgets relative to another.



Kristopher Kycia wrote:
> I would like to be able to center windows / dialogs however not every
> object seems to have a width and height.  And many objects do not have a
> default x and y position.  Ideally I would like to have my top level
> window appear at any position and then have subsequent dialogs appear
> centered relative to the position of the top level window (which can
> change if the user drags the window around). 

Hi Kristopher, I do this by subclassing off gtk_window, and overriding
realize. Something like:

static void
idialog_realize( GtkWidget *widget )
{
        iDialog *idlg = IDIALOG( widget );

        /* Centre over parent.
         */
        if( idlg->window_parent ) {
                GtkWindow *pwnd = widget_get_window( idlg->window_parent
);
                int x, y, w, h;
                int dx, dy;

                gdk_window_get_size( GTK_WIDGET( pwnd )->window, &w, &h
);
                gdk_window_get_origin( GTK_WIDGET( pwnd )->window, &x,
&y );
                dx = x + w / 2 - GTK_WIDGET( idlg )->requisition.width /
2;
                dy = y + h / 2 - GTK_WIDGET( idlg )->requisition.height
/ 2;

                gtk_widget_set_uposition( GTK_WIDGET( idlg ), dx, dy );
        }

        if( GTK_WIDGET_CLASS( parent_class )->realize )
                (*GTK_WIDGET_CLASS( parent_class )->realize)( widget );
}

Although this works for me, it's a horrible hack. gtk+-2.0 fixes this in
a nice way, so I hope this is just a temporary thing.

John
--
John Cupitt, john cupitt ng-london org uk, +44 (0)20 7747 2570
VASARI Lab, The National Gallery, Trafalgar Square, London, WC2N 5DN




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