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