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