2013-08-06 08:30:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
2021-01-25 12:42:36 +00:00
|
|
|
* Copyright (C) 2013-2021 KiCad Developers, see CHANGELOG.txt for contributors.
|
2013-08-06 08:30:09 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2021-10-05 16:57:25 +00:00
|
|
|
#include <ignore.h>
|
2019-05-14 12:39:34 +00:00
|
|
|
#include <macros.h>
|
2018-04-13 13:58:25 +00:00
|
|
|
#include <trace_helpers.h>
|
2013-08-02 14:46:53 +00:00
|
|
|
|
|
|
|
#include <tool/tool_manager.h>
|
2020-07-27 22:42:23 +00:00
|
|
|
#include <tool/tools_holder.h>
|
2013-08-02 14:46:53 +00:00
|
|
|
#include <tool/tool_dispatcher.h>
|
2017-02-21 12:42:08 +00:00
|
|
|
#include <tool/actions.h>
|
2020-07-27 22:42:23 +00:00
|
|
|
#include <tool/action_manager.h>
|
2020-03-24 01:01:23 +00:00
|
|
|
#include <tool/action_menu.h>
|
2013-08-02 14:46:53 +00:00
|
|
|
#include <view/view.h>
|
2013-09-24 14:12:02 +00:00
|
|
|
#include <view/wx_view_controls.h>
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2014-04-04 09:56:04 +00:00
|
|
|
#include <class_draw_panel_gal.h>
|
2019-06-08 22:14:57 +00:00
|
|
|
#include <eda_draw_frame.h>
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2020-11-18 01:21:04 +00:00
|
|
|
#include <core/arraydim.h>
|
2017-11-01 11:14:16 +00:00
|
|
|
#include <core/optional.h>
|
2021-06-07 22:20:47 +00:00
|
|
|
#include <wx/log.h>
|
2020-05-30 11:31:19 +00:00
|
|
|
#include <wx/stc/stc.h>
|
2021-09-24 17:14:08 +00:00
|
|
|
#include <wx/settings.h>
|
2020-10-15 04:38:32 +00:00
|
|
|
#include <kiplatform/app.h>
|
2021-06-07 22:20:47 +00:00
|
|
|
#include <kiplatform/ui.h>
|
2021-05-01 07:50:29 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Stores information about a mouse button state
|
2013-10-14 18:40:36 +00:00
|
|
|
struct TOOL_DISPATCHER::BUTTON_STATE
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
BUTTON_STATE( TOOL_MOUSE_BUTTONS aButton, const wxEventType& aDownEvent,
|
2013-11-28 14:19:50 +00:00
|
|
|
const wxEventType& aUpEvent, const wxEventType& aDblClickEvent ) :
|
2015-03-11 16:04:20 +00:00
|
|
|
dragging( false ),
|
|
|
|
pressed( false ),
|
2013-10-14 11:43:57 +00:00
|
|
|
button( aButton ),
|
|
|
|
downEvent( aDownEvent ),
|
2013-11-28 14:19:50 +00:00
|
|
|
upEvent( aUpEvent ),
|
|
|
|
dblClickEvent( aDblClickEvent )
|
2013-10-14 11:43:57 +00:00
|
|
|
{};
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Flag indicating that dragging is active for the given button.
|
2013-10-14 11:43:57 +00:00
|
|
|
bool dragging;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Flag indicating that the given button is pressed.
|
2013-10-14 11:43:57 +00:00
|
|
|
bool pressed;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Point where dragging has started (in world coordinates).
|
2013-10-14 11:43:57 +00:00
|
|
|
VECTOR2D dragOrigin;
|
|
|
|
|
2021-10-06 18:50:56 +00:00
|
|
|
///< Point where dragging has started (in screen coordinates).
|
|
|
|
VECTOR2D dragOriginScreen;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Point where click event has occurred.
|
2013-10-14 11:43:57 +00:00
|
|
|
VECTOR2D downPosition;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Determines the mouse button for which information are stored.
|
2013-10-14 18:40:36 +00:00
|
|
|
TOOL_MOUSE_BUTTONS button;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< The type of wxEvent that determines mouse button press.
|
2013-10-14 11:43:57 +00:00
|
|
|
wxEventType downEvent;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< The type of wxEvent that determines mouse button release.
|
2013-10-14 11:43:57 +00:00
|
|
|
wxEventType upEvent;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< The type of wxEvent that determines mouse button double click.
|
2013-11-28 14:19:50 +00:00
|
|
|
wxEventType dblClickEvent;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Time stamp for the last mouse button press event.
|
2013-10-14 11:43:57 +00:00
|
|
|
wxLongLong downTimestamp;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Restores initial state.
|
2013-10-14 11:43:57 +00:00
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
dragging = false;
|
|
|
|
pressed = false;
|
|
|
|
}
|
2014-07-15 15:34:53 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Checks the current state of the button.
|
2014-07-15 15:34:53 +00:00
|
|
|
bool GetState() const
|
|
|
|
{
|
|
|
|
wxMouseState mouseState = wxGetMouseState();
|
|
|
|
|
|
|
|
switch( button )
|
|
|
|
{
|
|
|
|
case BUT_LEFT:
|
|
|
|
return mouseState.LeftIsDown();
|
|
|
|
|
|
|
|
case BUT_MIDDLE:
|
|
|
|
return mouseState.MiddleIsDown();
|
|
|
|
|
|
|
|
case BUT_RIGHT:
|
|
|
|
return mouseState.RightIsDown();
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-02 14:46:53 +00:00
|
|
|
};
|
|
|
|
|
2013-08-06 08:30:09 +00:00
|
|
|
|
2021-03-27 20:02:34 +00:00
|
|
|
TOOL_DISPATCHER::TOOL_DISPATCHER( TOOL_MANAGER* aToolMgr ) :
|
|
|
|
m_toolMgr( aToolMgr )
|
2013-08-06 08:30:09 +00:00
|
|
|
{
|
2021-09-24 17:14:08 +00:00
|
|
|
m_sysDragMinX = wxSystemSettings::GetMetric( wxSYS_DRAG_X );
|
|
|
|
m_sysDragMinY = wxSystemSettings::GetMetric( wxSYS_DRAG_Y );
|
|
|
|
|
|
|
|
m_sysDragMinX = m_sysDragMinX != -1 ? m_sysDragMinX : DragDistanceThreshold;
|
|
|
|
m_sysDragMinY = m_sysDragMinY != -1 ? m_sysDragMinY : DragDistanceThreshold;
|
|
|
|
|
2013-11-28 14:19:50 +00:00
|
|
|
m_buttons.push_back( new BUTTON_STATE( BUT_LEFT, wxEVT_LEFT_DOWN,
|
|
|
|
wxEVT_LEFT_UP, wxEVT_LEFT_DCLICK ) );
|
|
|
|
m_buttons.push_back( new BUTTON_STATE( BUT_RIGHT, wxEVT_RIGHT_DOWN,
|
|
|
|
wxEVT_RIGHT_UP, wxEVT_RIGHT_DCLICK ) );
|
|
|
|
m_buttons.push_back( new BUTTON_STATE( BUT_MIDDLE, wxEVT_MIDDLE_DOWN,
|
|
|
|
wxEVT_MIDDLE_UP, wxEVT_MIDDLE_DCLICK ) );
|
2013-08-06 08:30:09 +00:00
|
|
|
|
|
|
|
ResetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
TOOL_DISPATCHER::~TOOL_DISPATCHER()
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( BUTTON_STATE* st : m_buttons )
|
2013-10-14 11:43:57 +00:00
|
|
|
delete st;
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 08:30:09 +00:00
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
void TOOL_DISPATCHER::ResetState()
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( BUTTON_STATE* st : m_buttons )
|
2013-10-14 11:43:57 +00:00
|
|
|
st->Reset();
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
KIGFX::VIEW* TOOL_DISPATCHER::getView()
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2017-10-31 11:01:59 +00:00
|
|
|
return m_toolMgr->GetView();
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 08:30:09 +00:00
|
|
|
|
2013-08-21 15:37:27 +00:00
|
|
|
bool TOOL_DISPATCHER::handleMouseButton( wxEvent& aEvent, int aIndex, bool aMotion )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
BUTTON_STATE* st = m_buttons[aIndex];
|
2013-10-14 11:43:57 +00:00
|
|
|
wxEventType type = aEvent.GetEventType();
|
2017-11-01 11:14:16 +00:00
|
|
|
OPT<TOOL_EVENT> evt;
|
2013-10-14 11:43:57 +00:00
|
|
|
bool isClick = false;
|
|
|
|
|
2014-07-15 15:34:53 +00:00
|
|
|
// bool up = type == st->upEvent;
|
|
|
|
// bool down = type == st->downEvent;
|
|
|
|
bool up = false, down = false;
|
2013-11-28 14:19:50 +00:00
|
|
|
bool dblClick = type == st->dblClickEvent;
|
2014-07-15 15:34:53 +00:00
|
|
|
bool state = st->GetState();
|
|
|
|
|
|
|
|
if( !dblClick )
|
|
|
|
{
|
|
|
|
// Sometimes the dispatcher does not receive mouse button up event, so it stays
|
|
|
|
// in the dragging mode even if the mouse button is not held anymore
|
|
|
|
if( st->pressed && !state )
|
|
|
|
up = true;
|
2017-12-12 17:02:15 +00:00
|
|
|
// Don't apply same logic to down events as it kills touchpad tapping
|
|
|
|
else if( !st->pressed && type == st->downEvent )
|
2014-07-15 15:34:53 +00:00
|
|
|
down = true;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2015-07-15 12:08:52 +00:00
|
|
|
int mods = decodeModifiers( static_cast<wxMouseEvent*>( &aEvent ) );
|
2013-10-14 11:43:57 +00:00
|
|
|
int args = st->button | mods;
|
|
|
|
|
|
|
|
if( down ) // Handle mouse button press
|
|
|
|
{
|
|
|
|
st->downTimestamp = wxGetLocalTimeMillis();
|
2017-07-27 08:54:08 +00:00
|
|
|
|
|
|
|
if( !st->pressed ) // save the drag origin on the first click only
|
2021-10-06 18:50:56 +00:00
|
|
|
{
|
2017-07-27 08:54:08 +00:00
|
|
|
st->dragOrigin = m_lastMousePos;
|
2021-10-06 18:50:56 +00:00
|
|
|
st->dragOriginScreen = m_lastMousePosScreen;
|
|
|
|
}
|
2017-07-27 08:54:08 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
st->downPosition = m_lastMousePos;
|
|
|
|
st->pressed = true;
|
2013-10-14 18:40:36 +00:00
|
|
|
evt = TOOL_EVENT( TC_MOUSE, TA_MOUSE_DOWN, args );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-11-29 15:45:39 +00:00
|
|
|
else if( up ) // Handle mouse button release
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
st->pressed = false;
|
|
|
|
|
|
|
|
if( st->dragging )
|
2021-09-24 17:14:08 +00:00
|
|
|
evt = TOOL_EVENT( TC_MOUSE, TA_MOUSE_UP, args );
|
2013-10-14 11:43:57 +00:00
|
|
|
else
|
|
|
|
isClick = true;
|
|
|
|
|
|
|
|
if( isClick )
|
2013-10-14 18:40:36 +00:00
|
|
|
evt = TOOL_EVENT( TC_MOUSE, TA_MOUSE_CLICK, args );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
st->dragging = false;
|
|
|
|
}
|
2013-11-28 14:19:50 +00:00
|
|
|
else if( dblClick )
|
|
|
|
{
|
|
|
|
evt = TOOL_EVENT( TC_MOUSE, TA_MOUSE_DBLCLICK, args );
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
if( st->pressed && aMotion )
|
|
|
|
{
|
2021-09-24 17:14:08 +00:00
|
|
|
if( !st->dragging )
|
|
|
|
{
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
if( wxGetLocalTimeMillis() - st->downTimestamp > DragTimeThreshold )
|
|
|
|
st->dragging = true;
|
2021-09-29 17:30:22 +00:00
|
|
|
#endif
|
2021-10-06 18:50:56 +00:00
|
|
|
VECTOR2D offset = m_lastMousePosScreen - st->dragOriginScreen;
|
2021-09-24 17:14:08 +00:00
|
|
|
|
|
|
|
if( abs( offset.x ) > m_sysDragMinX || abs( offset.y ) > m_sysDragMinY )
|
|
|
|
st->dragging = true;
|
2021-09-29 17:30:22 +00:00
|
|
|
|
2021-09-24 17:14:08 +00:00
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2021-09-24 17:14:08 +00:00
|
|
|
if( st->dragging )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
evt = TOOL_EVENT( TC_MOUSE, TA_MOUSE_DRAG, args );
|
2015-07-15 12:08:52 +00:00
|
|
|
evt->setMouseDragOrigin( st->dragOrigin );
|
|
|
|
evt->setMouseDelta( m_lastMousePos - st->dragOrigin );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( evt )
|
|
|
|
{
|
|
|
|
evt->SetMousePosition( isClick ? st->downPosition : m_lastMousePos );
|
|
|
|
m_toolMgr->ProcessEvent( *evt );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
|
2017-09-16 16:28:48 +00:00
|
|
|
// Helper function to know if a special key ( see key list ) should be captured
|
|
|
|
// or if the event can be skipped
|
2018-04-14 12:23:48 +00:00
|
|
|
// on Linux, the event must be passed to the GUI if they are not used by KiCad,
|
2017-09-16 16:28:48 +00:00
|
|
|
// especially the wxEVENT_CHAR_HOOK, if it is not handled
|
2019-07-08 09:44:06 +00:00
|
|
|
// Some keys have a predefined action in wxWidgets so, even if not used,
|
|
|
|
// the even will be not skipped
|
|
|
|
// the unused keys listed in isKeySpecialCode() will be not skipped
|
2017-09-16 16:28:48 +00:00
|
|
|
bool isKeySpecialCode( int aKeyCode )
|
|
|
|
{
|
2019-07-08 09:44:06 +00:00
|
|
|
// These keys have predefined actions (like move thumbtrack cursor),
|
|
|
|
// and we do not want these actions executed
|
2017-09-16 16:28:48 +00:00
|
|
|
const enum wxKeyCode special_keys[] =
|
|
|
|
{
|
|
|
|
WXK_PAGEUP, WXK_PAGEDOWN,
|
2019-07-08 09:44:06 +00:00
|
|
|
WXK_NUMPAD_PAGEUP, WXK_NUMPAD_PAGEDOWN
|
2017-09-16 16:28:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool isInList = false;
|
|
|
|
|
2019-01-06 16:43:12 +00:00
|
|
|
for( unsigned ii = 0; ii < arrayDim( special_keys ) && !isInList; ii++ )
|
2017-09-16 16:28:48 +00:00
|
|
|
{
|
|
|
|
if( special_keys[ii] == aKeyCode )
|
|
|
|
isInList = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isInList;
|
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
|
2019-07-08 09:44:06 +00:00
|
|
|
// Helper function to know if a key should be managed by DispatchWxEvent()
|
|
|
|
// or if the event can be ignored and skipped because the key is only a modifier
|
|
|
|
// that is not used alone in kicad
|
|
|
|
static bool isKeyModifierOnly( int aKeyCode )
|
|
|
|
{
|
|
|
|
const enum wxKeyCode special_keys[] =
|
|
|
|
{
|
|
|
|
WXK_CONTROL, WXK_RAW_CONTROL, WXK_SHIFT,WXK_ALT
|
|
|
|
};
|
|
|
|
|
|
|
|
bool isInList = false;
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < arrayDim( special_keys ) && !isInList; ii++ )
|
|
|
|
{
|
|
|
|
if( special_keys[ii] == aKeyCode )
|
|
|
|
isInList = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isInList;
|
|
|
|
}
|
2018-04-14 12:23:48 +00:00
|
|
|
|
2020-11-05 02:06:36 +00:00
|
|
|
|
|
|
|
static bool isMouseClick( wxEventType type )
|
|
|
|
{
|
|
|
|
return type == wxEVT_LEFT_DOWN || type == wxEVT_LEFT_UP || type == wxEVT_MIDDLE_DOWN
|
|
|
|
|| type == wxEVT_MIDDLE_UP || type == wxEVT_RIGHT_DOWN || type == wxEVT_RIGHT_UP
|
|
|
|
|| type == wxEVT_LEFT_DCLICK || type == wxEVT_MIDDLE_DCLICK
|
|
|
|
|| type == wxEVT_RIGHT_DCLICK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-08 09:44:06 +00:00
|
|
|
/* A helper class that convert some special key codes to an equivalent.
|
2017-09-16 16:28:48 +00:00
|
|
|
* WXK_NUMPAD_UP to WXK_UP,
|
|
|
|
* WXK_NUMPAD_DOWN to WXK_DOWN,
|
|
|
|
* WXK_NUMPAD_LEFT to WXK_LEFT,
|
|
|
|
* WXK_NUMPAD_RIGHT,
|
|
|
|
* WXK_NUMPAD_PAGEUP,
|
|
|
|
* WXK_NUMPAD_PAGEDOWN
|
|
|
|
* note:
|
2018-04-14 12:23:48 +00:00
|
|
|
* wxEVT_CHAR_HOOK does this conversion when it is skipped by firing a wxEVT_CHAR
|
2017-09-16 16:28:48 +00:00
|
|
|
* with this converted code, but we do not skip these key events because they also
|
|
|
|
* have default action (scroll the panel)
|
|
|
|
*/
|
|
|
|
int translateSpecialCode( int aKeyCode )
|
|
|
|
{
|
|
|
|
switch( aKeyCode )
|
|
|
|
{
|
|
|
|
case WXK_NUMPAD_UP: return WXK_UP;
|
|
|
|
case WXK_NUMPAD_DOWN: return WXK_DOWN;
|
|
|
|
case WXK_NUMPAD_LEFT: return WXK_LEFT;
|
|
|
|
case WXK_NUMPAD_RIGHT: return WXK_RIGHT;
|
|
|
|
case WXK_NUMPAD_PAGEUP: return WXK_PAGEUP;
|
|
|
|
case WXK_NUMPAD_PAGEDOWN: return WXK_PAGEDOWN;
|
|
|
|
default: break;
|
|
|
|
};
|
|
|
|
|
|
|
|
return aKeyCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 19:55:58 +00:00
|
|
|
OPT<TOOL_EVENT> TOOL_DISPATCHER::GetToolEvent( wxKeyEvent* aKeyEvent, bool* keyIsSpecial )
|
2020-05-02 13:36:29 +00:00
|
|
|
{
|
2020-05-03 19:55:58 +00:00
|
|
|
OPT<TOOL_EVENT> evt;
|
|
|
|
int key = aKeyEvent->GetKeyCode();
|
|
|
|
int unicode_key = aKeyEvent->GetUnicodeKey();
|
|
|
|
|
|
|
|
// This wxEVT_CHAR_HOOK event can be ignored: not useful in Kicad
|
|
|
|
if( isKeyModifierOnly( key ) )
|
|
|
|
{
|
|
|
|
aKeyEvent->Skip();
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
2020-07-27 22:42:23 +00:00
|
|
|
wxLogTrace( kicadTraceKeyEvent, "TOOL_DISPATCHER::GetToolEvent %s", dump( *aKeyEvent ) );
|
2020-05-03 19:55:58 +00:00
|
|
|
|
|
|
|
// if the key event must be skipped, skip it here if the event is a wxEVT_CHAR_HOOK
|
|
|
|
// and do nothing.
|
|
|
|
*keyIsSpecial = isKeySpecialCode( key );
|
|
|
|
|
|
|
|
if( aKeyEvent->GetEventType() == wxEVT_CHAR_HOOK )
|
|
|
|
key = translateSpecialCode( key );
|
|
|
|
|
|
|
|
int mods = decodeModifiers( aKeyEvent );
|
|
|
|
|
|
|
|
if( mods & MD_CTRL )
|
|
|
|
{
|
|
|
|
// wxWidgets maps key codes related to Ctrl+letter handled by CHAR_EVT
|
|
|
|
// (http://docs.wxwidgets.org/trunk/classwx_key_event.html):
|
|
|
|
// char events for ASCII letters in this case carry codes corresponding to the ASCII
|
|
|
|
// value of Ctrl-Latter, i.e. 1 for Ctrl-A, 2 for Ctrl-B and so on until 26 for Ctrl-Z.
|
|
|
|
// They are remapped here to be more easy to handle in code
|
2021-06-09 19:32:58 +00:00
|
|
|
// Note also on OSX wxWidgets has a different behavior and the mapping is made
|
2020-05-03 19:55:58 +00:00
|
|
|
// only for ctrl+'A' to ctlr+'Z' (unicode code return 'A' to 'Z').
|
|
|
|
// Others OS return WXK_CONTROL_A to WXK_CONTROL_Z, and Ctrl+'M' returns the same code as
|
|
|
|
// the return key, so the remapping does not use the unicode key value.
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if( unicode_key >= 'A' && unicode_key <= 'Z' && key >= WXK_CONTROL_A && key <= WXK_CONTROL_Z )
|
|
|
|
#else
|
2021-10-05 16:57:25 +00:00
|
|
|
ignore_unused( unicode_key );
|
2020-05-03 19:55:58 +00:00
|
|
|
|
|
|
|
if( key >= WXK_CONTROL_A && key <= WXK_CONTROL_Z )
|
|
|
|
#endif
|
|
|
|
key += 'A' - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if( mods & MD_ALT )
|
|
|
|
{
|
|
|
|
// OSX maps a bunch of commonly used extended-ASCII characters onto the keyboard
|
|
|
|
// using the ALT key. Since we use ALT for some of our hotkeys, we need to map back
|
|
|
|
// to the underlying keys. The kVK_ANSI_* values come from Apple and are said to be
|
2021-06-09 19:32:58 +00:00
|
|
|
// hardware independent.
|
2020-05-03 19:55:58 +00:00
|
|
|
switch( aKeyEvent->GetRawKeyCode() )
|
|
|
|
{
|
|
|
|
case /* kVK_ANSI_1 */ 0x12: key = '1'; break;
|
|
|
|
case /* kVK_ANSI_2 */ 0x13: key = '2'; break;
|
|
|
|
case /* kVK_ANSI_3 */ 0x14: key = '3'; break;
|
|
|
|
case /* kVK_ANSI_4 */ 0x15: key = '4'; break;
|
|
|
|
case /* kVK_ANSI_6 */ 0x16: key = '6'; break;
|
|
|
|
case /* kVK_ANSI_5 */ 0x17: key = '5'; break;
|
|
|
|
case /* kVK_ANSI_Equal */ 0x18: key = '='; break;
|
|
|
|
case /* kVK_ANSI_9 */ 0x19: key = '9'; break;
|
|
|
|
case /* kVK_ANSI_7 */ 0x1A: key = '7'; break;
|
|
|
|
case /* kVK_ANSI_Minus */ 0x1B: key = '-'; break;
|
|
|
|
case /* kVK_ANSI_8 */ 0x1C: key = '8'; break;
|
|
|
|
case /* kVK_ANSI_0 */ 0x1D: key = '0'; break;
|
|
|
|
default: ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( key == WXK_ESCAPE ) // ESC is the special key for canceling tools
|
|
|
|
evt = TOOL_EVENT( TC_COMMAND, TA_CANCEL_TOOL );
|
|
|
|
else
|
|
|
|
evt = TOOL_EVENT( TC_KEYBOARD, TA_KEY_PRESSED, key | mods );
|
|
|
|
|
|
|
|
return evt;
|
2020-05-02 13:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 19:55:58 +00:00
|
|
|
void TOOL_DISPATCHER::DispatchWxEvent( wxEvent& aEvent )
|
2013-08-02 14:46:53 +00:00
|
|
|
{
|
2020-03-18 22:48:36 +00:00
|
|
|
bool motion = false;
|
|
|
|
bool buttonEvents = false;
|
|
|
|
VECTOR2D pos;
|
2017-11-01 11:14:16 +00:00
|
|
|
OPT<TOOL_EVENT> evt;
|
2020-05-23 20:39:43 +00:00
|
|
|
bool keyIsEscape = false; // True if the keypress was the escape key
|
2020-03-18 22:48:36 +00:00
|
|
|
bool keyIsSpecial = false; // True if the key is a special key code
|
2020-05-30 11:31:19 +00:00
|
|
|
wxWindow* focus = wxWindow::FindFocus();
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2020-10-15 04:38:32 +00:00
|
|
|
// Required in win32 to ensure wxTimer events get scheduled in between other events
|
2021-01-25 12:42:36 +00:00
|
|
|
// Or else we may stall them out entirely and never get them during actions like rapid
|
|
|
|
// mouse moves.
|
2020-10-15 04:38:32 +00:00
|
|
|
KIPLATFORM::APP::ForceTimerMessagesToBeCreatedIfNecessary();
|
|
|
|
|
2020-11-05 02:06:36 +00:00
|
|
|
wxEventType type = aEvent.GetEventType();
|
|
|
|
|
2018-04-08 10:28:59 +00:00
|
|
|
// Sometimes there is no window that has the focus (it happens when another PCB_BASE_FRAME
|
2017-10-30 14:12:41 +00:00
|
|
|
// is opened and is iconized on Windows).
|
2019-07-08 09:44:06 +00:00
|
|
|
// In this case, give the focus to the parent frame (GAL canvas itself does not accept the
|
2019-06-09 12:18:11 +00:00
|
|
|
// focus when iconized for some obscure reason)
|
2020-05-30 11:31:19 +00:00
|
|
|
if( focus == nullptr )
|
2020-03-24 01:01:23 +00:00
|
|
|
{
|
2020-11-06 05:38:36 +00:00
|
|
|
wxWindow* holderWindow = dynamic_cast<wxWindow*>( m_toolMgr->GetToolHolder() );
|
2020-03-24 01:01:23 +00:00
|
|
|
|
2022-05-12 18:05:06 +00:00
|
|
|
#if defined( _WIN32 ) || defined( __WXGTK__ )
|
2020-11-04 12:21:05 +00:00
|
|
|
// Mouse events may trigger regardless of window status (windows feature)
|
|
|
|
// However we need to avoid focus fighting (especially modals)
|
2021-06-07 22:20:47 +00:00
|
|
|
if( holderWindow && KIPLATFORM::UI::IsWindowActive( holderWindow ) )
|
2020-11-04 12:21:05 +00:00
|
|
|
#else
|
2020-11-05 02:06:36 +00:00
|
|
|
if( holderWindow )
|
2020-11-04 12:21:05 +00:00
|
|
|
#endif
|
|
|
|
{
|
2020-11-05 02:06:36 +00:00
|
|
|
holderWindow->SetFocus();
|
2020-11-04 12:21:05 +00:00
|
|
|
}
|
2020-03-24 01:01:23 +00:00
|
|
|
}
|
2017-10-30 14:12:41 +00:00
|
|
|
|
2020-11-05 02:06:36 +00:00
|
|
|
if( isMouseClick( type ) )
|
|
|
|
{
|
2020-11-06 05:37:05 +00:00
|
|
|
if( m_toolMgr->GetToolHolder() && m_toolMgr->GetToolHolder()->GetToolCanvas() )
|
|
|
|
m_toolMgr->GetToolHolder()->GetToolCanvas()->SetFocus();
|
2020-11-05 02:06:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
// Mouse handling
|
2017-09-16 16:28:48 +00:00
|
|
|
// Note: wxEVT_LEFT_DOWN event must always be skipped.
|
2013-10-14 11:43:57 +00:00
|
|
|
if( type == wxEVT_MOTION || type == wxEVT_MOUSEWHEEL ||
|
2016-01-25 00:18:29 +00:00
|
|
|
#if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
|
2015-05-21 20:54:29 +00:00
|
|
|
type == wxEVT_MAGNIFY ||
|
|
|
|
#endif
|
2020-11-05 02:06:36 +00:00
|
|
|
isMouseClick( type ) ||
|
2018-04-14 12:23:48 +00:00
|
|
|
// Event issued when mouse retains position in screen coordinates,
|
2013-11-28 14:19:50 +00:00
|
|
|
// but changes in world coordinates (e.g. autopanning)
|
2013-10-14 14:13:35 +00:00
|
|
|
type == KIGFX::WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2014-02-06 09:43:19 +00:00
|
|
|
wxMouseEvent* me = static_cast<wxMouseEvent*>( &aEvent );
|
2015-07-15 12:08:52 +00:00
|
|
|
int mods = decodeModifiers( me );
|
2014-02-06 09:43:19 +00:00
|
|
|
|
2020-03-18 22:48:36 +00:00
|
|
|
if( m_toolMgr->GetViewControls() )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2020-03-18 22:48:36 +00:00
|
|
|
pos = m_toolMgr->GetViewControls()->GetMousePosition();
|
2021-10-06 18:50:56 +00:00
|
|
|
m_lastMousePosScreen = m_toolMgr->GetViewControls()->GetMousePosition( false );
|
2020-03-18 22:48:36 +00:00
|
|
|
|
|
|
|
if( pos != m_lastMousePos )
|
|
|
|
{
|
|
|
|
motion = true;
|
|
|
|
m_lastMousePos = pos;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
for( unsigned int i = 0; i < m_buttons.size(); i++ )
|
|
|
|
buttonEvents |= handleMouseButton( aEvent, i, motion );
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2020-03-18 22:48:36 +00:00
|
|
|
if( m_toolMgr->GetViewControls() )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2020-03-18 22:48:36 +00:00
|
|
|
if( !buttonEvents && motion )
|
|
|
|
{
|
|
|
|
evt = TOOL_EVENT( TC_MOUSE, TA_MOUSE_MOTION, mods );
|
|
|
|
evt->SetMousePosition( pos );
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-16 16:28:48 +00:00
|
|
|
else if( type == wxEVT_CHAR_HOOK || type == wxEVT_CHAR )
|
2013-08-21 15:37:27 +00:00
|
|
|
{
|
2020-05-23 20:39:43 +00:00
|
|
|
wxKeyEvent* ke = static_cast<wxKeyEvent*>( &aEvent );
|
|
|
|
|
2021-05-03 20:07:16 +00:00
|
|
|
wxLogTrace( kicadTraceKeyEvent, "TOOL_DISPATCHER::DispatchWxEvent %s", dump( *ke ) );
|
|
|
|
|
2021-05-29 17:18:20 +00:00
|
|
|
// Do not process wxEVT_CHAR_HOOK for a shift-modified key, as ACTION_MANAGER::RunHotKey
|
|
|
|
// will run the un-shifted key and that's not what we want. Wait to get the translated
|
|
|
|
// key from wxEVT_CHAR.
|
|
|
|
// See https://gitlab.com/kicad/code/kicad/-/issues/1809
|
|
|
|
if( type == wxEVT_CHAR_HOOK && ke->GetModifiers() == wxMOD_SHIFT )
|
|
|
|
{
|
|
|
|
aEvent.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:39:43 +00:00
|
|
|
keyIsEscape = ( ke->GetKeyCode() == WXK_ESCAPE );
|
2020-05-30 01:20:03 +00:00
|
|
|
|
2021-07-05 23:47:03 +00:00
|
|
|
if( KIUI::IsInputControlFocused( focus ) )
|
2020-05-30 11:31:19 +00:00
|
|
|
{
|
2021-07-05 23:47:03 +00:00
|
|
|
bool enabled = KIUI::IsInputControlEditable( focus );
|
2020-07-14 12:28:55 +00:00
|
|
|
|
2020-05-30 11:31:19 +00:00
|
|
|
// Never process key events for tools when a text entry has focus
|
2020-07-14 12:28:55 +00:00
|
|
|
if( enabled )
|
|
|
|
{
|
|
|
|
aEvent.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Even if not enabled, allow a copy out
|
|
|
|
else if( ke->GetModifiers() == wxMOD_CONTROL && ke->GetKeyCode() == 'C' )
|
|
|
|
{
|
|
|
|
aEvent.Skip();
|
|
|
|
return;
|
|
|
|
}
|
2020-05-30 11:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
evt = GetToolEvent( ke, &keyIsSpecial );
|
2013-08-21 15:37:27 +00:00
|
|
|
}
|
2020-03-24 01:01:23 +00:00
|
|
|
else if( type == wxEVT_MENU_OPEN || type == wxEVT_MENU_CLOSE || type == wxEVT_MENU_HIGHLIGHT )
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// wxWidgets has several issues that we have to work around:
|
|
|
|
//
|
|
|
|
// 1) wxWidgets 3.0.x Windows has a bug where wxEVT_MENU_OPEN and wxEVT_MENU_HIGHLIGHT
|
|
|
|
// events are not captured by the ACTON_MENU menus. So we forward them here.
|
|
|
|
// (FWIW, this one is fixed in wxWidgets 3.1.x.)
|
|
|
|
//
|
|
|
|
// 2) wxWidgets doesn't pass the menu pointer for wxEVT_MENU_HIGHLIGHT events. So we
|
|
|
|
// store the menu pointer from the wxEVT_MENU_OPEN call.
|
|
|
|
//
|
|
|
|
// 3) wxWidgets has no way to tell whether a command is from a menu selection or a
|
|
|
|
// hotkey. So we keep track of menu highlighting so we can differentiate.
|
|
|
|
//
|
|
|
|
|
|
|
|
static ACTION_MENU* currentMenu;
|
|
|
|
|
|
|
|
wxMenuEvent& menuEvent = *dynamic_cast<wxMenuEvent*>( &aEvent );
|
|
|
|
|
|
|
|
if( type == wxEVT_MENU_OPEN )
|
|
|
|
{
|
|
|
|
currentMenu = dynamic_cast<ACTION_MENU*>( menuEvent.GetMenu() );
|
|
|
|
|
|
|
|
if( currentMenu )
|
|
|
|
currentMenu->OnMenuEvent( menuEvent );
|
|
|
|
}
|
|
|
|
else if( type == wxEVT_MENU_HIGHLIGHT )
|
|
|
|
{
|
|
|
|
if( currentMenu )
|
|
|
|
currentMenu->OnMenuEvent( menuEvent );
|
|
|
|
}
|
|
|
|
else if( type == wxEVT_MENU_CLOSE )
|
|
|
|
{
|
|
|
|
if( currentMenu )
|
|
|
|
currentMenu->OnMenuEvent( menuEvent );
|
|
|
|
|
|
|
|
currentMenu = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
2013-08-21 15:37:27 +00:00
|
|
|
|
2019-06-19 00:14:48 +00:00
|
|
|
bool handled = false;
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( evt )
|
2019-07-08 14:28:45 +00:00
|
|
|
{
|
2019-07-27 14:11:41 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack, "TOOL_DISPATCHER::DispatchWxEvent %s", evt->Format() );
|
|
|
|
|
2020-05-03 19:55:58 +00:00
|
|
|
handled = m_toolMgr->ProcessEvent( *evt );
|
2017-09-15 14:28:55 +00:00
|
|
|
|
2020-05-23 20:39:43 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack, "TOOL_DISPATCHER::DispatchWxEvent - Handled: %s %s",
|
|
|
|
( handled ? "true" : "false" ), evt->Format() );
|
2019-07-08 14:28:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 14:28:55 +00:00
|
|
|
// pass the event to the GUI, it might still be interested in it
|
2018-04-14 12:23:48 +00:00
|
|
|
// Note wxEVT_CHAR_HOOK event is already skipped for special keys not used by KiCad
|
2017-09-16 16:28:48 +00:00
|
|
|
// and wxEVT_LEFT_DOWN must be always Skipped.
|
|
|
|
//
|
2017-09-15 14:28:55 +00:00
|
|
|
// On OS X, key events are always meant to be caught. An uncaught key event is assumed
|
|
|
|
// to be a user input error by OS X (as they are pressing keys in a context where nothing
|
|
|
|
// is there to catch the event). This annoyingly makes OS X beep and/or flash the screen
|
2018-04-14 12:23:48 +00:00
|
|
|
// in Pcbnew and the footprint editor any time a hotkey is used. The correct procedure is
|
2017-09-16 16:28:48 +00:00
|
|
|
// to NOT pass wxEVT_CHAR events to the GUI under OS X.
|
|
|
|
//
|
2019-07-08 09:44:06 +00:00
|
|
|
// On Windows, avoid to call wxEvent::Skip for special keys because some keys
|
|
|
|
// (PAGE_UP, PAGE_DOWN) have predefined actions (like move thumbtrack cursor), and we do
|
2018-04-14 12:23:48 +00:00
|
|
|
// not want these actions executed (most are handled by KiCad)
|
2017-09-15 14:28:55 +00:00
|
|
|
|
2017-09-16 16:28:48 +00:00
|
|
|
if( !evt || type == wxEVT_LEFT_DOWN )
|
2016-05-05 15:34:16 +00:00
|
|
|
aEvent.Skip();
|
2015-07-03 18:58:12 +00:00
|
|
|
|
2019-07-08 09:44:06 +00:00
|
|
|
// Not handled wxEVT_CHAR must be Skipped (sent to GUI).
|
2017-09-20 17:41:59 +00:00
|
|
|
// Otherwise accelerators and shortcuts in main menu or toolbars are not seen.
|
2020-05-23 20:39:43 +00:00
|
|
|
// Escape key presses are never skipped by the handler since they correspond to tool cancel
|
2021-01-25 12:42:36 +00:00
|
|
|
// events, and if they aren't skipped then they are propagated to other frames (which we
|
|
|
|
// don't want).
|
2020-05-23 20:39:43 +00:00
|
|
|
if( (type == wxEVT_CHAR || type == wxEVT_CHAR_HOOK)
|
|
|
|
&& !keyIsSpecial
|
|
|
|
&& !handled
|
|
|
|
&& !keyIsEscape )
|
2017-09-15 14:28:55 +00:00
|
|
|
aEvent.Skip();
|
2020-05-23 20:39:43 +00:00
|
|
|
|
|
|
|
wxLogTrace( kicadTraceToolStack, "TOOL_DISPATCHER::DispatchWxEvent - Wx event skipped: %s",
|
|
|
|
( aEvent.GetSkipped() ? "true" : "false" ) );
|
2013-08-02 14:46:53 +00:00
|
|
|
}
|