]> Dogcows Code - chaz/openbox/blob - scripts/focus.py
feb65cca8f29c692065a500428b3da6cf6c88ed7
[chaz/openbox] / scripts / focus.py
1 ###########################################################################
2 ### Functions for helping out with your window focus. ###
3 ###########################################################################
4
5 ###########################################################################
6 ### Options that affect the behavior of the focus module. ###
7 ### ###
8 # raise the window also when it is focused ###
9 cycle_raise = 1 ###
10 # raise as you cycle in stacked mode ###
11 stacked_cycle_raise = 0 ###
12 # show a pop-up list of windows while cycling ###
13 stacked_cycle_popup_list = 1 ###
14 # send focus somewhere when nothing is left with the focus, if possible ###
15 fallback = 0 ###
16 ### ###
17 ### ###
18 # Provides: ###
19 # def focus_next_stacked(data, forward=1): ###
20 # """Focus the next (or previous, with forward=0) window in a stacked ###
21 # order.""" ###
22 # def focus_prev_stacked(data): ###
23 # """Focus the previous window in a stacked order.""" ###
24 # def focus_next(data, num=1, forward=1): ###
25 # """Focus the next (or previous, with forward=0) window in a linear ###
26 # order.""" ###
27 # def focus_prev(data, num=1): ###
28 # """Focus the previous window in a linear order.""" ###
29 ### ###
30 # All of these functions call be used as callbacks for bindings ###
31 # directly. ###
32 ### ###
33 ###########################################################################
34
35 import otk
36 import ob
37
38 # maintain a list of clients, stacked in focus order
39 _clients = []
40 # maintaint he current focused window
41 _doing_stacked = 0
42
43 def _new_win(data):
44 global _clients
45 global _doing_stacked
46 global _cyc_w;
47
48 if _doing_stacked:
49 _clients.insert(_clients.index(_cyc_w), data.client.window())
50 _create_popup_list(data)
51 _hilite_popup_list(data)
52 else:
53 if not len(_clients):
54 _clients.append(data.client.window())
55 else:
56 _clients.insert(1, data.client.window()) # insert in 2nd slot
57
58 def _close_win(data):
59 global _clients
60 global _cyc_w;
61 global _doing_stacked
62
63 if not _doing_stacked:
64 # not in the middle of stacked cycling, so who cares
65 _clients.remove(data.client.window())
66 else:
67 # have to fix the cycling if we remove anything
68 win = data.client.window()
69 if _cyc_w == win:
70 _do_stacked_cycle(data, 1) # cycle off the window first, forward
71 _clients.remove(win)
72 _create_popup_list(data)
73
74 def _focused(data):
75 global _clients
76 global _doing_stacked
77 global _cyc_w
78
79 if data.client:
80 if not _doing_stacked: # only move the window when we're not cycling
81 win = data.client.window()
82 # move it to the top
83 _clients.remove(win)
84 _clients.insert(0, win)
85 else: # if we are cycling, then update our pointer
86 _cyc_w = data.client.window()
87 _hilite_popup_list(data)
88 elif fallback:
89 # pass around focus
90 desktop = ob.openbox.screen(_cyc_screen).desktop()
91 for w in _clients:
92 client = ob.openbox.findClient(w)
93 if client and (client.desktop() == desktop and \
94 client.normal() and client.focus()):
95 break
96 if _doing_stacked:
97 _cyc_w = 0
98 _hilite_popup_list(data)
99
100 _cyc_mask = 0
101 _cyc_key = 0
102 _cyc_w = 0 # last window cycled to
103 _cyc_screen = 0
104
105 def _do_stacked_cycle(data, forward):
106 global _cyc_w
107 global stacked_cycle_raise
108 global _clients
109
110 clients = _clients[:] # make a copy
111
112 if not forward:
113 clients.reverse()
114
115 try:
116 i = clients.index(_cyc_w) + 1
117 except ValueError:
118 i = 1
119 clients = clients[i:] + clients[:i]
120
121 desktop = ob.openbox.screen(data.screen).desktop()
122 for w in clients:
123 client = ob.openbox.findClient(w)
124 if client and (client.desktop() == desktop or
125 client.desktop() == 0xffffffff) \
126 and client.normal() and client.focus():
127 if stacked_cycle_raise:
128 ob.openbox.screen(data.screen).raiseWindow(client)
129 return
130
131 def _focus_stacked_ungrab(data):
132 global _cyc_mask;
133 global _cyc_key;
134 global _doing_stacked;
135
136 if data.action == ob.KeyAction.Release:
137 # have all the modifiers this started with been released?
138 if not _cyc_mask & data.state:
139 _destroy_popup_list()
140 ob.kungrab()
141 ob.mungrab()
142 _doing_stacked = 0;
143 if cycle_raise:
144 client = ob.openbox.findClient(_cyc_w)
145 if client:
146 ob.openbox.screen(data.screen).raiseWindow(client)
147
148 _list_widget = 0
149 _list_labels = []
150 _list_windows = []
151
152 def _hilite_popup_list(data):
153 global _cyc_w, _doing_stacked
154 global _list_widget, _list_labels, _list_windows
155 found = 0
156
157 if not _list_widget and _doing_stacked:
158 _create_popup_list(data)
159
160 if _list_widget:
161 i = 0
162 for w in _list_windows:
163 if w == _cyc_w:
164 _list_labels[i].focus()
165 found = 1
166 else:
167 _list_labels[i].unfocus()
168 i += 1
169 if not found:
170 _create_popup_list(data)
171
172 def _destroy_popup_list():
173 global _list_widget, _list_labels, _list_windows
174 if _list_widget:
175 _list_windows = []
176 _list_labels = []
177 _list_widget = 0
178
179 def _create_popup_list(data):
180 global _list_widget, _list_labels, _list_windows, _clients
181
182 if _list_widget:
183 _destroy_popup_list()
184
185 style = ob.openbox.screen(data.screen).style()
186 _list_widget = otk.Widget(ob.openbox, style,
187 otk.Widget.Vertical, 0,
188 style.bevelWidth(), 1)
189 t = style.titlebarFocusBackground()
190 _list_widget.setTexture(t)
191
192 titles = []
193 font = style.labelFont()
194 height = font.height()
195 longest = 0
196 for c in _clients:
197 client = ob.openbox.findClient(c)
198 desktop = ob.openbox.screen(data.screen).desktop()
199 if client and ((client.desktop() == desktop or
200 client.desktop() == 0xffffffff) and \
201 client.normal() and (client.canFocus() or
202 client.focusNotify())):
203 t = client.title()
204 if len(t) > 50: # limit the length of titles
205 t = t[:24] + "..." + t[-24:]
206 titles.append(t)
207 _list_windows.append(c)
208 l = font.measureString(t)
209 if l > longest: longest = l
210 if len(titles) > 1:
211 for t in titles:
212 w = otk.FocusLabel(_list_widget)
213 w.fitSize(longest, height)
214 w.setText(t)
215 w.unfocus()
216 _list_labels.append(w)
217 _list_widget.update()
218 area = otk.display.screenInfo(data.screen).rect()
219 _list_widget.move(area.x() + (area.width() -
220 _list_widget.width()) / 2,
221 area.y() + (area.height() -
222 _list_widget.height()) / 2)
223 _list_widget.show(1)
224 else:
225 _destroy_popup_list() # nothing (or only 1) to list
226
227 def focus_next_stacked(data, forward=1):
228 """Focus the next (or previous, with forward=0) window in a stacked
229 order."""
230 global _cyc_mask
231 global _cyc_key
232 global _cyc_w
233 global _cyc_screen
234 global _doing_stacked
235
236 if _doing_stacked:
237 if _cyc_key == data.key:
238 _do_stacked_cycle(data,forward)
239 else:
240 _cyc_mask = data.state
241 _cyc_key = data.key
242 _cyc_w = 0
243 _cyc_screen = data.screen
244 _doing_stacked = 1
245
246 global stacked_cycle_popup_list
247 if stacked_cycle_popup_list:
248 _create_popup_list(data)
249
250 ob.kgrab(data.screen, _focus_stacked_ungrab)
251 # the pointer grab causes pointer events during the keyboard grab to
252 # go away, which means we don't get enter notifies when the popup
253 # disappears, screwing up the focus
254 ob.mgrab(data.screen)
255 focus_next_stacked(data, forward) # start with the first press
256
257 def focus_prev_stacked(data):
258 """Focus the previous window in a stacked order."""
259 focus_next_stacked(data, forward=0)
260
261 def focus_next(data, num=1, forward=1):
262 """Focus the next (or previous, with forward=0) window in a linear
263 order."""
264 screen = ob.openbox.screen(data.screen)
265 count = screen.clientCount()
266
267 if not count: return # no clients
268
269 target = 0
270 if data.client:
271 client_win = data.client.window()
272 found = 0
273 r = range(count)
274 if not forward:
275 r.reverse()
276 for i in r:
277 if found:
278 target = i
279 found = 2
280 break
281 elif screen.client(i).window() == client_win:
282 found = 1
283 if found == 1: # wraparound
284 if forward: target = 0
285 else: target = count - 1
286
287 t = target
288 curdesk = screen.desktop()
289 while 1:
290 client = screen.client(t)
291 if client.normal() and \
292 (client.desktop() == curdesk or client.desktop() == 0xffffffff)\
293 and client.focus():
294 if cycle_raise:
295 screen.raiseWindow(client)
296 return
297 if forward:
298 t += num
299 if t >= count: t -= count
300 else:
301 t -= num
302 if t < 0: t += count
303 if t == target: return # nothing to focus
304
305 def focus_prev(data, num=1):
306 """Focus the previous window in a linear order."""
307 focus_next(data, num, forward=0)
308
309
310 ob.ebind(ob.EventAction.NewWindow, _new_win)
311 ob.ebind(ob.EventAction.CloseWindow, _close_win)
312 ob.ebind(ob.EventAction.Focus, _focused)
313
314 print "Loaded focus.py"
This page took 0.048997 seconds and 4 git commands to generate.