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>
35 #include <math/util.h> // for KiROUND
36 
37 
38 using namespace KIGFX;
39 
40 const wxEventType WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE = wxNewEventType();
41 
42 
43 static std::unique_ptr<ZOOM_CONTROLLER> GetZoomControllerForPlatform()
44 {
45 #ifdef __WXMAC__
46  // On Apple pointer devices, wheel events occur frequently and with
47  // smaller rotation values. For those devices, let's handle zoom
48  // based on the rotation amount rather than the time difference.
49  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::MAC_SCALE );
50 #elif __WXGTK3__
51  // GTK3 is similar, but the scale constant is smaller
52  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::GTK3_SCALE );
53 #else
54  return std::make_unique<ACCELERATING_ZOOM_CONTROLLER>();
55 #endif
56 }
57 
58 
59 WX_VIEW_CONTROLS::WX_VIEW_CONTROLS( VIEW* aView, wxScrolledCanvas* aParentPanel ) :
60  VIEW_CONTROLS( aView ),
61  m_state( IDLE ),
62  m_parentPanel( aParentPanel ),
63  m_scrollScale( 1.0, 1.0 ),
64 #ifdef __WXGTK3__
65  m_lastTimestamp( 0 ),
66 #endif
67  m_cursorPos( 0, 0 ),
68  m_updateCursor( true )
69 {
70  bool enableMousewheelPan = false;
71  bool enableZoomNoCenter = false;
72  bool enableAutoPan = true;
73 
74  Pgm().CommonSettings()->Read( ENBL_MOUSEWHEEL_PAN_KEY, &enableMousewheelPan, false );
75  Pgm().CommonSettings()->Read( ENBL_ZOOM_NO_CENTER_KEY, &enableZoomNoCenter, false );
76  Pgm().CommonSettings()->Read( ENBL_AUTO_PAN_KEY, &enableAutoPan, true );
77 
78  m_settings.m_enableMousewheelPan = enableMousewheelPan;
79  m_settings.m_warpCursor = !enableZoomNoCenter;
80  m_settings.m_autoPanSettingEnabled = enableAutoPan;
81 
82  m_parentPanel->Connect( wxEVT_MOTION,
83  wxMouseEventHandler( WX_VIEW_CONTROLS::onMotion ), NULL, this );
84 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
85  m_parentPanel->Connect( wxEVT_MAGNIFY,
86  wxMouseEventHandler( WX_VIEW_CONTROLS::onMagnify ), NULL, this );
87 #endif
88  m_parentPanel->Connect( wxEVT_MOUSEWHEEL,
89  wxMouseEventHandler( WX_VIEW_CONTROLS::onWheel ), NULL, this );
90  m_parentPanel->Connect( wxEVT_MIDDLE_UP,
91  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
92  m_parentPanel->Connect( wxEVT_MIDDLE_DOWN,
93  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
94  m_parentPanel->Connect( wxEVT_LEFT_UP,
95  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
96  m_parentPanel->Connect( wxEVT_LEFT_DOWN,
97  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
98  m_parentPanel->Connect( wxEVT_RIGHT_UP,
99  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
100  m_parentPanel->Connect( wxEVT_RIGHT_DOWN,
101  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
102 #if defined _WIN32 || defined _WIN64
103  m_parentPanel->Connect( wxEVT_ENTER_WINDOW,
104  wxMouseEventHandler( WX_VIEW_CONTROLS::onEnter ), NULL, this );
105 #endif
106  m_parentPanel->Connect( wxEVT_LEAVE_WINDOW,
107  wxMouseEventHandler( WX_VIEW_CONTROLS::onLeave ), NULL, this );
108  m_parentPanel->Connect( wxEVT_SCROLLWIN_THUMBTRACK,
109  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
110  m_parentPanel->Connect( wxEVT_SCROLLWIN_PAGEUP,
111  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
112  m_parentPanel->Connect( wxEVT_SCROLLWIN_PAGEDOWN,
113  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
114 
115  m_parentPanel->Connect( wxEVT_SCROLLWIN_BOTTOM,
116  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
117  m_parentPanel->Connect( wxEVT_SCROLLWIN_TOP,
118  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
119  m_parentPanel->Connect( wxEVT_SCROLLWIN_LINEUP,
120  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
121  m_parentPanel->Connect( wxEVT_SCROLLWIN_LINEDOWN,
122  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
123 
125 
126  m_cursorWarped = false;
127 
128  m_panTimer.SetOwner( this );
129  this->Connect( wxEVT_TIMER, wxTimerEventHandler( WX_VIEW_CONTROLS::onTimer ), NULL, this );
130 
134 }
135 
136 
138 {
139  wxConfigBase* cfg = Pgm().CommonSettings();
140 
141  if( cfg )
142  {
146  }
147 }
148 
149 
150 void WX_VIEW_CONTROLS::onMotion( wxMouseEvent& aEvent )
151 {
152  bool isAutoPanning = false;
153  VECTOR2D mousePos( aEvent.GetX(), aEvent.GetY() );
154 
156  isAutoPanning = handleAutoPanning( aEvent );
157 
158  if( !isAutoPanning && aEvent.Dragging() )
159  {
160  if( m_state == DRAG_PANNING )
161  {
162  VECTOR2D d = m_dragStartPoint - mousePos;
163  VECTOR2D delta = m_view->ToWorld( d, false );
164 
165  m_view->SetCenter( m_lookStartPoint + delta );
166  aEvent.StopPropagation();
167  }
168  }
169 
170  if( m_updateCursor ) // do not update the cursor position if it was explicitly set
171  m_cursorPos = m_view->ToWorld( mousePos );
172  else
173  m_updateCursor = true;
174 
175  aEvent.Skip();
176 }
177 
178 
179 void WX_VIEW_CONTROLS::onWheel( wxMouseEvent& aEvent )
180 {
181 #ifdef __WXGTK3__
182  if( aEvent.GetTimestamp() == m_lastTimestamp )
183  {
184  aEvent.Skip( false );
185  return;
186  }
187 
188  m_lastTimestamp = aEvent.GetTimestamp();
189 #endif
190 
191  const double wheelPanSpeed = 0.001;
192  const int axis = aEvent.GetWheelAxis();
193 
194  // mousewheelpan disabled:
195  // wheel + ctrl -> horizontal scrolling;
196  // wheel + shift -> vertical scrolling;
197  // wheel -> zooming;
198  // mousewheelpan enabled:
199  // wheel -> pan;
200  // wheel + ctrl -> zooming;
201  // wheel + shift -> horizontal scrolling.
202 
203  if( ( !m_settings.m_enableMousewheelPan && ( aEvent.ControlDown() || aEvent.ShiftDown() ) ) ||
204  ( m_settings.m_enableMousewheelPan && !aEvent.ControlDown() ) )
205  {
206  // Scrolling
207  VECTOR2D scrollVec = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) *
208  ( (double) aEvent.GetWheelRotation() * wheelPanSpeed );
209  double scrollX = 0.0;
210  double scrollY = 0.0;
211 
213  {
214  if ( axis == wxMOUSE_WHEEL_HORIZONTAL || aEvent.ShiftDown() )
215  scrollX = scrollVec.x;
216  else
217  scrollY = -scrollVec.y;
218  }
219  else
220  {
221  if( aEvent.ControlDown() )
222  scrollX = -scrollVec.x;
223  else
224  scrollY = -scrollVec.y;
225  }
226 
227  VECTOR2D delta( scrollX, scrollY );
228 
229  m_view->SetCenter( m_view->GetCenter() + delta );
230  refreshMouse();
231  }
232  else
233  {
234  // Restrict zoom handling to the vertical axis, otherwise horizontal
235  // scrolling events (e.g. touchpads and some mice) end up interpreted
236  // as vertical scroll events and confuse the user.
237  if( axis == wxMOUSE_WHEEL_VERTICAL )
238  {
239  const int rotation = aEvent.GetWheelRotation();
240  const double zoomScale = m_zoomController->GetScaleForRotation( rotation );
241 
242  if( IsCursorWarpingEnabled() )
243  {
244  CenterOnCursor();
245  m_view->SetScale( m_view->GetScale() * zoomScale );
246  }
247  else
248  {
249  const VECTOR2D anchor = m_view->ToWorld( VECTOR2D( aEvent.GetX(), aEvent.GetY() ) );
250  m_view->SetScale( m_view->GetScale() * zoomScale, anchor );
251  }
252  }
253  }
254 
255  // Do not skip this event, otherwise wxWidgets will fire
256  // 3 wxEVT_SCROLLWIN_LINEUP or wxEVT_SCROLLWIN_LINEDOWN (normal wxWidgets behavior)
257  // and we do not want that.
258  m_parentPanel->Refresh();
259 }
260 
261 
262 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
263 void WX_VIEW_CONTROLS::onMagnify( wxMouseEvent& aEvent )
264 {
265  // Scale based on the magnification from our underlying magnification event.
266  VECTOR2D anchor = m_view->ToWorld( VECTOR2D( aEvent.GetX(), aEvent.GetY() ) );
267  m_view->SetScale( m_view->GetScale() * ( aEvent.GetMagnification() + 1.0f ), anchor );
268 
269  aEvent.Skip();
270 }
271 #endif
272 
273 
274 void WX_VIEW_CONTROLS::onButton( wxMouseEvent& aEvent )
275 {
276  switch( m_state )
277  {
278  case IDLE:
279  case AUTO_PANNING:
280  if( aEvent.MiddleDown() ||
281  ( aEvent.LeftDown() && m_settings.m_panWithLeftButton ) ||
282  ( aEvent.RightDown() && m_settings.m_panWithRightButton ) )
283  {
284  m_dragStartPoint = VECTOR2D( aEvent.GetX(), aEvent.GetY() );
287  }
288 
289  if( aEvent.LeftUp() )
290  m_state = IDLE; // Stop autopanning when user release left mouse button
291 
292  break;
293 
294  case DRAG_PANNING:
295  if( aEvent.MiddleUp() || aEvent.LeftUp() || aEvent.RightUp() )
296  m_state = IDLE;
297 
298  break;
299  }
300 
301  aEvent.Skip();
302 }
303 
304 
305 void WX_VIEW_CONTROLS::onEnter( wxMouseEvent& aEvent )
306 {
307  m_parentPanel->SetFocus();
308 }
309 
310 
311 void WX_VIEW_CONTROLS::onLeave( wxMouseEvent& aEvent )
312 {
314  {
315  bool warp = false;
316  int x = aEvent.GetX();
317  int y = aEvent.GetY();
318  wxSize parentSize = m_parentPanel->GetClientSize();
319 
320  if( x < 0 )
321  {
322  x = 0;
323  warp = true;
324  }
325  else if( x >= parentSize.x )
326  {
327  x = parentSize.x - 1;
328  warp = true;
329  }
330 
331  if( y < 0 )
332  {
333  y = 0;
334  warp = true;
335  }
336  else if( y >= parentSize.y )
337  {
338  y = parentSize.y - 1;
339  warp = true;
340  }
341 
342  if( warp )
343  m_parentPanel->WarpPointer( x, y );
344  }
345 }
346 
347 
348 void WX_VIEW_CONTROLS::onTimer( wxTimerEvent& aEvent )
349 {
350  switch( m_state )
351  {
352  case AUTO_PANNING:
353  {
355  {
356  m_state = IDLE;
357  return;
358  }
359 
360  if( !m_parentPanel->HasFocus() )
361  break;
362 
363  double borderSize = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
365 
366  VECTOR2D dir( m_panDirection );
367 
368  if( dir.EuclideanNorm() > borderSize / 2 )
369  dir = dir.Resize( pow( borderSize, m_settings.m_autoPanAcceleration ) );
370  else if( dir.EuclideanNorm() > borderSize )
371  dir = dir.Resize( borderSize );
372 
373  dir = m_view->ToWorld( dir, false );
374  m_view->SetCenter( m_view->GetCenter() + dir );
375 
376  refreshMouse();
377  }
378  break;
379 
380  case IDLE: // Just remove unnecessary warnings
381  case DRAG_PANNING:
382  break;
383  }
384 }
385 
386 
387 void WX_VIEW_CONTROLS::onScroll( wxScrollWinEvent& aEvent )
388 {
389  const double linePanDelta = 0.05;
390  const double pagePanDelta = 0.5;
391 
392  int type = aEvent.GetEventType();
393  int dir = aEvent.GetOrientation();
394 
395  if( type == wxEVT_SCROLLWIN_THUMBTRACK )
396  {
397  auto center = m_view->GetCenter();
398  const auto& boundary = m_view->GetBoundary();
399 
400  // Flip scroll direction in flipped view
401  const double xstart = ( m_view->IsMirroredX() ?
402  boundary.GetRight() : boundary.GetLeft() );
403  const double xdelta = ( m_view->IsMirroredX() ? -1 : 1 );
404 
405  if( dir == wxHORIZONTAL )
406  center.x = xstart + xdelta * ( aEvent.GetPosition() / m_scrollScale.x );
407  else
408  center.y = boundary.GetTop() + aEvent.GetPosition() / m_scrollScale.y;
409 
410  m_view->SetCenter( center );
411  }
412  else
413  {
414  double dist = 0;
415 
416  if( type == wxEVT_SCROLLWIN_PAGEUP )
417  dist = pagePanDelta;
418  else if( type == wxEVT_SCROLLWIN_PAGEDOWN )
419  dist = -pagePanDelta;
420  else if( type == wxEVT_SCROLLWIN_LINEUP )
421  dist = linePanDelta;
422  else if( type == wxEVT_SCROLLWIN_LINEDOWN )
423  dist = -linePanDelta;
424  else
425  {
426  wxASSERT( "Unhandled event type" );
427  return;
428  }
429 
430  VECTOR2D scroll = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) * dist;
431 
432  double scrollX = 0.0;
433  double scrollY = 0.0;
434 
435  if ( dir == wxHORIZONTAL )
436  scrollX = -scroll.x;
437  else
438  scrollY = -scroll.y;
439 
440  VECTOR2D delta( scrollX, scrollY );
441 
442  m_view->SetCenter( m_view->GetCenter() + delta );
443  }
444 
445  m_parentPanel->Refresh();
446 }
447 
448 
449 void WX_VIEW_CONTROLS::SetGrabMouse( bool aEnabled )
450 {
451  if( aEnabled && !m_settings.m_grabMouse )
452  m_parentPanel->CaptureMouse();
453  else if( !aEnabled && m_settings.m_grabMouse )
454  m_parentPanel->ReleaseMouse();
455 
456  VIEW_CONTROLS::SetGrabMouse( aEnabled );
457 }
458 
459 
460 VECTOR2D WX_VIEW_CONTROLS::GetMousePosition( bool aWorldCoordinates ) const
461 {
463  VECTOR2D screenPos( msp.x, msp.y );
464 
465  return aWorldCoordinates ? m_view->ToWorld( screenPos ) : screenPos;
466 }
467 
468 
470 {
471  if( aEnableSnapping )
472  {
473  return m_view->GetGAL()->GetGridPoint( m_cursorPos );
474  }
475  else
476  {
477  return m_cursorPos;
478  }
479 }
480 
481 
482 VECTOR2D WX_VIEW_CONTROLS::GetCursorPosition( bool aEnableSnapping ) const
483 {
485  {
487  }
488  else
489  {
490  return GetRawCursorPosition( aEnableSnapping );
491  }
492 }
493 
494 
495 void WX_VIEW_CONTROLS::SetCursorPosition( const VECTOR2D& aPosition, bool aWarpView,
496  bool aTriggeredByArrows, long aArrowCommand )
497 {
498  m_updateCursor = false;
499 
500  if( aTriggeredByArrows )
501  {
504  m_settings.m_lastKeyboardCursorCommand = aArrowCommand;
505  m_cursorWarped = false;
506  }
507  else
508  {
512  m_cursorWarped = true;
513  }
514 
515  WarpCursor( aPosition, true, aWarpView );
516  m_cursorPos = aPosition;
517 }
518 
519 
520 void WX_VIEW_CONTROLS::SetCrossHairCursorPosition( const VECTOR2D& aPosition, bool aWarpView = true )
521 {
522  m_updateCursor = false;
523 
524  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
525  BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
526  VECTOR2D screenPos = m_view->ToScreen( aPosition );
527 
528  if( aWarpView && !screen.Contains( screenPos ) )
529  m_view->SetCenter( aPosition );
530 
531  m_cursorPos = aPosition;
532 }
533 
534 
535 void WX_VIEW_CONTROLS::WarpCursor( const VECTOR2D& aPosition, bool aWorldCoordinates,
536  bool aWarpView )
537 {
538  if( aWorldCoordinates )
539  {
540  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
541  BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
542  VECTOR2D screenPos = m_view->ToScreen( aPosition );
543 
544  if( !screen.Contains( screenPos ) )
545  {
546  if( aWarpView )
547  {
548  m_view->SetCenter( aPosition );
549  m_parentPanel->WarpPointer( screenSize.x / 2, screenSize.y / 2 );
550  }
551  }
552  else
553  {
554  m_parentPanel->WarpPointer( screenPos.x, screenPos.y );
555  }
556  }
557  else
558  {
559  m_parentPanel->WarpPointer( aPosition.x, aPosition.y );
560  }
561 
562  refreshMouse();
563 }
564 
565 
567 {
568  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
569  VECTOR2I screenCenter( screenSize / 2 );
570 
571  if( GetMousePosition( false ) != screenCenter )
572  {
574  m_parentPanel->WarpPointer( KiROUND( screenSize.x / 2 ), KiROUND( screenSize.y / 2 ) );
575  }
576 }
577 
578 
579 bool WX_VIEW_CONTROLS::handleAutoPanning( const wxMouseEvent& aEvent )
580 {
581  VECTOR2I p( aEvent.GetX(), aEvent.GetY() );
583 
585  {
586  // last cursor move event came from keyboard cursor control. If auto-panning is enabled and
587  // the next position is inside the autopan zone, check if it really came from a mouse event, otherwise
588  // disable autopan temporarily. Also temporaly disable autopan if the cursor is in the autopan zone
589  // because the application warped the cursor.
590 
591  m_cursorWarped = false;
592  return true;
593  }
594 
595  m_cursorWarped = false;
596 
597  // Compute areas where autopanning is active
598  int borderStart = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
600  borderStart = std::max( borderStart, 2 );
601  int borderEndX = m_view->GetScreenPixelSize().x - borderStart;
602  int borderEndY = m_view->GetScreenPixelSize().y - borderStart;
603 
604  if( p.x < borderStart )
605  m_panDirection.x = -( borderStart - p.x );
606  else if( p.x > borderEndX )
607  m_panDirection.x = ( p.x - borderEndX );
608  else
609  m_panDirection.x = 0;
610 
611  if( p.y < borderStart )
612  m_panDirection.y = -( borderStart - p.y );
613  else if( p.y > borderEndY )
614  m_panDirection.y = ( p.y - borderEndY );
615  else
616  m_panDirection.y = 0;
617 
618  bool borderHit = ( m_panDirection.x != 0 || m_panDirection.y != 0 );
619 
620  switch( m_state )
621  {
622  case AUTO_PANNING:
623  if( !borderHit )
624  {
625  m_panTimer.Stop();
626  m_state = IDLE;
627 
628  return false;
629  }
630 
631  return true;
632  break;
633 
634  case IDLE:
635  if( borderHit )
636  {
638  m_panTimer.Start( (int) ( 250.0 / 60.0 ) );
639 
640  return true;
641  }
642 
643  return false;
644  break;
645 
646  case DRAG_PANNING:
647  return false;
648  }
649 
650  wxASSERT_MSG( false, wxT( "This line should never be reached" ) );
651  return false; // Should not be reached, just avoid the compiler warnings..
652 }
653 
654 
656 {
657  // Notify tools that the cursor position has changed in the world coordinates
658  wxMouseEvent moveEvent( EVT_REFRESH_MOUSE );
660  moveEvent.SetX( msp.x );
661  moveEvent.SetY( msp.y );
662 
663  // Set the modifiers state
664  moveEvent.SetControlDown( wxGetKeyState( WXK_CONTROL ) );
665  moveEvent.SetShiftDown( wxGetKeyState( WXK_SHIFT ) );
666  moveEvent.SetAltDown( wxGetKeyState( WXK_ALT ) );
667 
668  m_cursorPos = m_view->ToWorld( VECTOR2D( msp.x, msp.y ) );
669  wxPostEvent( m_parentPanel, moveEvent );
670 }
671 
672 
674 {
675  wxPoint msp = wxGetMousePosition();
676  m_parentPanel->ScreenToClient( &msp.x, &msp.y );
677  return msp;
678 }
679 
680 
682 {
683  const BOX2D viewport = m_view->GetViewport();
684  const BOX2D& boundary = m_view->GetBoundary();
685 
686  m_scrollScale.x = 2e3 / viewport.GetWidth(); // TODO it does not have to be updated so often
687  m_scrollScale.y = 2e3 / viewport.GetHeight();
688  VECTOR2I newScroll( ( viewport.Centre().x - boundary.GetLeft() ) * m_scrollScale.x,
689  ( viewport.Centre().y - boundary.GetTop() ) * m_scrollScale.y );
690 
691  // We add the width of the scroll bar thumb to the range because the scroll range is given by
692  // the full bar while the position is given by the left/top position of the thumb
693  VECTOR2I newRange( m_scrollScale.x * boundary.GetWidth() + m_parentPanel->GetScrollThumb( wxSB_HORIZONTAL ),
694  m_scrollScale.y * boundary.GetHeight() + m_parentPanel->GetScrollThumb( wxSB_VERTICAL ) );
695 
696  // Flip scroll direction in flipped view
697  if( m_view->IsMirroredX() )
698  newScroll.x = ( boundary.GetRight() - viewport.Centre().x ) * m_scrollScale.x;
699 
700  // Adjust scrollbars only if it is needed. Otherwise there are cases when canvas is continuously
701  // refreshed (Windows)
702  if( m_scrollPos != newScroll || newRange.x != m_parentPanel->GetScrollRange( wxSB_HORIZONTAL )
703  || newRange.y != m_parentPanel->GetScrollRange( wxSB_VERTICAL ) )
704  {
705  m_parentPanel->SetScrollbars( 1, 1, newRange.x, newRange.y, newScroll.x, newScroll.y, true );
706  m_scrollPos = newScroll;
707 
708 #ifndef __APPLE__
709  // Trigger a mouse refresh to get the canvas update in GTK (re-draws the scrollbars).
710  // Note that this causes an infinite loop on OSX as it generates a paint event.
711  refreshMouse();
712 #endif
713  }
714 }
715 
716 void WX_VIEW_CONTROLS::ForceCursorPosition( bool aEnabled, const VECTOR2D& aPosition )
717 {
719  m_settings.m_forcedPosition = aPosition;
720 }
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:98
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.
#define ENBL_ZOOM_NO_CENTER_KEY
Definition: pgm_base.h:49
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
#define ENBL_AUTO_PAN_KEY
Definition: pgm_base.h:52
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
#define ENBL_MOUSEWHEEL_PAN_KEY
Definition: pgm_base.h:50
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