]> icculus.org git repositories - dana/openbox.git/blob - scripts/callbacks.py
new scripts for new script structure
[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
7 def state_above(data, add=2):
8     """Toggles, adds or removes the 'above' state on a window."""
9     if not data.client: return
10     ob.send_client_msg(ob.display.screenInfo(data.screen).rootWindow(),
11                        ob.Property_atoms().net_wm_state, data.client.window(),
12                        add, ob.Property_atoms().net_wm_state_above)
13     
14 def state_below(data, add=2):
15     """Toggles, adds or removes the 'below' state on a window."""
16     if not data.client: return
17     ob.send_client_msg(ob.display.screenInfo(data.screen).rootWindow(),
18                        ob.Property_atoms().net_wm_state, data.client.window(),
19                        add, ob.Property_atoms().net_wm_state_below)
20     
21 def state_shaded(data, add=2):
22     """Toggles, adds or removes the 'shaded' state on a window."""
23     if not data.client: return
24     ob.send_client_msg(ob.display.screenInfo(data.screen).rootWindow(),
25                        ob.Property_atoms().net_wm_state, data.client.window(),
26                        add, ob.Property_atoms().net_wm_state_shaded)
27
28 def iconify(data):
29     """Iconifies the window on which the event occured"""
30     if not data.client: return
31     ob.send_client_msg(ob.display.screenInfo(data.screen).rootWindow(),
32                        ob.Property_atoms().wm_change_state,
33                        data.client.window(), 3) # IconicState
34     
35 def restore(data):
36     """Un-iconifies the window on which the event occured, but does not focus
37        if. If you want to focus the window too, it is recommended that you
38        use the activate() function."""
39     if not data.client: return
40     ob.send_client_msg(ob.display.screenInfo(data.screen).rootWindow(),
41                        ob.Property_atoms().wm_change_state,
42                        data.client.window(), 1) # NormalState
43     
44 def close(data):
45     """Closes the window on which the event occured"""
46     if not data.client: return
47     ob.send_client_msg(ob.display.screenInfo(data.screen).rootWindow(),
48                        ob.Property_atoms().net_close_window,
49                        data.client.window(), 0)
50
51 def focus(data):
52     """Focuses the window on which the event occured"""
53     if not data.client: return
54     # !normal windows dont get focus from window enter events
55     if data.action == ob.EventAction.EnterWindow and not data.client.normal():
56         return
57     data.client.focus()
58
59 def move(data):
60     """Moves the window interactively. This should only be used with
61        MouseMotion events"""
62     if not data.client: return
63
64     # not-normal windows dont get moved
65     if not data.client.normal(): return
66
67     dx = data.xroot - data.pressx
68     dy = data.yroot - data.pressy
69     data.client.move(data.press_clientx + dx, data.press_clienty + dy)
70
71 def resize(data):
72     """Resizes the window interactively. This should only be used with
73        MouseMotion events"""
74     if not data.client: return
75
76     # not-normal windows dont get resized
77     if not data.client.normal(): return
78
79     px = data.pressx
80     py = data.pressy
81     dx = data.xroot - px
82     dy = data.yroot - py
83
84     # pick a corner to anchor
85     if not (resize_nearest or data.context == MC_Grip):
86         corner = Client.TopLeft
87     else:
88         x = px - data.press_clientx
89         y = py - data.press_clienty
90         if y < data.press_clientheight / 2:
91             if x < data.press_clientwidth / 2:
92                 corner = Client.BottomRight
93                 dx *= -1
94             else:
95                 corner = Client.BottomLeft
96             dy *= -1
97         else:
98             if x < data.press_clientwidth / 2:
99                 corner = Client.TopRight
100                 dx *= -1
101             else:
102                 corner = Client.TopLeft
103
104     data.client.resize(corner,
105                        data.press_clientwidth + dx,
106                        data.press_clientheight + dy);
107
108 def restart(data, other = ""):
109     """Restarts openbox, optionally starting another window manager."""
110     ob.openbox.restart(other)
111
112 def raise_win(data):
113     """Raises the window on which the event occured"""
114     if not data.client: return
115     ob.openbox.screen(data.screen).raiseWindow(data.client)
116
117 def lower_win(data):
118     """Lowers the window on which the event occured"""
119     if not data.client: return
120     ob.openbox.screen(data.screen).lowerWindow(data.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 = ob.display.screenInfo(data.screen).rootWindow()
137     ob.send_client_msg(root, ob.Property_atoms().net_current_desktop,
138                        root, num)
139
140 def next_desktop(data, no_wrap=0):
141     """Switches to the next desktop, optionally (by default) cycling around to
142        the first when going past the last."""
143     screen = ob.openbox.screen(data.screen)
144     d = screen.desktop()
145     n = screen.numDesktops()
146     if (d < (n-1)):
147         d = d + 1
148     elif not no_wrap:
149         d = 0
150     change_desktop(data, d)
151     
152 def prev_desktop(data, no_wrap=0):
153     """Switches to the previous desktop, optionally (by default) cycling around
154        to the last when going past the first."""
155     screen = ob.openbox.screen(data.screen)
156     d = screen.desktop()
157     n = screen.numDesktops()
158     if (d > 0):
159         d = d - 1
160     elif not no_wrap:
161         d = n - 1
162     change_desktop(data, d)
163
164 def send_to_desktop(data, num):
165     """Sends a client to a specified desktop"""
166     if not data.client: return
167     ob.send_client_msg(ob.display.screenInfo(data.screen).rootWindow(),
168                        ob.Property_atoms().net_wm_desktop,
169                        data.client.window(),num)
170
171 def toggle_all_desktops(data):
172     """Toggles between sending a client to all desktops and to the current
173        desktop."""
174     if not data.client: return
175     if not data.client.desktop() == 0xffffffff:
176         send_to_desktop(data, 0xffffffff)
177     else:
178         send_to_desktop(data, openbox.screen(data.screen).desktop())
179     
180 def send_to_all_desktops(data):
181     """Sends a client to all desktops"""
182     if not data.client: return
183     send_to_desktop(data, 0xffffffff)
184     
185 def send_to_next_desktop(data, no_wrap=0, follow=1):
186     """Sends a window to the next desktop, optionally (by default) cycling
187        around to the first when going past the last. Also optionally moving to
188        the new desktop after sending the window."""
189     if not data.client: return
190     screen = ob.openbox.screen(data.screen)
191     d = screen.desktop()
192     n = screen.numDesktops()
193     if (d < (n-1)):
194         d = d + 1
195     elif not no_wrap:
196         d = 0
197     send_to_desktop(data, d)
198     if follow:
199         change_desktop(data, d)
200     
201 def send_to_prev_desktop(data, no_wrap=0, follow=1):
202     """Sends a window to the previous desktop, optionally (by default) cycling
203        around to the last when going past the first. Also optionally moving to
204        the new desktop after sending the window."""
205     if not data.client: return
206     screen = ob.openbox.screen(data.screen)
207     d = screen.desktop()
208     n = screen.numDesktops()
209     if (d > 0):
210         d = d - 1
211     elif not no_wrap:
212         d = n - 1
213     send_to_desktop(data, d)
214     if follow:
215         change_desktop(data, d)
216
217 print "Loaded callbacks.py"