fd2pascal.pp 30 KB

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