Re: Dialogs in middle of windows

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

 



John Cupitt wrote:

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 ) );
}
_______________________________________________

gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list



Here's a little function I wrote a while back to center my dialogs in the center
of my app. It should work in both linux and windows. Depending on your
need you can probably do away with the viewmanager argument and just pass
in dialog, and parent.


static void
centeronparent( ViewManager *vm, GtkWidget *dialog, GtkWidget *parent_window )
{
gint x, y, width, height, screenwidth, screenheight, parentwidth, parentheight;
// handle the case that there is already a dialog being displayed. center on that instead of
// the app since that dialog will have focus.
GtkWidget *parent = parent_window ? parent_window : vm->m_this->app_window;
GdkScreen *parent_screen = gtk_window_get_screen( GTK_WINDOW( parent ) );


 gtk_window_get_position( GTK_WINDOW( parent ), &x, &y );
 gtk_window_get_size( GTK_WINDOW( parent ), &parentwidth, &parentheight );
 gtk_window_get_size( GTK_WINDOW( dialog ), &width, &height );

 screenwidth  = gdk_screen_get_width( parent_screen );
 screenheight = gdk_screen_get_height( parent_screen );

 x = ( parentwidth - width ) / 2 + x;
 y = ( parentheight - height ) / 2 + y;


//make sure that the dialog box never moves outside of //the screen if(x < 0) x = 0; if(y < 0) y = 0; if(x + width > screenwidth) x = screenwidth - width; if(y + height > screenheight) y = screenheight - height;

 gtk_window_set_screen( GTK_WINDOW( dialog ), parent_screen );
 gtk_window_move ( GTK_WINDOW( dialog ), x, y );

}

-todd

_______________________________________________

gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux