1.1 --- a/desktop/dialog.py Thu Dec 13 21:41:26 2007 +0000
1.2 +++ b/desktop/dialog.py Sat Dec 15 18:32:41 2007 +0000
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Simple desktop dialogue box support for Python.
1.6
1.7 -Copyright (C) 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This library is free software; you can redistribute it and/or
1.11 modify it under the terms of the GNU Lesser General Public
1.12 @@ -38,6 +38,9 @@
1.13 question.open("GNOME") # Insists on GNOME
1.14
1.15 The dialogue box options are documented in each class's docstring.
1.16 +
1.17 +Available dialogue box classes are listed in the desktop.dialog.available
1.18 +attribute.
1.19 """
1.20
1.21 from desktop import use_desktop, _run, _readfrom, _status
2.1 --- a/desktop/windows.py Thu Dec 13 21:41:26 2007 +0000
2.2 +++ b/desktop/windows.py Sat Dec 15 18:32:41 2007 +0000
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Simple desktop window enumeration for Python.
2.6
2.7 -Copyright (C) 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This library is free software; you can redistribute it and/or
2.11 modify it under the terms of the GNU Lesser General Public
2.12 @@ -26,16 +26,107 @@
2.13
2.14 To obtain a list of windows, use the desktop.windows.list function as follows:
2.15
2.16 -desktop.windows.list()
2.17 +windows = desktop.windows.list()
2.18 +
2.19 +Each window object can be inspected through a number of methods. For example:
2.20 +
2.21 +name = window.name()
2.22 +width, height = window.size()
2.23 +x, y = window.position()
2.24 +child_windows = window.children()
2.25 +
2.26 +See the desktop.windows.Window class for more information.
2.27 """
2.28
2.29 from desktop import _is_x11, _get_x11_vars, _readfrom, use_desktop
2.30
2.31 +def _xwininfo(s):
2.32 + d = {}
2.33 + for line in s.split("\n"):
2.34 + fields = line.split(":")
2.35 + if len(fields) < 2:
2.36 + continue
2.37 + key, values = fields[0].strip(), fields[1:]
2.38 + d[key] = values
2.39 + return d
2.40 +
2.41 +def _get_int_properties(d, properties):
2.42 + results = []
2.43 + for property in properties:
2.44 + results.append(int(d[property][0].strip()))
2.45 + return results
2.46 +
2.47 +# Window classes.
2.48 +# NOTE: X11 is the only supported desktop so far.
2.49 +
2.50 +class Window:
2.51 +
2.52 + "A window on the desktop."
2.53 +
2.54 + def __init__(self, identifier):
2.55 +
2.56 + "Initialise the window with the given 'identifier'."
2.57 +
2.58 + self.identifier = identifier
2.59 +
2.60 + def __repr__(self):
2.61 + return "Window(%r)" % self.identifier
2.62 +
2.63 + def children(self):
2.64 +
2.65 + "Return a list of windows which are children of this window."
2.66 +
2.67 + s = _readfrom(_get_x11_vars() + "xwininfo -id %s -children" % self.identifier, shell=1)
2.68 + handles = []
2.69 + adding = 0
2.70 + for line in s.split("\n"):
2.71 + if not adding and line.endswith("children:"):
2.72 + adding = 1
2.73 + elif adding and line:
2.74 + handles.append(line.strip().split()[0])
2.75 + return [Window(handle) for handle in handles]
2.76 +
2.77 + def name(self):
2.78 +
2.79 + "Return the name of the window."
2.80 +
2.81 + s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1)
2.82 + for line in s.split("\n"):
2.83 + if line.startswith("xwininfo:"):
2.84 +
2.85 + # Format is 'xwininfo: Window id: <handle> "<name>"
2.86 +
2.87 + fields = line.split(":")
2.88 + handle_and_name = fields[2].strip()
2.89 + fields2 = handle_and_name.split(" ")
2.90 +
2.91 + # Get the "<name>" part, stripping off the quotes.
2.92 +
2.93 + return " ".join(fields2[1:]).strip('"')
2.94 +
2.95 + return None
2.96 +
2.97 + def size(self):
2.98 +
2.99 + "Return a tuple containing the width and height of this window."
2.100 +
2.101 + s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1)
2.102 + d = _xwininfo(s)
2.103 + return _get_int_properties(d, ["Width", "Height"])
2.104 +
2.105 + def position(self):
2.106 +
2.107 + "Return a tuple containing the upper left co-ordinates of this window."
2.108 +
2.109 + s = _readfrom(_get_x11_vars() + "xwininfo -id %s -stats" % self.identifier, shell=1)
2.110 + d = _xwininfo(s)
2.111 + return _get_int_properties(d, ["Absolute upper-left X", "Absolute upper-left Y"])
2.112 +
2.113 def list(desktop=None):
2.114
2.115 """
2.116 - Return a list of window handles for the current desktop. If the optional
2.117 - 'desktop' parameter is specified then attempt to use that particular desktop
2.118 + Return a list of windows for the current desktop. If the optional 'desktop'
2.119 + parameter is specified then attempt to use that particular desktop
2.120 environment's mechanisms to look for windows.
2.121 """
2.122
2.123 @@ -54,6 +145,6 @@
2.124 else:
2.125 raise OSError, "Desktop '%s' not supported" % use_desktop(desktop)
2.126
2.127 - return handles
2.128 + return [Window(handle) for handle in handles]
2.129
2.130 # vim: tabstop=4 expandtab shiftwidth=4