How to add persistence to the Delphi Docking example
Asked Answered
O

2

11

Although I realise that in addition to the included Delphi docking demo there are other and better docking libraries available such as the Developer Express Library and the JVCL Docking Library, but for a specific demonstration project I am restricted to use only the Delphi built-in capability (despite some of the noted flaws).

My question relates to adding persistence to the docking state. I see from examining Controls.pas that TDockTree is the default dock manager and it has Stream I/O routines. Digging around on SO and in various forums though I cant see how anyone has called these routines. I've tried loading and saving to a file from the relevant Create and OnDrop events but I'm stabbing in the dark. I am happy saving and restoring form sizes and states but am struggling with the concepts of what I should be saving. Would any kind person give me a starting place?

I'm using Delphi XE3, so all (?) things are possible!

Many thanks.

Ostyak answered 4/1, 2013 at 16:17 Comment(1)
I found attempts at persistence using JVCL deeply difficult, and would be surprised if the built in (nearly useless) docking library code was any nicer, in fact, I'm sure it's worse. No point. If your time is worth money, just buy something that does what you need. ;-)Luannaluanne
U
0

I'm using Toolbar 2000 from J. Russels. It is providing panels, toolwindow's and toolbar's.

That one provides functions like TBRegSavePositions and TBRegSavePositions to store the user customization into registry.

Loading a "view" get's easily done by on code line:

TBRegLoadPositions(self, HKEY_CURRENT_USER, c_BaseUserRegKey);

in this case self is my form.

Upswell answered 4/10, 2013 at 11:54 Comment(0)
S
0

You can load and save your docking configuration with the LoadFromStream and SaveToStream methods by storing the data in a string.

Therefore, the following methods are required:

  • save the current docking configuration to a string
  • load the current docking configuration from a string

Here is some code to do this:

function GetDockString(const AManager: IDockManager): AnsiString;
var
  LStream: TMemoryStream;
begin
  LStream := TMemoryStream.Create();
  try
    AManager.SaveToStream(LStream);
    SetLength(Result, 2 * LStream.Size);
    BinToHex(LStream.Memory, PAnsiChar(Result), LStream.Size);
  finally
    FreeAndNil(LStream);
  end;
end;

procedure ReadDockString(const ADockString: AnsiString; const AManager: IDockManager);
var
  LStream: TMemoryStream;
begin
  LStream := TMemoryStream.Create();
  try
    LStream.Size := Length(ADockString) div 2;
    HexToBin(PAnsiChar(ADockString), LStream.Memory, LStream.Size);
    LStream.Position := 0;
    AManager.LoadFromStream(LStream);
  finally
    FreeAndNil(LStream);
  end;
end;

I've used such methods in an application to create dockable windows, but the vcl provides only a very basic user experience. You can do something about it, but it is hard to test and debug - I already spent too much time to use and override TCustDockDragObject and TCaptionedTabDockTree, so I would recommend using a docking framework.

Here is a minimal example which creates two forms and reads a docking configuration.

TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
  procedure FormDblClick(Sender: TObject);
private
    FPanel: TPanel;
end;

Implementation:

procedure TForm1.FormCreate(Sender: TObject);
var
  LWindow: TForm;
const
  LDockExample = '0000080000000000000000000000000000000000000000000000000100000000000000000B0000004368696C6457696E646F77FFFFFFFF';
begin
  FPanel := TPanel.Create(Self);
  FPanel.Align := alTop;
  FPanel.Height := 300;
  FPanel.DockSite := true;
  FPanel.Parent := Self;

  LWindow := TForm.CreateNew(Self);
  LWindow.Name := 'ChildWindow';
  LWindow.DragKind := dkDock;
  LWindow.BoundsRect:=Rect(10, 10, 400, 400);
  LWindow.Color := clGreen;
  LWindow.Show;

  ReadDockString(LDockExample, FPanel.DockManager);
end;

procedure TForm1.FormDblClick(Sender: TObject);
begin
  ShowMessage(GetDockString(FPanel.DockManager));
end;
Sect answered 22/9, 2018 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.