DemoCheckBox.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. unit DemoCheckBox;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. {$IFDEF Windows}
  7. Windows,
  8. {$ENDIF}
  9. FPReadPNG, Fresnel.DOM, Fresnel.Controls, Fresnel.Classes,
  10. FCL.Events, Fresnel.Events;
  11. type
  12. { TDemoCheckBoxButton }
  13. TDemoCheckBoxButton = class(TImage)
  14. private
  15. FChecked: boolean;
  16. procedure OnClickEvent(Event: TAbstractEvent);
  17. protected
  18. procedure SetChecked(const AValue: boolean); virtual;
  19. public
  20. const
  21. cStyle = ''
  22. +'.CheckBoxButton {'+LineEnding
  23. +' cursor: pointer;'+LineEnding
  24. +' border-radius: 2px;'+LineEnding
  25. +' padding: 1px;'+LineEnding
  26. +' width: 11px;'+LineEnding
  27. +' height: 11px;'+LineEnding
  28. +'}'+LineEnding;
  29. cCheckedStyle = 'background: #68f; border: 2px solid #68f;';
  30. cUncheckedStyle = 'background: white; border: 2px solid #999;';
  31. public
  32. CheckedStyle: string;
  33. UncheckedStyle: string;
  34. constructor Create(AOwner: TComponent); override;
  35. property Checked: boolean read FChecked write SetChecked;
  36. end;
  37. { TDemoCheckBox }
  38. TDemoCheckBox = class(TDiv)
  39. private
  40. FOnChange: TNotifyEvent;
  41. function GetCaption: TFresnelCaption;
  42. function GetChecked: boolean;
  43. procedure OnBoxClickEvent(Event: TAbstractEvent);
  44. procedure SetCaption(const AValue: TFresnelCaption);
  45. procedure SetChecked(const AValue: boolean);
  46. public
  47. // default styles
  48. const
  49. cStyle = ''
  50. +'.CheckBox {'+LineEnding
  51. +'}'+LineEnding
  52. +TDemoCheckBoxButton.cStyle
  53. +'.CheckBoxButton {'+LineEnding
  54. +' margin: 0 2px 0 0;'+LineEnding
  55. +'}'+LineEnding
  56. +'.CheckBoxLabel {'+LineEnding
  57. +' cursor: pointer;'+LineEnding
  58. +' font-size: 12px;'+LineEnding
  59. +' padding: 2px 3px;'+LineEnding
  60. +' margin-bottom: 0;'+LineEnding
  61. +'}'+LineEnding;
  62. public
  63. Box: TDemoCheckBoxButton;
  64. CaptionLabel: TLabel;
  65. constructor Create(AOwner: TComponent); override;
  66. property Caption: TFresnelCaption read GetCaption write SetCaption;
  67. property Checked: boolean read GetChecked write SetChecked;
  68. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  69. end;
  70. implementation
  71. {$R *.res}
  72. { TDemoCheckBoxButton }
  73. procedure TDemoCheckBoxButton.OnClickEvent(Event: TAbstractEvent);
  74. begin
  75. if Event is TFresnelMouseClickEvent then ;
  76. Checked:=not Checked;
  77. end;
  78. procedure TDemoCheckBoxButton.SetChecked(const AValue: boolean);
  79. begin
  80. if FChecked=AValue then Exit;
  81. FChecked:=AValue;
  82. if Checked then
  83. begin
  84. Style:=CheckedStyle;
  85. end else begin
  86. Style:=UncheckedStyle;
  87. end;
  88. end;
  89. constructor TDemoCheckBoxButton.Create(AOwner: TComponent);
  90. var
  91. aStream: TResourceStream;
  92. begin
  93. inherited Create(AOwner);
  94. CheckedStyle:=cCheckedStyle;
  95. UncheckedStyle:=cUncheckedStyle;
  96. Style:=UncheckedStyle;
  97. aStream := TResourceStream.Create(HINSTANCE, 'Check', RT_RCDATA);
  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. end.