]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/motion.py
add comments
[mikachu/openbox.git] / scripts / motion.py
1 ############################################################################
2 ###    Functions that provide callbacks for motion events to move and    ###
3 ###    resize windows.                                                   ###
4 ############################################################################
5
6 #############################################################################
7 ### Options that can be modified to change the functions' behaviors.      ###
8 ###                                                                       ###
9 # move_popup - display a coordinates popup when moving windows.           ###
10 move_popup = 1                                                            ###
11 ###                                                                       ###
12 # NOT IMPLEMENTED (yet?)                                                  ###
13 # move_rubberband - display an outline while moving instead of moving the ###
14 ###                 actual window, until the move is completed. Good for  ###
15 ###                 slower systems.                                       ###
16 move_rubberband = 0                                                       ###
17 ###                                                                       ###
18 # resize_popup - display a size popup when resizing windows.              ###
19 resize_popup = 1                                                          ###
20 ###                                                                       ###
21 # NOT IMPLEMENTED (yet?)                                                  ###
22 # resize_rubberband - display an outline while resizing instead of        ###
23 ###                   resizing the actual window, until the resize is     ###
24 ###                   completed. Good for slower systems.                 ###
25 resize_rubberband = 0                                                     ###
26 ###                                                                       ###
27 # resize_nearest - 1 to resize from the corner nearest where the mouse    ###
28 ###                is, 0 to resize always from the bottom right corner.   ###
29 resize_nearest = 1                                                        ###
30 ###                                                                       ###
31 ###                                                                       ###
32 # Provides:                                                               ###
33 # def move(data):                                                         ###
34 #     """Moves the window interactively. This should only be used with    ###
35 #        MouseMotion events. If move_popup or move_rubberband is enabled, ###
36 #        then the end_move function needs to be bound as well."""         ###
37 # def end_move(data):                                                     ###
38 #     """Complete the interactive move of a window."""                    ###
39 # def resize(data):                                                       ###
40 #     """Resizes the window interactively. This should only be used with  ###
41 #        MouseMotion events"""                                            ###
42 # def end_resize(data):                                                   ###
43 #     """Complete the interactive resize of a window."""                  ###
44 ###                                                                       ###
45 #############################################################################
46
47 import ob
48 import otk
49
50 _popwidget = 0
51 _poplabel = 0
52
53 # motion state
54 _inmove = 0
55 _inresize = 0
56
57 # last motion data
58 _cx = 0
59 _cy = 0
60 _cw = 0
61 _ch = 0
62 _px = 0
63 _py = 0
64 _dx = 0
65 _dy = 0
66 _client = 0
67 _screen = 0
68
69 _motion_mask = 0
70
71 def _motion_grab(data):
72     global _motion_mask, _inmove, _inresize;
73
74     if data.action == ob.KeyAction.Release:
75         # have all the modifiers this started with been released?
76         if not _motion_mask & data.state:
77             if _inmove:
78                 end_move(data)
79             elif _inresize:
80                 end_resize(data)
81             else:
82                 raise RuntimeError
83
84 def _do_move():
85     global _screen, _client, _cx, _cy, _dx, _dy
86
87     x = _cx + _dx
88     y = _cy + _dy
89
90     global move_rubberband
91     if move_rubberband:
92         # draw the outline ...
93         f=0
94     else:
95         _client.move(x, y)
96
97     global move_popup
98     if move_popup:
99         global _popwidget, _poplabel
100         style = ob.openbox.screen(_screen).style()
101         font = style.labelFont()
102         text = "X: " + str(x) + " Y: " + str(y)
103         length = font.measureString(text)
104         if not _popwidget:
105             _popwidget = otk.Widget(ob.openbox, style,
106                                     otk.Widget.Horizontal, 0,
107                                     style.bevelWidth(), 1)
108             _popwidget.setTexture(style.titlebarFocusBackground())
109             _poplabel = otk.Label(_popwidget)
110             _poplabel.setTexture(style.labelFocusBackground())
111             _popwidget.show(1)
112         _poplabel.fitString(text)
113         _poplabel.setText(text)
114         area = otk.display.screenInfo(_screen).rect()
115         _popwidget.update() 
116         _popwidget.move(area.x() + (area.width() -
117                                     _popwidget.width()) / 2,
118                         area.y() + (area.height() -
119                                     _popwidget.height()) / 2)
120
121 def move(data):
122     """Moves the window interactively. This should only be used with
123        MouseMotion events. If move_popup or move_rubberband is enabled, then
124        the end_move function needs to be bound as well."""
125     if not data.client: return
126
127     # not-normal windows dont get moved
128     if not data.client.normal(): return
129
130     global _screen, _client, _cx, _cy, _dx, _dy
131     _screen = data.screen
132     _client = data.client
133     _cx = data.press_clientx
134     _cy = data.press_clienty
135     _dx = data.xroot - data.pressx
136     _dy = data.yroot - data.pressy
137     _do_move()
138     global _inmove
139     if not _inmove:
140         ob.kgrab(_screen, _motion_grab)
141         _inmove = 1
142
143 def end_move(data):
144     """Complete the interactive move of a window."""
145     global move_rubberband, _inmove
146     global _popwidget, _poplabel
147     if _inmove:
148         r = move_rubberband
149         move_rubberband = 0
150         _do_move()
151         move_rubberband = r
152         _inmove = 0
153     _poplabel = 0
154     _popwidget = 0
155     ob.kungrab()
156
157 def _do_resize():
158     global _screen, _client, _cx, _cy, _cw, _ch, _px, _py, _dx, _dy
159
160     dx = _dx
161     dy = _dy
162     
163     # pick a corner to anchor
164     if not (resize_nearest or _context == ob.MouseContext.Grip):
165         corner = ob.Client.TopLeft
166     else:
167         x = _px - _cx
168         y = _py - _cy
169         if y < _ch / 2:
170             if x < _cw / 2:
171                 corner = ob.Client.BottomRight
172                 dx *= -1
173             else:
174                 corner = ob.Client.BottomLeft
175             dy *= -1
176         else:
177             if x < _cw / 2:
178                 corner = ob.Client.TopRight
179                 dx *= -1
180             else:
181                 corner = ob.Client.TopLeft
182
183     w = _cw + dx
184     h = _ch + dy
185
186     global resize_popup
187     if resize_rubberband:
188         # draw the outline ...
189         f=0
190     else:
191         _client.resize(corner, w, h)
192
193     global resize_popup
194     if resize_popup:
195         global _popwidget, _poplabel
196         style = ob.openbox.screen(_screen).style()
197         ls = _client.logicalSize()
198         text = "W: " + str(ls.x()) + " H: " + str(ls.y())
199         if not _popwidget:
200             _popwidget = otk.Widget(ob.openbox, style,
201                                     otk.Widget.Horizontal, 0,
202                                     style.bevelWidth(), 1)
203             _popwidget.setTexture(style.titlebarFocusBackground())
204             _poplabel = otk.Label(_popwidget)
205             _poplabel.setTexture(style.labelFocusBackground())
206             _popwidget.show(1)
207         _poplabel.fitString(text)
208         _poplabel.setText(text)
209         area = otk.display.screenInfo(_screen).rect()
210         _popwidget.update() 
211         _popwidget.move(area.x() + (area.width() -
212                                     _popwidget.width()) / 2,
213                         area.y() + (area.height() -
214                                     _popwidget.height()) / 2)
215
216 def resize(data):
217     """Resizes the window interactively. This should only be used with
218        MouseMotion events"""
219     if not data.client: return
220
221     # not-normal windows dont get resized
222     if not data.client.normal(): return
223
224     global _screen, _client, _cx, _cy, _cw, _ch, _px, _py, _dx, _dy
225     _screen = data.screen
226     _client = data.client
227     _cx = data.press_clientx
228     _cy = data.press_clienty
229     _cw = data.press_clientwidth
230     _ch = data.press_clientheight
231     _px = data.pressx
232     _py = data.pressy
233     _dx = data.xroot - _px
234     _dy = data.yroot - _py
235     _do_resize()
236     global _inresize
237     if not _inresize:
238         ob.kgrab(_screen, _motion_grab)
239         _inresize = 1
240
241 def end_resize(data):
242     """Complete the interactive resize of a window."""
243     global resize_rubberband, _inresize
244     global _popwidget, _poplabel
245     if _inresize:
246         r = resize_rubberband
247         resize_rubberband = 0
248         _do_resize()
249         resize_rubberband = r
250         _inresize = 0
251     _poplabel = 0
252     _popwidget = 0
253     ob.kungrab()