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