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

Last change on this file was 219, checked in by chronos, 5 days ago
  • Modified: Updated Common package.
  • Modified: Remove U prefix from unit names.
  • Modified: Use Gneeric.Collections instead of fgl.
  • Modified: Do not use global form variables.
File size: 4.1 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 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
39implementation
40
41{$R *.lfm}
42
43resourcestring
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
57procedure TFormKeyShortcuts.FormShow(Sender: TObject);
58var
59 I: Integer;
60begin
61 ListViewSort1.List.Clear;
62 for I := 0 to SourceComponents.Count - 1 do
63 LoadFromComponent(SourceComponents[I]);
64 ListViewSort1.Refresh;
65end;
66
67procedure TFormKeyShortcuts.ListView1Data(Sender: TObject; Item: TListItem);
68begin
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;
83end;
84
85procedure TFormKeyShortcuts.ListView1KeyPress(Sender: TObject; var Key: char);
86begin
87 if Key = #13 then MenuItem1Click(Sender);
88end;
89
90function TFormKeyShortcuts.ListViewSort1CompareItem(Item1, Item2: TObject
91 ): Integer;
92begin
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;
102end;
103
104procedure TFormKeyShortcuts.FormCreate(Sender: TObject);
105begin
106 SourceComponents := TObjectList<TComponent>.Create;
107 SourceComponents.OwnsObjects := False;
108end;
109
110procedure TFormKeyShortcuts.ButtonCloseClick(Sender: TObject);
111begin
112 Close;
113end;
114
115procedure TFormKeyShortcuts.FormDestroy(Sender: TObject);
116begin
117 FreeAndNil(SourceComponents);
118end;
119
120procedure TFormKeyShortcuts.MenuItem1Click(Sender: TObject);
121begin
122 if Assigned(ListView1.Selected) then
123 TAction(ListView1.Selected.Data).Execute;
124end;
125
126function TFormKeyShortcuts.GetImages: TCustomImageList;
127begin
128 Result := ListView1.SmallImages;
129end;
130
131procedure TFormKeyShortcuts.SetImages(AValue: TCustomImageList);
132begin
133 ListView1.SmallImages := AValue;
134end;
135
136procedure TFormKeyShortcuts.LoadFromComponent(C: TComponent);
137var
138 I: Integer;
139begin
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]);
148end;
149
150end.
151
Note: See TracBrowser for help on using the repository browser.