]> icculus.org git repositories - dana/openbox.git/blob - scripts/stackedcycle.py
include the desktop name if cycling on all desktops is enabled
[dana/openbox.git] / scripts / stackedcycle.py
1 ###########################################################################
2 ### Functions for cycling focus (in a 'stacked' order) between windows. ###
3 ###########################################################################
4
5 ###########################################################################
6 ###    Options that affect the behavior of the stackedcycle module.     ###
7 ###########################################################################
8 INCLUDE_ALL_DESKTOPS = 0
9 """If this is non-zero then windows from all desktops will be included in
10    the stacking list."""
11 INCLUDE_ICONS = 1
12 """If this is non-zero then windows which are iconified will be included
13    in the stacking list."""
14 INCLUDE_OMNIPRESENT = 1
15 """If this is non-zero then windows which are on all-desktops at once will
16    be included."""
17 TITLE_SIZE_LIMIT = 80
18 """This specifies a rough limit of characters for the cycling list titles.
19    Titles which are larger will be chopped with an elipsis in their
20    center."""
21 ACTIVATE_WHILE_CYCLING = 1
22 """If this is non-zero then windows will be activated as they are
23    highlighted in the cycling list (except iconified windows)."""
24 # See focus.AVOID_SKIP_TASKBAR
25 # See focuscycle.RAISE_WINDOW
26 ###########################################################################
27
28 def next(data):
29     """Focus the next window."""
30     if not data.state:
31         raise RuntimeError("stackedcycle.next must be bound to a key" +
32                            "combination with at least one modifier")
33     _o.cycle(data, 1)
34     
35 def previous(data):
36     """Focus the previous window."""
37     if not data.state:
38         raise RuntimeError("stackedcycle.previous must be bound to a key" +
39                            "combination with at least one modifier")
40     _o.cycle(data, 0)
41
42 ###########################################################################
43 ###########################################################################
44
45 ###########################################################################
46 ###      Internal stuff, should not be accessed outside the module.     ###
47 ###########################################################################
48
49 import otk
50 import ob
51 import focus
52 import focuscycle
53
54 class _cycledata:
55     def __init__(self):
56         self.cycling = 0
57
58     def createpopup(self):
59         self.widget = otk.Widget(self.screen.number(), ob.openbox,
60                                  otk.Widget.Vertical, 0, 1)
61
62     def destroypopup(self):
63         self.menuwidgets = []
64         self.widget = 0
65
66     def shouldadd(self, client):
67         """Determines if a client should be added to the list."""
68         curdesk = self.screen.desktop()
69         desk = client.desktop()
70
71         if not client.normal(): return 0
72         if not (client.canFocus() or client.focusNotify()): return 0
73         if focus.AVOID_SKIP_TASKBAR and client.skipTaskbar(): return 0
74
75         if INCLUDE_ICONS and client.iconic(): return 1
76         if INCLUDE_OMNIPRESENT and desk == 0xffffffff: return 1
77         if INCLUDE_ALL_DESKTOPS: return 1
78         if desk == curdesk: return 1
79
80         return 0
81
82     def populatelist(self):
83         """Populates self.clients and self.menuwidgets, and then shows and
84            positions the cycling popup."""
85
86         self.widget.hide()
87
88         try:
89             current = self.clients[self.menupos]
90         except IndexError: current = 0
91         oldpos = self.menupos
92         self.menupos = -1
93
94         # get the list of clients, keeping iconic windows at the bottom
95         self.clients = []
96         iconic_clients = []
97         for c in focus._clients:
98             if c.iconic(): iconic_clients.append(c)
99             else: self.clients.append(c)
100         self.clients.extend(iconic_clients)
101
102         # make the widgets
103         i = 0
104         self.menuwidgets = []
105         while i < len(self.clients):
106             c = self.clients[i]
107             if not self.shouldadd(c):
108                 # make the clients and menuwidgets lists match
109                 self.clients.pop(i) 
110                 continue
111             
112             w = otk.Label(self.widget)
113             if current and c.window() == current.window():
114                 self.menupos = i
115                 w.setHighlighted(1)
116             self.menuwidgets.append(w)
117
118             if c.iconic(): t = c.iconTitle()
119             else: t = c.title()
120
121             if INCLUDE_ALL_DESKTOPS:
122                 d = c.desktop()
123                 if d == 0xffffffff: d = self.screen.desktop()
124                 t = self.screen.desktopName(d) + " - " + t
125             
126             if len(t) > TITLE_SIZE_LIMIT: # limit the length of titles
127                 t = t[:TITLE_SIZE_LIMIT / 2 - 2] + "..." + \
128                     t[0 - TITLE_SIZE_LIMIT / 2 - 2:]
129             w.setText(t)
130
131             i += 1
132
133         # the window we were on may be gone
134         if self.menupos < 0:
135             # try stay at the same spot in the menu
136             if oldpos >= len(self.clients):
137                 self.menupos = len(self.clients) - 1
138             else:
139                 self.menupos = oldpos
140
141         # find the size for the popup
142         width = 0
143         height = 0
144         for w in self.menuwidgets:
145             size = w.minSize()
146             if size.width() > width: width = size.width()
147             height += size.height()
148         
149         # show or hide the list and its child widgets
150         if len(self.clients) > 1:
151             size = self.screeninfo.size()
152             self.widget.moveresize(otk.Rect((size.width() - width) / 2,
153                                             (size.height() - height) / 2,
154                                             width, height))
155             self.widget.show(1)
156
157     def activatetarget(self, final):
158         try:
159             client = self.clients[self.menupos]
160         except IndexError: return # empty list makes for this
161
162         # move the to client's desktop if required
163         if not (client.iconic() or client.desktop() == 0xffffffff or \
164                 client.desktop() == self.screen.desktop()):
165             root = self.screeninfo.rootWindow()
166             ob.send_client_msg(root, otk.atoms.net_current_desktop,
167                                root, client.desktop())
168         
169         # send a net_active_window message for the target
170         if final or not client.iconic():
171             if final: r = focuscycle.RAISE_WINDOW
172             else: r = 0
173             ob.send_client_msg(self.screeninfo.rootWindow(),
174                                otk.atoms.openbox_active_window,
175                                client.window(), final, r)
176             if not final:
177                 focus._skip += 1
178
179     def cycle(self, data, forward):
180         if not self.cycling:
181             ob.kgrab(data.screen, _grabfunc)
182             # the pointer grab causes pointer events during the keyboard grab
183             # to go away, which means we don't get enter notifies when the
184             # popup disappears, screwing up the focus
185             ob.mgrab(data.screen)
186
187             self.cycling = 1
188             self.state = data.state
189             self.screen = ob.openbox.screen(data.screen)
190             self.screeninfo = otk.display.screenInfo(data.screen)
191             self.menupos = 0
192             self.createpopup()
193             self.clients = [] # so it doesnt try start partway through the list
194             self.populatelist()
195         
196         if not len(self.clients): return # don't both doing anything
197         
198         self.menuwidgets[self.menupos].setHighlighted(0)
199         if forward:
200             self.menupos += 1
201         else:
202             self.menupos -= 1
203         # wrap around
204         if self.menupos < 0: self.menupos = len(self.clients) - 1
205         elif self.menupos >= len(self.clients): self.menupos = 0
206         self.menuwidgets[self.menupos].setHighlighted(1)
207         if ACTIVATE_WHILE_CYCLING:
208             self.activatetarget(0) # activate, but dont deiconify/unshade/raise
209
210     def grabfunc(self, data):
211         done = 0
212         notreverting = 1
213         # have all the modifiers this started with been released?
214         if (data.action == ob.KeyAction.Release and
215             not self.state & data.state):
216             done = 1
217         # has Escape been pressed?
218         elif data.action == ob.KeyAction.Press and data.key == "Escape":
219             done = 1
220             notreverting = 0
221             # revert
222             self.menupos = 0
223
224         if done:
225             # activate, and deiconify/unshade/raise
226             self.activatetarget(notreverting)
227             self.destroypopup()
228             self.cycling = 0
229             ob.kungrab()
230             ob.mungrab()
231
232 def _newwindow(data):
233     if _o.cycling: _o.populatelist()
234         
235 def _closewindow(data):
236     if _o.cycling: _o.populatelist()
237         
238 def _grabfunc(data):
239     _o.grabfunc(data)
240
241 ob.ebind(ob.EventAction.NewWindow, _newwindow)
242 ob.ebind(ob.EventAction.CloseWindow, _closewindow)
243
244 _o = _cycledata()
245
246 print "Loaded stackedcycle.py"