Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <limits.h>
22 : #include <tools/time.hxx>
23 : #include <tools/debug.hxx>
24 :
25 : #include <svids.hrc>
26 : #include <svdata.hxx>
27 : #include <scrwnd.hxx>
28 :
29 : #include <vcl/timer.hxx>
30 : #include <vcl/event.hxx>
31 :
32 : #include <math.h>
33 :
34 : #define WHEEL_WIDTH 25
35 : #define WHEEL_RADIUS ((WHEEL_WIDTH) >> 1 )
36 : #define MAX_TIME 300
37 : #define MIN_TIME 20
38 : #define DEF_TIMEOUT 50
39 :
40 0 : ImplWheelWindow::ImplWheelWindow( Window* pParent ) :
41 : FloatingWindow ( pParent, 0 ),
42 : mnRepaintTime ( 1UL ),
43 : mnTimeout ( DEF_TIMEOUT ),
44 : mnWheelMode ( WHEELMODE_NONE ),
45 : mnActDist ( 0UL ),
46 : mnActDeltaX ( 0L ),
47 0 : mnActDeltaY ( 0L )
48 : {
49 : // we need a parent
50 : DBG_ASSERT( pParent, "ImplWheelWindow::ImplWheelWindow(): Parent not set!" );
51 :
52 0 : const Size aSize( pParent->GetOutputSizePixel() );
53 0 : const sal_uInt16 nFlags = ImplGetSVData()->maWinData.mnAutoScrollFlags;
54 0 : const bool bHorz = ( nFlags & AUTOSCROLL_HORZ ) != 0;
55 0 : const bool bVert = ( nFlags & AUTOSCROLL_VERT ) != 0;
56 :
57 : // calculate maximum speed distance
58 0 : mnMaxWidth = (sal_uLong) ( 0.4 * hypot( (double) aSize.Width(), aSize.Height() ) );
59 :
60 : // create wheel window
61 0 : SetTitleType( FLOATWIN_TITLE_NONE );
62 0 : ImplCreateImageList();
63 0 : ResMgr* pResMgr = ImplGetResMgr();
64 0 : Bitmap aBmp;
65 0 : if( pResMgr )
66 0 : aBmp = Bitmap( ResId( SV_RESID_BITMAP_SCROLLMSK, *pResMgr ) );
67 0 : ImplSetRegion( aBmp );
68 :
69 : // set wheel mode
70 0 : if( bHorz && bVert )
71 0 : ImplSetWheelMode( WHEELMODE_VH );
72 0 : else if( bHorz )
73 0 : ImplSetWheelMode( WHEELMODE_H );
74 : else
75 0 : ImplSetWheelMode( WHEELMODE_V );
76 :
77 : // init timer
78 0 : mpTimer = new Timer;
79 0 : mpTimer->SetTimeoutHdl( LINK( this, ImplWheelWindow, ImplScrollHdl ) );
80 0 : mpTimer->SetTimeout( mnTimeout );
81 0 : mpTimer->Start();
82 :
83 0 : CaptureMouse();
84 0 : }
85 :
86 0 : ImplWheelWindow::~ImplWheelWindow()
87 : {
88 0 : ImplStop();
89 0 : delete mpTimer;
90 0 : }
91 :
92 0 : void ImplWheelWindow::ImplStop()
93 : {
94 0 : ReleaseMouse();
95 0 : mpTimer->Stop();
96 0 : Show(sal_False);
97 0 : }
98 :
99 0 : void ImplWheelWindow::ImplSetRegion( const Bitmap& rRegionBmp )
100 : {
101 0 : Point aPos( GetPointerPosPixel() );
102 0 : const Size aSize( rRegionBmp.GetSizePixel() );
103 0 : Point aPoint;
104 0 : const Rectangle aRect( aPoint, aSize );
105 :
106 0 : maCenter = maLastMousePos = aPos;
107 0 : aPos.X() -= aSize.Width() >> 1;
108 0 : aPos.Y() -= aSize.Height() >> 1;
109 :
110 0 : SetPosSizePixel( aPos, aSize );
111 0 : SetWindowRegionPixel( rRegionBmp.CreateRegion( COL_BLACK, aRect ) );
112 0 : }
113 :
114 0 : void ImplWheelWindow::ImplCreateImageList()
115 : {
116 0 : ResMgr* pResMgr = ImplGetResMgr();
117 0 : if( pResMgr )
118 : maImgList.InsertFromHorizontalBitmap
119 0 : ( ResId( SV_RESID_BITMAP_SCROLLBMP, *pResMgr ), 6, NULL );
120 0 : }
121 :
122 0 : void ImplWheelWindow::ImplSetWheelMode( sal_uLong nWheelMode )
123 : {
124 0 : if( nWheelMode != mnWheelMode )
125 : {
126 0 : mnWheelMode = nWheelMode;
127 :
128 0 : if( WHEELMODE_NONE == mnWheelMode )
129 : {
130 0 : if( IsVisible() )
131 0 : Hide();
132 : }
133 : else
134 : {
135 0 : if( !IsVisible() )
136 0 : Show();
137 :
138 0 : ImplDrawWheel();
139 : }
140 : }
141 0 : }
142 :
143 0 : void ImplWheelWindow::ImplDrawWheel()
144 : {
145 : sal_uInt16 nId;
146 :
147 0 : switch( mnWheelMode )
148 : {
149 0 : case( WHEELMODE_VH ): nId = 1; break;
150 0 : case( WHEELMODE_V ): nId = 2; break;
151 0 : case( WHEELMODE_H ): nId = 3; break;
152 0 : case( WHEELMODE_SCROLL_VH ):nId = 4; break;
153 0 : case( WHEELMODE_SCROLL_V ): nId = 5; break;
154 0 : case( WHEELMODE_SCROLL_H ): nId = 6; break;
155 0 : default: nId = 0; break;
156 : }
157 :
158 0 : if( nId )
159 0 : DrawImage( Point(), maImgList.GetImage( nId ) );
160 0 : }
161 :
162 0 : void ImplWheelWindow::ImplRecalcScrollValues()
163 : {
164 0 : if( mnActDist < WHEEL_RADIUS )
165 : {
166 0 : mnActDeltaX = mnActDeltaY = 0L;
167 0 : mnTimeout = DEF_TIMEOUT;
168 : }
169 : else
170 : {
171 : sal_uLong nCurTime;
172 :
173 : // calc current time
174 0 : if( mnMaxWidth )
175 : {
176 0 : const double fExp = ( (double) mnActDist / mnMaxWidth ) * log10( (double) MAX_TIME / MIN_TIME );
177 0 : nCurTime = (sal_uLong) ( MAX_TIME / pow( 10., fExp ) );
178 : }
179 : else
180 0 : nCurTime = MAX_TIME;
181 :
182 0 : if( !nCurTime )
183 0 : nCurTime = 1UL;
184 :
185 0 : if( mnRepaintTime <= nCurTime )
186 0 : mnTimeout = nCurTime - mnRepaintTime;
187 : else
188 : {
189 0 : long nMult = mnRepaintTime / nCurTime;
190 :
191 0 : if( !( mnRepaintTime % nCurTime ) )
192 0 : mnTimeout = 0UL;
193 : else
194 0 : mnTimeout = ++nMult * nCurTime - mnRepaintTime;
195 :
196 0 : double fValX = (double) mnActDeltaX * nMult;
197 0 : double fValY = (double) mnActDeltaY * nMult;
198 :
199 0 : if( fValX > LONG_MAX )
200 0 : mnActDeltaX = LONG_MAX;
201 0 : else if( fValX < LONG_MIN )
202 0 : mnActDeltaX = LONG_MIN;
203 : else
204 0 : mnActDeltaX = (long) fValX;
205 :
206 0 : if( fValY > LONG_MAX )
207 0 : mnActDeltaY = LONG_MAX;
208 0 : else if( fValY < LONG_MIN )
209 0 : mnActDeltaY = LONG_MIN;
210 : else
211 0 : mnActDeltaY = (long) fValY;
212 : }
213 : }
214 0 : }
215 :
216 0 : PointerStyle ImplWheelWindow::ImplGetMousePointer( long nDistX, long nDistY )
217 : {
218 : PointerStyle eStyle;
219 0 : const sal_uInt16 nFlags = ImplGetSVData()->maWinData.mnAutoScrollFlags;
220 0 : const bool bHorz = ( nFlags & AUTOSCROLL_HORZ ) != 0;
221 0 : const bool bVert = ( nFlags & AUTOSCROLL_VERT ) != 0;
222 :
223 0 : if( bHorz || bVert )
224 : {
225 0 : if( mnActDist < WHEEL_RADIUS )
226 : {
227 0 : if( bHorz && bVert )
228 0 : eStyle = POINTER_AUTOSCROLL_NSWE;
229 0 : else if( bHorz )
230 0 : eStyle = POINTER_AUTOSCROLL_WE;
231 : else
232 0 : eStyle = POINTER_AUTOSCROLL_NS;
233 : }
234 : else
235 : {
236 0 : double fAngle = atan2( (double) -nDistY, nDistX ) / F_PI180;
237 :
238 0 : if( fAngle < 0.0 )
239 0 : fAngle += 360.;
240 :
241 0 : if( bHorz && bVert )
242 : {
243 0 : if( fAngle >= 22.5 && fAngle <= 67.5 )
244 0 : eStyle = POINTER_AUTOSCROLL_NE;
245 0 : else if( fAngle >= 67.5 && fAngle <= 112.5 )
246 0 : eStyle = POINTER_AUTOSCROLL_N;
247 0 : else if( fAngle >= 112.5 && fAngle <= 157.5 )
248 0 : eStyle = POINTER_AUTOSCROLL_NW;
249 0 : else if( fAngle >= 157.5 && fAngle <= 202.5 )
250 0 : eStyle = POINTER_AUTOSCROLL_W;
251 0 : else if( fAngle >= 202.5 && fAngle <= 247.5 )
252 0 : eStyle = POINTER_AUTOSCROLL_SW;
253 0 : else if( fAngle >= 247.5 && fAngle <= 292.5 )
254 0 : eStyle = POINTER_AUTOSCROLL_S;
255 0 : else if( fAngle >= 292.5 && fAngle <= 337.5 )
256 0 : eStyle = POINTER_AUTOSCROLL_SE;
257 : else
258 0 : eStyle = POINTER_AUTOSCROLL_E;
259 : }
260 0 : else if( bHorz )
261 : {
262 0 : if( fAngle >= 270. || fAngle <= 90. )
263 0 : eStyle = POINTER_AUTOSCROLL_E;
264 : else
265 0 : eStyle = POINTER_AUTOSCROLL_W;
266 : }
267 : else
268 : {
269 0 : if( fAngle >= 0. && fAngle <= 180. )
270 0 : eStyle = POINTER_AUTOSCROLL_N;
271 : else
272 0 : eStyle = POINTER_AUTOSCROLL_S;
273 : }
274 0 : }
275 : }
276 : else
277 0 : eStyle = POINTER_ARROW;
278 :
279 0 : return eStyle;
280 : }
281 :
282 0 : void ImplWheelWindow::Paint( const Rectangle& )
283 : {
284 0 : ImplDrawWheel();
285 0 : }
286 :
287 0 : void ImplWheelWindow::MouseMove( const MouseEvent& rMEvt )
288 : {
289 0 : FloatingWindow::MouseMove( rMEvt );
290 :
291 0 : const Point aMousePos( OutputToScreenPixel( rMEvt.GetPosPixel() ) );
292 0 : const long nDistX = aMousePos.X() - maCenter.X();
293 0 : const long nDistY = aMousePos.Y() - maCenter.Y();
294 :
295 0 : mnActDist = (sal_uLong) hypot( (double) nDistX, nDistY );
296 :
297 0 : const PointerStyle eActStyle = ImplGetMousePointer( nDistX, nDistY );
298 0 : const sal_uInt16 nFlags = ImplGetSVData()->maWinData.mnAutoScrollFlags;
299 0 : const bool bHorz = ( nFlags & AUTOSCROLL_HORZ ) != 0;
300 0 : const bool bVert = ( nFlags & AUTOSCROLL_VERT ) != 0;
301 0 : const bool bOuter = mnActDist > WHEEL_RADIUS;
302 :
303 0 : if( bOuter && ( maLastMousePos != aMousePos ) )
304 : {
305 0 : switch( eActStyle )
306 : {
307 0 : case( POINTER_AUTOSCROLL_N ): mnActDeltaX = +0L, mnActDeltaY = +1L; break;
308 0 : case( POINTER_AUTOSCROLL_S ): mnActDeltaX = +0L, mnActDeltaY = -1L; break;
309 0 : case( POINTER_AUTOSCROLL_W ): mnActDeltaX = +1L, mnActDeltaY = +0L; break;
310 0 : case( POINTER_AUTOSCROLL_E ): mnActDeltaX = -1L, mnActDeltaY = +0L; break;
311 0 : case( POINTER_AUTOSCROLL_NW ): mnActDeltaX = +1L, mnActDeltaY = +1L; break;
312 0 : case( POINTER_AUTOSCROLL_NE ): mnActDeltaX = -1L, mnActDeltaY = +1L; break;
313 0 : case( POINTER_AUTOSCROLL_SW ): mnActDeltaX = +1L, mnActDeltaY = -1L; break;
314 0 : case( POINTER_AUTOSCROLL_SE ): mnActDeltaX = -1L, mnActDeltaY = -1L; break;
315 :
316 : default:
317 0 : break;
318 : }
319 : }
320 :
321 0 : ImplRecalcScrollValues();
322 0 : maLastMousePos = aMousePos;
323 0 : SetPointer( eActStyle );
324 :
325 0 : if( bHorz && bVert )
326 0 : ImplSetWheelMode( bOuter ? WHEELMODE_SCROLL_VH : WHEELMODE_VH );
327 0 : else if( bHorz )
328 0 : ImplSetWheelMode( bOuter ? WHEELMODE_SCROLL_H : WHEELMODE_H );
329 : else
330 0 : ImplSetWheelMode( bOuter ? WHEELMODE_SCROLL_V : WHEELMODE_V );
331 0 : }
332 :
333 0 : void ImplWheelWindow::MouseButtonUp( const MouseEvent& rMEvt )
334 : {
335 0 : if( mnActDist > WHEEL_RADIUS )
336 0 : GetParent()->EndAutoScroll();
337 : else
338 0 : FloatingWindow::MouseButtonUp( rMEvt );
339 0 : }
340 :
341 0 : IMPL_LINK_NOARG(ImplWheelWindow, ImplScrollHdl)
342 : {
343 0 : if ( mnActDeltaX || mnActDeltaY )
344 : {
345 0 : Window* pWindow = GetParent();
346 0 : const Point aMousePos( pWindow->OutputToScreenPixel( pWindow->GetPointerPosPixel() ) );
347 0 : Point aCmdMousePos( pWindow->ImplFrameToOutput( aMousePos ) );
348 0 : CommandScrollData aScrollData( mnActDeltaX, mnActDeltaY );
349 0 : CommandEvent aCEvt( aCmdMousePos, COMMAND_AUTOSCROLL, sal_True, &aScrollData );
350 0 : NotifyEvent aNCmdEvt( EVENT_COMMAND, pWindow, &aCEvt );
351 :
352 0 : if ( !ImplCallPreNotify( aNCmdEvt ) )
353 : {
354 0 : const sal_uLong nTime = Time::GetSystemTicks();
355 0 : ImplDelData aDel( this );
356 0 : pWindow->Command( aCEvt );
357 0 : if( aDel.IsDead() )
358 0 : return 0;
359 0 : mnRepaintTime = std::max( Time::GetSystemTicks() - nTime, (sal_uLong)1 );
360 0 : ImplRecalcScrollValues();
361 : }
362 : }
363 :
364 0 : if ( mnTimeout != mpTimer->GetTimeout() )
365 0 : mpTimer->SetTimeout( mnTimeout );
366 0 : mpTimer->Start();
367 :
368 0 : return 0L;
369 465 : }
370 :
371 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|