hotkeys_basic.cpp 23.8 KB
Newer Older
Wayne Stambaugh's avatar
Wayne Stambaugh committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2007 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2010-2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 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
 * 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
 */

26 27
/**
 * @file hotkeys_basic.cpp
28
 * @brief Some functions to handle hotkeys in KiCad
29
 */
30

31 32 33 34 35 36 37 38 39 40
#include <fctsys.h>
#include <appl_wxstruct.h>
#include <hotkeys_basic.h>
#include <id.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <wxstruct.h>
#include <macros.h>
#include <dialog_hotkeys_editor.h>
41
#include <menus_helpers.h>
CHARRAS's avatar
CHARRAS committed
42

43 44
#include <wx/apptrait.h>
#include <wx/stdpaths.h>
45
#include <wx/tokenzr.h>
46

47
#define HOTKEYS_CONFIG_KEY wxT( "Keys" )
48

49 50 51 52 53 54
wxString g_CommonSectionTag( wxT( "[common]" ) );
wxString g_SchematicSectionTag( wxT( "[eeschema]" ) );
wxString g_LibEditSectionTag( wxT( "[libedit]" ) );
wxString g_BoardEditorSectionTag( wxT( "[pcbnew]" ) );
wxString g_ModuleEditSectionTag( wxT( "[footprinteditor]" ) );

55

56
/* Class to handle hotkey commnands. hotkeys have a default value
57 58
 * This class allows the real key code changed by user from a key code list
 * file.
59
 */
60

61
EDA_HOTKEY::EDA_HOTKEY( const wxChar* infomsg, int idcommand, int keycode, int idmenuevent )
62
{
charras's avatar
charras committed
63 64 65 66 67
    m_KeyCode = keycode;            // Key code (ascii value for ascii keys
    // or wxWidgets code for function key
    m_InfoMsg   = infomsg;          // info message.
    m_Idcommand = idcommand;        // internal id for the corresponding
    // command (see hotkey_id_commnand list)
68
    m_IdMenuEvent = idmenuevent;    // id to call the corresponding event
charras's avatar
charras committed
69
    // (if any) (see id.h)
70 71
}

72

73
EDA_HOTKEY::EDA_HOTKEY( const EDA_HOTKEY* base )
74 75 76 77 78 79 80 81
{
    m_KeyCode     = base->m_KeyCode;
    m_InfoMsg     = base->m_InfoMsg;
    m_Idcommand   = base->m_Idcommand;
    m_IdMenuEvent = base->m_IdMenuEvent;
}


82 83 84 85 86
EDA_HOTKEY_CLIENT_DATA::~EDA_HOTKEY_CLIENT_DATA()
{
}


87
/* class to handle the printable name and the keycode
88 89 90
 */
struct hotkey_name_descr
{
91
    const wxChar* m_Name;
charras's avatar
charras committed
92
    int           m_KeyCode;
93 94
};

95 96 97 98
/* table giving the hotkey name from the hotkey code, for special keys
 * Note : when modifiers (ATL, SHIFT, CTRL) do not modify
 * the code of the key, do need to enter the modified key code
 * For instance wxT( "F1" ), WXK_F1 handle F1, AltF1, CtrlF1 ...
99 100 101
 * Key names are:
 *        "Space","Ctrl+Space","Alt+Space" or
 *      "Alt+A","Ctrl+F1", ...
102
 */
103
static struct hotkey_name_descr s_Hotkey_Name_List[] =
104
{
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    { wxT( "F1" ),           WXK_F1                                                   },
    { wxT( "F2" ),           WXK_F2                                                   },
    { wxT( "F3" ),           WXK_F3                                                   },
    { wxT( "F4" ),           WXK_F4                                                   },
    { wxT( "F5" ),           WXK_F5                                                   },
    { wxT( "F6" ),           WXK_F6                                                   },
    { wxT( "F7" ),           WXK_F7                                                   },
    { wxT( "F8" ),           WXK_F8                                                   },
    { wxT( "F9" ),           WXK_F9                                                   },
    { wxT( "F10" ),          WXK_F10                                                  },
    { wxT( "F11" ),          WXK_F11                                                  },
    { wxT( "F12" ),          WXK_F12                                                  },

    { wxT( "Esc" ),          WXK_ESCAPE                                               },
    { wxT( "Del" ),          WXK_DELETE                                               },
120
    { wxT( "Tab" ),          WXK_TAB                                                  },
121 122 123 124 125 126 127 128 129 130 131 132
    { wxT( "BkSp" ),         WXK_BACK                                                 },
    { wxT( "Ins" ),          WXK_INSERT                                               },

    { wxT( "Home" ),         WXK_HOME                                                 },
    { wxT( "End" ),          WXK_END                                                  },
    { wxT( "PgUp" ),         WXK_PAGEUP                                               },
    { wxT( "PgDn" ),         WXK_PAGEDOWN                                             },

    { wxT( "Up" ),           WXK_UP                                                   },
    { wxT( "Down" ),         WXK_DOWN                                                 },
    { wxT( "Left" ),         WXK_LEFT                                                 },
    { wxT( "Right" ),        WXK_RIGHT                                                },
charras's avatar
charras committed
133

134 135
    { wxT( "Space" ),        WXK_SPACE                                                },

charras's avatar
charras committed
136
    // Do not change this line: end of list
137
    { wxT( "" ),             0                                                        }
138 139
};

140 141 142 143
#define MODIFIER_CTRL wxT( "Ctrl+" )
#define MODIFIER_ALT  wxT( "Alt+" )
#define MODIFIER_SHIFT  wxT( "Shift+" )

144

145 146
/**
 * Function ReturnKeyNameFromKeyCode
147
 * return the key name from the key code
148 149
 * Only some wxWidgets key values are handled for function key ( see
 * s_Hotkey_Name_List[] )
150
 * @param aKeycode = key code (ascii value, or wxWidgets value for function keys)
151
 * @param aIsFound = a pointer to a bool to return true if found, or false. an be NULL default)
152 153
 * @return the key name in a wxString
 */
154
wxString ReturnKeyNameFromKeyCode( int aKeycode, bool* aIsFound )
155
{
156 157
    wxString keyname, modifier, fullkeyname;
    int      ii;
158
    bool     found = false;
159

160
    if( (aKeycode & GR_KB_CTRL) != 0 )
161
        modifier << MODIFIER_CTRL;
162

163
    if( (aKeycode & GR_KB_ALT) != 0 )
164
        modifier << MODIFIER_ALT;
165

166
    if( (aKeycode & GR_KB_SHIFT) != 0 )
167
        modifier << MODIFIER_SHIFT;
168

169
    aKeycode &= ~( GR_KB_CTRL | GR_KB_ALT | GR_KB_SHIFT );
170

171
    if( (aKeycode > ' ') && (aKeycode < 0x7F ) )
172
    {
173
        found   = true;
174
        keyname.Append( (wxChar)aKeycode );
175 176 177 178
    }
    else
    {
        for( ii = 0; ; ii++ )
179
        {
180 181 182 183 184
            if( s_Hotkey_Name_List[ii].m_KeyCode == 0 ) // End of list
            {
                keyname = wxT( "<unknown>" );
                break;
            }
185

186 187 188 189 190 191
            if( s_Hotkey_Name_List[ii].m_KeyCode == aKeycode )
            {
                keyname = s_Hotkey_Name_List[ii].m_Name;
                found   = true;
                break;
            }
192 193 194
        }
    }

195 196
    if( aIsFound )
        *aIsFound = found;
197

198 199
    fullkeyname = modifier + keyname;
    return fullkeyname;
200
}
201

202

203 204
/*
 * helper function use in AddHotkeyName to calculate an accelerator string
205 206
 * In some menus, accelerators do not perform exactly the same action as
 * the hotkey that perform a similar action.
207 208 209 210
 * this is usually the case when this action uses the current mouse position
 * for instance zoom action is ran from the F1 key or the Zoom menu.
 * a zoom uses the mouse position from a hot key and not from the menu
 * In this case, the accelerator if Shift+<hotkey>
211
 * But for many keys, the Shift modifier is not usable, and the accelerator is Alt+<hotkey>
212 213 214
 */
static void AddModifierToKey( wxString& aFullKey, const wxString & aKey )
{
215
#if 0       // set to 0 for new behavior, 1 for old
216 217
    aFullKey << wxT( " <" ) << aKey << wxT( ">" );
#else
218 219
    if( (aKey.Length() == 1) && (aKey[0] >= 'A')  && (aKey[0] <= 'Z'))
        // We can use Shift+<key> as accelerator ans <key> for hot key
220
        aFullKey << wxT( "\t" ) << MODIFIER_SHIFT << aKey;
221 222 223 224
    else
        // We must use Alt+<key> as accelerator ans <key> for hot key
        aFullKey << wxT( "\t" ) << MODIFIER_ALT << aKey;

225 226 227
#endif
}

228
/* AddHotkeyName
229
 * Add the key name from the Command id value ( m_Idcommand member value)
230
 *  aText = a wxString. returns aText + key name
231
 *  aList = pointer to a EDA_HOTKEY list of commands
232
 *  aCommandId = Command Id value
233 234
 *  aShortCutType = IS_HOTKEY to add <tab><keyname> (shortcuts in menus, same as hotkeys)
 *                  IS_ACCELERATOR to add <tab><Shift+keyname> (accelerators in menus, not hotkeys)
235
 *                  IS_COMMENT to add <spaces><(keyname)> mainly in tool tips
236
 *  Return a wxString (aTest + key name) if key found or aText without modification
237
 */
238
wxString AddHotkeyName( const wxString& aText, EDA_HOTKEY** aList,
239
                        int aCommandId, HOTKEY_ACTION_TYPE aShortCutType )
240
{
241
    wxString msg = aText;
242
    wxString keyname;
243

244 245
    if( aList )
        keyname = ReturnKeyNameFromCommandId( aList, aCommandId );
246 247

    if( !keyname.IsEmpty() )
248
    {
249 250 251 252 253
        switch( aShortCutType )
        {
            case IS_HOTKEY:
                msg << wxT( "\t" ) << keyname;
                break;
254

255 256 257
            case IS_ACCELERATOR:
                AddModifierToKey( msg, keyname );
                break;
258

259 260 261 262
            case IS_COMMENT:
                msg << wxT( " (" ) << keyname << wxT( ")" );
                break;
        }
263
    }
264

265 266 267
    return msg;
}

268

269
/* AddHotkeyName
270
 * Add the key name from the Command id value ( m_Idcommand member value)
271
 *  aText = a wxString. returns aText + key name
272
 *  aList = pointer to a EDA_HOTKEY_CONFIG DescrList of commands
273
 *  aCommandId = Command Id value
274 275 276
 *  aShortCutType = IS_HOTKEY to add <tab><keyname> (active shortcuts in menus)
 *                  IS_ACCELERATOR to add <tab><Shift+keyname> (active accelerators in menus)
 *                  IS_COMMENT to add <spaces><(keyname)>
277
 * Return a wxString (aText + key name) if key found or aText without modification
278
 */
279 280 281 282
wxString AddHotkeyName( const wxString&           aText,
                        struct EDA_HOTKEY_CONFIG* aDescList,
                        int                       aCommandId,
                        HOTKEY_ACTION_TYPE        aShortCutType )
283
{
284 285 286
    wxString     msg = aText;
    wxString     keyname;
    EDA_HOTKEY** List;
287

288
    if( aDescList )
289
    {
290
        for( ; aDescList->m_HK_InfoList != NULL; aDescList++ )
291
        {
292 293
            List    = aDescList->m_HK_InfoList;
            keyname = ReturnKeyNameFromCommandId( List, aCommandId );
294

295 296
            if( !keyname.IsEmpty() )
            {
297 298 299 300 301
                switch( aShortCutType )
                {
                    case IS_HOTKEY:
                        msg << wxT( "\t" ) << keyname;
                        break;
302

303 304 305
                    case IS_ACCELERATOR:
                        AddModifierToKey( msg, keyname );
                        break;
306

307 308 309 310
                    case IS_COMMENT:
                        msg << wxT( " (" ) << keyname << wxT( ")" );
                        break;
                }
311 312
                break;
            }
313 314 315 316 317 318 319
        }
    }

    return msg;
}


320 321
/**
 * Function ReturnKeyNameFromCommandId
322
 * return the key name from the Command id value ( m_Idcommand member value)
323
 * @param aList = pointer to a EDA_HOTKEY list of commands
324
 * @param aCommandId = Command Id value
325 326
 * @return the key name in a wxString
 */
327
wxString ReturnKeyNameFromCommandId( EDA_HOTKEY** aList, int aCommandId )
328 329 330
{
    wxString keyname;

331
    for( ; *aList != NULL; aList++ )
332
    {
333
        EDA_HOTKEY* hk_decr = *aList;
334

335
        if( hk_decr->m_Idcommand == aCommandId )
336 337 338 339 340 341 342 343 344 345
        {
            keyname = ReturnKeyNameFromKeyCode( hk_decr->m_KeyCode );
            break;
        }
    }

    return keyname;
}


346 347
/**
 * Function ReturnKeyCodeFromKeyName
348 349
 * return the key code from its key name
 * Only some wxWidgets key values are handled for function key
350
 * @param keyname = wxString key name to find in s_Hotkey_Name_List[],
charras's avatar
charras committed
351
 *   like F2 or space or an usual (ascii) char.
352 353
 * @return the key code
 */
354
int ReturnKeyCodeFromKeyName( const wxString& keyname )
355
{
356 357
    int ii, keycode = 0;

358 359 360 361 362
    // Search for modifiers: Ctrl+ Alt+ and Shift+
    wxString key = keyname;
    int modifier = 0;
    while( 1 )
    {
363
        if( key.StartsWith( MODIFIER_CTRL ) )
364 365 366 367
        {
            modifier |= GR_KB_CTRL;
            key.Remove( 0, 5 );
        }
368
        else if( key.StartsWith( MODIFIER_ALT ) )
369 370 371 372
        {
            modifier |= GR_KB_ALT;
            key.Remove( 0, 4 );
        }
373
        else if( key.StartsWith( MODIFIER_SHIFT ) )
374 375 376 377 378
        {
            modifier |= GR_KB_SHIFT;
            key.Remove( 0, 6 );
        }
        else
379
        {
380
            break;
381
        }
382 383
    }

384
    if( (key.length() == 1) && (key[0] > ' ') && (key[0] < 0x7F) )
385 386 387
    {
        keycode = key[0];
        keycode += modifier;
388
        return keycode;
389 390
    }

391 392 393 394
    for( ii = 0; ; ii++ )
    {
        if( s_Hotkey_Name_List[ii].m_KeyCode == 0 )  // End of list reached
            break;
395

396
        if( key.CmpNoCase( s_Hotkey_Name_List[ii].m_Name ) == 0 )
397
        {
398
            keycode = s_Hotkey_Name_List[ii].m_KeyCode + modifier;
399 400 401 402 403
            break;
        }
    }

    return keycode;
404 405
}

406

407
/* DisplayHotkeyList
408
 * Displays the current hotkey list
409
 * aList = a EDA_HOTKEY_CONFIG list(Null terminated)
410
 */
411
void DisplayHotkeyList( EDA_DRAW_FRAME* aFrame, struct EDA_HOTKEY_CONFIG* aDescList )
412
{
413 414
    wxString     keyname;
    EDA_HOTKEY** List;
415

416
    wxString     msg = wxT( "<html><body bgcolor=\"#E2E2E2\">" );
417 418

    msg += wxT( "<H3>");
419 420
    msg += _("Hotkeys List");
    msg += wxT("</H3> <table cellpadding=\"0\">");
421

422
    for( ; aDescList->m_HK_InfoList != NULL; aDescList++ )
423
    {
424
        List = aDescList->m_HK_InfoList;
425

426 427
        for( ; *List != NULL; List++ )
        {
428 429
            EDA_HOTKEY* hk_decr = *List;

Andrey Fedorushkov's avatar
Andrey Fedorushkov committed
430 431 432 433 434 435
            if( !hk_decr->m_InfoMsg.Contains( wxT( "Macros" ) ) )
            {
                keyname = ReturnKeyNameFromKeyCode( hk_decr->m_KeyCode );
                msg    += wxT( "<tr><td>" ) + hk_decr->m_InfoMsg + wxT("</td>");
                msg    += wxT("<td><b>&nbsp;&nbsp;") + keyname + wxT( "</b></td></tr>" );
            }
436
        }
437 438
    }

439 440
    msg += wxT("</table></html></body>");
    DisplayHtmlInfoMessage( aFrame, _("Hotkeys List"), msg, wxSize(340, 750));
441 442
}

443

444 445
/**
 * Function GetDescriptorFromHotkey
446
 * Return a EDA_HOTKEY * pointer from a key code for OnHotKey() function
447
 * @param aKey = key code (ascii value, or wxWidgets value for function keys
448 449
 * @param aList = pointer to a EDA_HOTKEY list of commands
 * @return the corresponding EDA_HOTKEY pointer from the EDA_HOTKEY List
450
 */
451
EDA_HOTKEY* GetDescriptorFromHotkey( int aKey, EDA_HOTKEY** aList )
452
{
453
    for( ; *aList != NULL; aList++ )
454
    {
455 456
        EDA_HOTKEY* hk_decr = *aList;

457
        if( hk_decr->m_KeyCode == aKey )
458
            return hk_decr;
459 460
    }

461
    return NULL;
462 463
}

464

465 466
/**
 * Function WriteHotkeyConfig
467 468
 * Store the current hotkey list
 * It is stored using the standard wxConfig mechanism or a file.
469
 *
470
 * @param aDescList = pointer to the current hotkey list.
471
 * @param aFullFileName = a wxString pointer to a full file name.
472 473 474
 *  if NULL, use the standard wxConfig mechanism (default)
 * the output format is: shortcut  "key"  "function"
 * lines starting with # are comments
475
 */
476 477
int EDA_BASE_FRAME::WriteHotkeyConfig( struct EDA_HOTKEY_CONFIG* aDescList,
                                       wxString*                 aFullFileName )
478
{
479 480 481 482 483
    wxString msg;
    wxString keyname, infokey;

    msg = wxT( "$hotkey list\n" );

484
    /* Print the current hotkey list */
485
    EDA_HOTKEY** List;
486

487
    for( ; aDescList->m_HK_InfoList != NULL; aDescList++ )
488
    {
489
        if( aDescList->m_Comment )
490
        {
491 492 493
            msg += wxT( "# " );
            msg += wxString( aDescList->m_Comment );
            msg += wxT( "\n" );
494
        }
495

496 497 498 499
        msg += *aDescList->m_SectionTag;
        msg += wxT( "\n" );

        List = aDescList->m_HK_InfoList;
500

501 502
        for( ; *List != NULL; List++ )
        {
503
            EDA_HOTKEY* hk_decr = *List;
504
            msg    += wxT( "shortcut   " );
505 506 507 508 509 510
            keyname = ReturnKeyNameFromKeyCode( hk_decr->m_KeyCode );
            AddDelimiterString( keyname );
            infokey = hk_decr->m_InfoMsg;
            AddDelimiterString( infokey );
            msg += keyname + wxT( ":    " ) + infokey + wxT( "\n" );
        }
511 512
    }

513 514 515 516 517
    msg += wxT( "$Endlist\n" );

    if( aFullFileName )
    {
        FILE* file = wxFopen( *aFullFileName, wxT( "wt" ) );
518

519
        if( file )
520
        {
521
            fputs( TO_UTF8( msg ), file );
522
        }
523 524 525 526 527 528 529 530 531 532 533 534
        else
        {
            msg.Printf( wxT( "Unable to write file %s" ), GetChars( *aFullFileName ) );
            return 0;
        }
    }
    else
    {
        wxConfig config( m_FrameName );
        config.Write( HOTKEYS_CONFIG_KEY, msg );
    }

535
    return 1;
536 537 538
}


539 540
/**
 * Function ReadHotkeyConfigFile
541
 * Read an old configuration file (&ltfile&gt.key) and fill the current hotkey list
542
 * with hotkeys
543 544
 * @param aFilename = file name to read.
 * @param aDescList = current hotkey list descr. to initialise.
545
 */
546 547
int EDA_BASE_FRAME::ReadHotkeyConfigFile( const wxString&           aFilename,
                                          struct EDA_HOTKEY_CONFIG* aDescList )
548
{
549
    wxFile cfgfile( aFilename );
550

551 552 553 554 555 556 557 558 559 560
    /* get length */
    cfgfile.SeekEnd();
    wxFileOffset size = cfgfile.Tell();
    cfgfile.Seek( 0 );

    /* read data */
    char*    buffer = new char[size];
    cfgfile.Read( buffer, size );

    wxString data( buffer, wxConvUTF8 );
561

562 563
    /* parse */
    ParseHotkeyConfig( data, aDescList );
564

565 566 567 568 569 570
    /* cleanup */
    delete buffer;
    cfgfile.Close();
    return 1;
}

571
void ReadHotkeyConfig( const wxString& Appname, struct EDA_HOTKEY_CONFIG* aDescList )
572 573 574 575
{
    wxConfig config( Appname );

    if( !config.HasEntry( HOTKEYS_CONFIG_KEY ) )
576
    {
577 578
        // assume defaults are ok
        return;
579 580
    }

581 582 583 584 585 586
    wxString data;
    config.Read( HOTKEYS_CONFIG_KEY, &data );

    ParseHotkeyConfig( data, aDescList );
}

587
/* Function ReadHotkeyConfig
588
 * Read configuration data and fill the current hotkey list with hotkeys
589
 * aDescList is the current hotkey list descr. to initialize.
590
 */
591
int EDA_BASE_FRAME::ReadHotkeyConfig( struct EDA_HOTKEY_CONFIG* aDescList )
592 593 594 595 596
{
    ::ReadHotkeyConfig( m_FrameName, aDescList );
    return 1;
}

597

598
/* Function ParseHotkeyConfig
599 600
 * the input format is: shortcut  "key"  "function"
 * lines starting by # are ignored (comments)
601
 * lines like [xxx] are tags (example: [common] or [libedit] which identify sections
602
 */
603 604
void ParseHotkeyConfig( const wxString&           data,
                        struct EDA_HOTKEY_CONFIG* aDescList )
605 606 607
{
    /* Read the config */
    wxStringTokenizer tokenizer( data, L"\r\n", wxTOKEN_STRTOK );
608
    EDA_HOTKEY**      CurrentHotkeyList = 0;
609 610

    while( tokenizer.HasMoreTokens() )
611
    {
612 613 614 615
        wxString          line = tokenizer.GetNextToken();
        wxStringTokenizer lineTokenizer( line );

        wxString          line_type = lineTokenizer.GetNextToken();
616

617 618 619 620
        if( line_type[0]  == '#' ) //comment
            continue;

        if( line_type[0]  == '[' ) // A tag is found. search infos in list
621
        {
622
            CurrentHotkeyList = 0;
623
            EDA_HOTKEY_CONFIG* DList = aDescList;
624

625
            for( ; DList->m_HK_InfoList; DList++ )
626
            {
627
                if( *DList->m_SectionTag == line_type )
628 629 630 631 632 633 634 635
                {
                    CurrentHotkeyList = DList->m_HK_InfoList;
                    break;
                }
            }

            continue;
        }
636

637
        if( line_type == wxT( "$Endlist" ) )
638
            break;
639

640 641
        if( line_type != wxT( "shortcut" ) )
            continue;
642

643 644
        if( CurrentHotkeyList == NULL )
            continue;
645 646

        /* Get the key name */
647 648
        lineTokenizer.SetString( lineTokenizer.GetString(), L"\"\r\n\t ", wxTOKEN_STRTOK );
        wxString keyname = lineTokenizer.GetNextToken();
649

650
        wxString remainder = lineTokenizer.GetString();
651

652
        /* Get the command name */
653
        wxString fctname = remainder.AfterFirst( '\"' ).BeforeFirst( '\"' );
654

655
        /* search the hotkey in current hotkey list */
656
        for( EDA_HOTKEY** List = CurrentHotkeyList; *List != NULL; List++ )
657
        {
658 659
            EDA_HOTKEY* hk_decr = *List;

660
            if( hk_decr->m_InfoMsg == fctname )
661
            {
662
                int code = ReturnKeyCodeFromKeyName( keyname );
663

664 665
                if( code )
                    hk_decr->m_KeyCode = code;
666

667 668 669 670
                break;
            }
        }
    }
671
}
CHARRAS's avatar
CHARRAS committed
672 673


674 675
/**
 * Function ImportHotkeyConfigFromFile
676
 * Prompt the user for an old hotkey file to read, and read it.
677
 * @param aDescList = current hotkey list descr. to initialize.
CHARRAS's avatar
CHARRAS committed
678
 */
679
void EDA_BASE_FRAME::ImportHotkeyConfigFromFile( struct EDA_HOTKEY_CONFIG* aDescList )
CHARRAS's avatar
CHARRAS committed
680
{
681 682 683 684 685 686 687 688 689 690 691 692
    wxString ext  = DEFAULT_HOTKEY_FILENAME_EXT;
    wxString mask = wxT( "*." ) + ext;
    wxString path = wxGetCwd();
    wxString filename;

    filename = EDA_FileSelector( _( "Read Hotkey Configuration File:" ),
                                 path,
                                 filename,
                                 ext,
                                 mask,
                                 this,
                                 wxFD_OPEN,
693
                                 true );
694 695 696

    if( filename.IsEmpty() )
        return;
CHARRAS's avatar
CHARRAS committed
697

698 699
    ReadHotkeyConfigFile( filename, aDescList );
}
CHARRAS's avatar
CHARRAS committed
700

701

702 703
/**
 * Function ExportHotkeyConfigToFile
704
 * Prompt the user for an old hotkey file to read, and read it.
705
 * @param aDescList = current hotkey list descr. to initialize.
706
 */
707
void EDA_BASE_FRAME::ExportHotkeyConfigToFile( struct EDA_HOTKEY_CONFIG* aDescList )
708 709 710 711 712 713
{
    wxString ext  = DEFAULT_HOTKEY_FILENAME_EXT;
    wxString mask = wxT( "*." ) + ext;
    wxString path = wxGetCwd();
    wxString filename;

714
    filename = EDA_FileSelector( _( "Write Hotkey Configuration File:" ),
715 716 717 718 719
                                 path,
                                 filename,
                                 ext,
                                 mask,
                                 this,
720 721
                                 wxFD_OPEN | wxFD_SAVE,
                                 true );
722 723 724 725 726

    if( filename.IsEmpty() )
        return;

    WriteHotkeyConfig( aDescList, &filename );
CHARRAS's avatar
CHARRAS committed
727 728 729
}


730
/* add hotkey config options submenu to aMenu
CHARRAS's avatar
CHARRAS committed
731
 */
732
void AddHotkeyConfigMenu( wxMenu* aMenu )
CHARRAS's avatar
CHARRAS committed
733
{
734 735 736
    if( aMenu == NULL )
        return;

charras's avatar
charras committed
737
    wxMenu*     HotkeySubmenu = new wxMenu();
CHARRAS's avatar
CHARRAS committed
738

739
    /* List existing hotkey menu*/
740 741
    AddMenuItem( HotkeySubmenu,
                 ID_PREFERENCES_HOTKEY_SHOW_CURRENT_LIST,
Wayne Stambaugh's avatar
Wayne Stambaugh committed
742
                 _( "&List Current Keys" ),
743 744
                 _( "Displays the current hotkeys list and corresponding commands" ),
                 KiBitmap( info_xpm ) );
CHARRAS's avatar
CHARRAS committed
745

746
    /* Call hotkeys editor*/
747
    AddMenuItem( HotkeySubmenu, ID_PREFERENCES_HOTKEY_SHOW_EDITOR,
Wayne Stambaugh's avatar
Wayne Stambaugh committed
748
                 _( "&Edit Hotkeys" ),
749 750
                 _( "Call the hotkeys editor" ),
                 KiBitmap( editor_xpm ) );
751 752 753 754

    HotkeySubmenu->AppendSeparator();

    /* create hotkey file to export current hotkeys config */
755
    AddMenuItem( HotkeySubmenu, ID_PREFERENCES_HOTKEY_EXPORT_CONFIG,
Wayne Stambaugh's avatar
Wayne Stambaugh committed
756 757
                 _( "E&xport Hotkeys" ),
                 _( "Create a hotkey configuration file to export the current hotkeys" ),
758
                 KiBitmap( save_setup_xpm ) );
CHARRAS's avatar
CHARRAS committed
759

760
    /* Reload hotkey file */
761
    AddMenuItem( HotkeySubmenu, ID_PREFERENCES_HOTKEY_IMPORT_CONFIG,
Wayne Stambaugh's avatar
Wayne Stambaugh committed
762
                 _( "&Import Hotkeys" ),
763 764
                 _( "Load an existing hotkey configuration file" ),
                 KiBitmap( reload_xpm ) );
765 766

    /* Append HotkeySubmenu to menu */
767
    AddMenuItem( aMenu, HotkeySubmenu,
Wayne Stambaugh's avatar
Wayne Stambaugh committed
768
                 ID_PREFERENCES_HOTKEY_SUBMENU, _( "&Hotkeys" ),
769 770
                 _( "Hotkeys configuration and preferences" ),
                 KiBitmap( hotkeys_xpm ) );
771
}