source: trunk/Packages/Common/Forms/FormKeyShortcuts.pas

Last change on this file was 178, checked in by chronos, 3 months ago
File size: 4.0 KB
Line 
1unit FormKeyShortcuts;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
7 ComCtrls, ActnList, LCLProc, Menus, FormEx, Generics.Collections, ImgList,
8 StdCtrls, ListViewSort;
9
10type
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
37implementation
38
39{$R *.lfm}
40
41resourcestring
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
54procedure TFormKeyShortcuts.FormShow(Sender: TObject);
55var
56 I: Integer;
57begin
58 ListViewSort1.List.Clear;
59 for I := 0 to SourceComponents.Count - 1 do
60 LoadFromComponent(SourceComponents[I]);
61 ListViewSort1.Refresh;
62end;
63
64procedure TFormKeyShortcuts.ListView1Data(Sender: TObject; Item: TListItem);
65begin
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;
80end;
81
82procedure TFormKeyShortcuts.ListView1KeyPress(Sender: TObject; var Key: char);
83begin
84 if Key = #13 then MenuItem1Click(Sender);
85end;
86
87function TFormKeyShortcuts.ListViewSort1CompareItem(Item1, Item2: TObject
88 ): Integer;
89begin
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;
99end;
100
101procedure TFormKeyShortcuts.FormCreate(Sender: TObject);
102begin
103 SourceComponents := TObjectList<TComponent>.Create;
104 SourceComponents.OwnsObjects := False;
105end;
106
107procedure TFormKeyShortcuts.FormDestroy(Sender: TObject);
108begin
109 FreeAndNil(SourceComponents);
110end;
111
112procedure TFormKeyShortcuts.MenuItem1Click(Sender: TObject);
113begin
114 if Assigned(ListView1.Selected) then
115 TAction(ListView1.Selected.Data).Execute;
116end;
117
118function TFormKeyShortcuts.GetImageList: TCustomImageList;
119begin
120 Result := ListView1.SmallImages;
121end;
122
123procedure TFormKeyShortcuts.SetImageList(AValue: TCustomImageList);
124begin
125 ListView1.SmallImages := AValue;
126end;
127
128procedure TFormKeyShortcuts.LoadFromComponent(C: TComponent);
129var
130 I: Integer;
131begin
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]);
140end;
141
142end.
143
Note: See TracBrowser for help on using the repository browser.