]> Dogcows Code - chaz/openbox/blob - scripts/stackedcycle.py
split out the linear cycling into focuscycle.py.
[chaz/openbox] / scripts / stackedcycle.py
1 ###########################################################################
2 ### Functions for cycling focus (in a 'stacked' order) between windows. ###
3 ###########################################################################
4
5 ###########################################################################
6 ### Options that affect the behavior of the stackedcycle module. ###
7 ###########################################################################
8 include_all_desktops = 0
9 """If this is non-zero then windows from all desktops will be included in
10 the stacking list."""
11 include_icons = 1
12 """If this is non-zero then windows which are iconified will be included
13 in the stacking list."""
14 include_omnipresent = 1
15 """If this is non-zero then windows which are on all-desktops at once will
16 be included."""
17 title_size_limit = 80
18 """This specifies a rough limit of characters for the cycling list titles.
19 Titles which are larger will be chopped with an elipsis in their
20 center."""
21 activate_while_cycling = 1
22 """If this is non-zero then windows will be activated as they are
23 highlighted in the cycling list (except iconified windows)."""
24 # See focus.avoid_skip_taskbar
25 # See focuscycle.raise_window
26 ###########################################################################
27
28 def next(data):
29 """Focus the next window."""
30 if not data.state:
31 raise RuntimeError("stackedcycle.next must be bound to a key" +
32 "combination with at least one modifier")
33 _o.cycle(data, 1)
34
35 def previous(data):
36 """Focus the previous window."""
37 if not data.state:
38 raise RuntimeError("stackedcycle.previous must be bound to a key" +
39 "combination with at least one modifier")
40 _o.cycle(data, 0)
41
42 ###########################################################################
43 ###########################################################################
44
45 ###########################################################################
46 ### Internal stuff, should not be accessed outside the module. ###
47 ###########################################################################
48
49 import otk
50 import ob
51 import focus
52 import focuscycle
53
54 class cycledata:
55 def __init__(self):
56 self.cycling = 0
57
58 def createpopup(self):
59 self.style = self.screen.style()
60 self.widget = otk.Widget(ob.openbox, self.style, otk.Widget.Vertical,
61 0, self.style.bevelWidth(), 1)
62 self.widget.setTexture(self.style.titlebarFocusBackground())
63
64 def destroypopup(self):
65 self.menuwidgets = []
66 self.widget = 0
67
68 def shouldadd(self, client):
69 """Determines if a client should be added to the list."""
70 curdesk = self.screen.desktop()
71 desk = client.desktop()
72
73 if not client.normal(): return 0
74 if not (client.canFocus() or client.focusNotify()): return 0
75 if focus.avoid_skip_taskbar and client.skipTaskbar(): return 0
76
77 if include_icons and client.iconic(): return 1
78 if include_omnipresent and desk == 0xffffffff: return 1
79 if include_all_desktops: return 1
80 if desk == curdesk: return 1
81
82 return 0
83
84 def populatelist(self):
85 """Populates self.clients and self.menuwidgets, and then shows and
86 positions the cycling popup."""
87
88 self.widget.hide()
89
90 try:
91 current = self.clients[self.menupos]
92 except IndexError: current = 0
93 oldpos = self.menupos
94 self.menupos = -1
95
96 # get the list of clients
97 self.clients = []
98 for i in focus._clients:
99 c = ob.openbox.findClient(i)
100 if c: self.clients.append(c)
101
102 font = self.style.labelFont()
103 longest = 0
104 height = font.height()
105
106 # make the widgets
107 i = 0
108 self.menuwidgets = []
109 while i < len(self.clients):
110 c = self.clients[i]
111 if not self.shouldadd(c):
112 # make the clients and menuwidgets lists match
113 self.clients.pop(i)
114 continue
115
116 w = otk.FocusLabel(self.widget)
117 if current and c.window() == current.window():
118 self.menupos = i
119 w.focus()
120 else:
121 w.unfocus()
122 self.menuwidgets.append(w)
123
124 if c.iconic(): t = c.iconTitle()
125 else: t = c.title()
126 if len(t) > title_size_limit: # limit the length of titles
127 t = t[:title_size_limit / 2 - 2] + "..." + \
128 t[0 - title_size_limit / 2 - 2:]
129 length = font.measureString(t)
130 if length > longest: longest = length
131 w.setText(t)
132
133 i += 1
134
135 # the window we were on may be gone
136 if self.menupos < 0:
137 # try stay at the same spot in the menu
138 if oldpos >= len(self.clients):
139 self.menupos = len(self.clients) - 1
140 else:
141 self.menupos = oldpos
142
143 # fit to the largest item in the menu
144 for w in self.menuwidgets:
145 w.fitSize(longest, height)
146
147 # show or hide the list and its child widgets
148 if len(self.clients) > 1:
149 area = self.screeninfo.rect()
150 self.widget.update()
151 self.widget.move(area.x() + (area.width() -
152 self.widget.width()) / 2,
153 area.y() + (area.height() -
154 self.widget.height()) / 2)
155 self.widget.show(1)
156
157 def activatetarget(self, final):
158 try:
159 client = self.clients[self.menupos]
160 except IndexError: return # empty list makes for this
161
162 # move the to client's desktop if required
163 if not (client.iconic() or client.desktop() == 0xffffffff or \
164 client.desktop() == self.screen.desktop()):
165 root = self.screeninfo.rootWindow()
166 ob.send_client_msg(root, otk.Property_atoms().net_current_desktop,
167 root, client.desktop())
168
169 # send a net_active_window message for the target
170 if final or not client.iconic():
171 if final: r = focuscycle.raise_window
172 else: r = 0
173 ob.send_client_msg(self.screeninfo.rootWindow(),
174 otk.Property_atoms().openbox_active_window,
175 client.window(), final, r)
176
177 def cycle(self, data, forward):
178 if not self.cycling:
179 self.cycling = 1
180 focus._disable = 1
181 self.state = data.state
182 self.screen = ob.openbox.screen(data.screen)
183 self.screeninfo = otk.display.screenInfo(data.screen)
184 self.menupos = 0
185 self.createpopup()
186 self.clients = [] # so it doesnt try start partway through the list
187 self.populatelist()
188
189 ob.kgrab(self.screen.number(), _grabfunc)
190 # the pointer grab causes pointer events during the keyboard grab
191 # to go away, which means we don't get enter notifies when the
192 # popup disappears, screwing up the focus
193 ob.mgrab(self.screen.number())
194
195 self.menuwidgets[self.menupos].unfocus()
196 if forward:
197 self.menupos += 1
198 else:
199 self.menupos -= 1
200 # wrap around
201 if self.menupos < 0: self.menupos = len(self.clients) - 1
202 elif self.menupos >= len(self.clients): self.menupos = 0
203 self.menuwidgets[self.menupos].focus()
204 if activate_while_cycling:
205 self.activatetarget(0) # activate, but dont deiconify/unshade/raise
206
207 def grabfunc(self, data):
208 done = 0
209 # have all the modifiers this started with been released?
210 if (data.action == ob.KeyAction.Release and
211 not self.state & data.state):
212 done = 1
213 # has Escape been pressed?
214 if data.action == ob.KeyAction.Press and data.key == "Escape":
215 done = 1
216 # revert
217 self.menupos = 0
218
219 if done:
220 self.cycling = 0
221 focus._disable = 0
222 self.activatetarget(1) # activate, and deiconify/unshade/raise
223 self.destroypopup()
224 ob.kungrab()
225 ob.mungrab()
226
227 def _newwindow(data):
228 if _o.cycling: _o.populatelist()
229
230 def _closewindow(data):
231 if _o.cycling: _o.populatelist()
232
233 def _grabfunc(data):
234 _o.grabfunc(data)
235
236 ob.ebind(ob.EventAction.NewWindow, _newwindow)
237 ob.ebind(ob.EventAction.CloseWindow, _closewindow)
238
239 _o = cycledata()
240
241 print "Loaded stackedcycle.py"
This page took 0.045331 seconds and 4 git commands to generate.