]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/motion.py
add motion.py, which provides funtions for moving/resizing a window interactively
[mikachu/openbox.git] / 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 _inmotion = 0
41
42 # last motion data
43 _cx = 0
44 _cy = 0
45 _cw = 0
46 _ch = 0
47 _px = 0
48 _py = 0
49 _dx = 0
50 _dy = 0
51 _client = 0
52 _screen = 0
53
54 def _do_move():
55     global _screen, _client, _cx, _cy, _dx, _dy
56
57     x = _cx + _dx
58     y = _cy + _dy
59
60     global move_rubberband
61     if move_rubberband:
62         # draw the outline ...
63         f=0
64     else:
65         _client.move(x, y)
66
67     global move_popup
68     if move_popup:
69         global _popwidget, _poplabel
70         style = ob.openbox.screen(_screen).style()
71         font = style.labelFont()
72         text = "X: " + str(x) + " Y: " + str(y)
73         length = font.measureString(text)
74         if not _popwidget:
75             _popwidget = otk.Widget(ob.openbox, style,
76                                     otk.Widget.Horizontal, 0,
77                                     style.bevelWidth(), 1)
78             _popwidget.setTexture(style.titlebarFocusBackground())
79             _poplabel = otk.Label(_popwidget)
80             _poplabel.setTexture(style.labelFocusBackground())
81             _popwidget.show(1)
82         _poplabel.resize(length, font.height())
83         _poplabel.setText(text)
84         area = otk.display.screenInfo(_screen).rect()
85         _popwidget.update() 
86         _popwidget.move(area.x() + (area.width() -
87                                     _popwidget.width()) / 2,
88                         area.y() + (area.height() -
89                                     _popwidget.height()) / 2)
90
91 def move(data):
92     """Moves the window interactively. This should only be used with
93        MouseMotion events. If move_popup or move_rubberband is enabled, then
94        the end_move function needs to be bound as well."""
95     if not data.client: return
96
97     # not-normal windows dont get moved
98     if not data.client.normal(): return
99
100     global _screen, _client, _cx, _cy, _dx, _dy
101     _screen = data.screen
102     _client = data.client
103     _cx = data.press_clientx
104     _cy = data.press_clienty
105     _dx = data.xroot - data.pressx
106     _dy = data.yroot - data.pressy
107     _do_move()
108     _inmotion = 1
109
110 def end_move(data):
111     """Complete the interactive move of a window."""
112     global move_rubberband, _inmotion
113     global _popwidget, _poplabel
114     if _inmotion:
115         r = move_rubberband
116         move_rubberband = 0
117         _do_move()
118         move_rubberband = r
119         _inmotion = 0
120     _poplabel = 0
121     _popwidget = 0
122
123 def _do_resize():
124     global _screen, _client, _cx, _cy, _cw, _ch, _px, _py, _dx, _dy
125
126     # pick a corner to anchor
127     if not (resize_nearest or _context == ob.MouseContext.Grip):
128         corner = ob.Client.TopLeft
129     else:
130         x = _px - _cx
131         y = _py - _cy
132         if y < _ch / 2:
133             if x < _cw / 2:
134                 corner = ob.Client.BottomRight
135                 _dx *= -1
136             else:
137                 corner = ob.Client.BottomLeft
138             _dy *= -1
139         else:
140             if x < _cw / 2:
141                 corner = ob.Client.TopRight
142                 _dx *= -1
143             else:
144                 corner = ob.Client.TopLeft
145
146     w = _cw + _dx
147     h = _ch + _dy
148
149     global resize_popup
150     if resize_rubberband:
151         # draw the outline ...
152         f=0
153     else:
154         _client.resize(corner, w, h)
155
156     global resize_popup
157     if resize_popup:
158         global _popwidget, _poplabel
159         style = ob.openbox.screen(_screen).style()
160         ls = _client.logicalSize()
161         text = "W: " + str(ls.x()) + " H: " + str(ls.y())
162         if not _popwidget:
163             _popwidget = otk.Widget(ob.openbox, style,
164                                     otk.Widget.Horizontal, 0,
165                                     style.bevelWidth(), 1)
166             _popwidget.setTexture(style.titlebarFocusBackground())
167             _poplabel = otk.Label(_popwidget)
168             _poplabel.setTexture(style.labelFocusBackground())
169             _popwidget.show(1)
170         _poplabel.fitString(text)
171         _poplabel.setText(text)
172         area = otk.display.screenInfo(_screen).rect()
173         _popwidget.update() 
174         _popwidget.move(area.x() + (area.width() -
175                                     _popwidget.width()) / 2,
176                         area.y() + (area.height() -
177                                     _popwidget.height()) / 2)
178
179 def resize(data):
180     """Resizes the window interactively. This should only be used with
181        MouseMotion events"""
182     if not data.client: return
183
184     # not-normal windows dont get resized
185     if not data.client.normal(): return
186
187     global _screen, _client, _cx, _cy, _cw, _ch, _px, _py, _dx, _dy
188     _screen = data.screen
189     _client = data.client
190     _cx = data.press_clientx
191     _cy = data.press_clienty
192     _cw = data.press_clientwidth
193     _ch = data.press_clientheight
194     _px = data.pressx
195     _py = data.pressy
196     _dx = data.xroot - _px
197     _dy = data.yroot - _py
198     _do_resize()
199     _inmotion = 1
200
201 def end_resize(data):
202     """Complete the interactive resize of a window."""
203     global resize_rubberband, _inmotion
204     global _popwidget, _poplabel
205     if _inmotion:
206         r = resize_rubberband
207         resize_rubberband = 0
208         _do_resize()
209         resize_rubberband = r
210         _inmotion = 0
211     _poplabel = 0
212     _popwidget = 0