Re: Dialogs in middle of windows



> >> What's the best way to get a dialog perfectly centered in the middle of
> >> a window? Currently my code seems to place the dialog in the center of
> >> the screen instead of the window.
> >
> > I think you need GTK_WIN_POS_CENTER_ON_PARENT.
> 
> I tried that, it doesn't seem to do what I wanted. It placed it at the
> bottom of the screen!
> 
> Any other ideas?

My understanding is that GTK_WIN_POS_CENTER_ON_PARENT sets a hint to
the window manager about placement, it's then up to the WM to actually
put the window there. If it doesn't work, you maybe have an odd window
manager. CENTER_ON_PARENT works for me with gnome (sfish? mcity? can't
remember), kde and win32.

In the bad old gtk-1.2 days I used to do this by adding stuff to the
_realize() handler to find the position and size of the parent and
child and calculate a position by hand. But this is rather unreliable
and will often misposition slightly due to the decorations the WM
adds.

FWIW, here's my crusty old code:

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

#ifdef DEBUG
        printf( "idialog_realize: %s\n", IWINDOW( idlg )->title );
#endif /*DEBUG*/

        /* Centre over parent. We should be able to just set a hint and let
         * the WM do this, but it's very unreliable. Do it ourselves.
         */
        if( idlg->window_parent ) {
                GtkWindow *pwnd = GTK_WINDOW(
                        gtk_widget_get_toplevel( idlg->window_parent ) );
                GtkWidget *wid = GTK_WIDGET( idlg );
                int x, y, w, h;
                int dx, dy;
                const int swidth = gdk_screen_width();
                const int sheight = gdk_screen_height();

                /* get_root_origin allows for WM decorations.
                 */
                gdk_window_get_size( GTK_WIDGET( pwnd )->window, &w, &h );
                gdk_window_get_root_origin( GTK_WIDGET( pwnd )->window,  
                        &x, &y ); 
                dx = x + w / 2 - wid->requisition.width / 2;
                dy = y + h / 2 - wid->requisition.height / 2;
                
                /* Clip against screen size. Allow a bit for desktop borders
                 * added by the WM. 
                 */
                dx = IM_CLIP( 50, dx, swidth - wid->requisition.width - 50 );
                dy = IM_CLIP( 50, dy, sheight - wid->requisition.height - 50 );

                gtk_widget_set_uposition( GTK_WIDGET( idlg ), dx, dy );
         
#ifdef DEBUG
                printf( "idialog_realize: centering over parent at %dx%d\n",
                        dx, dy );
#endif /*DEBUG*/
        }
                
        GTK_WIDGET_CLASS( parent_class )->realize( widget );

        if( idlg->entry )
                gtk_widget_grab_focus( GTK_WIDGET( idlg->entry ) );
}



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