MainUnit.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. unit MainUnit;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, math, FpImage, Fresnel.Forms, Fresnel.DOM, Fresnel.Controls, Fresnel.Events,
  6. FCL.Events, Fresnel.Classes, Fresnel.DemoRadioButton, Fresnel.DemoSlider;
  7. type
  8. { TMyDrawing }
  9. TMyDrawing = class(TImage)
  10. protected
  11. procedure DoRender(aRenderer: IFresnelRenderer); override;
  12. end;
  13. { TMainForm }
  14. TMainForm = class(TFresnelForm)
  15. procedure MainFormCreate(Sender: TObject);
  16. private
  17. procedure OnVerticalAlignClick(Event: TAbstractEvent);
  18. procedure PxSliderChange(Sender: TObject);
  19. procedure SetVerticalAlign(s: string);
  20. public
  21. VerticalAlignRadiogroup: TDiv;
  22. VerticalAlignBaselineRB: TDemoRadioButton;
  23. VerticalAlignSubRB: TDemoRadioButton;
  24. VerticalAlignSuperRB: TDemoRadioButton;
  25. VerticalAlignTextTopRB: TDemoRadioButton;
  26. VerticalAlignTextBottomRB: TDemoRadioButton;
  27. VerticalAlignMiddleRB: TDemoRadioButton;
  28. VerticalAlignPxRB: TDemoRadioButton;
  29. PxSlider: TDemoSlider;
  30. TextNormalDiv, TextBigDiv, TextSmallDiv, TextImageDiv: TDiv;
  31. NormalCaption, NormalLabel, BigCaption, BigLabel, SmallCaption, SmallLabel, ImageCaption: TLabel;
  32. MyDrawing: TMyDrawing;
  33. end;
  34. var
  35. MainForm: TMainForm;
  36. implementation
  37. {$R *.lfm}
  38. { TMyDrawing }
  39. procedure TMyDrawing.DoRender(aRenderer: IFresnelRenderer);
  40. var
  41. R: TFresnelRect;
  42. Points: TFresnelPointArray;
  43. w, h: TFresnelLength;
  44. begin
  45. R:=UsedContentBox;
  46. h:=Min(R.Width,R.Height);
  47. w:=h*0.66;
  48. Points:=[];
  49. SetLength(Points,3);
  50. Points[0].X:=R.Left+(r.Width-w)/2;
  51. Points[0].Y:=R.Top+(r.Height-h)/2;
  52. Points[1].X:=Points[0].X+w;
  53. Points[1].Y:=(R.Top+R.Bottom)/2;
  54. Points[2].X:=Points[0].X;
  55. Points[2].Y:=R.Bottom-(r.Height-h)/2;
  56. aRenderer.Polygon(colWhite,@Points[0],3);
  57. end;
  58. { TMainForm }
  59. procedure TMainForm.MainFormCreate(Sender: TObject);
  60. function AddDiv(aName: string; aParent: TFresnelElement): TDiv;
  61. begin
  62. Result:=TDiv.Create(Self);
  63. Result.Name:=aName;
  64. Result.Parent:=aParent;
  65. end;
  66. function AddLabel(aName, aCaption: string; aParent: TFresnelElement): TLabel;
  67. begin
  68. Result:=TLabel.Create(Self);
  69. Result.Name:=aName;
  70. Result.Caption:=aCaption;
  71. Result.Parent:=aParent;
  72. end;
  73. function AddRadioButton(aName, aCaption: string; aParent: TFresnelElement;
  74. const OnClick: TFresnelEventHandler = nil): TDemoRadioButton;
  75. begin
  76. Result:=TDemoRadioButton.Create(Self);
  77. Result.Name:=aName;
  78. Result.Caption:=aCaption;
  79. Result.Parent:=aParent;
  80. Result.AddEventListener(evtClick,OnClick);
  81. end;
  82. function AddSlider(aName, aCaption: string; aParent: TFresnelElement;
  83. MinPos, MaxPos: TFresnelLength; ValueFormat: string): TDemoSlider;
  84. begin
  85. Result:=TDemoSlider.Create(Self);
  86. Result.Name:=aName;
  87. Result.Caption:=aCaption;
  88. Result.MinPosition:=MinPos;
  89. Result.MaxPosition:=MaxPos;
  90. Result.ValueFormat:=ValueFormat;
  91. Result.Parent:=aParent;
  92. end;
  93. begin
  94. Style:='font-size: 15px;';
  95. Stylesheet.Text:=':root { font-size: 15px; }'+LineEnding
  96. +'#TextDiv { font-size: 30px; }'+LineEnding;
  97. VerticalAlignRadiogroup:=AddDiv('VerticalAlignRadiogroup',Self);
  98. VerticalAlignRadiogroup.Style:='padding: 1em;';
  99. VerticalAlignBaselineRB:=AddRadioButton('VerticalAlignBaselineRB','baseline',
  100. VerticalAlignRadiogroup,@OnVerticalAlignClick);
  101. VerticalAlignBaselineRB.Checked:=true;
  102. VerticalAlignSubRB:=AddRadioButton('VerticalAlignSubRB','sub',
  103. VerticalAlignRadiogroup,@OnVerticalAlignClick);
  104. VerticalAlignSuperRB:=AddRadioButton('VerticalAlignSuperRB','super',
  105. VerticalAlignRadiogroup,@OnVerticalAlignClick);
  106. VerticalAlignTextTopRB:=AddRadioButton('VerticalAlignTextTopRB','text-top',
  107. VerticalAlignRadiogroup,@OnVerticalAlignClick);
  108. VerticalAlignTextBottomRB:=AddRadioButton('VerticalAlignTextBottomRB','text-bottom',
  109. VerticalAlignRadiogroup,@OnVerticalAlignClick);
  110. VerticalAlignMiddleRB:=AddRadioButton('VerticalAlignMiddleRB','middle',
  111. VerticalAlignRadiogroup,@OnVerticalAlignClick);
  112. VerticalAlignPxRB:=AddRadioButton('VerticalAlignPxRB','px',
  113. VerticalAlignRadiogroup,@OnVerticalAlignClick);
  114. PxSlider:=AddSlider('PxSlider','',VerticalAlignRadiogroup,-10,10,'%fpx');
  115. PxSlider.Style:='margin-left: 3em; width: 10em;';
  116. PxSlider.OnChange:=@PxSliderChange;
  117. // normal size label
  118. TextNormalDiv:=AddDiv('TextNormalDiv',Self);
  119. TextNormalDiv.Style:='padding: 1em;';
  120. NormalCaption:=AddLabel('NormalCaption','Label:',TextNormalDiv);
  121. NormalCaption.Style:='margin-right: .5em; background: #ccc;';
  122. NormalLabel:=AddLabel('NormalLabel','Fresnel',TextNormalDiv);
  123. NormalLabel.Style:='background: #ccc;';
  124. // big size label
  125. TextBigDiv:=AddDiv('TextBigDiv',Self);
  126. TextBigDiv.Style:='padding: 1em;';
  127. BigCaption:=AddLabel('BigCaption','Label:',TextBigDiv);
  128. BigCaption.Style:='margin-right: .5em; background: #ccc;';
  129. BigLabel:=AddLabel('BigLabel','Fresnel',TextBigDiv);
  130. BigLabel.Style:='font-size: 200%; background: #ccc;';
  131. // small size label
  132. TextSmallDiv:=AddDiv('TextSmallDiv',Self);
  133. TextSmallDiv.Style:='padding: 1em;';
  134. SmallCaption:=AddLabel('SmallCaption','Label:',TextSmallDiv);
  135. SmallCaption.Style:='margin-right: .5em; background: #ccc;';
  136. SmallLabel:=AddLabel('SmallLabel','Fresnel',TextSmallDiv);
  137. SmallLabel.Style:='font-size: 50%; background: #ccc;';
  138. // label+image
  139. TextImageDiv:=AddDiv('TextImageDiv',Self);
  140. TextImageDiv.Style:='padding: 1em;';
  141. ImageCaption:=AddLabel('ImageCaption','Image:',TextImageDiv);
  142. ImageCaption.Style:='margin-right: .5em; background: #ccc;';
  143. MyDrawing:=TMyDrawing.Create(Self);
  144. MyDrawing.Name:='MyDrawing';
  145. MyDrawing.Style:='display: inline flow-root; width: .8em; height: .8em; background: blue;';
  146. MyDrawing.Parent:=TextImageDiv;
  147. end;
  148. procedure TMainForm.OnVerticalAlignClick(Event: TAbstractEvent);
  149. var
  150. s: String;
  151. begin
  152. if Event.Sender=VerticalAlignSubRB then
  153. s:='sub'
  154. else if Event.Sender=VerticalAlignSuperRB then
  155. s:='super'
  156. else if Event.Sender=VerticalAlignTextTopRB then
  157. s:='text-top'
  158. else if Event.Sender=VerticalAlignTextBottomRB then
  159. s:='text-bottom'
  160. else if Event.Sender=VerticalAlignMiddleRB then
  161. s:='middle'
  162. else if Event.Sender=VerticalAlignPxRB then
  163. s:=FloatToCSSPx(PxSlider.SliderPosition)
  164. else
  165. s:='baseline';
  166. SetVerticalAlign(s);
  167. end;
  168. procedure TMainForm.PxSliderChange(Sender: TObject);
  169. begin
  170. if VerticalAlignPxRB.Checked then
  171. SetVerticalAlign(FloatToCSSPx(PxSlider.SliderPosition));
  172. end;
  173. procedure TMainForm.SetVerticalAlign(s: string);
  174. begin
  175. NormalLabel.SetStyleAttr('vertical-align',s);
  176. BigLabel.SetStyleAttr('vertical-align',s);
  177. SmallLabel.SetStyleAttr('vertical-align',s);
  178. MyDrawing.SetStyleAttr('vertical-align',s);
  179. end;
  180. end.