]> icculus.org git repositories - dana/openbox.git/blob - scripts/focus.py
focus cycling 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 cycle_popup_list = 1
14 # send focus somewhere when nothing is left with the focus, if possible ###
15 fallback = 0                                                            ###
16 ###                                                                     ###
17 ###########################################################################
18
19 import otk
20 import ob
21
22 # maintain a list of clients, stacked in focus order
23 _clients = []
24 # maintaint he current focused window
25 _doing_stacked = 0
26
27 def _new_win(data):
28     global _clients
29     global _doing_stacked
30     global _cyc_w;
31
32     if _doing_stacked:
33         _clients.insert(_clients.index(_cyc_w), data.client.window())
34     else:
35         if not len(_clients):
36             _clients.append(data.client.window())
37         else:
38             _clients.insert(1, data.client.window()) # insert in 2nd slot
39
40 def _close_win(data):
41     global _clients
42     global _cyc_w;
43     global _doing_stacked
44
45     if not _doing_stacked:
46         # not in the middle of stacked cycling, so who cares
47         _clients.remove(data.client.window())
48     else:
49         # have to fix the cycling if we remove anything
50         win = data.client.window()
51         if _cyc_w == win:
52             _do_stacked_cycle(data) # cycle off the window first
53         _clients.remove(win)
54
55 def _focused(data):
56     global _clients
57     global _doing_stacked
58     global _cyc_w
59     
60     if data.client:
61         if not _doing_stacked: # only move the window when we're not cycling
62             win = data.client.window()
63             # move it to the top
64             _clients.remove(win)
65             _clients.insert(0, win)
66         else: # if we are cycling, then update our pointer
67             _cyc_w = data.client.window()
68             global _list_widget, _list_labels, _list_windows
69             if _list_widget:
70                 i = 0
71                 for w in _list_windows:
72                     if w == _cyc_w: _list_labels[i].focus()
73                     else: _list_labels[i].unfocus()
74                     i += 1
75     elif fallback: 
76         # pass around focus
77         desktop = ob.openbox.screen(_cyc_screen).desktop()
78         for w in _clients:
79             client = ob.openbox.findClient(w)
80             if client and (client.desktop() == desktop and \
81                            client.normal() and client.focus()):
82                 break
83
84 _cyc_mask = 0
85 _cyc_key = 0
86 _cyc_w = 0 # last window cycled to
87 _cyc_screen = 0
88
89 def _do_stacked_cycle(data, forward):
90     global _cyc_w
91     global stacked_cycle_raise
92     global _clients
93
94     clients = _clients[:] # make a copy
95
96     if not forward:
97         clients.reverse()
98
99     try:
100         i = clients.index(_cyc_w) + 1
101     except ValueError:
102         i = 1
103     clients = clients[i:] + clients[:i]
104         
105     desktop = ob.openbox.screen(data.screen).desktop()
106     for w in clients:
107         client = ob.openbox.findClient(w)
108         if client and (client.desktop() == desktop and \
109                        client.normal() and client.focus()):
110             if stacked_cycle_raise:
111                 ob.openbox.screen(data.screen).raiseWindow(client)
112             return
113
114 def _focus_stacked_ungrab(data):
115     global _cyc_mask;
116     global _cyc_key;
117     global _doing_stacked;
118
119     if data.action == ob.KeyAction.Release:
120         # have all the modifiers this started with been released?
121         if not _cyc_mask & data.state:
122             ob.kungrab() # ungrab ourself
123             _doing_stacked = 0;
124             if cycle_raise:
125                 client = ob.openbox.findClient(_cyc_w)
126                 if client:
127                     ob.openbox.screen(data.screen).raiseWindow(client)
128             global _list_widget, _list_labels, _list_windows
129             if _list_widget:
130                 _list_windows = []
131                 _list_labels = []
132                 _list_widget = 0
133
134 _list_widget = 0
135 _list_labels = []
136 _list_windows = []
137
138 def focus_next_stacked(data, forward=1):
139     """Focus the next (or previous, with forward=0) window in a stacked
140        order."""
141     global _cyc_mask
142     global _cyc_key
143     global _cyc_w
144     global _cyc_screen
145     global _doing_stacked
146
147     if _doing_stacked:
148         if _cyc_key == data.key:
149             _do_stacked_cycle(data,forward)
150     else:
151         _cyc_mask = data.state
152         _cyc_key = data.key
153         _cyc_w = 0
154         _cyc_screen = data.screen
155         _doing_stacked = 1
156
157         global cycle_popup_list
158         if cycle_popup_list:
159             global _list_widget, _list_labels
160             if not _list_widget: # make the widget list
161                 style = ob.openbox.screen(data.screen).style()
162                 _list_widget = otk.Widget(ob.openbox, style,
163                                           otk.Widget.Vertical, 0,
164                                           style.bevelWidth(), 1)
165                 t = style.labelFocusBackground()
166                 _list_widget.setTexture(t)
167
168                 titles = []
169                 font = style.labelFont()
170                 height = font.height()
171                 longest = 0
172                 for c in _clients:
173                     client = ob.openbox.findClient(c)
174                     screen = ob.openbox.screen(data.screen)
175                     desktop = screen.desktop()
176                     if client and (client.desktop() == desktop and \
177                                    client.normal()):
178                         t = client.title()
179                         titles.append(t)
180                         _list_windows.append(c)
181                         l = font.measureString(t)
182                         if l > longest: longest = l
183                 if len(titles):
184                     for t in titles:
185                         w = otk.FocusLabel(_list_widget)
186                         w.resize(longest, height)
187                         w.setText(t)
188                         w.unfocus()
189                         _list_labels.append(w)
190                     _list_labels[0].focus()
191                     _list_widget.update()
192                     area = screen.area()
193                     _list_widget.move(area.x() + (area.width() -
194                                                   _list_widget.width()) / 2,
195                                       area.y() + (area.height() -
196                                                   _list_widget.height()) / 2)
197                     _list_widget.show(1)
198                 else:
199                     _list_widget = 0 #nothing to list
200
201         ob.kgrab(data.screen, _focus_stacked_ungrab)
202         focus_next_stacked(data, forward) # start with the first press
203
204 def focus_prev_stacked(data):
205     """Focus the previous window in a stacked order."""
206     focus_next_stacked(data, forward=0)
207
208 def focus_next(data, num=1, forward=1):
209     """Focus the next (or previous, with forward=0) window in a linear
210        order."""
211     screen = ob.openbox.screen(data.screen)
212     count = screen.clientCount()
213
214     if not count: return # no clients
215     
216     target = 0
217     if data.client:
218         client_win = data.client.window()
219         found = 0
220         r = range(count)
221         if not forward:
222             r.reverse()
223         for i in r:
224             if found:
225                 target = i
226                 found = 2
227                 break
228             elif screen.client(i).window() == client_win:
229                 found = 1
230         if found == 1: # wraparound
231             if forward: target = 0
232             else: target = count - 1
233
234     t = target
235     curdesk = screen.desktop()
236     while 1:
237         client = screen.client(t)
238         if client.normal() and \
239                (client.desktop() == curdesk or client.desktop() == 0xffffffff)\
240                and client.focus():
241             if cycle_raise:
242                 screen.raiseWindow(client)
243             return
244         if forward:
245             t += num
246             if t >= count: t -= count
247         else:
248             t -= num
249             if t < 0: t += count
250         if t == target: return # nothing to focus
251
252 def focus_prev(data, num=1):
253     """Focus the previous window in a linear order."""
254     focus_next(data, num, forward=0)
255
256
257 ob.ebind(ob.EventAction.NewWindow, _new_win)
258 ob.ebind(ob.EventAction.CloseWindow, _close_win)
259 ob.ebind(ob.EventAction.Focus, _focused)
260
261 print "Loaded focus.py"