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