unit Fresnel.DemoRadioButton; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Fresnel.DOM, Fresnel.Controls, Fresnel.Classes, FCL.Events, Fresnel.Events, fpCSSTree; type { TDemoRadioButton } TDemoRadioButton = class(TDiv) private FChecked: boolean; FOnChange: TNotifyEvent; function GetCaption: TFresnelCaption; function GetChecked: boolean; protected procedure OnClickEvent(Event: TAbstractEvent); virtual; procedure SetCaption(const AValue: TFresnelCaption); virtual; procedure SetChecked(AValue: boolean); virtual; procedure UncheckOthers; virtual; public // default styles const cStyle = '' +'.RadioButton {'+LineEnding +'}'+LineEnding +'.RadioButtonBox {'+LineEnding +' width: 0.9em;'+LineEnding +' height: 0.9em;'+LineEnding +' border-radius: 50%;'+LineEnding +'}'+LineEnding +'.RadioButtonLabel {'+LineEnding +' cursor: pointer;'+LineEnding +' padding: 0.2em 0.3em;'+LineEnding +' margin-bottom: 0;'+LineEnding +'}'+LineEnding; cCheckedStyle = 'background: #68f; border: 2px solid #333;'; cUncheckedStyle = 'background: white; border: 2px solid #333;'; public Box: TSpan; CaptionLabel: TLabel; constructor Create(AOwner: TComponent); override; class function GetCSSTypeStyle: TCSSString; override; published property Caption: TFresnelCaption read GetCaption write SetCaption; property Checked: boolean read GetChecked write SetChecked; property OnChange: TNotifyEvent read FOnChange write FOnChange; end; implementation { TDemoRadioButton } function TDemoRadioButton.GetCaption: TFresnelCaption; begin Result:=CaptionLabel.Caption; end; function TDemoRadioButton.GetChecked: boolean; begin Result:=FChecked; end; procedure TDemoRadioButton.OnClickEvent(Event: TAbstractEvent); begin if Event=nil then ; Checked:=true; if Assigned(OnChange) then OnChange(Self); end; procedure TDemoRadioButton.SetCaption(const AValue: TFresnelCaption); begin CaptionLabel.Caption:=AValue; end; procedure TDemoRadioButton.SetChecked(AValue: boolean); begin if Checked=AValue then exit; FChecked:=AValue; if Checked then begin Box.Style:=cCheckedStyle; UncheckOthers; end else begin Box.Style:=cUncheckedStyle; end; end; procedure TDemoRadioButton.UncheckOthers; var i: Integer; El: TFresnelElement; begin for i:=0 to Parent.NodeCount-1 do begin El:=Parent.Nodes[i]; if (El<>Self) and (El is TDemoRadioButton) then TDemoRadioButton(El).Checked:=false; end; end; constructor TDemoRadioButton.Create(AOwner: TComponent); begin inherited Create(AOwner); CSSClasses.Add('RadioButton'); Box:=TSpan.Create(Self); with Box do begin Name:='Box'; CSSClasses.Add('RadioButtonBox'); Style:=cUncheckedStyle; Parent:=Self; end; CaptionLabel:=TLabel.Create(Self); with CaptionLabel do begin Name:='CaptionLabel'; CSSClasses.Add('RadioButtonLabel'); Parent:=Self; end; AddEventListener(evtClick,@OnClickEvent); end; class function TDemoRadioButton.GetCSSTypeStyle: TCSSString; begin Result:=cStyle; end; end.