{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.delphi-jedi.org

Known Issues:
-----------------------------------------------------------------------------}
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  JvDockControlForm, JvDockVIDStyle, JvDockDelphiStyle, JvDockVIDVCStyle,
  StdCtrls, JvComponent, ComCtrls, ExtCtrls, Fr_1, Fr_2;

type

  TRunTimeForm = class(TForm)
  private
    FDockClient: TJvDockClient;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Close(Sender: TObject; var Action: TCloseAction);
    property DockClient: TJvDockClient read FDockClient;
  end;

  TMyCustomFloatingForm = class(TCustomDockForm)
  private
    FDockClient: TJvDockClient;
  public
    constructor Create(AOwner: TComponent); override;
    property DockClient: TJvDockClient read FDockClient;
  end;

  TForm1 = class(TForm)
    Frame32: TFrame3;
    Frame31: TFrame3;
    Frame41: TFrame4;
    procedure FormCreate(Sender: TObject);
    procedure Frame31EndDock(Sender, Target: TObject; X, Y: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  FFormCount: array[0..4] of Integer;

  function ConstructRunTimeForm(AStyle: TJvDockBasicStyle; const AName: string; const Loading: Boolean):TRunTimeForm;
  procedure FreeRunTimeForms;
  function GetFormCount(AStyle: TJvDockBasicStyle): Integer;
  procedure SetFormCount(AStyle: TJvDockBasicStyle; const Value: Integer);
  public
    { Public declarations }
   JvDockVIDStyle1: TJvDockVIDStyle;
   JvDockVIDVCStyle1: TJvDockVIDVCStyle;
   function StyleToStr(AStyle: TJvDockBasicStyle): string;
   function StyleToID(AStyle: TJvDockBasicStyle): Integer;
   property FormCount[AStyle: TJvDockBasicStyle]: Integer read GetFormCount write SetFormCount;
   end;

var
  Form1: TForm1;
  lbDockServer1: TJvDockServer;
  JvDockDelphiStyle1: TJvDockDelphiStyle;

implementation

{$R *.DFM}

var R: TRect;

constructor TRunTimeForm.Create(AOwner: TComponent);
begin
  CreateNew(AOwner);
  Width := 186;
  Height := 188;
  BorderStyle := bsSizeToolWin;
  DockSite := False;
  DragKind := dkDock;
  DragMode := dmAutomatic;
  Font.Name := 'MS Shell Dlg 2';
  FormStyle := fsStayOnTop;
  onClose:= Close;
//  Position := poDefaultPosOnly;
  FDockClient := TJvDockClient.Create(Self);
  with FDockClient do
  begin
    DirectDrag := True;
    ShowHint := True;
    EnableCloseButton := True;
    EachOtherDock := False;
//    DockStyle := Form1.JvDockVIDStyle1
  end
end;

procedure TRunTimeForm.Close(Sender: TObject; var Action: TCloseAction);
begin
   Action := caFree
end;

destructor TRunTimeForm.Destroy;
begin
  inherited Destroy;
end;

constructor TMyCustomFloatingForm.Create(AOwner: TComponent);
begin
  inherited
  Create(AOwner);
  FDockClient := TJvDockClient.Create(Self);
  with FDockClient do
  begin
    DirectDrag := True;
    ShowHint := True;
    EnableCloseButton := True;
    EachOtherDock := False;
    DockStyle := Form1.JvDockVIDVCStyle1
  end
end;

function TForm1.GetFormCount(AStyle: TJvDockBasicStyle): Integer;
begin
  Result := FFormCount[StyleToID(AStyle)];
end;

procedure TForm1.SetFormCount(AStyle: TJvDockBasicStyle;
  const Value: Integer);
begin
  FFormCount[StyleToID(AStyle)] := Value;
end;

function CreateUniqueName: string;
var
  I: Integer;

  function IsUnique(const AName: string): Boolean;
  var
    I: Integer;
  begin
    Result := False;
    for I := 0 to Screen.FormCount - 1 do
      if CompareText(AName, Screen.Forms[I].Name) = 0 then
        Exit;
    Result := True;
  end;

begin
  for I := 1 to MaxInt do
  begin
    Result := Format('RunTimeForm_%d', [I]);
    if IsUnique(Result) then
      Exit;
  end;
end;

function TForm1.StyleToID(AStyle: TJvDockBasicStyle): Integer;
begin
  if AStyle is TJvDockDelphiStyle then
    Result := 0
  else
    raise Exception.Create('Unknown style');
end;

function TForm1.StyleToStr(AStyle: TJvDockBasicStyle): string;
begin
  Result := 'Delphi Style';
end;

function TForm1.ConstructRunTimeForm(AStyle: TJvDockBasicStyle; const AName: string; const Loading: Boolean):TRunTimeForm;
var
  LStyle: Integer;
begin
  Result := TRunTimeForm.Create(Application);
  if not Loading then
    Result.Visible := True;
  if AName = '' then
    Result.Name := CreateUniqueName
  else
  Result.Name := AName;
  Result.Caption := StyleToStr(AStyle) + ' _ ' + IntToStr(FormCount[AStyle]);
  FormCount[AStyle] := FormCount[AStyle] + 1;
//  Result.DockClient.DockStyle := Form1.JvDockVIDStyle1;
  Result.DockClient.DockStyle := Form1.JvDockVIDVCStyle1;

  LStyle := StyleToID(AStyle);
  Result.DockClient.DirectDrag := (LStyle = 0);
  Result.DockClient.EachOtherDock :=
    (LStyle = 3) or (LStyle = 4) or (LStyle = 2);

end;

procedure TForm1.FreeRunTimeForms;
var
  I: Integer;
  Frm: TForm;
  ADockClient: TJvDockClient;
begin
  for I := Screen.FormCount - 1 downto 0 do
    if Screen.Forms[I] is TRunTimeForm then
    begin
      Frm := Screen.Forms[I];
      ADockClient := FindDockClient(Frm);
      if ADockClient <> nil then
        DoFloatForm(Frm);
      Frm.Free;
    end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeRunTimeForms
end;

procedure TForm1.FormCreate(Sender: TObject);
var DTest: TRunTimeForm;
begin
    lbDockServer1:= TJvDockServer.Create(Self);
    JvDockVIDStyle1:= TJvDockVIDStyle.Create(Self);
    JvDockDelphiStyle1:= TJvDockDelphiStyle.Create(Self);
    JvDockVIDVCStyle1:= TJvDockVIDVCStyle.Create(Self);

    with lbDockServer1 do begin
    LeftSplitterStyle.Cursor := crHSplit;
    LeftSplitterStyle.ParentColor := False;
    TopSplitterStyle.Cursor := crVSplit;
    TopSplitterStyle.ParentColor := False;
    RightSplitterStyle.Cursor := crHSplit;
    RightSplitterStyle.ParentColor := False;
    BottomSplitterStyle.Cursor := crVSplit;
    BottomSplitterStyle.ParentColor := False;
//    DockStyle := JvDockVIDStyle1;
    DockStyle := JvDockVIDVCStyle1;
    Left := 24;
    Top := 16;
    end;

    with JvDockVIDStyle1 do begin
    ConjoinServerOption.GrabbersSize := 18;
    ConjoinServerOption.SplitterWidth := 4;
    TabServerOption.TabPosition := tpBottom;
    Left := 24;
    Top := 48;
    end;

    with JvDockDelphiStyle1 do begin
    ConjoinServerOption.GrabbersSize := 12;
    ConjoinServerOption.SplitterWidth := 4;
    Left := 32;
    Top := 112;
    end;

    with JvDockVIDVCStyle1 do begin
    ConjoinServerOption.GrabbersSize := 18;
    ConjoinServerOption.SplitterWidth := 4;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).ActiveFont.Charset := ANSI_CHARSET;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).ActiveFont.Color := clWhite;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).ActiveFont.Height := -11;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).ActiveFont.Name := 'Tahoma';
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).ActiveFont.Style := [fsBold];
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).InactiveFont.Charset := ANSI_CHARSET;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).InactiveFont.Color := 13160660;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).InactiveFont.Height := -11;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).InactiveFont.Name := 'Tahoma';
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).InactiveFont.Style := [fsBold];
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).TextAlignment := taRightJustify;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).ActiveTitleStartColor := 6956042;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).ActiveTitleEndColor := clSkyBlue ;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).InactiveTitleStartColor := clGray ;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).InactiveTitleEndColor := clSilver  ;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).TextEllipsis := True;
    TJvDockVIDVCConjoinServerOption(ConjoinServerOption).SystemInfo := False;
    TabServerOption.HotTrack := True;
    TabServerOption.TabPosition := tpBottom;
    TJvDockVIDVCTabServerOption(TabServerOption).ActiveSheetColor := clBtnFace;
    TJvDockVIDVCTabServerOption(TabServerOption).InactiveSheetColor := clBtnShadow;
    TJvDockVIDVCTabServerOption(TabServerOption).ActiveFont.Charset := DEFAULT_CHARSET;
    TJvDockVIDVCTabServerOption(TabServerOption).ActiveFont.Color := clWindowText;
    TJvDockVIDVCTabServerOption(TabServerOption).ActiveFont.Height := -11;
    TJvDockVIDVCTabServerOption(TabServerOption).ActiveFont.Name := 'MS Sans Serif';
    TJvDockVIDVCTabServerOption(TabServerOption).ActiveFont.Style := [];
    TJvDockVIDVCTabServerOption(TabServerOption).InactiveFont.Charset := DEFAULT_CHARSET;
    TJvDockVIDVCTabServerOption(TabServerOption).InactiveFont.Color := clWhite;
    TJvDockVIDVCTabServerOption(TabServerOption).InactiveFont.Height := -11;
    TJvDockVIDVCTabServerOption(TabServerOption).InactiveFont.Name := 'MS Sans Serif';
    TJvDockVIDVCTabServerOption(TabServerOption).InactiveFont.Style := [];
    TJvDockVIDVCTabServerOption(TabServerOption).HotTrackColor := clBlue;
    TJvDockVIDVCTabServerOption(TabServerOption).ShowTabImages := True;
    Left := 216;
    Top := 112;
    end;

    GetWindowRect(Frame41.Handle,R);
    Frame41.ManualFloat(R);

    DTest:=ConstructRunTimeForm(JvDockDelphiStyle1, '', False);
    DTest.Width:=Frame41.HostDockSite.Width;
    DTest.Height:=Frame41.HostDockSite.Height;
    DTest.Color:=Frame41.Color;

    Frame41.DragKind:=dkDrag;
    Frame41.DragMode:=dmManual;
    Frame41.Parent:=DTest;
    TFrame(DTest.FindChildControl(Frame41.Name)).Top:=0;
    TFrame(DTest.FindChildControl(Frame41.Name)).Left:=0;

    DTest.ManualDock(lbDockServer1.RightDockPanel,nil,alNone);

//    Frame31.FloatingDockSiteClass:=TMyCustomFloatingForm;
//    Frame32.FloatingDockSiteClass:=TMyCustomFloatingForm;

end;

procedure TForm1.Frame31EndDock(Sender, Target: TObject; X, Y: Integer);
var NForm: TRunTimeForm; i:integer; pt: TPoint;
begin

  pt:=mouse.CursorPos;

//  showmessage((Sender as TControl).HostDockSite.ClassName+'~~~'+(Sender as TControl).HostDockSite.Name);

  if FindDragTarget(pt,true) <> nil then begin

  if (Sender as TControl).HostDockSite.ClassName = 'TJvDockVIDVCPanel' then
  begin

  end
  else begin
  NForm:=ConstructRunTimeForm(JvDockDelphiStyle1, '', False);
  NForm.Width:=TFrame(Sender).HostDockSite.Width;
  NForm.Height:=TFrame(Sender).HostDockSite.Height;

  NForm.Left:=X-TFrame(FindDragTarget(pt,true)).Left;
  NForm.Top:=Y-TFrame(FindDragTarget(pt,true)).Top;

  NForm.Color:=TFrame(Sender).Color;
  TFrame(Sender).DragKind:=dkDrag;
  TFrame(Sender).DragMode:=dmManual;
  TFrame(Sender).Parent:=NForm;
  TFrame(NForm.FindChildControl(TFrame(Sender).Name)).Top:=0;
  TFrame(NForm.FindChildControl(TFrame(Sender).Name)).Left:=0;
  end;

  end;

end;

end.

