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

View File

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

View File

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

View File

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