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()->mpWinData
->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(StockImage::Yes
, SV_RESID_BITMAP_SCROLLVH
);
122 maImgList
.emplace_back(StockImage::Yes
, SV_RESID_BITMAP_SCROLLV
);
123 maImgList
.emplace_back(StockImage::Yes
, SV_RESID_BITMAP_SCROLLH
);
124 maImgList
.emplace_back(StockImage::Yes
, SV_RESID_BITMAP_WHEELVH
);
125 maImgList
.emplace_back(StockImage::Yes
, SV_RESID_BITMAP_WHEELV
);
126 maImgList
.emplace_back(StockImage::Yes
, SV_RESID_BITMAP_WHEELH
);
129 void ImplWheelWindow::ImplSetWheelMode( WheelMode nWheelMode
)
131 if( nWheelMode
== mnWheelMode
)
134 mnWheelMode
= nWheelMode
;
136 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 mnActDeltaX
= o3tl::saturating_cast
<tools::Long
>(fValX
);
221 mnActDeltaY
= o3tl::saturating_cast
<tools::Long
>(fValY
);
226 PointerStyle
ImplWheelWindow::ImplGetMousePointer( tools::Long nDistX
, tools::Long nDistY
) const
229 const StartAutoScrollFlags nFlags
= ImplGetSVData()->mpWinData
->mnAutoScrollFlags
;
230 const bool bHorz( nFlags
& StartAutoScrollFlags::Horz
);
231 const bool bVert( nFlags
& StartAutoScrollFlags::Vert
);
235 if( mnActDist
< WHEEL_RADIUS
)
238 eStyle
= PointerStyle::AutoScrollNSWE
;
240 eStyle
= PointerStyle::AutoScrollWE
;
242 eStyle
= PointerStyle::AutoScrollNS
;
246 double fAngle
= basegfx::rad2deg(atan2(static_cast<double>(-nDistY
), nDistX
));
253 if( fAngle
>= 22.5 && fAngle
<= 67.5 )
254 eStyle
= PointerStyle::AutoScrollNE
;
255 else if( fAngle
>= 67.5 && fAngle
<= 112.5 )
256 eStyle
= PointerStyle::AutoScrollN
;
257 else if( fAngle
>= 112.5 && fAngle
<= 157.5 )
258 eStyle
= PointerStyle::AutoScrollNW
;
259 else if( fAngle
>= 157.5 && fAngle
<= 202.5 )
260 eStyle
= PointerStyle::AutoScrollW
;
261 else if( fAngle
>= 202.5 && fAngle
<= 247.5 )
262 eStyle
= PointerStyle::AutoScrollSW
;
263 else if( fAngle
>= 247.5 && fAngle
<= 292.5 )
264 eStyle
= PointerStyle::AutoScrollS
;
265 else if( fAngle
>= 292.5 && fAngle
<= 337.5 )
266 eStyle
= PointerStyle::AutoScrollSE
;
268 eStyle
= PointerStyle::AutoScrollE
;
272 if( fAngle
>= 270. || fAngle
<= 90. )
273 eStyle
= PointerStyle::AutoScrollE
;
275 eStyle
= PointerStyle::AutoScrollW
;
279 if( fAngle
>= 0. && fAngle
<= 180. )
280 eStyle
= PointerStyle::AutoScrollN
;
282 eStyle
= PointerStyle::AutoScrollS
;
287 eStyle
= PointerStyle::Arrow
;
292 void ImplWheelWindow::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
&)
294 ImplDrawWheel(rRenderContext
);
297 void ImplWheelWindow::MouseMove( const MouseEvent
& rMEvt
)
299 FloatingWindow::MouseMove( rMEvt
);
301 const Point
aMousePos( OutputToScreenPixel( rMEvt
.GetPosPixel() ) );
302 const tools::Long nDistX
= aMousePos
.X() - maCenter
.X();
303 const tools::Long nDistY
= aMousePos
.Y() - maCenter
.Y();
305 mnActDist
= static_cast<sal_uLong
>(hypot( static_cast<double>(nDistX
), nDistY
));
307 const PointerStyle eActStyle
= ImplGetMousePointer( nDistX
, nDistY
);
308 const StartAutoScrollFlags nFlags
= ImplGetSVData()->mpWinData
->mnAutoScrollFlags
;
309 const bool bHorz( nFlags
& StartAutoScrollFlags::Horz
);
310 const bool bVert( nFlags
& StartAutoScrollFlags::Vert
);
311 const bool bOuter
= mnActDist
> WHEEL_RADIUS
;
313 if( bOuter
&& ( maLastMousePos
!= aMousePos
) )
317 case PointerStyle::AutoScrollN
: mnActDeltaX
= +0; mnActDeltaY
= +1; break;
318 case PointerStyle::AutoScrollS
: mnActDeltaX
= +0; mnActDeltaY
= -1; break;
319 case PointerStyle::AutoScrollW
: mnActDeltaX
= +1; mnActDeltaY
= +0; break;
320 case PointerStyle::AutoScrollE
: mnActDeltaX
= -1; mnActDeltaY
= +0; break;
321 case PointerStyle::AutoScrollNW
: mnActDeltaX
= +1; mnActDeltaY
= +1; break;
322 case PointerStyle::AutoScrollNE
: mnActDeltaX
= -1; mnActDeltaY
= +1; break;
323 case PointerStyle::AutoScrollSW
: mnActDeltaX
= +1; mnActDeltaY
= -1; break;
324 case PointerStyle::AutoScrollSE
: mnActDeltaX
= -1; mnActDeltaY
= -1; break;
331 ImplRecalcScrollValues();
332 maLastMousePos
= aMousePos
;
333 SetPointer( eActStyle
);
336 ImplSetWheelMode( bOuter
? WheelMode::ScrollVH
: WheelMode::VH
);
338 ImplSetWheelMode( bOuter
? WheelMode::ScrollH
: WheelMode::H
);
340 ImplSetWheelMode( bOuter
? WheelMode::ScrollV
: WheelMode::V
);
343 void ImplWheelWindow::MouseButtonUp( const MouseEvent
& rMEvt
)
345 if( mnActDist
> WHEEL_RADIUS
)
346 GetParent()->EndAutoScroll();
348 FloatingWindow::MouseButtonUp( rMEvt
);
351 IMPL_LINK_NOARG(ImplWheelWindow
, ImplScrollHdl
, Timer
*, void)
353 if ( mnActDeltaX
|| mnActDeltaY
)
355 vcl::Window
* pWindow
= GetParent();
356 const Point
aMousePos( pWindow
->OutputToScreenPixel( pWindow
->GetPointerPosPixel() ) );
357 Point
aCmdMousePos( pWindow
->ImplFrameToOutput( aMousePos
) );
358 CommandScrollData
aScrollData( mnActDeltaX
, mnActDeltaY
);
359 CommandEvent
aCEvt( aCmdMousePos
, CommandEventId::AutoScroll
, true, &aScrollData
);
360 NotifyEvent
aNCmdEvt( NotifyEventType::COMMAND
, pWindow
, &aCEvt
);
362 if ( !ImplCallPreNotify( aNCmdEvt
) )
364 const sal_uInt64 nTime
= tools::Time::GetSystemTicks();
365 VclPtr
<ImplWheelWindow
> xWin(this);
366 pWindow
->Command( aCEvt
);
367 if( xWin
->isDisposed() )
369 mnRepaintTime
= std::max( tools::Time::GetSystemTicks() - nTime
, sal_uInt64(1) );
370 ImplRecalcScrollValues();
374 if ( mnTimeout
!= mpTimer
->GetTimeout() )
375 mpTimer
->SetTimeout( mnTimeout
);
379 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */