{------------------------------------------------------------------------------} { Unit Name: OptionButton Purpose : Button to be used in option forms Author : Vesa Lappalainen Date : 10.09.00 Changed : 15.9.2001/vl + VCL/CLX compiling (define const CLX) Changed : 16.9.2001/vl + please use ButtonType insted of ModalResult, because ModalResult has problems in CLX ToDo : Usage: Place OptionButton to form with components that have either components with TCheckLegal (with name Check) or TIniSave (with name Ini) (or both) properties. Choose OptionsButton ButtonType acording: obCancel - for use of Cancel-button, Resores all old values for components having TIniSave property, closes the form obRestore - for use of Restore-button, as mrCancel, but does not close the form obOK - for use of OK-button, checks all components having TCheckLegal property and jumps to first illegal. If no illegal exists, then saves all values and closes the form obSave - for use of Save-button, as mrOK, but does not close the form New properties: ShowReason - if true and mrOK or mrSave, then ShowMessage box with reason why illegal. CheckAll - if true, then run DoCheck for all controls even an illegal is found. Then if ShowReason is true, the ShowMessage will show all illegal reasons found. ButtonType - type for button } {------------------------------------------------------------------------------} unit OptionButton; interface {$ifdef CLX} uses Classes,QStdCtrls; {$else} uses Classes,StdCtrls; {$endif} type TOptionButtonType = (obNone,obOK,obCancel,obSave,obRestore); TOptionButton = class(TButton) private FShowReason: boolean; FCheckAll: boolean; FButtonType: TOptionButtonType; procedure SetButtonType(const Value: TOptionButtonType); procedure SetCaption(const s: string); public procedure Click; override; constructor Create(AOwner:TComponent); override; published property Align; property ShowReason : boolean read FShowReason write FShowReason default false; property CheckAll : boolean read FCheckAll write FCheckAll default false; property ButtonType : TOptionButtonType read FButtonType write SetButtonType default obNone; end; procedure Register; implementation uses {$ifdef CLX} QForms,QDialogs,QControls, {$else} Forms,Dialogs,Controls, {$endif} kiniSave,kCheck; procedure Register; begin RegisterComponents('KaveOptions', [TOptionButton]); end; { TOptionButton } procedure TOptionButton.Click; var ctr : TControl; procedure DoNotCloseForm; begin if ( Owner is TForm ) then TForm(Owner).ModalResult := mrNone; end; procedure SetResult(res:TModalResult); begin if ( Owner is TForm ) then TForm(Owner).ModalResult := res; end; function Check:boolean; var s:string; begin Result := true; ctr := CheckLegalDoCheck(Owner,not CheckAll,@s); if ( ctr <> nil ) then begin DoNotCloseForm; Result := false; if ( ctr is TWinControl ) then TWinControl(ctr).SetFocus; if ShowReason then ShowMessage(s); end; end; begin inherited; if ( ButtonType = obCancel ) then begin IniSaveRestoreAllOptions(Owner); SetResult(mrCancel); Exit; end; if ( ButtonType = obRestore ) then begin IniSaveRestoreAllOptions(Owner); DoNotCloseForm; Exit; end; if ( ButtonType = obSave) then begin if not Check then exit; IniSaveSaveAll(Owner); DoNotCloseForm; Exit; end; if ( ButtonType = obOK ) then begin if not Check then exit; IniSaveSaveAll(Owner); SetResult(mrOK); Exit; end; end; procedure TOptionButton.SetCaption(const s:string); begin if ( pos('Button',Caption) > 0 ) then Caption := s; end; procedure TOptionButton.SetButtonType(const Value: TOptionButtonType); begin FButtonType := Value; case ButtonType of obOK : begin Name := 'ButtonOK'; SetCaption('OK'); Default := true; end; obCancel : begin Name := 'ButtonCancel'; SetCaption('Cancel'); Cancel := true; end; obSave : begin Name := 'ButtonSave'; SetCaption('&Save'); end; obRestore: begin Name := 'ButtonRestore'; SetCaption('&Restore'); end; end; end; constructor TOptionButton.Create(AOwner: TComponent); begin inherited; end; end.