LibreOffice Module vcl (master) 1
scrwnd.cxx
Go to the documentation of this file.
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#include <limits.h>
21
23#include <tools/time.hxx>
24
25#include <bitmaps.hlst>
26#include <svdata.hxx>
27#include <scrwnd.hxx>
28
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>
34
35#include <math.h>
36
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
42
44 FloatingWindow ( pParent, 0 ),
45 mnRepaintTime ( 1 ),
46 mnTimeout ( DEF_TIMEOUT ),
47 mnWheelMode ( WheelMode::NONE ),
48 mnActDist ( 0 ),
49 mnActDeltaX ( 0 ),
50 mnActDeltaY ( 0 )
51{
52 // we need a parent
53 SAL_WARN_IF( !pParent, "vcl", "ImplWheelWindow::ImplWheelWindow(): Parent not set!" );
54
55 const Size aSize( pParent->GetOutputSizePixel() );
57 const bool bHorz( nFlags & StartAutoScrollFlags::Horz );
58 const bool bVert( nFlags & StartAutoScrollFlags::Vert );
59
60 // calculate maximum speed distance
61 mnMaxWidth = static_cast<sal_uLong>( 0.4 * hypot( static_cast<double>(aSize.Width()), aSize.Height() ) );
62
63 // create wheel window
66 BitmapEx aBmp(SV_RESID_BITMAP_SCROLLMSK);
68
69 // set wheel mode
70 if( bHorz && bVert )
72 else if( bHorz )
74 else
76
77 // init timer
78 mpTimer.reset(new Timer("WheelWindowTimer"));
79 mpTimer->SetInvokeHandler( LINK( this, ImplWheelWindow, ImplScrollHdl ) );
80 mpTimer->SetTimeout( mnTimeout );
81 mpTimer->Start();
82
84}
85
87{
89}
90
92{
93 ImplStop();
94 mpTimer.reset();
96}
97
99{
100 ReleaseMouse();
101 mpTimer->Stop();
102 Show(false);
103}
104
105void ImplWheelWindow::ImplSetRegion( const Bitmap& rRegionBmp )
106{
107 Point aPos( GetPointerPosPixel() );
108 const Size aSize( rRegionBmp.GetSizePixel() );
109 const tools::Rectangle aRect( Point(), aSize );
110
111 maCenter = maLastMousePos = aPos;
112 aPos.AdjustX( -(aSize.Width() >> 1) );
113 aPos.AdjustY( -(aSize.Height() >> 1) );
114
115 SetPosSizePixel( aPos, aSize );
116 SetWindowRegionPixel( rRegionBmp.CreateRegion( COL_BLACK, aRect ) );
117}
118
120{
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);
127}
128
130{
131 if( nWheelMode == mnWheelMode )
132 return;
133
134 mnWheelMode = nWheelMode;
135
137 {
138 if( IsVisible() )
139 Hide();
140 }
141 else
142 {
143 if( !IsVisible() )
144 Show();
145
146 Invalidate();
147 }
148}
149
151{
152 int nIndex;
153
154 switch (mnWheelMode)
155 {
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;
166 nIndex = 3;
167 break;
169 nIndex = 4;
170 break;
172 nIndex = 5;
173 break;
174 default:
175 nIndex = -1;
176 break;
177 }
178
179 if (nIndex >= 0)
180 rRenderContext.DrawImage(Point(), maImgList[nIndex]);
181}
182
184{
185 if( mnActDist < WHEEL_RADIUS )
186 {
189 }
190 else
191 {
192 sal_uInt64 nCurTime;
193
194 // calc current time
195 if( mnMaxWidth )
196 {
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 ) );
199 }
200 else
201 nCurTime = MAX_TIME;
202
203 if( !nCurTime )
204 nCurTime = 1;
205
206 if( mnRepaintTime <= nCurTime )
207 mnTimeout = nCurTime - mnRepaintTime;
208 else
209 {
210 sal_uInt64 nMult = mnRepaintTime / nCurTime;
211
212 if( !( mnRepaintTime % nCurTime ) )
213 mnTimeout = 0;
214 else
215 mnTimeout = ++nMult * nCurTime - mnRepaintTime;
216
217 double fValX = static_cast<double>(mnActDeltaX) * nMult;
218 double fValY = static_cast<double>(mnActDeltaY) * nMult;
219
220 mnActDeltaX = o3tl::saturating_cast<tools::Long>(fValX);
221 mnActDeltaY = o3tl::saturating_cast<tools::Long>(fValY);
222 }
223 }
224}
225
227{
228 PointerStyle eStyle;
230 const bool bHorz( nFlags & StartAutoScrollFlags::Horz );
231 const bool bVert( nFlags & StartAutoScrollFlags::Vert );
232
233 if( bHorz || bVert )
234 {
235 if( mnActDist < WHEEL_RADIUS )
236 {
237 if( bHorz && bVert )
239 else if( bHorz )
241 else
243 }
244 else
245 {
246 double fAngle = basegfx::rad2deg(atan2(static_cast<double>(-nDistY), nDistX));
247
248 if( fAngle < 0.0 )
249 fAngle += 360.;
250
251 if( bHorz && bVert )
252 {
253 if( fAngle >= 22.5 && fAngle <= 67.5 )
255 else if( fAngle >= 67.5 && fAngle <= 112.5 )
257 else if( fAngle >= 112.5 && fAngle <= 157.5 )
259 else if( fAngle >= 157.5 && fAngle <= 202.5 )
261 else if( fAngle >= 202.5 && fAngle <= 247.5 )
263 else if( fAngle >= 247.5 && fAngle <= 292.5 )
265 else if( fAngle >= 292.5 && fAngle <= 337.5 )
267 else
269 }
270 else if( bHorz )
271 {
272 if( fAngle >= 270. || fAngle <= 90. )
274 else
276 }
277 else
278 {
279 if( fAngle >= 0. && fAngle <= 180. )
281 else
283 }
284 }
285 }
286 else
287 eStyle = PointerStyle::Arrow;
288
289 return eStyle;
290}
291
293{
294 ImplDrawWheel(rRenderContext);
295}
296
298{
300
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();
304
305 mnActDist = static_cast<sal_uLong>(hypot( static_cast<double>(nDistX), nDistY ));
306
307 const PointerStyle eActStyle = ImplGetMousePointer( nDistX, nDistY );
309 const bool bHorz( nFlags & StartAutoScrollFlags::Horz );
310 const bool bVert( nFlags & StartAutoScrollFlags::Vert );
311 const bool bOuter = mnActDist > WHEEL_RADIUS;
312
313 if( bOuter && ( maLastMousePos != aMousePos ) )
314 {
315 switch( eActStyle )
316 {
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;
325
326 default:
327 break;
328 }
329 }
330
332 maLastMousePos = aMousePos;
333 SetPointer( eActStyle );
334
335 if( bHorz && bVert )
337 else if( bHorz )
339 else
341}
342
344{
345 if( mnActDist > WHEEL_RADIUS )
347 else
349}
350
351IMPL_LINK_NOARG(ImplWheelWindow, ImplScrollHdl, Timer *, void)
352{
353 if ( mnActDeltaX || mnActDeltaY )
354 {
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 );
361
362 if ( !ImplCallPreNotify( aNCmdEvt ) )
363 {
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();
371 }
372 }
373
374 if ( mnTimeout != mpTimer->GetTimeout() )
375 mpTimer->SetTimeout( mnTimeout );
376 mpTimer->Start();
377}
378
379/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:203
Size GetSizePixel() const
vcl::Region CreateRegion(const Color &rColor, const tools::Rectangle &rRect) const
Create region of similar colors in a given rectangle.
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects.
Definition: floatwin.cxx:204
void SetTitleType(FloatWinTitleType nTitle)
Definition: floatwin.cxx:756
sal_uLong mnActDist
Definition: scrwnd.hxx:54
virtual void MouseMove(const MouseEvent &rMEvt) override
Definition: scrwnd.cxx:297
Point maLastMousePos
Definition: scrwnd.hxx:47
void ImplRecalcScrollValues()
Definition: scrwnd.cxx:183
WheelMode mnWheelMode
Definition: scrwnd.hxx:52
tools::Long mnActDeltaY
Definition: scrwnd.hxx:56
virtual ~ImplWheelWindow() override
Definition: scrwnd.cxx:86
void ImplSetWheelMode(WheelMode nWheelMode)
Definition: scrwnd.cxx:129
virtual void MouseButtonUp(const MouseEvent &rMEvt) override
Definition: scrwnd.cxx:343
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects.
Definition: scrwnd.cxx:91
sal_uInt64 mnRepaintTime
Definition: scrwnd.hxx:50
void ImplSetRegion(const Bitmap &rRegionBmp)
Definition: scrwnd.cxx:105
std::vector< Image > maImgList
Definition: scrwnd.hxx:46
void ImplStop()
Definition: scrwnd.cxx:98
Point maCenter
Definition: scrwnd.hxx:48
sal_uLong mnMaxWidth
Definition: scrwnd.hxx:53
void ImplCreateImageList()
Definition: scrwnd.cxx:119
void ImplDrawWheel(vcl::RenderContext &rRenderContext)
Definition: scrwnd.cxx:150
std::unique_ptr< Timer > mpTimer
Definition: scrwnd.hxx:49
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
Definition: scrwnd.cxx:292
tools::Long mnActDeltaX
Definition: scrwnd.hxx:55
sal_uInt64 mnTimeout
Definition: scrwnd.hxx:51
ImplWheelWindow(vcl::Window *pParent)
Definition: scrwnd.cxx:43
const Point & GetPosPixel() const
Definition: event.hxx:123
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:171
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr tools::Long Height() const
constexpr tools::Long Width() const
Definition: timer.hxx:27
bool isDisposed() const
static sal_uInt64 GetSystemTicks()
Point OutputToScreenPixel(const Point &rPos) const
Definition: window.cxx:2810
SAL_DLLPRIVATE Point ImplFrameToOutput(const Point &rPos)
Definition: window2.cxx:973
vcl::Window * GetParent() const
Definition: window2.cxx:1128
virtual void Command(const CommandEvent &rCEvt)
Definition: window.cxx:1927
virtual void MouseButtonUp(const MouseEvent &rMEvt)
Definition: mouse.cxx:427
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2191
void Hide()
Definition: window.hxx:885
virtual void MouseMove(const MouseEvent &rMEvt)
Definition: mouse.cxx:414
void ReleaseMouse()
Definition: mouse.cxx:469
Size GetOutputSizePixel() const
Definition: window3.cxx:89
Point GetPointerPosPixel()
Definition: mouse.cxx:564
void SetWindowRegionPixel()
Definition: paint.cxx:1037
void SetPointer(PointerStyle)
Definition: mouse.cxx:486
bool IsVisible() const
Definition: window2.cxx:1133
void CaptureMouse()
Definition: mouse.cxx:451
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
Definition: paint.cxx:1143
SAL_DLLPRIVATE PointerStyle ImplGetMousePointer() const
Definition: mouse.cxx:93
virtual void SetPosSizePixel(const Point &rNewPos, const Size &rNewSize)
Definition: window2.cxx:1299
void EndAutoScroll()
Definition: window2.cxx:362
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
sal_Int32 nIndex
#define SAL_WARN_IF(condition, area, stream)
NONE
constexpr double rad2deg(double v)
long Long
PointerStyle
Definition: ptrstyle.hxx:26
#define MIN_TIME
Definition: scrwnd.cxx:40
#define MAX_TIME
Definition: scrwnd.cxx:39
#define DEF_TIMEOUT
Definition: scrwnd.cxx:41
IMPL_LINK_NOARG(ImplWheelWindow, ImplScrollHdl, Timer *, void)
Definition: scrwnd.cxx:351
#define WHEEL_RADIUS
Definition: scrwnd.cxx:38
WheelMode
Definition: scrwnd.hxx:29
sal_uIntPtr sal_uLong
ImplSVWinData * mpWinData
Definition: svdata.hxx:403
StartAutoScrollFlags mnAutoScrollFlags
Definition: svdata.hxx:269
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:76
StartAutoScrollFlags
Definition: window.hxx:279
bool ImplCallPreNotify(NotifyEvent &rEvt)
Definition: winproc.cxx:67