]> Dogcows Code - chaz/openbox/blob - scripts/stackedcycle.py
kill a warning
[chaz/openbox] / 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 if len(t) > TITLE_SIZE_LIMIT: # limit the length of titles
121 t = t[:TITLE_SIZE_LIMIT / 2 - 2] + "..." + \
122 t[0 - TITLE_SIZE_LIMIT / 2 - 2:]
123 w.setText(t)
124
125 i += 1
126
127 # the window we were on may be gone
128 if self.menupos < 0:
129 # try stay at the same spot in the menu
130 if oldpos >= len(self.clients):
131 self.menupos = len(self.clients) - 1
132 else:
133 self.menupos = oldpos
134
135 # find the size for the popup
136 width = 0
137 height = 0
138 for w in self.menuwidgets:
139 size = w.minSize()
140 if size.width() > width: width = size.width()
141 height += size.height()
142
143 # show or hide the list and its child widgets
144 if len(self.clients) > 1:
145 size = self.screeninfo.size()
146 self.widget.moveresize(otk.Rect((size.width() - width) / 2,
147 (size.height() - height) / 2,
148 width, height))
149 self.widget.show(1)
150
151 def activatetarget(self, final):
152 try:
153 client = self.clients[self.menupos]
154 except IndexError: return # empty list makes for this
155
156 # move the to client's desktop if required
157 if not (client.iconic() or client.desktop() == 0xffffffff or \
158 client.desktop() == self.screen.desktop()):
159 root = self.screeninfo.rootWindow()
160 ob.send_client_msg(root, otk.atoms.net_current_desktop,
161 root, client.desktop())
162
163 # send a net_active_window message for the target
164 if final or not client.iconic():
165 if final: r = focuscycle.RAISE_WINDOW
166 else: r = 0
167 ob.send_client_msg(self.screeninfo.rootWindow(),
168 otk.atoms.openbox_active_window,
169 client.window(), final, r)
170 if not final:
171 focus._skip += 1
172
173 def cycle(self, data, forward):
174 if not self.cycling:
175 ob.kgrab(data.screen, _grabfunc)
176 # the pointer grab causes pointer events during the keyboard grab
177 # to go away, which means we don't get enter notifies when the
178 # popup disappears, screwing up the focus
179 ob.mgrab(data.screen)
180
181 self.cycling = 1
182 self.state = data.state
183 self.screen = ob.openbox.screen(data.screen)
184 self.screeninfo = otk.display.screenInfo(data.screen)
185 self.menupos = 0
186 self.createpopup()
187 self.clients = [] # so it doesnt try start partway through the list
188 self.populatelist()
189
190 if not len(self.clients): return # don't both doing anything
191
192 self.menuwidgets[self.menupos].setHighlighted(0)
193 if forward:
194 self.menupos += 1
195 else:
196 self.menupos -= 1
197 # wrap around
198 if self.menupos < 0: self.menupos = len(self.clients) - 1
199 elif self.menupos >= len(self.clients): self.menupos = 0
200 self.menuwidgets[self.menupos].setHighlighted(1)
201 if ACTIVATE_WHILE_CYCLING:
202 self.activatetarget(0) # activate, but dont deiconify/unshade/raise
203
204 def grabfunc(self, data):
205 done = 0
206 notreverting = 1
207 # have all the modifiers this started with been released?
208 if (data.action == ob.KeyAction.Release and
209 not self.state & data.state):
210 done = 1
211 # has Escape been pressed?
212 elif data.action == ob.KeyAction.Press and data.key == "Escape":
213 done = 1
214 notreverting = 0
215 # revert
216 self.menupos = 0
217
218 if done:
219 # activate, and deiconify/unshade/raise
220 self.activatetarget(notreverting)
221 self.destroypopup()
222 self.cycling = 0
223 ob.kungrab()
224 ob.mungrab()
225
226 def _newwindow(data):
227 if _o.cycling: _o.populatelist()
228
229 def _closewindow(data):
230 if _o.cycling: _o.populatelist()
231
232 def _grabfunc(data):
233 _o.grabfunc(data)
234
235 ob.ebind(ob.EventAction.NewWindow, _newwindow)
236 ob.ebind(ob.EventAction.CloseWindow, _closewindow)
237
238 _o = _cycledata()
239
240 print "Loaded stackedcycle.py"
This page took 0.046932 seconds and 4 git commands to generate.