| 1 | unit FormKeyShortcuts; | 
|---|
| 2 |  | 
|---|
| 3 | interface | 
|---|
| 4 |  | 
|---|
| 5 | uses | 
|---|
| 6 | Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, | 
|---|
| 7 | ComCtrls, ActnList, LCLProc, Menus, FormEx, Generics.Collections, ImgList, | 
|---|
| 8 | StdCtrls, ListViewSort; | 
|---|
| 9 |  | 
|---|
| 10 | type | 
|---|
| 11 |  | 
|---|
| 12 | { TFormKeyShortcuts } | 
|---|
| 13 |  | 
|---|
| 14 | TFormKeyShortcuts = class(TFormEx) | 
|---|
| 15 | ListView1: TListView; | 
|---|
| 16 | ListViewSort1: TListViewSort; | 
|---|
| 17 | MenuItem1: TMenuItem; | 
|---|
| 18 | PopupMenu1: TPopupMenu; | 
|---|
| 19 | procedure FormCreate(Sender: TObject); | 
|---|
| 20 | procedure FormDestroy(Sender: TObject); | 
|---|
| 21 | procedure FormShow(Sender: TObject); | 
|---|
| 22 | procedure ListView1Data(Sender: TObject; Item: TListItem); | 
|---|
| 23 | procedure ListView1KeyPress(Sender: TObject; var Key: char); | 
|---|
| 24 | function ListViewSort1CompareItem(Item1, Item2: TObject): Integer; | 
|---|
| 25 | procedure MenuItem1Click(Sender: TObject); | 
|---|
| 26 | private | 
|---|
| 27 | function GetImageList: TCustomImageList; | 
|---|
| 28 | procedure SetImageList(AValue: TCustomImageList); | 
|---|
| 29 | public | 
|---|
| 30 | SourceComponents: TObjectList<TComponent>; | 
|---|
| 31 | MainForm: TForm; | 
|---|
| 32 | procedure LoadFromComponent(C: TComponent); | 
|---|
| 33 | property ImageList: TCustomImageList read GetImageList write SetImageList; | 
|---|
| 34 | end; | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | implementation | 
|---|
| 38 |  | 
|---|
| 39 | {$R *.lfm} | 
|---|
| 40 |  | 
|---|
| 41 | resourcestring | 
|---|
| 42 | SMainForm = 'Main window'; | 
|---|
| 43 | SGlobal = 'Global'; | 
|---|
| 44 | // TODO: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41095 | 
|---|
| 45 | {$hints off} | 
|---|
| 46 | SAction = 'Action'; | 
|---|
| 47 | SWindow = 'Window'; | 
|---|
| 48 | SExecute = 'Execute'; | 
|---|
| 49 | SShortcut = 'Shortcut'; | 
|---|
| 50 | SKeyShortcuts = 'Key shortcuts'; | 
|---|
| 51 |  | 
|---|
| 52 | { TFormKeyShortcuts } | 
|---|
| 53 |  | 
|---|
| 54 | procedure TFormKeyShortcuts.FormShow(Sender: TObject); | 
|---|
| 55 | var | 
|---|
| 56 | I: Integer; | 
|---|
| 57 | begin | 
|---|
| 58 | ListViewSort1.List.Clear; | 
|---|
| 59 | for I := 0 to SourceComponents.Count - 1 do | 
|---|
| 60 | LoadFromComponent(SourceComponents[I]); | 
|---|
| 61 | ListViewSort1.Refresh; | 
|---|
| 62 | end; | 
|---|
| 63 |  | 
|---|
| 64 | procedure TFormKeyShortcuts.ListView1Data(Sender: TObject; Item: TListItem); | 
|---|
| 65 | begin | 
|---|
| 66 | if Item.Index < ListViewSort1.List.Count then | 
|---|
| 67 | with TAction(ListViewSort1.List[Item.Index]) do begin | 
|---|
| 68 | Item.Caption := Caption; | 
|---|
| 69 | Item.ImageIndex := ImageIndex; | 
|---|
| 70 | Item.Data := TAction(ListViewSort1.List[Item.Index]); | 
|---|
| 71 | with Item.SubItems do begin | 
|---|
| 72 | if ActionList.Owner = MainForm then Add(SMainForm) | 
|---|
| 73 | else if ActionList.Owner is TDataModule then Add(SGlobal) | 
|---|
| 74 | else if ActionList.Owner is TForm then Add(TForm(ActionList.Owner).Caption) | 
|---|
| 75 | else Add(ActionList.Name); | 
|---|
| 76 | if ShortCut <> 0 then Add(ShortCutToText(ShortCut)) | 
|---|
| 77 | else Add(''); | 
|---|
| 78 | end; | 
|---|
| 79 | end; | 
|---|
| 80 | end; | 
|---|
| 81 |  | 
|---|
| 82 | procedure TFormKeyShortcuts.ListView1KeyPress(Sender: TObject; var Key: char); | 
|---|
| 83 | begin | 
|---|
| 84 | if Key = #13 then MenuItem1Click(Sender); | 
|---|
| 85 | end; | 
|---|
| 86 |  | 
|---|
| 87 | function TFormKeyShortcuts.ListViewSort1CompareItem(Item1, Item2: TObject | 
|---|
| 88 | ): Integer; | 
|---|
| 89 | begin | 
|---|
| 90 | Result := 0; | 
|---|
| 91 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin | 
|---|
| 92 | with ListViewSort1 do begin | 
|---|
| 93 | if Column = 0 then Result := CompareString(TAction(Item1).Caption, TAction(Item2).Caption) | 
|---|
| 94 | else if Column = 2 then Result := CompareString(ShortCutToText(TAction(Item1).ShortCut), | 
|---|
| 95 | ShortCutToText(TAction(Item2).ShortCut)); | 
|---|
| 96 | end; | 
|---|
| 97 | if ListViewSort1.Order = soDown then Result := -Result; | 
|---|
| 98 | end else Result := 0; | 
|---|
| 99 | end; | 
|---|
| 100 |  | 
|---|
| 101 | procedure TFormKeyShortcuts.FormCreate(Sender: TObject); | 
|---|
| 102 | begin | 
|---|
| 103 | SourceComponents := TObjectList<TComponent>.Create; | 
|---|
| 104 | SourceComponents.OwnsObjects := False; | 
|---|
| 105 | end; | 
|---|
| 106 |  | 
|---|
| 107 | procedure TFormKeyShortcuts.FormDestroy(Sender: TObject); | 
|---|
| 108 | begin | 
|---|
| 109 | FreeAndNil(SourceComponents); | 
|---|
| 110 | end; | 
|---|
| 111 |  | 
|---|
| 112 | procedure TFormKeyShortcuts.MenuItem1Click(Sender: TObject); | 
|---|
| 113 | begin | 
|---|
| 114 | if Assigned(ListView1.Selected) then | 
|---|
| 115 | TAction(ListView1.Selected.Data).Execute; | 
|---|
| 116 | end; | 
|---|
| 117 |  | 
|---|
| 118 | function TFormKeyShortcuts.GetImageList: TCustomImageList; | 
|---|
| 119 | begin | 
|---|
| 120 | Result := ListView1.SmallImages; | 
|---|
| 121 | end; | 
|---|
| 122 |  | 
|---|
| 123 | procedure TFormKeyShortcuts.SetImageList(AValue: TCustomImageList); | 
|---|
| 124 | begin | 
|---|
| 125 | ListView1.SmallImages := AValue; | 
|---|
| 126 | end; | 
|---|
| 127 |  | 
|---|
| 128 | procedure TFormKeyShortcuts.LoadFromComponent(C: TComponent); | 
|---|
| 129 | var | 
|---|
| 130 | I: Integer; | 
|---|
| 131 | begin | 
|---|
| 132 | if C is TActionList then | 
|---|
| 133 | with TActionList(C) do begin | 
|---|
| 134 | for I := 0 to ActionCount - 1 do | 
|---|
| 135 | if Actions[I] is TAction then | 
|---|
| 136 | ListViewSort1.List.Add(Actions[I]); | 
|---|
| 137 | end; | 
|---|
| 138 | for I := 0 to C.ComponentCount - 1 do | 
|---|
| 139 | LoadFromComponent(C.Components[I]); | 
|---|
| 140 | end; | 
|---|
| 141 |  | 
|---|
| 142 | end. | 
|---|
| 143 |  | 
|---|