forked from pyscripter/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlgCustomShortcuts.pas
466 lines (402 loc) · 13.7 KB
/
dlgCustomShortcuts.pas
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
{-----------------------------------------------------------------------------
Unit Name: frmCustomShortcuts
Author: Kiriakos Vlahos
Purpose: Dialog and code to support IDE shortcut customization
History: Based on Delhpi Magazine article
-----------------------------------------------------------------------------}
unit dlgCustomShortcuts;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus, System.Actions, ActnList, ComCtrls, SpTBXControls,
Buttons, SpTBXEditors, dlgPyIDEBase, ExtCtrls,
SpTBXItem;
type
TActionProxyItem = class(TCollectionItem)
private
fSecondaryShortCuts: TShortCutList;
FShortCut: TShortCut;
fActionListName: string;
fActionName: string;
function IsSecondaryShortCutsStored: Boolean;
procedure SetSecondaryShortCuts(const Value: TCustomShortCutList);
function GetSecondaryShortCuts: TCustomShortCutList;
public
Category : string;
Caption : string;
Hint : string;
destructor Destroy; override;
published
property ActionListName : string read fActionListName write fActionListName;
property ActionName : string read fActionName write fActionName;
property ShortCut: TShortCut read FShortCut write FShortCut default 0;
property SecondaryShortCuts: TCustomShortCutList read GetSecondaryShortCuts
write SetSecondaryShortCuts stored IsSecondaryShortCutsStored;
end;
TActionListArray = array of TActionList;
TActionProxyCollection = class(TCollection)
constructor Create(ActionListArray : TActionListArray);
procedure ApplyShortCuts(ActionListArray : TActionListArray);
end;
TfrmCustomKeyboard = class(TPyIDEDlgBase)
gbDescription: TSpTBXGroupBox;
edNewShortcut: THotKey;
btnOK: TSpTBXButton;
btnCancel: TSpTBXButton;
btnHelp: TSpTBXButton;
btnAssign: TSpTBXButton;
btnRemove: TSpTBXButton;
lblNewShortcutKey: TSpTBXLabel;
lblCategories: TSpTBXLabel;
lblCommands: TSpTBXLabel;
lblCurrent: TSpTBXLabel;
lblAssignedTo: TSpTBXLabel;
lblCurrentKeys: TSpTBXLabel;
lblDescription: TSpTBXLabel;
lbCategories: TSpTBXListBox;
lbCommands: TSpTBXListBox;
lbCurrentKeys: TSpTBXListBox;
Bevel1: TBevel;
SpTBXPanel1: TSpTBXPanel;
procedure HelpButtonClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lbCategoriesClick(Sender: TObject);
procedure lbCommandsClick(Sender: TObject);
procedure btnAssignClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnRemoveClick(Sender: TObject);
procedure edNewShortcutChange(Sender: TObject);
procedure lbCurrentKeysClick(Sender: TObject);
private
procedure SetCategories;
procedure DoneItems;
procedure SelectItem(Idx: Integer);
function GetCurrentAction: TActionProxyItem;
procedure FillFunctionList;
procedure AssignKeysToActionProxy(var CurAction: TActionProxyItem);
{ Private declarations }
public
GotKey : Boolean;
Categories : TStringList;
FunctionList : TStringList;
KeyList : TStringList;
ActionProxyCollection : TActionProxyCollection;
procedure PrepActions(ActionListArray : TActionListArray);
function Execute(ActionListArray : TActionListArray) : Boolean;
property CurrentAction : TActionProxyItem
read GetCurrentAction;
{ Public declarations }
end;
var
frmCustomKeyboard: TfrmCustomKeyboard;
implementation
{$R *.DFM}
{ TfrmCustomKeyboard }
function TfrmCustomKeyboard.Execute(ActionListArray: TActionListArray) : Boolean;
begin
Result := False;
DoneItems;
PrepActions(ActionListArray);
if ShowModal = mrOk then begin
ActionProxyCollection.ApplyShortCuts(ActionListArray);
Result := True;
end;
DoneItems;
end;
procedure TfrmCustomKeyboard.AssignKeysToActionProxy(var CurAction: TActionProxyItem);
var
i: Integer;
begin
if lbCurrentKeys.Count > 0 then
CurAction.ShortCut := TShortCut(lbCurrentKeys.Items.Objects[0])
else
CurAction.ShortCut := 0;
{ Assign secondary shortcuts }
CurAction.SecondaryShortCuts.Clear;
for i := 1 to lbCurrentKeys.Count - 1 do
CurAction.SecondaryShortCuts.AddObject(lbCurrentKeys.Items[i],
lbCurrentKeys.Items.Objects[i]);
end;
procedure TfrmCustomKeyboard.FormCreate(Sender: TObject);
begin
inherited;
btnAssign.Enabled := False;
btnRemove.Enabled := False;
FunctionList := TStringList.Create;
FunctionList.Sorted := True;
FunctionList.Duplicates := dupIgnore;
KeyList := TStringList.Create;
KeyList.Sorted := True;
KeyList.Duplicates := dupIgnore;
end;
procedure TfrmCustomKeyboard.FormDestroy(Sender: TObject);
begin
KeyList.Free;
FunctionList.Free;
ActionProxyCollection.Free;
end;
procedure TfrmCustomKeyboard.FormShow(Sender: TObject);
begin
lbCategories.Items.Clear;
lbCommands.Items.Clear;
lbCurrentKeys.Items.Clear;
edNewShortCut.HotKey := 0;
SetCategories;
SelectItem(0);
lbCategories.ItemIndex := 0;
lbCategories.SetFocus;
end;
procedure TfrmCustomKeyboard.SetCategories;
begin
lbCategories.Items.Clear;
lbCategories.Items.AddStrings(FunctionList);
SelectItem(0);
end;
procedure TfrmCustomKeyboard.DoneItems;
var
i : Integer;
begin
for i := Pred(FunctionList.Count) downto 0 do begin
(FunctionList.Objects[i] as TStringList).Free;
FunctionList.Delete(i);
end;
end;
procedure TfrmCustomKeyboard.lbCategoriesClick(Sender: TObject);
begin
SelectItem(lbCategories.ItemIndex);
end;
procedure TfrmCustomKeyboard.SelectItem(Idx: Integer);
begin
edNewShortCut.HotKey := 0;
edNewShortcutChange(Self);
lbCurrentkeys.Items.Clear;
lbCommands.Items.Clear;
lblDescription.Caption := '';
lbCommands.Items.AddStrings(FunctionList.Objects[Idx] as TStrings);
btnRemove.Enabled := False;
end;
procedure TfrmCustomKeyboard.lbCommandsClick(Sender: TObject);
var
A : TActionProxyItem;
begin
if lbCommands.ItemIndex < 0 then Exit;
A := CurrentAction;
edNewShortCut.HotKey := 0;
edNewShortcutChange(Self);
lbCurrentKeys.Items.Clear;
lblDescription.Caption := GetLongHint(A.Hint);
if A.ShortCut <> 0 then
lbCurrentKeys.Items.AddObject(ShortCutToText(A.ShortCut), TObject(A.ShortCut));
lbCurrentKeys.Items.AddStrings(A.SecondaryShortCuts);
btnRemove.Enabled := False;
end;
procedure TfrmCustomKeyboard.btnAssignClick(Sender: TObject);
var
ShortCut : TShortCut;
CurAction : TActionProxyItem;
begin
if lbCommands.ItemIndex < 0 then Exit;
if edNewShortcut.HotKey <> 0 then begin
try
ShortCut := edNewShortcut.HotKey;
CurAction := CurrentAction;
if lbCurrentKeys.Items.IndexOf(ShortCutToText(edNewShortcut.HotKey)) < 0 then begin
{ show the keystroke }
lbCurrentKeys.Items.AddObject(ShortCutToText(edNewShortcut.HotKey), TObject(edNewShortcut.HotKey));
AssignKeysToActionProxy(CurAction);
{ track the keystroke assignment }
KeyList.Add(ShortCutToText(ShortCut) + '=' + CurAction.ActionName);
{ Update the lblAssignedTo}
edNewShortcutChange(Self);
end else begin
MessageBeep(MB_ICONEXCLAMATION);
end;
except
MessageBeep(MB_ICONEXCLAMATION);
edNewShortcut.SetFocus;
end;
end;
end;
function TfrmCustomKeyboard.GetCurrentAction: TActionProxyItem;
var
CatIdx, CmdIdx : Integer;
SL : TStringList;
begin
CatIdx := FunctionList.IndexOf(lbCategories.Items[lbCategories.ItemIndex]);
SL := FunctionList.Objects[CatIdx] as TStringList;
CmdIdx := SL.IndexOf(lbCommands.Items[lbCommands.ItemIndex]);
Result := (SL.Objects[CmdIdx] as TActionProxyItem);
end;
procedure TfrmCustomKeyboard.PrepActions(ActionListArray: TActionListArray);
begin
ActionProxyCollection := TActionProxyCollection.Create(ActionListArray);
FillFunctionList;
SetCategories;
end;
procedure TfrmCustomKeyboard.FillFunctionList;
var
i, j, Idx : Integer;
A : TActionProxyItem;
begin
for i := 0 to ActionProxyCollection.Count - 1 do begin
A := TActionProxyItem(ActionProxyCollection.Items[i]);
{ get category index }
Idx := FunctionList.IndexOf(A.Category);
{ if category doesn't already exist, add it }
if Idx < 0 then
Idx := FunctionList.AddObject(A.Category, TStringList.Create);
{ add keyboard function to list }
(FunctionList.Objects[Idx] as TStringList).AddObject(A.ActionName, A);
{ shortcut value already assigned }
if A.ShortCut <> 0 then begin
{ track the keystroke }
KeyList.Add(ShortCutToText(A.ShortCut) + '=' + A.ActionName);
end;
{ Deal with secondary shortcuts }
if A.IsSecondaryShortCutsStored then
for j := 0 to A.SecondaryShortCuts.Count - 1 do
KeyList.Add(ShortCutToText(A.SecondaryShortCuts.ShortCuts[j]) + '=' + A.ActionName);
end;
end;
procedure TfrmCustomKeyboard.btnRemoveClick(Sender: TObject);
var
CurAction : TActionProxyItem;
Index : integer;
begin
if lbCurrentKeys.ItemIndex < 0 then Exit;
CurAction := CurrentAction;
{ Remove shortcut from keylist }
Index := KeyList.IndexOf(lbCurrentKeys.Items[lbCurrentKeys.ItemIndex]
+ '=' + CurrentAction.ActionName);
if Index >= 0 then
KeyList.Delete(Index);
{ Remove shortcut from lbCurrentKeys }
lbCurrentKeys.Items.Delete(lbCurrentKeys.ItemIndex);
AssignKeysToActionProxy(CurAction);
{ Update the lblAssignedTo}
edNewShortcutChange(Self);
btnRemove.Enabled := False;
end;
procedure TfrmCustomKeyboard.edNewShortcutChange(Sender: TObject);
begin
if edNewShortCut.HotKey = 0 then begin
btnAssign.Enabled := False;
lblAssignedTo.Visible := False;
lblCurrent.Visible := False;
end
else begin
btnAssign.Enabled := True;
lblAssignedTo.Visible := True;
if KeyList.IndexOfName(ShortCutToText(edNewShortCut.HotKey)) > -1 then begin
lblCurrent.Visible := True;
lblAssignedTo.Caption := KeyList.Values[ShortCutToText(edNewShortCut.HotKey)];
end else begin
lblCurrent.Visible := False;
lblAssignedTo.Caption := '[Unassigned]';
end;
end;
end;
procedure TfrmCustomKeyboard.lbCurrentKeysClick(Sender: TObject);
begin
btnAssign.Enabled := False;
btnRemove.Enabled := True;
end;
{ TActionProxyItem }
procedure TActionProxyItem.SetSecondaryShortCuts(const Value: TCustomShortCutList);
begin
if FSecondaryShortCuts = nil then
FSecondaryShortCuts := TShortCutList.Create;
fSecondaryShortCuts.Assign(Value);
end;
function TActionProxyItem.IsSecondaryShortCutsStored: Boolean;
begin
Result := Assigned(FSecondaryShortCuts) and (FSecondaryShortCuts.Count > 0);
end;
destructor TActionProxyItem.Destroy;
begin
if Assigned(FSecondaryShortCuts) then
FreeAndNil(FSecondaryShortCuts);
inherited;
end;
function TActionProxyItem.GetSecondaryShortCuts: TCustomShortCutList;
begin
if FSecondaryShortCuts = nil then
FSecondaryShortCuts := TShortCutList.Create;
Result := FSecondaryShortCuts;
end;
{ TActionProxyCollection }
constructor TActionProxyCollection.Create(ActionListArray: TActionListArray);
var
i, j : integer;
Action : TCustomAction;
ActionList : TActionList;
ActionProxyItem : TActionProxyItem;
begin
inherited Create(TActionProxyItem);
for i := Low(ActionListArray) to High(ActionListArray) do begin
ActionList := ActionListArray[i];
for j := 0 to ActionList.ActionCount - 1 do begin
Action := ActionList.Actions[j] as TCustomAction;
ActionProxyItem := Add as TActionProxyItem;
ActionProxyItem.fActionListName := ActionList.Name;
ActionProxyItem.fActionName := Action.Name;
ActionProxyItem.FShortCut := Action.ShortCut;
ActionProxyItem.Category := Action.Category;
ActionProxyItem.Caption := Action.Caption;
ActionProxyItem.Hint := Action.Hint;
if Action.SecondaryShortCuts.Count > 0 then
ActionProxyItem.SecondaryShortCuts := Action.SecondaryShortCuts;
end;
end;
end;
function FindActionListByName(Name : string;
ActionListArray: TActionListArray) : TActionList;
var
i : integer;
begin
Result := nil;
for i := Low(ActionListArray) to High(ActionListArray) do
if ActionListArray[i].Name = Name then begin
Result := ActionListArray[i];
break;
end;
end;
function FindActionByName(Name : string; ActionList : TActionList): TCustomAction;
var
i : integer;
begin
Result := nil;
for i := 0 to ActionList.ActionCount - 1 do
if ActionList.Actions[i].Name = Name then begin
Result := ActionList.Actions[i] as TCustomAction;
break;
end;
end;
procedure TActionProxyCollection.ApplyShortCuts(
ActionListArray: TActionListArray);
var
i : integer;
ActionProxyItem : TActionProxyItem;
ActionList : TActionList;
Action : TCustomAction;
begin
for i := 0 to Count - 1 do begin
ActionProxyItem := Items[i] as TActionProxyItem;
ActionList := FindActionListByName(ActionProxyItem.ActionListName, ActionListArray);
if Assigned(ActionList) then begin
Action := FindActionByName(ActionProxyItem.ActionName, ActionList);
if Assigned(Action) then begin
Action.ShortCut := ActionProxyItem.ShortCut;
Action.SecondaryShortCuts.Clear;
if ActionProxyItem.IsSecondaryShortCutsStored then
Action.SecondaryShortCuts.Assign(ActionProxyItem.SecondaryShortCuts);
end;
end;
end;
end;
procedure TfrmCustomKeyboard.HelpButtonClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
end.