DemoCheckBox.pas 3.9 KB

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