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