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
|
{
fpGUI - Free Pascal GUI Library
Copyright (C) 2006 - 2008 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,
gfx_widget,
gui_dialogs,
vfdforms,
vfddesigner,
vfdfile,
newformdesigner;
const
program_version = '0.7 beta';
type
TMainDesigner = class(TObject)
private
procedure SetEditedFileName(const Value: string);
procedure LoadGridResolution;
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 OnPaletteChange(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;
end;
var
maindsgn: TMainDesigner;
implementation
uses
vfdformparser,
gui_iniutils,
gfx_utils;
{ 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 := 'Open form file';
afiledialog.Filter := 'Pascal source files (*.pas;*.inc;*.dpr;*.lpr)|*.pas;*.inc;*.dpr;*.lpr|All Files (*)|*';
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('File does not exists.', 'Error loading form');
Exit;
end;
Writeln('loading file...');
FFile.LoadFile(fname);
FFile.GetBlocks;
for n := 1 to FFile.BlockCount do
begin
bl := FFile.Block(n);
if bl.BlockID = 'VFD_HEAD_BEGIN' then
for m := n + 1 to FFile.BlockCount 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;
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).Name = 'btnSave')
and (EditedFileName <> '') then
fname := EditedFileName
else
begin
afiledialog := TfpgFileDialog.Create(nil);
afiledialog.Filename := EditedFilename;
afiledialog.WindowTitle := 'Save form source';
afiledialog.Filter := 'Pascal source files (*.pas;*.inc;*.dpr;*.lpr)|*.pas;*.inc;*.dpr;*.lpr|All Files (*)|*';
if afiledialog.RunSaveFile then
begin
EditedFileName := aFileDialog.Filename;
fname := EditedFilename;
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 := ExtractFileName(fname);
i := pos('.pas', LowerCase(uname));
if i > 0 then
uname := copy(uname, 1, i - 1);
FFile.NewFileSkeleton(uname);
end;
for n := 1 to DesignerCount do
begin
fd := Designer(n);
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.WindowTitle := 'fpGUI Designer v' + program_version + ' - ' + fname;
// everything is done by SetEditedFileName (EditedFileName := ...)
frmMain.mru.AddItem(fname);
except
Writeln('Form save I/O failure.');
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.OnPaletteChange(Sender: TObject);
begin
end;
procedure TMainDesigner.OnNewForm(Sender: TObject);
var
fd: TFormDesigner;
nfrm: TNewFormForm;
begin
Writeln('new form');
nfrm := TNewFormForm.Create(nil);
if nfrm.ShowModal = 1 then
if nfrm.edName.Text <> '' then
begin
fd := TFormDesigner.Create;
fd.Form.Name := nfrm.edName.Text;
fd.Form.WindowTitle := nfrm.edName.Text;
FDesigners.Add(fd);
fd.Show;
end;
nfrm.Free;
end;
procedure TMainDesigner.CreateWindows;
//var
// fd : TFormDesigner;
begin
frmMain := TfrmMain.Create(nil);
frmMain.WindowTitle := 'fpGUI Designer v' + program_version;
frmMain.Show;
frmProperties := TfrmProperties.Create(nil);
frmProperties.Show;
// fd := TFormDesigner.Create;
// fd.Form.Name := 'frmNewForm';
// fd.Form.WindowTitle := u8('frmNewForm');
// FDesigners.Add(fd);
// fd.Show;
end;
constructor TMainDesigner.Create;
begin
FDesigners := TList.Create;
SelectedForm := nil;
FFile := TVFDFile.Create;
// options
SaveComponentNames := True;
LoadGridResolution;
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
//Writeln('selected...');
if (SelectedForm <> nil) and (SelectedForm <> aform) then
SelectedForm.DeSelectAll;
SelectedForm := aform;
end;
function TMainDesigner.Designer(index: integer): TFormDesigner;
begin
Result := nil;
if (index < 1) or (index > FDesigners.Count) then
Exit;
Result := TFormDesigner(FDesigners[index - 1]);
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 := 1;
while (n <= DesignerCount) do
begin
if Designer(n).Form.Name = s then
Break;
Inc(n);
end;
until n > DesignerCount;
Result := s;
end;
procedure TMainDesigner.CreateParseForm(const FormName, FormHead, FormBody: string);
var
fd: TFormDesigner;
fp: TVFDFormParser;
begin
Writeln('CreateParseForm: ', FormName);
fp := TVFDFormParser.Create(FormName, FormHead, FormBody);
fd := fp.ParseForm;
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.EditWidgetOrder;
end;
procedure TMainDesigner.OnEditTabOrder(Sender: TObject);
begin
if SelectedForm <> nil then
SelectedForm.EditTabOrder;
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 = 1 then
begin
LoadGridResolution;
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 := '[new]';
frmMain.WindowTitle := 'fpGUI Designer v' + program_version + ' - ' + s;
end;
procedure TMainDesigner.LoadGridResolution;
begin
case gINI.ReadInteger('Options', 'GridResolution', 2) of
1: GridResolution := 2;
2: GridResolution := 4;
3: GridResolution := 8;
end;
end;
end.
|