]> icculus.org git repositories - dana/openbox.git/blob - scripts/stackedcycle.py
store pointers instead of window id's. this lets us use them directly instead of...
[dana/openbox.git] / 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, keeping iconic windows at the bottom
97         self.clients = []
98         iconic_clients = []
99         for c in focus._clients:
100             if c.iconic(): iconic_clients.append(c)
101             else: self.clients.append(c)
102         self.clients.extend(iconic_clients)
103
104         font = self.style.labelFont()
105         longest = 0
106         height = font.height()
107             
108         # make the widgets
109         i = 0
110         self.menuwidgets = []
111         while i < len(self.clients):
112             c = self.clients[i]
113             if not self.shouldadd(c):
114                 # make the clients and menuwidgets lists match
115                 self.clients.pop(i) 
116                 continue
117             
118             w = otk.FocusLabel(self.widget)
119             if current and c.window() == current.window():
120                 self.menupos = i
121                 w.focus()
122             else:
123                 w.unfocus()
124             self.menuwidgets.append(w)
125
126             if c.iconic(): t = c.iconTitle()
127             else: t = c.title()
128             if len(t) > TITLE_SIZE_LIMIT: # limit the length of titles
129                 t = t[:TITLE_SIZE_LIMIT / 2 - 2] + "..." + \
130                     t[0 - TITLE_SIZE_LIMIT / 2 - 2:]
131             length = font.measureString(t)
132             if length > longest: longest = length
133             w.setText(t)
134
135             i += 1
136
137         # the window we were on may be gone
138         if self.menupos < 0:
139             # try stay at the same spot in the menu
140             if oldpos >= len(self.clients):
141                 self.menupos = len(self.clients) - 1
142             else:
143                 self.menupos = oldpos
144
145         # fit to the largest item in the menu
146         for w in self.menuwidgets:
147             w.fitSize(longest, height)
148
149         # show or hide the list and its child widgets
150         if len(self.clients) > 1:
151             area = self.screeninfo.rect()
152             self.widget.update()
153             self.widget.move(area.x() + (area.width() -
154                                          self.widget.width()) / 2,
155                              area.y() + (area.height() -
156                                          self.widget.height()) / 2)
157             self.widget.show(1)
158
159     def activatetarget(self, final):
160         try:
161             client = self.clients[self.menupos]
162         except IndexError: return # empty list makes for this
163
164         # move the to client's desktop if required
165         if not (client.iconic() or client.desktop() == 0xffffffff or \
166                 client.desktop() == self.screen.desktop()):
167             root = self.screeninfo.rootWindow()
168             ob.send_client_msg(root, otk.Property_atoms().net_current_desktop,
169                                root, client.desktop())
170         
171         # send a net_active_window message for the target
172         if final or not client.iconic():
173             if final: r = focuscycle.RAISE_WINDOW
174             else: r = 0
175             ob.send_client_msg(self.screeninfo.rootWindow(),
176                                otk.Property_atoms().openbox_active_window,
177                                client.window(), final, r)
178
179     def cycle(self, data, forward):
180         if not self.cycling:
181             self.cycling = 1
182             focus._disable = 1
183             self.state = data.state
184             self.screen = ob.openbox.screen(data.screen)
185             self.screeninfo = otk.display.screenInfo(data.screen)
186             self.menupos = 0
187             self.createpopup()
188             self.clients = [] # so it doesnt try start partway through the list
189             self.populatelist()
190         
191             ob.kgrab(self.screen.number(), _grabfunc)
192             # the pointer grab causes pointer events during the keyboard grab
193             # to go away, which means we don't get enter notifies when the
194             # popup disappears, screwing up the focus
195             ob.mgrab(self.screen.number())
196
197         if not len(self.clients): return # don't both doing anything
198         
199         self.menuwidgets[self.menupos].unfocus()
200         if forward:
201             self.menupos += 1
202         else:
203             self.menupos -= 1
204         # wrap around
205         if self.menupos < 0: self.menupos = len(self.clients) - 1
206         elif self.menupos >= len(self.clients): self.menupos = 0
207         self.menuwidgets[self.menupos].focus()
208         if ACTIVATE_WHILE_CYCLING:
209             self.activatetarget(0) # activate, but dont deiconify/unshade/raise
210
211     def grabfunc(self, data):
212         done = 0
213         notreverting = 1
214         # have all the modifiers this started with been released?
215         if (data.action == ob.KeyAction.Release and
216             not self.state & data.state):
217             done = 1
218         # has Escape been pressed?
219         elif data.action == ob.KeyAction.Press and data.key == "Escape":
220             done = 1
221             notreverting = 0
222             # revert
223             self.menupos = 0
224
225         if done:
226             self.cycling = 0
227             focus._disable = 0
228             # activate, and deiconify/unshade/raise
229             self.activatetarget(notreverting)
230             self.destroypopup()
231             ob.kungrab()
232             ob.mungrab()
233
234 def _newwindow(data):
235     if _o.cycling: _o.populatelist()
236         
237 def _closewindow(data):
238     if _o.cycling: _o.populatelist()
239         
240 def _grabfunc(data):
241     _o.grabfunc(data)
242
243 ob.ebind(ob.EventAction.NewWindow, _newwindow)
244 ob.ebind(ob.EventAction.CloseWindow, _closewindow)
245
246 _o = _cycledata()
247
248 print "Loaded stackedcycle.py"