KiCad PCB EDA Suite
tool_event.h
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) 2013 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef __TOOL_EVENT_H
26 #define __TOOL_EVENT_H
27 
28 #include <cstdio>
29 #include <deque>
30 #include <iterator>
31 
32 #include <math/vector2d.h>
33 #include <core/optional.h>
34 
35 #ifdef WX_COMPATIBILITY
36 #include <wx/debug.h>
37 #else
38 #include <cassert>
39 #endif
40 
41 
42 class TOOL_ACTION;
43 class TOOL_MANAGER;
44 class TOOL_BASE;
45 
51 {
52  TC_NONE = 0x00,
53  TC_MOUSE = 0x01,
54  TC_KEYBOARD = 0x02,
55  TC_COMMAND = 0x04,
56  TC_MESSAGE = 0x08,
57  TC_VIEW = 0x10,
58  TC_ANY = 0xffffffff
59 };
60 
62 {
63  // UI input events
64  TA_NONE = 0x0000,
65  TA_MOUSE_CLICK = 0x0001,
67  TA_MOUSE_UP = 0x0004,
68  TA_MOUSE_DOWN = 0x0008,
69  TA_MOUSE_DRAG = 0x0010,
70  TA_MOUSE_MOTION = 0x0020,
71  TA_MOUSE_WHEEL = 0x0040,
72  TA_MOUSE = 0x007f,
73 
74  TA_KEY_PRESSED = 0x0080,
76 
77  // View related events
78  TA_VIEW_REFRESH = 0x0100,
79  TA_VIEW_ZOOM = 0x0200,
80  TA_VIEW_PAN = 0x0400,
81  TA_VIEW_DIRTY = 0x0800,
82  TA_VIEW = 0x0f00,
83 
84  TA_CHANGE_LAYER = 0x1000,
85 
86  // Tool cancel event. Issued automagically when the user hits escape or selects End Tool from
87  // the context menu.
88  TA_CANCEL_TOOL = 0x2000,
89 
90  // Context menu update. Issued whenever context menu is open and the user hovers the mouse
91  // over one of choices. Used in dynamic highligting in disambiguation menu
93 
94  // Context menu choice. Sent if the user picked something from the context menu or
95  // closed it without selecting anything.
97 
98  // Context menu is closed, no matter whether anything has been chosen or not.
100 
102 
103  // This event is sent *before* undo/redo command is performed.
104  TA_UNDO_REDO_PRE = 0x20000,
105 
106  // This event is sent *after* undo/redo command is performed.
107  TA_UNDO_REDO_POST = 0x40000,
108 
109  // Tool action (allows one to control tools).
110  TA_ACTION = 0x80000,
111 
112  // Tool activation event.
113  TA_ACTIVATE = 0x100000,
114 
115  // Model has changed (partial update).
116  TA_MODEL_CHANGE = 0x200000,
117 
118  TA_ANY = 0xffffffff
119 };
120 
122 {
123  BUT_NONE = 0x0,
124  BUT_LEFT = 0x1,
125  BUT_RIGHT = 0x2,
126  BUT_MIDDLE = 0x4,
128  BUT_ANY = 0xffffffff
129 };
130 
132 {
133  MD_SHIFT = 0x1000,
134  MD_CTRL = 0x2000,
135  MD_ALT = 0x4000,
137 };
138 
141 {
145 };
146 
149 {
150  AF_NONE = 0,
153 };
154 
157 {
158  CMENU_BUTTON = 0, // On the right button
159  CMENU_NOW, // Right now (after TOOL_INTERACTIVE::SetContextMenu)
160  CMENU_OFF // Never
161 };
162 
169 {
170 public:
177  const std::string Format() const;
178 
180  TOOL_ACTION_SCOPE aScope = AS_GLOBAL, void* aParameter = nullptr ) :
181  m_category( aCategory ),
182  m_actions( aAction ),
183  m_scope( aScope ),
184  m_mouseButtons( 0 ),
185  m_keyCode( 0 ),
186  m_modifiers( 0 ),
187  m_param( aParameter ),
188  m_firstResponder( nullptr )
189  {
190  init();
191  }
192 
193  TOOL_EVENT( TOOL_EVENT_CATEGORY aCategory, TOOL_ACTIONS aAction, int aExtraParam,
194  TOOL_ACTION_SCOPE aScope = AS_GLOBAL, void* aParameter = nullptr ) :
195  m_category( aCategory ),
196  m_actions( aAction ),
197  m_scope( aScope ),
198  m_mouseButtons( 0 ),
199  m_keyCode( 0 ),
200  m_modifiers( 0 ),
201  m_param( aParameter ),
202  m_firstResponder( nullptr )
203  {
204  if( aCategory == TC_MOUSE )
205  {
206  setMouseButtons( aExtraParam & BUT_BUTTON_MASK );
207  }
208  else if( aCategory == TC_KEYBOARD )
209  {
210  m_keyCode = aExtraParam & ~MD_MODIFIER_MASK; // Filter out modifiers
211  }
212  else if( aCategory == TC_COMMAND )
213  {
214  m_commandId = aExtraParam;
215  }
216 
217  if( aCategory & ( TC_MOUSE | TC_KEYBOARD ) )
218  {
219  m_modifiers = aExtraParam & MD_MODIFIER_MASK;
220  }
221 
222  init();
223  }
224 
226  const std::string& aExtraParam, TOOL_ACTION_SCOPE aScope = AS_GLOBAL,
227  void* aParameter = nullptr ) :
228  m_category( aCategory ),
229  m_actions( aAction ),
230  m_scope( aScope ),
231  m_mouseButtons( 0 ),
232  m_keyCode( 0 ),
233  m_modifiers( 0 ),
234  m_param( aParameter ),
235  m_firstResponder( nullptr )
236  {
237  if( aCategory == TC_COMMAND || aCategory == TC_MESSAGE )
238  m_commandStr = aExtraParam;
239 
240  init();
241  }
242 
245 
247  TOOL_ACTIONS Action() const { return m_actions; }
248 
252  bool PassEvent() const { return m_passEvent; }
253  void SetPassEvent() { m_passEvent = true; }
254 
257  bool HasPosition() const { return m_hasPosition; }
258  void SetHasPosition( bool aHasPosition ) { m_hasPosition = aHasPosition; }
259 
261  void SetFirstResponder( TOOL_BASE* aTool ) { m_firstResponder = aTool; }
262 
265  const VECTOR2D Delta() const
266  {
268  }
269 
271  const VECTOR2D Position() const
272  {
274  }
275 
277  const VECTOR2D DragOrigin() const
278  {
280  }
281 
283  int Buttons() const
284  {
285  assert( m_category == TC_MOUSE ); // this should be used only with mouse events
286  return m_mouseButtons;
287  }
288 
289  bool IsClick( int aButtonMask = BUT_ANY ) const;
290 
291  bool IsDblClick( int aButtonMask = BUT_ANY ) const;
292 
293  bool IsDrag( int aButtonMask = BUT_ANY ) const
294  {
295  return m_actions == TA_MOUSE_DRAG && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
296  }
297 
298  bool IsMouseUp( int aButtonMask = BUT_ANY ) const
299  {
300  return m_actions == TA_MOUSE_UP && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
301  }
302 
303  bool IsMotion() const
304  {
305  return m_actions == TA_MOUSE_MOTION;
306  }
307 
308  bool IsMouseAction() const
309  {
310  return ( m_actions & TA_MOUSE );
311  }
312 
313  bool IsCancel() const
314  {
315  return m_actions == TA_CANCEL_TOOL;
316  }
317 
318  bool IsActivate() const
319  {
320  return m_actions == TA_ACTIVATE;
321  }
322 
323  bool IsUndoRedo() const
324  {
326  }
327 
328  bool IsChoiceMenu() const
329  {
330  return m_actions & TA_CHOICE_MENU;
331  }
332 
334  int Modifier( int aMask = MD_MODIFIER_MASK ) const
335  {
336  return m_modifiers & aMask;
337  }
338 
339  int KeyCode() const
340  {
341  return m_keyCode;
342  }
343 
344  bool IsKeyPressed() const
345  {
346  return m_actions == TA_KEY_PRESSED;
347  }
348 
356  bool Matches( const TOOL_EVENT& aEvent ) const
357  {
358  if( !( m_category & aEvent.m_category ) )
359  return false;
360 
362  {
363  if( (bool) m_commandStr && (bool) aEvent.m_commandStr )
364  return *m_commandStr == *aEvent.m_commandStr;
365 
366  if( (bool) m_commandId && (bool) aEvent.m_commandId )
367  return *m_commandId == *aEvent.m_commandId;
368  }
369 
370  // BUGFIX: TA_ANY should match EVERYTHING, even TA_NONE (for TC_MESSAGE)
371  if( m_actions == TA_ANY && aEvent.m_actions == TA_NONE && aEvent.m_category == TC_MESSAGE )
372  return true;
373 
374  // BUGFIX: This check must happen after the TC_COMMAND check because otherwise events of
375  // the form { TC_COMMAND, TA_NONE } will be incorrectly skipped
376  if( !( m_actions & aEvent.m_actions ) )
377  return false;
378 
379  return true;
380  }
381 
388  bool IsAction( const TOOL_ACTION* aAction ) const;
389 
396  bool IsCancelInteractive();
397 
403  bool IsSelectionEvent();
404 
411  bool IsPointEditor();
412 
419  bool IsMoveTool();
420 
426  template<typename T>
427  inline T Parameter() const
428  {
429  // Exhibit #798 on why I love to hate C++
430  // - reinterpret_cast needs to be used for pointers
431  // - static_cast must be used for enums
432  // - templates can't usefully distinguish between pointer and non-pointer types
433  // Fortunately good old C's cast can be a reinterpret_cast or a static_cast, and
434  // C99 gave us intptr_t which is guaranteed to be round-trippable with a pointer.
435  return (T) reinterpret_cast<intptr_t>( m_param );
436  }
437 
444  template<typename T>
445  void SetParameter(T aParam)
446  {
447  m_param = (void*) aParam;
448  }
449 
451  {
452  return m_commandId;
453  }
454 
456  {
457  return m_commandStr;
458  }
459 
460  void SetMousePosition( const VECTOR2D& aP )
461  {
462  m_mousePos = aP;
463  }
464 
465 private:
466  friend class TOOL_DISPATCHER;
467 
468  void init();
469 
470  void setMouseDragOrigin( const VECTOR2D& aP )
471  {
472  m_mouseDragOrigin = aP;
473  }
474 
475  void setMouseDelta( const VECTOR2D& aP )
476  {
477  m_mouseDelta = aP;
478  }
479 
480  void setMouseButtons( int aButtons )
481  {
482  assert( ( aButtons & ~BUT_BUTTON_MASK ) == 0 );
483  m_mouseButtons = aButtons;
484  }
485 
486  void setModifiers( int aMods )
487  {
488  assert( ( aMods & ~MD_MODIFIER_MASK ) == 0 );
489  m_modifiers = aMods;
490  }
491 
502  {
503  #ifdef WX_COMPATIBILITY
504  wxCHECK_MSG( HasPosition(), VECTOR2D(),
505  "Attempted to get position from non-position event" );
506  #else
507  assert( HasPosition() );
508  #endif
509 
510  return aPos;
511  }
512 
518 
522 
525 
528 
531 
534 
537 
539  void* m_param;
540 
543 
546 };
547 
549 
557 {
558 public:
560  typedef std::deque<TOOL_EVENT>::iterator iterator;
561  typedef std::deque<TOOL_EVENT>::const_iterator const_iterator;
562 
565  {}
566 
568  TOOL_EVENT_LIST( const TOOL_EVENT& aSingleEvent )
569  {
570  m_events.push_back( aSingleEvent );
571  }
572 
579  const std::string Format() const;
580 
582  {
583  for( const TOOL_EVENT& event : m_events )
584  {
585  if( event.Matches( aEvent ) )
586  return event;
587  }
588 
589  return OPT<const TOOL_EVENT&>();
590  }
591 
597  void Add( const TOOL_EVENT& aEvent )
598  {
599  m_events.push_back( aEvent );
600  }
601 
603  {
604  return m_events.begin();
605  }
606 
608  {
609  return m_events.end();
610  }
611 
613  {
614  return m_events.begin();
615  }
616 
618  {
619  return m_events.end();
620  }
621 
622  int size() const
623  {
624  return m_events.size();
625  }
626 
627  void clear()
628  {
629  m_events.clear();
630  }
631 
633  {
634  m_events.clear();
635 
636  for( const TOOL_EVENT& event : aEventList.m_events )
637  m_events.push_back( event );
638 
639  return *this;
640  }
641 
643  {
644  m_events.clear();
645  m_events.push_back( aEvent );
646  return *this;
647  }
648 
650  {
651  Add( aEvent );
652  return *this;
653  }
654 
656  {
657  std::copy( aEvent.m_events.begin(), aEvent.m_events.end(), std::back_inserter( m_events ) );
658  return *this;
659  }
660 
661 private:
662  std::deque<TOOL_EVENT> m_events;
663 };
664 
665 
666 inline const TOOL_EVENT_LIST operator||( const TOOL_EVENT& aEventA, const TOOL_EVENT& aEventB )
667 {
668  TOOL_EVENT_LIST l;
669 
670  l.Add( aEventA );
671  l.Add( aEventB );
672 
673  return l;
674 }
675 
676 
677 inline const TOOL_EVENT_LIST operator||( const TOOL_EVENT& aEvent,
678  const TOOL_EVENT_LIST& aEventList )
679 {
680  TOOL_EVENT_LIST l( aEventList );
681 
682  l.Add( aEvent );
683  return l;
684 }
685 
686 
687 #endif
int Buttons() const
Returns information about mouse buttons state.
Definition: tool_event.h:283
bool IsCancel() const
Definition: tool_event.h:313
void setMouseButtons(int aButtons)
Definition: tool_event.h:480
bool m_hasPosition
Definition: tool_event.h:517
OPT< int > GetCommandId() const
Definition: tool_event.h:450
void setModifiers(int aMods)
Definition: tool_event.h:486
TOOL_EVENT(TOOL_EVENT_CATEGORY aCategory=TC_NONE, TOOL_ACTIONS aAction=TA_NONE, TOOL_ACTION_SCOPE aScope=AS_GLOBAL, void *aParameter=nullptr)
Definition: tool_event.h:179
void setMouseDelta(const VECTOR2D &aP)
Definition: tool_event.h:475
TOOL_EVENT value_type
Definition: tool_event.h:559
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:69
iterator begin()
Definition: tool_event.h:602
void init()
Definition: tool_event.cpp:54
TOOL_EVENT(TOOL_EVENT_CATEGORY aCategory, TOOL_ACTIONS aAction, int aExtraParam, TOOL_ACTION_SCOPE aScope=AS_GLOBAL, void *aParameter=nullptr)
Definition: tool_event.h:193
OPT< const TOOL_EVENT & > Matches(const TOOL_EVENT &aEvent) const
Definition: tool_event.h:581
TOOL_ACTIONS Action() const
Returns more specific information about the type of an event.
Definition: tool_event.h:247
int m_modifiers
State of key modifierts (Ctrl/Alt/etc.)
Definition: tool_event.h:536
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:174
bool IsMouseAction() const
Definition: tool_event.h:308
TOOL_ACTIONS m_actions
Definition: tool_event.h:514
TOOL_ACTIONS
Definition: tool_event.h:61
bool IsPointEditor()
Function IsPointEditor.
Definition: tool_event.cpp:204
int m_mouseButtons
State of mouse buttons
Definition: tool_event.h:530
Action activates a tool
Definition: tool_event.h:152
bool IsMotion() const
Definition: tool_event.h:303
bool IsKeyPressed() const
Definition: tool_event.h:344
TOOL_EVENT_CATEGORY Category() const
Returns the category (eg. mouse/keyboard/action) of an event..
Definition: tool_event.h:244
VECTOR2D returnCheckedPosition(const VECTOR2D &aPos) const
Ensure that the event is a type that has a position before returning a position, otherwise return a n...
Definition: tool_event.h:501
OPT< std::string > m_commandStr
Definition: tool_event.h:545
VECTOR2D m_mouseDelta
Difference between mouse cursor position and the point where dragging event has started
Definition: tool_event.h:521
TOOL_EVENT_CATEGORY
Internal (GUI-independent) event definitions.
Definition: tool_event.h:50
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_event.h:140
int m_keyCode
Stores code of pressed/released key
Definition: tool_event.h:533
TOOL_EVENT_LIST & operator=(const TOOL_EVENT &aEvent)
Definition: tool_event.h:642
VECTOR2D m_mouseDragOrigin
Point where dragging has started
Definition: tool_event.h:527
TOOL_EVENT_LIST & operator||(const TOOL_EVENT_LIST &aEvent)
Definition: tool_event.h:655
OPT< int > m_commandId
Definition: tool_event.h:544
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_event.h:148
void SetMousePosition(const VECTOR2D &aP)
Definition: tool_event.h:460
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:63
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:662
TOOL_EVENT_LIST()
Default constructor. Creates an empty list.
Definition: tool_event.h:564
TOOL_EVENT(TOOL_EVENT_CATEGORY aCategory, TOOL_ACTIONS aAction, const std::string &aExtraParam, TOOL_ACTION_SCOPE aScope=AS_GLOBAL, void *aParameter=nullptr)
Definition: tool_event.h:225
Class TOOL_MANAGER.
Definition: tool_manager.h:50
void SetParameter(T aParam)
Function SetParameter() Sets a non-standard parameter assigned to the event.
Definition: tool_event.h:445
bool m_passEvent
Definition: tool_event.h:516
Class TOOL_EVENT_LIST.
Definition: tool_event.h:556
bool IsMoveTool()
Function IsMoveTool.
Definition: tool_event.cpp:213
bool IsDrag(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:293
Action belongs to a particular tool (i.e. a part of a pop-up menu)
Definition: tool_event.h:143
const VECTOR2D Delta() const
Returns information about difference between current mouse cursor position and the place where draggi...
Definition: tool_event.h:265
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
TOOL_ACTION_SCOPE m_scope
Definition: tool_event.h:515
bool IsDblClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:180
TOOL_EVENT_LIST & operator||(const TOOL_EVENT &aEvent)
Definition: tool_event.h:649
CONTEXT_MENU_TRIGGER
Defines when a context menu is opened.
Definition: tool_event.h:156
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:427
Class TOOL_EVENT.
Definition: tool_event.h:168
TOOL_EVENT_LIST(const TOOL_EVENT &aSingleEvent)
Constructor for a list containing only one TOOL_EVENT.
Definition: tool_event.h:568
void SetPassEvent()
Definition: tool_event.h:253
const VECTOR2D DragOrigin() const
Returns the point where dragging has started.
Definition: tool_event.h:277
bool Matches(const TOOL_EVENT &aEvent) const
Function Matches() Tests whether two events match in terms of category & action or command.
Definition: tool_event.h:356
std::deque< TOOL_EVENT >::const_iterator const_iterator
Definition: tool_event.h:561
All active tools
Definition: tool_event.h:144
bool IsMouseUp(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:298
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:163
bool IsCancelInteractive()
Function IsCancelInteractive()
Definition: tool_event.cpp:186
bool IsSelectionEvent()
Function IsSelectionEvent()
Definition: tool_event.cpp:196
Class TOOL_DISPATCHER.
int Modifier(int aMask=MD_MODIFIER_MASK) const
Returns information about key modifiers state (Ctrl, Alt, etc.)
Definition: tool_event.h:334
TOOL_MOUSE_BUTTONS
Definition: tool_event.h:121
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:455
const_iterator cend() const
Definition: tool_event.h:617
bool IsActivate() const
Definition: tool_event.h:318
bool IsUndoRedo() const
Definition: tool_event.h:323
bool PassEvent() const
These give a tool a method of informing the TOOL_MANAGER that a particular event should be passed on ...
Definition: tool_event.h:252
void Add(const TOOL_EVENT &aEvent)
Function Add() Adds a tool event to the list.
Definition: tool_event.h:597
TOOL_BASE * m_firstResponder
The first tool to receive the event
Definition: tool_event.h:542
const_iterator cbegin() const
Definition: tool_event.h:612
int size() const
Definition: tool_event.h:622
void SetFirstResponder(TOOL_BASE *aTool)
Definition: tool_event.h:261
Class TOOL_BASE.
Definition: tool_base.h:67
Class TOOL_ACTION.
Definition: tool_action.h:46
TOOL_EVENT_LIST & operator=(const TOOL_EVENT_LIST &aEventList)
Definition: tool_event.h:632
bool IsChoiceMenu() const
Definition: tool_event.h:328
void * m_param
Generic parameter used for passing non-standard data.
Definition: tool_event.h:539
boost::optional< T > OPT
Definition: optional.h:7
TOOL_MODIFIERS
Definition: tool_event.h:131
TOOL_BASE * FirstResponder() const
Definition: tool_event.h:260
bool HasPosition() const
Returns if it this event has a valid position (true for mouse events and context-menu or hotkey-based...
Definition: tool_event.h:257
std::deque< TOOL_EVENT >::iterator iterator
Definition: tool_event.h:560
void setMouseDragOrigin(const VECTOR2D &aP)
Definition: tool_event.h:470
int KeyCode() const
Definition: tool_event.h:339
iterator end()
Definition: tool_event.h:607
VECTOR2D m_mousePos
Current mouse cursor position
Definition: tool_event.h:524
const TOOL_EVENT_LIST operator||(const TOOL_EVENT &aEventA, const TOOL_EVENT &aEventB)
Definition: tool_event.h:666
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:513
const VECTOR2D Position() const
Returns mouse cursor position in world coordinates.
Definition: tool_event.h:271
void SetHasPosition(bool aHasPosition)
Definition: tool_event.h:258
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:548