Commit da75c526 authored by Maciej Suminski's avatar Maciej Suminski
Browse files

Hot keys settings are synchronized between GAL & legacy

parent 99e52289
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -350,6 +350,15 @@ bool EDA_DRAW_FRAME::OnHotKey( wxDC* aDC, int aHotKey, const wxPoint& aPosition,
    return false;
}

int EDA_DRAW_FRAME::WriteHotkeyConfig( struct EDA_HOTKEY_CONFIG* aDescList, wxString* aFullFileName )
{
    int result = EDA_BASE_FRAME::WriteHotkeyConfig( aDescList, aFullFileName );

    if( IsGalCanvasActive() )
        GetToolManager()->UpdateHotKeys();

    return result;
}

void EDA_DRAW_FRAME::ToolOnRightClick( wxCommandEvent& event )
{
+15 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@
#include <macros.h>
#include <dialog_hotkeys_editor.h>
#include <menus_helpers.h>
#include <tool/tool_manager.h>

#include <wx/apptrait.h>
#include <wx/stdpaths.h>
@@ -482,6 +483,20 @@ EDA_HOTKEY* GetDescriptorFromHotkey( int aKey, EDA_HOTKEY** aList )
}


EDA_HOTKEY* GetDescriptorFromCommand( int aCommand, EDA_HOTKEY** aList )
{
    for( ; *aList != NULL; aList++ )
    {
        EDA_HOTKEY* hk_decr = *aList;

        if( hk_decr->m_Idcommand == aCommand )
            return hk_decr;
    }

    return NULL;
}


int EDA_BASE_FRAME::WriteHotkeyConfig( struct EDA_HOTKEY_CONFIG* aDescList,
                                       wxString*                 aFullFileName )
{
+87 −13
Original line number Diff line number Diff line
@@ -24,9 +24,12 @@

#include <tool/action_manager.h>
#include <tool/tool_manager.h>
#include <tool/tool_event.h>
#include <tool/tool_action.h>
#include <draw_frame.h>

#include <hotkeys_basic.h>
#include <boost/foreach.hpp>
#include <boost/range/adaptor/map.hpp>
#include <cassert>

ACTION_MANAGER::ACTION_MANAGER( TOOL_MANAGER* aToolManager ) :
@@ -61,18 +64,6 @@ void ACTION_MANAGER::RegisterAction( TOOL_ACTION* aAction )

    m_actionNameIndex[aAction->m_name] = aAction;
    m_actionIdIndex[aAction->m_id] = aAction;

#ifndef NDEBUG
    // Check if there are two global actions assigned to the same hotkey
    if( aAction->GetScope() == AS_GLOBAL )
    {
        BOOST_FOREACH( const TOOL_ACTION* action, m_actionHotKeys[aAction->m_currentHotKey] )
            assert( action->GetScope() != AS_GLOBAL );
    }
#endif /* not NDEBUG */

    if( aAction->HasHotKey() )
        m_actionHotKeys[aAction->m_currentHotKey].push_back( aAction );
}


@@ -181,3 +172,86 @@ bool ACTION_MANAGER::RunHotKey( int aHotKey ) const

    return false;
}


void ACTION_MANAGER::UpdateHotKeys()
{
    m_actionHotKeys.clear();
    std::list<TOOL_ACTION*>& actions = GetActionList();

    for( std::list<TOOL_ACTION*>::iterator it = actions.begin(); it != actions.end(); ++it )
    {
        TOOL_ACTION* aAction = *it;

        int hotkey = processHotKey( aAction, true );

        if( hotkey > 0 )
            m_actionHotKeys[hotkey].push_back( aAction );
    }

#ifndef NDEBUG
    // Check if there are two global actions assigned to the same hotkey
    BOOST_FOREACH( std::list<TOOL_ACTION*>& action_list, m_actionHotKeys | boost::adaptors::map_values )
    {
        int global_actions_cnt = 0;

        BOOST_FOREACH( TOOL_ACTION* action, action_list )
        {
            if( action->GetScope() == AS_GLOBAL )
                ++global_actions_cnt;
        }

        assert( global_actions_cnt <= 1 );
    }
#endif /* not NDEBUG */
}


int ACTION_MANAGER::processHotKey( TOOL_ACTION* aAction, bool aForceUpdate )
{
    int hotkey = 0;

    if( aForceUpdate )
        hotkey = aAction->getDefaultHotKey();
    else
        hotkey = aAction->GetHotKey();

    if( ( hotkey & TOOL_ACTION::LEGACY_HK ) )
    {
        hotkey = hotkey & ~TOOL_ACTION::LEGACY_HK;  // it leaves only HK_xxx identifier
        EDA_DRAW_FRAME* frame = static_cast<EDA_DRAW_FRAME*>( m_toolMgr->GetEditFrame() );
        EDA_HOTKEY* hk_desc = frame->GetHotKeyDescription( hotkey );

        if( hk_desc )
        {
            hotkey = hk_desc->m_KeyCode;

            // Convert modifiers to the ones used by the Tool Framework
            if( hotkey & GR_KB_CTRL )
            {
                hotkey &= ~GR_KB_CTRL;
                hotkey |= MD_CTRL;
            }

            if( hotkey & GR_KB_ALT )
            {
                hotkey &= ~GR_KB_ALT;
                hotkey |= MD_ALT;
            }

            if( hotkey & GR_KB_SHIFT )
            {
                hotkey &= ~GR_KB_SHIFT;
                hotkey |= MD_SHIFT;
            }
        }
        else
        {
            hotkey = 0;
        }

        aAction->setHotKey( hotkey );
    }

    return hotkey;
}
+35 −20
Original line number Diff line number Diff line
@@ -114,33 +114,15 @@ wxMenuItem* CONTEXT_MENU::Add( const wxString& aLabel, int aId, const BITMAP_OPA
wxMenuItem* CONTEXT_MENU::Add( const TOOL_ACTION& aAction )
{
    /// ID numbers for tool actions need to have a value higher than ACTION_ID
    int id = ACTION_ID + aAction.GetId();
    const BITMAP_OPAQUE* icon = aAction.GetIcon();

    wxMenuItem* item = new wxMenuItem( this, id, aAction.GetMenuItem(),
    wxMenuItem* item = new wxMenuItem( this, getMenuId( aAction ), aAction.GetMenuItem(),
                                       aAction.GetDescription(), wxITEM_NORMAL );

    if( icon )
        item->SetBitmap( KiBitmap( icon ) );

    if( aAction.HasHotKey() )
    {
        int key = aAction.GetHotKey() & ~MD_MODIFIER_MASK;
        int mod = aAction.GetHotKey() & MD_MODIFIER_MASK;
        int flags = wxACCEL_NORMAL;

        switch( mod )
        {
        case MD_ALT:    flags = wxACCEL_ALT;    break;
        case MD_CTRL:   flags = wxACCEL_CTRL;   break;
        case MD_SHIFT:  flags = wxACCEL_SHIFT;  break;
        }

        wxAcceleratorEntry accel( flags, key, id, item );
        item->SetAccel( &accel );
    }

    m_toolActions[id] = &aAction;
    m_toolActions[getMenuId( aAction )] = &aAction;

    return Append( item );
}
@@ -198,11 +180,44 @@ void CONTEXT_MENU::Clear()
void CONTEXT_MENU::UpdateAll()
{
    m_update_handler();
    updateHotKeys();

    runOnSubmenus( boost::bind( &CONTEXT_MENU::UpdateAll, _1 ) );
}


void CONTEXT_MENU::updateHotKeys()
{
    for( std::map<int, const TOOL_ACTION*>::const_iterator it = m_toolActions.begin();
            it != m_toolActions.end(); ++it )
    {
        int id = it->first;
        const TOOL_ACTION& action = *it->second;

        if( action.HasHotKey() )
        {
            int key = action.GetHotKey() & ~MD_MODIFIER_MASK;
            int mod = action.GetHotKey() & MD_MODIFIER_MASK;
            int flags = wxACCEL_NORMAL;
            wxMenuItem* item = FindChildItem( id );

            if( item )
            {
                switch( mod )
                {
                    case MD_ALT:    flags = wxACCEL_ALT;    break;
                    case MD_CTRL:   flags = wxACCEL_CTRL;   break;
                    case MD_SHIFT:  flags = wxACCEL_SHIFT;  break;
                }

                wxAcceleratorEntry accel( flags, key, id, item );
                item->SetAccel( &accel );
            }
        }
    }
}


void CONTEXT_MENU::onMenuEvent( wxMenuEvent& aEvent )
{
    OPT_TOOL_EVENT evt;
+7 −0
Original line number Diff line number Diff line
@@ -332,6 +332,12 @@ void TOOL_MANAGER::RunAction( const TOOL_ACTION& aAction, bool aNow, void* aPara
}


void TOOL_MANAGER::UpdateHotKeys()
{
    m_actionMgr->UpdateHotKeys();
}


bool TOOL_MANAGER::invokeTool( TOOL_BASE* aTool )
{
    wxASSERT( aTool != NULL );
@@ -723,6 +729,7 @@ void TOOL_MANAGER::SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
    m_view = aView;
    m_viewControls = aViewControls;
    m_editFrame = aFrame;
    m_actionMgr->UpdateHotKeys();
}


Loading