]> icculus.org git repositories - dana/openbox.git/blob - scripts/focus.py
add windows on all-desktops to the cycle list
[dana/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 # 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 and \
125                        client.normal() and client.focus()):
126             if stacked_cycle_raise:
127                 ob.openbox.screen(data.screen).raiseWindow(client)
128             return
129
130 def _focus_stacked_ungrab(data):
131     global _cyc_mask;
132     global _cyc_key;
133     global _doing_stacked;
134
135     if data.action == ob.KeyAction.Release:
136         # have all the modifiers this started with been released?
137         if not _cyc_mask & data.state:
138             _destroy_popup_list()
139             ob.kungrab() # ungrab ourself
140             _doing_stacked = 0;
141             if cycle_raise:
142                 client = ob.openbox.findClient(_cyc_w)
143                 if client:
144                     ob.openbox.screen(data.screen).raiseWindow(client)
145
146 _list_widget = 0
147 _list_labels = []
148 _list_windows = []
149
150 def _hilite_popup_list(data):
151     global _cyc_w, _doing_stacked
152     global _list_widget, _list_labels, _list_windows
153     found = 0
154
155     if not _list_widget and _doing_stacked:
156         _create_popup_list(data)
157     
158     if _list_widget:
159         i = 0
160         for w in _list_windows:
161             if w == _cyc_w:
162                 _list_labels[i].focus()
163                 found = 1
164             else:
165                 _list_labels[i].unfocus()
166             i += 1
167     if not found:
168         _create_popup_list(data)
169
170 def _destroy_popup_list():
171     global _list_widget, _list_labels, _list_windows
172     if _list_widget:
173         _list_windows = []
174         _list_labels = []
175         _list_widget = 0
176     
177 def _create_popup_list(data):
178     global _list_widget, _list_labels, _list_windows, _clients
179
180     if _list_widget:
181         _destroy_popup_list()
182     
183     style = ob.openbox.screen(data.screen).style()
184     _list_widget = otk.Widget(ob.openbox, style,
185                               otk.Widget.Vertical, 0,
186                               style.bevelWidth(), 1)
187     t = style.titlebarFocusBackground()
188     _list_widget.setTexture(t)
189
190     titles = []
191     font = style.labelFont()
192     height = font.height()
193     longest = 0
194     for c in _clients:
195         client = ob.openbox.findClient(c)
196         desktop = ob.openbox.screen(data.screen).desktop()
197         if client and ((client.desktop() == desktop or
198                         client.desktop() == 0xffffffff) and \
199                        client.normal()):
200             t = client.title()
201             if len(t) > 50: # limit the length of titles
202                 t = t[:24] + "..." + t[-24:]
203             titles.append(t)
204             _list_windows.append(c)
205             l = font.measureString(t) + 10 # add margin
206             if l > longest: longest = l
207     if len(titles) > 1:
208         for t in titles:
209             w = otk.FocusLabel(_list_widget)
210             w.resize(longest, height)
211             w.setText(t)
212             w.unfocus()
213             _list_labels.append(w)
214         _list_widget.update()
215         area = otk.display.screenInfo(data.screen).rect()
216         _list_widget.move(area.x() + (area.width() -
217                                       _list_widget.width()) / 2,
218                           area.y() + (area.height() -
219                                       _list_widget.height()) / 2)
220         _list_widget.show(1)
221     else:
222         _destroy_popup_list() # nothing (or only 1) to list
223
224 def focus_next_stacked(data, forward=1):
225     """Focus the next (or previous, with forward=0) window in a stacked
226        order."""
227     global _cyc_mask
228     global _cyc_key
229     global _cyc_w
230     global _cyc_screen
231     global _doing_stacked
232
233     if _doing_stacked:
234         if _cyc_key == data.key:
235             _do_stacked_cycle(data,forward)
236     else:
237         _cyc_mask = data.state
238         _cyc_key = data.key
239         _cyc_w = 0
240         _cyc_screen = data.screen
241         _doing_stacked = 1
242
243         global stacked_cycle_popup_list
244         if stacked_cycle_popup_list:
245             _create_popup_list(data)
246
247         ob.kgrab(data.screen, _focus_stacked_ungrab)
248         focus_next_stacked(data, forward) # start with the first press
249
250 def focus_prev_stacked(data):
251     """Focus the previous window in a stacked order."""
252     focus_next_stacked(data, forward=0)
253
254 def focus_next(data, num=1, forward=1):
255     """Focus the next (or previous, with forward=0) window in a linear
256        order."""
257     screen = ob.openbox.screen(data.screen)
258     count = screen.clientCount()
259
260     if not count: return # no clients
261     
262     target = 0
263     if data.client:
264         client_win = data.client.window()
265         found = 0
266         r = range(count)
267         if not forward:
268             r.reverse()
269         for i in r:
270             if found:
271                 target = i
272                 found = 2
273                 break
274             elif screen.client(i).window() == client_win:
275                 found = 1
276         if found == 1: # wraparound
277             if forward: target = 0
278             else: target = count - 1
279
280     t = target
281     curdesk = screen.desktop()
282     while 1:
283         client = screen.client(t)
284         if client.normal() and \
285                (client.desktop() == curdesk or client.desktop() == 0xffffffff)\
286                and client.focus():
287             if cycle_raise:
288                 screen.raiseWindow(client)
289             return
290         if forward:
291             t += num
292             if t >= count: t -= count
293         else:
294             t -= num
295             if t < 0: t += count
296         if t == target: return # nothing to focus
297
298 def focus_prev(data, num=1):
299     """Focus the previous window in a linear order."""
300     focus_next(data, num, forward=0)
301
302
303 ob.ebind(ob.EventAction.NewWindow, _new_win)
304 ob.ebind(ob.EventAction.CloseWindow, _close_win)
305 ob.ebind(ob.EventAction.Focus, _focused)
306
307 print "Loaded focus.py"