fresnel.democheckbox.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. unit Fresnel.DemoCheckbox;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. FPReadPNG, Fresnel.DOM, Fresnel.Controls, Fresnel.Classes,
  7. FCL.Events, Fresnel.Events, fpCSSTree;
  8. type
  9. { TDemoCheckBoxButton }
  10. TDemoCheckBoxButton = class(TImage)
  11. private
  12. FChecked: boolean;
  13. procedure OnClickEvent(Event: TAbstractEvent);
  14. protected
  15. procedure SetChecked(const AValue: boolean); virtual;
  16. public
  17. const
  18. cStyle = ''
  19. +'.CheckBoxButton {'+LineEnding
  20. +' cursor: pointer;'+LineEnding
  21. +' border-radius: 0.2em;'+LineEnding
  22. +' padding: 1px;'+LineEnding
  23. +' width: 1em;'+LineEnding
  24. +' height: 1em;'+LineEnding
  25. +'}'+LineEnding;
  26. cCheckedStyle = 'background: #68f; border: 2px solid #68f;';
  27. cUncheckedStyle = 'background: white; border: 2px solid #999;';
  28. public
  29. CheckedStyle: string;
  30. UncheckedStyle: string;
  31. constructor Create(AOwner: TComponent); override;
  32. property Checked: boolean read FChecked write SetChecked;
  33. end;
  34. { TDemoCheckBox }
  35. TDemoCheckBox = class(TDiv)
  36. private
  37. FOnChange: TNotifyEvent;
  38. function GetCaption: TFresnelCaption;
  39. function GetChecked: boolean;
  40. procedure OnBoxClickEvent(Event: TAbstractEvent);
  41. procedure SetCaption(const AValue: TFresnelCaption);
  42. procedure SetChecked(const AValue: boolean);
  43. public
  44. // default styles
  45. const
  46. cStyle = ''
  47. +'.CheckBox {'+LineEnding
  48. +'}'+LineEnding
  49. +TDemoCheckBoxButton.cStyle
  50. +'.CheckBoxButton {'+LineEnding
  51. +' margin: 0 0.2em 0 0;'+LineEnding
  52. +'}'+LineEnding
  53. +'.CheckBoxLabel {'+LineEnding
  54. +' cursor: pointer;'+LineEnding
  55. +' padding: 0.2em 0.3em;'+LineEnding
  56. +' margin-bottom: 0;'+LineEnding
  57. +'}'+LineEnding;
  58. public
  59. Box: TDemoCheckBoxButton;
  60. CaptionLabel: TLabel;
  61. constructor Create(AOwner: TComponent); override;
  62. class function GetCSSTypeStyle: TCSSString; override;
  63. published
  64. property Caption: TFresnelCaption read GetCaption write SetCaption;
  65. property Checked: boolean read GetChecked write SetChecked;
  66. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  67. end;
  68. implementation
  69. {$I fresnel.democheckbox.inc}
  70. { TDemoCheckBoxButton }
  71. procedure TDemoCheckBoxButton.OnClickEvent(Event: TAbstractEvent);
  72. begin
  73. if Event is TFresnelMouseClickEvent then ;
  74. Checked:=not Checked;
  75. end;
  76. procedure TDemoCheckBoxButton.SetChecked(const AValue: boolean);
  77. begin
  78. if FChecked=AValue then Exit;
  79. FChecked:=AValue;
  80. if Checked then
  81. begin
  82. Style:=CheckedStyle;
  83. end else begin
  84. Style:=UncheckedStyle;
  85. end;
  86. end;
  87. constructor TDemoCheckBoxButton.Create(AOwner: TComponent);
  88. var
  89. aStream: TStream;
  90. begin
  91. inherited Create(AOwner);
  92. CheckedStyle:=cCheckedStyle;
  93. UncheckedStyle:=cUncheckedStyle;
  94. Style:=UncheckedStyle;
  95. aStream:=TMemoryStream.Create;
  96. aStream.WriteBuffer(CheckBoxImage[0],length(CheckBoxImage));
  97. aStream.Position:=0;
  98. try
  99. Image.LoadFromStream(aStream);
  100. finally
  101. aStream.Free;
  102. end;
  103. AddEventListener(evtClick,@OnClickEvent);
  104. end;
  105. { TDemoCheckBox }
  106. function TDemoCheckBox.GetCaption: TFresnelCaption;
  107. begin
  108. Result:=CaptionLabel.Caption;
  109. end;
  110. function TDemoCheckBox.GetChecked: boolean;
  111. begin
  112. Result:=Box.Checked;
  113. end;
  114. procedure TDemoCheckBox.OnBoxClickEvent(Event: TAbstractEvent);
  115. begin
  116. if Event=nil then ;
  117. if Assigned(OnChange) then OnChange(Self);
  118. end;
  119. procedure TDemoCheckBox.SetCaption(const AValue: TFresnelCaption);
  120. begin
  121. CaptionLabel.Caption:=AValue;
  122. end;
  123. procedure TDemoCheckBox.SetChecked(const AValue: boolean);
  124. begin
  125. Box.Checked:=AValue;
  126. end;
  127. constructor TDemoCheckBox.Create(AOwner: TComponent);
  128. begin
  129. inherited Create(AOwner);
  130. CSSClasses.Add('CheckBox');
  131. Box:=TDemoCheckBoxButton.Create(Self);
  132. with Box do begin
  133. Name:='Box';
  134. CSSClasses.Add('CheckBoxButton');
  135. Parent:=Self;
  136. AddEventListener(evtClick,@OnBoxClickEvent);
  137. end;
  138. CaptionLabel:=TLabel.Create(Self);
  139. with CaptionLabel do begin
  140. Name:='CaptionLabel';
  141. CSSClasses.Add('CheckBoxLabel');
  142. Parent:=Self;
  143. end;
  144. end;
  145. class function TDemoCheckBox.GetCSSTypeStyle: TCSSString;
  146. begin
  147. Result:=cStyle;
  148. end;
  149. end.