-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLifeSaveDialog.pas
312 lines (282 loc) · 7.89 KB
/
LifeSaveDialog.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
unit LifeSaveDialog;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
stdctrls, extctrls;
type
TLifeOpenDialog = class(TOpenDialog)
private
FLifeLabel: TLabel;
FLifeMemo: TMemo;
FLifePanel: TPanel;
FOpenToClipboard: TButton;
FComments: TStringList;
FUseRules: TCheckBox;
FCurrentRules: string;
FOnOpenToClipboard: TNotifyEvent;
protected
procedure DoClose; override;
procedure DoSelectionChange; override;
procedure DoShow; override;
procedure LifeMemoMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
function GetUseNewRules: Boolean;
procedure OpenToClipboardClick(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function Execute: Boolean; override;
property Comments: TStringList read FComments;
property CurrentRules: string read FCurrentRules write FCurrentRules;
property UseNewRules: boolean read GetUseNewRules;
published
property OnOpenToClipboard: TNotifyEvent
read FOnOpenToClipboard write FOnOpenToClipboard;
end;
TLifeSaveDialog = class(TLifeOpenDialog)
protected
procedure DoShow; override;
procedure LifeMemoKeyPress(Sender: TObject; var Key: Char);
procedure SetIncludeTorusInfo(Value: boolean);
public
constructor Create(AOwner: TComponent); override;
function Execute: boolean; override;
property IncludeTorusInfo: boolean read GetUseNewRules write SetIncludeTorusInfo;
end;
procedure Register;
implementation
uses
CommDlg, LifeConst, LifeLoad, LifeRules;
{$R LifeSaveDlg.Res}
procedure Register;
begin
RegisterComponents('Johan', [TLifeOpenDialog]);
RegisterComponents('Johan', [TLifeSaveDialog]);
end;
function ConvertRules(ARule: string): string;
var
LifeRules: TLifeRules;
begin
Result:= '';
LifeRules:= TLifeRules.Create;
try
LifeRules.Rulestring:= ARule;
Result:= LifeRules.Rulestring;
finally
LifeRules.Free;
end; {try}
end;
const
RuleCaption = 'Use these rules: %s';
constructor TLifeOpenDialog.Create(AOwner: TComponent);
var
ACaption: string;
begin
inherited Create(AOwner);
CurrentRules:= DefaultRules;
Filter:= LifeFilter;
FLifePanel:= TPanel.Create(Self);
FComments:= TStringList.Create;
with FLifePanel do begin
Name:= 'EditPanel';
Caption:= '';
SetBounds(6,160,200,300);
BevelOuter:= bvNone;
BorderWidth:= 10;
TabOrder:= 1;
FUseRules:= TCheckBox.Create(Self);
with FUseRules do begin
Name:= 'UseRules';
FmtStr(ACaption,RuleCaption,[DefaultRules]);
Caption:= ACaption;
SetBounds(10,10,66,10+ABS(Font.Height));
Checked:= true;
Enabled:= false;
Align:= alTop;
Parent:= FLifePanel;
Visible:= not(Self is TLifeSaveDialog);
end;
FOpenToClipboard:= TButton.Create(Self);
with FOpenToClipboard do begin
Name:= 'OpenToClipboard';
Caption:= 'Open to clipboard';
SetBounds(300,13,120,32);
Parent:= FLifePanel;
OnClick:= OpenToClipboardClick;
Visible:= not(Self is TLifeSaveDialog);
end;
FLifeLabel:= TLabel.Create(Self);
with FLifeLabel do begin
Name:= 'LifeLabel';
Caption:= ' Description of the pattern';
SetBounds(10,26,157,10+ABS(Font.Height));
Align:= alTop;
AutoSize:= false;
Parent:= FLifePanel;
end; {with}
FLifeMemo:= TMemo.Create(Self);
if not (Self is TLifeSaveDialog) then begin
FLifeMemo.TabStop:= false;
FLifeMemo.WantTabs:= false;
end;
with FLifeMemo do begin
Ctl3D:= true;
ScrollBars:= ssVertical;
Name:= 'LifeEdit';
SetBounds(8,49,520,90);
ReadOnly:= true;
Color:= clBtnFace;
Align:= alClient;
TabOrder:= 0;
//ShowHint:= true;
WordWrap:= true;
Font.Name:= 'Arial';
Font.Size:= 9;
Parent:= FLifePanel;
OnMouseDown:= LifeMemoMouseDown;
end; {with}
end; {with}
end;
constructor TLifeSaveDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLifeLabel.Caption:= ' Enter a description of the pattern';
with FLifeMemo do begin
ReadOnly:= false;
Color:= clWindow;
WantReturns:= true;
OnKeyPress:= LifeMemoKeyPress;
end; {with}
//UseRules doubles as "save torus info Y/N".
with FUseRules do begin
Caption:= 'Save torus data';
Enabled:= true;
Checked:= false;
end;
end; {Create}
destructor TLifeOpenDialog.Destroy;
begin
try
FComments.Free;
FLifeMemo.Free;
FLifePanel.Free;
except {ignore}
end; {try}
inherited Destroy;
end;
procedure TLifeOpenDialog.OpenToClipboardClick(Sender: TObject);
begin
if FileExists(FileName) then begin
DoClose;
if Assigned(FOnOpenToClipboard) then FOnOpenToClipboard(Sender);
PostMessage(GetParent(Handle),wm_Close, 0,0);
end;
end;
procedure TLifeOpenDialog.DoSelectionChange;
var
FullName: string;
ValidLifeFile: boolean;
LifeLines: TStringList;
DescLines: TStringList;
Rules: string;
function ValidFile(const FileName: string): boolean;
begin
Result:= GetFileAttributes(PChar(FileName)) <> $FFFFFFFF;
end;
begin
//this is a bit of a hack, but who cares.
if not(Self is TLifeSaveDialog) then begin
FullName:= FileName;
ValidLifeFile:= FileExists(FullName) and (ValidFile(FullName));
if ValidLifeFile then begin
LifeLines:= TStringList.Create;
try
LifeLines.LoadFromFile(FullName);
DescLines:= GetDescription(LifeLines);
Rules:= Trim(ConvertRules(GetRules(LifeLines)));
if Rules <> CurrentRules then begin
FUseRules.Font.Color:= clRed;
FUseRules.Enabled:= true;
end
else begin
FUseRules.Font.Color:= clWindowText;
FUseRules.Enabled:= false;
end;
FmtStr(Rules, RuleCaption, [Rules]);
FUseRules.Caption:= Rules;
if Assigned(DescLines) then begin
FLifeMemo.Lines.Assign(DescLines);
DescLines.Free;
end
else FLifeMemo.Lines.Clear;
except FLifeMemo.Lines.Clear;
end; {try}
LifeLines.Free;
end {if}
else FLifeMemo.Lines.Clear;
end; {if}
inherited DoSelectionChange;
end;
procedure TLifeOpenDialog.DoClose;
begin
FComments.Assign(FLifeMemo.Lines);
inherited DoClose;
Application.HideHint;
end;
function TLifeOpenDialog.GetUseNewRules: Boolean;
begin
Result:= FUseRules.Checked;
end;
procedure TLifeOpenDialog.DoShow;
var
PreviewRect: TRect;
begin
GetClientRect(Handle, PreviewRect);
PreviewRect.Top:= GetStaticRect.Bottom;
Dec(PreviewRect.Top,16);
FLifePanel.BoundsRect:= PreviewRect;
FLifePanel.ParentWindow:= Handle;
inherited DoShow;
FLifeMemo.Lines.Clear;
end;
procedure TLifeSaveDialog.DoShow;
begin
inherited DoShow;
try
FLifeMemo.Lines.Assign(FComments)
except FLifeMemo.Lines.Clear;
end; {try}
end;
procedure TLifeOpenDialog.LifeMemoMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Application.HideHint;
end;
function TLifeOpenDialog.Execute: boolean;
begin
if NewStyleControls and not(ofOldStyleDialog in Options) then
Template:= 'MYTEMPLATE'
else Template:= nil;
//For some #$@! reason the "wm_close" message I post to the
//dialog clears the caption of the button, so we re-set it.
FOpenToClipboard.Caption:= '';
FOpenToClipboard.Caption:= 'Open to clipboard';
Result:= inherited Execute;
end;
function TLifeSaveDialog.Execute: boolean;
begin
if NewStyleControls and not (ofOldStyleDialog in Options) then
Template:= 'MYTEMPLATE'
else Template:= nil;
Result:= DoExecute(@GetSaveFileName);
end;
procedure TLifeSaveDialog.LifeMemoKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then Key:= #10;
end;
procedure TLifeSaveDialog.SetIncludeTorusInfo(Value: boolean);
begin
FUseRules.Visible:= Value;
FUseRules.Checked:= true;
end;
end.