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