]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/builtins.py
add frame.. wrapper changes.
[mikachu/openbox.git] / scripts / builtins.py
1 ###########################################################################
2 ### Functions that can be used as callbacks for mouse/keyboard bindings ###
3 ###########################################################################
4
5 def state_above(data, add=2):
6     """Toggles, adds or removes the 'above' state on a window."""
7     if not data.client: return
8     send_client_msg(display.screenInfo(data.screen).rootWindow(),
9                     Property_atoms().net_wm_state, data.client.window(), add,
10                     Property_atoms().net_wm_state_above)
11     
12 def state_below(data, add=2):
13     """Toggles, adds or removes the 'below' state on a window."""
14     if not data.client: return
15     send_client_msg(display.screenInfo(data.screen).rootWindow(),
16                     Property_atoms().net_wm_state, data.client.window(), add,
17                     Property_atoms().net_wm_state_below)
18     
19 def state_shaded(data, add=2):
20     """Toggles, adds or removes the 'shaded' state on a window."""
21     if not data.client: return
22     send_client_msg(display.screenInfo(data.screen).rootWindow(),
23                     Property_atoms().net_wm_state, data.client.window(), add,
24                     Property_atoms().net_wm_state_shaded)
25     
26 def close(data):
27     """Closes the window on which the event occured"""
28     if not data.client: return
29     send_client_msg(display.screenInfo(data.screen).rootWindow(),
30                     Property_atoms().net_close_window, data.client.window(), 0)
31
32 def focus(data):
33     """Focuses the window on which the event occured"""
34     if not data.client: return
35     # !normal windows dont get focus from window enter events
36     if data.action == EventEnterWindow and not data.client.normal():
37         return
38     data.client.focus()
39
40 def move(data):
41     """Moves the window interactively. This should only be used with
42        MouseMotion events"""
43     if not data.client: return
44
45     # !normal windows dont get moved
46     if not data.client.normal(): return
47
48     dx = data.xroot - data.pressx
49     dy = data.yroot - data.pressy
50     data.client.move(data.press_clientx + dx, data.press_clienty + dy)
51
52 def resize(data):
53     """Resizes the window interactively. This should only be used with
54        MouseMotion events"""
55     if not data.client: return
56
57     # !normal windows dont get moved
58     if not data.client.normal(): return
59
60     px = data.pressx
61     py = data.pressy
62     dx = data.xroot - px
63     dy = data.yroot - py
64
65     # pick a corner to anchor
66     if not (resize_nearest or data.context == MC_Grip):
67         corner = Client.TopLeft
68     else:
69         x = px - data.press_clientx
70         y = py - data.press_clienty
71         if y < data.press_clientheight / 2:
72             if x < data.press_clientwidth / 2:
73                 corner = Client.BottomRight
74                 dx *= -1
75             else:
76                 corner = Client.BottomLeft
77             dy *= -1
78         else:
79             if x < data.press_clientwidth / 2:
80                 corner = Client.TopRight
81                 dx *= -1
82             else:
83                 corner = Client.TopLeft
84
85     data.client.resize(corner,
86                        data.press_clientwidth + dx,
87                        data.press_clientheight + dy);
88
89 def restart(data, other = ""):
90     """Restarts openbox, optionally starting another window manager."""
91     openbox.restart(other)
92
93 def raise_win(data):
94     """Raises the window on which the event occured"""
95     if not data.client: return
96     openbox.screen(data.screen).raiseWindow(data.client)
97
98 def lower_win(data):
99     """Lowers the window on which the event occured"""
100     if not data.client: return
101     openbox.screen(data.screen).lowerWindow(data.client)
102
103 def toggle_shade(data):
104     """Toggles the shade status of the window on which the event occured"""
105     state_shaded(data)
106
107 def shade(data):
108     """Shades the window on which the event occured"""
109     state_shaded(data, 1)
110
111 def unshade(data):
112     """Unshades the window on which the event occured"""
113     state_shaded(data, 0)
114
115 def change_desktop(data, num):
116     """Switches to a specified desktop"""
117     root = display.screenInfo(data.screen).rootWindow()
118     send_client_msg(root, Property_atoms().net_current_desktop, root, num)
119
120 def next_desktop(data, no_wrap=0):
121     """Switches to the next desktop, optionally (by default) cycling around to
122        the first when going past the last."""
123     screen = openbox.screen(data.screen)
124     d = screen.desktop()
125     n = screen.numDesktops()
126     if (d < (n-1)):
127         d = d + 1
128     elif not no_wrap:
129         d = 0
130     change_desktop(data, d)
131     
132 def prev_desktop(data, no_wrap=0):
133     """Switches to the previous desktop, optionally (by default) cycling around
134        to the last when going past the first."""
135     screen = openbox.screen(data.screen)
136     d = screen.desktop()
137     n = screen.numDesktops()
138     if (d > 0):
139         d = d - 1
140     elif not no_wrap:
141         d = n - 1
142     change_desktop(data, d)
143
144 def send_to_all_desktops(data):
145     """Sends a client to all desktops"""
146     if not data.client: return
147     send_client_msg(display.screenInfo(data.screen).rootWindow(),
148                     Property_atoms().net_wm_desktop, data.client.window(),
149                     0xffffffff)
150     
151 def send_to_desktop(data, num):
152     """Sends a client to a specified desktop"""
153     if not data.client: return
154     send_client_msg(display.screenInfo(data.screen).rootWindow(),
155                     Property_atoms().net_wm_desktop, data.client.window(), num)
156
157 def send_to_next_desktop(data, no_wrap=0, follow=1):
158     """Sends a window to the next desktop, optionally (by default) cycling
159        around to the first when going past the last. Also optionally moving to
160        the new desktop after sending the window."""
161     if not data.client: return
162     screen = openbox.screen(data.screen)
163     d = screen.desktop()
164     n = screen.numDesktops()
165     if (d < (n-1)):
166         d = d + 1
167     elif not no_wrap:
168         d = 0
169     send_to_desktop(data, d)
170     if follow:
171         change_desktop(data, d)
172     
173 def send_to_prev_desktop(data, no_wrap=0, follow=1):
174     """Sends a window to the previous desktop, optionally (by default) cycling
175        around to the last when going past the first. Also optionally moving to
176        the new desktop after sending the window."""
177     if not data.client: return
178     screen = openbox.screen(data.screen)
179     d = screen.desktop()
180     n = screen.numDesktops()
181     if (d > 0):
182         d = d - 1
183     elif not no_wrap:
184         d = n - 1
185     send_to_desktop(data, d)
186     if follow:
187         change_desktop(data, d)
188
189 #########################################
190 ### Convenience functions for scripts ###
191 #########################################
192
193 def execute(bin, screen = 0):
194     """Executes a command on the specified screen. It is recommended that you
195        use this call instead of a python system call. If the specified screen
196        is beyond your range of screens, the default is used instead."""
197     openbox.execute(screen, bin)
198
199 def setup_click_focus(click_raise = 1):
200     """Sets up for focusing windows by clicking on or in the window.
201        Optionally, clicking on or in a window can raise the window to the
202        front of its stacking layer."""
203     mbind("Left", MC_Titlebar, MousePress, focus)
204     mbind("Left", MC_Handle, MousePress, focus)
205     mbind("Left", MC_Grip, MousePress, focus)
206     mbind("Left", MC_Window, MousePress, focus)
207     if click_raise:
208         mbind("Left", MC_Titlebar, MousePress, raise_win)
209         mbind("Left", MC_Handle, MousePress, raise_win)
210         mbind("Left", MC_Grip, MousePress, raise_win)
211         mbind("Left", MC_Window, MousePress, raise_win)
212
213 def setup_sloppy_focus(click_focus = 1, click_raise = 0):
214     """Sets up for focusing windows when the mouse pointer enters them.
215        Optionally, clicking on or in a window can focus it if your pointer
216        ends up inside a window without focus. Also, optionally, clicking on or
217        in a window can raise the window to the front of its stacking layer."""
218     ebind(EventEnterWindow, focus)
219     if click_focus:
220         setup_click_focus(click_raise)
221
222 def setup_window_clicks():
223     """Sets up the default bindings for various mouse buttons for various
224        contexts.
225        This includes:
226         * Alt-left drag anywhere on a window will move it
227         * Alt-right drag anywhere on a window will resize it
228         * Left drag on a window's titlebar/handle will move it
229         * Left drag on a window's handle grips will resize it
230         * Alt-left press anywhere on a window's will raise it to the front of
231           its stacking layer.
232         * Left press on a window's titlebar/handle will raise it to the front
233           of its stacking layer.
234         * Alt-middle click anywhere on a window's will lower it to the bottom
235           of its stacking layer.
236         * Middle click on a window's titlebar/handle will lower it to the
237           bottom of its stacking layer.
238         * Double-left click on a window's titlebar will toggle shading it
239     """
240     mbind("A-Left", MC_Frame, MouseMotion, move)
241     mbind("Left", MC_Titlebar, MouseMotion, move)
242     mbind("Left", MC_Handle, MouseMotion, move)
243
244     mbind("A-Right", MC_Frame, MouseMotion, resize)
245     mbind("Left", MC_Grip, MouseMotion, resize)
246
247     mbind("Left", MC_Titlebar, MousePress, raise_win)
248     mbind("Left", MC_Handle, MousePress, raise_win)
249     mbind("A-Left", MC_Frame, MousePress, raise_win)
250     mbind("A-Middle", MC_Frame, MouseClick, lower_win)
251     mbind("Middle", MC_Titlebar, MouseClick, lower_win)
252     mbind("Middle", MC_Handle, MouseClick, lower_win)
253
254     mbind("Left", MC_Titlebar, MouseDoubleClick, toggle_shade)
255
256 def setup_window_buttons():
257     """Sets up the default behaviors for the buttons in the window titlebar."""
258     mbind("Left", MC_CloseButton, MouseClick, close)
259
260 def setup_scroll():
261     """Sets up the default behaviors for the mouse scroll wheel.
262        This includes:
263         * scrolling on a window titlebar will shade/unshade it
264         * alt-scrolling anywhere will switch to the next/previous desktop
265         * control-alt-scrolling on a window will send it to the next/previous
266           desktop, and switch to the desktop with the window
267     """
268     mbind("Up", MC_Titlebar, MouseClick, shade)
269     mbind("Down", MC_Titlebar, MouseClick, unshade)
270
271     mbind("A-Up", MC_Frame, MouseClick, next_desktop)
272     mbind("A-Up", MC_Root, MouseClick, next_desktop)
273     mbind("A-Down", MC_Frame, MouseClick, prev_desktop)
274     mbind("A-Down", MC_Root, MouseClick, prev_desktop)
275
276     mbind("C-A-Up", MC_Frame, MouseClick, send_to_next_desktop)
277     mbind("C-A-Down", MC_Frame, MouseClick, send_to_prev_desktop)
278
279 def setup_fallback_focus():
280     """Sets up a focus fallback routine so that when no windows are focused,
281        the last window to have focus on the desktop will be focused."""
282     focus_stack = []
283     def focused(data):
284         #global focus_stack
285         if data.client:
286             window = data.client.window()
287             # add to front the stack
288             if window in focus_stack:
289                 focus_stack.remove(window)
290             focus_stack.insert(0, window)
291         else:
292             # pass around focus
293             desktop = openbox.screen(data.screen).desktop()
294             l = len(focus_stack)
295             i = 0
296             while i < l:
297                 w = focus_stack[i]
298                 client = openbox.findClient(w)
299                 if not client: # window is gone, remove it
300                     focus_stack.pop(i)
301                     l = l - 1
302                 elif client.desktop() == desktop and \
303                          client.normal() and client.focus():
304                     break
305                 else:
306                     i = i + 1
307
308     ebind(EventFocus, focused)
309
310     
311 ############################################################################
312 ### Window placement algorithms, choose one of these and ebind it to the ###
313 ### EventPlaceWindow action.                                             ###
314 ############################################################################
315
316 ob_rand = None
317 import random
318 def placewindows_random(data):
319     if not data.client: return
320     client_area = data.client.area()
321     frame_size = data.client.frame.size()
322     screen = display.screenInfo(data.screen)
323     width = screen.width() - (client_area.width() +
324                               frame_size.left + frame_size.right)
325     height = screen.height() - (client_area.height() + 
326                                 frame_size.top + frame_size.bottom)
327     global ob_rand
328     if not ob_rand: ob_rand = random.Random()
329     x = ob_rand.randrange(0, width-1)
330     y = ob_rand.randrange(0, height-1)
331     data.client.move(x, y)
332
333
334 print "Loaded builtins.py"