DemoCheckBox.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. function GetCaption: TFresnelCaption;
  37. function GetChecked: boolean;
  38. procedure SetCaption(const AValue: TFresnelCaption);
  39. procedure SetChecked(const AValue: boolean);
  40. public
  41. // default styles
  42. const
  43. cStyle = ''
  44. +'.CheckBox {'+LineEnding
  45. +' position: relative;'+LineEnding
  46. +'}'+LineEnding
  47. +'.CheckBoxButton {'+LineEnding
  48. +' margin: 0 2px 0 0;'+LineEnding
  49. +'}'+LineEnding
  50. +'.CheckBoxLabel {'+LineEnding
  51. +' cursor: pointer;'+LineEnding
  52. +' font-size: 11px;'+LineEnding
  53. +' padding: 2px 3px;'+LineEnding
  54. +' margin-bottom: 0;'+LineEnding
  55. +'}'+LineEnding;
  56. public
  57. Box: TDemoCheckBoxButton;
  58. CaptionLabel: TLabel;
  59. constructor Create(AOwner: TComponent); override;
  60. property Caption: TFresnelCaption read GetCaption write SetCaption;
  61. property Checked: boolean read GetChecked write SetChecked;
  62. end;
  63. implementation
  64. {$R *.res}
  65. { TDemoCheckBoxButton }
  66. procedure TDemoCheckBoxButton.OnClickEvent(Event: TAbstractEvent);
  67. begin
  68. if Event is TFresnelMouseClickEvent then ;
  69. Checked:=not Checked;
  70. end;
  71. procedure TDemoCheckBoxButton.SetChecked(const AValue: boolean);
  72. begin
  73. if FChecked=AValue then Exit;
  74. FChecked:=AValue;
  75. if Checked then
  76. begin
  77. Style:=CheckedStyle;
  78. end else begin
  79. Style:=UncheckedStyle;
  80. end;
  81. end;
  82. constructor TDemoCheckBoxButton.Create(AOwner: TComponent);
  83. var
  84. aStream: TResourceStream;
  85. begin
  86. inherited Create(AOwner);
  87. CheckedStyle:=cCheckedStyle;
  88. UncheckedStyle:=cUncheckedStyle;
  89. Style:=UncheckedStyle;
  90. aStream := TResourceStream.Create(HINSTANCE, 'Check', RT_RCDATA);
  91. try
  92. Image.LoadFromStream(aStream);
  93. finally
  94. aStream.Free;
  95. end;
  96. AddEventListener(evtClick,@OnClickEvent);
  97. end;
  98. { TDemoCheckBox }
  99. function TDemoCheckBox.GetCaption: TFresnelCaption;
  100. begin
  101. Result:=CaptionLabel.Caption;
  102. end;
  103. function TDemoCheckBox.GetChecked: boolean;
  104. begin
  105. Result:=Box.Checked;
  106. end;
  107. procedure TDemoCheckBox.SetCaption(const AValue: TFresnelCaption);
  108. begin
  109. CaptionLabel.Caption:=AValue;
  110. end;
  111. procedure TDemoCheckBox.SetChecked(const AValue: boolean);
  112. begin
  113. Box.Checked:=AValue;
  114. end;
  115. constructor TDemoCheckBox.Create(AOwner: TComponent);
  116. begin
  117. inherited Create(AOwner);
  118. CSSClasses.Add('CheckBox');
  119. Box:=TDemoCheckBoxButton.Create(Self);
  120. with Box do begin
  121. Name:='Box';
  122. CSSClasses.Add('CheckBoxButton');
  123. Parent:=Self;
  124. end;
  125. CaptionLabel:=TLabel.Create(Self);
  126. with CaptionLabel do begin
  127. Name:='CaptionLabel';
  128. CSSClasses.Add('CheckBoxLabel');
  129. Parent:=Self;
  130. end;
  131. end;
  132. end.