fd2pascal.pp 30 KB

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