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 <trace_helpers.h>
37 #include <math/util.h> // for KiROUND
38 #include <widgets/ui_common.h>
39 
40 #if defined __WXMSW__
41  #define USE_MOUSE_CAPTURE
42 #endif
43 
44 using namespace KIGFX;
45 
46 const wxEventType WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE = wxNewEventType();
47 
48 
49 static std::unique_ptr<ZOOM_CONTROLLER> GetZoomControllerForPlatform()
50 {
51 #ifdef __WXMAC__
52  // On Apple pointer devices, wheel events occur frequently and with
53  // smaller rotation values. For those devices, let's handle zoom
54  // based on the rotation amount rather than the time difference.
55  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::MAC_SCALE );
56 #elif __WXGTK3__
57  // GTK3 is similar, but the scale constant is smaller
58  return std::make_unique<CONSTANT_ZOOM_CONTROLLER>( CONSTANT_ZOOM_CONTROLLER::GTK3_SCALE );
59 #else
60  return std::make_unique<ACCELERATING_ZOOM_CONTROLLER>();
61 #endif
62 }
63 
64 
65 WX_VIEW_CONTROLS::WX_VIEW_CONTROLS( VIEW* aView, wxScrolledCanvas* aParentPanel ) :
66  VIEW_CONTROLS( aView ),
67  m_state( IDLE ),
68  m_parentPanel( aParentPanel ),
69  m_scrollScale( 1.0, 1.0 ),
70  m_initialZoomScale( 0.0 ),
71 #ifdef __WXGTK3__
72  m_lastTimestamp( 0 ),
73 #endif
74  m_cursorPos( 0, 0 ),
75  m_updateCursor( true )
76 {
77  LoadSettings();
78 
79  m_parentPanel->Connect( wxEVT_MOTION,
80  wxMouseEventHandler( WX_VIEW_CONTROLS::onMotion ), NULL, this );
81 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
82  m_parentPanel->Connect( wxEVT_MAGNIFY,
83  wxMouseEventHandler( WX_VIEW_CONTROLS::onMagnify ), NULL, this );
84 #endif
85  m_parentPanel->Connect( wxEVT_MOUSEWHEEL,
86  wxMouseEventHandler( WX_VIEW_CONTROLS::onWheel ), NULL, this );
87  m_parentPanel->Connect( wxEVT_MIDDLE_UP,
88  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
89  m_parentPanel->Connect( wxEVT_MIDDLE_DOWN,
90  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
91  m_parentPanel->Connect( wxEVT_LEFT_UP,
92  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
93  m_parentPanel->Connect( wxEVT_LEFT_DOWN,
94  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
95  m_parentPanel->Connect( wxEVT_RIGHT_UP,
96  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
97  m_parentPanel->Connect( wxEVT_RIGHT_DOWN,
98  wxMouseEventHandler( WX_VIEW_CONTROLS::onButton ), NULL, this );
99 #if defined __WXMSW__
100  m_parentPanel->Connect( wxEVT_ENTER_WINDOW,
101  wxMouseEventHandler( WX_VIEW_CONTROLS::onEnter ), NULL, this );
102 #endif
103  m_parentPanel->Connect( wxEVT_LEAVE_WINDOW,
104  wxMouseEventHandler( WX_VIEW_CONTROLS::onLeave ), NULL, this );
105  m_parentPanel->Connect( wxEVT_SCROLLWIN_THUMBTRACK,
106  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
107  m_parentPanel->Connect( wxEVT_SCROLLWIN_PAGEUP,
108  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
109  m_parentPanel->Connect( wxEVT_SCROLLWIN_PAGEDOWN,
110  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
111 
112  m_parentPanel->Connect( wxEVT_SCROLLWIN_BOTTOM,
113  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
114  m_parentPanel->Connect( wxEVT_SCROLLWIN_TOP,
115  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
116  m_parentPanel->Connect( wxEVT_SCROLLWIN_LINEUP,
117  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
118  m_parentPanel->Connect( wxEVT_SCROLLWIN_LINEDOWN,
119  wxScrollWinEventHandler( WX_VIEW_CONTROLS::onScroll ), NULL, this );
120 #if defined USE_MOUSE_CAPTURE
121  m_parentPanel->Connect( wxEVT_MOUSE_CAPTURE_LOST,
122  wxMouseEventHandler( WX_VIEW_CONTROLS::onCaptureLost ), NULL, this );
123 #endif
124 
125  m_cursorWarped = false;
126 
127  m_panTimer.SetOwner( this );
128  this->Connect( wxEVT_TIMER, wxTimerEventHandler( WX_VIEW_CONTROLS::onTimer ), NULL, this );
129 
133 }
134 
135 
137 {
138 #if defined USE_MOUSE_CAPTURE
139  if( m_parentPanel->HasCapture() )
140  m_parentPanel->ReleaseMouse();
141 #endif
142 }
143 
144 
146 {
147  COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
148 
162 
163  m_zoomController.reset();
164 
165  if( cfg->m_Input.zoom_speed_auto )
166  {
167  // TODO(JE) this ignores the acceleration option
169  }
170  else
171  {
172  if( cfg->m_Input.zoom_acceleration )
173  {
175  std::make_unique<ACCELERATING_ZOOM_CONTROLLER>( cfg->m_Input.zoom_speed );
176  }
177  else
178  {
180 
181  m_zoomController = std::make_unique<CONSTANT_ZOOM_CONTROLLER>( scale );
182  }
183  }
184 }
185 
186 
187 void WX_VIEW_CONTROLS::onMotion( wxMouseEvent& aEvent )
188 {
189  bool isAutoPanning = false;
190  int x = aEvent.GetX();
191  int y = aEvent.GetY();
192  VECTOR2D mousePos( x, y );
193 
195  handleCursorCapture( x, y );
196 
198  isAutoPanning = handleAutoPanning( aEvent );
199 
200  if( !isAutoPanning && aEvent.Dragging() )
201  {
202  if( m_state == DRAG_PANNING )
203  {
204  static bool justWarped = false;
205  int warpX = 0;
206  int warpY = 0;
207  wxSize parentSize = m_parentPanel->GetClientSize();
208 
209  if( x < 0 )
210  {
211  warpX = parentSize.x;
212  }
213  else if(x >= parentSize.x )
214  {
215  warpX = -parentSize.x;
216  }
217 
218  if( y < 0 )
219  {
220  warpY = parentSize.y;
221  }
222  else if( y >= parentSize.y )
223  {
224  warpY = -parentSize.y;
225  }
226 
227  if( !justWarped )
228  {
229  VECTOR2D d = m_dragStartPoint - mousePos;
230  VECTOR2D delta = m_view->ToWorld( d, false );
231  m_view->SetCenter( m_lookStartPoint + delta );
232  aEvent.StopPropagation();
233  }
234 
235  if( warpX || warpY )
236  {
237  if( !justWarped )
238  {
239  m_parentPanel->WarpPointer( x + warpX, y + warpY );
240  m_dragStartPoint += VECTOR2D( warpX, warpY );
241  justWarped = true;
242  }
243  else
244  justWarped = false;
245  }
246  else
247  justWarped = false;
248  }
249  else if( m_state == DRAG_ZOOMING )
250  {
251  static bool justWarped = false;
252  int warpY = 0;
253  wxSize parentSize = m_parentPanel->GetClientSize();
254 
255  if( y < 0 )
256  {
257  warpY = parentSize.y;
258  }
259  else if( y >= parentSize.y )
260  {
261  warpY = -parentSize.y;
262  }
263 
264  if( !justWarped )
265  {
266  VECTOR2D d = m_dragStartPoint - mousePos;
267  double scale = exp( d.y * m_settings.m_zoomSpeed * 0.001 );
268 
269  wxLogTrace( traceZoomScroll, wxString::Format( "dy: %f scale: %f", d.y, scale ) );
270 
272  aEvent.StopPropagation();
273  }
274 
275  if( warpY )
276  {
277  if( !justWarped )
278  {
279  m_parentPanel->WarpPointer( x, y + warpY );
280  m_dragStartPoint += VECTOR2D( 0, warpY );
281  justWarped = true;
282  }
283  else
284  justWarped = false;
285  }
286  else
287  justWarped = false;
288  }
289  }
290 
291  if( m_updateCursor ) // do not update the cursor position if it was explicitly set
292  m_cursorPos = m_view->ToWorld( mousePos );
293  else
294  m_updateCursor = true;
295 
296  aEvent.Skip();
297 }
298 
299 
300 void WX_VIEW_CONTROLS::onWheel( wxMouseEvent& aEvent )
301 {
302 #ifdef __WXGTK3__
303  if( aEvent.GetTimestamp() == m_lastTimestamp )
304  {
305  aEvent.Skip( false );
306  return;
307  }
308 
309  m_lastTimestamp = aEvent.GetTimestamp();
310 #endif
311 
312  const double wheelPanSpeed = 0.001;
313  const int axis = aEvent.GetWheelAxis();
314 
315  if( axis == wxMOUSE_WHEEL_HORIZONTAL && !m_settings.m_horizontalPan )
316  return;
317 
318  // Pick the modifier, if any. Shift beats control beats alt, we don't support more than one.
319  int modifiers =
320  aEvent.ShiftDown() ? WXK_SHIFT :
321  ( aEvent.ControlDown() ? WXK_CONTROL : ( aEvent.AltDown() ? WXK_ALT : 0 ) );
322 
323  // Restrict zoom handling to the vertical axis, otherwise horizontal
324  // scrolling events (e.g. touchpads and some mice) end up interpreted
325  // as vertical scroll events and confuse the user.
326  if( axis == wxMOUSE_WHEEL_VERTICAL && modifiers == m_settings.m_scrollModifierZoom )
327  {
328  const int rotation = aEvent.GetWheelRotation();
329  const double zoomScale = m_zoomController->GetScaleForRotation( rotation );
330 
331  if( IsCursorWarpingEnabled() )
332  {
333  CenterOnCursor();
334  m_view->SetScale( m_view->GetScale() * zoomScale );
335  }
336  else
337  {
338  const VECTOR2D anchor = m_view->ToWorld( VECTOR2D( aEvent.GetX(), aEvent.GetY() ) );
339  m_view->SetScale( m_view->GetScale() * zoomScale, anchor );
340  }
341 
342  // Refresh the zoom level and mouse position on message panel
343  // (mouse position has not changed, only the zoom level has changed):
344  refreshMouse();
345  }
346  else
347  {
348  // Scrolling
349  VECTOR2D scrollVec = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) *
350  ( (double) aEvent.GetWheelRotation() * wheelPanSpeed );
351  double scrollX = 0.0;
352  double scrollY = 0.0;
353 
354  if( axis == wxMOUSE_WHEEL_HORIZONTAL || modifiers == m_settings.m_scrollModifierPanH )
355  scrollX = scrollVec.x;
356  else
357  scrollY = -scrollVec.y;
358 
359  VECTOR2D delta( scrollX, scrollY );
360 
361  m_view->SetCenter( m_view->GetCenter() + delta );
362  refreshMouse();
363  }
364 
365  // Do not skip this event, otherwise wxWidgets will fire
366  // 3 wxEVT_SCROLLWIN_LINEUP or wxEVT_SCROLLWIN_LINEDOWN (normal wxWidgets behavior)
367  // and we do not want that.
368  m_parentPanel->Refresh();
369 }
370 
371 
372 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
373 void WX_VIEW_CONTROLS::onMagnify( wxMouseEvent& aEvent )
374 {
375  // Scale based on the magnification from our underlying magnification event.
376  VECTOR2D anchor = m_view->ToWorld( VECTOR2D( aEvent.GetX(), aEvent.GetY() ) );
377  m_view->SetScale( m_view->GetScale() * ( aEvent.GetMagnification() + 1.0f ), anchor );
378 
379  aEvent.Skip();
380 }
381 #endif
382 
383 
384 void WX_VIEW_CONTROLS::onButton( wxMouseEvent& aEvent )
385 {
386  switch( m_state )
387  {
388  case IDLE:
389  case AUTO_PANNING:
390  if( ( aEvent.MiddleDown() && m_settings.m_dragMiddle == MOUSE_DRAG_ACTION::PAN ) ||
391  ( aEvent.RightDown() && m_settings.m_dragRight == MOUSE_DRAG_ACTION::PAN ) )
392  {
393  m_dragStartPoint = VECTOR2D( aEvent.GetX(), aEvent.GetY() );
396 #if defined USE_MOUSE_CAPTURE
397  if( !m_parentPanel->HasCapture() )
398  m_parentPanel->CaptureMouse();
399 #endif
400  }
401  else if( ( aEvent.MiddleDown() && m_settings.m_dragMiddle == MOUSE_DRAG_ACTION::ZOOM ) ||
402  ( aEvent.RightDown() && m_settings.m_dragRight == MOUSE_DRAG_ACTION::ZOOM ) )
403  {
404  m_dragStartPoint = VECTOR2D( aEvent.GetX(), aEvent.GetY() );
408 #if defined USE_MOUSE_CAPTURE
409  if( !m_parentPanel->HasCapture() )
410  m_parentPanel->CaptureMouse();
411 #endif
412  }
413 
414  if( aEvent.LeftUp() )
415  m_state = IDLE; // Stop autopanning when user release left mouse button
416 
417  break;
418 
419  case DRAG_ZOOMING:
420  case DRAG_PANNING:
421  if( aEvent.MiddleUp() || aEvent.LeftUp() || aEvent.RightUp() )
422  {
423  m_state = IDLE;
424 #if defined USE_MOUSE_CAPTURE
425  if( !m_settings.m_cursorCaptured && m_parentPanel->HasCapture() )
426  m_parentPanel->ReleaseMouse();
427 #endif
428  }
429 
430  break;
431  }
432 
433  aEvent.Skip();
434 }
435 
436 
437 void WX_VIEW_CONTROLS::onEnter( wxMouseEvent& aEvent )
438 {
439  // Avoid stealing focus from text controls
440  // This is particularly important for users using On-Screen-Keyboards
441  // They may move the mouse over the canvas to reach the keyboard
443  {
444  return;
445  }
446 
447 #if defined( _WIN32 )
448  // Win32 transmits mouse move and wheel events to all controls below the mouse regardless of focus
449  // Forcing the focus here will cause the EDA FRAMES to immediately become the top level active window
450  if( m_parentPanel->GetParent() != nullptr )
451  {
452  // this assumes the parent panel's parent is the eda window
453  if( GetForegroundWindow() == m_parentPanel->GetParent()->GetHWND() )
454  {
455  m_parentPanel->SetFocus();
456  }
457  }
458 #else
459  m_parentPanel->SetFocus();
460 #endif
461 }
462 
463 
464 void WX_VIEW_CONTROLS::onLeave( wxMouseEvent& aEvent )
465 {
466 #if !defined USE_MOUSE_CAPTURE
467  onMotion( aEvent );
468 #endif
469 }
470 
471 void WX_VIEW_CONTROLS::onCaptureLost( wxMouseEvent& aEvent )
472 {
473  // This method must be present to suppress the capture-lost assertion
474 }
475 
476 void WX_VIEW_CONTROLS::onTimer( wxTimerEvent& aEvent )
477 {
478  switch( m_state )
479  {
480  case AUTO_PANNING:
481  {
483  {
484  m_state = IDLE;
485  return;
486  }
487 
488  if( !m_parentPanel->HasFocus() )
489  break;
490 
491  double borderSize = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
493 
494  // When the mouse cursor is outside the area with no pan,
495  // m_panDirection is the dist to this area limit ( in pixels )
496  // It will be used also as pan value (the pan speed depends on this dist).
497  VECTOR2D dir( m_panDirection );
498 
499  // When the mouse cursor is outside the area with no pan, the pan value
500  // is accelerated depending on the dist between the area and the cursor
501  float accel = 0.5f + ( m_settings.m_autoPanAcceleration / 5.0f );
502 
503  // For a small mouse cursor dist to area, just use the distance.
504  // But for a dist > borderSize / 2, use an accelerated pan value
505 
506  if( dir.EuclideanNorm() >= borderSize ) // far from area limits
507  dir = dir.Resize( borderSize * accel );
508  else if( dir.EuclideanNorm() > borderSize / 2 ) // Near from area limits
509  dir = dir.Resize( borderSize );
510 
511  dir = m_view->ToWorld( dir, false );
512  m_view->SetCenter( m_view->GetCenter() + dir );
513 
514  refreshMouse();
515  }
516  break;
517 
518  case IDLE: // Just remove unnecessary warnings
519  case DRAG_PANNING:
520  case DRAG_ZOOMING:
521  break;
522  }
523 }
524 
525 
526 void WX_VIEW_CONTROLS::onScroll( wxScrollWinEvent& aEvent )
527 {
528  const double linePanDelta = 0.05;
529  const double pagePanDelta = 0.5;
530 
531  int type = aEvent.GetEventType();
532  int dir = aEvent.GetOrientation();
533 
534  if( type == wxEVT_SCROLLWIN_THUMBTRACK )
535  {
536  auto center = m_view->GetCenter();
537  const auto& boundary = m_view->GetBoundary();
538 
539  // Flip scroll direction in flipped view
540  const double xstart = ( m_view->IsMirroredX() ?
541  boundary.GetRight() : boundary.GetLeft() );
542  const double xdelta = ( m_view->IsMirroredX() ? -1 : 1 );
543 
544  if( dir == wxHORIZONTAL )
545  center.x = xstart + xdelta * ( aEvent.GetPosition() / m_scrollScale.x );
546  else
547  center.y = boundary.GetTop() + aEvent.GetPosition() / m_scrollScale.y;
548 
549  m_view->SetCenter( center );
550  }
551  else
552  {
553  double dist = 0;
554 
555  if( type == wxEVT_SCROLLWIN_PAGEUP )
556  dist = pagePanDelta;
557  else if( type == wxEVT_SCROLLWIN_PAGEDOWN )
558  dist = -pagePanDelta;
559  else if( type == wxEVT_SCROLLWIN_LINEUP )
560  dist = linePanDelta;
561  else if( type == wxEVT_SCROLLWIN_LINEDOWN )
562  dist = -linePanDelta;
563  else
564  {
565  wxASSERT( "Unhandled event type" );
566  return;
567  }
568 
569  VECTOR2D scroll = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) * dist;
570 
571  double scrollX = 0.0;
572  double scrollY = 0.0;
573 
574  if ( dir == wxHORIZONTAL )
575  scrollX = -scroll.x;
576  else
577  scrollY = -scroll.y;
578 
579  VECTOR2D delta( scrollX, scrollY );
580 
581  m_view->SetCenter( m_view->GetCenter() + delta );
582  }
583 
584  m_parentPanel->Refresh();
585 }
586 
587 
588 void WX_VIEW_CONTROLS::CaptureCursor( bool aEnabled )
589 {
590 #if defined USE_MOUSE_CAPTURE
591  if( aEnabled && !m_parentPanel->HasCapture() )
592  m_parentPanel->CaptureMouse();
593  else if( !aEnabled && m_parentPanel->HasCapture()
595  m_parentPanel->ReleaseMouse();
596 #endif
597  VIEW_CONTROLS::CaptureCursor( aEnabled );
598 }
599 
600 
602 {
604  {
605  m_state = IDLE;
606 #if defined USE_MOUSE_CAPTURE
607  if( !m_settings.m_cursorCaptured && m_parentPanel->HasCapture() )
608  m_parentPanel->ReleaseMouse();
609 #endif
610  }
611 }
612 
613 
614 VECTOR2D WX_VIEW_CONTROLS::GetMousePosition( bool aWorldCoordinates ) const
615 {
616  wxPoint msp = getMouseScreenPosition();
617  VECTOR2D screenPos( msp.x, msp.y );
618 
619  return aWorldCoordinates ? m_view->ToWorld( screenPos ) : screenPos;
620 }
621 
622 
624 {
625  GAL* gal = m_view->GetGAL();
626 
627  if( aEnableSnapping && gal->GetGridSnapping() )
628  {
629  return gal->GetGridPoint( m_cursorPos );
630  }
631  else
632  {
633  return m_cursorPos;
634  }
635 }
636 
637 
638 VECTOR2D WX_VIEW_CONTROLS::GetCursorPosition( bool aEnableSnapping ) const
639 {
641  {
643  }
644  else
645  {
646  return GetRawCursorPosition( aEnableSnapping );
647  }
648 }
649 
650 
651 void WX_VIEW_CONTROLS::SetCursorPosition( const VECTOR2D& aPosition, bool aWarpView,
652  bool aTriggeredByArrows, long aArrowCommand )
653 {
654  m_updateCursor = false;
655 
656  if( aTriggeredByArrows )
657  {
660  m_settings.m_lastKeyboardCursorCommand = aArrowCommand;
661  m_cursorWarped = false;
662  }
663  else
664  {
668  m_cursorWarped = true;
669  }
670 
671  WarpCursor( aPosition, true, aWarpView );
672  m_cursorPos = aPosition;
673 }
674 
675 
676 void WX_VIEW_CONTROLS::SetCrossHairCursorPosition( const VECTOR2D& aPosition, bool aWarpView = true )
677 {
678  m_updateCursor = false;
679 
680  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
681  BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
682  VECTOR2D screenPos = m_view->ToScreen( aPosition );
683 
684  if( aWarpView && !screen.Contains( screenPos ) )
685  m_view->SetCenter( aPosition );
686 
687  m_cursorPos = aPosition;
688 }
689 
690 
691 void WX_VIEW_CONTROLS::WarpCursor( const VECTOR2D& aPosition, bool aWorldCoordinates,
692  bool aWarpView )
693 {
694  if( aWorldCoordinates )
695  {
696  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
697  BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
698  VECTOR2D screenPos = m_view->ToScreen( aPosition );
699 
700  if( !screen.Contains( screenPos ) )
701  {
702  if( aWarpView )
703  {
704  m_view->SetCenter( aPosition );
705  m_parentPanel->WarpPointer( screenSize.x / 2, screenSize.y / 2 );
706  }
707  }
708  else
709  {
710  m_parentPanel->WarpPointer( screenPos.x, screenPos.y );
711  }
712  }
713  else
714  {
715  m_parentPanel->WarpPointer( aPosition.x, aPosition.y );
716  }
717 
718  refreshMouse();
719 }
720 
721 
723 {
724  const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
725  VECTOR2I screenCenter( screenSize / 2 );
726 
727  if( GetMousePosition( false ) != screenCenter )
728  {
730  m_parentPanel->WarpPointer( KiROUND( screenSize.x / 2 ), KiROUND( screenSize.y / 2 ) );
731  }
732 }
733 
734 
735 bool WX_VIEW_CONTROLS::handleAutoPanning( const wxMouseEvent& aEvent )
736 {
737  VECTOR2I p( aEvent.GetX(), aEvent.GetY() );
739 
741  {
742  // last cursor move event came from keyboard cursor control. If auto-panning is enabled and
743  // the next position is inside the autopan zone, check if it really came from a mouse event, otherwise
744  // disable autopan temporarily. Also temporaly disable autopan if the cursor is in the autopan zone
745  // because the application warped the cursor.
746 
747  m_cursorWarped = false;
748  return true;
749  }
750 
751  m_cursorWarped = false;
752 
753  // Compute areas where autopanning is active
754  int borderStart = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
756  borderStart = std::max( borderStart, 2 );
757  int borderEndX = m_view->GetScreenPixelSize().x - borderStart;
758  int borderEndY = m_view->GetScreenPixelSize().y - borderStart;
759 
760  if( p.x < borderStart )
761  m_panDirection.x = -( borderStart - p.x );
762  else if( p.x > borderEndX )
763  m_panDirection.x = ( p.x - borderEndX );
764  else
765  m_panDirection.x = 0;
766 
767  if( p.y < borderStart )
768  m_panDirection.y = -( borderStart - p.y );
769  else if( p.y > borderEndY )
770  m_panDirection.y = ( p.y - borderEndY );
771  else
772  m_panDirection.y = 0;
773 
774  bool borderHit = ( m_panDirection.x != 0 || m_panDirection.y != 0 );
775 
776  switch( m_state )
777  {
778  case AUTO_PANNING:
779  if( !borderHit )
780  {
781  m_panTimer.Stop();
782  m_state = IDLE;
783 
784  return false;
785  }
786 
787  return true;
788  break;
789 
790  case IDLE:
791  if( borderHit )
792  {
794  m_panTimer.Start( (int) ( 250.0 / 60.0 ) );
795 
796  return true;
797  }
798 
799  return false;
800  break;
801 
802  case DRAG_PANNING:
803  case DRAG_ZOOMING:
804  return false;
805  }
806 
807  wxASSERT_MSG( false, wxT( "This line should never be reached" ) );
808  return false; // Should not be reached, just avoid the compiler warnings..
809 }
810 
811 
813 {
815  {
816  bool warp = false;
817  wxSize parentSize = m_parentPanel->GetClientSize();
818 
819  if( x < 0 )
820  {
821  x = 0;
822  warp = true;
823  }
824  else if( x >= parentSize.x )
825  {
826  x = parentSize.x - 1;
827  warp = true;
828  }
829 
830  if( y < 0 )
831  {
832  y = 0;
833  warp = true;
834  }
835  else if( y >= parentSize.y )
836  {
837  y = parentSize.y - 1;
838  warp = true;
839  }
840 
841  if( warp )
842  m_parentPanel->WarpPointer( x, y );
843  }
844 }
845 
846 
848 {
849  // Notify tools that the cursor position has changed in the world coordinates
850  wxMouseEvent moveEvent( EVT_REFRESH_MOUSE );
851  wxPoint msp = getMouseScreenPosition();
852  moveEvent.SetX( msp.x );
853  moveEvent.SetY( msp.y );
854 
855  // Set the modifiers state
856  moveEvent.SetControlDown( wxGetKeyState( WXK_CONTROL ) );
857  moveEvent.SetShiftDown( wxGetKeyState( WXK_SHIFT ) );
858  moveEvent.SetAltDown( wxGetKeyState( WXK_ALT ) );
859 
860  m_cursorPos = m_view->ToWorld( VECTOR2D( msp.x, msp.y ) );
861  wxPostEvent( m_parentPanel, moveEvent );
862 }
863 
864 
866 {
867  wxPoint msp = wxGetMousePosition();
868  m_parentPanel->ScreenToClient( &msp.x, &msp.y );
869  return msp;
870 }
871 
872 
874 {
875  const BOX2D viewport = m_view->GetViewport();
876  const BOX2D& boundary = m_view->GetBoundary();
877 
878  m_scrollScale.x = 2e3 / viewport.GetWidth(); // TODO it does not have to be updated so often
879  m_scrollScale.y = 2e3 / viewport.GetHeight();
880  VECTOR2I newScroll( ( viewport.Centre().x - boundary.GetLeft() ) * m_scrollScale.x,
881  ( viewport.Centre().y - boundary.GetTop() ) * m_scrollScale.y );
882 
883  // We add the width of the scroll bar thumb to the range because the scroll range is given by
884  // the full bar while the position is given by the left/top position of the thumb
885  VECTOR2I newRange( m_scrollScale.x * boundary.GetWidth() + m_parentPanel->GetScrollThumb( wxSB_HORIZONTAL ),
886  m_scrollScale.y * boundary.GetHeight() + m_parentPanel->GetScrollThumb( wxSB_VERTICAL ) );
887 
888  // Flip scroll direction in flipped view
889  if( m_view->IsMirroredX() )
890  newScroll.x = ( boundary.GetRight() - viewport.Centre().x ) * m_scrollScale.x;
891 
892  // Adjust scrollbars only if it is needed. Otherwise there are cases when canvas is continuously
893  // refreshed (Windows)
894  if( m_scrollPos != newScroll || newRange.x != m_parentPanel->GetScrollRange( wxSB_HORIZONTAL )
895  || newRange.y != m_parentPanel->GetScrollRange( wxSB_VERTICAL ) )
896  {
897  m_parentPanel->SetScrollbars( 1, 1, newRange.x, newRange.y, newScroll.x, newScroll.y, true );
898  m_scrollPos = newScroll;
899 
900 #if !defined( __APPLE__ ) && !defined( WIN32 )
901  // Trigger a mouse refresh to get the canvas update in GTK (re-draws the scrollbars).
902  // Note that this causes an infinite loop on OSX and Windows (in certain cases) as it generates a paint event.
903  refreshMouse();
904 #endif
905  }
906 }
907 
908 void WX_VIEW_CONTROLS::ForceCursorPosition( bool aEnabled, const VECTOR2D& aPosition )
909 {
911  m_settings.m_forcedPosition = aPosition;
912 }
VECTOR2D m_lookStartPoint
Current direction of panning (only autopanning mode).
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:513
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.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
const BOX2D & GetBoundary() const
Definition: view.h:292
Panning with mouse button pressed.
static std::unique_ptr< ZOOM_CONTROLLER > GetZoomControllerForPlatform()
static const wxEventType EVT_REFRESH_MOUSE
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:450
coord_type GetTop() const
Definition: box2.h:204
MOUSE_DRAG_ACTION drag_left
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition: view.h:333
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
VECTOR2D m_forcedPosition
Is the forced cursor position enabled.
Definition: view_controls.h:55
coord_type GetRight() const
Definition: box2.h:199
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
VIEW * m_view
< Pointer to controlled VIEW.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
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.
bool IsInputControlFocused()
Checks if a input control has focus.
Definition: ui_common.cpp:137
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
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
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:579
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
#define NULL
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
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:197
int m_scrollModifierZoom
What modifier key to enable horizontal pan with the (vertical) scroll wheel.
bool Contains(const Vec &aPoint) const
Function Contains.
Definition: box2.h:151
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:1149
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
Multiplier for manual scale ssetting.
wxLogTrace helper definitions.
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:469
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:237
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:553
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).
wxScrolledCanvas * m_parentPanel
Store information about point where dragging has started.
void onTimer(wxTimerEvent &WXUNUSED(aEvent))
const int scale
see class PGM_BASE
Vec Centre() const
Definition: box2.h:79
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.
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:68
VECTOR2I m_scrollPos
The zoom scale when a drag zoom started.
coord_type GetHeight() const
Definition: box2.h:198
WX_VIEW_CONTROLS(VIEW *aView, wxScrolledCanvas *aParentPanel)
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.
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:203
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
double GetScale() const
Definition: view.h:263
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)
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.