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