fresnel.demoradiobutton.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. unit Fresnel.DemoRadioButton;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. Fresnel.DOM, Fresnel.Controls, Fresnel.Classes,
  7. FCL.Events, Fresnel.Events, fpCSSTree;
  8. type
  9. { TDemoRadioButton }
  10. TDemoRadioButton = class(TDiv)
  11. private
  12. FChecked: boolean;
  13. FOnChange: TNotifyEvent;
  14. function GetCaption: TFresnelCaption;
  15. function GetChecked: boolean;
  16. protected
  17. procedure OnClickEvent(Event: TAbstractEvent); virtual;
  18. procedure SetCaption(const AValue: TFresnelCaption); virtual;
  19. procedure SetChecked(AValue: boolean); virtual;
  20. procedure UncheckOthers; virtual;
  21. public
  22. // default styles
  23. const
  24. cStyle = ''
  25. +'.RadioButton {'+LineEnding
  26. +'}'+LineEnding
  27. +'.RadioButtonBox {'+LineEnding
  28. +' width: 0.9em;'+LineEnding
  29. +' height: 0.9em;'+LineEnding
  30. +' border-radius: 50%;'+LineEnding
  31. +'}'+LineEnding
  32. +'.RadioButtonLabel {'+LineEnding
  33. +' cursor: pointer;'+LineEnding
  34. +' padding: 0.2em 0.3em;'+LineEnding
  35. +' margin-bottom: 0;'+LineEnding
  36. +'}'+LineEnding;
  37. cCheckedStyle = 'background: #68f; border: 2px solid #333;';
  38. cUncheckedStyle = 'background: white; border: 2px solid #333;';
  39. public
  40. Box: TSpan;
  41. CaptionLabel: TLabel;
  42. constructor Create(AOwner: TComponent); override;
  43. class function GetCSSTypeStyle: TCSSString; override;
  44. published
  45. property Caption: TFresnelCaption read GetCaption write SetCaption;
  46. property Checked: boolean read GetChecked write SetChecked;
  47. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  48. end;
  49. implementation
  50. { TDemoRadioButton }
  51. function TDemoRadioButton.GetCaption: TFresnelCaption;
  52. begin
  53. Result:=CaptionLabel.Caption;
  54. end;
  55. function TDemoRadioButton.GetChecked: boolean;
  56. begin
  57. Result:=FChecked;
  58. end;
  59. procedure TDemoRadioButton.OnClickEvent(Event: TAbstractEvent);
  60. begin
  61. if Event=nil then ;
  62. Checked:=true;
  63. if Assigned(OnChange) then OnChange(Self);
  64. end;
  65. procedure TDemoRadioButton.SetCaption(const AValue: TFresnelCaption);
  66. begin
  67. CaptionLabel.Caption:=AValue;
  68. end;
  69. procedure TDemoRadioButton.SetChecked(AValue: boolean);
  70. begin
  71. if Checked=AValue then exit;
  72. FChecked:=AValue;
  73. if Checked then
  74. begin
  75. Box.Style:=cCheckedStyle;
  76. UncheckOthers;
  77. end else begin
  78. Box.Style:=cUncheckedStyle;
  79. end;
  80. end;
  81. procedure TDemoRadioButton.UncheckOthers;
  82. var
  83. i: Integer;
  84. El: TFresnelElement;
  85. begin
  86. for i:=0 to Parent.NodeCount-1 do
  87. begin
  88. El:=Parent.Nodes[i];
  89. if (El<>Self) and (El is TDemoRadioButton) then
  90. TDemoRadioButton(El).Checked:=false;
  91. end;
  92. end;
  93. constructor TDemoRadioButton.Create(AOwner: TComponent);
  94. begin
  95. inherited Create(AOwner);
  96. CSSClasses.Add('RadioButton');
  97. Box:=TSpan.Create(Self);
  98. with Box do begin
  99. Name:='Box';
  100. CSSClasses.Add('RadioButtonBox');
  101. Style:=cUncheckedStyle;
  102. Parent:=Self;
  103. end;
  104. CaptionLabel:=TLabel.Create(Self);
  105. with CaptionLabel do begin
  106. Name:='CaptionLabel';
  107. CSSClasses.Add('RadioButtonLabel');
  108. Parent:=Self;
  109. end;
  110. AddEventListener(evtClick,@OnClickEvent);
  111. end;
  112. class function TDemoRadioButton.GetCSSTypeStyle: TCSSString;
  113. begin
  114. Result:=cStyle;
  115. end;
  116. end.