1
0
mirror of synced 2024-09-16 16:46:09 +00:00

Fixed: ClearCollection removed keys instead of sections

Fixed: TX2FormPosSettings does not apply the form size if not initialized
This commit is contained in:
Mark van Renswoude 2010-04-13 10:04:05 +00:00
parent 5b05c3237c
commit 6197f5463c
4 changed files with 114 additions and 70 deletions

View File

@ -28,8 +28,8 @@ type
function Read(AObject: TObject): Boolean; virtual; function Read(AObject: TObject): Boolean; virtual;
procedure Write(AObject: TObject); virtual; procedure Write(AObject: TObject); virtual;
function CreateReader(): IX2PersistReader; virtual; function CreateReader: IX2PersistReader; virtual;
function CreateWriter(): IX2PersistWriter; virtual; function CreateWriter: IX2PersistWriter; virtual;
function CreateSectionReader(const ASection: String): IX2PersistReader; virtual; function CreateSectionReader(const ASection: String): IX2PersistReader; virtual;
function CreateSectionWriter(const ASection: String): IX2PersistWriter; virtual; function CreateSectionWriter(const ASection: String): IX2PersistWriter; virtual;
@ -54,12 +54,12 @@ type
property Sections: TStrings read FSections; property Sections: TStrings read FSections;
public public
constructor Create(AIsReader: Boolean); constructor Create(AIsReader: Boolean);
destructor Destroy(); override; destructor Destroy; override;
{ IX2PersistFiler } { IX2PersistFiler }
function BeginSection(const AName: String): Boolean; virtual; function BeginSection(const AName: String): Boolean; virtual;
procedure EndSection(); virtual; procedure EndSection; virtual;
procedure GetKeys(const ADest: TStrings); virtual; abstract; procedure GetKeys(const ADest: TStrings); virtual; abstract;
procedure GetSections(const ADest: TStrings); virtual; abstract; procedure GetSections(const ADest: TStrings); virtual; abstract;
@ -88,7 +88,7 @@ type
function WriteInt64(const AName: String; AValue: Int64): Boolean; virtual; abstract; function WriteInt64(const AName: String; AValue: Int64): Boolean; virtual; abstract;
function WriteStream(const AName: String; AStream: TStream): Boolean; virtual; function WriteStream(const AName: String; AStream: TStream): Boolean; virtual;
procedure ClearCollection(); virtual; procedure ClearCollection; virtual;
procedure WriteCollection(ACollection: TCollection); virtual; procedure WriteCollection(ACollection: TCollection); virtual;
procedure DeleteKey(const AName: String); virtual; abstract; procedure DeleteKey(const AName: String); virtual; abstract;
@ -123,7 +123,7 @@ type
{ IX2PersistFiler } { IX2PersistFiler }
function BeginSection(const AName: String): Boolean; function BeginSection(const AName: String): Boolean;
procedure EndSection(); procedure EndSection;
procedure GetKeys(const ADest: TStrings); procedure GetKeys(const ADest: TStrings);
procedure GetSections(const ADest: TStrings); procedure GetSections(const ADest: TStrings);
@ -152,18 +152,18 @@ type
procedure DeleteSection(const AName: String); procedure DeleteSection(const AName: String);
public public
constructor Create(const AFiler: IX2PersistFiler; const ASection: String); constructor Create(const AFiler: IX2PersistFiler; const ASection: String);
destructor Destroy(); override; destructor Destroy; override;
end; end;
{ TX2CustomPersist } { TX2CustomPersist }
function TX2CustomPersist.CreateReader(): IX2PersistReader; function TX2CustomPersist.CreateReader: IX2PersistReader;
begin begin
Result := (CreateFiler(True) as IX2PersistReader); Result := (CreateFiler(True) as IX2PersistReader);
end; end;
function TX2CustomPersist.CreateWriter(): IX2PersistWriter; function TX2CustomPersist.CreateWriter: IX2PersistWriter;
begin begin
Result := (CreateFiler(False) as IX2PersistWriter); Result := (CreateFiler(False) as IX2PersistWriter);
end; end;
@ -171,26 +171,26 @@ end;
function TX2CustomPersist.CreateSectionReader(const ASection: String): IX2PersistReader; function TX2CustomPersist.CreateSectionReader(const ASection: String): IX2PersistReader;
begin begin
Result := (TX2PersistSectionFilerProxy.Create(CreateReader(), ASection) as IX2PersistReader); Result := (TX2PersistSectionFilerProxy.Create(CreateReader, ASection) as IX2PersistReader);
end; end;
function TX2CustomPersist.CreateSectionWriter(const ASection: String): IX2PersistWriter; function TX2CustomPersist.CreateSectionWriter(const ASection: String): IX2PersistWriter;
begin begin
Result := (TX2PersistSectionFilerProxy.Create(CreateWriter(), ASection) as IX2PersistWriter); Result := (TX2PersistSectionFilerProxy.Create(CreateWriter, ASection) as IX2PersistWriter);
end; end;
function TX2CustomPersist.Read(AObject: TObject): Boolean; function TX2CustomPersist.Read(AObject: TObject): Boolean;
begin begin
with CreateReader() do with CreateReader do
Result := Read(AObject); Result := Read(AObject);
end; end;
procedure TX2CustomPersist.Write(AObject: TObject); procedure TX2CustomPersist.Write(AObject: TObject);
begin begin
with CreateWriter() do with CreateWriter do
Write(AObject); Write(AObject);
end; end;
@ -198,14 +198,14 @@ end;
{ TX2CustomPersistFiler } { TX2CustomPersistFiler }
constructor TX2CustomPersistFiler.Create(AIsReader: Boolean); constructor TX2CustomPersistFiler.Create(AIsReader: Boolean);
begin begin
inherited Create(); inherited Create;
FIsReader := AIsReader; FIsReader := AIsReader;
FSections := TStringList.Create(); FSections := TStringList.Create;
end; end;
destructor TX2CustomPersistFiler.Destroy(); destructor TX2CustomPersistFiler.Destroy;
begin begin
FreeAndNil(FSections); FreeAndNil(FSections);
@ -236,7 +236,7 @@ begin
end; end;
procedure TX2CustomPersistFiler.EndSection(); procedure TX2CustomPersistFiler.EndSection;
begin begin
Assert(FSections.Count > 0, 'EndSection called without BeginSection'); Assert(FSections.Count > 0, 'EndSection called without BeginSection');
FSections.Delete(Pred(FSections.Count)); FSections.Delete(Pred(FSections.Count));
@ -385,7 +385,7 @@ begin
try try
AContinue := Read(objectProp); AContinue := Read(objectProp);
finally finally
EndSection(); EndSection;
end; end;
end; end;
end; end;
@ -496,7 +496,7 @@ begin
try try
Write(objectProp); Write(objectProp);
finally finally
EndSection(); EndSection;
end; end;
end; end;
end; end;
@ -514,22 +514,22 @@ var
begin begin
if ReadInteger(CollectionCountName, itemCount) then if ReadInteger(CollectionCountName, itemCount) then
begin begin
ACollection.BeginUpdate(); ACollection.BeginUpdate;
try try
ACollection.Clear(); ACollection.Clear;
for itemIndex := 0 to Pred(itemCount) do for itemIndex := 0 to Pred(itemCount) do
begin begin
if BeginSection(CollectionItemNamePrefix + IntToStr(itemIndex)) then if BeginSection(CollectionItemNamePrefix + IntToStr(itemIndex)) then
try try
collectionItem := ACollection.Add(); collectionItem := ACollection.Add;
Read(collectionItem); Read(collectionItem);
finally finally
EndSection(); EndSection;
end; end;
end; end;
finally finally
ACollection.EndUpdate(); ACollection.EndUpdate;
end; end;
end; end;
end; end;
@ -546,23 +546,23 @@ begin
end; end;
procedure TX2CustomPersistFiler.ClearCollection(); procedure TX2CustomPersistFiler.ClearCollection;
var var
keyNames: TStringList; sections: TStringList;
keyIndex: Integer; sectionIndex: Integer;
begin begin
inherited; inherited;
keyNames := TStringList.Create(); sections := TStringList.Create;
try try
GetKeys(keyNames); GetSections(sections);
for keyIndex := 0 to Pred(keyNames.Count) do for sectionIndex := 0 to Pred(sections.Count) do
if SameTextS(keyNames[keyIndex], CollectionItemNamePrefix) then if SameTextS(sections[sectionIndex], CollectionItemNamePrefix) then
DeleteKey(keyNames[keyIndex]); DeleteSection(sections[sectionIndex]);
finally finally
FreeAndNil(keyNames); FreeAndNil(sections);
end; end;
end; end;
@ -572,7 +572,7 @@ var
itemIndex: Integer; itemIndex: Integer;
begin begin
ClearCollection(); ClearCollection;
WriteInteger(CollectionCountName, ACollection.Count); WriteInteger(CollectionCountName, ACollection.Count);
for itemIndex := 0 to Pred(ACollection.Count) do for itemIndex := 0 to Pred(ACollection.Count) do
@ -581,7 +581,7 @@ begin
try try
Write(ACollection.Items[itemIndex]); Write(ACollection.Items[itemIndex]);
finally finally
EndSection(); EndSection;
end; end;
end; end;
end; end;
@ -610,7 +610,7 @@ var
undoIndex: Integer; undoIndex: Integer;
begin begin
inherited Create(); inherited Create;
FFiler := AFiler; FFiler := AFiler;
@ -625,7 +625,7 @@ begin
begin begin
{ Undo all sections so far } { Undo all sections so far }
for undoIndex := 0 to Pred(SectionCount) do for undoIndex := 0 to Pred(SectionCount) do
Filer.EndSection(); Filer.EndSection;
FFiler := nil; FFiler := nil;
Break; Break;
@ -636,14 +636,14 @@ begin
end; end;
destructor TX2PersistSectionFilerProxy.Destroy(); destructor TX2PersistSectionFilerProxy.Destroy;
var var
sectionIndex: Integer; sectionIndex: Integer;
begin begin
if Assigned(Filer) then if Assigned(Filer) then
for sectionIndex := 0 to Pred(SectionCount) do for sectionIndex := 0 to Pred(SectionCount) do
Filer.EndSection(); Filer.EndSection;
inherited; inherited;
end; end;
@ -677,10 +677,10 @@ begin
end; end;
procedure TX2PersistSectionFilerProxy.EndSection(); procedure TX2PersistSectionFilerProxy.EndSection;
begin begin
if Assigned(Filer) then if Assigned(Filer) then
Filer.EndSection(); Filer.EndSection ;
end; end;

View File

@ -24,9 +24,15 @@ type
FMaximized: Boolean; FMaximized: Boolean;
FTop: Integer; FTop: Integer;
FWidth: Integer; FWidth: Integer;
FBoundsSet: Boolean;
function GetBounds(): TRect;
function GetBounds: TRect;
procedure SetBounds(const Value: TRect); procedure SetBounds(const Value: TRect);
procedure SetHeight(const Value: Integer);
procedure SetLeft(const Value: Integer);
procedure SetMaximized(const Value: Boolean);
procedure SetTop(const Value: Integer);
procedure SetWidth(const Value: Integer);
protected protected
procedure AssignTo(Dest: TPersistent); override; procedure AssignTo(Dest: TPersistent); override;
public public
@ -34,11 +40,11 @@ type
property Bounds: TRect read GetBounds write SetBounds; property Bounds: TRect read GetBounds write SetBounds;
published published
property Maximized: Boolean read FMaximized write FMaximized; property Maximized: Boolean read FMaximized write SetMaximized;
property Left: Integer read FLeft write FLeft; property Left: Integer read FLeft write SetLeft;
property Top: Integer read FTop write FTop; property Top: Integer read FTop write SetTop;
property Width: Integer read FWidth write FWidth; property Width: Integer read FWidth write SetWidth;
property Height: Integer read FHeight write FHeight; property Height: Integer read FHeight write SetHeight;
end; end;
@ -65,7 +71,7 @@ var
formPos: TX2FormPosSettings; formPos: TX2FormPosSettings;
begin begin
formPos := TX2FormPosSettings.Create(); formPos := TX2FormPosSettings.Create;
try try
formPos.Assign(AForm); formPos.Assign(AForm);
AReader.Read(formPos); AReader.Read(formPos);
@ -81,7 +87,7 @@ var
formPos: TX2FormPosSettings; formPos: TX2FormPosSettings;
begin begin
formPos := TX2FormPosSettings.Create(); formPos := TX2FormPosSettings.Create;
try try
formPos.Assign(AForm); formPos.Assign(AForm);
AWriter.Write(formPos); AWriter.Write(formPos);
@ -101,7 +107,7 @@ begin
if Source is TCustomForm then if Source is TCustomForm then
begin begin
sourceForm := TProtectedCustomForm(Source); sourceForm := TProtectedCustomForm(Source);
FMaximized := (sourceForm.WindowState = wsMaximized); Maximized := (sourceForm.WindowState = wsMaximized);
FillChar(placement, SizeOf(TWindowPlacement), #0); FillChar(placement, SizeOf(TWindowPlacement), #0);
placement.length := SizeOf(TWindowPlacement); placement.length := SizeOf(TWindowPlacement);
@ -120,6 +126,9 @@ var
boundsRect: TRect; boundsRect: TRect;
begin begin
if not FBoundsSet then
Exit;
if Dest is TCustomForm then if Dest is TCustomForm then
begin begin
destForm := TProtectedCustomForm(Dest); destForm := TProtectedCustomForm(Dest);
@ -143,7 +152,7 @@ begin
end; end;
function TX2FormPosSettings.GetBounds(): TRect; function TX2FormPosSettings.GetBounds: TRect;
begin begin
Result := Rect(FLeft, FTop, FLeft + FWidth, FTop + FHeight); Result := Rect(FLeft, FTop, FLeft + FWidth, FTop + FHeight);
end; end;
@ -151,10 +160,45 @@ end;
procedure TX2FormPosSettings.SetBounds(const Value: TRect); procedure TX2FormPosSettings.SetBounds(const Value: TRect);
begin begin
FLeft := Value.Left; Left := Value.Left;
FTop := Value.Top; Top := Value.Top;
FWidth := RectWidth(Value); Width := RectWidth(Value);
FHeight := RectHeight(Value); Height := RectHeight(Value);
end;
procedure TX2FormPosSettings.SetHeight(const Value: Integer);
begin
FHeight := Value;
FBoundsSet := True;
end;
procedure TX2FormPosSettings.SetLeft(const Value: Integer);
begin
FLeft := Value;
FBoundsSet := True;
end;
procedure TX2FormPosSettings.SetMaximized(const Value: Boolean);
begin
FMaximized := Value;
FBoundsSet := True;
end;
procedure TX2FormPosSettings.SetTop(const Value: Integer);
begin
FTop := Value;
FBoundsSet := True;
end;
procedure TX2FormPosSettings.SetWidth(const Value: Integer);
begin
FWidth := Value;
FBoundsSet := True;
end; end;
end. end.

View File

@ -29,8 +29,8 @@ type
function Read(AObject: TObject): Boolean; function Read(AObject: TObject): Boolean;
procedure Write(AObject: TObject); procedure Write(AObject: TObject);
function CreateReader(): IX2PersistReader; function CreateReader: IX2PersistReader;
function CreateWriter(): IX2PersistWriter; function CreateWriter: IX2PersistWriter;
function CreateSectionReader(const ASection: String): IX2PersistReader; function CreateSectionReader(const ASection: String): IX2PersistReader;
function CreateSectionWriter(const ASection: String): IX2PersistWriter; function CreateSectionWriter(const ASection: String): IX2PersistWriter;
@ -40,7 +40,7 @@ type
IX2PersistFiler = interface IX2PersistFiler = interface
['{BF63CDAA-98D4-42EE-A937-DFCD0074A0ED}'] ['{BF63CDAA-98D4-42EE-A937-DFCD0074A0ED}']
function BeginSection(const AName: String): Boolean; function BeginSection(const AName: String): Boolean;
procedure EndSection(); procedure EndSection;
procedure GetKeys(const ADest: TStrings); procedure GetKeys(const ADest: TStrings);
procedure GetSections(const ADest: TStrings); procedure GetSections(const ADest: TStrings);

View File

@ -25,7 +25,7 @@ type
protected protected
function CreateFiler(AIsReader: Boolean): IX2PersistFiler; override; function CreateFiler(AIsReader: Boolean): IX2PersistFiler; override;
public public
constructor Create(); constructor Create;
property Key: String read FKey write FKey; property Key: String read FKey write FKey;
property RootKey: HKEY read FRootKey write FRootKey; property RootKey: HKEY read FRootKey write FRootKey;
@ -40,7 +40,7 @@ type
function OpenKey(const ANewKey: String): Boolean; function OpenKey(const ANewKey: String): Boolean;
public public
function BeginSection(const AName: String): Boolean; override; function BeginSection(const AName: String): Boolean; override;
procedure EndSection(); override; procedure EndSection; override;
procedure GetKeys(const ADest: TStrings); override; procedure GetKeys(const ADest: TStrings); override;
@ -70,7 +70,7 @@ type
property Registry: TRegistry read FRegistry; property Registry: TRegistry read FRegistry;
public public
constructor Create(AIsReader: Boolean; ARootKey: HKEY; const AKey: String); constructor Create(AIsReader: Boolean; ARootKey: HKEY; const AKey: String);
destructor Destroy(); override; destructor Destroy; override;
end; end;
@ -93,32 +93,32 @@ const
{ Wrapper functions } { Wrapper functions }
function ReadFromRegistry(AObject: TObject; const AKey: String; ARootKey: HKEY): Boolean; function ReadFromRegistry(AObject: TObject; const AKey: String; ARootKey: HKEY): Boolean;
begin begin
with TX2UtPersistRegistry.Create() do with TX2UtPersistRegistry.Create do
try try
RootKey := ARootKey; RootKey := ARootKey;
Key := AKey; Key := AKey;
Result := Read(AObject); Result := Read(AObject);
finally finally
Free(); Free;
end; end;
end; end;
procedure WriteToRegistry(AObject: TObject; const AKey: String; ARootKey: HKEY); procedure WriteToRegistry(AObject: TObject; const AKey: String; ARootKey: HKEY);
begin begin
with TX2UtPersistRegistry.Create() do with TX2UtPersistRegistry.Create do
try try
RootKey := ARootKey; RootKey := ARootKey;
Key := AKey; Key := AKey;
Write(AObject); Write(AObject);
finally finally
Free(); Free;
end; end;
end; end;
{ TX2UtPersistRegistry } { TX2UtPersistRegistry }
constructor TX2UtPersistRegistry.Create(); constructor TX2UtPersistRegistry.Create;
begin begin
inherited; inherited;
@ -140,7 +140,7 @@ begin
if AIsReader then if AIsReader then
FRegistry := TRegistry.Create(KEY_READ) FRegistry := TRegistry.Create(KEY_READ)
else else
FRegistry := TRegistry.Create(); FRegistry := TRegistry.Create;
FRegistry.RootKey := ARootKey; FRegistry.RootKey := ARootKey;
FKey := AKey; FKey := AKey;
@ -149,7 +149,7 @@ begin
end; end;
destructor TX2UtPersistRegistryFiler.Destroy(); destructor TX2UtPersistRegistryFiler.Destroy;
begin begin
FreeAndNil(FRegistry); FreeAndNil(FRegistry);
@ -195,7 +195,7 @@ begin
end; end;
procedure TX2UtPersistRegistryFiler.EndSection(); procedure TX2UtPersistRegistryFiler.EndSection;
begin begin
inherited; inherited;