7

I have a paint box which I want the user to be able to undock and move around. So I set its DragKind to dkDock and its DragMode to dmAutomatic, and put it inside a panel with DockSite set to True. I'm experiencing a rather odd behavior when I dock the paint box after having undocked it to a floating form. The close button of the floating form appears inside the panel. I've attached two screenshots. One from the original state, and one after docking the paint box again. What am I missing?

Original State:

Before undocking

After docking:

After docking


UPDATE After using TLama's solution, here's the result.

After Docking ; Using new dock manager

iMan Biglari
  • 4,674
  • 1
  • 38
  • 83

2 Answers2

4

You're not missing anything. That's how the default dock manager implementation works. It just wants to have grabber with the close button available on dock site, which uses it. What you can do, is implement your own dock manager and override its AdjustDockRect method, which controls the size of docking zone and where is in default dock manager implementation made a space for grabber with close button. If you don't want that grabber, just keep the size of dock zone rectangle as it was passed to the method, in size of the whole dock site. In other words, do nothing in that method override.

That's for the functional part of the grabber, but except that you need to intercept hardcoded drawing of it. To do so, you need to override the PaintDockFrame event method and like before, do just nothing there.

Here's a code sample:

type
  TNoGrabDockManager = class(TDockTree)
  protected
    procedure AdjustDockRect(Control: TControl; var ARect: TRect); override;
    procedure PaintDockFrame(Canvas: TCanvas; Control: TControl;
      const ARect: TRect); override;
  end;

implementation

{ TNoGrabDockManager }

procedure TNoGrabDockManager.AdjustDockRect(Control: TControl; var ARect: TRect);
begin
  // here you can make space for a grabber by shifting top or left position
  // of the ARect parameter, which is by default set to the whole dock site
  // bounds size, so if you do nothing here, there will be no grabber
end;

procedure TNoGrabDockManager.PaintDockFrame(Canvas: TCanvas; Control: TControl;
  const ARect: TRect);
begin
  // in this event method, the grabber with that close button are drawn, so
  // as in case of disabling grabber functionality do precisely nothing for
  // drawing it here, that will make it visually disappear
end;

Here's how to use such custom dock manager (see below for note about UseDockManager property):

procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel1.DockManager := TNoGrabDockManager.Create(Panel1);
  Panel1.UseDockManager := True;
end;

Important

As few sources suggest, you should set the UseDockManager property of your dock panel to False at design time. I don't know why, but from quick tests I've made, some of the event methods of the custom dock manager were not fired when I didn't have set that property at design time (the AdjustDockRect event method worked properly even without doing so, but I wouldn't personally rely on it).

TLama
  • 75,147
  • 17
  • 214
  • 392
  • Here's a reason for recycling `UseDockManager` after changing `DockManager`: `procedure TWinControl.SetUseDockManager(Value: Boolean); begin if FUseDockManager <> Value then begin FUseDockManager := Value; if not (csDesigning in ComponentState) and Value then FDockManager := CreateDockManager; end; end;` – iMan Biglari Jan 23 '13 at 13:13
  • Could you please take another look at the update? There's a residue of the close button behind my paint box :-( – iMan Biglari Jan 23 '13 at 13:20
  • 1
    I see, now it's disabled just the functional part of a grabber, but there's still hardcoded drawing of it based by the evil `FGrabberSize` field which takes another hardcoded constant value `GrabberSize`. You'll need to override yet another method, the `PaintDockFrame`, where the grabber is drawn. – TLama Jan 23 '13 at 13:26
  • I just overrode `PaintDockFrame`. Thanks for the hint :-) – iMan Biglari Jan 23 '13 at 13:26
  • 1
    You're welcome! However, that implementation is quite disaster. The slow motion of dragged objects and glitches like that hardcoded grabber would tend me to use another solution than this one which will be in Delphi from some prehistorical ages, I guess. Surely you can make your own dock manager, but it takes some time. – TLama Jan 23 '13 at 13:32
  • 1
    I'll get back this part of my application to use another docking solution, perhaps JVCL in a few weeks, but I just needed a quick fix for this. I guess you have had those customers who always nag about even the smallest visual glitches in their software ;-) – iMan Biglari Jan 23 '13 at 13:33
2

Rather than using a panel as the dock target, use a TPageControl and hide the tab from the generated tab sheet. Since a page control normally has visible tabs, the delete handle is not displayed. Unfortunately, when you hide a tab sheet's tab, the sheet itself is also hidden. So you must save and restore it by adding the following OnDockDrop event:

procedure TForm2.PageControl1DockDrop(Sender: TObject; Source: TDragDockObject;
  X, Y: Integer);
var
  ix: Integer;
begin
  ix := PageControl1.ActivePageIndex;
  PageControl1.ActivePage.TabVisible := false;
  PageControl1.ActivePageIndex := ix;
end;
SteveC
  • 644
  • 7
  • 12