2
0

fd2pascal.pp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. Program fd2pascal;
  2. { ---------------------------------------------------------------------------
  3. Program to convert forms fdesign file to pascal code
  4. Copyright (C) 1997 Michael Van Canneyt
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. MA 02110-1301, USA.
  17. --------------------------------------------------------------------------- }
  18. {$IFDEF FPC_DOTTEDUNITS}
  19. uses
  20. UnixApi.Base,
  21. UnixApi.Unix,
  22. System.SysUtils;
  23. {$ELSE FPC_DOTTEDUNITS}
  24. uses
  25. baseunix,
  26. Unix,
  27. sysutils;
  28. {$ENDIF FPC_DOTTEDUNITS}
  29. Const RevString = '$Revision: 1.5 $';
  30. NrOptions = 4;
  31. Options : Array[0..NrOptions] Of String[20] =
  32. ('v','callback','main','altformat','compensate');
  33. Type
  34. { Properties of an object }
  35. ContProps=(CPclass,CPtype,CPbox,CPBoxtype,CPColors,CPalignment,CPstyle,CPsize,
  36. CPlcol,CPlabel,CPShortcut,CPresize,CPgravity,CPname,CPCallback,
  37. CPargument,
  38. CPinvalid);
  39. { Properties of an object for which defaults must be set }
  40. AdjProps=(APClass,APBoxtype,ApColors,APAlignment,APSize,APLcol,APstyle,APgravity);
  41. { List of all object classes }
  42. ObjClasses=(FL_INVALID,FL_BUTTON, FL_LIGHTBUTTON,FL_ROUNDBUTTON, FL_ROUND3DBUTTON,
  43. FL_CHECKBUTTON, FL_BITMAPBUTTON, FL_PIXMAPBUTTON,FL_BITMAP, FL_PIXMAP,
  44. FL_BOX, FL_TEXT, FL_MENU, FL_CHART, FL_CHOICE, FL_COUNTER, FL_SLIDER, FL_VALSLIDER, FL_INPUT,
  45. FL_BROWSER,FL_DIAL,FL_TIMER,FL_CLOCK, FL_POSITIONER, FL_FREE,
  46. FL_XYPLOT,FL_FRAME, FL_LABELFRAME, FL_CANVAS, FL_GLCANVAS,
  47. FL_IMAGECANVAS, FL_FOLDER);
  48. { Properties in preamble }
  49. PreProps=(PPmagic,PPNrforms,PPUnitofMeasure,PPinvalid);
  50. { Properties of a form }
  51. FormProps=(FPName,FPWidth,FPHeight,FPnumObjs,FPinvalid);
  52. Const
  53. { Names of all object properties }
  54. ContPropNames : Array[ContProps] of string[20] =
  55. ('class','type','box','boxtype','colors','alignment','style','size',
  56. 'lcol','label','shortcut','resize','gravity','name','callback',
  57. 'argument',
  58. 'invalid');
  59. { Names of all object properties which must be checked.}
  60. AdjPropsNames : Array[AdjProps] of string[20] =
  61. ('class','boxtype','colors','alignment','size','lcol','style','gravity');
  62. { Names of all preamble properties }
  63. PrePropNames : Array[PreProps] of string[20] =
  64. ('Magic','Number of forms','Unit of measure','Invalid');
  65. { Names of all form properties }
  66. FormPropNames : Array[FormProps] of string[20] =
  67. ('Name','Width','Height','Number of Objects','Invalid');
  68. { Names of all object classes }
  69. FObjClassNames : Array[ObjClasses] of string[20]=
  70. ('FL_INVALID','BUTTON', 'LIGHTBUTTON','ROUNDBUTTON', 'ROUND3DBUTTON',
  71. 'CHECKBUTTON', 'BITMAPBUTTON', 'PIXMAPBUTTON','BITMAP', 'PIXMAP',
  72. 'BOX', 'TEXT', 'MENU', 'CHART', 'CHOICE', 'COUNTER', 'SLIDER', 'VALSLIDER', 'INPUT',
  73. 'BROWSER','DIAL','TIMER','CLOCK', 'POSITIONER', 'FREE',
  74. 'XYPLOT','FRAME', 'LABELFRAME', 'CANVAS', 'GLCANVAS',
  75. 'IMAGECANVAS', 'FOLDER');
  76. { Default properties. If empty a property is ignored.
  77. To force setting of a property, put 'FL_FORCE' as a string.
  78. Mind : Case sensitive }
  79. DefProps : array[ObjClasses,AdjProps] of string[30] =
  80. (('FL_INVALID','','','','','','FL_NORMAL_STYLE','FL_FORCE'),
  81. ('BUTTON','FL_UP_BOX','FL_COL1 FL_COL1','FL_ALIGN_CENTER','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  82. ('LIGHTBUTTON','FL_UP_BOX','FL_COL1 FL_YELLOW','FL_ALIGN_CENTER','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  83. ('ROUNDBUTTON','FL_NO_BOX','FL_MCOL FL_YELLOW','FL_ALIGN_CENTER','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  84. ('ROUND3DBUTTON','FL_NO_BOX','FL_COL1 FL_BLACK','FL_ALIGN_CENTER','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  85. ('CHECKBUTTON','FL_NO_BOX','FL_COL1 FL_YELLOW','FL_ALIGN_CENTER','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  86. ('BITMAPBUTTON','FL_UP_BOX','FL_COL1 FL_BLUE','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  87. ('PIXMAPBUTTON','FL_UP_BOX','FL_COL1 FL_YELLOW','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  88. ('BITMAP','FL_NO_BOX','FL_COL1 FL_COL1','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  89. ('PIXMAP','FL_NO_BOX','FL_COL1 FL_COL1','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  90. ('BOX','','','','','','FL_NORMAL_STYLE','FL_FORCE'),
  91. ('TEXT','FL_FLAT_BOX','FL_COL1 FL_MCOL','FL_ALIGN_LEFT','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  92. ('MENU','FL_BORDER_BOX','FL_COL1 FL_MCOL','FL_ALIGN_CENTER','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  93. ('CHART','FL_BORDER_BOX','FL_COL1','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  94. ('CHOICE','FL_ROUNDED_BOX','FL_COL1 FL_LCOL','FL_ALIGN_LEFT','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  95. ('COUNTER','FL_UP_BOX','FL_COL1 FL_BLUE','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  96. ('SLIDER','FL_DOWN_BOX','FL_COL1 FL_COL1','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  97. ('VALSLIDER','FL_DOWN_BOX','FL_COL1 FL_COL1','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  98. ('INPUT','FL_DOWN_BOX','FL_COL1 FL_MCOL','FL_ALIGN_LEFT','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  99. ('BROWSER','FL_DOWN_BOX','FL_COL1 FL_YELLOW','FL_ALIGN_BOTTOM','FL_SMALL_FONT','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  100. ('DIAL','FL_FLAT_BOX','FL_COL1 FL_RIGHT_BCOL','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  101. ('TIMER','FL_DOWN_BOX','FL_COL1 FL_RED','FL_ALIGN_CENTER','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  102. ('CLOCK','FL_UP_BOX','FL_INACTIVE_COL FL_BOTTOM_BCOL','FL_ALIGN_BOTTOM','','FL_BLACK','FL_NORMAL_STYLE','FL_FORCE'),
  103. ('POSITIONER','FL_DOWN_BOX','FL_COL1 FL_RED','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  104. ('FREE','','','','','','FL_NORMAL_STYLE','FL_FORCE'),
  105. ('XYPLOT','FL_FLAT_BOX','FL_COL1','FL_ALIGN_BOTTOM','','FL_LCOL','FL_NORMAL_STYLE','FL_FORCE'),
  106. ('FRAME','','FL_BLACK FL_COL1','','','FL_BLACK','FL_NORMAL_STYLE','FL_FORCE'),
  107. ('LABELFRAME','','','','','','FL_NORMAL_STYLE','FL_FORCE'),
  108. ('CANVAS','FL_NO_BOX','','FL_ALIGN_TOP','','','FL_NORMAL_STYLE','FL_FORCE'),
  109. ('GLCANVAS','','','','','','FL_NORMAL_STYLE','FL_FORCE'),
  110. ('IMAGECANVAS','','','','','','FL_NORMAL_STYLE','FL_FORCE'),
  111. ('FOLDER','','','','','','FL_NORMAL_STYLE','FL_FORCE'));
  112. Type
  113. { object data type }
  114. PControl = ^TControl;
  115. TControl = Record
  116. Props : array[ContProps] of string;
  117. NextControl : PControl;
  118. end;
  119. { Form data type}
  120. PFormRec = ^TFormRec;
  121. TFormRec = Record
  122. Name : String;
  123. Width,Height : String[5];
  124. Controls : PControl;
  125. NextForm : PFormRec;
  126. end;
  127. { Callback data type }
  128. PCBrec = ^TCBrec;
  129. TCBrec = record
  130. name : string;
  131. next : PCBrec;
  132. end;
  133. { Property emitting procedures }
  134. EmitProp = Procedure (Data : PControl;ObjClass : ObjClasses);
  135. Var
  136. OptionsSet : Array[1..NrOptions] Of Boolean;
  137. FileName : String;
  138. Infile,outfile : Text;
  139. LineNr : Longint;
  140. NrForms,NrControls : Longint;
  141. FormRoot : PFormRec;
  142. cbroot : pcbrec;
  143. { Default properties emitters }
  144. EmitProcs : array [AdjProps] of EmitProp;
  145. { Class specific property emitters. Nil pointers are ignored.}
  146. ClassEmitters : Array[ObjClasses] of EmitProp;
  147. { ------------------------------------------------------------------------
  148. Utilities Code
  149. ------------------------------------------------------------------------ }
  150. Function BaseName(const s:ansistring;suf:ansistring):ansistring;
  151. begin
  152. BaseName:=extractfilename(s);
  153. if '.'+suf=extractfileext(s) then
  154. BaseName:=changefileext(s,ansistring(''));
  155. end;
  156. Procedure EmitError (Const s : String);
  157. begin
  158. writeln (stderr,'Error: ',s);
  159. flush(stderr)
  160. end;
  161. Procedure EmitLineError (Const s : string);
  162. begin
  163. EmitError('Line '+IntToStr(LineNr)+': '+s)
  164. end;
  165. { ------------------------------------------------------------------------
  166. Option handling Code
  167. ------------------------------------------------------------------------ }
  168. Procedure DoOptions;
  169. Var i,j,k : byte;
  170. os : string;
  171. Procedure ShowVersion;
  172. begin
  173. Writeln ('fd2pascal : ',RevString);
  174. Halt(0);
  175. end;
  176. Procedure ShowUsage;
  177. begin
  178. Writeln ('fd2pascal : usage :');
  179. writeln (' fd2pascal [options] filename');
  180. writeln (' Where [options] may be zero or more of :');
  181. writeln (' -compensate Emit size-compensation code.');
  182. writeln (' -altformat Emit code in alternate format.');
  183. writeln (' -main Emit program instead of unit.');
  184. writeln (' -callback Emit callback stubs.');
  185. writeln;
  186. halt(0);
  187. end;
  188. begin
  189. if paramcount=0 then
  190. ShowUsage;
  191. FileName:='';
  192. for i:=1 to paramcount do
  193. begin
  194. if paramstr(i)[1]<>'-' then
  195. If FileName<>'' then
  196. EmitError('Only one filename supported. Ignoring :'+paramstr(i))
  197. else
  198. Filename:=Paramstr(i)
  199. else
  200. begin
  201. os:=copy(paramstr(i),2,length(paramstr(i))-1);
  202. k:=NrOptions+1;
  203. for j:=0 to NrOptions do
  204. if os=options[j] then k:=j;
  205. if k=NrOptions+1 then
  206. EmitError('Option not recognised : '+paramstr(i))
  207. else
  208. if k=0 then ShowVersion else OptionsSet[k]:=True;
  209. end
  210. end; {for}
  211. if FileName='' then
  212. begin
  213. EmitError('No filename supplied. Exiting.');
  214. halt(1);
  215. end;
  216. end;
  217. { ------------------------------------------------------------------------
  218. Code for reading the input file.
  219. ------------------------------------------------------------------------ }
  220. Procedure OpenFile;
  221. begin
  222. if pos('.fd',FileName)=0 then
  223. FileName:=FileName+'.fd';
  224. assign(infile,Filename);
  225. {$push}{$i-}
  226. reset (infile);
  227. {$pop}
  228. if ioresult<>0 then
  229. begin
  230. EmitError('Can''t open : '+filename);
  231. halt(1);
  232. end;
  233. LineNr:=0;
  234. end;
  235. Procedure CloseFile;
  236. begin
  237. Close(infile);
  238. end;
  239. Procedure GetLine(Var S : String);
  240. begin
  241. inc(LineNr);
  242. Readln(infile,s);
  243. {$ifdef debug}
  244. writeln ('Reading line : ',linenr)
  245. {$endif}
  246. end;
  247. Procedure ProcessPreAmbleLine (Const s: String);
  248. var key,value : string;
  249. ppos : Longint;
  250. i,k : PreProps;
  251. code : Word;
  252. begin
  253. if s='' then exit;
  254. ppos:=pos(':',s);
  255. if ppos=0 then
  256. exit;
  257. Key:=Copy(s,1,ppos-1);
  258. Value:=Copy(s,ppos+2,length(s)-ppos-1);
  259. k:=PPinvalid;
  260. for i:=PPmagic to PPinvalid do
  261. if key=PrePropNames[i] then k:=i;
  262. if k=PPinvalid then
  263. EmitLineError('Unknown keyword : '+Key)
  264. else
  265. Case K of
  266. PPMagic,
  267. PPunitofmeasure: ;
  268. PPnrforms: begin
  269. val(value,NrForms,code);
  270. if code<>0 then EmitLineError('Invalid number of forms');
  271. end;
  272. end;
  273. end;
  274. { ------------------------------------------------------------------------
  275. Code for reading preamble.
  276. ------------------------------------------------------------------------ }
  277. Procedure DoPreamble;
  278. var line : String;
  279. begin
  280. {$ifdef debug}
  281. writeln ('Starting preamble');
  282. {$endif}
  283. Getline (line);
  284. while pos('= FORM =',line)=0 do
  285. begin
  286. ProcessPreAmbleLine(line);
  287. GetLine(Line)
  288. end;
  289. end;
  290. { ------------------------------------------------------------------------
  291. Code for reading 1 object.
  292. ------------------------------------------------------------------------ }
  293. Procedure ProcessControlLine (PC : PControl; const S : String);
  294. Var Key,Value : String;
  295. i,k : ContProps;
  296. ppos : word;
  297. begin
  298. if s='' then exit;
  299. ppos:=pos(':',s);
  300. if ppos=0 then
  301. exit;
  302. Key:=Copy(s,1,ppos-1);
  303. Value:=Copy(s,ppos+2,length(s)-ppos-1);
  304. K:=CPInvalid;
  305. For i:=CPclass to CPInvalid do
  306. if ContPropNames[i]=Key then k:=i;
  307. if K=CPinvalid then
  308. begin
  309. EmitLineError('Unknown keyword'+key);
  310. exit
  311. end;
  312. PC^.props[k]:=value;
  313. end;
  314. Procedure ProcessControl (PC : PControl);
  315. var line : String;
  316. begin
  317. {$ifdef debug}
  318. Writeln ('Starting Control');
  319. {$endif}
  320. Getline(Line);
  321. while Line<>'' do
  322. begin
  323. ProcessControlLine (PC,line);
  324. Getline(Line);
  325. end;
  326. Getline(Line)
  327. end;
  328. { ------------------------------------------------------------------------
  329. Code for reading 1 form.
  330. ------------------------------------------------------------------------ }
  331. Procedure ProcessFormLine (PF : PFormRec; const S : String);
  332. Var Key,Value : String;
  333. i,k : FormProps;
  334. ppos,code : word;
  335. begin
  336. if s='' then exit;
  337. ppos:=pos(':',s);
  338. if ppos=0 then
  339. exit;
  340. Key:=Copy(s,1,ppos-1);
  341. Value:=Copy(s,ppos+2,length(s)-ppos-1);
  342. K:=FPInvalid;
  343. For i:=FPName to FPInvalid do
  344. if FormPropNames[i]=Key then k:=i;
  345. if K=FPinvalid then
  346. begin
  347. EmitLineError('Unknown keyword'+key);
  348. exit
  349. end;
  350. case k of
  351. FPname : PF^.name:=value;
  352. FPWidth : PF^.width:=value;
  353. FPHeight : PF^.height:=value;
  354. FPNumObjs : begin
  355. val(value,Nrcontrols,code);
  356. If Code<>0 then EmitLineError('Invalid number of objects : '+value)
  357. end;
  358. end;
  359. end;
  360. Procedure ProcessForm (PF : PFormRec);
  361. Var line : String;
  362. CurrentControl : PControl;
  363. I : Integer;
  364. begin
  365. {$ifdef debug}
  366. writeln('Starting form');
  367. {$endif}
  368. NrControls:=0;
  369. with PF^ do
  370. begin
  371. name:='';
  372. Width:='';
  373. Height:='';
  374. Controls:=nil;
  375. GetLine(Line);
  376. while line<>'' do
  377. begin
  378. ProcessFormLine(PF,Line);
  379. GetLine(Line);
  380. end;
  381. Getline(Line);
  382. If NrControls=0 then
  383. Controls:=nil
  384. else
  385. begin
  386. New (Controls);
  387. CurrentControl:=Controls;
  388. for i:=1 to nrcontrols do
  389. begin
  390. ProcessControl(CurrentControl);
  391. if i<NrControls then
  392. New(CurrentControl^.NextControl)
  393. else
  394. CurrentControl^.NextControl:=nil;
  395. CurrentControl:=CurrentControl^.NextControl
  396. end; { for }
  397. end; { Else }
  398. end; { With }
  399. end;
  400. { ------------------------------------------------------------------------
  401. Code for reading the forms.
  402. ------------------------------------------------------------------------ }
  403. Procedure DoForms;
  404. Var
  405. i : Longint;
  406. CurrentForm: PformRec;
  407. begin
  408. FormRoot:=Nil;
  409. if NrForms=0 then exit;
  410. new(FormRoot);
  411. Currentform:=FormRoot;
  412. for i:=1 to nrforms do
  413. begin
  414. ProcessForm (CurrentForm);
  415. If i=nrforms then
  416. Currentform^.NextForm:=nil
  417. else
  418. New(CurrentForm^.NextForm);
  419. CurrentForm:=CurrentForm^.NextForm;
  420. end;
  421. end;
  422. { ------------------------------------------------------------------------
  423. Code for reading the postamble.
  424. ------------------------------------------------------------------------ }
  425. Procedure DoPostamble;
  426. begin
  427. end;
  428. { ------------------------------------------------------------------------
  429. Code for writing the output file.
  430. ------------------------------------------------------------------------ }
  431. Procedure OpenOutFile;
  432. var info : stat;
  433. begin
  434. FileName:=Copy(Filename,1,Length(Filename)-3)+'.pp';
  435. if fpstat(FileName,info)<>-1 Then
  436. begin
  437. { File exists, move to .bak}
  438. fplink (FileName,FileName+'.bak');
  439. fpunlink(FileName);
  440. end;
  441. assign(outfile,filename);
  442. {$push}{$i-}
  443. rewrite(outfile);
  444. {$pop}
  445. if ioresult<>0 then
  446. begin
  447. EmitError('Couldn''t open output file : '+filename);
  448. halt(1)
  449. end;
  450. end;
  451. Procedure CloseOutFile;
  452. begin
  453. Close(OutFile);
  454. end;
  455. { ------------------------------------------------------------------------
  456. Code to emit Header/variable/type declarations
  457. ------------------------------------------------------------------------ }
  458. Procedure EmitType (fp : Pformrec);
  459. var cp : PControl;
  460. begin
  461. writeln (OutFile,' TFD_',fp^.name,' = record');
  462. writeln (OutFile,' ',fp^.name,' : PFL_FORM;');
  463. writeln (OutFile,' vdata : Pointer;');
  464. writeln (OutFile,' ldata : Longint;');
  465. cp:=fp^.controls;
  466. {Skip first control, is formbackground }
  467. if cp<>nil then cp:=cp^.nextcontrol;
  468. while cp<>nil do
  469. begin
  470. if cp^.props[CPclass]<>'FL_END_GROUP' then
  471. begin
  472. write (Outfile,' ',cp^.props[CPname]);
  473. if cp^.nextcontrol<>nil then
  474. writeln (OutFile,',')
  475. else
  476. writeln (OutFile,' : PFL_OBJECT;');
  477. end;
  478. cp:=cp^.nextcontrol;
  479. end;
  480. writeln (OutFile,' end;');
  481. writeln (OutFile,' PFD_',fp^.name,' = ^TFD_',fp^.name,';');
  482. writeln (OutFile);
  483. end;
  484. Procedure EmitVar (fp : Pformrec);
  485. var cp : PControl;
  486. begin
  487. writeln (OutFile,' ',fp^.name,' : PFL_FORM;');
  488. cp:=fp^.controls;
  489. {Skip first control, is formbackground }
  490. if cp<>nil then cp:=cp^.nextcontrol;
  491. while cp<>nil do
  492. begin
  493. if cp^.props[CPclass]<>'FL_END_GROUP' then
  494. begin
  495. write (Outfile,' ',cp^.props[CPname]);
  496. if cp^.nextcontrol<>nil then
  497. writeln (OutFile,',')
  498. else
  499. writeln (OutFile,' : PFL_OBJECT;');
  500. end;
  501. cp:=cp^.nextcontrol;
  502. end;
  503. writeln (OutFile);
  504. end;
  505. Procedure EmitHeader;
  506. var fp : PFormRec;
  507. begin
  508. if OptionsSet[2] then
  509. write (OutFile,'Program ')
  510. else
  511. write (OutFile,'Unit ');
  512. writeln (OutFile,basename(filename,'.pp'),';');
  513. writeln (OutFile);
  514. writeln (OutFile,'{ Form definition file generated by fd2pascal }');
  515. writeln (Outfile);
  516. if not OptionsSet[2] then
  517. begin
  518. writeln (OutFile,'Interface');
  519. writeln (OutFile);
  520. end;
  521. writeln (OutFile,'Uses forms;');
  522. writeln (OutFile);
  523. writeln (OutFile,' { Variable / Type definitions. }');
  524. if Optionsset[3] then
  525. writeln (OutFile,'Var')
  526. else
  527. writeln (OutFile,'Type');
  528. fp:=FormRoot;
  529. While fp<>nil do
  530. begin
  531. if not optionsset[3] then
  532. EmitType(fp) { Emit Type definitions }
  533. else
  534. EmitVar(fp); { Emit Variable declaration}
  535. fp:=fp^.nextform;
  536. end;
  537. if not optionsset[2] then
  538. begin
  539. { No program, we must emit interface stuff }
  540. if not (optionsset[3]) then
  541. begin
  542. { Emit normal interface declarations
  543. -> functions }
  544. fp:=formroot;
  545. while fp<>nil do
  546. begin
  547. with fp^ do
  548. writeln (OutFile,'Function create_form_',name,' : PFD_',name,';');
  549. fp:=fp^.nextform;
  550. end;
  551. end
  552. else
  553. begin
  554. { Emit alternate interface declaration
  555. -> 1 function to create all forms.}
  556. writeln (OutFile,'Procedure Create_The_Forms;');
  557. end;
  558. writeln (OutFile);
  559. writeln (OutFile,'Implementation');
  560. end
  561. else
  562. begin
  563. { We must make a program. }
  564. if not(optionsset[3]) then
  565. begin
  566. { Normal format, so we need to emit variables for the forms.}
  567. writeln (OutFile,'Var');
  568. fp:=formroot;
  569. while fp<>nil do
  570. begin
  571. writeln (OutFile,' ',fp^.name,' : PFD_',fp^.name,';');
  572. fp:=fp^.nextform;
  573. end;
  574. writeln (OutFile);
  575. end;
  576. end;
  577. writeln (OutFile);
  578. end;
  579. { ------------------------------------------------------------------------
  580. Code to emit footer/main program
  581. ------------------------------------------------------------------------ }
  582. Procedure EmitCreateforms;
  583. var fp : PFormRec;
  584. begin
  585. writeln (OutFile,'Procedure Create_The_Forms;');
  586. writeln (OutFile);
  587. writeln (OutFile,'begin');
  588. fp:=FormRoot;
  589. while fp<>nil do
  590. begin
  591. writeln(OutFile,'create_form_',fp^.name,';');
  592. fp:=fp^.nextform;
  593. end;
  594. writeln (outFile,'End;');
  595. writeln (OutFile);
  596. end;
  597. Procedure EmitAlternateMain;
  598. begin
  599. { Alternate format, we just call creatallforms to create all forms}
  600. writeln (OutFile,'Create_The_Forms;');
  601. writeln (OutFile,' fl_show_form(',formroot^.name,
  602. ',FL_PLACE_CENTER,FL_FULLBORDER,''',
  603. FormRoot^.name,''');');
  604. end;
  605. Procedure EmitMain;
  606. var fp : PFormRec;
  607. begin
  608. { variables are emitted in the header }
  609. fp:=formroot;
  610. { Create all forms }
  611. while fp<>nil do
  612. begin
  613. writeln (OutFile,' ',fp^.name,' :=Create_Form_',fp^.name,';');
  614. fp:=fp^.nextform;
  615. end;
  616. { Show the first form }
  617. writeln (OutFile,' fl_show_form(',formroot^.name,'^.',Formroot^.name,
  618. ',FL_PLACE_CENTER,FL_FULLBORDER,''',
  619. FormRoot^.name,''');');
  620. end;
  621. Procedure EmitFooter;
  622. begin
  623. if OptionsSet[3] then {Alternate format.}
  624. EmitCreateForms;
  625. if Optionsset[2] then
  626. begin
  627. {Emit Main Program}
  628. writeln (OutFile);
  629. writeln (OutFile,'Begin');
  630. writeln (OutFile,' fl_initialize (@argc,argv,''',
  631. basename(Filename,'.pp'),''',nil,0);');
  632. if Not(OptionsSet[3]) then
  633. EmitMain
  634. else
  635. EmitAlternateMain;
  636. writeln (OutFile,' fl_do_forms;');
  637. end
  638. else
  639. writeln (OutFile,'begin');
  640. writeln (OutFile,'end.')
  641. end;
  642. { ------------------------------------------------------------------------
  643. Code to emit properties
  644. ------------------------------------------------------------------------ }
  645. Function EmitString(S : string) : String;
  646. var temp : String;
  647. i : longint;
  648. begin
  649. temp:='''';
  650. for i:=1 to length(s) do
  651. if s[i]<>'''' then temp:=temp+s[i] else temp:=temp+'''''';
  652. Temp:=temp+'''';
  653. EmitString:=temp;
  654. end;
  655. Procedure EmitBoxtype (cp : PControl;ObjClass : ObjClasses);
  656. begin
  657. {$ifdef debug}
  658. writeln ('EmitBoxType called with args:');
  659. writeln (cp^.props[cpboxtype]);
  660. writeln (defprops[objclass,APboxtype]);
  661. writeln ('for object : ',defprops[objclass,apclass]);
  662. writeln ('With object : ',cp^.props[cpclass]);
  663. {$endif}
  664. if cp^.props[cpboxtype]<>defprops[objclass,APboxtype] then
  665. writeln (OutFile,' fl_set_object_boxtype(obj,',
  666. cp^.props[cpboxtype],');')
  667. end;
  668. Procedure EmitColors (cp : PControl;ObjClass : ObjClasses);
  669. var temp : string;
  670. begin
  671. if cp^.props[cpcolors]<>defprops[objclass,APcolors] then
  672. begin
  673. temp:=cp^.props[cpcolors];
  674. if pos(' ',temp)=0 then exit;
  675. temp[pos(' ',temp)]:=',';
  676. writeln (OutFile,' fl_set_object_color(obj,',temp,');');
  677. end;
  678. end;
  679. Procedure EmitAlignment (cp : PControl;ObjClass : ObjClasses);
  680. begin
  681. if cp^.props[cpalignment]<>defprops[objclass,APalignment] then
  682. writeln (OutFile,' fl_set_object_alignment(obj,',
  683. cp^.props[cpalignment],');');
  684. end;
  685. Procedure EmitLcol (cp : PControl;ObjClass : ObjClasses);
  686. begin
  687. if cp^.props[cplcol]<>defprops[objclass,APlcol] then
  688. writeln (OutFile,' fl_set_object_lcol(obj,',
  689. cp^.props[cplcol],');');
  690. end;
  691. Procedure EmitSize (cp : PControl;ObjClass : ObjClasses);
  692. begin
  693. if cp^.props[cpsize]<>defprops[objclass,APsize] then
  694. writeln (OutFile,' fl_set_object_lsize(obj,',
  695. cp^.props[cpsize],');');
  696. end;
  697. Procedure EmitStyle (cp : PControl;ObjClass : ObjClasses);
  698. begin
  699. if cp^.props[cpstyle]<>defprops[objclass,APstyle] then
  700. writeln (OutFile,' fl_set_object_lstyle(obj,',
  701. cp^.props[cpstyle],');');
  702. end;
  703. Procedure EmitGravity (cp : PControl;ObjClass : ObjClasses);
  704. var temp: string;
  705. begin
  706. if cp^.props[cpstyle]<>'FL_NoGravity FL_NoGravity' then
  707. begin
  708. temp:=cp^.props[cpstyle];
  709. if pos(' ',temp)=0 then exit;
  710. temp[pos(' ',temp)]:=',';
  711. writeln (OutFile,' fl_set_object_gravity(obj,',
  712. temp,');');
  713. end;
  714. end;
  715. Procedure EmitProperties (Cp : PControl; Objclass : ObjClasses);
  716. Var i : AdjProps;
  717. begin
  718. for i:=APboxtype to APgravity do
  719. if DefProps[ObjClass,i]<>'' then
  720. EmitProcs[i](cp,objclass);
  721. end;
  722. { ------------------------------------------------------------------------
  723. Code to emit objects
  724. ------------------------------------------------------------------------ }
  725. Procedure EmitObject(cp : PControl);
  726. var temp : string;
  727. I : Longint;
  728. j,k : ObjClasses;
  729. begin
  730. with cp^ do
  731. begin
  732. temp:=lowercase(props[CPclass]);
  733. delete(temp,1,3);
  734. if temp='begin_group' then
  735. begin
  736. writeln (OutFile);
  737. write (OutFile,' ');
  738. if not (Optionsset[3]) then Write (OutFile,'fdui^.');
  739. writeln (OutFile,props[cpname],':=fl_bgn_group;');
  740. exit;
  741. end
  742. else if temp='end_group' then
  743. begin
  744. writeln (OutFile,' fl_end_group;');
  745. writeln (OutFile);
  746. exit;
  747. end;
  748. { Normal object. Emit creation code. }
  749. write (OutFile,' obj:=fl_add_',temp,' (FL_',props[Cptype],',');
  750. temp:=props[cpbox];
  751. for i:=1 to 3 do
  752. begin
  753. write (OutFile,copy(temp,1,pos(' ',temp)-1),',');
  754. delete (temp,1,pos(' ',temp));
  755. end;
  756. writeln (OutFile,temp,',',EmitString(props[cplabel]),');');
  757. { Emit Callback code if needed }
  758. if props[cpcallback]<>'' then
  759. begin
  760. write (OutFile,' fl_set_object_callback(obj,PFL_CALLBACKPTR(@');
  761. write (OutFile,props[CPcallback],'),');
  762. if props[CPargument]<>'' then
  763. writeln (OutFile,props[CPargument],');')
  764. else
  765. writeln (OutFile,'0);');
  766. end;
  767. { If known object, start emitting properties }
  768. temp:=props[CPclass];
  769. delete(temp,1,3);
  770. k:=FL_INVALID;
  771. for j:=FL_BUTTON to FL_FOLDER do
  772. if temp=DefProps[j,apclass] then k:=j;
  773. if k<>FL_INVALID then
  774. begin
  775. { Emit defaults }
  776. EmitProperties (cp,k);
  777. { If A class-specific emitter exists, call it.}
  778. if Assigned(ClassEmitters[k]) then
  779. ClassEmitters[k] (cp,k);
  780. end;
  781. { Assign to needed object. }
  782. if Optionsset[3] then
  783. Writeln (OutFile,' ',props[cpname],':=obj;')
  784. else
  785. Writeln (OutFile,' fdui^.',props[cpname],':=obj;');
  786. end;
  787. end;
  788. { ------------------------------------------------------------------------
  789. Code to emit forms
  790. ------------------------------------------------------------------------ }
  791. Procedure EmitForm(fp : PFormRec);
  792. Var
  793. cp : PControl;
  794. begin
  795. with fp^ do
  796. begin
  797. if Optionsset[3] then
  798. begin
  799. writeln (OutFile,'Procedure create_form_',name,';');
  800. writeln (OutFile);
  801. writeln (OutFile,'Var obj : PFL_OBJECT;');
  802. writeln (OutFile);
  803. writeln (OutFile,'Begin');
  804. writeln (OutFile,' If ',name,'<>nil then exit;');
  805. write (OutFile,' ',name);
  806. end
  807. else
  808. begin
  809. writeln (OutFile,'Function create_form_',name,' : PFD_',name,';');
  810. writeln (OutFile);
  811. writeln (OutFile,'Var obj : PFL_OBJECT;');
  812. writeln (OutFile,' fdui : PFD_',name,';');
  813. writeln (OutFile);
  814. writeln (OutFile,'Begin');
  815. writeln (OutFile,' New(fdui);');
  816. write (OutFile,' fdui^.',name);
  817. end;
  818. writeln (OutFile,':=fl_bgn_form(FL_NO_BOX,'
  819. ,width,','
  820. ,height,');');
  821. cp:=controls;
  822. writeln (OutFile,' obj:=fl_add_box(',cp^.props[CPboxtype],',0,0,',
  823. width,',',
  824. height,',',
  825. EmitString (cp^.props[CPname]),');');
  826. cp:=cp^.nextcontrol;
  827. { Emit all objects }
  828. while cp<>nil do
  829. begin
  830. EmitObject(cp);
  831. cp:=cp^.nextcontrol;
  832. end;
  833. writeln (OutFile,' fl_end_form;');
  834. if Optionsset[4] then
  835. begin
  836. { Emit Compensation code }
  837. write (OutFile,' fl_adjust_form_size(');
  838. if not(OptionsSet[3]) then write (OutFile,'fdui^.');
  839. writeln(OutFile,fp^.name,');');
  840. end;
  841. if not(OptionsSet[3]) then
  842. begin
  843. writeln (OutFile,' fdui^.',fp^.name,'^.fdui:=fdui;');
  844. writeln (OutFile,' create_form_',fp^.name,':=fdui;');
  845. end;
  846. writeln (OutFile,'end;');
  847. writeln (OutFile);
  848. end;
  849. end;
  850. Procedure EmitForms;
  851. var
  852. fp : PformRec;
  853. begin
  854. { Start emitting forms }
  855. fp:=Formroot;
  856. while fp<>nil do
  857. begin
  858. EmitForm(fp);
  859. fp:=fp^.nextform;
  860. end;
  861. end;
  862. { ------------------------------------------------------------------------
  863. Code to emit callbacks
  864. ------------------------------------------------------------------------ }
  865. Procedure CollectCallbacks;
  866. Var CurrentCb,CBwalk : PCBrec;
  867. fp : PformRec;
  868. cp : PControl;
  869. begin
  870. CbRoot:=nil;
  871. CurrentCB:=cbroot;
  872. fp:=formroot;
  873. while fp<>nil do
  874. begin
  875. cp:=fp^.controls;
  876. while cp<>nil do
  877. begin
  878. if cp^.props[CPcallback]<>'' then
  879. if cbroot<>nil then
  880. begin
  881. cbwalk:=cbroot;
  882. while cbwalk<>nil do
  883. if upcase(cbwalk^.name)=upcase(cp^.props[CPcallback]) then
  884. break
  885. else
  886. cbwalk:=cbwalk^.next;
  887. if cbwalk=nil then
  888. begin
  889. new(currentcb^.next);
  890. currentcb:=currentcb^.next;
  891. currentcb^.name:=cp^.props[CPcallback];
  892. currentcb^.next:=nil;
  893. end;
  894. end
  895. else
  896. begin
  897. new(cbroot);
  898. currentcb:=cbroot;
  899. cbroot^.name:=cp^.props[CPcallback];
  900. cbroot^.next:=nil;
  901. end;
  902. cp:=cp^.nextcontrol;
  903. end;
  904. fp:=fp^.nextform;
  905. end;
  906. end;
  907. Procedure EmitCallback (Const s : string);
  908. begin
  909. writeln (OutFile,'Procedure ',s,' (Sender: PFL_OBJECT; Data : Longint); export;');
  910. writeln (OutFile);
  911. writeln (OutFile,'begin');
  912. writeln (OutFile,' { Place your code here }');
  913. writeln (OutFile,'end;');
  914. writeln (OutFile);
  915. end;
  916. Procedure EmitCallBacks;
  917. var cb : pcbrec;
  918. begin
  919. { See if we must emit callback stubs }
  920. If Optionsset[1] then
  921. begin
  922. cb:=cbroot;
  923. while cb<>nil do
  924. begin
  925. EmitCallBack(cb^.Name);
  926. cb:=cb^.next;
  927. end;
  928. end;
  929. end;
  930. { ------------------------------------------------------------------------
  931. EmitterTable initialization Code
  932. ------------------------------------------------------------------------ }
  933. Procedure EmitDummy (cp : PControl;ObjClass : ObjClasses);
  934. begin
  935. end;
  936. Procedure InitEmitters;
  937. var i : objclasses;
  938. begin
  939. EmitProcs[APClass]:=@EmitDummy;
  940. EmitProcs[APBoxtype]:=@EmitBoxType;
  941. EmitProcs[APColors]:=@EmitColors;
  942. EmitProcs[APAlignment]:=@EmitAlignment;
  943. EmitProcs[APlcol]:=@EmitLcol;
  944. EmitProcs[APsize]:=@EmitSize;
  945. EmitProcs[APStyle]:=@EmitStyle;
  946. EmitProcs[APgravity]:=@EmitGravity;
  947. for i:=FL_INVALID to FL_FOLDER do
  948. ClassEmitters[i]:=EmitProp(Nil);
  949. end;
  950. { ------------------------------------------------------------------------
  951. Main program Code
  952. ------------------------------------------------------------------------ }
  953. begin
  954. { Process options }
  955. DoOptions;
  956. { Read input file }
  957. OpenFile;
  958. DoPreamble;
  959. DoForms;
  960. DoPostamble;
  961. CloseFile;
  962. { Write output file }
  963. OpenOutfile;
  964. InitEmitters;
  965. CollectCallbacks;
  966. EmitHeader;
  967. EmitCallbacks;
  968. EmitForms;
  969. EmitFooter;
  970. CloseOutFile;
  971. end.