1 | unit UConfirmationForm;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
---|
9 | StdCtrls, Buttons, ExtCtrls, URegistry, SpecializedList;
|
---|
10 |
|
---|
11 | type
|
---|
12 | { TConfirmMessage }
|
---|
13 |
|
---|
14 | TConfirmMessage = class
|
---|
15 | SysName: string;
|
---|
16 | Enabled: Boolean;
|
---|
17 | Title: string;
|
---|
18 | Text: string;
|
---|
19 | Buttons: TMsgDlgButtons;
|
---|
20 | DefaultAction: TModalResult;
|
---|
21 | function DefaultActionText: string;
|
---|
22 | end;
|
---|
23 |
|
---|
24 | { TConfirmMessageList }
|
---|
25 |
|
---|
26 | TConfirmMessageList = class(TListObject)
|
---|
27 | public
|
---|
28 | constructor Create; override;
|
---|
29 | function AddItem(SysName: string; Title, Text: string; Buttons: TMsgDlgButtons;
|
---|
30 | DefaultAction: TModalResult = mrNone): TConfirmMessage;
|
---|
31 | procedure Register(Message: TConfirmMessage);
|
---|
32 | procedure Unregister(Message: TConfirmMessage);
|
---|
33 | procedure LoadFromRegistry(Context: TRegistryContext);
|
---|
34 | procedure SaveToRegistry(Context: TRegistryContext);
|
---|
35 | function SearchByName(Name: string): TConfirmMessage;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | { TConfirmationForm }
|
---|
39 |
|
---|
40 | TConfirmationForm = class(TForm)
|
---|
41 | BitBtnOk: TBitBtn;
|
---|
42 | BitBtnYes: TBitBtn;
|
---|
43 | BitBtnNo: TBitBtn;
|
---|
44 | BitBtnCancel: TBitBtn;
|
---|
45 | CheckBoxAutoEnabled: TCheckBox;
|
---|
46 | Image1: TImage;
|
---|
47 | LabelDescription: TLabel;
|
---|
48 | procedure FormCreate(Sender: TObject);
|
---|
49 | procedure FormDestroy(Sender: TObject);
|
---|
50 | procedure FormShow(Sender: TObject);
|
---|
51 | private
|
---|
52 | ModalResult: Integer;
|
---|
53 | public
|
---|
54 | ConfirmMessages: TConfirmMessageList;
|
---|
55 | function ShowDialog(Message: TConfirmMessage; Parameters: array of const): Integer;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | var
|
---|
59 | ConfirmationForm: TConfirmationForm;
|
---|
60 |
|
---|
61 |
|
---|
62 | implementation
|
---|
63 |
|
---|
64 | {$R *.lfm}
|
---|
65 |
|
---|
66 | resourcestring
|
---|
67 | SYes = 'Yes';
|
---|
68 | SNo = 'No';
|
---|
69 | SCancel = 'Cancel';
|
---|
70 | SOk = 'Ok';
|
---|
71 | SAbort = 'Abort';
|
---|
72 | SRetry = 'Retry';
|
---|
73 | SIgnore = 'Ignore';
|
---|
74 | SClose = 'Close';
|
---|
75 | SDoSameActionNextTime = 'Do same action next time';
|
---|
76 | // SConfirmMessageNotFound = 'Confirm message id %s not found';
|
---|
77 |
|
---|
78 |
|
---|
79 | procedure TConfirmationForm.FormCreate(Sender: TObject);
|
---|
80 | begin
|
---|
81 | ConfirmMessages := TConfirmMessageList.Create;
|
---|
82 | ConfirmMessages.OwnsObjects := False;
|
---|
83 | end;
|
---|
84 |
|
---|
85 | procedure TConfirmationForm.FormDestroy(Sender: TObject);
|
---|
86 | begin
|
---|
87 | FreeAndNil(ConfirmMessages);
|
---|
88 | end;
|
---|
89 |
|
---|
90 | procedure TConfirmationForm.FormShow(Sender: TObject);
|
---|
91 | begin
|
---|
92 | BitBtnOk.Caption := SOk;
|
---|
93 | BitBtnCancel.Caption := SCancel;
|
---|
94 | BitBtnNo.Caption := SNo;
|
---|
95 | BitBtnYes.Caption := SYes;
|
---|
96 | CheckBoxAutoEnabled.Caption := SDoSameActionNextTime;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | function TConfirmationForm.ShowDialog(Message: TConfirmMessage; Parameters: array of const): Integer;
|
---|
100 | var
|
---|
101 | RightPos: Integer;
|
---|
102 | const
|
---|
103 | Space = 10;
|
---|
104 | begin
|
---|
105 | with Message do begin
|
---|
106 | RightPos := Width;
|
---|
107 | if mbCancel in Buttons then begin
|
---|
108 | Dec(RightPos, BitBtnCancel.Width + Space);
|
---|
109 | BitBtnCancel.Left := RightPos;
|
---|
110 | BitBtnCancel.Visible := True;
|
---|
111 | end else BitBtnCancel.Visible := False;
|
---|
112 | if mbNo in Buttons then begin
|
---|
113 | Dec(RightPos, BitBtnNo.Width + Space);
|
---|
114 | BitBtnNo.Left := RightPos;
|
---|
115 | BitBtnNo.Visible := True;
|
---|
116 | end else BitBtnNo.Visible := False;
|
---|
117 | if mbYes in Buttons then begin
|
---|
118 | Dec(RightPos, BitBtnYes.Width + Space);
|
---|
119 | BitBtnYes.Left := RightPos;
|
---|
120 | BitBtnYes.Visible := True;
|
---|
121 | end else BitBtnYes.Visible := False;
|
---|
122 | if mbOk in Buttons then begin
|
---|
123 | Dec(RightPos, BitBtnOk.Width + Space);
|
---|
124 | BitBtnOk.Left := RightPos;
|
---|
125 | BitBtnOk.Visible := True;
|
---|
126 | end else BitBtnOk.Visible := False;
|
---|
127 |
|
---|
128 | Caption := Title;
|
---|
129 | LabelDescription.Caption := Format(Text, Parameters);
|
---|
130 |
|
---|
131 | CheckBoxAutoEnabled.Checked := False;
|
---|
132 |
|
---|
133 | if Enabled or (DefaultAction = mrNone) then ModalResult := ShowModal
|
---|
134 | else ModalResult := DefaultAction;
|
---|
135 |
|
---|
136 | if CheckBoxAutoEnabled.Checked then begin
|
---|
137 | Enabled := False;
|
---|
138 | DefaultAction := ModalResult;
|
---|
139 | end;
|
---|
140 | Result := ModalResult;
|
---|
141 | end;
|
---|
142 | end;
|
---|
143 |
|
---|
144 | { TConfirmMessageList }
|
---|
145 |
|
---|
146 | constructor TConfirmMessageList.Create;
|
---|
147 | begin
|
---|
148 | inherited;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | function TConfirmMessageList.AddItem(SysName: string; Title, Text: string;
|
---|
152 | Buttons: TMsgDlgButtons; DefaultAction: TModalResult = mrNone): TConfirmMessage;
|
---|
153 | var
|
---|
154 | NewMessage: TConfirmMessage;
|
---|
155 | begin
|
---|
156 | NewMessage := TConfirmMessage.Create;
|
---|
157 | NewMessage.SysName := SysName;
|
---|
158 | NewMessage.Title := Title;
|
---|
159 | NewMessage.Text := Text;
|
---|
160 | NewMessage.Enabled := DefaultAction = mrNone;
|
---|
161 | NewMessage.DefaultAction := DefaultAction;
|
---|
162 | NewMessage.Buttons := Buttons;
|
---|
163 | Add(TObject(NewMessage));
|
---|
164 | Result := NewMessage;
|
---|
165 | end;
|
---|
166 |
|
---|
167 | procedure TConfirmMessageList.Register(Message: TConfirmMessage);
|
---|
168 | begin
|
---|
169 | Add(Message);
|
---|
170 | end;
|
---|
171 |
|
---|
172 | procedure TConfirmMessageList.Unregister(Message: TConfirmMessage);
|
---|
173 | begin
|
---|
174 | Remove(Message);
|
---|
175 | end;
|
---|
176 |
|
---|
177 | procedure TConfirmMessageList.LoadFromRegistry(Context: TRegistryContext);
|
---|
178 | var
|
---|
179 | I: Integer;
|
---|
180 | begin
|
---|
181 | with TRegistryEx.Create do
|
---|
182 | try
|
---|
183 | RootKey := Context.RootKey;
|
---|
184 | for I := 0 to Count - 1 do
|
---|
185 | with TConfirmMessage(Items[I]) do
|
---|
186 | begin
|
---|
187 | OpenKey(Context.Key + '\' + SysName, True);
|
---|
188 | Enabled := ReadBoolWithDefault('Enabled', Enabled);
|
---|
189 | DefaultAction := ReadIntegerWithDefault('DefaultAction', DefaultAction);
|
---|
190 | end;
|
---|
191 | finally
|
---|
192 | Free;
|
---|
193 | end;
|
---|
194 | end;
|
---|
195 |
|
---|
196 | procedure TConfirmMessageList.SaveToRegistry(Context: TRegistryContext);
|
---|
197 | var
|
---|
198 | I: Integer;
|
---|
199 | begin
|
---|
200 | with TRegistryEx.Create do
|
---|
201 | try
|
---|
202 | RootKey := Context.RootKey;
|
---|
203 | for I := 0 to Count - 1 do
|
---|
204 | with TConfirmMessage(Items[I]) do
|
---|
205 | begin
|
---|
206 | OpenKey(Context.Key + '\' + SysName, True);
|
---|
207 | WriteBool('Enabled', Enabled);
|
---|
208 | WriteInteger('DefaultAction', DefaultAction);
|
---|
209 | end;
|
---|
210 | finally
|
---|
211 | Free;
|
---|
212 | end;
|
---|
213 | end;
|
---|
214 |
|
---|
215 | function TConfirmMessageList.SearchByName(Name: string): TConfirmMessage;
|
---|
216 | var
|
---|
217 | I: Integer;
|
---|
218 | begin
|
---|
219 | I := 0;
|
---|
220 | while (I < Count) and (TConfirmMessage(Items[I]).SysName <> Name) do Inc(I);
|
---|
221 | if I < Count then Result := TConfirmMessage(Items[I])
|
---|
222 | else Result := nil;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | { TConfirmMessage }
|
---|
226 |
|
---|
227 | function TConfirmMessage.DefaultActionText: string;
|
---|
228 | begin
|
---|
229 | if DefaultAction = mrNone then Result := ''
|
---|
230 | else if DefaultAction = mrYes then Result := SYes
|
---|
231 | else if DefaultAction = mrNo then Result := SNo
|
---|
232 | else if DefaultAction = mrCancel then Result := SCancel
|
---|
233 | else if DefaultAction = mrOK then Result := SOk
|
---|
234 | else if DefaultAction = mrAbort then Result := SAbort
|
---|
235 | else if DefaultAction = mrRetry then Result := SRetry
|
---|
236 | else if DefaultAction = mrIgnore then Result := SIgnore
|
---|
237 | else if DefaultAction = mrClose then Result := SClose;
|
---|
238 | end;
|
---|
239 |
|
---|
240 |
|
---|
241 | initialization
|
---|
242 | // {$I UConfirmationForm.lrs}
|
---|
243 |
|
---|
244 | end.
|
---|
245 |
|
---|