DemoCheckBox.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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: 3px;'+LineEnding
  21. +' padding: 1px;'+LineEnding
  22. +' width: 11px;'+LineEnding
  23. +' height: 11px;'+LineEnding
  24. +'}'+LineEnding;
  25. cCheckedStyle = 'background: blue; border: 2px solid blue;';
  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. +'.CheckBoxButton {'+LineEnding
  45. +' margin: 0 2px 0 0;'+LineEnding
  46. +'}'+LineEnding
  47. +'.CheckBoxLabel {'+LineEnding
  48. +' cursor: pointer;'+LineEnding
  49. +' font-size: 11px;'+LineEnding
  50. +' padding: 4px 3px;'+LineEnding
  51. +' margin-bottom: 0;'+LineEnding
  52. +'}'+LineEnding;
  53. public
  54. Box: TDemoCheckBoxButton;
  55. CaptionLabel: TLabel;
  56. constructor Create(AOwner: TComponent); override;
  57. property Caption: TFresnelCaption read GetCaption write SetCaption;
  58. property Checked: boolean read GetChecked write SetChecked;
  59. end;
  60. implementation
  61. { TDemoCheckBoxButton }
  62. procedure TDemoCheckBoxButton.OnClickEvent(Event: TAbstractEvent);
  63. begin
  64. if Event is TFresnelMouseClickEvent then ;
  65. Checked:=not Checked;
  66. end;
  67. procedure TDemoCheckBoxButton.SetChecked(const AValue: boolean);
  68. begin
  69. if FChecked=AValue then Exit;
  70. FChecked:=AValue;
  71. if Checked then
  72. begin
  73. Style:=CheckedStyle;
  74. end else begin
  75. Style:=UncheckedStyle;
  76. end;
  77. end;
  78. constructor TDemoCheckBoxButton.Create(AOwner: TComponent);
  79. begin
  80. inherited Create(AOwner);
  81. CheckedStyle:=cCheckedStyle;
  82. UncheckedStyle:=cUncheckedStyle;
  83. Style:=UncheckedStyle;
  84. Image.LoadFromFile('Check.png');
  85. AddEventListener(evtClick,@OnClickEvent);
  86. end;
  87. { TDemoCheckBox }
  88. function TDemoCheckBox.GetCaption: TFresnelCaption;
  89. begin
  90. Result:=CaptionLabel.Caption;
  91. end;
  92. function TDemoCheckBox.GetChecked: boolean;
  93. begin
  94. Result:=Box.Checked;
  95. end;
  96. procedure TDemoCheckBox.SetCaption(const AValue: TFresnelCaption);
  97. begin
  98. CaptionLabel.Caption:=AValue;
  99. end;
  100. procedure TDemoCheckBox.SetChecked(const AValue: boolean);
  101. begin
  102. Box.Checked:=AValue;
  103. end;
  104. constructor TDemoCheckBox.Create(AOwner: TComponent);
  105. begin
  106. inherited Create(AOwner);
  107. Box:=TDemoCheckBoxButton.Create(Self);
  108. with Box do begin
  109. Name:='Box';
  110. CSSClasses.Add('CheckBoxButton');
  111. Parent:=Self;
  112. end;
  113. CaptionLabel:=TLabel.Create(Self);
  114. with CaptionLabel do begin
  115. Name:='CaptionLabel';
  116. CSSClasses.Add('CheckBoxCaption');
  117. Parent:=Self;
  118. end;
  119. end;
  120. end.