MainUnit.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. unit MainUnit;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Fresnel.Forms, Fresnel.DOM, Fresnel.Controls, Fresnel.Events, FCL.Events,
  6. Fresnel.Classes, Fresnel.DemoRadioButton, Fresnel.DemoSlider;
  7. type
  8. { TMainForm }
  9. TMainForm = class(TFresnelForm)
  10. procedure MainFormCreate(Sender: TObject);
  11. private
  12. procedure OnLineClick(Event: TAbstractEvent);
  13. procedure OnRangeClick(Event: TAbstractEvent);
  14. procedure OnStyleClick(Event: TAbstractEvent);
  15. procedure OnThicknessClick(Event: TAbstractEvent);
  16. procedure OnThicknessPxChanged(Sender: TObject);
  17. public
  18. LineRadiogroup: TDiv;
  19. LineLabel: TLabel;
  20. LineUnderlineRadioButton: TDemoRadioButton;
  21. LineOverlineRadioButton: TDemoRadioButton;
  22. LineLineThroughRadioButton: TDemoRadioButton;
  23. LineGrammarErrorRadioButton: TDemoRadioButton;
  24. LineSpellingErrorRadioButton: TDemoRadioButton;
  25. StyleRadiogroup: TDiv;
  26. StyleLabel: TLabel;
  27. StyleSolidRadioButton: TDemoRadioButton;
  28. StyleDoubleRadioButton: TDemoRadioButton;
  29. StyleDashedRadioButton: TDemoRadioButton;
  30. StyleDottedRadioButton: TDemoRadioButton;
  31. StyleWavyRadioButton: TDemoRadioButton;
  32. ThicknessRadiogroup: TDiv;
  33. ThicknessLabel: TLabel;
  34. ThicknessAutoRadioButton: TDemoRadioButton;
  35. ThicknessFromFontRadioButton: TDemoRadioButton;
  36. ThicknessPxRadioButton: TDemoRadioButton;
  37. ThicknessPxSlider: TDemoSlider;
  38. RangeRadiogroup: TDiv;
  39. RangeLabel: TLabel;
  40. RangeLabelRadioButton: TDemoRadioButton;
  41. RangeSpanRadioButton: TDemoRadioButton;
  42. RangeDivRadioButton: TDemoRadioButton;
  43. El: TFresnelElement;
  44. TextDiv: TDiv;
  45. Span1: TSpan;
  46. Label1, Label2, Label3, Label4, Label5: TLabel;
  47. end;
  48. var
  49. MainForm: TMainForm;
  50. implementation
  51. {$R *.lfm}
  52. { TMainForm }
  53. procedure TMainForm.MainFormCreate(Sender: TObject);
  54. function AddLabel(aName, aCaption: string; aParent: TFresnelElement): TLabel;
  55. begin
  56. Result:=TLabel.Create(Self);
  57. Result.Name:=aName;
  58. Result.Caption:=aCaption;
  59. Result.Parent:=aParent;
  60. end;
  61. function AddSpan(aName: string; aParent: TFresnelElement): TSpan;
  62. begin
  63. Result:=TSpan.Create(Self);
  64. Result.Name:=aName;
  65. Result.Parent:=aParent;
  66. end;
  67. function AddDiv(aName: string; aParent: TFresnelElement): TDiv;
  68. begin
  69. Result:=TDiv.Create(Self);
  70. Result.Name:=aName;
  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, CurPos: TFresnelLength; ValueFormat: string;
  84. const OnChange: TNotifyEvent): TDemoSlider;
  85. begin
  86. Result:=TDemoSlider.Create(Self);
  87. Result.Name:=aName;
  88. Result.Caption:=aCaption;
  89. Result.MinPosition:=MinPos;
  90. Result.MaxPosition:=MaxPos;
  91. Result.SliderPosition:=CurPos;
  92. Result.ValueFormat:=ValueFormat;
  93. Result.OnChange:=OnChange;
  94. Result.Parent:=aParent;
  95. end;
  96. begin
  97. StyleSheet.Text:=':root { font-size: 12px; }'+LineEnding
  98. +'#TextDiv { font-size: 30px; }'+LineEnding
  99. +'#TextDiv label { margin-right: .5em; }';
  100. // line radiogroup
  101. LineRadiogroup:=AddDiv('LineRadiogroup',Self);
  102. LineRadiogroup.Style:='padding: 1em;';
  103. LineLabel:=AddLabel('LineLabel','text-decoration-line',LineRadiogroup);
  104. LineUnderlineRadioButton:=AddRadioButton('LineUnderlineRadioButton','underline',
  105. LineRadiogroup,@OnLineClick);
  106. LineUnderlineRadioButton.Checked:=true;
  107. LineOverlineRadioButton:=AddRadioButton('LineOverlineRadioButton','overline',
  108. LineRadiogroup,@OnLineClick);
  109. LineLineThroughRadioButton:=AddRadioButton('LineLineThroughRadioButton','line-through',
  110. LineRadiogroup,@OnLineClick);
  111. LineGrammarErrorRadioButton:=AddRadioButton('LineGrammarErrorRadioButton','grammar-error',
  112. LineRadiogroup,@OnLineClick);
  113. LineSpellingErrorRadioButton:=AddRadioButton('LineSpellingErrorRadioButton','spelling-error',
  114. LineRadiogroup,@OnLineClick);
  115. // style radiogroup
  116. StyleRadiogroup:=AddDiv('StyleRadiogroup',Self);
  117. StyleRadiogroup.Style:='padding: 1em;';
  118. StyleLabel:=AddLabel('StyleLabel','text-decoration-style',StyleRadiogroup);
  119. StyleSolidRadioButton:=AddRadioButton('StyleSolidRadioButton','solid',
  120. StyleRadiogroup,@OnStyleClick);
  121. StyleSolidRadioButton.Checked:=true;
  122. StyleDashedRadioButton:=AddRadioButton('StyleDashedRadioButton','dashed',
  123. StyleRadiogroup,@OnStyleClick);
  124. StyleDottedRadioButton:=AddRadioButton('StyleDottedRadioButton','dotted',
  125. StyleRadiogroup,@OnStyleClick);
  126. StyleDoubleRadioButton:=AddRadioButton('StyleDoubleRadioButton','doubled',
  127. StyleRadiogroup,@OnStyleClick);
  128. StyleWavyRadioButton:=AddRadioButton('StyleWavyRadioButton','wavy',
  129. StyleRadiogroup,@OnStyleClick);
  130. // thickness
  131. ThicknessRadiogroup:=AddDiv('ThicknessRadiogroup',Self);
  132. ThicknessRadiogroup.Style:='padding: 1em;';
  133. ThicknessLabel:=AddLabel('ThicknessLabel','text-decoration-thickness',ThicknessRadiogroup);
  134. ThicknessAutoRadioButton:=AddRadioButton('ThicknessAutoRadioButton','auto',
  135. ThicknessRadiogroup,@OnThicknessClick);
  136. ThicknessAutoRadioButton.Checked:=true;
  137. ThicknessFromFontRadioButton:=AddRadioButton('ThicknessFromFontRadioButton','from-font',
  138. ThicknessRadiogroup,@OnThicknessClick);
  139. ThicknessPxRadioButton:=AddRadioButton('ThicknessPxRadioButton','px',
  140. ThicknessRadiogroup,@OnThicknessClick);
  141. ThicknessPxSlider:=AddSlider('ThicknessPxSlider','',ThicknessRadiogroup,
  142. 0,5,1,'%fpx',@OnThicknessPxChanged);
  143. // range radiogroup
  144. RangeRadiogroup:=AddDiv('RangeRadiogroup',Self);
  145. RangeRadiogroup.Style:='padding: 1em;';
  146. RangeLabel:=AddLabel('RangeLabel','Apply to',StyleRadiogroup);
  147. RangeLabelRadioButton:=AddRadioButton('RangeLabelRadioButton','label',
  148. RangeRadiogroup,@OnRangeClick);
  149. RangeLabelRadioButton.Checked:=true;
  150. RangeSpanRadioButton:=AddRadioButton('RangeSpanRadioButton','span',
  151. RangeRadiogroup,@OnRangeClick);
  152. RangeDivRadioButton:=AddRadioButton('RangeDivRadioButton','div',
  153. RangeRadiogroup,@OnRangeClick);
  154. // div, span, label
  155. TextDiv:=AddDiv('TextDiv',Self);
  156. TextDiv.Style:='padding: 1em; border: 1px solid black;';
  157. Span1:=AddSpan('Span1',TextDiv);
  158. Span1.Style:='border: 1px solid blue;';
  159. Label1:=AddLabel('Label1','Happy',Span1);
  160. El:=Label1;
  161. Label1.SetStyleAttr('text-decoration-line','underline');
  162. Label2:=AddLabel('Label2','Fresnel',Span1);
  163. Label3:=AddLabel('Label3','day',Span1);
  164. Label4:=AddLabel('Label4','to',TextDiv);
  165. Label5:=AddLabel('Label5','everyone',TextDiv);
  166. end;
  167. procedure TMainForm.OnLineClick(Event: TAbstractEvent);
  168. var
  169. s: String;
  170. begin
  171. if Event.Sender=LineUnderlineRadioButton then
  172. s:='underline'
  173. else if Event.Sender=LineOverlineRadioButton then
  174. s:='overline'
  175. else if Event.Sender=LineLineThroughRadioButton then
  176. s:='line-through'
  177. else if Event.Sender=LineGrammarErrorRadioButton then
  178. s:='grammar-error'
  179. else if Event.Sender=LineSpellingErrorRadioButton then
  180. s:='spelling-error'
  181. else
  182. s:='none';
  183. El.SetStyleAttr('text-decoration-line',s);
  184. end;
  185. procedure TMainForm.OnRangeClick(Event: TAbstractEvent);
  186. var
  187. NewEl: TFresnelElement;
  188. OldLine, OldStyle, OldThickness: String;
  189. begin
  190. if Event.Sender=RangeDivRadioButton then
  191. NewEl:=TextDiv
  192. else if Event.Sender=RangeSpanRadioButton then
  193. NewEl:=Span1
  194. else
  195. NewEl:=Label1;
  196. if El=NewEl then exit;
  197. OldLine:=El.GetStyleAttr('text-decoration-line');
  198. El.SetStyleAttr('text-decoration-line','');
  199. OldStyle:=El.GetStyleAttr('text-decoration-style');
  200. El.SetStyleAttr('text-decoration-style','');
  201. OldThickness:=El.GetStyleAttr('text-decoration-thickness');
  202. El.SetStyleAttr('text-decoration-thickness','');
  203. El:=NewEl;
  204. El.SetStyleAttr('text-decoration-line',OldLine);
  205. El.SetStyleAttr('text-decoration-style',OldStyle);
  206. El.SetStyleAttr('text-decoration-thickness',OldThickness);
  207. end;
  208. procedure TMainForm.OnStyleClick(Event: TAbstractEvent);
  209. var
  210. s: String;
  211. begin
  212. if Event.Sender=StyleDashedRadioButton then
  213. s:='dashed'
  214. else if Event.Sender=StyleDottedRadioButton then
  215. s:='dotted'
  216. else if Event.Sender=StyleDoubleRadioButton then
  217. s:='double'
  218. else if Event.Sender=StyleWavyRadioButton then
  219. s:='wavy'
  220. else
  221. s:='solid';
  222. El.SetStyleAttr('text-decoration-style',s);
  223. end;
  224. procedure TMainForm.OnThicknessClick(Event: TAbstractEvent);
  225. var
  226. s: String;
  227. begin
  228. if Event.Sender=ThicknessAutoRadioButton then
  229. s:='auto'
  230. else if Event.Sender=ThicknessFromFontRadioButton then
  231. s:='from-font'
  232. else if Event.Sender=ThicknessPxRadioButton then
  233. s:=FloatToCSSPx(ThicknessPxSlider.SliderPosition);
  234. El.SetStyleAttr('text-decoration-thickness',s);
  235. end;
  236. procedure TMainForm.OnThicknessPxChanged(Sender: TObject);
  237. begin
  238. if ThicknessPxRadioButton.Checked then
  239. El.SetStyleAttr('text-decoration-thickness',FloatToCSSPx(ThicknessPxSlider.SliderPosition));
  240. end;
  241. end.