fd2pascal.pp 31 KB

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