]> Dogcows Code - chaz/openbox/blob - scripts/motion.py
rm debug prints
[chaz/openbox] / scripts / motion.py
1 ############################################################################
2 ### Functions that provide callbacks for motion events to move and ###
3 ### 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
33 import ob
34 import otk
35
36 _popwidget = 0
37 _poplabel = 0
38
39 # motion state
40 _inmove = 0
41 _inresize = 0
42
43 # last motion data
44 _cx = 0
45 _cy = 0
46 _cw = 0
47 _ch = 0
48 _px = 0
49 _py = 0
50 _dx = 0
51 _dy = 0
52 _client = 0
53 _screen = 0
54
55 _motion_mask = 0
56
57 def _motion_grab(data):
58 global _motion_mask, _inmove, _inresize;
59
60 if data.action == ob.KeyAction.Release:
61 # have all the modifiers this started with been released?
62 if not _motion_mask & data.state:
63 if _inmove:
64 end_move(data)
65 elif _inresize:
66 end_resize(data)
67 else:
68 raise RuntimeError
69
70 def _do_move():
71 global _screen, _client, _cx, _cy, _dx, _dy
72
73 x = _cx + _dx
74 y = _cy + _dy
75
76 global move_rubberband
77 if move_rubberband:
78 # draw the outline ...
79 f=0
80 else:
81 _client.move(x, y)
82
83 global move_popup
84 if move_popup:
85 global _popwidget, _poplabel
86 style = ob.openbox.screen(_screen).style()
87 font = style.labelFont()
88 text = "X: " + str(x) + " Y: " + str(y)
89 length = font.measureString(text)
90 if not _popwidget:
91 _popwidget = otk.Widget(ob.openbox, style,
92 otk.Widget.Horizontal, 0,
93 style.bevelWidth(), 1)
94 _popwidget.setTexture(style.titlebarFocusBackground())
95 _poplabel = otk.Label(_popwidget)
96 _poplabel.setTexture(style.labelFocusBackground())
97 _popwidget.show(1)
98 _poplabel.resize(length, font.height())
99 _poplabel.setText(text)
100 area = otk.display.screenInfo(_screen).rect()
101 _popwidget.update()
102 _popwidget.move(area.x() + (area.width() -
103 _popwidget.width()) / 2,
104 area.y() + (area.height() -
105 _popwidget.height()) / 2)
106
107 def move(data):
108 """Moves the window interactively. This should only be used with
109 MouseMotion events. If move_popup or move_rubberband is enabled, then
110 the end_move function needs to be bound as well."""
111 if not data.client: return
112
113 # not-normal windows dont get moved
114 if not data.client.normal(): return
115
116 global _screen, _client, _cx, _cy, _dx, _dy
117 _screen = data.screen
118 _client = data.client
119 _cx = data.press_clientx
120 _cy = data.press_clienty
121 _dx = data.xroot - data.pressx
122 _dy = data.yroot - data.pressy
123 _do_move()
124 global _inmove
125 if not _inmove:
126 ob.kgrab(_screen, _motion_grab)
127 _inmove = 1
128
129 def end_move(data):
130 """Complete the interactive move of a window."""
131 global move_rubberband, _inmove
132 global _popwidget, _poplabel
133 if _inmove:
134 r = move_rubberband
135 move_rubberband = 0
136 _do_move()
137 move_rubberband = r
138 _inmove = 0
139 _poplabel = 0
140 _popwidget = 0
141 ob.kungrab()
142
143 def _do_resize():
144 global _screen, _client, _cx, _cy, _cw, _ch, _px, _py, _dx, _dy
145
146 # pick a corner to anchor
147 if not (resize_nearest or _context == ob.MouseContext.Grip):
148 corner = ob.Client.TopLeft
149 else:
150 x = _px - _cx
151 y = _py - _cy
152 if y < _ch / 2:
153 if x < _cw / 2:
154 corner = ob.Client.BottomRight
155 _dx *= -1
156 else:
157 corner = ob.Client.BottomLeft
158 _dy *= -1
159 else:
160 if x < _cw / 2:
161 corner = ob.Client.TopRight
162 _dx *= -1
163 else:
164 corner = ob.Client.TopLeft
165
166 w = _cw + _dx
167 h = _ch + _dy
168
169 global resize_popup
170 if resize_rubberband:
171 # draw the outline ...
172 f=0
173 else:
174 _client.resize(corner, w, h)
175
176 global resize_popup
177 if resize_popup:
178 global _popwidget, _poplabel
179 style = ob.openbox.screen(_screen).style()
180 ls = _client.logicalSize()
181 text = "W: " + str(ls.x()) + " H: " + str(ls.y())
182 if not _popwidget:
183 _popwidget = otk.Widget(ob.openbox, style,
184 otk.Widget.Horizontal, 0,
185 style.bevelWidth(), 1)
186 _popwidget.setTexture(style.titlebarFocusBackground())
187 _poplabel = otk.Label(_popwidget)
188 _poplabel.setTexture(style.labelFocusBackground())
189 _popwidget.show(1)
190 _poplabel.fitString(text)
191 _poplabel.setText(text)
192 area = otk.display.screenInfo(_screen).rect()
193 _popwidget.update()
194 _popwidget.move(area.x() + (area.width() -
195 _popwidget.width()) / 2,
196 area.y() + (area.height() -
197 _popwidget.height()) / 2)
198
199 def resize(data):
200 """Resizes the window interactively. This should only be used with
201 MouseMotion events"""
202 if not data.client: return
203
204 # not-normal windows dont get resized
205 if not data.client.normal(): return
206
207 global _screen, _client, _cx, _cy, _cw, _ch, _px, _py, _dx, _dy
208 _screen = data.screen
209 _client = data.client
210 _cx = data.press_clientx
211 _cy = data.press_clienty
212 _cw = data.press_clientwidth
213 _ch = data.press_clientheight
214 _px = data.pressx
215 _py = data.pressy
216 _dx = data.xroot - _px
217 _dy = data.yroot - _py
218 _do_resize()
219 global _inresize
220 if not _inresize:
221 ob.kgrab(_screen, _motion_grab)
222 _inresize = 1
223
224 def end_resize(data):
225 """Complete the interactive resize of a window."""
226 global resize_rubberband, _inresize
227 global _popwidget, _poplabel
228 if _inresize:
229 r = resize_rubberband
230 resize_rubberband = 0
231 _do_resize()
232 resize_rubberband = r
233 _inresize = 0
234 _poplabel = 0
235 _popwidget = 0
236 ob.kungrab()
This page took 0.051038 seconds and 4 git commands to generate.