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