{------------------------------------------------------------------------------} { Unit Name: kIniComp Purpose : Example of the use of TIniSave Author : Vesa Lappalainen Date : 14.09.1997 Changed : 06.09.00 + separated to 3 pieces (kIniComp, kIniSave, kPopFunc) for easier use in other component packages Changed : 15.9.2001/vl + VCL/CLX compiling (define const CLX) ToDo : Classes: ======== TEditIni - example of class using TIniSave TCheckBoxIni - another (even more simple) example } {------------------------------------------------------------------------------} unit kinicomp; interface uses classes, {$ifdef CLX} QControls,QStdctrls, {$else} Messages,controls,stdctrls, {$endif} IniFiles,kIniSave; {------------------------------------------------------------------------------} type TEditIni = class(TEdit) FIni : TIniSave; private function GetAsString: string; procedure SetAsString(const Value: string); public constructor Create(AOwner:TComponent); override; destructor Destroy; override; procedure Change; override; procedure Loaded; override; {$ifdef CLX} {$else} procedure WMDestroy(var Message: TWMDestroy); message WM_DESTROY; procedure WMCreate(var Message: TWMDestroy); message WM_CREATE; {$endif} published property Ini : TIniSave read FIni write FIni; property Align; property AsString : string read GetAsString write SetAsString stored false; end; TCheckBoxIni = class(TCheckBox) FIni : TIniSave; public constructor Create(AOwner:TComponent); override; destructor Destroy; override; procedure Click; override; procedure Toggle; override; procedure Loaded; override; procedure SetAsString(s:string); virtual; function GetAsString:string; virtual; procedure SetName(const NewName:TComponentName); override; published property Ini : TIniSave read FIni write FIni; property AsString : string read GetAsString write SetAsString stored false; property Align; end; TComboBoxIni = class(TComboBox) FIni : TIniSave; public constructor Create(AOwner:TComponent); override; destructor Destroy; override; procedure Change; override; procedure Loaded; override; procedure SetAsString(s:string); virtual; function GetAsString:string; virtual; procedure SetName(const NewName:TComponentName); override; {$ifdef CLX} {$else} procedure WMDestroy(var Message: TWMDestroy); message WM_DESTROY; procedure WMCreate(var Message: TWMDestroy); message WM_CREATE; {$endif} published property Ini : TIniSave read FIni write FIni; property AsString : string read GetAsString write SetAsString stored false; property Align; end; procedure Register; {------------------------------------------------------------------------------} implementation {------------------------------------------------------------------------------} uses SysUtils,kPropFunc, {$ifdef CLX} QDialogs; {$else} Dialogs; {$endif} {------------------------------------------------------------------------------} procedure Register; begin RegisterComponents('KaveOptions', [TEditIni]); RegisterComponents('KaveOptions', [TCheckBoxIni]); RegisterComponents('KaveOptions', [TComboBoxIni]); end; {------------------------------------------------------------------------------} { TEditIni: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX} {------------------------------------------------------------------------------} constructor TEditIni.Create(AOwner:TComponent); begin inherited Create(AOwner); FIni := TIniSave.Create(self); Ini.AutoSave := true; Ini.PropName := 'Text'; Text := ''; end; {$ifdef CLX} {$else} procedure TEditIni.WMDestroy(var Message: TWMDestroy); begin if ( ini <> nil ) then Ini.DoLastSave; inherited; end; procedure TEditIni.WMCreate(var Message: TWMDestroy); begin inherited; if ( ini <> nil ) then Ini.ReleaseLastSave; end; {$endif} destructor TEditIni.Destroy; begin Ini.DoLastSave; Ini.Free; Ini := nil; inherited Destroy; end; procedure TEditIni.Loaded; begin inherited; Ini.Load; end; procedure TEditIni.Change; begin inherited Change; Ini.DoAutoSave; end; function TEditIni.GetAsString: string; begin Result := Text; end; procedure TEditIni.SetAsString(const Value: string); begin Text := Value; end; {------------------------------------------------------------------------------} { TCheckBoxIni: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX} {------------------------------------------------------------------------------} constructor TCheckBoxIni.Create(AOwner:TComponent); begin inherited Create(AOwner); FIni := TIniSave.Create(self); Ini.AutoSave := true; end; destructor TCheckBoxIni.Destroy; begin Ini.DoAutoSave; Ini.Free; inherited Destroy; end; procedure TCheckBoxIni.Loaded; begin inherited; Ini.Load; end; procedure TCheckBoxIni.Click; begin inherited Click; Ini.DoAutoSave; end; procedure TCheckBoxIni.Toggle; var oldstate : integer; begin oldstate := Ord(state); inherited Toggle; if ( Assigned(GlobalCBAfterSet) ) then GlobalCBAfterSet(Name + ' : ' + IntToStr(oldstate) + ' -> ' + IntToStr(Ord(State))); end; procedure TCheckBoxIni.SetAsString(s:string); begin State := TCheckBoxState(SetBetween(StrToIntDef(s,0),0,2)); end; function TCheckBoxIni.GetAsString:string; begin Result := IntToStr(Ord(State)); end; procedure TCheckBoxIni.SetName(const NewName:TComponentName); var oldname:string; begin oldname := Name; inherited SetName(NewName); Caption := GetNameCaption(self,NewName,oldname,Caption); end; {------------------------------------------------------------------------------} { TCheckBoxIni: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX} {------------------------------------------------------------------------------} {$ifdef CLX} {$else} procedure TComboBoxIni.WMDestroy(var Message: TWMDestroy); begin if ( ini <> nil ) then Ini.DoLastSave; inherited; end; procedure TComboBoxIni.WMCreate(var Message: TWMDestroy); begin inherited; if ( ini <> nil ) then Ini.ReleaseLastSave; end; {$endif} constructor TComboBoxIni.Create(AOwner:TComponent); begin inherited Create(AOwner); FIni := TIniSave.Create(self); Ini.AutoSave := true; end; destructor TComboBoxIni.Destroy; begin Ini.DoLastSave; Ini.Free; Ini := nil; inherited Destroy; end; procedure TComboBoxIni.Loaded; begin inherited; Ini.Load; end; procedure TComboBoxIni.Change; begin inherited Change; Ini.DoAutoSave; end; procedure TComboBoxIni.SetAsString(s:string); var index:integer; begin if Style=csDropDown Then begin Text:=s; end else begin index:=Items.IndexOf(s); if index<>-1 then ItemIndex:=index; end; end; function TComboBoxIni.GetAsString:string; begin Result:=''; if Style=csDropDown Then begin Result:=Text; end else begin if (ItemIndex<>-1) And (Items.Count>ItemIndex) then Result:=Items.Strings[ItemIndex]; end; end; procedure TComboBoxIni.SetName(const NewName:TComponentName); var oldname:string; begin oldname := Name; inherited SetName(NewName); Caption := GetNameCaption(self,NewName,oldname,Caption); end; end.