-
Notifications
You must be signed in to change notification settings - Fork 8
/
TMStruct.pas
661 lines (570 loc) · 16.9 KB
/
TMStruct.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
unit TMStruct;
{
Aestan Tray Menu
Made by Onno Broekmans; visit http://www.xs4all.nl/~broekroo/aetraymenu
for more information.
This work is hereby released into the Public Domain. To view a copy of the
public domain dedication, visit:
http://creativecommons.org/licenses/publicdomain/
or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,
California 94305, USA.
This unit contains some classes that are used for saving the settings
read from the configuration file.
}
{
NOTE:
Lots of code from this unit have been taken from the Inno Setup source code.
The original copyright notice for this code is:
Copyright (C) 1998-2002 Jordan Russell
Portions by Martijn Laan
}
interface
uses Classes, Windows, Contnrs, TMSrvCtrl;
type
TConfigSectionDirective = (
csImageList,
csServiceCheckInterval,
csTrayIconAllRunning,
csTrayIconSomeRunning,
csTrayIconNoneRunning,
csTrayIcon,
csServiceGlyphRunning,
csServiceGlyphPaused,
csServiceGlyphStopped,
csID,
csAboutHeader,
csAboutVersion,
csAboutTextFile,
csHtmlActions
);
TMessagesSectionDirective = (
msAllRunningHint,
msSomeRunningHint,
msNoneRunningHint
);
TMenuSectionDirective = (
mnAutoHotkeys,
mnAutoLineReduction,
mnBarBackPictureDrawStyle,
mnBarBackPictureHorzAlignment,
mnBarBackPictureOffsetX,
mnBarBackPictureOffsetY,
mnBarBackPicturePicture,
mnBarBackPictureTransparent,
mnBarBackPictureVertAlignment,
mnBarCaptionAlignment,
mnBarCaptionCaption,
mnBarCaptionDepth,
mnBarCaptionDirection,
mnBarCaptionFont,
mnBarCaptionHighlightColor,
mnBarCaptionOffsetY,
mnBarCaptionShadowColor,
mnBarPictureHorzAlignment,
mnBarPictureOffsetX,
mnBarPictureOffsetY,
mnBarPicturePicture,
mnBarPictureTransparent,
mnBarPictureVertAlignment,
mnBarBorder,
mnBarGradientEnd,
mnBarGradientStart,
mnBarGradientStyle,
mnBarSide,
mnBarSpace,
mnBarVisible,
mnBarWidth,
mnMenuFont,
mnSeparatorsAlignment,
mnSeparatorsFade,
mnSeparatorsFadeColor,
mnSeparatorsFadeWidth,
mnSeparatorsFlatLines,
mnSeparatorsFont,
mnSeparatorsGradientEnd,
mnSeparatorsGradientStart,
mnSeparatorsGradientStyle,
mnSeparatorsSeparatorStyle
);
TShowCmd = (
swNormal = SW_SHOWNORMAL,
swHidden = SW_HIDE,
swMaximized = SW_SHOWMAXIMIZED,
swMinimized = SW_SHOWMINNOACTIVE
);
TMenuItemType = (
mitItem,
mitSeparator,
mitSubMenu,
mitServiceSubMenu
);
TVarType = (
vtStatic,
vtRegistry,
vtEnvironment,
vtCmdLine,
vtPrompt
);
TVariableFlag = ( //see also VariableFlags
vfIsPath
);
TVariableFlags = set of TVariableFlag;
TActionFlag = ( //see also ActionFlags
afIgnoreErrors,
afWaitUntilTerminated,
afWaitUntilIdle
);
TActionFlags = set of TActionFlag;
THtmlWindowFlag = ( //see also HtmlWindowFlags
hwfMaximized,
hwfNoResize,
hwfNoScrollbars,
hwfEnableContextMenu,
hwfNoCloseButton,
hwfNoHeader,
hwfAlwaysOnTop
);
THtmlWindowFlags = set of THtmlWindowFlag;
TBuiltInAction = (
biaAbout,
biaExit,
biaControlPanelServices,
biaReadConfig,
biaCloseServices,
biaResetServices
);
TServiceAction = (
saStartResume,
saPause,
saStop,
saRestart
);
// TVariable = class
// public
// Name, Value: String;
// Flags: TVariableFlags;
// end;
TVariableBase = class
protected
FName: String;
FFlags: TVariableFlags;
function GetValue: String; virtual; abstract;
procedure SetValue(const AValue: String); virtual; abstract;
public
property Name: String read FName write FName;
property Value: String read GetValue write SetValue;
property Flags: TVariableFlags read FFlags write FFlags;
end;
TVariable = class(TVariableBase)
protected
FValue: String;
function GetValue: String; override;
procedure SetValue(const AValue: String); override;
end;
TPromptVariable = class(TVariableBase)
private
FText: String;
FCaption: String;
FDefaultValue: String;
protected
function GetValue: String; override;
procedure SetValue(const AValue: String); override;
public
property Caption: String read FCaption write FCaption;
property Text: String read FText write FText;
property DefaultValue: String read FDefaultValue write FDefaultValue;
end;
TTMAction = class
private
FFlags: TActionFlags;
public
property Flags: TActionFlags read FFlags write FFlags;
procedure ExecuteAction; virtual; abstract;
//Performs the action
function CanExecute: Boolean; virtual;
//Returns if the corresponding menu item should be enabled;
//the default implementation always returns true.
end; //TTMAction
TTMBuiltInAction = class(TTMAction)
private
FOnExecute: TNotifyEvent;
FBuiltInAction: TBuiltInAction;
public
property OnExecute: TNotifyEvent read FOnExecute write FOnExecute;
property BuiltInAction: TBuiltInAction read FBuiltInAction write FBuiltInAction;
constructor Create(ABuiltInAction: TBuiltInAction); virtual;
procedure ExecuteAction; override;
end; //TTMBuiltInAction
TTMRunAction = class(TTMAction)
private
FFileName: String;
FWorkingDir: String;
FParameters: String;
FShowCmd: TShowCmd;
FVariables: TObjectList;
public
property FileName: String read FFileName write FFileName;
property WorkingDir: String read FWorkingDir write FWorkingDir;
property Parameters: String read FParameters write FParameters;
property ShowCmd: TShowCmd read FShowCmd write FShowCmd;
property Variables: TObjectList read FVariables write FVariables;
constructor Create; virtual;
procedure ExecuteAction; override;
end; //TTMRunAction
TTMShellExecuteAction = class(TTMAction)
private
FFileName: String;
FWorkingDir: String;
FParameters: String;
FShowCmd: TShowCmd;
FVerb: String;
FVariables: TObjectList;
public
property FileName: String read FFileName write FFileName;
property WorkingDir: String read FWorkingDir write FWorkingDir;
property Parameters: String read FParameters write FParameters;
property ShowCmd: TShowCmd read FShowCmd write FShowCmd;
property Verb: String read FVerb write FVerb;
property Variables: TObjectList read FVariables write FVariables;
constructor Create; virtual;
procedure ExecuteAction; override;
end; //TTMShellExecuteAction
TTMServiceAction = class(TTMAction)
private
FService: TTMService;
FAction: TServiceAction;
public
property Service: TTMService read FService write FService;
property Action: TServiceAction read FAction write FAction;
procedure ExecuteAction; override;
function CanExecute: Boolean; override;
end; //TTMServiceAction
TTMMultiAction = class(TTMAction)
private
FItems: TObjectList;
function GetItems(Index: Integer): TTMAction;
function GetCount: Integer;
public
property Items[Index: Integer]: TTMAction read GetItems; default;
property Count: Integer read GetCount;
procedure ExecuteAction; override;
constructor Create; virtual;
destructor Destroy; override;
function Add(AAction: TTMAction): Integer;
procedure Clear;
procedure Delete(Index: Integer);
procedure Insert(Index: Integer; AAction: TTMAction);
procedure Move(CurIndex, NewIndex: Integer);
end; //TTMMultiAction
TTMHtmlWindowAction = class(TTMAction)
private
FSrc: String;
FHeader: String;
FVariables: TObjectList;
FHtmlActions: TStringList;
FHtmlWindowFlags: THtmlWindowFlags;
FHeight: Integer;
FWidth: Integer;
FLeft: Integer;
FTop: Integer;
public
property Header: String read FHeader write FHeader;
property Src: String read FSrc write FSrc;
property HtmlWindowFlags: THtmlWindowFlags read FHtmlWindowFlags write FHtmlWindowFlags;
property Height: Integer read FHeight write FHeight;
property Width: Integer read FWidth write FWidth;
property Left: Integer read FLeft write FLeft;
property Top: Integer read FTop write FTop;
property Variables: TObjectList read FVariables write FVariables;
property HtmlActions: TStringList read FHtmlActions write FHtmlActions;
procedure ExecuteAction; override;
end; //TTmHtmlWindowAction
const
{ Param name constants }
ParamCommonFlags = 'Flags';
ParamServicesName = 'Name';
ParamServicesDisplayName = 'DisplayName';
ParamVariablesName = 'Name';
ParamVariablesType = 'Type';
ParamVariablesFlags = 'Flags';
ParamVariablesValue = 'Value';
ParamVariablesRegRoot = 'Root';
ParamVariablesRegKey = 'Key';
ParamVariablesRegValue = 'ValueName';
ParamVariablesEnvName = 'EnvName';
ParamVariablesCmdLineParamName = 'ParamName';
ParamVariablesDefaultValue = 'DefaultValue';
ParamVariablesPromptCaption = 'PromptCaption';
ParamVariablesPromptText = 'PromptText';
ParamMenuItemType = 'Type';
ParamMenuItemCaption = 'Caption';
ParamMenuItemGlyph = 'Glyph';
ParamMenuItemSection = 'SubMenu';
ParamActionAction = 'Action';
ParamActionFileName = 'FileName';
ParamActionParams = 'Parameters';
ParamActionWorkingDir = 'WorkingDir';
ParamActionShowCmd = 'ShowCmd';
ParamActionShellExecVerb = 'Verb';
ParamActionMultiSection = 'Actions';
ParamActionFlags = 'Flags';
ParamServiceService = 'Service';
ParamActionServiceAction = 'ServiceAction';
ParamActionHtmlSrc = 'Src';
ParamActionHtmlHeader = 'Header';
ParamActionHtmlWindowFlags = 'HtmlWindowFlags';
ParamActionHtmlHeight = 'Height';
ParamActionHtmlWidth = 'Width';
ParamActionHtmlLeft = 'Left';
ParamActionHtmlTop = 'Top';
{ Flag name constants }
VariableFlags: array[0..0] of PChar = ( //see also TVariableFlags
'ispath');
ActionFlags: array[0..2] of PChar = ( //see also TActionFlags
'ignoreerrors', 'waituntilterminated', 'waituntilidle');
HtmlWindowFlags: array[0..6] of PChar = ( //see also THtmlWindowFlags
'maximized', 'noresize', 'noscrollbars', 'enablecontextmenu',
'noclosebutton', 'noheader', 'alwaysontop');
implementation
uses SysUtils, Forms,
TMCmnFunc, Dialogs, TMMsgs, TMHtmlWindow;
{ TTMAction }
function TTMAction.CanExecute: Boolean;
begin
Result := True;
end;
{ TTMBuiltInAction }
constructor TTMBuiltInAction.Create(ABuiltInAction: TBuiltInAction);
begin
BuiltInAction := ABuiltInAction;
end;
procedure TTMBuiltInAction.ExecuteAction;
begin
inherited;
try
if Assigned(OnExecute) then
OnExecute(Self);
except
on E: Exception do
if not (afIgnoreErrors in Flags) then
raise Exception.Create('Could not perform built-in action:'#13#10 + E.Message);
end; //try..except
end;
{ TTMRunAction }
constructor TTMRunAction.Create;
begin
ShowCmd := swNormal;
end;
procedure ProcessMessagesProc; far;
begin
Application.ProcessMessages;
end;
procedure TTMRunAction.ExecuteAction;
var
ResultCode: Integer;
begin
inherited;
if not InstExec(ExpandVariables(FileName, Variables),
ExpandVariables(Parameters, Variables),
ExpandVariables(WorkingDir, Variables),
afWaitUntilTerminated in Flags,
afWaitUntilIdle in Flags,
Ord(ShowCmd),
ProcessMessagesProc,
ResultCode) then
if not (afIgnoreErrors in Flags) then
raise Exception.Create('Could not execute run action:'#13#10 + SysErrorMessage(ResultCode));
{ TODO 0 -cError handling : Improve TTMRunAction error handling (added ignoreerrors flag; need further improvement?}
end;
{ TTMShellExecuteAction }
constructor TTMShellExecuteAction.Create;
begin
ShowCmd := swNormal;
end;
procedure TTMShellExecuteAction.ExecuteAction;
var
ErrorCode: Integer;
begin
inherited;
if not InstShellExec(ExpandVariables(FileName, Variables),
ExpandVariables(Parameters, Variables),
Verb,
ExpandVariables(WorkingDir, Variables),
Ord(ShowCmd), ErrorCode) then
if not (afIgnoreErrors in Flags) then
raise Exception.Create('Could not execute shellexecute action:'#13#10 + SysErrorMessage(ErrorCode));
{ TODO 0 -cError handling : Improve TTMShellExecuteAction error handling (added ignoreerrors flag; need further improvement? }
end;
{ TTMServiceAction }
function TTMServiceAction.CanExecute: Boolean;
begin
Result := True;
with Service do
if not Active then
Result := False
else
case Action of
saStartResume:
Result := (State <> svsRunning);
saPause:
Result := (svcPauseContinue in ControlsAccepted) and (State <> svsPaused);
saStop:
Result := (svcStop in ControlsAccepted) and (State <> svsStopped);
saRestart:
Result := (svcStop in ControlsAccepted) and (State <> svsStopped);
end; //else not active then with service do case action of
end;
procedure TTMServiceAction.ExecuteAction;
begin
inherited;
{ TODO 0 : Improve TTMServiceAction error handling (added ignoreerrors flag; need further improvement?}
try
with Service do
begin
if not Active then
Exit;
case Action of
saStartResume:
if State = svsPaused then
begin
Continue;
if afWaitUntilTerminated in Flags then
while State = svsContinuePending do
begin
Application.ProcessMessages;
if Application.Terminated then
Exit;
end;
end
else
begin
Start;
if afWaitUntilTerminated in Flags then
while State = svsStartPending do
begin
Application.ProcessMessages;
if Application.Terminated then
Exit;
end;
end;
saPause: begin
Pause;
if afWaitUntilTerminated in Flags then
while State = svsPausePending do
begin
Application.ProcessMessages;
if Application.Terminated then
Exit;
end;
end;
saStop: begin
Stop;
if afWaitUntilTerminated in Flags then
while State = svsStopPending do
begin
Application.ProcessMessages;
if Application.Terminated then
Exit;
end;
end;
saRestart: begin
Stop;
while State = svsStopPending do
begin
Application.ProcessMessages;
if Application.Terminated then
Exit;
end;
Start;
end;
end; //case
end; //with service do
except
on E: Exception do
if not (afIgnoreErrors in Flags) then
raise Exception.Create('Could not perform service action:'#13#10 + E.Message);
end; //try..except
end;
{ TTMMultiAction }
function TTMMultiAction.Add(AAction: TTMAction): Integer;
begin
Result := FItems.Add(AAction);
end;
procedure TTMMultiAction.Clear;
begin
FItems.Clear;
end;
constructor TTMMultiAction.Create;
begin
FItems := TObjectList.Create(True);
end;
procedure TTMMultiAction.Delete(Index: Integer);
begin
FItems.Delete(Index);
end;
destructor TTMMultiAction.Destroy;
begin
FreeAndNil(FItems);
end;
procedure TTMMultiAction.ExecuteAction;
var
I: Integer;
begin
inherited;
for I := 0 to FItems.Count - 1 do
(Items[I] as TTMAction).ExecuteAction;
end;
function TTMMultiAction.GetCount: Integer;
begin
Result := FItems.Count;
end;
function TTMMultiAction.GetItems(Index: Integer): TTMAction;
begin
Result := (FItems[Index] as TTMAction);
end;
procedure TTMMultiAction.Insert(Index: Integer; AAction: TTMAction);
begin
FItems.Insert(Index, AAction);
end;
procedure TTMMultiAction.Move(CurIndex, NewIndex: Integer);
begin
FItems.Move(CurIndex, NewIndex);
end;
{ TVariable }
function TVariable.GetValue: String;
begin
Result := FValue;
end;
procedure TVariable.SetValue(const AValue: String);
begin
inherited;
FValue := AValue;
end;
{ TPromptVariable }
function TPromptVariable.GetValue: String;
begin
Result := InputBox(Caption, Text, DefaultValue);
end;
procedure TPromptVariable.SetValue(const AValue: String);
begin
inherited;
raise EInvalidOperation.Create(SCannotSetPromptVarValue);
end;
{ TTMHtmlWindowAction }
procedure TTMHtmlWindowAction.ExecuteAction;
begin
inherited;
if Assigned(HtmlWindow) then
FreeAndNil(HtmlWindow);
HtmlWindow := THtmlWindow.Create(Application);
with HtmlWindow do
begin
HtmlWindowFlags := Self.HtmlWindowFlags;
Src := ExpandVariables(Self.Src, Variables);
Header := ExpandVariables(Self.Header, Variables);
HtmlActions := Self.HtmlActions;
SetPosAndDimensions(Self.Left, Self.Top, Self.Width, Self.Height);
Show;
end; //with htmlwindow
end;
end.