Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 01-04-2012, 04:44 PM
lars lars is offline
Member
 
Join Date: Apr 2002
Posts: 34
Unhappy TTreeview porting from D7 to XE (unicode)

Hello,

I was using the treeview component TTreeViewEx from Workshell in D7
now im trying to port a project of mine to delphi Xe , everything works now only every file saved from the treeview in D7 is not readable in the XE project.

This has probably to do with unicode.

Does anybody knows how to solve this so it works both on d7 and XE??

================================================== =======
SOurce Code:

function StreamReadString(Stream: TStream): String;
var
Count, I: Integer;
Buffer: String;
C: Char;
begin
if Stream = nil then
begin
Result := '';
Exit;
end;
Stream.Read(Count,SizeOf(Integer));
Buffer := '';
for I := 1 to Count do
begin
Stream.Read(C,SizeOf(Char));
Buffer := Buffer + C;
end;
Result := Buffer;
end;

procedure StreamWriteString(Stream: TStream; S: String);
var
Count, I: Integer;
C: Char;
begin
if Stream = nil then Exit;
Count := Length(S);
Stream.Write(Count,SizeOf(Integer));
for I := 1 to Count do
begin
C := S[I];
Stream.Write(C,SizeOf(Char));
end;
end;

{ TTreeViewEx }

procedure TTreeViewEx.LoadFromFileEx(const FileName: String);
var
F: TFileStream;
begin
F := TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
try
LoadFromStreamEx(F);
finally
F.Free;
end;
end;

procedure TTreeViewEx.SaveToFileEx(const FileName: String);
var
F: TFileStream;
begin
F := TFileStream.Create(FileName,fmCreate or fmShareDenyWrite);
try
SaveToStreamEx(F);
finally
F.Free;
end;
end;

procedure TTreeViewEx.LoadNodes(Stream: TStream; Node: TTreeNode);
var
Name: String;
Level, ImageIndex, StateIndex, SelectedIndex, DataSize, ChildCount, I: Integer;
Data: Pointer;
NewNode: TTreeNode;
begin
Name := StreamReadString(Stream);
Stream.Read(Level,SizeOf(Integer));
Stream.Read(ImageIndex,SizeOf(Integer));
Stream.Read(StateIndex,SizeOf(Integer));
Stream.Read(SelectedIndex,SizeOf(Integer));
Stream.Read(DataSize,SizeOf(Integer));
if DataSize > 0 then
begin
GetMem(Data,DataSize);
Stream.Read(Data^,DataSize);
end
else
Data := nil;
Stream.Read(ChildCount,SizeOf(Integer));
if Level > 0 then
NewNode := Self.Items.AddChild(Node,Name)
else
NewNode := Self.Items.Add(nil,Name);
NewNode.ImageIndex := ImageIndex;
NewNode.StateIndex := StateIndex;
NewNode.SelectedIndex := SelectedIndex;
NewNode.Data := Data;
if Assigned(FOnNodeLoaded) then FOnNodeLoaded(Self,NewNode);
for I := 1 to ChildCount do
begin
LoadNodes(Stream,NewNode);
end;
end;

procedure TTreeViewEx.SaveNodes(Stream: TStream; Node: TTreeNode);
var
Name: String;
Level, ImageIndex, StateIndex, SelectedIndex, DataSize, ChildCount, I: Integer;
NewNode: TTreeNode;
begin
Name := Node.Text;
StreamWriteString(Stream,Name);
Level := Node.Level;
Stream.Write(Level,SizeOf(Integer));
ImageIndex := Node.ImageIndex;
Stream.Write(ImageIndex,SizeOf(Integer));
StateIndex := Node.StateIndex;
Stream.Write(StateIndex,SizeOf(Integer));
SelectedIndex := Node.SelectedIndex;
Stream.Write(SelectedIndex,SizeOf(Integer));
if Assigned(FOnRequestDataSize) then
FOnRequestDataSize(Self,Node,DataSize)
else
DataSize := -1;
if Node.Data = nil then DataSize := -1;
Stream.Write(DataSize,SizeOf(Integer));
if DataSize > 0 then Stream.Write(Node.Data^,DataSize);
ChildCount := Node.Count;
Stream.Write(ChildCount,SizeOf(Integer));

NewNode := Node.GetFirstChild;
if NewNode = nil then Exit;
SaveNodes(Stream,NewNode);
for I := 2 to ChildCount do
begin
NewNode := Node.GetNextChild(NewNode);
if NewNode <> nil then SaveNodes(Stream,NewNode);
end;
end;

procedure TTreeViewEx.LoadFromStreamEx(Stream: TStream);
var
Count, I: Integer;
begin
if Stream = nil then Exit;

Self.Items.Clear;
Stream.Read(Count,SizeOf(Integer));
for I := 1 to Count do
begin
LoadNodes(Stream,nil);
end;
end;

procedure TTreeViewEx.SaveToStreamEx(Stream: TStream);
var
Count, I: Integer;
begin
if Stream = nil then Exit;

Count := 0;
for I := 0 to Self.Items.Count - 1 do
begin
if Self.Items[I].Level = 0 then Inc(Count);
end;
Stream.Write(Count,SizeOf(Integer));
for I := 0 to Self.Items.Count - 1 do
begin
if Self.Items[I].Level = 0 then SaveNodes(Stream,Self.Items[I]);
end;
end;
Closed Thread

Tags
treeview, unicode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:03 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.