]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/builtins.py
new python interface! using the .py shadow wrappers from swig
[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(OBDisplay_screenInfo(data.screen).rootWindow(),
9                     OBProperty.net_wm_state, data.client.window(), add,
10                     openbox.property().atom(OBProperty.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(OBDisplay_screenInfo(data.screen).rootWindow(),
16                     OBProperty.net_wm_state, data.client.window(), add,
17                     openbox.property().atom(OBProperty.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(OBDisplay_screenInfo(data.screen).rootWindow(),
23                     OBProperty.net_wm_state, data.client,window(), add,
24                     openbox.property().atom(OBProperty.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(OBDisplay_screenInfo(data.screen).rootWindow(),
30                     OBProperty.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 = OBClient.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 = OBClient.BottomRight
74                 dx *= -1
75             else:
76                 corner = OBClient.BottomLeft
77             dy *= -1
78         else:
79             if x < data.press_clientwidth / 2:
80                 corner = OBClient.TopRight
81                 dx *= -1
82             else:
83                 corner = OBClient.TopLeft
84
85     data.client.resize(corner,
86                        data.press_clientwidth + dx,
87                        data.press_clientheight + dy);
88
89 def restart(data):
90     """Restarts openbox"""
91     openbox.restart("")
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).restack(1, 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).restack(0, 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 = OBDisplay_screenInfo(data.screen).rootWindow()
118     send_client_msg(root, OBProperty.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_desktop(data, num):
145     """Sends a client to a specified desktop"""
146     if not data.client: return
147     send_client_msg(OBDisplay_screenInfo(data.screen).rootWindow(),
148                     OBProperty.net_wm_desktop, data.client.window(), num)
149
150 def send_to_next_desktop(data, no_wrap=0, follow=1):
151     """Sends a window to the next desktop, optionally (by default) cycling
152        around to the first when going past the last. Also optionally moving to
153        the new desktop after sending the window."""
154     if not data.client: return
155     screen = openbox.screen(data.screen)
156     d = screen.desktop()
157     n = screen.numDesktops()
158     if (d < (n-1)):
159         d = d + 1
160     elif not no_wrap:
161         d = 0
162     send_to_desktop(data, d)
163     if follow:
164         change_desktop(data, d)
165     
166 def send_to_prev_desktop(data, no_wrap=0, follow=1):
167     """Sends a window to the previous desktop, optionally (by default) cycling
168        around to the last when going past the first. Also optionally moving to
169        the new desktop after sending the window."""
170     if not data.client: return
171     screen = openbox.screen(data.screen)
172     d = screen.desktop()
173     n = screen.numDesktops()
174     if (d > 0):
175         d = d - 1
176     elif not no_wrap:
177         d = n - 1
178     send_to_desktop(data, d)
179     if follow:
180         change_desktop(data, d)
181
182 #########################################
183 ### Convenience functions for scripts ###
184 #########################################
185
186 def execute(bin, screen = 0):
187     """Executes a command on the specified screen. It is recommended that you
188        use this call instead of a python system call. If the specified screen
189        is beyond your range of screens, the default is used instead."""
190     openbox.execute(screen, bin)
191
192 print "Loaded builtins.py"