]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/focus.py
import config
[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     if data.client:
94         if not _doing_stacked: # only move the window when we're not cycling
95             win = data.client.window()
96             # move it to the top
97             _clients.remove(win)
98             _clients.insert(0, win)
99         else: # if we are cycling, then update our pointer
100             _cyc_w = data.client.window()
101             _hilite_popup_list(data)
102     elif fallback: 
103         # pass around focus
104         desktop = ob.openbox.screen(_cyc_screen).desktop()
105         for w in _clients:
106             client = ob.openbox.findClient(w)
107             if client and _focusable(client, desktop) and client.focus():
108                 break
109         if _doing_stacked:
110             _cyc_w = 0
111             _hilite_popup_list(data)
112
113 _cyc_mask = 0
114 _cyc_key = 0
115 _cyc_w = 0 # last window cycled to
116 _cyc_screen = 0
117
118 def _do_stacked_cycle(data, forward):
119     global _cyc_w
120     global stacked_cycle_raise
121     global _clients
122
123     clients = _clients[:] # make a copy
124
125     if not forward:
126         clients.reverse()
127
128     try:
129         i = clients.index(_cyc_w) + 1
130     except ValueError:
131         i = 1
132     clients = clients[i:] + clients[:i]
133         
134     desktop = ob.openbox.screen(data.screen).desktop()
135     for w in clients:
136         client = ob.openbox.findClient(w)
137                    
138         if client and _focusable(client, desktop) and client.focus():
139             if stacked_cycle_raise:
140                 ob.openbox.screen(data.screen).raiseWindow(client)
141             return
142
143 def _focus_stacked_ungrab(data):
144     global _cyc_mask;
145     global _cyc_key;
146     global _doing_stacked;
147
148     if data.action == ob.KeyAction.Release:
149         # have all the modifiers this started with been released?
150         if not _cyc_mask & data.state:
151             _destroy_popup_list()
152             ob.kungrab()
153             ob.mungrab()
154             _doing_stacked = 0;
155             if cycle_raise:
156                 client = ob.openbox.findClient(_cyc_w)
157                 if client:
158                     ob.openbox.screen(data.screen).raiseWindow(client)
159
160 _list_widget = 0
161 _list_labels = []
162 _list_windows = []
163
164 def _hilite_popup_list(data):
165     global _cyc_w, _doing_stacked
166     global _list_widget, _list_labels, _list_windows
167     found = 0
168
169     if not _list_widget and _doing_stacked:
170         _create_popup_list(data)
171     
172     if _list_widget:
173         i = 0
174         for w in _list_windows:
175             if w == _cyc_w:
176                 _list_labels[i].focus()
177                 found = 1
178             else:
179                 _list_labels[i].unfocus()
180             i += 1
181     if not found:
182         _create_popup_list(data)
183
184 def _destroy_popup_list():
185     global _list_widget, _list_labels, _list_windows
186     if _list_widget:
187         _list_windows = []
188         _list_labels = []
189         _list_widget = 0
190     
191 def _create_popup_list(data):
192     global avoid_skip_taskbar
193     global _list_widget, _list_labels, _list_windows, _clients
194
195     if _list_widget:
196         _destroy_popup_list()
197     
198     style = ob.openbox.screen(data.screen).style()
199     _list_widget = otk.Widget(ob.openbox, style,
200                               otk.Widget.Vertical, 0,
201                               style.bevelWidth(), 1)
202     t = style.titlebarFocusBackground()
203     _list_widget.setTexture(t)
204
205     titles = []
206     font = style.labelFont()
207     height = font.height()
208     longest = 0
209     desktop = ob.openbox.screen(data.screen).desktop()
210     for c in _clients:
211         client = ob.openbox.findClient(c)
212         if client and _focusable(client, desktop):
213             t = client.title()
214             if len(t) > 50: # limit the length of titles
215                 t = t[:24] + "..." + t[-24:]
216             titles.append(t)
217             _list_windows.append(c)
218             l = font.measureString(t)
219             if l > longest: longest = l
220     if len(titles) > 1:
221         for t in titles:
222             w = otk.FocusLabel(_list_widget)
223             w.fitSize(longest, height)
224             w.setText(t)
225             w.unfocus()
226             _list_labels.append(w)
227         _list_widget.update()
228         area = otk.display.screenInfo(data.screen).rect()
229         _list_widget.move(area.x() + (area.width() -
230                                       _list_widget.width()) / 2,
231                           area.y() + (area.height() -
232                                       _list_widget.height()) / 2)
233         _list_widget.show(1)
234     else:
235         _destroy_popup_list() # nothing (or only 1) to list
236
237 def focus_next_stacked(data, forward=1):
238     """Focus the next (or previous, with forward=0) window in a stacked
239        order."""
240     global _cyc_mask
241     global _cyc_key
242     global _cyc_w
243     global _cyc_screen
244     global _doing_stacked
245
246     if _doing_stacked:
247         if _cyc_key == data.key:
248             _do_stacked_cycle(data,forward)
249     else:
250         _cyc_mask = data.state
251         _cyc_key = data.key
252         _cyc_w = 0
253         _cyc_screen = data.screen
254         _doing_stacked = 1
255
256         global stacked_cycle_popup_list
257         if stacked_cycle_popup_list:
258             _create_popup_list(data)
259
260         ob.kgrab(data.screen, _focus_stacked_ungrab)
261         # the pointer grab causes pointer events during the keyboard grab to
262         # go away, which means we don't get enter notifies when the popup
263         # disappears, screwing up the focus
264         ob.mgrab(data.screen)
265         focus_next_stacked(data, forward) # start with the first press
266
267 def focus_prev_stacked(data):
268     """Focus the previous window in a stacked order."""
269     focus_next_stacked(data, forward=0)
270
271 def focus_next(data, num=1, forward=1):
272     """Focus the next (or previous, with forward=0) window in a linear
273        order."""
274     global avoid_skip_taskbar
275
276     screen = ob.openbox.screen(data.screen)
277     count = screen.clientCount()
278
279     if not count: return # no clients
280     
281     target = 0
282     if data.client:
283         client_win = data.client.window()
284         found = 0
285         r = range(count)
286         if not forward:
287             r.reverse()
288         for i in r:
289             if found:
290                 target = i
291                 found = 2
292                 break
293             elif screen.client(i).window() == client_win:
294                 found = 1
295         if found == 1: # wraparound
296             if forward: target = 0
297             else: target = count - 1
298
299     t = target
300     desktop = screen.desktop()
301     while 1:
302         client = screen.client(t)
303         if client and _focusable(client, desktop) and client.focus():
304             if cycle_raise:
305                 screen.raiseWindow(client)
306             return
307         if forward:
308             t += num
309             if t >= count: t -= count
310         else:
311             t -= num
312             if t < 0: t += count
313         if t == target: return # nothing to focus
314
315 def focus_prev(data, num=1):
316     """Focus the previous window in a linear order."""
317     focus_next(data, num, forward=0)
318
319
320 ob.ebind(ob.EventAction.NewWindow, _new_win)
321 ob.ebind(ob.EventAction.CloseWindow, _close_win)
322 ob.ebind(ob.EventAction.Focus, _focused)
323
324 print "Loaded focus.py"