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-2021 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * @author Tomasz Wlostowski <[email protected]>
9  * @author Maciej Suminski <[email protected]>
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 <profile.h>
31 #include <view/view.h>
32 #include <view/wx_view_controls.h>
33 #include <view/zoom_controller.h>
35 #include <tool/tool_dispatcher.h>
36 #include <trace_helpers.h>
38 #include <math/util.h> // for KiROUND
40 #include <widgets/ui_common.h>
41 #include <class_draw_panel_gal.h>
42 #include <kiplatform/ui.h>
43 #include <wx/log.h>
44 
45 
46 #if defined __WXMSW__
47  #define USE_MOUSE_CAPTURE
48 #endif
49 
50 using namespace KIGFX;
51 
52 const wxEventType WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE = wxNewEventType();
53 
54 
55 static std::unique_ptr<ZOOM_CONTROLLER> GetZoomControllerForPlatform( bool aAcceleration )
56 {
57 #ifdef __WXMAC__
58  // On Apple pointer devices, wheel events occur frequently and with
59  // smaller rotation values. For those devices, let's handle zoom
60  // based on the rotation amount rather than the time difference.
61  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::MAC_SCALE );
62 #elif __WXGTK3__
63  // GTK3 is similar, but the scale constant is smaller
64  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::GTK3_SCALE );
65 #else
66  if( aAcceleration )
67  return std::make_unique<ACCELERATING_ZOOM_CONTROLLER>();
68  else
69  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::MSW_SCALE );
70 #endif
71 }
72 
73 
75  VIEW_CONTROLS( aView ),
76  m_state( IDLE ),
77  m_parentPanel( aParentPanel ),
78  m_scrollScale( 1.0, 1.0 ),
79  m_initialZoomScale( 0.0 ),
80 #ifdef __WXGTK3__
81  m_lastTimestamp( 0 ),
82 #endif
83  m_cursorPos( 0, 0 ),
84  m_updateCursor( true )
85 {
86  LoadSettings();
87 
88  m_MotionEventCounter = std::make_unique<PROF_COUNTER>( "Mouse motion events" );
89 
90  m_parentPanel->Connect( wxEVT_MOTION,
91  wxMouseEventHandler( WX_VIEW_CONTROLS::onMotion ), nullptr, this );
92 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
93  m_parentPanel->Connect( wxEVT_MAGNIFY,
94  wxMouseEventHandler( WX_VIEW_CONTROLS::onMagnify ), nullptr, this );
95 #endif
96  m_parentPanel->Connect( wxEVT_MOUSEWHEEL,
97  wxMouseEventHandler( WX_VIEW_CONTROLS::onWheel ), nullptr, this );
98  m_parentPanel->Connect( wxEVT_MIDDLE_UP,
99  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), nullptr, this );
100  m_parentPanel->Connect( wxEVT_MIDDLE_DOWN,
101  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), nullptr, this );
102  m_parentPanel->Connect( wxEVT_LEFT_UP,
103  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), nullptr, this );
104  m_parentPanel->Connect( wxEVT_LEFT_DOWN,
105  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), nullptr, this );
106  m_parentPanel->Connect( wxEVT_RIGHT_UP,
107  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), nullptr, this );
108  m_parentPanel->Connect( wxEVT_RIGHT_DOWN,
109  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), nullptr, this );
110 #if defined __WXMSW__
111  m_parentPanel->Connect( wxEVT_ENTER_WINDOW,
112  wxMouseEventHandler( WX_VIEW_CONTROLS::onEnter ), nullptr, this );
113 #endif
114  m_parentPanel->Connect( wxEVT_LEAVE_WINDOW,
115  wxMouseEventHandler( WX_VIEW_CONTROLS::onLeave ), nullptr, this );
116  m_parentPanel->Connect( wxEVT_SCROLLWIN_THUMBTRACK,
117  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), nullptr, this );
118  m_parentPanel->Connect( wxEVT_SCROLLWIN_PAGEUP,
119  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), nullptr, this );
120  m_parentPanel->Connect( wxEVT_SCROLLWIN_PAGEDOWN,
121  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), nullptr, this );
122 
123  m_parentPanel->Connect( wxEVT_SCROLLWIN_BOTTOM,
124  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), nullptr, this );
125  m_parentPanel->Connect( wxEVT_SCROLLWIN_TOP,
126  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), nullptr, this );
127  m_parentPanel->Connect( wxEVT_SCROLLWIN_LINEUP,
128  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), nullptr, this );
129  m_parentPanel->Connect( wxEVT_SCROLLWIN_LINEDOWN,
130  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), nullptr, this );
131 #if defined USE_MOUSE_CAPTURE
132  m_parentPanel->Connect( wxEVT_MOUSE_CAPTURE_LOST,
133  wxMouseEventHandler( WX_VIEW_CONTROLS::onCaptureLost ), nullptr, this );
134 #endif
135 
136  m_cursorWarped = false;
137 
138  m_panTimer.SetOwner( this );
139  this->Connect( wxEVT_TIMER, wxTimerEventHandler( WX_VIEW_CONTROLS::onTimer ), nullptr, this );
140 
144 }
145 
146 
148 {
149 #if defined USE_MOUSE_CAPTURE
150  if( m_parentPanel->HasCapture() )
151  m_parentPanel->ReleaseMouse();
152 #endif
153 }
154 
155 
157 {
158  COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
159 
173 
174  m_zoomController.reset();
175 
176  if( cfg->m_Input.zoom_speed_auto )
177  {
179  }
180  else
181  {
182  if( cfg->m_Input.zoom_acceleration )
183  {
185  std::make_unique<ACCELERATING_ZOOM_CONTROLLER>( cfg->m_Input.zoom_speed );
186  }
187  else
188  {
190 
191  m_zoomController = std::make_unique<CONSTANT_ZOOM_CONTROLLER>( scale );
192  }
193  }
194 }
195 
196 
197 void WX_VIEW_CONTROLS::onMotion( wxMouseEvent& aEvent )
198 {
199  ( *m_MotionEventCounter )++;
200 
201  bool isAutoPanning = false;
202  int x = aEvent.GetX();
203  int y = aEvent.GetY();
204  VECTOR2D mousePos( x, y );
205 
207  handleCursorCapture( x, y );
208 
210  isAutoPanning = handleAutoPanning( aEvent );
211 
212  if( !isAutoPanning && aEvent.Dragging() )
213  {
214  if( m_state == DRAG_PANNING )
215  {
216  static bool justWarped = false;
217  int warpX = 0;
218  int warpY = 0;
219  wxSize parentSize = m_parentPanel->GetClientSize();
220 
221  if( x < 0 )
222  {
223  warpX = parentSize.x;
224  }
225  else if(x >= parentSize.x )
226  {
227  warpX = -parentSize.x;
228  }
229 
230  if( y < 0 )
231  {
232  warpY = parentSize.y;
233  }
234  else if( y >= parentSize.y )
235  {
236  warpY = -parentSize.y;
237  }
238 
239  if( !justWarped )
240  {
241  VECTOR2D d = m_dragStartPoint - mousePos;
242  VECTOR2D delta = m_view->ToWorld( d, false );
244  aEvent.StopPropagation();
245  }
246 
247  if( warpX || warpY )
248  {
249  if( !justWarped )
250  {
251  m_parentPanel->WarpPointer( x + warpX, y + warpY );
252  m_dragStartPoint += VECTOR2D( warpX, warpY );
253  justWarped = true;
254  }
255  else
256  justWarped = false;
257  }
258  else
259  justWarped = false;
260  }
261  else if( m_state == DRAG_ZOOMING )
262  {
263  static bool justWarped = false;
264  int warpY = 0;
265  wxSize parentSize = m_parentPanel->GetClientSize();
266 
267  if( y < 0 )
268  {
269  warpY = parentSize.y;
270  }
271  else if( y >= parentSize.y )
272  {
273  warpY = -parentSize.y;
274  }
275 
276  if( !justWarped )
277  {
278  VECTOR2D d = m_dragStartPoint - mousePos;
279  double scale = exp( d.y * m_settings.m_zoomSpeed * 0.001 );
280 
281  wxLogTrace( traceZoomScroll, wxString::Format( "dy: %f scale: %f", d.y, scale ) );
282 
284  aEvent.StopPropagation();
285  }
286 
287  if( warpY )
288  {
289  if( !justWarped )
290  {
291  m_parentPanel->WarpPointer( x, y + warpY );
292  m_dragStartPoint += VECTOR2D( 0, warpY );
293  justWarped = true;
294  }
295  else
296  justWarped = false;
297  }
298  else
299  justWarped = false;
300  }
301  }
302 
303  if( m_updateCursor ) // do not update the cursor position if it was explicitly set
304  m_cursorPos = GetClampedCoords( m_view->ToWorld( mousePos ) );
305  else
306  m_updateCursor = true;
307 
308  aEvent.Skip();
309 }
310 
311 
312 void WX_VIEW_CONTROLS::onWheel( wxMouseEvent& aEvent )
313 {
314 #ifdef __WXGTK3__
315  if( aEvent.GetTimestamp() == m_lastTimestamp )
316  {
317  aEvent.Skip( false );
318  return;
319  }
320 
321  m_lastTimestamp = aEvent.GetTimestamp();
322 #endif
323 
324  const double wheelPanSpeed = 0.001;
325  const int axis = aEvent.GetWheelAxis();
326 
327  if( axis == wxMOUSE_WHEEL_HORIZONTAL && !m_settings.m_horizontalPan )
328  return;
329 
330  // Pick the modifier, if any. Shift beats control beats alt, we don't support more than one.
331  int modifiers =
332  aEvent.ShiftDown() ? WXK_SHIFT :
333  ( aEvent.ControlDown() ? WXK_CONTROL : ( aEvent.AltDown() ? WXK_ALT : 0 ) );
334 
335  // Restrict zoom handling to the vertical axis, otherwise horizontal
336  // scrolling events (e.g. touchpads and some mice) end up interpreted
337  // as vertical scroll events and confuse the user.
338  if( axis == wxMOUSE_WHEEL_VERTICAL && modifiers == m_settings.m_scrollModifierZoom )
339  {
340  const int rotation = aEvent.GetWheelRotation();
341  const double zoomScale = m_zoomController->GetScaleForRotation( rotation );
342 
343  if( IsCursorWarpingEnabled() )
344  {
345  CenterOnCursor();
346  m_view->SetScale( m_view->GetScale() * zoomScale );
347  }
348  else
349  {
350  const VECTOR2D anchor = m_view->ToWorld( VECTOR2D( aEvent.GetX(), aEvent.GetY() ) );
351  m_view->SetScale( m_view->GetScale() * zoomScale, anchor );
352  }
353 
354  // Refresh the zoom level and mouse position on message panel
355  // (mouse position has not changed, only the zoom level has changed):
356  refreshMouse();
357  }
358  else
359  {
360  // Scrolling
361  VECTOR2D scrollVec = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) *
362  ( (double) aEvent.GetWheelRotation() * wheelPanSpeed );
363  double scrollX = 0.0;
364  double scrollY = 0.0;
365 
366  if( axis == wxMOUSE_WHEEL_HORIZONTAL || modifiers == m_settings.m_scrollModifierPanH )
367  scrollX = scrollVec.x;
368  else
369  scrollY = -scrollVec.y;
370 
371  VECTOR2D delta( scrollX, scrollY );
372 
374  refreshMouse();
375  }
376 
377  // Do not skip this event, otherwise wxWidgets will fire
378  // 3 wxEVT_SCROLLWIN_LINEUP or wxEVT_SCROLLWIN_LINEDOWN (normal wxWidgets behavior)
379  // and we do not want that.
381 }
382 
383 
384 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
385 void WX_VIEW_CONTROLS::onMagnify( wxMouseEvent& aEvent )
386 {
387  // Scale based on the magnification from our underlying magnification event.
388  VECTOR2D anchor = m_view->ToWorld( VECTOR2D( aEvent.GetX(), aEvent.GetY() ) );
389  m_view->SetScale( m_view->GetScale() * ( aEvent.GetMagnification() + 1.0f ), anchor );
390 
391  aEvent.Skip();
392 }
393 #endif
394 
395 
396 void WX_VIEW_CONTROLS::onButton( wxMouseEvent& aEvent )
397 {
398  switch( m_state )
399  {
400  case IDLE:
401  case AUTO_PANNING:
402  if( ( aEvent.MiddleDown() && m_settings.m_dragMiddle == MOUSE_DRAG_ACTION::PAN ) ||
403  ( aEvent.RightDown() && m_settings.m_dragRight == MOUSE_DRAG_ACTION::PAN ) )
404  {
405  m_dragStartPoint = VECTOR2D( aEvent.GetX(), aEvent.GetY() );
408 
409 #if defined USE_MOUSE_CAPTURE
410  if( !m_parentPanel->HasCapture() )
411  m_parentPanel->CaptureMouse();
412 #endif
413  }
414  else if( ( aEvent.MiddleDown() && m_settings.m_dragMiddle == MOUSE_DRAG_ACTION::ZOOM ) ||
415  ( aEvent.RightDown() && m_settings.m_dragRight == MOUSE_DRAG_ACTION::ZOOM ) )
416  {
417  m_dragStartPoint = VECTOR2D( aEvent.GetX(), aEvent.GetY() );
421 
422 #if defined USE_MOUSE_CAPTURE
423  if( !m_parentPanel->HasCapture() )
424  m_parentPanel->CaptureMouse();
425 #endif
426  }
427 
428  if( aEvent.LeftUp() )
429  m_state = IDLE; // Stop autopanning when user release left mouse button
430 
431  break;
432 
433  case DRAG_ZOOMING:
434  case DRAG_PANNING:
435  if( aEvent.MiddleUp() || aEvent.LeftUp() || aEvent.RightUp() )
436  {
437  m_state = IDLE;
438 
439 #if defined USE_MOUSE_CAPTURE
440  if( !m_settings.m_cursorCaptured && m_parentPanel->HasCapture() )
441  m_parentPanel->ReleaseMouse();
442 #endif
443  }
444 
445  break;
446  }
447 
448  aEvent.Skip();
449 }
450 
451 
452 void WX_VIEW_CONTROLS::onEnter( wxMouseEvent& aEvent )
453 {
454  // Avoid stealing focus from text controls
455  // This is particularly important for users using On-Screen-Keyboards
456  // They may move the mouse over the canvas to reach the keyboard
458  {
459  return;
460  }
461 
462 #if defined( _WIN32 )
463  // Win32 transmits mouse move and wheel events to all controls below the mouse regardless
464  // of focus. Forcing the focus here will cause the EDA FRAMES to immediately become the
465  // top level active window.
466  if( m_parentPanel->GetParent() != nullptr )
467  {
468  // this assumes the parent panel's parent is the eda window
469  if( KIPLATFORM::UI::IsWindowActive( m_parentPanel->GetParent() ) )
470  {
472  }
473  }
474 #else
476 #endif
477 }
478 
479 
480 void WX_VIEW_CONTROLS::onLeave( wxMouseEvent& aEvent )
481 {
482 #if !defined USE_MOUSE_CAPTURE
483  onMotion( aEvent );
484 #endif
485 }
486 
487 void WX_VIEW_CONTROLS::onCaptureLost( wxMouseEvent& aEvent )
488 {
489  // This method must be present to suppress the capture-lost assertion
490 
491  // Set the flag to allow calling m_parentPanel->CaptureMouse()
492  // Note: One cannot call m_parentPanel->CaptureMouse() twice, this is not accepted
493  // by wxWidgets (MSW specific) so we need this guard
495 }
496 
497 
498 void WX_VIEW_CONTROLS::onTimer( wxTimerEvent& aEvent )
499 {
500  switch( m_state )
501  {
502  case AUTO_PANNING:
503  {
505  {
506  m_state = IDLE;
507  return;
508  }
509 
510  if( !m_parentPanel->HasFocus() )
511  break;
512 
513  double borderSize = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
515 
516  // When the mouse cursor is outside the area with no pan,
517  // m_panDirection is the dist to this area limit ( in pixels )
518  // It will be used also as pan value (the pan speed depends on this dist).
519  VECTOR2D dir( m_panDirection );
520 
521  // When the mouse cursor is outside the area with no pan, the pan value
522  // is accelerated depending on the dist between the area and the cursor
523  float accel = 0.5f + ( m_settings.m_autoPanAcceleration / 5.0f );
524 
525  // For a small mouse cursor dist to area, just use the distance.
526  // But for a dist > borderSize / 2, use an accelerated pan value
527 
528  if( dir.EuclideanNorm() >= borderSize ) // far from area limits
529  dir = dir.Resize( borderSize * accel );
530  else if( dir.EuclideanNorm() > borderSize / 2 ) // Near from area limits
531  dir = dir.Resize( borderSize );
532 
533  dir = m_view->ToWorld( dir, false );
534  m_view->SetCenter( m_view->GetCenter() + dir );
535 
536  refreshMouse();
537  }
538  break;
539 
540  case IDLE: // Just remove unnecessary warnings
541  case DRAG_PANNING:
542  case DRAG_ZOOMING:
543  break;
544  }
545 }
546 
547 
548 void WX_VIEW_CONTROLS::onScroll( wxScrollWinEvent& aEvent )
549 {
550  const double linePanDelta = 0.05;
551  const double pagePanDelta = 0.5;
552 
553  int type = aEvent.GetEventType();
554  int dir = aEvent.GetOrientation();
555 
556  if( type == wxEVT_SCROLLWIN_THUMBTRACK )
557  {
558  auto center = m_view->GetCenter();
559  const auto& boundary = m_view->GetBoundary();
560 
561  // Flip scroll direction in flipped view
562  const double xstart = ( m_view->IsMirroredX() ?
563  boundary.GetRight() : boundary.GetLeft() );
564  const double xdelta = ( m_view->IsMirroredX() ? -1 : 1 );
565 
566  if( dir == wxHORIZONTAL )
567  center.x = xstart + xdelta * ( aEvent.GetPosition() / m_scrollScale.x );
568  else
569  center.y = boundary.GetTop() + aEvent.GetPosition() / m_scrollScale.y;
570 
571  m_view->SetCenter( center );
572  }
573  else
574  {
575  double dist = 0;
576 
577  if( type == wxEVT_SCROLLWIN_PAGEUP )
578  dist = pagePanDelta;
579  else if( type == wxEVT_SCROLLWIN_PAGEDOWN )
580  dist = -pagePanDelta;
581  else if( type == wxEVT_SCROLLWIN_LINEUP )
582  dist = linePanDelta;
583  else if( type == wxEVT_SCROLLWIN_LINEDOWN )
584  dist = -linePanDelta;
585  else
586  wxCHECK_MSG( false, /* void */, wxT( "Unhandled event type" ) );
587 
588  VECTOR2D scroll = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) * dist;
589 
590  double scrollX = 0.0;
591  double scrollY = 0.0;
592 
593  if ( dir == wxHORIZONTAL )
594  scrollX = -scroll.x;
595  else
596  scrollY = -scroll.y;
597 
598  VECTOR2D delta( scrollX, scrollY );
599 
601  }
602 
604 }
605 
606 
607 void WX_VIEW_CONTROLS::CaptureCursor( bool aEnabled )
608 {
609 #if defined USE_MOUSE_CAPTURE
610  // Note: for some reason, m_parentPanel->HasCapture() can be false even if CaptureMouse()
611  // was called (i.e. mouse was captured, so when need to test m_MouseCapturedLost to be
612  // sure a wxEVT_MOUSE_CAPTURE_LOST event was fired before. Otherwise wxMSW complains
613  if( aEnabled && !m_parentPanel->HasCapture() && m_parentPanel->m_MouseCapturedLost )
614  {
615  m_parentPanel->CaptureMouse();
616 
617  // Clear the flag to allow calling m_parentPanel->CaptureMouse()
618  // Calling it without calling ReleaseMouse() is not accepted by wxWidgets (MSW specific)
620  }
621  else if( !aEnabled && m_parentPanel->HasCapture()
623  {
624  m_parentPanel->ReleaseMouse();
625 
626  // Mouse is released, calling CaptureMouse() is allowed now:
628  }
629 #endif
630  VIEW_CONTROLS::CaptureCursor( aEnabled );
631 }
632 
633 
635 {
637  {
638  m_state = IDLE;
639 #if defined USE_MOUSE_CAPTURE
640  if( !m_settings.m_cursorCaptured && m_parentPanel->HasCapture() )
641  m_parentPanel->ReleaseMouse();
642 #endif
643  }
644 }
645 
646 
647 VECTOR2D WX_VIEW_CONTROLS::GetMousePosition( bool aWorldCoordinates ) const
648 {
649  wxPoint msp = getMouseScreenPosition();
650  VECTOR2D screenPos( msp.x, msp.y );
651 
652  return aWorldCoordinates ? GetClampedCoords( m_view->ToWorld( screenPos ) ) : screenPos;
653 }
654 
655 
657 {
658  GAL* gal = m_view->GetGAL();
659 
660  if( aEnableSnapping && gal->GetGridSnapping() )
661  {
662  return gal->GetGridPoint( m_cursorPos );
663  }
664  else
665  {
666  return m_cursorPos;
667  }
668 }
669 
670 
671 VECTOR2D WX_VIEW_CONTROLS::GetCursorPosition( bool aEnableSnapping ) const
672 {
674  {
676  }
677  else
678  {
679  return GetClampedCoords( GetRawCursorPosition( aEnableSnapping ) );
680  }
681 }
682 
683 
684 void WX_VIEW_CONTROLS::SetCursorPosition( const VECTOR2D& aPosition, bool aWarpView,
685  bool aTriggeredByArrows, long aArrowCommand )
686 {
687  m_updateCursor = false;
688 
689  VECTOR2D clampedPosition = GetClampedCoords( aPosition );
690 
691  if( aTriggeredByArrows )
692  {
694  m_settings.m_lastKeyboardCursorPosition = clampedPosition;
695  m_settings.m_lastKeyboardCursorCommand = aArrowCommand;
696  m_cursorWarped = false;
697  }
698  else
699  {
703  m_cursorWarped = true;
704  }
705 
706  WarpCursor( clampedPosition, true, aWarpView );
707  m_cursorPos = clampedPosition;
708 }
709 
710 
712  bool aWarpView = true )
713 {
714  m_updateCursor = false;
715 
716  VECTOR2D clampedPosition = GetClampedCoords( aPosition );
717 
718  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
719  BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
720  VECTOR2D screenPos = m_view->ToScreen( clampedPosition );
721 
722  if( aWarpView && !screen.Contains( screenPos ) )
723  m_view->SetCenter( clampedPosition );
724 
725  m_cursorPos = clampedPosition;
726 }
727 
728 
729 void WX_VIEW_CONTROLS::WarpCursor( const VECTOR2D& aPosition, bool aWorldCoordinates,
730  bool aWarpView )
731 {
732  if( aWorldCoordinates )
733  {
734  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
735  BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
736  VECTOR2D clampedPosition = GetClampedCoords( aPosition );
737  VECTOR2D screenPos = m_view->ToScreen( clampedPosition );
738 
739  if( !screen.Contains( screenPos ) )
740  {
741  if( aWarpView )
742  {
743  m_view->SetCenter( clampedPosition );
744  m_parentPanel->WarpPointer( screenSize.x / 2, screenSize.y / 2 );
745  }
746  }
747  else
748  {
749  m_parentPanel->WarpPointer( screenPos.x, screenPos.y );
750  }
751  }
752  else
753  {
754  m_parentPanel->WarpPointer( aPosition.x, aPosition.y );
755  }
756 
757  refreshMouse();
758 }
759 
760 
762 {
763  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
764  VECTOR2I screenCenter( screenSize / 2 );
765 
766  if( GetMousePosition( false ) != screenCenter )
767  {
769  m_parentPanel->WarpPointer( KiROUND( screenSize.x / 2 ), KiROUND( screenSize.y / 2 ) );
770  }
771 }
772 
773 
774 bool WX_VIEW_CONTROLS::handleAutoPanning( const wxMouseEvent& aEvent )
775 {
776  VECTOR2I p( aEvent.GetX(), aEvent.GetY() );
778 
780  {
781  // last cursor move event came from keyboard cursor control. If auto-panning is enabled
782  // and the next position is inside the autopan zone, check if it really came from a mouse
783  // event, otherwise disable autopan temporarily. Also temporarily disable autopan if the
784  // cursor is in the autopan zone because the application warped the cursor.
785 
786  m_cursorWarped = false;
787  return true;
788  }
789 
790  m_cursorWarped = false;
791 
792  // Compute areas where autopanning is active
793  int borderStart = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
795  borderStart = std::max( borderStart, 2 );
796  int borderEndX = m_view->GetScreenPixelSize().x - borderStart;
797  int borderEndY = m_view->GetScreenPixelSize().y - borderStart;
798 
799  if( p.x < borderStart )
800  m_panDirection.x = -( borderStart - p.x );
801  else if( p.x > borderEndX )
802  m_panDirection.x = ( p.x - borderEndX );
803  else
804  m_panDirection.x = 0;
805 
806  if( p.y < borderStart )
807  m_panDirection.y = -( borderStart - p.y );
808  else if( p.y > borderEndY )
809  m_panDirection.y = ( p.y - borderEndY );
810  else
811  m_panDirection.y = 0;
812 
813  bool borderHit = ( m_panDirection.x != 0 || m_panDirection.y != 0 );
814 
815  switch( m_state )
816  {
817  case AUTO_PANNING:
818  if( !borderHit )
819  {
820  m_panTimer.Stop();
821  m_state = IDLE;
822 
823  return false;
824  }
825 
826  return true;
827  break;
828 
829  case IDLE:
830  if( borderHit )
831  {
833  m_panTimer.Start( (int) ( 250.0 / 60.0 ) );
834 
835  return true;
836  }
837 
838  return false;
839  break;
840 
841  case DRAG_PANNING:
842  case DRAG_ZOOMING:
843  return false;
844  }
845 
846  wxCHECK_MSG( false, false, wxT( "This line should never be reached" ) );
847 }
848 
849 
851 {
853  {
854  bool warp = false;
855  wxSize parentSize = m_parentPanel->GetClientSize();
856 
857  if( x < 0 )
858  {
859  x = 0;
860  warp = true;
861  }
862  else if( x >= parentSize.x )
863  {
864  x = parentSize.x - 1;
865  warp = true;
866  }
867 
868  if( y < 0 )
869  {
870  y = 0;
871  warp = true;
872  }
873  else if( y >= parentSize.y )
874  {
875  y = parentSize.y - 1;
876  warp = true;
877  }
878 
879  if( warp )
880  m_parentPanel->WarpPointer( x, y );
881  }
882 }
883 
884 
886 {
887  // Notify tools that the cursor position has changed in the world coordinates
888  wxMouseEvent moveEvent( EVT_REFRESH_MOUSE );
889  wxPoint msp = getMouseScreenPosition();
890  moveEvent.SetX( msp.x );
891  moveEvent.SetY( msp.y );
892 
893  // Set the modifiers state
894  moveEvent.SetControlDown( wxGetKeyState( WXK_CONTROL ) );
895  moveEvent.SetShiftDown( wxGetKeyState( WXK_SHIFT ) );
896  moveEvent.SetAltDown( wxGetKeyState( WXK_ALT ) );
897 
898  m_cursorPos = GetClampedCoords( m_view->ToWorld( VECTOR2D( msp.x, msp.y ) ) );
899  wxPostEvent( m_parentPanel, moveEvent );
900 }
901 
902 
904 {
905  wxPoint msp = wxGetMousePosition();
906  m_parentPanel->ScreenToClient( &msp.x, &msp.y );
907  return msp;
908 }
909 
910 
912 {
913  const BOX2D viewport = m_view->GetViewport();
914  const BOX2D& boundary = m_view->GetBoundary();
915 
916  m_scrollScale.x = 2e3 / viewport.GetWidth(); // TODO it does not have to be updated so often
917  m_scrollScale.y = 2e3 / viewport.GetHeight();
918  VECTOR2I newScroll( ( viewport.Centre().x - boundary.GetLeft() ) * m_scrollScale.x,
919  ( viewport.Centre().y - boundary.GetTop() ) * m_scrollScale.y );
920 
921  // We add the width of the scroll bar thumb to the range because the scroll range is given by
922  // the full bar while the position is given by the left/top position of the thumb
923  VECTOR2I newRange( m_scrollScale.x * boundary.GetWidth() +
924  m_parentPanel->GetScrollThumb( wxSB_HORIZONTAL ),
925  m_scrollScale.y * boundary.GetHeight() +
926  m_parentPanel->GetScrollThumb( wxSB_VERTICAL ) );
927 
928  // Flip scroll direction in flipped view
929  if( m_view->IsMirroredX() )
930  newScroll.x = ( boundary.GetRight() - viewport.Centre().x ) * m_scrollScale.x;
931 
932  // Adjust scrollbars only if it is needed. Otherwise there are cases when canvas is continuously
933  // refreshed (Windows)
934  if( m_scrollPos != newScroll || newRange.x != m_parentPanel->GetScrollRange( wxSB_HORIZONTAL )
935  || newRange.y != m_parentPanel->GetScrollRange( wxSB_VERTICAL ) )
936  {
937  m_parentPanel->SetScrollbars( 1, 1, newRange.x, newRange.y, newScroll.x, newScroll.y,
938  true );
939  m_scrollPos = newScroll;
940 
941 #if !defined( __APPLE__ ) && !defined( WIN32 )
942  // Trigger a mouse refresh to get the canvas update in GTK (re-draws the scrollbars).
943  // Note that this causes an infinite loop on OSX and Windows (in certain cases) as it
944  // generates a paint event.
945  refreshMouse();
946 #endif
947  }
948 }
949 
950 void WX_VIEW_CONTROLS::ForceCursorPosition( bool aEnabled, const VECTOR2D& aPosition )
951 {
952  VECTOR2D clampedPosition = GetClampedCoords( aPosition );
953 
955  m_settings.m_forcedPosition = clampedPosition;
956 }
VECTOR2D m_lookStartPoint
Current direction of panning (only autopanning mode).
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:512
const wxChar *const traceZoomScroll
Flag to enable debug output of zoom-scrolling calculations in KIGFX::ZOOM_CONTROLLER and derivatives.
void LoadSettings() override
Event that forces mouse move event in the dispatcher (eg.
MOUSE_DRAG_ACTION m_dragRight
Is last cursor motion event coming from keyboard arrow cursor motion action.
EDA_DRAW_PANEL_GAL * m_parentPanel
Store information about point where dragging has started.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:243
const BOX2D & GetBoundary() const
Definition: view.h:293
Panning with mouse button pressed.
static const wxEventType EVT_REFRESH_MOUSE
static constexpr double MSW_SCALE
A suitable (magic) scale factor for Windows systems.
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:449
coord_type GetTop() const
Definition: box2.h:187
MOUSE_DRAG_ACTION drag_left
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition: view.h:334
wxPoint getMouseScreenPosition() const
Get the cursor position in the screen coordinates.
VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const override
Return the current mouse pointer position.
std::unique_ptr< ZOOM_CONTROLLER > m_zoomController
void onButton(wxMouseEvent &aEvent)
VC_SETTINGS m_settings
static std::unique_ptr< ZOOM_CONTROLLER > GetZoomControllerForPlatform(bool aAcceleration)
VECTOR2D m_forcedPosition
Is the forced cursor position enabled.
Definition: view_controls.h:55
coord_type GetRight() const
Definition: box2.h:182
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:190
VIEW * m_view
< Pointer to controlled VIEW.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:622
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
VECTOR2D m_scrollScale
Current scrollbar position.
WX_VIEW_CONTROLS class definition.
static constexpr double GTK3_SCALE
A suitable (magic) scale factor for Mac systems.
MOUSE_DRAG_ACTION drag_right
void UpdateScrollbars()
End any mouse drag action still in progress.
void onCaptureLost(wxMouseEvent &WXUNUSED(aEvent))
VECTOR2D GetGridPoint(const VECTOR2D &aPoint) const
For a given point it returns the nearest point belonging to the grid in world coordinates.
Zooming with mouse button pressed.
const VECTOR2I & GetScreenPixelSize() const
Return GAL canvas size in pixels.
int m_scrollModifierPanH
What modifier key to enable vertical with the (vertical) scroll wheel.
VECTOR2D m_lastKeyboardCursorPosition
bool GetGridSnapping() const
bool IsInputControlFocused(wxWindow *aFocus=nullptr)
Check if a input control has focus.
Definition: ui_common.cpp:220
VECTOR2D GetRawCursorPosition(bool aSnappingEnabled=true) const override
Return the current cursor position in world coordinates ignoring the cursorUp position force mode.
bool m_autoPanSettingEnabled
Distance from cursor to VIEW edge when panning is active.
Definition: view_controls.h:73
void onWheel(wxMouseEvent &aEvent)
Handler functions.
MOUSE_DRAG_ACTION m_dragLeft
VECTOR2< ret_type > GetClampedCoords(const VECTOR2< in_type > &aCoords, pad_type aPadding=0u)
Clamps a vector to values that can be negated, respecting numeric limits of coordinates data type wit...
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:578
bool IsWindowActive(wxWindow *aWindow)
Check to see if the given window is the currently active window (e.g.
Definition: gtk/ui.cpp:50
Panning on approaching borders of the frame.
bool m_forceCursorPosition
Should the cursor be locked within the parent window area.
Definition: view_controls.h:58
VECTOR2< double > VECTOR2D
Definition: vector2d.h:621
MOUSE_DRAG_ACTION m_dragMiddle
bool m_zoomSpeedAuto
What modifier key to enable zoom with the (vertical) scroll wheel.
Definition: view_controls.h:97
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
coord_type GetWidth() const
Definition: box2.h:180
int m_scrollModifierZoom
What modifier key to enable horizontal pan with the (vertical) scroll wheel.
bool Contains(const Vec &aPoint) const
Definition: box2.h:134
void SetCrossHairCursorPosition(const VECTOR2D &aPosition, bool aWarpView) override
void CaptureCursor(bool aEnabled) override
Force the cursor to stay within the drawing panel area.
float m_autoPanAcceleration
If the cursor is allowed to be warped.
Definition: view_controls.h:82
int m_zoomSpeed
When true, ignore zoom_speed and pick a platform-specific default.
Definition: view_controls.h:94
const VECTOR2I & GetScreenPixelSize() const
Return the size of the our rendering area in pixels.
Definition: view.cpp:1167
Functions to provide common constants and other functions to assist in making a consistent UI.
bool IsCursorWarpingEnabled() const
An interface for classes handling user events controlling the view behavior such as zooming,...
bool m_warpCursor
Enable horizontal panning with the horizontal scroll/trackpad input.
Definition: view_controls.h:85
static constexpr double MAC_SCALE
wxLogTrace helper definitions.
virtual void SetFocus() override
a few functions useful in geometry calculations.
float m_autoPanMargin
How fast is panning when in auto mode.
Definition: view_controls.h:76
bool m_horizontalPan
Enable the accelerating zoom controller.
Definition: view_controls.h:88
bool m_cursorWarped
Current VIEW_CONTROLS settings.
void refreshMouse()
Send an event to refresh mouse position.
VECTOR2D ToScreen(const VECTOR2D &aCoord, bool aAbsolute=true) const
Convert a world space point/vector to a point/vector in screen space coordinates.
Definition: view.cpp:468
bool m_autoPanEnabled
Flag for turning on autopanning.
Definition: view_controls.h:70
void handleCursorCapture(int x, int y)
Limit the cursor position to within the canvas by warping it.
bool m_zoomAcceleration
Zoom speed for the non-accelerating zoom controller.
Definition: view_controls.h:91
void onMotion(wxMouseEvent &aEvent)
void WarpCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false) override
Set the viewport center to the current cursor position and warps the cursor to the screen center.
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition: view.h:238
void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0)) override
Applies VIEW_CONTROLS settings from the program COMMON_SETTINGS.
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:552
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
VECTOR2D m_zoomStartPoint
Current cursor position (world coordinates).
std::unique_ptr< PROF_COUNTER > m_MotionEventCounter
void onTimer(wxTimerEvent &WXUNUSED(aEvent))
bool m_MouseCapturedLost
used on wxMSW: true after a wxEVT_MOUSE_CAPTURE_LOST was received false after the mouse is recaptured...
const int scale
see class PGM_BASE
Vec Centre() const
Definition: box2.h:63
VECTOR2D m_panDirection
Timer responsible for handling autopanning.
long m_lastKeyboardCursorCommand
Position of the above event.
static constexpr double MANUAL_SCALE_FACTOR
bool m_updateCursor
A #ZOOM_CONTROLLER that determines zoom steps. This is platform-specific.
bool m_lastKeyboardCursorPositionValid
ACTIONS::CURSOR_UP, ACTIONS::CURSOR_DOWN, etc.
double m_initialZoomScale
The mouse position when a drag zoom started.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
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:73
VECTOR2I m_scrollPos
The zoom scale when a drag zoom started.
coord_type GetHeight() const
Definition: box2.h:181
bool handleAutoPanning(const wxMouseEvent &aEvent)
Compute new viewport settings while in autopanning mode.
void onEnter(wxMouseEvent &WXUNUSED(aEvent))
VECTOR2D m_dragStartPoint
Stores information about the center of viewport when dragging has started.
constexpr int delta
void SetCursorPosition(const VECTOR2D &aPosition, bool warpView, bool aTriggeredByArrows, long aArrowCommand) override
Move the graphic crosshair cursor to the requested position expressed in world coordinates.
STATE m_state
Panel that is affected by VIEW_CONTROLS.
coord_type GetLeft() const
Definition: box2.h:186
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
double GetScale() const
Definition: view.h:264
void CenterOnCursor() const override
Adjusts the scrollbars position to match the current viewport.
bool m_cursorCaptured
Should the cursor snap to grid or move freely.
Definition: view_controls.h:61
void onScroll(wxScrollWinEvent &aEvent)
WX_VIEW_CONTROLS(VIEW *aView, EDA_DRAW_PANEL_GAL *aParentPanel)
MOUSE_DRAG_ACTION drag_middle
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
Abstract interface for drawing on a 2D-surface.
void onLeave(wxMouseEvent &WXUNUSED(aEvent))
VECTOR2D m_cursorPos
Flag deciding whether the cursor position should be calculated using the mouse position.
wxTimer m_panTimer
Ratio used for scaling world coordinates to scrollbar position.