dialog_hotkeys_editor.cpp 7.3 KB
Newer Older
1 2 3 4
/**
 * @file dialog_hotkeys_editor.cpp
 */

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 1992-2010 Kicad Developers, see change_log.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
 * 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
 */

28 29
#include <algorithm>

30 31 32
#include <fctsys.h>
#include <appl_wxstruct.h>
#include <common.h>
33

34
#include <dialog_hotkeys_editor.h>
35

36
void InstallHotkeyFrame( EDA_DRAW_FRAME* parent, EDA_HOTKEY_CONFIG* hotkeys )
37 38 39 40 41 42 43 44 45 46 47 48
{
    HOTKEYS_EDITOR_DIALOG dialog( parent, hotkeys );

    int diag = dialog.ShowModal();
    if( diag == wxID_OK )
    {
        parent->ReCreateMenuBar();
        parent->Refresh();
    }
}


49 50
HOTKEYS_EDITOR_DIALOG::HOTKEYS_EDITOR_DIALOG( EDA_DRAW_FRAME*    parent,
                                              EDA_HOTKEY_CONFIG* hotkeys ) :
51 52 53 54
    HOTKEYS_EDITOR_DIALOG_BASE( parent )
{
    m_parent  = parent;
    m_hotkeys = hotkeys;
55
    m_curEditingRow = -1;
56 57 58 59 60 61 62 63 64 65 66 67 68

    m_table = new HotkeyGridTable( hotkeys );
    m_hotkeyGrid->SetTable( m_table, true );

    m_hotkeyGrid->AutoSizeColumn( 0 );
    m_hotkeyGrid->EnableDragGridSize( false );

    for( int i = 0; i < m_hotkeyGrid->GetNumberRows(); ++i )
    {
        m_hotkeyGrid->SetReadOnly( i, 0, true );
        m_hotkeyGrid->SetReadOnly( i, 1, true );
    }

69 70
    m_OKButton->SetDefault();
    m_hotkeyGrid->SetFocus();
71 72 73 74 75 76 77 78 79 80
    GetSizer()->SetSizeHints( this );
    Center();
}


void HOTKEYS_EDITOR_DIALOG::OnOKClicked( wxCommandEvent& event )
{
    /* edit the live hotkey table */
    HotkeyGridTable::hotkey_spec_vector& hotkey_vec = m_table->getHotkeys();

81
    EDA_HOTKEY_CONFIG*      section;
82 83 84

    for( section = m_hotkeys; section->m_HK_InfoList; section++ )
    {
85 86 87
        wxString     sectionTag = *section->m_SectionTag;

        EDA_HOTKEY** info_ptr;
88 89 90

        for( info_ptr = section->m_HK_InfoList; *info_ptr; info_ptr++ )
        {
91 92
            EDA_HOTKEY* info = *info_ptr;

93 94
            /* find the corresponding hotkey */
            HotkeyGridTable::hotkey_spec_vector::iterator i;
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
            for( i = hotkey_vec.begin(); i != hotkey_vec.end(); ++i )
            {
                if( i->first == sectionTag
                    && i->second
                    && i->second->m_Idcommand == info->m_Idcommand )
                {
                    info->m_KeyCode = i->second->m_KeyCode;
                    break;
                }
            }
        }
    }

    /* save the hotkeys */
    m_parent->WriteHotkeyConfig( m_hotkeys );

112
    EndModal( wxID_OK );
113 114 115 116 117
}


void HOTKEYS_EDITOR_DIALOG::CancelClicked( wxCommandEvent& event )
{
118
    EndModal( wxID_CANCEL );
119 120 121
}


122 123
/* Reinit the hotkeys to the initial state (remove all pending changes
 */
124 125 126
void HOTKEYS_EDITOR_DIALOG::UndoClicked( wxCommandEvent& event )
{
    m_table->RestoreFrom( m_hotkeys );
127
    m_curEditingRow = -1;
128

129 130 131
    for( int i = 0; i < m_hotkeyGrid->GetNumberRows(); ++i )
        SetHotkeyCellState( i, false );

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    m_hotkeyGrid->Refresh();
    Update();
}


void HOTKEYS_EDITOR_DIALOG::SetHotkeyCellState( int aRow, bool aHightlight )
{
    if( aHightlight )
    {
        m_hotkeyGrid->SetCellTextColour( aRow, 1, *wxRED );
        wxFont bold_font(m_hotkeyGrid->GetDefaultCellFont() );
        bold_font.SetWeight(wxFONTWEIGHT_BOLD);
        m_hotkeyGrid->SetCellFont( aRow, 1, bold_font );
    }
    else
    {
        m_hotkeyGrid->SetCellTextColour( aRow, 1, m_hotkeyGrid->GetDefaultCellTextColour() );
        m_hotkeyGrid->SetCellFont( aRow, 1, m_hotkeyGrid->GetDefaultCellFont() );
    }
}


154
void HOTKEYS_EDITOR_DIALOG::OnClickOnCell( wxGridEvent& event )
155 156 157 158 159
{
    if( m_curEditingRow != -1 )
        SetHotkeyCellState( m_curEditingRow, false );

    int newRow = event.GetRow();
160

161
    if( ( event.GetCol() != 1 ) || ( m_table->isHeader( newRow ) ) )
162 163 164 165 166 167 168 169 170 171 172 173
    {
        m_curEditingRow = -1;
    }
    else
    {
        m_curEditingRow = newRow;
        SetHotkeyCellState( m_curEditingRow, true );
    }
    m_hotkeyGrid->Refresh();
    Update();
}

174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
/** OnRightClickOnCell
 * If a cell is selected, display a list of keys for selection
 * The list is restricted to keys that cannot be entered:
 * tab, home ... because these keys have special functions in dialogs
 */
void HOTKEYS_EDITOR_DIALOG::OnRightClickOnCell( wxGridEvent& event )
{
    // Select the new cell if needed
    OnClickOnCell(event);

    if( m_curEditingRow == -1 )
        return;

    // Do not translate these key names. They are internally used.
    //ee hotkeys_basic.cpp
    #define C_COUNT 8
    wxString choices[C_COUNT] =
    {
        wxT("End")
        wxT("Tab"),
        wxT("Ctrl+Tab"),
        wxT("Alt+Tab"),
        wxT("Home"),
        wxT("Space"),
        wxT("Ctrl+Space"),
        wxT("Alt+Space"),
    };

203 204
    wxString keyname = wxGetSingleChoice( _( "Special keys only. For others keys, use keyboard" ),
                                          _( "Select a key" ), C_COUNT, choices, this );
205 206 207 208
    int key = ReturnKeyCodeFromKeyName( keyname );

    if( key == 0 )
        return;
209

210 211 212 213
    m_table->SetKeyCode( m_curEditingRow, key );
    m_hotkeyGrid->Refresh();
    Update();
}
214

215

216
void HOTKEYS_EDITOR_DIALOG::OnKeyPressed( wxKeyEvent& event )
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
{
    if( m_curEditingRow != -1 )
    {
        long key = event.GetKeyCode();

        switch( key )
        {
        case WXK_ESCAPE:
            SetHotkeyCellState( m_curEditingRow, false );
            m_curEditingRow = -1;
            break;

        default:
            if( event.ControlDown() )
                key |= GR_KB_CTRL;
232

233 234
            if( event.AltDown() )
                key |= GR_KB_ALT;
235

236 237 238 239 240 241 242 243
            if( event.ShiftDown() && (key > 256) )
                key |= GR_KB_SHIFT;

            // Remap Ctrl A (=1+GR_KB_CTRL) to Ctrl Z(=26+GR_KB_CTRL)
            // to GR_KB_CTRL+'A' .. GR_KB_CTRL+'Z'
            if( (key > GR_KB_CTRL) && (key <= GR_KB_CTRL+26) )
                key += ('A' - 1);

244
            if( key >= 'a' && key <= 'z' ) // convert to uppercase
245 246
                key = key + ('A' - 'a');

247
#if 0       // For debug only
248 249 250 251
            wxString msg;
            msg.Printf(wxT("key %X, keycode %X"),event.GetKeyCode(), key);
            wxMessageBox(msg);
#endif
252
            // See if this key code is handled in hotkeys names list
253 254
            bool exists;
            ReturnKeyNameFromKeyCode( key, &exists );
255

256
            if( !exists )   // not handled, see hotkeys_basic.cpp
257 258 259
            {
                wxMessageBox( _( "Hotkey code not handled" ) );
            }
260 261 262 263
            else
            {
                m_table->SetKeyCode( m_curEditingRow, key );
            }
264

265 266 267 268 269 270 271
            break;
        }
    }

    m_hotkeyGrid->Refresh();
    Update();
}