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
|
{
fpGUI - Free Pascal GUI Toolkit
Copyright (C) 2006 - 2013 See the file AUTHORS.txt, included in this
distribution, for details of the copyright.
See the file COPYING.modifiedLGPL, included in this distribution,
for details about redistributing fpGUI.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Description:
Main window functionality and designer class.
}
unit vfdmain;
{$mode objfpc}{$H+}
interface
uses
Classes,
SysUtils,
fpg_base,
fpg_main,
fpg_widget,
fpg_dialogs,
vfdprops,
vfdforms,
vfddesigner,
vfdfile,
newformdesigner;
type
TMainDesigner = class(TObject)
private
FShowGrid: boolean;
procedure SetEditedFileName(const Value: string);
procedure LoadDefaults;
procedure SetShowGrid(AValue: boolean);
protected
FDesigners: TList;
FFile: TVFDFile;
FEditedFileName: string;
public
GridResolution: integer;
SaveComponentNames: boolean;
selectedform: TFormDesigner;
constructor Create;
destructor Destroy; override;
procedure CreateWindows;
procedure SelectForm(aform: TFormDesigner);
function Designer(index: integer): TFormDesigner;
function DesignerCount: integer;
function NewFormName: string;
procedure CreateParseForm(const FormName, FormHead, FormBody: string);
procedure OnNewForm(Sender: TObject);
procedure OnNewFile(Sender: TObject);
procedure OnSaveFile(Sender: TObject);
procedure OnLoadFile(Sender: TObject);
procedure OnPropTextChange(Sender: TObject);
procedure OnPropNameChange(Sender: TObject);
procedure OnPropPosEdit(Sender: TObject);
procedure OnOtherChange(Sender: TObject);
procedure OnAnchorChange(Sender: TObject);
procedure OnEditWidget(Sender: TObject);
procedure OnEditWidgetOrder(Sender: TObject);
procedure OnEditTabOrder(Sender: TObject);
procedure OnExit(Sender: TObject);
procedure OnOptionsClick(Sender: TObject);
property EditedFileName: string read FEditedFileName write SetEditedFileName;
property ShowGrid: boolean read FShowGrid write SetShowGrid;
end;
var
maindsgn: TMainDesigner;
implementation
uses
fpg_constants,
fpg_iniutils,
fpg_utils,
vfd_constants,
vfdformparser;
var
DefaultPasExt : String = '.pas';
OneClickMove: Boolean;
{ TMainDesigner }
procedure TMainDesigner.OnNewFile(Sender: TObject);
var
n: integer;
begin
EditedFileName := '';
for n := 0 to FDesigners.Count - 1 do
begin
selectedform := nil;
TFormDesigner(FDesigners[n]).Free;
end;
FDesigners.Clear;
OnNewForm(Sender);
end;
procedure TMainDesigner.OnLoadFile(Sender: TObject);
var
n, m: integer;
bl, bl2: TVFDFileBlock;
fname: string;
afiledialog: TfpgFileDialog;
begin
fname := EditedFileName;
if Sender <> maindsgn then
begin
afiledialog := TfpgFileDialog.Create(nil);
afiledialog.Filename := EditedFilename;
afiledialog.WindowTitle := rsOpenFormFile;
afiledialog.Filter :=
Format(cFileFilter, [rsPascalSourceFiles, cPascalSourceFiles, cPascalSourceFiles])
+ '|' + Format(cFileFilter, [rsAllFiles, AllFilesMask, AllFilesMask]);
if afiledialog.RunOpenFile then
begin
EditedFileName := aFileDialog.Filename;
fname := EditedFilename;
end
else
fname := '';
FreeAndNil(aFileDialog);
end;
if fname = '' then
Exit;
for n := 0 to FDesigners.Count - 1 do
begin
selectedform := nil;
TFormDesigner(FDesigners[n]).Free;
end;
FDesigners.Clear;
if not fpgFileExists(fname) then
begin
ShowMessage(Format(rsErrUnitNotFound, [fname]), rsErrLoadingForm);
Exit;
end;
FFile.LoadFile(fname);
FFile.GetBlocks;
for n := 0 to FFile.BlockCount-1 do
begin
bl := FFile.Block(n);
if bl.BlockID = 'VFD_HEAD_BEGIN' then
for m := n + 1 to FFile.BlockCount-1 do
begin
bl2 := FFile.Block(m);
if (bl2.BlockID = 'VFD_BODY_BEGIN') and (bl2.FormName = bl.FormName) then
CreateParseForm(bl.FormName, bl.Data, bl2.Data); // pair was found
end;
end;
for n := 0 to FDesigners.Count - 1 do
begin
selectedform := nil;
TFormDesigner(FDesigners[n]).Form.ShowGrid := FShowGrid;
end;
frmMain.mru.AddItem(fname);
end;
procedure TMainDesigner.OnSaveFile(Sender: TObject);
var
n, i: integer;
fd: TFormDesigner;
fdata: string;
ff: file;
fname, uname: string;
aFileDialog: TfpgFileDialog;
begin
fname := EditedFileName;
if ((Sender as TComponent).Tag = 10) and (EditedFileName <> '') then
fname := EditedFileName
else
begin
afiledialog := TfpgFileDialog.Create(nil);
afiledialog.Filename := EditedFilename;
afiledialog.WindowTitle := rsSaveFormFile;
afiledialog.Filter :=
Format(cFileFilter, [rsPascalSourceFiles, cPascalSourceFiles, cPascalSourceFiles])
+ '|' + Format(cFileFilter, [rsAllFiles, AllFilesMask, AllFilesMask]);
if afiledialog.RunSaveFile then
begin
fname:=aFileDialog.Filename;
if (ExtractFileExt(fname)='') then
fname:=fname+DefaultPasExt;
EditedFileName := fname;
end
else
fname := '';
aFileDialog.Free;
end;
if fname = '' then
Exit;
EditedFileName := fname;
if fpgFileExists(fname) then
begin
FFile.LoadFile(fname);
FFile.GetBlocks;
end
else
begin
uname := fpgExtractFileName(fname);
i := pos('.pas', LowerCase(uname));
if i > 0 then
uname := copy(uname, 1, i - 1);
FFile.NewFileSkeleton(uname);
end;
for n := 0 to DesignerCount-1 do
begin
fd := nil;
fd := Designer(n);
if fd = nil then
raise Exception.Create(rsErrFailedToFindDesignerForm);
FFile.SetFormData(fd.Form.Name, fd.GetFormSourceDecl, fd.GetFormSourceImpl);
end;
fdata := FFile.MergeBlocks;
AssignFile(ff, fpgToOSEncoding(fname));
try
Rewrite(ff, 1);
try
BlockWrite(ff, fdata[1], length(fdata));
finally
CloseFile(ff);
end;
frmMain.mru.AddItem(fname);
except
on E: Exception do
raise Exception.CreateFmt(rsErrFormSaveIOError + LineEnding + E.Message, ['TMainDesigner.OnSaveFile']);
end;
end;
procedure TMainDesigner.OnAnchorChange(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.OnAnchorChange(Sender);
end;
procedure TMainDesigner.OnOtherChange(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.OnOtherChange(Sender);
end;
procedure TMainDesigner.OnPropNameChange(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.OnPropNameChange(Sender);
end;
procedure TMainDesigner.OnPropPosEdit(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.OnPropPosEdit(Sender);
end;
procedure TMainDesigner.OnPropTextChange(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.OnPropTextChange(Sender);
end;
procedure TMainDesigner.OnNewForm(Sender: TObject);
var
fd: TFormDesigner;
nfrm: TNewFormForm;
function DoesNameAlreadyExist(const AName: string): boolean;
var
i: integer;
begin
Result := False;
for i := 0 to FDesigners.Count-1 do
begin
if TFormDesigner(FDesigners[i]).Form.Name = AName then
begin
Result := True;
break;
end;
end;
end;
begin
nfrm := TNewFormForm.Create(nil);
try
if nfrm.ShowModal = mrOK then
begin
if DoesNameAlreadyExist(nfrm.edName.Text) then
begin
TfpgMessageDialog.Critical(rsErrNameConflict,'The form name already exists in the current unit, please try again');
exit;
end;
fd := TFormDesigner.Create;
if nfrm.edName.Text <> '' then
fd.Form.Name := nfrm.edName.Text;
fd.Form.WindowTitle := fd.Form.Name;
fd.Form.ShowGrid := FShowGrid;
fd.OneClickMove := OneClickMove;
FDesigners.Add(fd);
SelectedForm := fd;
fd.Show;
end;
finally
nfrm.Free;
end;
end;
procedure TMainDesigner.CreateWindows;
begin
frmMain := TfrmMain.Create(nil);
frmMain.WindowTitle := cAppNameAndVersion;
frmMain.Show;
frmProperties := TfrmProperties.Create(nil);
frmProperties.Show;
end;
constructor TMainDesigner.Create;
begin
FDesigners := TList.Create;
SelectedForm := nil;
FFile := TVFDFile.Create;
// options
SaveComponentNames := True;
LoadDefaults;
FEditedFileName := '';
end;
destructor TMainDesigner.Destroy;
var
n: integer;
begin
for n := 0 to FDesigners.Count - 1 do
TFormDesigner(FDesigners[n]).Free;
FDesigners.Free;
FFile.Free;
frmProperties.Free;
frmMain.Free;
inherited;
end;
procedure TMainDesigner.SelectForm(aform: TFormDesigner);
begin
if (SelectedForm <> nil) and (SelectedForm <> aform) then
SelectedForm.DeSelectAll;
SelectedForm := aform;
end;
function TMainDesigner.Designer(index: integer): TFormDesigner;
begin
Result := nil;
if (index < 0) or (index > FDesigners.Count-1) then
Exit;
Result := TFormDesigner(FDesigners[index]);
end;
function TMainDesigner.DesignerCount: integer;
begin
Result := FDesigners.Count;
end;
function TMainDesigner.NewFormName: string;
var
n, i: integer;
s: string;
begin
i := 0;
repeat
Inc(i);
s := 'Form' + IntToStr(i);
n := 0;
while (n < DesignerCount) do
begin
if Designer(n).Form.Name = s then
Break;
Inc(n);
end;
until n > DesignerCount-1;
Result := s;
end;
procedure TMainDesigner.CreateParseForm(const FormName, FormHead, FormBody: string);
var
fd: TFormDesigner;
fp: TVFDFormParser;
begin
fp := TVFDFormParser.Create(FormName, FormHead, FormBody);
fd := fp.ParseForm;
fd.OneClickMove := OneClickMove;
fp.Free;
FDesigners.Add(fd);
fd.Show;
end;
procedure TMainDesigner.OnEditWidget(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.OnEditWidget(Sender);
end;
procedure TMainDesigner.OnEditWidgetOrder(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.EditWidgetOrTabOrder(emWidgetOrder);
end;
procedure TMainDesigner.OnEditTabOrder(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.EditWidgetOrTabOrder(emTabOrder);
end;
procedure TMainDesigner.OnExit(Sender: TObject);
begin
frmProperties.Close;
frmMain.Close;
end;
procedure TMainDesigner.OnOptionsClick(Sender: TObject);
var
frm: TfrmVFDSetup;
begin
frm := TfrmVFDSetup.Create(nil);
try
if frm.ShowModal = mrOK then
begin
LoadDefaults;
frmMain.mru.MaxItems := gINI.ReadInteger('Options', 'MRUFileCount', 4);
frmMain.mru.ShowFullPath := gINI.ReadBool('Options', 'ShowFullPath', True);
end;
finally
frm.Free;
end;
end;
procedure TMainDesigner.SetEditedFileName(const Value: string);
var
s: string;
begin
FEditedFileName := Value;
s := ExtractFileName(FEditedFileName);
if s = '' then
s := '[' + rsNewUnnamedForm + ']';
frmMain.WindowTitle := cAppNameAndVersion + ' - ' + s;
end;
procedure TMainDesigner.LoadDefaults;
begin
GridResolution := gINI.ReadInteger('Options', 'GridResolution', 4);
DefaultPasExt := gINI.ReadString('Options', 'DefaultFileExt', '.pas');
UndoOnPropExit := gINI.ReadBool('Options', 'UndoOnExit', DefUndoOnPropExit);
OneClickMove := gINI.ReadBool('Options', 'OneClickMove', True);
fpgApplication.HintPause := 1000;
end;
procedure TMainDesigner.SetShowGrid(AValue: boolean);
var
i: integer;
begin
if FShowGrid = AValue then
Exit;
FShowGrid := AValue;
for i := 0 to FDesigners.Count-1 do
TFormDesigner(FDesigners[i]).Form.ShowGrid := AValue;
end;
end.
|