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);
67  ImplSetRegion(aBmp.GetBitmap());
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 
83  CaptureMouse();
84 }
85 
87 {
88  disposeOnce();
89 }
90 
92 {
93  ImplStop();
94  mpTimer.reset();
96 }
97 
99 {
100  ReleaseMouse();
101  mpTimer->Stop();
102  Show(false);
103 }
104 
105 void 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(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));
127 }
128 
130 {
131  if( nWheelMode != mnWheelMode )
132  {
133  mnWheelMode = nWheelMode;
134 
136  {
137  if( IsVisible() )
138  Hide();
139  }
140  else
141  {
142  if( !IsVisible() )
143  Show();
144 
145  Invalidate();
146  }
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;
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;
177  }
178 
179  if (nIndex >= 0)
180  rRenderContext.DrawImage(Point(), maImgList[nIndex]);
181 }
182 
184 {
185  if( mnActDist < WHEEL_RADIUS )
186  {
187  mnActDeltaX = mnActDeltaY = 0;
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  if( !o3tl::convertsToAtMost(fValX, LONG_MAX) )
222  else if( !o3tl::convertsToAtLeast(fValX, LONG_MIN) )
223  mnActDeltaX = LONG_MIN;
224  else
225  mnActDeltaX = static_cast<long>(fValX);
226 
227  if( !o3tl::convertsToAtMost(fValY, LONG_MAX) )
229  else if( !o3tl::convertsToAtLeast(fValY, LONG_MIN) )
230  mnActDeltaY = LONG_MIN;
231  else
232  mnActDeltaY = static_cast<long>(fValY);
233  }
234  }
235 }
236 
238 {
239  PointerStyle eStyle;
241  const bool bHorz( nFlags & StartAutoScrollFlags::Horz );
242  const bool bVert( nFlags & StartAutoScrollFlags::Vert );
243 
244  if( bHorz || bVert )
245  {
246  if( mnActDist < WHEEL_RADIUS )
247  {
248  if( bHorz && bVert )
250  else if( bHorz )
252  else
254  }
255  else
256  {
257  double fAngle = basegfx::rad2deg(atan2(static_cast<double>(-nDistY), nDistX));
258 
259  if( fAngle < 0.0 )
260  fAngle += 360.;
261 
262  if( bHorz && bVert )
263  {
264  if( fAngle >= 22.5 && fAngle <= 67.5 )
266  else if( fAngle >= 67.5 && fAngle <= 112.5 )
267  eStyle = PointerStyle::AutoScrollN;
268  else if( fAngle >= 112.5 && fAngle <= 157.5 )
270  else if( fAngle >= 157.5 && fAngle <= 202.5 )
271  eStyle = PointerStyle::AutoScrollW;
272  else if( fAngle >= 202.5 && fAngle <= 247.5 )
274  else if( fAngle >= 247.5 && fAngle <= 292.5 )
275  eStyle = PointerStyle::AutoScrollS;
276  else if( fAngle >= 292.5 && fAngle <= 337.5 )
278  else
279  eStyle = PointerStyle::AutoScrollE;
280  }
281  else if( bHorz )
282  {
283  if( fAngle >= 270. || fAngle <= 90. )
284  eStyle = PointerStyle::AutoScrollE;
285  else
286  eStyle = PointerStyle::AutoScrollW;
287  }
288  else
289  {
290  if( fAngle >= 0. && fAngle <= 180. )
291  eStyle = PointerStyle::AutoScrollN;
292  else
293  eStyle = PointerStyle::AutoScrollS;
294  }
295  }
296  }
297  else
298  eStyle = PointerStyle::Arrow;
299 
300  return eStyle;
301 }
302 
304 {
305  ImplDrawWheel(rRenderContext);
306 }
307 
309 {
310  FloatingWindow::MouseMove( rMEvt );
311 
312  const Point aMousePos( OutputToScreenPixel( rMEvt.GetPosPixel() ) );
313  const long nDistX = aMousePos.X() - maCenter.X();
314  const long nDistY = aMousePos.Y() - maCenter.Y();
315 
316  mnActDist = static_cast<sal_uLong>(hypot( static_cast<double>(nDistX), nDistY ));
317 
318  const PointerStyle eActStyle = ImplGetMousePointer( nDistX, nDistY );
320  const bool bHorz( nFlags & StartAutoScrollFlags::Horz );
321  const bool bVert( nFlags & StartAutoScrollFlags::Vert );
322  const bool bOuter = mnActDist > WHEEL_RADIUS;
323 
324  if( bOuter && ( maLastMousePos != aMousePos ) )
325  {
326  switch( eActStyle )
327  {
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;
336 
337  default:
338  break;
339  }
340  }
341 
343  maLastMousePos = aMousePos;
344  SetPointer( eActStyle );
345 
346  if( bHorz && bVert )
348  else if( bHorz )
350  else
352 }
353 
355 {
356  if( mnActDist > WHEEL_RADIUS )
358  else
360 }
361 
362 IMPL_LINK_NOARG(ImplWheelWindow, ImplScrollHdl, Timer *, void)
363 {
364  if ( mnActDeltaX || mnActDeltaY )
365  {
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 );
372 
373  if ( !ImplCallPreNotify( aNCmdEvt ) )
374  {
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();
382  }
383  }
384 
385  if ( mnTimeout != mpTimer->GetTimeout() )
386  mpTimer->SetTimeout( mnTimeout );
387  mpTimer->Start();
388 }
389 
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void CaptureMouse()
Definition: mouse.cxx:438
Point GetPointerPosPixel()
Definition: mouse.cxx:548
#define MIN_TIME
Definition: scrwnd.cxx:40
long mnActDeltaY
Definition: scrwnd.hxx:56
sal_uLong mnActDist
Definition: scrwnd.hxx:54
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
void EndAutoScroll()
Definition: window2.cxx:332
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...
bool ImplCallPreNotify(NotifyEvent &rEvt)
Definition: winproc.cxx:58
virtual ~ImplWheelWindow() override
Definition: scrwnd.cxx:86
void ImplCreateImageList()
Definition: scrwnd.cxx:119
void ImplStop()
Definition: scrwnd.cxx:98
long AdjustX(long nHorzMove)
StartAutoScrollFlags
Definition: window.hxx:297
sal_uIntPtr sal_uLong
sal_uLong mnMaxWidth
Definition: scrwnd.hxx:53
#define DEF_TIMEOUT
Definition: scrwnd.cxx:41
Size GetSizePixel() const
virtual void MouseButtonUp(const MouseEvent &rMEvt)
Definition: mouse.cxx:419
constexpr double rad2deg(double v)
sal_uInt64 mnRepaintTime
Definition: scrwnd.hxx:50
std::unique_ptr< Timer > mpTimer
Definition: scrwnd.hxx:49
sal_uInt64 mnTimeout
Definition: scrwnd.hxx:51
void Hide()
Definition: window.hxx:930
#define MAX_TIME
Definition: scrwnd.cxx:39
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
WheelMode mnWheelMode
Definition: scrwnd.hxx:52
void SetWindowRegionPixel()
Definition: paint.cxx:1027
IMPL_LINK_NOARG(ImplWheelWindow, ImplScrollHdl, Timer *, void)
Definition: scrwnd.cxx:362
SAL_DLLPRIVATE PointerStyle ImplGetMousePointer() const
Definition: mouse.cxx:92
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
void ImplSetWheelMode(WheelMode nWheelMode)
Definition: scrwnd.cxx:129
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:66
NONE
StartAutoScrollFlags mnAutoScrollFlags
Definition: svdata.hxx:215
ImplWheelWindow(vcl::Window *pParent)
Definition: scrwnd.cxx:43
long AdjustY(long nVertMove)
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
Definition: paint.cxx:1153
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:304
vcl::Region CreateRegion(const Color &rColor, const tools::Rectangle &rRect) const
Create region of similar colors in a given rectangle.
virtual void Paint(vcl::RenderContext &rRenderContext, const tools::Rectangle &rRect) override
Definition: scrwnd.cxx:303
std::enable_if_t< std::is_floating_point_v< F > &&std::is_integral_v< I >, bool > convertsToAtMost(F value, I max)
Size GetOutputSizePixel() const
Definition: outdev.hxx:441
#define WHEEL_RADIUS
Definition: scrwnd.cxx:38
vcl::Window * GetParent() const
Definition: window2.cxx:1091
long X() const
Point maCenter
Definition: scrwnd.hxx:48
const long LONG_MAX
virtual void SetPosSizePixel(const Point &rNewPos, const Size &rNewSize)
Definition: window2.cxx:1262
Point maLastMousePos
Definition: scrwnd.hxx:47
void ReleaseMouse()
Definition: mouse.cxx:457
ImplSVWinData maWinData
Definition: svdata.hxx:354
void SetPointer(PointerStyle)
Definition: mouse.cxx:473
void ImplRecalcScrollValues()
Definition: scrwnd.cxx:183
#define SAL_WARN_IF(condition, area, stream)
bool IsVisible() const
Definition: window2.cxx:1096
WheelMode
Definition: scrwnd.hxx:29
Definition: image.hxx:51
SAL_DLLPRIVATE Point ImplFrameToOutput(const Point &rPos)
Definition: window2.cxx:936
std::enable_if_t< std::is_floating_point_v< F > &&std::is_integral_v< I >, bool > convertsToAtLeast(F value, I min)
void ImplDrawWheel(vcl::RenderContext &rRenderContext)
Definition: scrwnd.cxx:150
const Point & GetPosPixel() const
Definition: event.hxx:117
static sal_uInt64 GetSystemTicks()
PointerStyle
Definition: ptrstyle.hxx:25
bool IsDisposed() const
Definition: window.cxx:135
long mnActDeltaX
Definition: scrwnd.hxx:55
virtual void MouseMove(const MouseEvent &rMEvt)
Definition: mouse.cxx:406
Definition: timer.hxx:26
Point OutputToScreenPixel(const Point &rPos) const
Definition: window.cxx:2818
std::vector< Image > maImgList
Definition: scrwnd.hxx:46
virtual void Command(const CommandEvent &rCEvt)
Definition: window.cxx:1886
virtual void MouseMove(const MouseEvent &rMEvt) override
Definition: scrwnd.cxx:308
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: floatwin.cxx:203
void SetTitleType(FloatWinTitleType nTitle)
Definition: floatwin.cxx:701
void ImplSetRegion(const Bitmap &rRegionBmp)
Definition: scrwnd.cxx:105
long Y() const
virtual void MouseButtonUp(const MouseEvent &rMEvt) override
Definition: scrwnd.cxx:354
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2150