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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, j-p.charras at wanadoo.fr
* Copyright (C) 2010-2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 1992-2015 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
*/
/**
* @file hotkeys_basic.cpp
* @brief Some functions to handle hotkeys in KiCad
*/
#include <fctsys.h>
#include <kiface_i.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>
#include <menus_helpers.h>
#include <tool/tool_manager.h>
#include <wx/apptrait.h>
#include <wx/stdpaths.h>
#include <wx/tokenzr.h>
#define HOTKEYS_CONFIG_KEY wxT( "Keys" )
wxString g_CommonSectionTag( wxT( "[common]" ) );
/* Class to handle hotkey commands hotkeys have a default value
* This class allows the real key code changed by user from a key code list
* file.
*/
EDA_HOTKEY::EDA_HOTKEY( const wxChar* infomsg, int idcommand, int keycode, int idmenuevent )
{
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)
m_IdMenuEvent = idmenuevent; // id to call the corresponding event
// (if any) (see id.h)
}
EDA_HOTKEY::EDA_HOTKEY( const EDA_HOTKEY* base )
{
m_KeyCode = base->m_KeyCode;
m_InfoMsg = base->m_InfoMsg;
m_Idcommand = base->m_Idcommand;
m_IdMenuEvent = base->m_IdMenuEvent;
}
EDA_HOTKEY_CLIENT_DATA::~EDA_HOTKEY_CLIENT_DATA()
{
}
/* class to handle the printable name and the keycode
*/
struct hotkey_name_descr
{
const wxChar* m_Name;
int m_KeyCode;
};
/* 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 ...
* Key names are:
* "Space","Ctrl+Space","Alt+Space" or
* "Alt+A","Ctrl+F1", ...
*/
static struct hotkey_name_descr hotkeyNameList[] =
{
{ 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 },
{ wxT( "Tab" ), WXK_TAB },
{ 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 },
{ wxT( "Return" ), WXK_RETURN },
{ wxT( "Space" ), WXK_SPACE },
// Do not change this line: end of list
{ wxT( "" ), 0 }
};
// name of modifier keys.
// Note: the Ctrl key is Cmd key on Mac OS X.
// However, in wxWidgets defs, the key WXK_CONTROL is the Cmd key,
// so the code using WXK_CONTROL should be ok on any system.
// (on Mac OS X the actual Ctrl key code is WXK_RAW_CONTROL)
#ifdef __WXMAC__
#define USING_MAC_CMD
#endif
#ifdef USING_MAC_CMD
#define MODIFIER_CTRL wxT( "Cmd+" )
#else
#define MODIFIER_CTRL wxT( "Ctrl+" )
#endif
#define MODIFIER_CMD_MAC wxT( "Cmd+" )
#define MODIFIER_CTRL_BASE wxT( "Ctrl+" )
#define MODIFIER_ALT wxT( "Alt+" )
#define MODIFIER_SHIFT wxT( "Shift+" )
/**
* Function KeyNameFromKeyCode
* return the key name from the key code
* Only some wxWidgets key values are handled for function key ( see
* hotkeyNameList[] )
* @param aKeycode = key code (ascii value, or wxWidgets value for function keys)
* @param aIsFound = a pointer to a bool to return true if found, or false. an be NULL default)
* @return the key name in a wxString
*/
wxString KeyNameFromKeyCode( int aKeycode, bool* aIsFound )
{
wxString keyname, modifier, fullkeyname;
int ii;
bool found = false;
// Assume keycode of 0 is "unassigned"
if( aKeycode == 0 )
return wxT( "<unassigned>");
if( (aKeycode & GR_KB_CTRL) != 0 )
modifier << MODIFIER_CTRL;
if( (aKeycode & GR_KB_ALT) != 0 )
modifier << MODIFIER_ALT;
if( (aKeycode & GR_KB_SHIFT) != 0 )
modifier << MODIFIER_SHIFT;
aKeycode &= ~( GR_KB_CTRL | GR_KB_ALT | GR_KB_SHIFT );
if( (aKeycode > ' ') && (aKeycode < 0x7F ) )
{
found = true;
keyname.Append( (wxChar)aKeycode );
}
else
{
for( ii = 0; ; ii++ )
{
if( hotkeyNameList[ii].m_KeyCode == 0 ) // End of list
{
keyname = wxT( "<unknown>" );
break;
}
if( hotkeyNameList[ii].m_KeyCode == aKeycode )
{
keyname = hotkeyNameList[ii].m_Name;
found = true;
break;
}
}
}
if( aIsFound )
*aIsFound = found;
fullkeyname = modifier + keyname;
return fullkeyname;
}
/*
* helper function use in AddHotkeyName to calculate an accelerator string
* In some menus, accelerators do not perform exactly the same action as
* the hotkey that perform a similar action.
* 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>
* But for many keys, the Shift modifier is not usable, and the accelerator is Alt+<hotkey>
*/
static void AddModifierToKey( wxString& aFullKey, const wxString & aKey )
{
if( (aKey.Length() == 1) && (aKey[0] >= 'A') && (aKey[0] <= 'Z'))
// We can use Shift+<key> as accelerator and <key> for hot key
aFullKey << wxT( "\t" ) << MODIFIER_SHIFT << aKey;
else
// We must use Alt+<key> as accelerator and <key> for hot key
aFullKey << wxT( "\t" ) << MODIFIER_ALT << aKey;
}
/* AddHotkeyName
* Add the key name from the Command id value ( m_Idcommand member value)
* aText = a wxString. returns aText + key name
* aList = pointer to a EDA_HOTKEY list of commands
* aCommandId = Command Id value
* 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)
* IS_COMMENT to add <spaces><(keyname)> mainly in tool tips
* Return a wxString (aTest + key name) if key found or aText without modification
*/
wxString AddHotkeyName( const wxString& aText, EDA_HOTKEY** aList,
int aCommandId, HOTKEY_ACTION_TYPE aShortCutType )
{
wxString msg = aText;
wxString keyname;
if( aList )
keyname = KeyNameFromCommandId( aList, aCommandId );
if( !keyname.IsEmpty() )
{
switch( aShortCutType )
{
case IS_HOTKEY:
msg << wxT( "\t" ) << keyname;
break;
case IS_ACCELERATOR:
AddModifierToKey( msg, keyname );
break;
case IS_COMMENT:
msg << wxT( " (" ) << keyname << wxT( ")" );
break;
}
}
#ifdef USING_MAC_CMD
// On OSX, the modifier equivalent to the Ctrl key of PCs
// is the Cmd key, but in code we should use Ctrl as prefix in menus
msg.Replace( MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE );
#endif
return msg;
}
/* AddHotkeyName
* Add the key name from the Command id value ( m_Idcommand member value)
* aText = a wxString. returns aText + key name
* aList = pointer to a EDA_HOTKEY_CONFIG DescrList of commands
* aCommandId = Command Id value
* 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)>
* Return a wxString (aText + key name) if key found or aText without modification
*/
wxString AddHotkeyName( const wxString& aText,
struct EDA_HOTKEY_CONFIG* aDescList,
int aCommandId,
HOTKEY_ACTION_TYPE aShortCutType )
{
wxString msg = aText;
wxString keyname;
EDA_HOTKEY** list;
if( aDescList )
{
for( ; aDescList->m_HK_InfoList != NULL; aDescList++ )
{
list = aDescList->m_HK_InfoList;
keyname = KeyNameFromCommandId( list, aCommandId );
if( !keyname.IsEmpty() )
{
switch( aShortCutType )
{
case IS_HOTKEY:
msg << wxT( "\t" ) << keyname;
break;
case IS_ACCELERATOR:
AddModifierToKey( msg, keyname );
break;
case IS_COMMENT:
msg << wxT( " (" ) << keyname << wxT( ")" );
break;
}
break;
}
}
}
#ifdef USING_MAC_CMD
// On OSX, the modifier equivalent to the Ctrl key of PCs
// is the Cmd key, but in code we should use Ctrl as prefix in menus
msg.Replace( MODIFIER_CMD_MAC, MODIFIER_CTRL_BASE );
#endif
return msg;
}
/**
* Function KeyNameFromCommandId
* return the key name from the Command id value ( m_Idcommand member value)
* @param aList = pointer to a EDA_HOTKEY list of commands
* @param aCommandId = Command Id value
* @return the key name in a wxString
*/
wxString KeyNameFromCommandId( EDA_HOTKEY** aList, int aCommandId )
{
wxString keyname;
for( ; *aList != NULL; aList++ )
{
EDA_HOTKEY* hk_decr = *aList;
if( hk_decr->m_Idcommand == aCommandId )
{
keyname = KeyNameFromKeyCode( hk_decr->m_KeyCode );
break;
}
}
return keyname;
}
/**
* Function KeyCodeFromKeyName
* return the key code from its key name
* Only some wxWidgets key values are handled for function key
* @param keyname = wxString key name to find in hotkeyNameList[],
* like F2 or space or an usual (ascii) char.
* @return the key code
*/
int KeyCodeFromKeyName( const wxString& keyname )
{
int ii, keycode = 0;
// Search for modifiers: Ctrl+ Alt+ and Shift+
// Note: on Mac OSX, the Cmd key is equiv here to Ctrl
wxString key = keyname;
wxString prefix;
int modifier = 0;
while( 1 )
{
prefix.Empty();
if( key.StartsWith( MODIFIER_CTRL_BASE ) )
{
modifier |= GR_KB_CTRL;
prefix = MODIFIER_CTRL_BASE;
}
else if( key.StartsWith( MODIFIER_CMD_MAC ) )
{
modifier |= GR_KB_CTRL;
prefix = MODIFIER_CMD_MAC;
}
else if( key.StartsWith( MODIFIER_ALT ) )
{
modifier |= GR_KB_ALT;
prefix = MODIFIER_ALT;
}
else if( key.StartsWith( MODIFIER_SHIFT ) )
{
modifier |= GR_KB_SHIFT;
prefix = MODIFIER_SHIFT;
}
else
{
break;
}
if( !prefix.IsEmpty() )
key.Remove( 0, prefix.Len() );
}
if( (key.length() == 1) && (key[0] > ' ') && (key[0] < 0x7F) )
{
keycode = key[0];
keycode += modifier;
return keycode;
}
for( ii = 0; ; ii++ )
{
if( hotkeyNameList[ii].m_KeyCode == 0 ) // End of list reached
break;
if( key.CmpNoCase( hotkeyNameList[ii].m_Name ) == 0 )
{
keycode = hotkeyNameList[ii].m_KeyCode + modifier;
break;
}
}
return keycode;
}
/* DisplayHotkeyList
* Displays the current hotkey list
* aList = a EDA_HOTKEY_CONFIG list(Null terminated)
*/
#include <html_messagebox.h>
void DisplayHotkeyList( EDA_BASE_FRAME* aFrame, struct EDA_HOTKEY_CONFIG* aDescList )
{
wxString keyname;
wxString keymessage;
EDA_HOTKEY** list;
wxString msg = wxT( "<html><body bgcolor=\"#E2E2E2\">" );
msg += wxT( "<H3>" );
msg += _( "Hotkeys List" );
msg += wxT( "</H3> <table cellpadding=\"0\">" );
for( ; aDescList->m_HK_InfoList != NULL; aDescList++ )
{
list = aDescList->m_HK_InfoList;
for( ; *list != NULL; list++ )
{
EDA_HOTKEY* hk_decr = *list;
if( !hk_decr->m_InfoMsg.Contains( wxT( "Macros" ) ) )
{
keyname = KeyNameFromKeyCode( hk_decr->m_KeyCode );
keymessage = wxGetTranslation( hk_decr->m_InfoMsg );
// Some chars are modified, using html encoding, to be
// displayed by DisplayHtmlInfoMessage()
keyname.Replace( wxT( "<" ), wxT( "<" ) );
keyname.Replace( wxT( ">" ), wxT( ">" ) );
msg += wxT( "<tr><td>" ) + keymessage + wxT( "</td>" );
msg += wxT( "<td><b> " ) + keyname + wxT( "</b></td></tr>" );
}
}
}
msg += wxT( "</table></html></body>" );
#if 0 // Set to 1 to create a modal dialog (blocking)
DisplayHtmlInfoMessage( aFrame, _( "Hotkeys List" ), msg, wxSize( 340, 750 ) );
#else
// Create a non modal dialog, which shows the list of hotkeys until dismissed
// but does not block the parent window
HTML_MESSAGE_BOX *dlg = new HTML_MESSAGE_BOX( aFrame, _( "Hotkeys List" ),
wxDefaultPosition, wxSize( 340, 750 ) );
dlg->AddHTML_Text( msg );
dlg->Show( true );
#endif
}
/**
* Function GetDescriptorFromHotkey
* Return a EDA_HOTKEY * pointer from a key code for OnHotKey() function
* @param aKey = key code (ascii value, or wxWidgets value for function keys
* @param aList = pointer to a EDA_HOTKEY list of commands
* @return the corresponding EDA_HOTKEY pointer from the EDA_HOTKEY List
*/
EDA_HOTKEY* GetDescriptorFromHotkey( int aKey, EDA_HOTKEY** aList )
{
for( ; *aList != NULL; aList++ )
{
EDA_HOTKEY* hk_decr = *aList;
if( hk_decr->m_KeyCode == aKey )
return hk_decr;
}
return NULL;
}
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 )
{
wxString msg;
wxString keyname, infokey;
msg = wxT( "$hotkey list\n" );
// Print the current hotkey list
EDA_HOTKEY** list;
for( ; aDescList->m_HK_InfoList != NULL; aDescList++ )
{
if( aDescList->m_Title )
{
msg += wxT( "# " );
msg += *aDescList->m_Title;
msg += wxT( "\n" );
}
msg += *aDescList->m_SectionTag;
msg += wxT( "\n" );
list = aDescList->m_HK_InfoList;
for( ; *list != NULL; list++ )
{
EDA_HOTKEY* hk_decr = *list;
msg += wxT( "shortcut " );
keyname = KeyNameFromKeyCode( hk_decr->m_KeyCode );
AddDelimiterString( keyname );
infokey = hk_decr->m_InfoMsg;
AddDelimiterString( infokey );
msg += keyname + wxT( ": " ) + infokey + wxT( "\n" );
}
}
msg += wxT( "$Endlist\n" );
if( aFullFileName )
{
FILE* file = wxFopen( *aFullFileName, wxT( "wt" ) );
if( file )
{
fputs( TO_UTF8( msg ), file );
fclose( file );
}
else
{
msg.Printf( wxT( "Unable to write file %s" ), GetChars( *aFullFileName ) );
return 0;
}
}
else
{
wxFileName fn( GetName() );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
wxConfigBase* config = GetNewConfig( fn.GetFullPath() );
config->Write( HOTKEYS_CONFIG_KEY, msg );
delete config;
}
return 1;
}
int EDA_BASE_FRAME::ReadHotkeyConfigFile( const wxString& aFilename,
struct EDA_HOTKEY_CONFIG* aDescList )
{
wxFileName fn( aFilename );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
wxFile cfgfile( fn.GetFullPath() );
if( !cfgfile.IsOpened() ) // There is a problem to open file
return 0;
// 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 );
// parse
ParseHotkeyConfig( data, aDescList );
// cleanup
delete[] buffer;
cfgfile.Close();
return 1;
}
void ReadHotkeyConfig( const wxString& Appname, struct EDA_HOTKEY_CONFIG* aDescList )
{
wxFileName fn( Appname );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
wxConfigBase* config = GetNewConfig( fn.GetFullPath() );
if( !config->HasEntry( HOTKEYS_CONFIG_KEY ) )
{
// assume defaults are ok
return;
}
wxString data;
config->Read( HOTKEYS_CONFIG_KEY, &data );
delete config;
ParseHotkeyConfig( data, aDescList );
}
/* Function ReadHotkeyConfig
* Read configuration data and fill the current hotkey list with hotkeys
* aDescList is the current hotkey list descr. to initialize.
*/
int EDA_BASE_FRAME::ReadHotkeyConfig( struct EDA_HOTKEY_CONFIG* aDescList )
{
::ReadHotkeyConfig( GetName(), aDescList );
return 1;
}
/* Function ParseHotkeyConfig
* the input format is: shortcut "key" "function"
* lines starting by # are ignored (comments)
* lines like [xxx] are tags (example: [common] or [libedit] which identify sections
*/
void ParseHotkeyConfig( const wxString& data,
struct EDA_HOTKEY_CONFIG* aDescList )
{
// Read the config
wxStringTokenizer tokenizer( data, L"\r\n", wxTOKEN_STRTOK );
EDA_HOTKEY** CurrentHotkeyList = 0;
while( tokenizer.HasMoreTokens() )
{
wxString line = tokenizer.GetNextToken();
wxStringTokenizer lineTokenizer( line );
wxString line_type = lineTokenizer.GetNextToken();
if( line_type[0] == '#' ) //comment
continue;
if( line_type[0] == '[' ) // A tag is found. search infos in list
{
CurrentHotkeyList = 0;
EDA_HOTKEY_CONFIG* DList = aDescList;
for( ; DList->m_HK_InfoList; DList++ )
{
if( *DList->m_SectionTag == line_type )
{
CurrentHotkeyList = DList->m_HK_InfoList;
break;
}
}
continue;
}
if( line_type == wxT( "$Endlist" ) )
break;
if( line_type != wxT( "shortcut" ) )
continue;
if( CurrentHotkeyList == NULL )
continue;
// Get the key name
lineTokenizer.SetString( lineTokenizer.GetString(), L"\"\r\n\t ", wxTOKEN_STRTOK );
wxString keyname = lineTokenizer.GetNextToken();
wxString remainder = lineTokenizer.GetString();
// Get the command name
wxString fctname = remainder.AfterFirst( '\"' ).BeforeFirst( '\"' );
// search the hotkey in current hotkey list
for( EDA_HOTKEY** list = CurrentHotkeyList; *list != NULL; list++ )
{
EDA_HOTKEY* hk_decr = *list;
if( hk_decr->m_InfoMsg == fctname )
{
hk_decr->m_KeyCode = KeyCodeFromKeyName( keyname );
break;
}
}
}
}
void EDA_BASE_FRAME::ImportHotkeyConfigFromFile( EDA_HOTKEY_CONFIG* aDescList,
const wxString& aDefaultShortname )
{
wxString ext = DEFAULT_HOTKEY_FILENAME_EXT;
wxString mask = wxT( "*." ) + ext;
#if 0 // pass in the project dir as an argument
wxString path = wxPathOnly( Prj().GetProjectFullName() );
#else
wxString path = GetMruPath();
#endif
wxFileName fn( aDefaultShortname );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
wxString filename = EDA_FILE_SELECTOR( _( "Read Hotkey Configuration File:" ),
path,
fn.GetFullPath(),
ext,
mask,
this,
wxFD_OPEN,
true );
if( filename.IsEmpty() )
return;
ReadHotkeyConfigFile( filename, aDescList );
SetMruPath( wxFileName( filename ).GetPath() );
}
void EDA_BASE_FRAME::ExportHotkeyConfigToFile( EDA_HOTKEY_CONFIG* aDescList,
const wxString& aDefaultShortname )
{
wxString ext = DEFAULT_HOTKEY_FILENAME_EXT;
wxString mask = wxT( "*." ) + ext;
#if 0
wxString path = wxPathOnly( Prj().GetProjectFullName() );
#else
wxString path = GetMruPath();
#endif
wxFileName fn( aDefaultShortname );
fn.SetExt( DEFAULT_HOTKEY_FILENAME_EXT );
wxString filename = EDA_FILE_SELECTOR( _( "Write Hotkey Configuration File:" ),
path,
fn.GetFullPath(),
ext,
mask,
this,
wxFD_SAVE,
true );
if( filename.IsEmpty() )
return;
WriteHotkeyConfig( aDescList, &filename );
SetMruPath( wxFileName( filename ).GetPath() );
}
/* add hotkey config options submenu to aMenu
*/
void AddHotkeyConfigMenu( wxMenu* aMenu )
{
if( aMenu == NULL )
return;
wxMenu* HotkeySubmenu = new wxMenu();
// List existing hotkey menu
AddMenuItem( HotkeySubmenu,
ID_PREFERENCES_HOTKEY_SHOW_CURRENT_LIST,
_( "&List Current Keys" ),
_( "Displays the current hotkeys list and corresponding commands" ),
KiBitmap( info_xpm ) );
// Call hotkeys editor
AddMenuItem( HotkeySubmenu, ID_PREFERENCES_HOTKEY_SHOW_EDITOR,
_( "&Edit Hotkeys" ),
_( "Call the hotkeys editor" ),
KiBitmap( editor_xpm ) );
HotkeySubmenu->AppendSeparator();
// create hotkey file to export current hotkeys config
AddMenuItem( HotkeySubmenu, ID_PREFERENCES_HOTKEY_EXPORT_CONFIG,
_( "E&xport Hotkeys" ),
_( "Create a hotkey configuration file to export the current hotkeys" ),
KiBitmap( save_setup_xpm ) );
// Reload hotkey file
AddMenuItem( HotkeySubmenu, ID_PREFERENCES_HOTKEY_IMPORT_CONFIG,
_( "&Import Hotkeys" ),
_( "Load an existing hotkey configuration file" ),
KiBitmap( reload_xpm ) );
// Append HotkeySubmenu to menu
AddMenuItem( aMenu, HotkeySubmenu,
wxID_ANY, _( "&Hotkeys" ),
_( "Hotkeys configuration and preferences" ),
KiBitmap( hotkeys_xpm ) );
}
|