use client msg's to toggle shaded
[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     client = Openbox_findClient(openbox, data.window())
8     if not client: return
9     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
10     window = OBClient_window(client)
11     above = OBProperty_atom(Openbox_property(openbox),
12                             OBProperty_net_wm_state_above)
13     send_client_msg(root, OBProperty_net_wm_state, window, add,
14                     above)
15     
16 def state_below(data, add=2):
17     """Toggles, adds or removes the 'below' state on a window."""
18     client = Openbox_findClient(openbox, data.window())
19     if not client: return
20     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
21     window = OBClient_window(client)
22     below = OBProperty_atom(Openbox_property(openbox),
23                             OBProperty_net_wm_state_below)
24     send_client_msg(root, OBProperty_net_wm_state, window, add,
25                     below)
26     
27 def state_shaded(data, add=2):
28     """Toggles, adds or removes the 'shaded' state on a window."""
29     client = Openbox_findClient(openbox, data.window())
30     if not client: return
31     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
32     window = OBClient_window(client)
33     shaded = OBProperty_atom(Openbox_property(openbox),
34                             OBProperty_net_wm_state_shaded)
35     send_client_msg(root, OBProperty_net_wm_state, window, add,
36                     shaded)
37     
38 def close(data):
39     """Closes the window on which the event occured"""
40     client = Openbox_findClient(openbox, data.window())
41     if client: OBClient_close(client)
42
43 def focus(data):
44     """Focuses the window on which the event occured"""
45     client = Openbox_findClient(openbox, data.window())
46     if not client: return
47     type = OBClient_type(client)
48     # !normal windows dont get focus from window enter events
49     if data.action() == EventEnterWindow and not OBClient_normal(client):
50         return
51     OBClient_focus(client)
52
53 def move(data):
54     """Moves the window interactively. This should only be used with
55        MouseMotion events"""
56     client = Openbox_findClient(openbox, data.window())
57     if not client: return
58
59     # !normal windows dont get moved
60     if not OBClient_normal(client): return
61
62     dx = data.xroot() - data.pressx()
63     dy = data.yroot() - data.pressy()
64     OBClient_move(client, data.press_clientx() + dx, data.press_clienty() + dy)
65
66 def resize(data):
67     """Resizes the window interactively. This should only be used with
68        MouseMotion events"""
69     client = Openbox_findClient(openbox, data.window())
70     if not client: return
71
72     # !normal windows dont get moved
73     if not OBClient_normal(client): return
74
75     px = data.pressx()
76     py = data.pressy()
77     dx = data.xroot() - px
78     dy = data.yroot() - py
79
80     # pick a corner to anchor
81     if not (resize_nearest or data.context() == MC_Grip):
82         corner = OBClient_TopLeft
83     else:
84         x = px - data.press_clientx()
85         y = py - data.press_clienty()
86         if y < data.press_clientheight() / 2:
87             if x < data.press_clientwidth() / 2:
88                 corner = OBClient_BottomRight
89                 dx *= -1
90             else:
91                 corner = OBClient_BottomLeft
92             dy *= -1
93         else:
94             if x < data.press_clientwidth() / 2:
95                 corner = OBClient_TopRight
96                 dx *= -1
97             else:
98                 corner = OBClient_TopLeft
99
100     OBClient_resize(client, corner,
101                     data.press_clientwidth() + dx,
102                     data.press_clientheight() + dy);
103
104 def restart(data):
105     """Restarts openbox"""
106     Openbox_restart(openbox, "")
107
108 def raise_win(data):
109     """Raises the window on which the event occured"""
110     client = Openbox_findClient(openbox, data.window())
111     if not client: return
112     screen = Openbox_screen(openbox, OBClient_screen(client))
113     OBScreen_restack(screen, 1, client)
114
115 def lower_win(data):
116     """Lowers the window on which the event occured"""
117     client = Openbox_findClient(openbox, data.window())
118     if not client: return
119     screen = Openbox_screen(openbox, OBClient_screen(client))
120     OBScreen_restack(screen, 0, client)
121
122 def toggle_shade(data):
123     """Toggles the shade status of the window on which the event occured"""
124     state_shaded(data)
125
126 def shade(data):
127     """Shades the window on which the event occured"""
128     state_shaded(data, 1)
129
130 def unshade(data):
131     """Unshades the window on which the event occured"""
132     state_shaded(data, 0)
133
134 def change_desktop(data, num):
135     """Switches to a specified desktop"""
136     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
137     send_client_msg(root, OBProperty_net_current_desktop, root, num)
138
139 def next_desktop(data, no_wrap=0):
140     """Switches to the next desktop, optionally (by default) cycling around to
141        the first when going past the last."""
142     screen = Openbox_screen(openbox, data.screen())
143     d = OBScreen_desktop(screen)
144     n = OBScreen_numDesktops(screen)
145     if (d < (n-1)):
146         d = d + 1
147     elif not no_wrap:
148         d = 0
149     change_desktop(data, d)
150     
151 def prev_desktop(data, no_wrap=0):
152     """Switches to the previous desktop, optionally (by default) cycling around
153        to the last when going past the first."""
154     screen = Openbox_screen(openbox, data.screen())
155     d = OBScreen_desktop(screen)
156     n = OBScreen_numDesktops(screen)
157     if (d > 0):
158         d = d - 1
159     elif not no_wrap:
160         d = n - 1
161     change_desktop(data, d)
162
163 def send_to_desktop(data, num):
164     """Sends a client to a specified desktop"""
165     root = ScreenInfo_rootWindow(OBDisplay_screenInfo(data.screen()))
166     client = Openbox_findClient(openbox, data.window())
167     if client:
168         window = OBClient_window(client)
169         send_client_msg(root, OBProperty_net_wm_desktop, window, num)
170
171 def send_to_next_desktop(data, no_wrap=0, follow=1):
172     """Sends a window to the next desktop, optionally (by default) cycling
173        around to the first when going past the last. Also optionally moving to
174        the new desktop after sending the window."""
175     client = Openbox_findClient(openbox, data.window())
176     if not client: return
177     screen = Openbox_screen(openbox, data.screen())
178     d = OBScreen_desktop(screen)
179     n = OBScreen_numDesktops(screen)
180     if (d < (n-1)):
181         d = d + 1
182     elif not no_wrap:
183         d = 0
184     send_to_desktop(data, d)
185     if follow:
186         change_desktop(data, d)
187     
188 def send_to_prev_desktop(data, no_wrap=0, follow=1):
189     """Sends a window to the previous desktop, optionally (by default) cycling
190        around to the last when going past the first. Also optionally moving to
191        the new desktop after sending the window."""
192     client = Openbox_findClient(openbox, data.window())
193     if not client: return
194     screen = Openbox_screen(openbox, data.screen())
195     d = OBScreen_desktop(screen)
196     n = OBScreen_numDesktops(screen)
197     if (d > 0):
198         d = d - 1
199     elif not no_wrap:
200         d = n - 1
201     send_to_desktop(data, d)
202     if follow:
203         change_desktop(data, d)
204
205 #########################################
206 ### Convenience functions for scripts ###
207 #########################################
208
209 def execute(bin, screen = 0):
210     """Executes a command on the specified screen. It is recommended that you
211        use this call instead of a python system call. If the specified screen
212        is beyond your range of screens, the default is used instead."""
213     Openbox_execute(openbox, screen, bin)
214
215 print "Loaded builtins.py"