KiCad PCB EDA Suite
wx_view_controls.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5  * Copyright (C) 2013-2015 CERN
6  * Copyright (C) 2012-2016 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
9  * @author Maciej Suminski <maciej.suminski@cern.ch>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <pgm_base.h>
30 #include <view/view.h>
31 #include <view/wx_view_controls.h>
32 #include <view/zoom_controller.h>
34 #include <tool/tool_dispatcher.h>
36 #include <math/util.h> // for KiROUND
37 
38 
39 using namespace KIGFX;
40 
41 const wxEventType WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE = wxNewEventType();
42 
43 
44 static std::unique_ptr<ZOOM_CONTROLLER> GetZoomControllerForPlatform()
45 {
46 #ifdef __WXMAC__
47  // On Apple pointer devices, wheel events occur frequently and with
48  // smaller rotation values. For those devices, let's handle zoom
49  // based on the rotation amount rather than the time difference.
50  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::MAC_SCALE );
51 #elif __WXGTK3__
52  // GTK3 is similar, but the scale constant is smaller
53  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::GTK3_SCALE );
54 #else
55  return std::make_unique<ACCELERATING_ZOOM_CONTROLLER>();
56 #endif
57 }
58 
59 
60 WX_VIEW_CONTROLS::WX_VIEW_CONTROLS( VIEW* aView, wxScrolledCanvas* aParentPanel ) :
61  VIEW_CONTROLS( aView ),
62  m_state( IDLE ),
63  m_parentPanel( aParentPanel ),
64  m_scrollScale( 1.0, 1.0 ),
65 #ifdef __WXGTK3__
66  m_lastTimestamp( 0 ),
67 #endif
68  m_cursorPos( 0, 0 ),
69  m_updateCursor( true )
70 {
71  bool enableMousewheelPan = Pgm().GetCommonSettings()->m_Input.mousewheel_pan;
72  bool enableZoomNoCenter = !Pgm().GetCommonSettings()->m_Input.center_on_zoom;
73  bool enableAutoPan = Pgm().GetCommonSettings()->m_Input.auto_pan;
74 
75  m_settings.m_enableMousewheelPan = enableMousewheelPan;
76  m_settings.m_warpCursor = !enableZoomNoCenter;
77  m_settings.m_autoPanSettingEnabled = enableAutoPan;
78 
79  m_parentPanel->Connect( wxEVT_MOTION,
80  wxMouseEventHandler( WX_VIEW_CONTROLS::onMotion ), NULL, this );
81 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
82  m_parentPanel->Connect( wxEVT_MAGNIFY,
83  wxMouseEventHandler( WX_VIEW_CONTROLS::onMagnify ), NULL, this );
84 #endif
85  m_parentPanel->Connect( wxEVT_MOUSEWHEEL,
86  wxMouseEventHandler( WX_VIEW_CONTROLS::onWheel ), NULL, this );
87  m_parentPanel->Connect( wxEVT_MIDDLE_UP,
88  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
89  m_parentPanel->Connect( wxEVT_MIDDLE_DOWN,
90  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
91  m_parentPanel->Connect( wxEVT_LEFT_UP,
92  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
93  m_parentPanel->Connect( wxEVT_LEFT_DOWN,
94  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
95  m_parentPanel->Connect( wxEVT_RIGHT_UP,
96  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
97  m_parentPanel->Connect( wxEVT_RIGHT_DOWN,
98  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
99 #if defined _WIN32 || defined _WIN64
100  m_parentPanel->Connect( wxEVT_ENTER_WINDOW,
101  wxMouseEventHandler( WX_VIEW_CONTROLS::onEnter ), NULL, this );
102 #endif
103  m_parentPanel->Connect( wxEVT_LEAVE_WINDOW,
104  wxMouseEventHandler( WX_VIEW_CONTROLS::onLeave ), NULL, this );
105  m_parentPanel->Connect( wxEVT_SCROLLWIN_THUMBTRACK,
106  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
107  m_parentPanel->Connect( wxEVT_SCROLLWIN_PAGEUP,
108  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
109  m_parentPanel->Connect( wxEVT_SCROLLWIN_PAGEDOWN,
110  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
111 
112  m_parentPanel->Connect( wxEVT_SCROLLWIN_BOTTOM,
113  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
114  m_parentPanel->Connect( wxEVT_SCROLLWIN_TOP,
115  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
116  m_parentPanel->Connect( wxEVT_SCROLLWIN_LINEUP,
117  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
118  m_parentPanel->Connect( wxEVT_SCROLLWIN_LINEDOWN,
119  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
120 
122 
123  m_cursorWarped = false;
124 
125  m_panTimer.SetOwner( this );
126  this->Connect( wxEVT_TIMER, wxTimerEventHandler( WX_VIEW_CONTROLS::onTimer ), NULL, this );
127 
131 }
132 
133 
135 {
136  COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
137 
138  if( cfg )
139  {
143  }
144 }
145 
146 
147 void WX_VIEW_CONTROLS::onMotion( wxMouseEvent& aEvent )
148 {
149  bool isAutoPanning = false;
150  VECTOR2D mousePos( aEvent.GetX(), aEvent.GetY() );
151 
153  isAutoPanning = handleAutoPanning( aEvent );
154 
155  if( !isAutoPanning && aEvent.Dragging() )
156  {
157  if( m_state == DRAG_PANNING )
158  {
159  VECTOR2D d = m_dragStartPoint - mousePos;
160  VECTOR2D delta = m_view->ToWorld( d, false );
161 
162  m_view->SetCenter( m_lookStartPoint + delta );
163  aEvent.StopPropagation();
164  }
165  }
166 
167  if( m_updateCursor ) // do not update the cursor position if it was explicitly set
168  m_cursorPos = m_view->ToWorld( mousePos );
169  else
170  m_updateCursor = true;
171 
172  aEvent.Skip();
173 }
174 
175 
176 void WX_VIEW_CONTROLS::onWheel( wxMouseEvent& aEvent )
177 {
178 #ifdef __WXGTK3__
179  if( aEvent.GetTimestamp() == m_lastTimestamp )
180  {
181  aEvent.Skip( false );
182  return;
183  }
184 
185  m_lastTimestamp = aEvent.GetTimestamp();
186 #endif
187 
188  const double wheelPanSpeed = 0.001;
189  const int axis = aEvent.GetWheelAxis();
190 
191  // mousewheelpan disabled:
192  // wheel + ctrl -> horizontal scrolling;
193  // wheel + shift -> vertical scrolling;
194  // wheel -> zooming;
195  // mousewheelpan enabled:
196  // wheel -> pan;
197  // wheel + ctrl -> zooming;
198  // wheel + shift -> horizontal scrolling.
199 
200  if( ( !m_settings.m_enableMousewheelPan && ( aEvent.ControlDown() || aEvent.ShiftDown() ) ) ||
201  ( m_settings.m_enableMousewheelPan && !aEvent.ControlDown() ) )
202  {
203  // Scrolling
204  VECTOR2D scrollVec = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) *
205  ( (double) aEvent.GetWheelRotation() * wheelPanSpeed );
206  double scrollX = 0.0;
207  double scrollY = 0.0;
208 
210  {
211  if ( axis == wxMOUSE_WHEEL_HORIZONTAL || aEvent.ShiftDown() )
212  scrollX = scrollVec.x;
213  else
214  scrollY = -scrollVec.y;
215  }
216  else
217  {
218  if( aEvent.ControlDown() )
219  scrollX = -scrollVec.x;
220  else
221  scrollY = -scrollVec.y;
222  }
223 
224  VECTOR2D delta( scrollX, scrollY );
225 
226  m_view->SetCenter( m_view->GetCenter() + delta );
227  refreshMouse();
228  }
229  else
230  {
231  // Restrict zoom handling to the vertical axis, otherwise horizontal
232  // scrolling events (e.g. touchpads and some mice) end up interpreted
233  // as vertical scroll events and confuse the user.
234  if( axis == wxMOUSE_WHEEL_VERTICAL )
235  {
236  const int rotation = aEvent.GetWheelRotation();
237  const double zoomScale = m_zoomController->GetScaleForRotation( rotation );
238 
239  if( IsCursorWarpingEnabled() )
240  {
241  CenterOnCursor();
242  m_view->SetScale( m_view->GetScale() * zoomScale );
243  }
244  else
245  {
246  const VECTOR2D anchor = m_view->ToWorld( VECTOR2D( aEvent.GetX(), aEvent.GetY() ) );
247  m_view->SetScale( m_view->GetScale() * zoomScale, anchor );
248  }
249  }
250  }
251 
252  // Do not skip this event, otherwise wxWidgets will fire
253  // 3 wxEVT_SCROLLWIN_LINEUP or wxEVT_SCROLLWIN_LINEDOWN (normal wxWidgets behavior)
254  // and we do not want that.
255  m_parentPanel->Refresh();
256 }
257 
258 
259 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
260 void WX_VIEW_CONTROLS::onMagnify( wxMouseEvent& aEvent )
261 {
262  // Scale based on the magnification from our underlying magnification event.
263  VECTOR2D anchor = m_view->ToWorld( VECTOR2D( aEvent.GetX(), aEvent.GetY() ) );
264  m_view->SetScale( m_view->GetScale() * ( aEvent.GetMagnification() + 1.0f ), anchor );
265 
266  aEvent.Skip();
267 }
268 #endif
269 
270 
271 void WX_VIEW_CONTROLS::onButton( wxMouseEvent& aEvent )
272 {
273  switch( m_state )
274  {
275  case IDLE:
276  case AUTO_PANNING:
277  if( aEvent.MiddleDown() ||
278  ( aEvent.LeftDown() && m_settings.m_panWithLeftButton ) ||
279  ( aEvent.RightDown() && m_settings.m_panWithRightButton ) )
280  {
281  m_dragStartPoint = VECTOR2D( aEvent.GetX(), aEvent.GetY() );
284  }
285 
286  if( aEvent.LeftUp() )
287  m_state = IDLE; // Stop autopanning when user release left mouse button
288 
289  break;
290 
291  case DRAG_PANNING:
292  if( aEvent.MiddleUp() || aEvent.LeftUp() || aEvent.RightUp() )
293  m_state = IDLE;
294 
295  break;
296  }
297 
298  aEvent.Skip();
299 }
300 
301 
302 void WX_VIEW_CONTROLS::onEnter( wxMouseEvent& aEvent )
303 {
304  m_parentPanel->SetFocus();
305 }
306 
307 
308 void WX_VIEW_CONTROLS::onLeave( wxMouseEvent& aEvent )
309 {
311  {
312  bool warp = false;
313  int x = aEvent.GetX();
314  int y = aEvent.GetY();
315  wxSize parentSize = m_parentPanel->GetClientSize();
316 
317  if( x < 0 )
318  {
319  x = 0;
320  warp = true;
321  }
322  else if( x >= parentSize.x )
323  {
324  x = parentSize.x - 1;
325  warp = true;
326  }
327 
328  if( y < 0 )
329  {
330  y = 0;
331  warp = true;
332  }
333  else if( y >= parentSize.y )
334  {
335  y = parentSize.y - 1;
336  warp = true;
337  }
338 
339  if( warp )
340  m_parentPanel->WarpPointer( x, y );
341  }
342 }
343 
344 
345 void WX_VIEW_CONTROLS::onTimer( wxTimerEvent& aEvent )
346 {
347  switch( m_state )
348  {
349  case AUTO_PANNING:
350  {
352  {
353  m_state = IDLE;
354  return;
355  }
356 
357  if( !m_parentPanel->HasFocus() )
358  break;
359 
360  double borderSize = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
362 
363  VECTOR2D dir( m_panDirection );
364 
365  if( dir.EuclideanNorm() > borderSize / 2 )
366  dir = dir.Resize( pow( borderSize, m_settings.m_autoPanAcceleration ) );
367  else if( dir.EuclideanNorm() > borderSize )
368  dir = dir.Resize( borderSize );
369 
370  dir = m_view->ToWorld( dir, false );
371  m_view->SetCenter( m_view->GetCenter() + dir );
372 
373  refreshMouse();
374  }
375  break;
376 
377  case IDLE: // Just remove unnecessary warnings
378  case DRAG_PANNING:
379  break;
380  }
381 }
382 
383 
384 void WX_VIEW_CONTROLS::onScroll( wxScrollWinEvent& aEvent )
385 {
386  const double linePanDelta = 0.05;
387  const double pagePanDelta = 0.5;
388 
389  int type = aEvent.GetEventType();
390  int dir = aEvent.GetOrientation();
391 
392  if( type == wxEVT_SCROLLWIN_THUMBTRACK )
393  {
394  auto center = m_view->GetCenter();
395  const auto& boundary = m_view->GetBoundary();
396 
397  // Flip scroll direction in flipped view
398  const double xstart = ( m_view->IsMirroredX() ?
399  boundary.GetRight() : boundary.GetLeft() );
400  const double xdelta = ( m_view->IsMirroredX() ? -1 : 1 );
401 
402  if( dir == wxHORIZONTAL )
403  center.x = xstart + xdelta * ( aEvent.GetPosition() / m_scrollScale.x );
404  else
405  center.y = boundary.GetTop() + aEvent.GetPosition() / m_scrollScale.y;
406 
407  m_view->SetCenter( center );
408  }
409  else
410  {
411  double dist = 0;
412 
413  if( type == wxEVT_SCROLLWIN_PAGEUP )
414  dist = pagePanDelta;
415  else if( type == wxEVT_SCROLLWIN_PAGEDOWN )
416  dist = -pagePanDelta;
417  else if( type == wxEVT_SCROLLWIN_LINEUP )
418  dist = linePanDelta;
419  else if( type == wxEVT_SCROLLWIN_LINEDOWN )
420  dist = -linePanDelta;
421  else
422  {
423  wxASSERT( "Unhandled event type" );
424  return;
425  }
426 
427  VECTOR2D scroll = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) * dist;
428 
429  double scrollX = 0.0;
430  double scrollY = 0.0;
431 
432  if ( dir == wxHORIZONTAL )
433  scrollX = -scroll.x;
434  else
435  scrollY = -scroll.y;
436 
437  VECTOR2D delta( scrollX, scrollY );
438 
439  m_view->SetCenter( m_view->GetCenter() + delta );
440  }
441 
442  m_parentPanel->Refresh();
443 }
444 
445 
446 void WX_VIEW_CONTROLS::SetGrabMouse( bool aEnabled )
447 {
448  if( aEnabled && !m_settings.m_grabMouse )
449  m_parentPanel->CaptureMouse();
450  else if( !aEnabled && m_settings.m_grabMouse )
451  m_parentPanel->ReleaseMouse();
452 
453  VIEW_CONTROLS::SetGrabMouse( aEnabled );
454 }
455 
456 
457 VECTOR2D WX_VIEW_CONTROLS::GetMousePosition( bool aWorldCoordinates ) const
458 {
460  VECTOR2D screenPos( msp.x, msp.y );
461 
462  return aWorldCoordinates ? m_view->ToWorld( screenPos ) : screenPos;
463 }
464 
465 
467 {
468  if( aEnableSnapping )
469  {
470  return m_view->GetGAL()->GetGridPoint( m_cursorPos );
471  }
472  else
473  {
474  return m_cursorPos;
475  }
476 }
477 
478 
479 VECTOR2D WX_VIEW_CONTROLS::GetCursorPosition( bool aEnableSnapping ) const
480 {
482  {
484  }
485  else
486  {
487  return GetRawCursorPosition( aEnableSnapping );
488  }
489 }
490 
491 
492 void WX_VIEW_CONTROLS::SetCursorPosition( const VECTOR2D& aPosition, bool aWarpView,
493  bool aTriggeredByArrows, long aArrowCommand )
494 {
495  m_updateCursor = false;
496 
497  if( aTriggeredByArrows )
498  {
501  m_settings.m_lastKeyboardCursorCommand = aArrowCommand;
502  m_cursorWarped = false;
503  }
504  else
505  {
509  m_cursorWarped = true;
510  }
511 
512  WarpCursor( aPosition, true, aWarpView );
513  m_cursorPos = aPosition;
514 }
515 
516 
517 void WX_VIEW_CONTROLS::SetCrossHairCursorPosition( const VECTOR2D& aPosition, bool aWarpView = true )
518 {
519  m_updateCursor = false;
520 
521  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
522  BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
523  VECTOR2D screenPos = m_view->ToScreen( aPosition );
524 
525  if( aWarpView && !screen.Contains( screenPos ) )
526  m_view->SetCenter( aPosition );
527 
528  m_cursorPos = aPosition;
529 }
530 
531 
532 void WX_VIEW_CONTROLS::WarpCursor( const VECTOR2D& aPosition, bool aWorldCoordinates,
533  bool aWarpView )
534 {
535  if( aWorldCoordinates )
536  {
537  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
538  BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
539  VECTOR2D screenPos = m_view->ToScreen( aPosition );
540 
541  if( !screen.Contains( screenPos ) )
542  {
543  if( aWarpView )
544  {
545  m_view->SetCenter( aPosition );
546  m_parentPanel->WarpPointer( screenSize.x / 2, screenSize.y / 2 );
547  }
548  }
549  else
550  {
551  m_parentPanel->WarpPointer( screenPos.x, screenPos.y );
552  }
553  }
554  else
555  {
556  m_parentPanel->WarpPointer( aPosition.x, aPosition.y );
557  }
558 
559  refreshMouse();
560 }
561 
562 
564 {
565  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
566  VECTOR2I screenCenter( screenSize / 2 );
567 
568  if( GetMousePosition( false ) != screenCenter )
569  {
571  m_parentPanel->WarpPointer( KiROUND( screenSize.x / 2 ), KiROUND( screenSize.y / 2 ) );
572  }
573 }
574 
575 
576 bool WX_VIEW_CONTROLS::handleAutoPanning( const wxMouseEvent& aEvent )
577 {
578  VECTOR2I p( aEvent.GetX(), aEvent.GetY() );
580 
582  {
583  // last cursor move event came from keyboard cursor control. If auto-panning is enabled and
584  // the next position is inside the autopan zone, check if it really came from a mouse event, otherwise
585  // disable autopan temporarily. Also temporaly disable autopan if the cursor is in the autopan zone
586  // because the application warped the cursor.
587 
588  m_cursorWarped = false;
589  return true;
590  }
591 
592  m_cursorWarped = false;
593 
594  // Compute areas where autopanning is active
595  int borderStart = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
597  borderStart = std::max( borderStart, 2 );
598  int borderEndX = m_view->GetScreenPixelSize().x - borderStart;
599  int borderEndY = m_view->GetScreenPixelSize().y - borderStart;
600 
601  if( p.x < borderStart )
602  m_panDirection.x = -( borderStart - p.x );
603  else if( p.x > borderEndX )
604  m_panDirection.x = ( p.x - borderEndX );
605  else
606  m_panDirection.x = 0;
607 
608  if( p.y < borderStart )
609  m_panDirection.y = -( borderStart - p.y );
610  else if( p.y > borderEndY )
611  m_panDirection.y = ( p.y - borderEndY );
612  else
613  m_panDirection.y = 0;
614 
615  bool borderHit = ( m_panDirection.x != 0 || m_panDirection.y != 0 );
616 
617  switch( m_state )
618  {
619  case AUTO_PANNING:
620  if( !borderHit )
621  {
622  m_panTimer.Stop();
623  m_state = IDLE;
624 
625  return false;
626  }
627 
628  return true;
629  break;
630 
631  case IDLE:
632  if( borderHit )
633  {
635  m_panTimer.Start( (int) ( 250.0 / 60.0 ) );
636 
637  return true;
638  }
639 
640  return false;
641  break;
642 
643  case DRAG_PANNING:
644  return false;
645  }
646 
647  wxASSERT_MSG( false, wxT( "This line should never be reached" ) );
648  return false; // Should not be reached, just avoid the compiler warnings..
649 }
650 
651 
653 {
654  // Notify tools that the cursor position has changed in the world coordinates
655  wxMouseEvent moveEvent( EVT_REFRESH_MOUSE );
657  moveEvent.SetX( msp.x );
658  moveEvent.SetY( msp.y );
659 
660  // Set the modifiers state
661  moveEvent.SetControlDown( wxGetKeyState( WXK_CONTROL ) );
662  moveEvent.SetShiftDown( wxGetKeyState( WXK_SHIFT ) );
663  moveEvent.SetAltDown( wxGetKeyState( WXK_ALT ) );
664 
665  m_cursorPos = m_view->ToWorld( VECTOR2D( msp.x, msp.y ) );
666  wxPostEvent( m_parentPanel, moveEvent );
667 }
668 
669 
671 {
672  wxPoint msp = wxGetMousePosition();
673  m_parentPanel->ScreenToClient( &msp.x, &msp.y );
674  return msp;
675 }
676 
677 
679 {
680  const BOX2D viewport = m_view->GetViewport();
681  const BOX2D& boundary = m_view->GetBoundary();
682 
683  m_scrollScale.x = 2e3 / viewport.GetWidth(); // TODO it does not have to be updated so often
684  m_scrollScale.y = 2e3 / viewport.GetHeight();
685  VECTOR2I newScroll( ( viewport.Centre().x - boundary.GetLeft() ) * m_scrollScale.x,
686  ( viewport.Centre().y - boundary.GetTop() ) * m_scrollScale.y );
687 
688  // We add the width of the scroll bar thumb to the range because the scroll range is given by
689  // the full bar while the position is given by the left/top position of the thumb
690  VECTOR2I newRange( m_scrollScale.x * boundary.GetWidth() + m_parentPanel->GetScrollThumb( wxSB_HORIZONTAL ),
691  m_scrollScale.y * boundary.GetHeight() + m_parentPanel->GetScrollThumb( wxSB_VERTICAL ) );
692 
693  // Flip scroll direction in flipped view
694  if( m_view->IsMirroredX() )
695  newScroll.x = ( boundary.GetRight() - viewport.Centre().x ) * m_scrollScale.x;
696 
697  // Adjust scrollbars only if it is needed. Otherwise there are cases when canvas is continuously
698  // refreshed (Windows)
699  if( m_scrollPos != newScroll || newRange.x != m_parentPanel->GetScrollRange( wxSB_HORIZONTAL )
700  || newRange.y != m_parentPanel->GetScrollRange( wxSB_VERTICAL ) )
701  {
702  m_parentPanel->SetScrollbars( 1, 1, newRange.x, newRange.y, newScroll.x, newScroll.y, true );
703  m_scrollPos = newScroll;
704 
705 #ifndef __APPLE__
706  // Trigger a mouse refresh to get the canvas update in GTK (re-draws the scrollbars).
707  // Note that this causes an infinite loop on OSX as it generates a paint event.
708  refreshMouse();
709 #endif
710  }
711 }
712 
713 void WX_VIEW_CONTROLS::ForceCursorPosition( bool aEnabled, const VECTOR2D& aPosition )
714 {
716  m_settings.m_forcedPosition = aPosition;
717 }
bool m_enableMousewheelPan
Mousewheel (2-finger touchpad) panning
Definition: view_controls.h:91
VECTOR2D m_lookStartPoint
Stores information about the center of viewport when dragging has started.
BOX2D GetViewport() const
Function GetViewport() Returns the current viewport visible area rectangle.
Definition: view.cpp:538
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:103
static const int dist[10][10]
Definition: ar_matrix.cpp:326
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:131
const BOX2D & GetBoundary() const
Function GetBoundary()
Definition: view.h:287
static std::unique_ptr< ZOOM_CONTROLLER > GetZoomControllerForPlatform()
static const wxEventType EVT_REFRESH_MOUSE
Event that forces mouse move event in the dispatcher (eg.
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Function ToWorld() Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:475
coord_type GetTop() const
Definition: box2.h:203
const VECTOR2D & GetCenter() const
Function GetCenter() Returns the center point of this VIEW (in world space coordinates)
Definition: view.h:328
wxPoint getMouseScreenPosition() const
Gets the cursor position in the screen coordinates.
bool m_grabMouse
Flag for grabbing the mouse cursor
Definition: view_controls.h:70
VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const override
Function GetMousePosition() Returns the current mouse pointer position.
std::unique_ptr< ZOOM_CONTROLLER > m_zoomController
a ZOOM_CONTROLLER that determines zoom steps. This is platform-specific.
void onButton(wxMouseEvent &aEvent)
VC_SETTINGS m_settings
Current VIEW_CONTROLS settings
VECTOR2D m_forcedPosition
Forced cursor position (world coordinates)
Definition: view_controls.h:58
coord_type GetRight() const
Definition: box2.h:198
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
bool m_panWithRightButton
Allow panning with the right button in addition to middle
Definition: view_controls.h:94
VIEW * m_view
Pointer to controlled VIEW.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
VECTOR2D m_scrollScale
Ratio used for scaling world coordinates to scrollbar position.
WX_VIEW_CONTROLS class definition.
static constexpr double GTK3_SCALE
A suitable (magic) scale factor for GTK3 systems.
void UpdateScrollbars()
Adjusts the scrollbars position to match the current viewport.
VECTOR2D GetGridPoint(const VECTOR2D &aPoint) const
Function GetGridPoint() For a given point it returns the nearest point belonging to the grid in world...
const VECTOR2I & GetScreenPixelSize() const
Returns GAL canvas size in pixels.
VECTOR2D m_lastKeyboardCursorPosition
Position of the above event
VECTOR2D GetRawCursorPosition(bool aSnappingEnabled=true) const override
Returns the current cursor position in world coordinates - ingoring the cursorUp position force mode.
bool m_autoPanSettingEnabled
Flag for turning on autopanning
Definition: view_controls.h:76
void onWheel(wxMouseEvent &aEvent)
Handler functions.
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:604
Panning with mouse button pressed.
bool m_forceCursorPosition
Is the forced cursor position enabled
Definition: view_controls.h:61
#define NULL
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
coord_type GetWidth() const
Definition: box2.h:196
bool Contains(const Vec &aPoint) const
Function Contains.
Definition: box2.h:150
void SetCrossHairCursorPosition(const VECTOR2D &aPosition, bool aWarpView) override
Moves the graphic crosshair cursor to the requested position expressed in world coordinates.
float m_autoPanAcceleration
How fast does panning accelerate when approaching the window boundary
Definition: view_controls.h:85
const VECTOR2I & GetScreenPixelSize() const
Function GetScreenPixelSize() Returns the size of the our rendering area, in pixels.
Definition: view.cpp:1177
bool IsCursorWarpingEnabled() const
Function IsCursorWarpingEnabled()
VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (such a...
bool m_warpCursor
If the cursor is allowed to be warped
Definition: view_controls.h:88
static constexpr double MAC_SCALE
A suitable (magic) scale factor for Mac systems.
float m_autoPanMargin
Distance from cursor to VIEW edge when panning is active
Definition: view_controls.h:79
void SetGrabMouse(bool aEnabled) override
Function SetGrabMouse() Enables/disables mouse cursor grabbing (limits the movement field only to the...
bool m_cursorWarped
Application warped the cursor, not the user (keyboard)
void refreshMouse()
Sends an event to refresh mouse position.
VECTOR2D ToScreen(const VECTOR2D &aCoord, bool aAbsolute=true) const
Function ToScreen() Converts a world space point/vector to a point/vector in screen space coordinates...
Definition: view.cpp:494
bool m_autoPanEnabled
Flag for turning on autopanning
Definition: view_controls.h:73
void onMotion(wxMouseEvent &aEvent)
void WarpCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false) override
bool IsMirroredX() const
Function IsMirroredX() Returns true if view is flipped across the X axis.
Definition: view.h:230
void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0)) override
Function ForceCursorPosition() Places the cursor immediately at a given point.
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Function SetScale() Sets the scaling factor, zooming around a given anchor point.
Definition: view.cpp:578
wxScrolledCanvas * m_parentPanel
Panel that is affected by VIEW_CONTROLS.
void onTimer(wxTimerEvent &WXUNUSED(aEvent))
see class PGM_BASE
Vec Centre() const
Definition: box2.h:78
VECTOR2D m_panDirection
Current direction of panning (only autopanning mode)
long m_lastKeyboardCursorCommand
ACTIONS::CURSOR_UP, ACTIONS::CURSOR_DOWN, etc.
bool m_updateCursor
Flag deciding whether the cursor position should be calculated using the mouse position.
bool m_lastKeyboardCursorPositionValid
Is last cursor motion event coming from keyboard arrow cursor motion action
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61
virtual void SetGrabMouse(bool aEnabled)
Function SetGrabMouse Turns on/off mouse grabbing.
VECTOR2I m_scrollPos
Current scrollbar position.
coord_type GetHeight() const
Definition: box2.h:197
WX_VIEW_CONTROLS(VIEW *aView, wxScrolledCanvas *aParentPanel)
bool handleAutoPanning(const wxMouseEvent &aEvent)
Function handleAutoPanning() Computes new viewport settings while in autopanning mode.
void onEnter(wxMouseEvent &WXUNUSED(aEvent))
VECTOR2D m_dragStartPoint
Stores information about point where dragging has started.
void SetCursorPosition(const VECTOR2D &aPosition, bool warpView, bool aTriggeredByArrows, long aArrowCommand) override
Moves cursor to the requested position expressed in world coordinates.
STATE m_state
Current state of VIEW_CONTROLS.
coord_type GetLeft() const
Definition: box2.h:202
VIEW.
Definition: view.h:61
double GetScale() const
Function GetScale()
Definition: view.h:257
void CenterOnCursor() const override
Function CenterOnCursor() Sets the viewport center to the current cursor position and warps the curso...
bool m_cursorCaptured
Should the cursor be locked within the parent window area
Definition: view_controls.h:64
void onScroll(wxScrollWinEvent &aEvent)
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
void onLeave(wxMouseEvent &WXUNUSED(aEvent))
VECTOR2D m_cursorPos
Current cursor position (world coordinates)
wxTimer m_panTimer
Timer repsonsible for handling autopanning.
bool m_panWithLeftButton
Allow panning with the left button in addition to middle
Definition: view_controls.h:97