5

I've used Wnck to check whether a window has been created like this:

    screen = Wnck.Screen.get_default()
    screen.force_update()  # recommended per Wnck documentation
    window_list = screen.get_windows()

    for window in window_list:
        print(window.get_name())
        if window.has_name():
            if window.get_name() == self.xld_main_window.get_title():
                window_found = True
                break
    assert window_found, 'The Gtk.Window named {window_name} has not been found.'\
        .format(window_name=self.xld_main_window.get_title())

    # clean up Wnck (saves resources, check documentation)
    window = None
    screen = None

However, since dialogs don't show up in the list of tasks, I can't find them that way. What is an appropriate way of checking whether they're displayed (and modal / not modal)?

Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86

2 Answers2

1

The Wnck.Screen.get_windows method returns all windows including dialogs. There is no distinction as the function returns any Wnck.Window that is currently mapped. The source goes like this:

* The #WnckScreen represents a physical screen. A screen may consist of
* multiple monitors which are merged to form a large screen area. The
* #WnckScreen is at the bottom of the libwnck stack of objects: #WnckWorkspace
* objects exist a #WnckScreen and #WnckWindow objects are displayed on a
* #WnckWorkspace.
*
* The #WnckScreen corresponds to the notion of
* <classname>GdkScreen</classname> in GDK.

GList*
wnck_screen_get_windows (WnckScreen *screen)
{
  g_return_val_if_fail (WNCK_IS_SCREEN (screen), NULL);

  return screen->priv->mapped_windows;
}

where screen->priv points to a struct containing some lists of the windows (mapped, stacked), a pointer to the active window, etc. Some WnckWindow can have WNCK_WINDOW_DIALOG set and be a dialog.

The WnckWindow class also provides a function transient_is_most_recently_activated() to know if the focus should go to a transient child window when selected in a WnckTaskList or to minimize the transient window with its parent. For example, to know wether My Application window has a most recently activated transient:

screen = Wnck.Screen.get_default()
screen.force_update()  # recommended per Wnck documentation
window_list = screen.get_windows()

for window in window_list:
    if window.get_name() == 'My Application':
        print(window.transient_is_most_recently_activated())

The script below catches the dialogs as other mapped windows (no matter if they are modal/non-modal or the application they are from).

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Wnck', '3.0')
from gi.repository import Gtk, Wnck

class DialogExample(Gtk.Dialog):

    def __init__(self, parent):
        Gtk.Dialog.__init__(self, "My Dialog", parent, 0, #or Gtk.DialogFlags.MODAL
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OK, Gtk.ResponseType.OK))

        self.set_default_size(100, 100)

        label = Gtk.Label("This is a dialog to display additional information")

        box = self.get_content_area()
        box.add(label)
        self.show_all()

class DialogWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Dialog Example")
        self.set_border_width(6)
        button = Gtk.Button("Open dialog")
        button.connect("clicked", self.on_button_clicked)
        self.add(button)

    def on_button_clicked(self, widget):
        dialog = DialogExample(self)
        response = dialog.run()

        if response == Gtk.ResponseType.OK:
            print("The OK button was clicked")
        elif response == Gtk.ResponseType.CANCEL:
            print("The Cancel button was clicked")

        screen = Wnck.Screen.get_default()
        screen.force_update()  # recommended per Wnck documentation
        window_list = screen.get_windows()

        for window in window_list:
            print(window.get_name())

        window, window_list = (None,)*2
        screen = None

        dialog.destroy()

win = DialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()

window = None
screen = None
Gtk.main()

Using Gdk instead of Wnck, you are doing the same thing at a slightly lower level.

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
  • Thank you for your answer, but afaik this: _"The Wnck.Screen.get_windows method returns all windows including dialogs."_ is not true. If the dialog was in that list of windows returned by the function, I'd never have posted this question. Now it's been a while, but the only other mistake I can imagine is, that I'd have to wait a little before getting the window list, but since it is not about the dialog being visible for now, I don't think the creation of the dialog should take so much time. Do you know other reasons, why the dialog would not be in that list of windows? – Zelphir Kaltstahl May 21 '16 at 20:40
  • Note also that dialogs do not appear in any taskbar on my system. So if wnck uses some kind of taskbar checks, it will not work like this. – Zelphir Kaltstahl May 21 '16 at 20:45
  • I was able to get to run your example and now I am convinced, that there must be some other mistake in my pytest test, which makes it so, that the dialog does not appear in the list. Maybe I can find it working with your example. – Zelphir Kaltstahl May 22 '16 at 02:08
  • If you are looking for a dialogue linked to a specific application, you can test if `dialog_window.get_transient() == main_window`. – Jacques Gaudin May 22 '16 at 17:55
0

Taking partially from this answer by Eye of Hell that says how to get the open windows, you can do this:

from gtk import gdk

name = "MyDialog"

root = gdk.get_default_root_window()
matches = []
for id in root.property_get("_NET_CLIENT_LIST"):
    window = gdk.window_foreign_new(id)
    if window and window.property_get("WM_NAME")[2] == name:
        matches.append(window)

for match in matches:
    print(match, match.get_modal_hint())
Community
  • 1
  • 1
zondo
  • 19,901
  • 8
  • 44
  • 83
  • Sorry, but I am inquiring about dialogs and not windows. I already used the function in the OP to check for windows, but it doesn't work on dialogs (or there is another mistake somewhere). – Zelphir Kaltstahl May 21 '16 at 20:43