File: editor_keymapping_options.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (738 lines) | stat: -rw-r--r-- 23,909 bytes parent folder | download | duplicates (2)
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
{
 ***************************************************************************
 *                                                                         *
 *   This source 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 code 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.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************
}
unit editor_keymapping_options;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  // LCL
  Forms, StdCtrls, ComCtrls, Controls, Dialogs, LCLType, LazUTF8, Menus, Buttons,
  Clipbrd, EditBtn, ExtCtrls,
  // LazControls
  TreeFilterEdit,
  // IdeIntf
  IDEOptionsIntf, IDEOptEditorIntf, IDEImagesIntf, SrcEditorIntf,
  // IDE
  EditorOptions, LazarusIDEStrConsts, editor_general_options,
  KeymapSchemeDlg, KeyMapping, IDECommands, KeyMapShortCutDlg;

type

  { TEditorKeymappingOptionsFrame }

  TEditorKeymappingOptionsFrame = class(TAbstractIDEOptionsEditor)
    BtnPanel: TPanel;
    ChooseSchemeButton: TBitBtn;
    ClearButton: TBitBtn;
    EditButton: TBitBtn;
    FilterEdit: TTreeFilterEdit;
    FindKeyButton: TBitBtn;
    CommandLabel: TLabel;
    ConflictsTreeView: TTreeView;
    KeyMapSplitter: TSplitter;
    KeyMapTreePanel: TPanel;
    SchemeLabel: TLabel;
    ResetKeyFilterBtn: TSpeedButton;
    TreeView: TTreeView;
    EditMenuItem: TMenuItem;
    ClearMenuItem: TMenuItem;
    PopupMenu1: TPopupMenu;
    procedure ClearMenuItemClick(Sender: TObject);
    procedure ConflictsTreeViewMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure EditButtonClick(Sender: TObject);
    procedure EditMenuItemClick(Sender: TObject);
    procedure ChooseSchemeButtonClick(Sender: TObject);
    procedure ClearButtonClick(Sender: TObject);
    procedure FilterEditAfterFilter(Sender: TObject);
    function FilterEditFilterItem(ItemData: Pointer; out Done: Boolean): Boolean;
    procedure FilterEditKeyPress(Sender: TObject; var {%H-}Key: char);
    procedure FindKeyButtonClick(Sender: TObject);
    procedure KeyMapSplitterMoved(Sender: TObject);
    procedure OnIdle(Sender: TObject; var {%H-}Done: Boolean);
    procedure ResetKeyFilterBtnClick(Sender: TObject);
    procedure TreeViewDblClick(Sender: TObject);
    procedure TreeViewKeyPress(Sender: TObject; var Key: char);
    procedure TreeViewSelectionChanged(Sender: TObject);
    procedure PopupMenu1Popup(Sender: TObject);
  private
    FDialog: TAbstractOptionsEditorDialog;
    FEditingKeyMap: TKeyCommandRelationList;
    FIdleConnected: boolean;
    KeyMapKeyFilter: TIDEShortCut;
    fModified: Boolean;
    function GeneralPage: TEditorGeneralOptionsFrame; inline;
    procedure FillKeyMappingTreeView;
    procedure EditCommandMapping(ANode: TTreeNode);
    procedure EditConflict(ANode: TTreeNode);
    procedure EditCommandRelation(ARelation: TKeyCommandRelation);
    procedure ClearCommandMapping(ANode: TTreeNode);
    procedure ClearConflict(ANode: TTreeNode);
    procedure ClearCommandRelation(ARelation: TKeyCommandRelation);
    function KeyMappingRelationToCaption(Index: Integer): String;
    function KeyMappingRelationToCaption(KeyRelation: TKeyCommandRelation): String;
    function KeyShortCutToCaption(const aKey: TKeyCommandRelation;
      const aShortCut: TIDEShortCut): string;
    function CaptionToKeyMappingRelation(aCaption: string): TKeyCommandRelation;
    procedure SetIdleConnected(AValue: boolean);
    procedure UpdateKeyFilterButton;
    procedure UpdateSchemeLabel;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    function GetTitle: String; override;
    procedure Setup(ADialog: TAbstractOptionsEditorDialog); override;
    procedure ReadSettings(AOptions: TAbstractIDEOptions); override;
    procedure WriteSettings(AOptions: TAbstractIDEOptions); override;
    class function SupportedOptionsClass: TAbstractIDEOptionsClass; override;
    procedure SelectByIdeCommand(ACmd: word);
    procedure UpdateTree;
    procedure UpdateConflictTree;
    property EditingKeyMap: TKeyCommandRelationList read FEditingKeyMap;
    property IdleConnected: boolean read FIdleConnected write SetIdleConnected;
  end;

implementation

{$R *.lfm}

var
  imgKeyCategory, imgKeyItem: Integer;

type

  { TKeyMapErrorsForm }

  TKeyMapErrorsForm = class(TForm)
    ListBox: TListBox;
    BackButton: TButton;
    ErrorsPopupMenu: TPopupMenu;
    CopyMenuItem: TMenuItem;
    procedure BackButtonClick(Sender: TObject);
    procedure CopyMenuItemClick(Sender: TObject);
  public
    constructor Create(AnOwner: TComponent); override;
  end;

constructor TKeyMapErrorsForm.Create(AnOwner: TComponent);
begin
  inherited CreateNew(AnOwner);
  SetBounds((Screen.Width - 410) div 2, (Screen.Height - 260) div 2, 400, 250);
  Caption := dlgKeyMappingErrors;

  ListBox := TListBox.Create(Self);
  with ListBox do
  begin
    Name := 'ListBox';
    Align := alTop;
    Parent := Self;
  end;

  BackButton := TButton.Create(Self);
  with BackButton do
  begin
    Name := 'BackButton';
    AutoSize := true;
    Anchors := [akLeft,akBottom];
    Parent := Self;
    AnchorParallel(akBottom,6,Self);
    AnchorParallel(akLeft,6,Self);
    Caption := dlgEdBack;
    OnClick := @BackButtonClick;
  end;

  ErrorsPopupMenu := TPopupMenu.Create(Self);
  ErrorsPopupMenu.Name := 'ErrorsPopupMenu';
  CopyMenuItem := TMenuItem.Create(Self);
  CopyMenuItem.Caption := lisCopyAllItemsToClipboard;
  CopyMenuItem.OnClick := @CopyMenuItemClick;
  CopyMenuItem.ImageIndex := IDEImages.LoadImage('laz_copy');
  ErrorsPopupMenu.Items.Add(CopyMenuItem);

  ListBox.AnchorToNeighbour(akBottom,6,BackButton);
  ListBox.PopupMenu := ErrorsPopupMenu;
end;

procedure TKeyMapErrorsForm.BackButtonClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

procedure TKeyMapErrorsForm.CopyMenuItemClick(Sender: TObject);
begin
  Clipboard.AsText := ListBox.Items.Text;
end;

{ TEditorKeymappingOptionsFrame }

constructor TEditorKeymappingOptionsFrame.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FEditingKeyMap := TKeyCommandRelationList.Create;
  EditButton.Enabled:=false;
  ClearButton.Enabled:=false;
  fModified:=False;
end;

destructor TEditorKeymappingOptionsFrame.Destroy;
begin
  IdleConnected:=false;
  FEditingKeyMap.Free;
  inherited Destroy;
end;

procedure TEditorKeymappingOptionsFrame.ChooseSchemeButtonClick(Sender: TObject);
var
  NewScheme: String;
begin
  NewScheme := EditorOpts.KeyMappingScheme;
  if ShowChooseKeySchemeDialog(NewScheme) = mrOk then begin
    EditorOpts.KeyMappingScheme := NewScheme;
    FEditingKeyMap.LoadScheme(NewScheme);
    FillKeyMappingTreeView;
    fModified:=False;
    UpdateSchemeLabel;
    UpdateConflictTree;
  end;
end;

procedure TEditorKeymappingOptionsFrame.EditButtonClick(Sender: TObject);
begin
  EditCommandMapping(TreeView.Selected)
end;

procedure TEditorKeymappingOptionsFrame.ClearButtonClick(Sender: TObject);
begin
  ClearCommandMapping(TreeView.Selected)
end;

function TEditorKeymappingOptionsFrame.FilterEditFilterItem(ItemData: Pointer;
  out Done: Boolean): Boolean;
var
  KeyRel: TKeyCommandRelation;
begin
  Done:=True;
  Result:=False;
  if TObject(ItemData) is TKeyCommandRelation then
  begin
    KeyRel:=TKeyCommandRelation(ItemData);      // Tree item is actual key command.
    Done:=False;
    Result:=KeyMapKeyFilter.Key1<>VK_UNKNOWN;
    if Result then begin                        // Key filter is defined.
      Done:=True;
      Result:=(CompareIDEShortCutKey1s(@KeyMapKeyFilter,@KeyRel.ShortcutA)=0)
           or (CompareIDEShortCutKey1s(@KeyMapKeyFilter,@KeyRel.ShortcutB)=0);
    end;
  end;
end;

procedure TEditorKeymappingOptionsFrame.FilterEditKeyPress(Sender: TObject; var Key: char);
begin
  ResetKeyFilterBtnClick(Nil);
end;

procedure TEditorKeymappingOptionsFrame.FindKeyButtonClick(Sender: TObject);
var
  ShortCutDialog: TShortCutDialog;
begin
  ShortCutDialog := TShortCutDialog.Create(nil);
  try
    ShortCutDialog.ShowSecondary:=False;
    ShortCutDialog.ShowSequence:=False;
    ShortCutDialog.Caption:=lisChooseAKey;
    ShortCutDialog.PrimaryShortCut := KeyMapKeyFilter;
    if ShortCutDialog.ShowModal = mrOK then begin
      KeyMapKeyFilter := ShortCutDialog.PrimaryShortCut;
      UpdateKeyFilterButton;
      FilterEdit.Filter:='';       // Allow only one of the filters to be active.
      FilterEdit.InvalidateFilter;
    end;
  finally
    ShortCutDialog.Free;
  end;
end;

procedure TEditorKeymappingOptionsFrame.KeyMapSplitterMoved(Sender: TObject);
begin
  TreeView.Update;
  ConflictsTreeView.Update;
end;

procedure TEditorKeymappingOptionsFrame.OnIdle(Sender: TObject;
  var Done: Boolean);
begin
  IdleConnected:=false;
  UpdateConflictTree;
end;

procedure TEditorKeymappingOptionsFrame.ResetKeyFilterBtnClick(Sender: TObject);
begin
  KeyMapKeyFilter.Key1 := VK_UNKNOWN;
  KeyMapKeyFilter.Key2 := VK_UNKNOWN;
  UpdateKeyFilterButton;           // Allow only one of the filters to be active.
  FilterEdit.InvalidateFilter;
end;

procedure TEditorKeymappingOptionsFrame.TreeViewDblClick(Sender: TObject);
var
  P: TPoint;
  ANode: TTreeNode;
begin
  P := TreeView.ScreenToClient(Mouse.CursorPos);
  ANode := TreeView.GetNodeAt(P.X, P.Y);
  if (ANode<>nil) and (ANode.Data<>nil) and (TObject(ANode.Data) is TKeyCommandRelation) then
    EditCommandMapping(ANode);
end;

procedure TEditorKeymappingOptionsFrame.TreeViewKeyPress(Sender: TObject; var Key: char);
begin
  if (Key = char(VK_RETURN)) and (TreeView.Selected<>nil) then
    EditCommandMapping(TreeView.Selected);
end;

procedure TEditorKeymappingOptionsFrame.TreeViewSelectionChanged(Sender: TObject);
var
  ANode: TTreeNode;
begin
  ANode := TreeView.Selected;
  EditButton.Enabled:=
    (ANode<>nil) and (ANode.Data<>nil) and (TObject(ANode.Data) is TKeyCommandRelation);
  ClearButton.Enabled:=EditButton.Enabled;
end;

procedure TEditorKeymappingOptionsFrame.PopupMenu1Popup(Sender: TObject);
var
  ANode: TTreeNode;
  pop: TPopupMenu;
begin
  pop := Sender as TPopupMenu;
  if pop.PopupComponent = TreeView then begin
    ANode := TreeView.Selected;
    EditMenuItem.Enabled:=
      (ANode<>nil) and (ANode.Data<>nil) and (TObject(ANode.Data) is TKeyCommandRelation);
  end else if pop.PopupComponent=ConflictsTreeView then begin
    ANode:=ConflictsTreeView.Selected;
    EditMenuItem.Enabled := (ANode<>nil) and (CaptionToKeyMappingRelation(ANode.Text)<>nil);
  end else
    EditMenuItem.Enabled := False;
  ClearMenuItem.Enabled := EditMenuItem.Enabled;
end;

procedure TEditorKeymappingOptionsFrame.EditMenuItemClick(Sender: TObject);
begin
  if PopupMenu1.PopupComponent=TreeView then
    EditCommandMapping(TreeView.Selected)
  else
    EditConflict(ConflictsTreeView.Selected);
end;

procedure TEditorKeymappingOptionsFrame.ClearMenuItemClick(Sender: TObject);
begin
  if PopupMenu1.PopupComponent=TreeView then
    ClearCommandMapping(TreeView.Selected)
  else
    ClearConflict(ConflictsTreeView.Selected);
end;

procedure TEditorKeymappingOptionsFrame.ConflictsTreeViewMouseDown(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  Node: TTreeNode;
begin
  if (Button=mbLeft) and (ssDouble in Shift) then begin
    Node:=ConflictsTreeView.GetNodeAt(X,Y);
    EditConflict(Node);
  end;
end;

function TEditorKeymappingOptionsFrame.GeneralPage: TEditorGeneralOptionsFrame; inline;
begin
  Result := TEditorGeneralOptionsFrame(FDialog.FindEditor(TEditorGeneralOptionsFrame));
end;

function TEditorKeymappingOptionsFrame.GetTitle: String;
begin
  Result := dlgKeyMapping;
end;

procedure TEditorKeymappingOptionsFrame.Setup(ADialog: TAbstractOptionsEditorDialog);
begin
  FDialog := ADialog;
  ChooseSchemeButton.Caption := lisEdOptsLoadAScheme;
  FindKeyButton.Caption := lisFindKeyCombination;
  CommandLabel.Caption := lisSelectedCommandsMapping;
  EditButton.Caption := lisEdit;
  ClearButton.Caption := lisClear;
  EditMenuItem.Caption := lisEdit;
  ClearMenuItem.Caption := lisClear;

  TreeView.Images := IDEImages.Images_16;
  ConflictsTreeView.Images := IDEImages.Images_16;
  imgKeyCategory := IDEImages.LoadImage('item_keyboard');
  imgKeyItem := IDEImages.LoadImage('item_character');
  IDEImages.AssignImage(ChooseSchemeButton, 'item_keyboard'); // keymapcategory
  IDEImages.AssignImage(FindKeyButton, 'menu_search_find');
  IDEImages.AssignImage(EditButton, 'laz_edit');
  IDEImages.AssignImage(ClearButton, 'menu_clean');
  PopupMenu1.Images := IDEImages.Images_16;
  EditMenuItem.ImageIndex := IDEImages.LoadImage('laz_edit');
  ClearMenuItem.ImageIndex := IDEImages.LoadImage('menu_clean');

  IDEImages.AssignImage(ResetKeyFilterBtn, ResBtnListFilter);
  ResetKeyFilterBtn.Enabled := not IDEShortCutEmpty(KeyMapKeyFilter);

//  FillKeyMappingTreeView;    ... Done in ReadSettings.
//  UpdateSchemeLabel;
end;

procedure TEditorKeymappingOptionsFrame.ReadSettings(AOptions: TAbstractIDEOptions);
var
  i: integer;
begin
  with AOptions as TEditorOptions do
    FEditingKeyMap.Assign(KeyMap);
  FillKeyMappingTreeView;
  UpdateSchemeLabel;
  with GeneralPage do
    for i := Low(PreviewEdits) to High(PreviewEdits) do
      if PreviewEdits[i] <> nil then
        FEditingKeyMap.AssignTo(PreviewEdits[i].KeyStrokes, TSourceEditorWindowInterface);
  IdleConnected:=true;
end;

procedure TEditorKeymappingOptionsFrame.WriteSettings(AOptions: TAbstractIDEOptions);
begin
  with AOptions as TEditorOptions do
    KeyMap.Assign(FEditingKeyMap);
end;

class function TEditorKeymappingOptionsFrame.SupportedOptionsClass: TAbstractIDEOptionsClass;
begin
  Result := TEditorOptions;
end;

procedure TEditorKeymappingOptionsFrame.FillKeyMappingTreeView;
var
  i, j: Integer;
  NewCategoryNode, NewKeyNode: TTreeNode;
  CurCategory: TIDECommandCategory;
  CurKeyRelation: TKeyCommandRelation;
  ChildNodeIndex: Integer;
  CategoryNodeIndex: Integer;
  ItemCaption: String;
begin
  with TreeView do
  begin
    BeginUpdate;
    CategoryNodeIndex:=0;
    for i := 0 to FEditingKeyMap.CategoryCount - 1 do
    begin
      CurCategory := FEditingKeyMap.Categories[i];
      if Items.TopLvlCount > CategoryNodeIndex then
      begin
        NewCategoryNode := Items.TopLvlItems[CategoryNodeIndex];
        NewCategoryNode.Text := CurCategory.Description;
        NewCategoryNode.Data := CurCategory;
      end
      else
        NewCategoryNode := Items.AddObject(Nil, CurCategory.Description, CurCategory);
      NewCategoryNode.ImageIndex := imgKeyCategory;
      NewCategoryNode.SelectedIndex := NewCategoryNode.ImageIndex;
      ChildNodeIndex:=0;
      for j := 0 to CurCategory.Count - 1 do
      begin
        CurKeyRelation := TKeyCommandRelation(CurCategory[j]);
        ItemCaption:=KeyMappingRelationToCaption(CurKeyRelation);
        if NewCategoryNode.Count > ChildNodeIndex then
        begin
          NewKeyNode := NewCategoryNode.Items[ChildNodeIndex];
          NewKeyNode.Text := ItemCaption;
          NewKeyNode.Data := CurKeyRelation;
        end
        else
          NewKeyNode := Items.AddChildObject(NewCategoryNode,ItemCaption, CurKeyRelation);
        NewKeyNode.ImageIndex := imgKeyItem;
        NewKeyNode.SelectedIndex := NewKeyNode.ImageIndex;
        inc(ChildNodeIndex);
      end;
      // delete unneeded ones
      while NewCategoryNode.Count > ChildNodeIndex do
        NewCategoryNode[NewCategoryNode.Count - 1].Delete;
      if NewCategoryNode.Count>0 then
        inc(CategoryNodeIndex);
    end;
    while Items.TopLvlCount > CategoryNodeIndex do
      Items.TopLvlItems[Items.TopLvlCount - 1].Delete;
    EndUpdate;
  end;
end;

procedure TEditorKeymappingOptionsFrame.FilterEditAfterFilter(Sender: TObject);
begin
  TreeView.MakeSelectionVisible;
end;

function TEditorKeymappingOptionsFrame.KeyMappingRelationToCaption(Index: Integer): String;
begin
  Result := KeyMappingRelationToCaption(FEditingKeyMap.Relations[Index]);
end;

function TEditorKeymappingOptionsFrame.KeyMappingRelationToCaption(
  KeyRelation: TKeyCommandRelation): String;
const
  MaxLength = 60;
begin
  with KeyRelation do
  begin
    Result := LocalizedName;
    if UTF8Length(Result)>MaxLength then
      Result := UTF8Copy(LocalizedName, 1, MaxLength)+'...';
    if Result <> '' then
      Result := Result + '  ';
    Result := Result + KeyValuesToCaptionStr(ShortcutA, ShortcutB, '[');
  end;
end;

function TEditorKeymappingOptionsFrame.KeyShortCutToCaption(
  const aKey: TKeyCommandRelation; const aShortCut: TIDEShortCut): string;
begin
  Result:=aKey.Category.Description+'/'
        +KeyMappingRelationToCaption(aKey);
end;

function TEditorKeymappingOptionsFrame.CaptionToKeyMappingRelation(
  aCaption: string): TKeyCommandRelation;
var
  c: Integer;
  aCategory: TIDECommandCategory;
  CatStr: String;
  i: Integer;
  aKey: TKeyCommandRelation;
  s: String;
begin
  for c:=0 to FEditingKeyMap.CategoryCount-1 do begin
    aCategory:=FEditingKeyMap.Categories[c];
    CatStr:=aCategory.Description+'/';
    if LeftStr(aCaption,length(CatStr))<>CatStr then continue;
    for i:=0 to aCategory.Count-1 do begin
      aKey:=TObject(aCategory[i]) as TKeyCommandRelation;
      s:=CatStr+EditorCommandToDescriptionString(aKey.Command);
      if LeftStr(aCaption,length(s))<>s then continue;
      Result:=aKey;
      exit;
    end;
  end;
  Result:=nil;
end;

procedure TEditorKeymappingOptionsFrame.SetIdleConnected(AValue: boolean);
begin
  if FIdleConnected=AValue then Exit;
  FIdleConnected:=AValue;
  if IdleConnected then
    Application.AddOnIdleHandler(@OnIdle)
  else
    Application.RemoveOnIdleHandler(@OnIdle);
end;

procedure TEditorKeymappingOptionsFrame.UpdateKeyFilterButton;
begin
  if IDEShortCutEmpty(KeyMapKeyFilter) then
    FindKeyButton.Caption := lisFindKeyCombination
  else
    FindKeyButton.Caption:=
      Format(lisFilter3, [KeyAndShiftStateToEditorKeyString(KeyMapKeyFilter)]);
  ResetKeyFilterBtn.Enabled := not IDEShortCutEmpty(KeyMapKeyFilter);
end;

procedure TEditorKeymappingOptionsFrame.UpdateSchemeLabel;
var
  s: String;
begin
  s:=lisNowLoadedScheme+EditorOpts.KeyMappingScheme;
  if fModified then
    s:=s+' (*)';
  SchemeLabel.Caption:=s;
end;

procedure TEditorKeymappingOptionsFrame.SelectByIdeCommand(ACmd: word);
var
  Node: TTreeNode;
begin
  Node := TreeView.Items.GetFirstNode;
  while node <> nil do begin
    if (node.Data <> nil) and (TObject(Node.Data) is TKeyCommandRelation) and
       (TKeyCommandRelation(Node.Data).Command = ACmd)
    then
      break;
    node := Node.GetNext;
  end;
  if node <> nil then begin
    Node.MakeVisible;
    Node.Selected := True;
  end;
end;

procedure TEditorKeymappingOptionsFrame.UpdateTree;
begin
  FillKeyMappingTreeView;
end;

procedure TEditorKeymappingOptionsFrame.UpdateConflictTree;
var
  ConflictCount: integer;
  Key1: TKeyCommandRelation;
  Key2: TKeyCommandRelation;

  procedure Check(const ShortCut1, ShortCut2: TIDEShortCut);
  // check if ShortCut1 hides ShortCut2
  var
    ConflictNode: TTreeNode;
    KeyNode: TTreeNode;
  begin
    if (ShortCut1.Key1=VK_UNKNOWN)
    or (ShortCut1.Key1<>ShortCut2.Key1)
    or (ShortCut1.Shift1<>ShortCut2.Shift1) then
      exit;    // first keys differ

    if (ShortCut1.Key2=VK_UNKNOWN) or (ShortCut2.Key2=VK_UNKNOWN)
    or ((ShortCut1.Key2=ShortCut2.Key2) and (ShortCut1.Shift2=ShortCut2.Shift2))
    then begin
      // conflict found, add node with a sub node for each key
      inc(ConflictCount);
      ConflictNode:=ConflictsTreeView.Items.Add(nil,srkmConflic+IntToStr(ConflictCount));
      ConflictNode.ImageIndex:=imgKeyItem;
      ConflictNode.StateIndex:=imgKeyItem;
      KeyNode:=ConflictsTreeView.Items.AddChild(ConflictNode,
                                          KeyShortCutToCaption(Key1,ShortCut1));
      KeyNode.ImageIndex := imgKeyItem;
      KeyNode.SelectedIndex := imgKeyItem;
      KeyNode:=ConflictsTreeView.Items.AddChild(ConflictNode,
                                          KeyShortCutToCaption(Key2,ShortCut2));
      KeyNode.ImageIndex := imgKeyItem;
      KeyNode.SelectedIndex := imgKeyItem;
      ConflictNode.Expanded:=true;
    end;
  end;

var
  i: Integer;
  j: Integer;
begin
  ConflictsTreeView.BeginUpdate;
  ConflictsTreeView.Items.Clear;

  ConflictCount:=0;
  for i:=0 to FEditingKeyMap.Count-1 do begin
    Key1:=FEditingKeyMap[i];
    for j:=i+1 to FEditingKeyMap.Count-1 do begin
      Key2:=FEditingKeyMap[j];
      if (not Key1.Category.ScopeIntersects(Key2.Category.Scope)) then
        continue;
      Check(Key1.ShortcutA,Key2.ShortcutA);
      Check(Key1.ShortcutA,Key2.ShortcutB);
      Check(Key1.ShortcutB,Key2.ShortcutA);
      Check(Key1.ShortcutB,Key2.ShortcutB);
    end;
  end;

  if ConflictsTreeView.Items.Count=0 then
    ConflictsTreeView.Items.Add(nil, lisThereAreNoConflictingKeys);

  ConflictsTreeView.EndUpdate;
end;

procedure TEditorKeymappingOptionsFrame.EditCommandMapping(ANode: TTreeNode);
begin
  if ANode=nil then exit;
  EditCommandRelation(TKeyCommandRelation(ANode.Data));
end;

procedure TEditorKeymappingOptionsFrame.EditConflict(ANode: TTreeNode);
var
  ARelation: TKeyCommandRelation;
begin
  if ANode=nil then exit;
  ARelation:=CaptionToKeyMappingRelation(ANode.Text);
  EditCommandRelation(ARelation);
end;

procedure TEditorKeymappingOptionsFrame.EditCommandRelation(
  ARelation: TKeyCommandRelation);
var
  i: Integer;
begin
  if ARelation=nil then exit;
  i := FEditingKeyMap.IndexOf(ARelation);
  if (i < 0) or (ShowKeyMappingEditForm(i, FEditingKeyMap) <> mrOk) then exit;
  FillKeyMappingTreeView;
  fModified:=True;
  UpdateSchemeLabel;
  with GeneralPage do
    for i := Low(PreviewEdits) to High(PreviewEdits) do
      if PreviewEdits[i] <> nil then
        FEditingKeyMap.AssignTo(PreviewEdits[i].KeyStrokes, TSourceEditorWindowInterface);
  UpdateConflictTree;
end;

procedure TEditorKeymappingOptionsFrame.ClearCommandMapping(ANode: TTreeNode);
begin
  if ANode=nil then exit;
  ClearCommandRelation(TKeyCommandRelation(ANode.Data));
end;

procedure TEditorKeymappingOptionsFrame.ClearConflict(ANode: TTreeNode);
var
  ARelation: TKeyCommandRelation;
begin
  if ANode=nil then exit;
  ARelation:=CaptionToKeyMappingRelation(ANode.Text);
  ClearCommandRelation(ARelation);
end;

procedure TEditorKeymappingOptionsFrame.ClearCommandRelation(
  ARelation: TKeyCommandRelation);
var
  i: Integer;
begin
  if ARelation=nil then exit;
  i := FEditingKeyMap.IndexOf(ARelation);
  if (i < 0) then exit;
  ARelation.ShortcutA := IDEShortCut(VK_UNKNOWN, []);
  ARelation.ShortcutB := IDEShortCut(VK_UNKNOWN, []);
  FillKeyMappingTreeView;
  fModified:=True;
  UpdateSchemeLabel;
  with GeneralPage do
    for i := Low(PreviewEdits) to High(PreviewEdits) do
      if PreviewEdits[i] <> nil then
        FEditingKeyMap.AssignTo(PreviewEdits[i].KeyStrokes, TSourceEditorWindowInterface);
  UpdateConflictTree;
end;

initialization
  RegisterIDEOptionsEditor(GroupEditor, TEditorKeymappingOptionsFrame, EdtOptionsKeys);
end.