-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathVirtualTrees.Actions.pas
328 lines (268 loc) · 9.2 KB
/
VirtualTrees.Actions.pas
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
unit VirtualTrees.Actions;
interface
uses
System.Classes,
System.Actions,
Vcl.Controls,
Vcl.ActnList,
VirtualTrees.Types,
VirtualTrees.BaseTree;
type
TVirtualTreeAction = class(TCustomAction)
strict private
fTree: TBaseVirtualTree; // Member variable for the property "Control"
fTreeAutoDetect: Boolean; // True if a potential Virtual TreeView should be detected automatically, false if a specific Tree was assigned to the property "Tree"
fOnAfterExecute: TNotifyEvent; // Member variable for the OnAfterExecute event
function GetSelectedOnly: Boolean; // Setter for the property "SelectedOnly"
procedure SetSelectedOnly(const Value: Boolean); // Getter for the property "SelectedOnly"
strict protected
fFilter: TVirtualNodeStates; // Apply only of nodes which match these states
procedure SetControl(Value: TBaseVirtualTree); // Setter for the property "Control"
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure DoAfterExecute; virtual;// Fires the event "OnAfterExecute"
property SelectedOnly: Boolean read GetSelectedOnly write SetSelectedOnly default False;
public
function HandlesTarget(Target: TObject): Boolean; override;
procedure UpdateTarget(Target: TObject); override;
procedure ExecuteTarget(Target: TObject); override;
published
constructor Create(AOwner: TComponent); override;
function Update: Boolean; override;
property Control: TBaseVirtualTree read fTree write SetControl;
property OnAfterExecute: TNotifyEvent read fOnAfterExecute write fOnAfterExecute; // Executed after the action was performed
property Caption;
property Enabled;
property HelpContext;
property HelpType;
property Hint;
property ImageIndex;
property ShortCut;
property SecondaryShortCuts;
property Visible;
property OnHint;
end;
TVirtualTreePerItemAction = class(TVirtualTreeAction)
strict private
fOnBeforeExecute: TNotifyEvent;
fOldCursor: TCursor;
strict protected
fToExecute: TVTGetNodeProc; // method which is executed per item to perform this action
procedure DoBeforeExecute();
procedure DoAfterExecute(); override;// Fires the event "OnAfterExecute"
public
constructor Create(AOwner: TComponent); override;
procedure ExecuteTarget(Target: TObject); override;
published
property OnBeforeExecute: TNotifyEvent read fOnBeforeExecute write fOnBeforeExecute;
end;
// A standard action which checkmarks nodes in a virtual treeview
TVirtualTreeCheckAll = class(TVirtualTreePerItemAction)
protected
fDesiredCheckState: TCheckState;
public
constructor Create(AOwner: TComponent); override;
published
property SelectedOnly;
property OnUpdate;
end;
// A standard action which unchecks nodes in a virtual treeview
TVirtualTreeUncheckAll = class(TVirtualTreeCheckAll)
public
constructor Create(AOwner: TComponent); override;
end;
TVirtualTreeSelectAll = class(TVirtualTreeAction)
public
procedure UpdateTarget(Target: TObject); override;
procedure ExecuteTarget(Target: TObject); override;
end;
// Base class for actions that are applied to selected nodes only
TVirtualTreeForSelectedAction = class(TVirtualTreeAction)
public
constructor Create(AOwner: TComponent); override;
end;
TVirtualTreeCopy = class(TVirtualTreeForSelectedAction)
public
procedure ExecuteTarget(Target: TObject); override;
end;
TVirtualTreeCut = class(TVirtualTreeForSelectedAction)
public
procedure ExecuteTarget(Target: TObject); override;
end;
TVirtualTreePaste = class(TVirtualTreeForSelectedAction)
public
procedure ExecuteTarget(Target: TObject); override;
end;
TVirtualTreeDelete = class(TVirtualTreeForSelectedAction)
public
procedure ExecuteTarget(Target: TObject); override;
end;
procedure Register;
implementation
uses
WinApi.Windows,
Vcl.Forms;
procedure Register;
begin
RegisterActions('VirtualTree', [TVirtualTreeCheckAll, TVirtualTreeUncheckAll, TVirtualTreeSelectAll, TVirtualTreeCopy, TVirtualTreeCut, TVirtualTreePaste, TVirtualTreeDelete], nil);
end;
{ TVirtualTreeAction }
constructor TVirtualTreeAction.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fTree := nil;
fFilter := [];
fOnAfterExecute := nil;
fTreeAutoDetect := True;
end;
function TVirtualTreeAction.GetSelectedOnly: Boolean;
begin
exit(TVirtualNodeState.vsSelected in fFilter);
end;
procedure TVirtualTreeAction.SetSelectedOnly(const Value: Boolean);
begin
if Value then
Include(fFilter, TVirtualNodeState.vsSelected)
else
Exclude(fFilter, TVirtualNodeState.vsSelected);
end;
procedure TVirtualTreeAction.DoAfterExecute;
begin
if Assigned(fOnAfterExecute) then
fOnAfterExecute(Self);
end;
function TVirtualTreeAction.HandlesTarget(Target: TObject): Boolean;
begin
Result := (Target is TBaseVirtualTree);
end;
function TVirtualTreeAction.Update(): Boolean;
begin
Result := inherited;
// If an OnUpdate event handler is assigned, TBasicAction.Update() will return True and so TBasicAction.UpdateTarget() will not be called.
// We would then end up with Control == nil. So trigger update of action manually.
if Result and Assigned(OnUpdate) then
SendAppMessage(CM_ACTIONUPDATE, 0, LPARAM(Self))
end;
procedure TVirtualTreeAction.UpdateTarget(Target: TObject);
begin
if fTreeAutoDetect and (Target is TBaseVirtualTree) then
fTree := (Target as TBaseVirtualTree);
Enabled := Assigned(Control) and not Control.IsEmpty and (not SelectedOnly or (Control.SelectedCount > 0))
end;
procedure TVirtualTreeAction.ExecuteTarget(Target: TObject);
begin
DoAfterExecute();
end;
procedure TVirtualTreeAction.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FTree) then
FTree := nil;
end;
procedure TVirtualTreeAction.SetControl(Value: TBaseVirtualTree);
begin
if Value <> fTree then begin
fTree := Value;
if Assigned(fTree) then begin
fTree.FreeNotification(Self);// register Self as a component that should be notified when fTree is about to be destroyed.
end;//if
// Do not update the target of this action if it wa set explicitely by the developer
fTreeAutoDetect := not Assigned(fTree);
end;//if
end;
{ TVirtualTreePerItemAction }
constructor TVirtualTreePerItemAction.Create(AOwner: TComponent);
begin
inherited;
fToExecute := nil;
fOnBeforeExecute := nil;
fOldCursor := crNone;
end;
procedure TVirtualTreePerItemAction.DoAfterExecute;
begin
inherited;
if fOldCursor <> crNone then
Screen.Cursor := fOldCursor;
end;
procedure TVirtualTreePerItemAction.DoBeforeExecute;
begin
if Screen.Cursor <> crHourGlass then begin
fOldCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
end;//if
if Assigned(fOnBeforeExecute) then
fOnBeforeExecute(Self);
end;
procedure TVirtualTreePerItemAction.ExecuteTarget(Target: TObject);
begin
DoBeforeExecute();
Control.BeginUpdate();
try
Control.IterateSubtree(nil, Self.fToExecute, nil, fFilter, true);
finally
Control.EndUpdate();
DoAfterExecute();
end;
end;
{ TVirtualTreeCheckAll }
constructor TVirtualTreeCheckAll.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Hint := 'Check all items in the list';
Caption := 'Check &All';
fDesiredCheckState := csCheckedNormal;
fToExecute := procedure(Sender: TBaseVirtualTree; Node: PVirtualNode; Data: Pointer; var Abort: Boolean)
begin
if not Control.CheckState[Node].IsDisabled then
Control.CheckState[Node] := fDesiredCheckState;
end;
end;
{ TVirtualTreeUncheckAll }
constructor TVirtualTreeUncheckAll.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Hint := 'Uncheck all items in the list';
Caption := '&Uncheck All';
fDesiredCheckState := csUncheckedNormal;
end;
{ TVirtualStringSelectAll }
procedure TVirtualTreeSelectAll.UpdateTarget(Target: TObject);
begin
Inherited;
//Enabled := Enabled and (toMultiSelect in Control.TreeOptions.SelectionOptions) // TreeOptions is protected :-(
end;
procedure TVirtualTreeSelectAll.ExecuteTarget(Target: TObject);
begin
Control.SelectAll(False);
inherited;
end;
{ TVirtualTreeForSelectedAction }
constructor TVirtualTreeForSelectedAction.Create(AOwner: TComponent);
begin
inherited;
SelectedOnly := True;
end;
{ TVirtualTreeCopy }
procedure TVirtualTreeCopy.ExecuteTarget(Target: TObject);
begin
Control.CopyToClipboard();
Inherited;
end;
{ TVirtualTreeCut }
procedure TVirtualTreeCut.ExecuteTarget(Target: TObject);
begin
Control.CutToClipboard();
Inherited;
end;
{ TVirtualTreePaste }
procedure TVirtualTreePaste.ExecuteTarget(Target: TObject);
begin
Control.PasteFromClipboard();
Inherited;
end;
{ TVirtualTreeDelete }
procedure TVirtualTreeDelete.ExecuteTarget(Target: TObject);
begin
Control.DeleteSelectedNodes();
Inherited;
end;
end.