bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / source / window / scrwnd.cxx
blob934dceaff7e33866a0d13cfe5c08f1abc7699580
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()->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());
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(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 )
132 return;
134 mnWheelMode = nWheelMode;
136 if( WheelMode::NONE == mnWheelMode )
138 if( IsVisible() )
139 Hide();
141 else
143 if( !IsVisible() )
144 Show();
146 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 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
228 PointerStyle eStyle;
229 const StartAutoScrollFlags nFlags = ImplGetSVData()->mpWinData->mnAutoScrollFlags;
230 const bool bHorz( nFlags & StartAutoScrollFlags::Horz );
231 const bool bVert( nFlags & StartAutoScrollFlags::Vert );
233 if( bHorz || bVert )
235 if( mnActDist < WHEEL_RADIUS )
237 if( bHorz && bVert )
238 eStyle = PointerStyle::AutoScrollNSWE;
239 else if( bHorz )
240 eStyle = PointerStyle::AutoScrollWE;
241 else
242 eStyle = PointerStyle::AutoScrollNS;
244 else
246 double fAngle = basegfx::rad2deg(atan2(static_cast<double>(-nDistY), nDistX));
248 if( fAngle < 0.0 )
249 fAngle += 360.;
251 if( bHorz && bVert )
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;
267 else
268 eStyle = PointerStyle::AutoScrollE;
270 else if( bHorz )
272 if( fAngle >= 270. || fAngle <= 90. )
273 eStyle = PointerStyle::AutoScrollE;
274 else
275 eStyle = PointerStyle::AutoScrollW;
277 else
279 if( fAngle >= 0. && fAngle <= 180. )
280 eStyle = PointerStyle::AutoScrollN;
281 else
282 eStyle = PointerStyle::AutoScrollS;
286 else
287 eStyle = PointerStyle::Arrow;
289 return eStyle;
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 ) )
315 switch( eActStyle )
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;
326 default:
327 break;
331 ImplRecalcScrollValues();
332 maLastMousePos = aMousePos;
333 SetPointer( eActStyle );
335 if( bHorz && bVert )
336 ImplSetWheelMode( bOuter ? WheelMode::ScrollVH : WheelMode::VH );
337 else if( bHorz )
338 ImplSetWheelMode( bOuter ? WheelMode::ScrollH : WheelMode::H );
339 else
340 ImplSetWheelMode( bOuter ? WheelMode::ScrollV : WheelMode::V );
343 void ImplWheelWindow::MouseButtonUp( const MouseEvent& rMEvt )
345 if( mnActDist > WHEEL_RADIUS )
346 GetParent()->EndAutoScroll();
347 else
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() )
368 return;
369 mnRepaintTime = std::max( tools::Time::GetSystemTicks() - nTime, sal_uInt64(1) );
370 ImplRecalcScrollValues();
374 if ( mnTimeout != mpTimer->GetTimeout() )
375 mpTimer->SetTimeout( mnTimeout );
376 mpTimer->Start();
379 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */