From 4905bbe5002fbcf78db45f399e5ab535df33f90e Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Fri, 6 Oct 2017 09:23:13 +0200 Subject: [PATCH] hotkey management: enhancements in code to know if a hotkey was handled by an event or not. No actual change in key management: in legacy it was partially existing, and not yet used in GAL mode. --- common/draw_frame.cpp | 17 ++++++++++++++++- common/tool/tool_manager.cpp | 12 ++++++++---- eeschema/controle.cpp | 24 ++++++++++++------------ gerbview/controle.cpp | 12 +++++------- include/draw_frame.h | 18 ++++++++++++++++-- include/tool/tool_manager.h | 6 ++++-- pcbnew/controle.cpp | 10 ++++------ pcbnew/moduleframe.cpp | 10 ++++------ 8 files changed, 69 insertions(+), 40 deletions(-) diff --git a/common/draw_frame.cpp b/common/draw_frame.cpp index d559a8423a..db3a31234f 100644 --- a/common/draw_frame.cpp +++ b/common/draw_frame.cpp @@ -95,6 +95,8 @@ const wxChar EDA_DRAW_FRAME::CANVAS_TYPE_KEY[] = wxT( "canvas_type" ); static const wxString MaxUndoItemsEntry(wxT( "DevelMaxUndoItems" ) ); BEGIN_EVENT_TABLE( EDA_DRAW_FRAME, KIWAY_PLAYER ) + EVT_CHAR_HOOK( EDA_DRAW_FRAME::OnCharHook ) + EVT_MOUSEWHEEL( EDA_DRAW_FRAME::OnMouseEvent ) EVT_MENU_OPEN( EDA_DRAW_FRAME::OnMenuOpen ) EVT_ACTIVATE( EDA_DRAW_FRAME::OnActivate ) @@ -227,6 +229,14 @@ EDA_DRAW_FRAME::~EDA_DRAW_FRAME() } +void EDA_DRAW_FRAME::OnCharHook( wxKeyEvent& event ) +{ + // Key events can be filtered here. + // Currently no filtering is made. + event.Skip(); +} + + void EDA_DRAW_FRAME::ReleaseFile() { m_file_checker = nullptr; @@ -1317,9 +1327,10 @@ void EDA_DRAW_FRAME::RefreshCrossHair( const wxPoint &aOldPos, } } -void EDA_DRAW_FRAME::GeneralControlKeyMovement( int aHotKey, wxPoint *aPos, +bool EDA_DRAW_FRAME::GeneralControlKeyMovement( int aHotKey, wxPoint *aPos, bool aSnapToGrid ) { + bool key_handled = false; // If requested snap the current position to the grid if( aSnapToGrid ) @@ -1344,6 +1355,7 @@ void EDA_DRAW_FRAME::GeneralControlKeyMovement( int aHotKey, wxPoint *aPos, case WXK_LEFT: case WXK_NUMPAD6: case WXK_RIGHT: + key_handled = true; { /* Here's a tricky part: when doing cursor key movement, the * 'previous' point should be taken from memory, *not* from the @@ -1399,6 +1411,7 @@ void EDA_DRAW_FRAME::GeneralControlKeyMovement( int aHotKey, wxPoint *aPos, default: /* Can't happen since we entered the statement */ break; } + m_canvas->MoveCursor( *aPos ); m_movingCursorWithKeyboard = true; } @@ -1407,6 +1420,8 @@ void EDA_DRAW_FRAME::GeneralControlKeyMovement( int aHotKey, wxPoint *aPos, default: break; } + + return key_handled; } diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index d1c7f95fc5..9c869eddec 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -728,9 +728,9 @@ TOOL_MANAGER::ID_LIST::iterator TOOL_MANAGER::finishTool( TOOL_STATE* aState ) } -void TOOL_MANAGER::ProcessEvent( const TOOL_EVENT& aEvent ) +bool TOOL_MANAGER::ProcessEvent( const TOOL_EVENT& aEvent ) { - processEvent( aEvent ); + bool hotkey_handled = processEvent( aEvent ); if( TOOL_STATE* active = GetCurrentToolState() ) { @@ -742,6 +742,8 @@ void TOOL_MANAGER::ProcessEvent( const TOOL_EVENT& aEvent ) EDA_DRAW_FRAME* f = static_cast( GetEditFrame() ); f->GetGalCanvas()->Refresh(); // fixme: ugly hack, provide a method in TOOL_DISPATCHER. } + + return hotkey_handled; } @@ -859,11 +861,11 @@ void TOOL_MANAGER::popViewControls() } -void TOOL_MANAGER::processEvent( const TOOL_EVENT& aEvent ) +bool TOOL_MANAGER::processEvent( const TOOL_EVENT& aEvent ) { // Early dispatch of events destined for the TOOL_MANAGER if( !dispatchStandardEvents( aEvent ) ) - return; + return true; dispatchInternal( aEvent ); dispatchActivation( aEvent ); @@ -876,6 +878,8 @@ void TOOL_MANAGER::processEvent( const TOOL_EVENT& aEvent ) m_eventQueue.pop_front(); processEvent( event ); } + + return false; } bool TOOL_MANAGER::IsToolActive( TOOL_ID aId ) const diff --git a/eeschema/controle.cpp b/eeschema/controle.cpp index 82f2ca0a7f..2876b965d3 100644 --- a/eeschema/controle.cpp +++ b/eeschema/controle.cpp @@ -216,8 +216,6 @@ SCH_ITEM* SCH_EDIT_FRAME::LocateItem( const wxPoint& aPosition, const KICAD_T aF bool SCH_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KEY aHotKey ) { - bool eventHandled = true; - // Filter out the 'fake' mouse motion after a keyboard movement if( !aHotKey && m_movingCursorWithKeyboard ) { @@ -239,7 +237,7 @@ bool SCH_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KE wxPoint pos = aPosition; wxPoint oldpos = GetCrossHairPosition(); - GeneralControlKeyMovement( aHotKey, &pos, snapToGrid ); + bool keyHandled = GeneralControlKeyMovement( aHotKey, &pos, snapToGrid ); // Update cursor position. SetCrossHairPosition( pos, snapToGrid ); @@ -248,23 +246,25 @@ bool SCH_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KE if( aHotKey ) { SCH_SCREEN* screen = GetScreen(); + bool hk_handled; if( screen->GetCurItem() && screen->GetCurItem()->GetFlags() ) - eventHandled = OnHotKey( aDC, aHotKey, aPosition, screen->GetCurItem() ); + hk_handled = OnHotKey( aDC, aHotKey, aPosition, screen->GetCurItem() ); else - eventHandled = OnHotKey( aDC, aHotKey, aPosition, NULL ); + hk_handled = OnHotKey( aDC, aHotKey, aPosition, NULL ); + + if( hk_handled ) + keyHandled = true; } UpdateStatusBar(); /* Display cursor coordinates info */ - return eventHandled; + return keyHandled; } bool LIB_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KEY aHotKey ) { - bool eventHandled = true; - // Filter out the 'fake' mouse motion after a keyboard movement if( !aHotKey && m_movingCursorWithKeyboard ) { @@ -286,20 +286,20 @@ bool LIB_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KE wxPoint pos = aPosition; wxPoint oldpos = GetCrossHairPosition(); - GeneralControlKeyMovement( aHotKey, &pos, snapToGrid ); + bool keyHandled = GeneralControlKeyMovement( aHotKey, &pos, snapToGrid ); // Update the cursor position. SetCrossHairPosition( pos, snapToGrid ); RefreshCrossHair( oldpos, aPosition, aDC ); - if( aHotKey ) + if( aHotKey && OnHotKey( aDC, aHotKey, aPosition, NULL ) ) { - eventHandled = OnHotKey( aDC, aHotKey, aPosition, NULL ); + keyHandled = true; } UpdateStatusBar(); - return eventHandled; + return keyHandled; } diff --git a/gerbview/controle.cpp b/gerbview/controle.cpp index 6d275c15d2..267d5d91a2 100644 --- a/gerbview/controle.cpp +++ b/gerbview/controle.cpp @@ -1,8 +1,8 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 1992-2010 - * Copyright (C) 1992-2010 KiCad Developers, see change_log.txt for contributors. + * Copyright (C) 1992-2017 + * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -33,8 +33,6 @@ bool GERBVIEW_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KEY aHotKey ) { - bool eventHandled = true; - // Filter out the 'fake' mouse motion after a keyboard movement if( !aHotKey && m_movingCursorWithKeyboard ) { @@ -44,14 +42,14 @@ bool GERBVIEW_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KE wxPoint pos = aPosition; wxPoint oldpos = GetCrossHairPosition(); - GeneralControlKeyMovement( aHotKey, &pos, true ); + bool eventHandled = GeneralControlKeyMovement( aHotKey, &pos, true ); SetCrossHairPosition( pos ); RefreshCrossHair( oldpos, aPosition, aDC ); - if( aHotKey ) + if( aHotKey && OnHotKey( aDC, aHotKey, aPosition ) ) { - eventHandled = OnHotKey( aDC, aHotKey, aPosition ); + eventHandled = true; } UpdateStatusBar(); diff --git a/include/draw_frame.h b/include/draw_frame.h index 6c17ffd30e..536814e0b6 100644 --- a/include/draw_frame.h +++ b/include/draw_frame.h @@ -158,8 +158,13 @@ protected: /** * Function GeneralControlKeyMovement * Handle the common part of GeneralControl dedicated to global - * cursor keys (i.e. cursor movement by keyboard) */ - void GeneralControlKeyMovement( int aHotKey, wxPoint *aPos, bool aSnapToGrid ); + * cursor keys (i.e. cursor movement by keyboard) + * @param aHotKey is the hotkey code + * @param aPos is the position of the cursor (initial then new) + * @param aSnapToGrid = true to force the cursor position on grid + * @return true if the hotkey code is handled (captured). + */ + bool GeneralControlKeyMovement( int aHotKey, wxPoint *aPos, bool aSnapToGrid ); /** * Move and refresh the crosshair after movement and call the mouse capture function. @@ -194,6 +199,14 @@ public: ~EDA_DRAW_FRAME(); + /** this function capture the key event before it is sent to the GUI. + * the basic frame does not capture this event. + * editor frames should override this event function to capture and filter + * these keys when they are used as hotkeys, and skip it if the key is not + * used as hotkey (otherwise the key events will be not sent to menus) + */ + virtual void OnCharHook( wxKeyEvent& event ); + /** * Function LockFile * marks a schematic file as being in use. Use ReleaseFile() to undo this. @@ -571,6 +584,7 @@ public: * @param aDC A device context. * @param aPosition The current cursor position in logical (drawing) units. * @param aHotKey A key event used for application specific control if not zero. + * @return true if the hotkey code is handled (captured). */ virtual bool GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KEY aHotKey = 0 ) { diff --git a/include/tool/tool_manager.h b/include/tool/tool_manager.h index 19b331614c..2762fd806b 100644 --- a/include/tool/tool_manager.h +++ b/include/tool/tool_manager.h @@ -227,8 +227,9 @@ public: /** * Propagates an event to tools that requested events of matching type(s). * @param aEvent is the event to be processed. + * @return true if the event is a managed hotkey */ - void ProcessEvent( const TOOL_EVENT& aEvent ); + bool ProcessEvent( const TOOL_EVENT& aEvent ); /** * Puts an event to the event queue to be processed at the end of event processing cycle. @@ -491,7 +492,8 @@ private: void popViewControls(); ///> Main function for event processing. - void processEvent( const TOOL_EVENT& aEvent ); + ///> @return true if a hotkey was handled + bool processEvent( const TOOL_EVENT& aEvent ); /// Index of registered tools current states, associated by tools' objects. TOOL_STATE_MAP m_toolState; diff --git a/pcbnew/controle.cpp b/pcbnew/controle.cpp index 70cb8caefe..b27e76c53d 100644 --- a/pcbnew/controle.cpp +++ b/pcbnew/controle.cpp @@ -276,8 +276,6 @@ BOARD_ITEM* PCB_BASE_FRAME::PcbGeneralLocateAndDisplay( int aHotKeyCode ) bool PCB_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KEY aHotKey ) { - bool eventHandled = true; - // Filter out the 'fake' mouse motion after a keyboard movement if( !aHotKey && m_movingCursorWithKeyboard ) { @@ -295,7 +293,7 @@ bool PCB_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KE wxPoint oldpos = GetCrossHairPosition(); wxPoint pos = aPosition; - GeneralControlKeyMovement( aHotKey, &pos, snapToGrid ); + bool keyHandled = GeneralControlKeyMovement( aHotKey, &pos, snapToGrid ); // Put cursor in new position, according to the zoom keys (if any). SetCrossHairPosition( pos, snapToGrid ); @@ -336,12 +334,12 @@ bool PCB_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KE RefreshCrossHair( oldpos, aPosition, aDC ); - if( aHotKey ) + if( aHotKey && OnHotKey( aDC, aHotKey, aPosition ) ) { - eventHandled = OnHotKey( aDC, aHotKey, aPosition ); + keyHandled = true; } UpdateStatusBar(); // Display new cursor coordinates - return eventHandled; + return keyHandled; } diff --git a/pcbnew/moduleframe.cpp b/pcbnew/moduleframe.cpp index 038fdf28b1..fa46873eb5 100644 --- a/pcbnew/moduleframe.cpp +++ b/pcbnew/moduleframe.cpp @@ -740,8 +740,6 @@ void FOOTPRINT_EDIT_FRAME::Show3D_Frame( wxCommandEvent& event ) bool FOOTPRINT_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, EDA_KEY aHotKey ) { - bool eventHandled = true; - // Filter out the 'fake' mouse motion after a keyboard movement if( !aHotKey && m_movingCursorWithKeyboard ) { @@ -759,19 +757,19 @@ bool FOOTPRINT_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, wxPoint oldpos = GetCrossHairPosition(); wxPoint pos = aPosition; - GeneralControlKeyMovement( aHotKey, &pos, snapToGrid ); + bool keyHandled = GeneralControlKeyMovement( aHotKey, &pos, snapToGrid ); SetCrossHairPosition( pos, snapToGrid ); RefreshCrossHair( oldpos, aPosition, aDC ); - if( aHotKey ) + if( aHotKey && OnHotKey( aDC, aHotKey, aPosition ) ) { - eventHandled = OnHotKey( aDC, aHotKey, aPosition ); + keyHandled = true; } UpdateStatusBar(); - return eventHandled; + return keyHandled; }