]> icculus.org git repositories - dana/openbox.git/blob - scripts/callbacks.py
restart and catch errors appropriately
[dana/openbox.git] / scripts / callbacks.py
1 ############################################################################
2 ### Functions that can be used as callbacks for mouse/keyboard bindings  ###
3 ############################################################################
4
5 import ob
6 import otk
7
8 def iconify(data):
9     """Iconifies the window on which the event occured"""
10     if not data.client: return
11     data.client.iconify(1)
12     
13 def restore(data):
14     """Un-iconifies the window on which the event occured, but does not focus
15        if. If you want to focus the window too, it is recommended that you
16        use the activate() function."""
17     if not data.client: return
18     data.client.iconify(0)
19     
20 def close(data):
21     """Closes the window on which the event occured"""
22     if not data.client: return
23     data.client.close()
24
25 def focus(data):
26     """Focuses the window on which the event occured"""
27     if not data.client: return
28     # !normal windows dont get focus from window enter events
29     if data.action == ob.EventAction.EnterWindow and not data.client.normal():
30         return
31     data.client.focus()
32
33 def raise_win(data):
34     """Raises the window on which the event occured"""
35     if not data.client: return
36     data.client.raiseWindow()
37
38 def lower_win(data):
39     """Lowers the window on which the event occured"""
40     if not data.client: return
41     data.client.lowerWindow()
42
43 def toggle_maximize(data):
44     """Toggles the maximized status of the window on which the event occured"""
45     if not data.client: return
46     data.client.maximize(not (data.client.maxHorz() or data.client.maxVert()))
47
48 def toggle_maximize_horz(data):
49     """Toggles the horizontal maximized status of the window on which the event
50        occured"""
51     if not data.client: return
52     data.client.maximizeHorizontal(not data.client.maxHorz())
53
54 def toggle_maximize_vert(data):
55     """Toggles the vertical maximized status of the window on which the event
56        occured"""
57     if not data.client: return
58     data.client.maximizeVertical(not data.client.maxVert())
59
60 def maximize(data):
61     """Maximizes the window on which the event occured"""
62     if not data.client: return
63     data.client.maximize(1)
64
65 def maximize_horz(data):
66     """Horizontally maximizes the window on which the event occured"""
67     if not data.client: return
68     data.client.maximizeHorizontal(1)
69
70 def maximize_vert(data):
71     """Vertically maximizes the window on which the event occured"""
72     if not data.client: return
73     data.client.maximizeVertical(1)
74
75 def unmaximize(data):
76     """Unmaximizes the window on which the event occured"""
77     if not data.client: return
78     data.client.maximize(0)
79
80 def unmaximize_horz(data):
81     """Horizontally unmaximizes the window on which the event occured"""
82     if not data.client: return
83     data.client.maximizeHorizontal(0)
84
85 def unmaximize_vert(data):
86     """Vertically unmaximizes the window on which the event occured"""
87     if not data.client: return
88     data.client.maximizeVertical(0)
89
90 def toggle_shade(data):
91     """Toggles the shade status of the window on which the event occured"""
92     if not data.client: return
93     data.client.shade(not data.client.shaded())
94
95 def shade(data):
96     """Shades the window on which the event occured"""
97     if not data.client: return
98     data.client.shade(1)
99
100 def unshade(data):
101     """Unshades the window on which the event occured"""
102     if not data.client: return
103     data.client.shade(0)
104
105 def change_desktop(data, num):
106     """Switches to a specified desktop"""
107     ob.openbox.screen(data.screen).changeDesktop(num)
108
109 def show_desktop(data, show=1):
110     """Shows and focuses the desktop, hiding any client windows. Optionally,
111        if show is zero, this will hide the desktop, leaving show-desktop
112        mode."""
113     ob.openbox.screen(data.screen).showDesktop(show)
114
115 def hide_desktop(data):
116     """Hides the desktop, re-showing the client windows. Leaves show-desktop
117        mode."""
118     show_desktop(data, 0)
119
120 def toggle_show_desktop(data):
121     """Requests the Openbox to show the desktop, hiding the client windows, or
122        redisplay the clients."""
123     screen = ob.openbox.screen(data.screen)
124     screen.showDesktop(not screen.showingDesktop())
125
126 def next_desktop(data, no_wrap=0):
127     """Switches to the next desktop, optionally (by default) cycling around to
128        the first when going past the last."""
129     screen = ob.openbox.screen(data.screen)
130     d = screen.desktop()
131     n = screen.numDesktops()
132     if (d < (n-1)):
133         d = d + 1
134     elif not no_wrap:
135         d = 0
136     change_desktop(data, d)
137     
138 def prev_desktop(data, no_wrap=0):
139     """Switches to the previous desktop, optionally (by default) cycling around
140        to the last when going past the first."""
141     screen = ob.openbox.screen(data.screen)
142     d = screen.desktop()
143     n = screen.numDesktops()
144     if (d > 0):
145         d = d - 1
146     elif not no_wrap:
147         d = n - 1
148     change_desktop(data, d)
149
150 def up_desktop(data, num=1):
151     """Switches to the desktop vertically above the current one. This is based
152        on the desktop layout chosen by an EWMH compliant pager. Optionally, num
153        can be specified to move more than one row at a time."""
154     screen = ob.openbox.screen(data.screen)
155     d = screen.desktop()
156     n = screen.numDesktops()
157     l = screen.desktopLayout()
158
159     target = d - num * l.columns
160     if target < 0:
161         target += l.rows * l.columns
162     while target >= n:
163         target -= l.columns
164     change_desktop(data, target)
165
166 def down_desktop(data, num=1):
167     """Switches to the desktop vertically below the current one. This is based
168        on the desktop layout chosen by an EWMH compliant pager. Optionally, num
169        can be specified to move more than one row at a time."""
170     screen = ob.openbox.screen(data.screen)
171     d = screen.desktop()
172     n = screen.numDesktops()
173     l = screen.desktopLayout()
174
175     target = d + num * l.columns
176     if target >= n:
177         target -= l.rows * l.columns
178     while target < 0:
179         target += l.columns
180     change_desktop(data, target)
181
182 def left_desktop(data, num=1):
183     """Switches to the desktop horizotally left of the current one. This is
184        based on the desktop layout chosen by an EWMH compliant pager.
185        Optionally, num can be specified to move more than one column at a
186        time."""
187     screen = ob.openbox.screen(data.screen)
188     d = screen.desktop()
189     n = screen.numDesktops()
190     l = screen.desktopLayout()
191
192     rowstart = d - d % l.columns
193     target = d - num
194     while target < rowstart:
195         target += l.columns
196     change_desktop(data, target)
197
198 def right_desktop(data, num=1):
199     """Switches to the desktop horizotally right of the current one. This is
200        based on the desktop layout chosen by an EWMH compliant pager.
201        Optionally, num can be specified to move more than one column at a
202        time."""
203     screen = ob.openbox.screen(data.screen)
204     d = screen.desktop()
205     n = screen.numDesktops()
206     l = screen.desktopLayout()
207
208     rowstart = d - d % l.columns
209     target = d + num
210     while target >= rowstart + l.columns:
211         target -= l.columns
212     change_desktop(data, target)
213
214 def send_to_desktop(data, num):
215     """Sends a client to a specified desktop"""
216     if not data.client: return
217     data.client.setDesktop(num)
218
219 def toggle_all_desktops(data):
220     """Toggles between sending a client to all desktops and to the current
221        desktop."""
222     if not data.client: return
223     if not data.client.desktop() == 0xffffffff:
224         send_to_desktop(data, 0xffffffff)
225     else:
226         send_to_desktop(data, ob.openbox.screen(data.screen).desktop())
227     
228 def send_to_all_desktops(data):
229     """Sends a client to all desktops"""
230     if not data.client: return
231     send_to_desktop(data, 0xffffffff)
232     
233 def send_to_next_desktop(data, no_wrap=0, follow=1):
234     """Sends a window to the next desktop, optionally (by default) cycling
235        around to the first when going past the last. Also optionally moving to
236        the new desktop after sending the window."""
237     if not data.client: return
238     screen = ob.openbox.screen(data.screen)
239     d = screen.desktop()
240     n = screen.numDesktops()
241     if (d < (n-1)):
242         d = d + 1
243     elif not no_wrap:
244         d = 0
245     send_to_desktop(data, d)
246     if follow:
247         change_desktop(data, d)
248     
249 def send_to_prev_desktop(data, no_wrap=0, follow=1):
250     """Sends a window to the previous desktop, optionally (by default) cycling
251        around to the last when going past the first. Also optionally moving to
252        the new desktop after sending the window."""
253     if not data.client: return
254     screen = ob.openbox.screen(data.screen)
255     d = screen.desktop()
256     n = screen.numDesktops()
257     if (d > 0):
258         d = d - 1
259     elif not no_wrap:
260         d = n - 1
261     send_to_desktop(data, d)
262     if follow:
263         change_desktop(data, d)
264
265 def restart(data=0, other = ""):
266     """Restarts Openbox, optionally starting another window manager."""
267     ob.openbox.restart(other)
268
269 def exit(data=0):
270     """Exits Openbox."""
271     ob.openbox.shutdown()
272
273 export_functions = iconify, restore, close, focus, raise_win, lower_win, \
274                    toggle_maximize, toggle_maximize_horz, \
275                    toggle_maximize_vert, maximize, maximize_horz, \
276                    maximize_vert, unmaximize, unmaximize_horz, \
277                    unmaximize_vert, toggle_shade, shade, unshade, \
278                    change_desktop, show_desktop, hide_desktop, \
279                    toggle_show_desktop, next_desktop, prev_desktop, \
280                    up_desktop, down_desktop, left_desktop, right_desktop, \
281                    send_to_desktop, toggle_all_desktops, send_to_all_desktops,\
282                    send_to_next_desktop, send_to_prev_desktop, restart, exit
283
284 print "Loaded callbacks.py"