Re: Focus on transient windows/dialogs.



Daniel Åborg <tjost ctrl-c liu se> writes:
> 
> I've now spent the morning gleaning the web and GTK-documentation for
> information about focusing windows, to no avail. Therefore, I'm hoping
> for a share of the collective wisdom of gtk-list in solving my puzzle.
> 
> Essentially, I want to open transient dialog windows from other
> windows of my application, such that the dialogs get focus and are
> immediately ready for input by the user.
> 
> The dialogs are of the type search dialog, are you sure, confirm, etc.
> I would also want them to open at some intuitive "central" location of
> the screen so that they're immediately visible to the user.

Typically the right things to do are:

 - set transient for hint on the dialog
   (gtk_window_set_transient_for(), or pass parent to
   gtk_dialog_new_with_buttons() (GTK 2 only))
   This gives the window manager enough info to center the 
   dialog on the application window.
 - if there's no clear "parent" window call 
   gtk_window_set_position (dialog, GTK_WIN_POS_CENTER)
 - most window managers should focus the thing when you show it; 
   if yours doesn't then kick it for being lame
 - in GTK 2, call gtk_window_present(), which will focus the dialog, 
   raise it, get it on the current workspace, etc., all subject
   to window manager cooperation, but GTK does the best it can
 - in GTK 1.2 you could write a "focus window" function along these
   lines:

   #include <gdk/gdkx.h>

    void 
    gdk_window_focus (GdkWindow *window)
    {
      gdk_error_trap_push ();
      XSetInputFocus (GDK_WINDOW_XDISPLAY (window),
                      GDK_WINDOW_XWINDOW (window),
                      RevertToParent,
                      timestamp);
      XSync (GDK_WINDOW_XDISPLAY (window), False);
      gdk_error_trap_pop ();
    }

   Then call that on widget->window _after the window has been mapped_
   (after you get a "map_event" signal back on the window)


In general though, my opinion is that you just need to set the
transient_for() hint, and window managers should be smart enough to do
the rest of the right thing, at least if you are showing the dialog
for the first time. If you want to bring an existing dialog to the
user's attention, then gtk_window_present()/gdk_window_focus() are 
needed to tell the WM about that.

Havoc
 



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