1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <o3tl/float_int_conversion.hxx>
23 #include <tools/time.hxx>
25 #include <bitmaps.hlst>
29 #include <vcl/timer.hxx>
30 #include <vcl/commandevent.hxx>
31 #include <vcl/event.hxx>
32 #include <vcl/ptrstyle.hxx>
33 #include <sal/log.hxx>
37 #define WHEEL_WIDTH 25
38 #define WHEEL_RADIUS ((WHEEL_WIDTH) >> 1 )
41 #define DEF_TIMEOUT 50
43 ImplWheelWindow::ImplWheelWindow( vcl::Window
* pParent
) :
44 FloatingWindow ( pParent
, 0 ),
46 mnTimeout ( DEF_TIMEOUT
),
47 mnWheelMode ( WheelMode::NONE
),
53 SAL_WARN_IF( !pParent
, "vcl", "ImplWheelWindow::ImplWheelWindow(): Parent not set!" );
55 const Size
aSize( pParent
->GetOutputSizePixel() );
56 const StartAutoScrollFlags nFlags
= ImplGetSVData()->maWinData
.mnAutoScrollFlags
;
57 const bool bHorz( nFlags
& StartAutoScrollFlags::Horz
);
58 const bool bVert( nFlags
& StartAutoScrollFlags::Vert
);
60 // calculate maximum speed distance
61 mnMaxWidth
= static_cast<sal_uLong
>( 0.4 * hypot( static_cast<double>(aSize
.Width()), aSize
.Height() ) );
63 // create wheel window
64 SetTitleType( FloatWinTitleType::NONE
);
65 ImplCreateImageList();
66 BitmapEx
aBmp(SV_RESID_BITMAP_SCROLLMSK
);
67 ImplSetRegion(aBmp
.GetBitmap());
71 ImplSetWheelMode( WheelMode::VH
);
73 ImplSetWheelMode( WheelMode::H
);
75 ImplSetWheelMode( WheelMode::V
);
78 mpTimer
.reset(new Timer("WheelWindowTimer"));
79 mpTimer
->SetInvokeHandler( LINK( this, ImplWheelWindow
, ImplScrollHdl
) );
80 mpTimer
->SetTimeout( mnTimeout
);
86 ImplWheelWindow::~ImplWheelWindow()
91 void ImplWheelWindow::dispose()
95 FloatingWindow::dispose();
98 void ImplWheelWindow::ImplStop()
105 void ImplWheelWindow::ImplSetRegion( const Bitmap
& rRegionBmp
)
107 Point
aPos( GetPointerPosPixel() );
108 const Size
aSize( rRegionBmp
.GetSizePixel() );
109 const tools::Rectangle
aRect( Point(), aSize
);
111 maCenter
= maLastMousePos
= aPos
;
112 aPos
.AdjustX( -(aSize
.Width() >> 1) );
113 aPos
.AdjustY( -(aSize
.Height() >> 1) );
115 SetPosSizePixel( aPos
, aSize
);
116 SetWindowRegionPixel( rRegionBmp
.CreateRegion( COL_BLACK
, aRect
) );
119 void ImplWheelWindow::ImplCreateImageList()
121 maImgList
.emplace_back(Image(StockImage::Yes
, SV_RESID_BITMAP_SCROLLVH
));
122 maImgList
.emplace_back(Image(StockImage::Yes
, SV_RESID_BITMAP_SCROLLV
));
123 maImgList
.emplace_back(Image(StockImage::Yes
, SV_RESID_BITMAP_SCROLLH
));
124 maImgList
.emplace_back(Image(StockImage::Yes
, SV_RESID_BITMAP_WHEELVH
));
125 maImgList
.emplace_back(Image(StockImage::Yes
, SV_RESID_BITMAP_WHEELV
));
126 maImgList
.emplace_back(Image(StockImage::Yes
, SV_RESID_BITMAP_WHEELH
));
129 void ImplWheelWindow::ImplSetWheelMode( WheelMode nWheelMode
)
131 if( nWheelMode
!= mnWheelMode
)
133 mnWheelMode
= nWheelMode
;
135 if( WheelMode::NONE
== mnWheelMode
)
150 void ImplWheelWindow::ImplDrawWheel(vcl::RenderContext
& rRenderContext
)
165 case WheelMode::ScrollVH
:
168 case WheelMode::ScrollV
:
171 case WheelMode::ScrollH
:
180 rRenderContext
.DrawImage(Point(), maImgList
[nIndex
]);
183 void ImplWheelWindow::ImplRecalcScrollValues()
185 if( mnActDist
< WHEEL_RADIUS
)
187 mnActDeltaX
= mnActDeltaY
= 0;
188 mnTimeout
= DEF_TIMEOUT
;
197 const double fExp
= ( static_cast<double>(mnActDist
) / mnMaxWidth
) * log10( double(MAX_TIME
) / MIN_TIME
);
198 nCurTime
= static_cast<sal_uInt64
>( MAX_TIME
/ pow( 10., fExp
) );
206 if( mnRepaintTime
<= nCurTime
)
207 mnTimeout
= nCurTime
- mnRepaintTime
;
210 sal_uInt64 nMult
= mnRepaintTime
/ nCurTime
;
212 if( !( mnRepaintTime
% nCurTime
) )
215 mnTimeout
= ++nMult
* nCurTime
- mnRepaintTime
;
217 double fValX
= static_cast<double>(mnActDeltaX
) * nMult
;
218 double fValY
= static_cast<double>(mnActDeltaY
) * nMult
;
220 if( !o3tl::convertsToAtMost(fValX
, LONG_MAX
) )
221 mnActDeltaX
= LONG_MAX
;
222 else if( !o3tl::convertsToAtLeast(fValX
, LONG_MIN
) )
223 mnActDeltaX
= LONG_MIN
;
225 mnActDeltaX
= static_cast<long>(fValX
);
227 if( !o3tl::convertsToAtMost(fValY
, LONG_MAX
) )
228 mnActDeltaY
= LONG_MAX
;
229 else if( !o3tl::convertsToAtLeast(fValY
, LONG_MIN
) )
230 mnActDeltaY
= LONG_MIN
;
232 mnActDeltaY
= static_cast<long>(fValY
);
237 PointerStyle
ImplWheelWindow::ImplGetMousePointer( long nDistX
, long nDistY
)
240 const StartAutoScrollFlags nFlags
= ImplGetSVData()->maWinData
.mnAutoScrollFlags
;
241 const bool bHorz( nFlags
& StartAutoScrollFlags::Horz
);
242 const bool bVert( nFlags
& StartAutoScrollFlags::Vert
);
246 if( mnActDist
< WHEEL_RADIUS
)
249 eStyle
= PointerStyle::AutoScrollNSWE
;
251 eStyle
= PointerStyle::AutoScrollWE
;
253 eStyle
= PointerStyle::AutoScrollNS
;
257 double fAngle
= basegfx::rad2deg(atan2(static_cast<double>(-nDistY
), nDistX
));
264 if( fAngle
>= 22.5 && fAngle
<= 67.5 )
265 eStyle
= PointerStyle::AutoScrollNE
;
266 else if( fAngle
>= 67.5 && fAngle
<= 112.5 )
267 eStyle
= PointerStyle::AutoScrollN
;
268 else if( fAngle
>= 112.5 && fAngle
<= 157.5 )
269 eStyle
= PointerStyle::AutoScrollNW
;
270 else if( fAngle
>= 157.5 && fAngle
<= 202.5 )
271 eStyle
= PointerStyle::AutoScrollW
;
272 else if( fAngle
>= 202.5 && fAngle
<= 247.5 )
273 eStyle
= PointerStyle::AutoScrollSW
;
274 else if( fAngle
>= 247.5 && fAngle
<= 292.5 )
275 eStyle
= PointerStyle::AutoScrollS
;
276 else if( fAngle
>= 292.5 && fAngle
<= 337.5 )
277 eStyle
= PointerStyle::AutoScrollSE
;
279 eStyle
= PointerStyle::AutoScrollE
;
283 if( fAngle
>= 270. || fAngle
<= 90. )
284 eStyle
= PointerStyle::AutoScrollE
;
286 eStyle
= PointerStyle::AutoScrollW
;
290 if( fAngle
>= 0. && fAngle
<= 180. )
291 eStyle
= PointerStyle::AutoScrollN
;
293 eStyle
= PointerStyle::AutoScrollS
;
298 eStyle
= PointerStyle::Arrow
;
303 void ImplWheelWindow::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
&)
305 ImplDrawWheel(rRenderContext
);
308 void ImplWheelWindow::MouseMove( const MouseEvent
& rMEvt
)
310 FloatingWindow::MouseMove( rMEvt
);
312 const Point
aMousePos( OutputToScreenPixel( rMEvt
.GetPosPixel() ) );
313 const long nDistX
= aMousePos
.X() - maCenter
.X();
314 const long nDistY
= aMousePos
.Y() - maCenter
.Y();
316 mnActDist
= static_cast<sal_uLong
>(hypot( static_cast<double>(nDistX
), nDistY
));
318 const PointerStyle eActStyle
= ImplGetMousePointer( nDistX
, nDistY
);
319 const StartAutoScrollFlags nFlags
= ImplGetSVData()->maWinData
.mnAutoScrollFlags
;
320 const bool bHorz( nFlags
& StartAutoScrollFlags::Horz
);
321 const bool bVert( nFlags
& StartAutoScrollFlags::Vert
);
322 const bool bOuter
= mnActDist
> WHEEL_RADIUS
;
324 if( bOuter
&& ( maLastMousePos
!= aMousePos
) )
328 case PointerStyle::AutoScrollN
: mnActDeltaX
= +0; mnActDeltaY
= +1; break;
329 case PointerStyle::AutoScrollS
: mnActDeltaX
= +0; mnActDeltaY
= -1; break;
330 case PointerStyle::AutoScrollW
: mnActDeltaX
= +1; mnActDeltaY
= +0; break;
331 case PointerStyle::AutoScrollE
: mnActDeltaX
= -1; mnActDeltaY
= +0; break;
332 case PointerStyle::AutoScrollNW
: mnActDeltaX
= +1; mnActDeltaY
= +1; break;
333 case PointerStyle::AutoScrollNE
: mnActDeltaX
= -1; mnActDeltaY
= +1; break;
334 case PointerStyle::AutoScrollSW
: mnActDeltaX
= +1; mnActDeltaY
= -1; break;
335 case PointerStyle::AutoScrollSE
: mnActDeltaX
= -1; mnActDeltaY
= -1; break;
342 ImplRecalcScrollValues();
343 maLastMousePos
= aMousePos
;
344 SetPointer( eActStyle
);
347 ImplSetWheelMode( bOuter
? WheelMode::ScrollVH
: WheelMode::VH
);
349 ImplSetWheelMode( bOuter
? WheelMode::ScrollH
: WheelMode::H
);
351 ImplSetWheelMode( bOuter
? WheelMode::ScrollV
: WheelMode::V
);
354 void ImplWheelWindow::MouseButtonUp( const MouseEvent
& rMEvt
)
356 if( mnActDist
> WHEEL_RADIUS
)
357 GetParent()->EndAutoScroll();
359 FloatingWindow::MouseButtonUp( rMEvt
);
362 IMPL_LINK_NOARG(ImplWheelWindow
, ImplScrollHdl
, Timer
*, void)
364 if ( mnActDeltaX
|| mnActDeltaY
)
366 vcl::Window
* pWindow
= GetParent();
367 const Point
aMousePos( pWindow
->OutputToScreenPixel( pWindow
->GetPointerPosPixel() ) );
368 Point
aCmdMousePos( pWindow
->ImplFrameToOutput( aMousePos
) );
369 CommandScrollData
aScrollData( mnActDeltaX
, mnActDeltaY
);
370 CommandEvent
aCEvt( aCmdMousePos
, CommandEventId::AutoScroll
, true, &aScrollData
);
371 NotifyEvent
aNCmdEvt( MouseNotifyEvent::COMMAND
, pWindow
, &aCEvt
);
373 if ( !ImplCallPreNotify( aNCmdEvt
) )
375 const sal_uInt64 nTime
= tools::Time::GetSystemTicks();
376 VclPtr
<ImplWheelWindow
> xWin(this);
377 pWindow
->Command( aCEvt
);
378 if( xWin
->IsDisposed() )
380 mnRepaintTime
= std::max( tools::Time::GetSystemTicks() - nTime
, sal_uInt64(1) );
381 ImplRecalcScrollValues();
385 if ( mnTimeout
!= mpTimer
->GetTimeout() )
386 mpTimer
->SetTimeout( mnTimeout
);
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */