Re: How to check whether the window is visible(at top/front) to user?

On Fri, 30 Jul 2010 01:21:25 +1000
Tao Wang <dancefire gmail com> wrote:
> I have an icon shown on tray by Gtk::StatusIcon, and I expect that if
> I click the icon, the main window will show, and click it again, the
> window will hide. So far so good. But I also expect that if the
> window is not visible by user, such as some other windows at the top
> of the window, then, when I click the tray icon, the main window will
> not hide (because ->get_visible() is true), instead, the window move
> to the front/top, so user can see it.
> I use following code to do that:
>     if (main_window->get_visible())
>     {
>         if (main_window->is_focus())
>         {
>             main_window->hide();
>         }else{
>             main_window->raise();
>         }
>     }else{
>         main_window->show();
>     }
> However, it doesn't work. is_focus() is always false when I clicked
> the tray icon, so the window cannot hide. Can anyone tell me where am
> I wrong? Thanks.

If get_visible() returns true, it doesn't mean the widget concerned
isn't obscured or (I think) iconified.  It just means it has been shown
and has not since had hide() called on it.  If you are interested in
whether a widget is obscured you need to connect to, or override the
virtual function for, visibility-notify-event, and if is iconified, to
window-state-event. (Note that the "visible" in get_visible() is
different to the "visible" in visibility-notify-event: perversely
perhaps, calling hide() on a widget does not trigger a visibility
notify event.)

I cannot see why focus is relevant in your case, but you may have a
reason.  focus determines whether a particular widget happens to be the
one receiving global input events.  I am also not sure why you want to
call raise() if there is no global input focus: it's effect is to raise
the window to the top of the stacking order, which is mainly irrelevant
to focus.  (The effect is similar to calling Gtk::Window::present(),
but not identical because it only deals with stacking order.)

If you want to present a top level window, Gtk::Widget::show() won't do
it.  You should use Gtk::Window::present(), but what that does is
entirely at the discretion of the window manager (if it is on a
different virtual desktop, generally the window manager will not make
it move desktops, and if that is what you want you would have to do
that programatically).


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