bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / window / scrwnd.cxx
blob27d96f56bfaff5fe0fa0eee104fe00056480d2ef
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <limits.h>
22 #include <o3tl/float_int_conversion.hxx>
23 #include <tools/time.hxx>
25 #include <bitmaps.hlst>
26 #include <svdata.hxx>
27 #include <scrwnd.hxx>
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>
35 #include <math.h>
37 #define WHEEL_WIDTH 25
38 #define WHEEL_RADIUS ((WHEEL_WIDTH) >> 1 )
39 #define MAX_TIME 300
40 #define MIN_TIME 20
41 #define DEF_TIMEOUT 50
43 ImplWheelWindow::ImplWheelWindow( vcl::Window* pParent ) :
44 FloatingWindow ( pParent, 0 ),
45 mnRepaintTime ( 1 ),
46 mnTimeout ( DEF_TIMEOUT ),
47 mnWheelMode ( WheelMode::NONE ),
48 mnActDist ( 0 ),
49 mnActDeltaX ( 0 ),
50 mnActDeltaY ( 0 )
52 // we need a parent
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());
69 // set wheel mode
70 if( bHorz && bVert )
71 ImplSetWheelMode( WheelMode::VH );
72 else if( bHorz )
73 ImplSetWheelMode( WheelMode::H );
74 else
75 ImplSetWheelMode( WheelMode::V );
77 // init timer
78 mpTimer.reset(new Timer("WheelWindowTimer"));
79 mpTimer->SetInvokeHandler( LINK( this, ImplWheelWindow, ImplScrollHdl ) );
80 mpTimer->SetTimeout( mnTimeout );
81 mpTimer->Start();
83 CaptureMouse();
86 ImplWheelWindow::~ImplWheelWindow()
88 disposeOnce();
91 void ImplWheelWindow::dispose()
93 ImplStop();
94 mpTimer.reset();
95 FloatingWindow::dispose();
98 void ImplWheelWindow::ImplStop()
100 ReleaseMouse();
101 mpTimer->Stop();
102 Show(false);
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 )
137 if( IsVisible() )
138 Hide();
140 else
142 if( !IsVisible() )
143 Show();
145 Invalidate();
150 void ImplWheelWindow::ImplDrawWheel(vcl::RenderContext& rRenderContext)
152 int nIndex;
154 switch (mnWheelMode)
156 case WheelMode::VH:
157 nIndex = 0;
158 break;
159 case WheelMode::V:
160 nIndex = 1;
161 break;
162 case WheelMode::H:
163 nIndex = 2;
164 break;
165 case WheelMode::ScrollVH:
166 nIndex = 3;
167 break;
168 case WheelMode::ScrollV:
169 nIndex = 4;
170 break;
171 case WheelMode::ScrollH:
172 nIndex = 5;
173 break;
174 default:
175 nIndex = -1;
176 break;
179 if (nIndex >= 0)
180 rRenderContext.DrawImage(Point(), maImgList[nIndex]);
183 void ImplWheelWindow::ImplRecalcScrollValues()
185 if( mnActDist < WHEEL_RADIUS )
187 mnActDeltaX = mnActDeltaY = 0;
188 mnTimeout = DEF_TIMEOUT;
190 else
192 sal_uInt64 nCurTime;
194 // calc current time
195 if( mnMaxWidth )
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 ) );
200 else
201 nCurTime = MAX_TIME;
203 if( !nCurTime )
204 nCurTime = 1;
206 if( mnRepaintTime <= nCurTime )
207 mnTimeout = nCurTime - mnRepaintTime;
208 else
210 sal_uInt64 nMult = mnRepaintTime / nCurTime;
212 if( !( mnRepaintTime % nCurTime ) )
213 mnTimeout = 0;
214 else
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;
224 else
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;
231 else
232 mnActDeltaY = static_cast<long>(fValY);
237 PointerStyle ImplWheelWindow::ImplGetMousePointer( long nDistX, long nDistY )
239 PointerStyle eStyle;
240 const StartAutoScrollFlags nFlags = ImplGetSVData()->maWinData.mnAutoScrollFlags;
241 const bool bHorz( nFlags & StartAutoScrollFlags::Horz );
242 const bool bVert( nFlags & StartAutoScrollFlags::Vert );
244 if( bHorz || bVert )
246 if( mnActDist < WHEEL_RADIUS )
248 if( bHorz && bVert )
249 eStyle = PointerStyle::AutoScrollNSWE;
250 else if( bHorz )
251 eStyle = PointerStyle::AutoScrollWE;
252 else
253 eStyle = PointerStyle::AutoScrollNS;
255 else
257 double fAngle = basegfx::rad2deg(atan2(static_cast<double>(-nDistY), nDistX));
259 if( fAngle < 0.0 )
260 fAngle += 360.;
262 if( bHorz && bVert )
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;
278 else
279 eStyle = PointerStyle::AutoScrollE;
281 else if( bHorz )
283 if( fAngle >= 270. || fAngle <= 90. )
284 eStyle = PointerStyle::AutoScrollE;
285 else
286 eStyle = PointerStyle::AutoScrollW;
288 else
290 if( fAngle >= 0. && fAngle <= 180. )
291 eStyle = PointerStyle::AutoScrollN;
292 else
293 eStyle = PointerStyle::AutoScrollS;
297 else
298 eStyle = PointerStyle::Arrow;
300 return eStyle;
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 ) )
326 switch( eActStyle )
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;
337 default:
338 break;
342 ImplRecalcScrollValues();
343 maLastMousePos = aMousePos;
344 SetPointer( eActStyle );
346 if( bHorz && bVert )
347 ImplSetWheelMode( bOuter ? WheelMode::ScrollVH : WheelMode::VH );
348 else if( bHorz )
349 ImplSetWheelMode( bOuter ? WheelMode::ScrollH : WheelMode::H );
350 else
351 ImplSetWheelMode( bOuter ? WheelMode::ScrollV : WheelMode::V );
354 void ImplWheelWindow::MouseButtonUp( const MouseEvent& rMEvt )
356 if( mnActDist > WHEEL_RADIUS )
357 GetParent()->EndAutoScroll();
358 else
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() )
379 return;
380 mnRepaintTime = std::max( tools::Time::GetSystemTicks() - nTime, sal_uInt64(1) );
381 ImplRecalcScrollValues();
385 if ( mnTimeout != mpTimer->GetTimeout() )
386 mpTimer->SetTimeout( mnTimeout );
387 mpTimer->Start();
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */