Sign in Join
Wiki copytemporary

Anchor Docking: what changed

Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11About docking in general see [[Docking]].
22
33## Overview
44
55Docking allows you to combine forms, or to treat controls as forms. You use the mouse to drag and dock forms together or undock (split) them. The basic dragging, docking and undocking functionality is provided by the LCL. But you need a docking manager to add splitters and grab zones, and to save and restore layouts.
66
77Anchor docking has been implemented since version 0.9.29 in the package *anchordocking.lpk* for use in your own Lazarus applications, and *AnchorDockingDsgn.lpk* if you wish to make the Lazarus IDE windows dockable. It is easy to use, and provides many features and options not available in earlier docking packages.
88
99You will find a code example at: components/anchordocking/miniide/miniide1.lpi
1010
1111This means that the earlier TLazDockingManager and unit LDockCtrl are obsolete and now deprecated.
1212
1313### Features
1414
1515- **Drag and dock**: Headers are added that can be dragged to dock controls with the mouse. Preview rectangles will show where and how a control is docked.
1616- **Any layout**: By using the LCL's anchors almost any layout is possible. You can dock to left, right, top, bottom, inside or outside or as page like in a TPageControl. You can enlarge docked controls via the popup menu. No need to undock your whole layout.
1717- **What you see is how it is structured**: There are no hidden panels to align controls in rows and columns. Anchor docking makes sure forms do not overlap.
1818- **Splitters** are automatically inserted between docked forms to allow resizing.
1919- **Page docking**. Forms can be docked not only left/right/above/below, but in pages too. A TPageControl is automatically created for native look and feel. A page can contain arbitrary docked forms too, including paged docked forms, allowing nested pages and layouts. When a page is undocked the pagecontrol is automatically removed. You can drag and drop the tabs or use the popup menu to move pages.
2020- **Easy use**: With one line of code you can make a form or control dockable. Just give them unique names.
2121- Not only forms, but any **TWinControl** can be made dockable.
2222- Can **save/load layouts**. The layout information is stored for each dockable control and old layout files will still work even when the application gets more dockable controls or some are removed. The information is stored in the abstract class LCL TConfigStorage. So you can use for example TXMLConfigStorage to store layouts in xml files or you can write your own storage to store it wherever you want.
2323- Each docked form is put into a docksite providing an optional **header**. The header shows the caption of the docked form and contains a close button. You can *drag the header* to drag the form and undock it or to dock it at another position. The header can be at any of the four sides and moves by default automatically to the smaller side to save space. You can customize this. The header shows a customizable hint allowing to read long captions.
2424- When a docksite is resized the *child sites are automatically scaled*. You can turn this off.
2525- **Preserve size of forms**. For instance when docking a form to a left side of a site the width of the docked form is kept. The same for the other sides and the same for undocking.
2626- **Flickerless**: The restore tries to reuse existing sites and splitters to reduce creation and flickering of forms and to preserve the Z order of forms. This allows to quickly switch between layouts.
2727- **Header popup menu**:
2828 - lock/unlock (disable dragging)
2929 - header position (auto, left, top, right, bottom), this is saved/restored with the layout
3030 - merge (for example after moving a dock page into a layout)
3131 - undock (needed if no place to undock on screen)
3232 - enlarge side to left, top, right, bottom
3333 - close
3434 - options
3535- **Page popup menu**:
3636 - lock/unlock (disable dragging)
3737 - undock (needed if no place to undock on screen)
3838 - tab position for pagecontrol (top,bottom, left, right), this is saved/restored with the layout
3939 - move page to the left, right, leftmost, rightmost
4040 - close
4141 - options
4242- Forms can be made dock sites. Then they can dock at one side. But they can not be docked itself.
4343- close button automatically saves a layout
4444- Powerful functions to alter the layout. You can use D&D to move a form and you can undock and dock in one move. There are functions to enlarge forms and shrinking others. These functions are available via the header popup menu as well. See below.
4545- There is an IDE package which makes the Lazarus IDE dockable. It is called anchordockingdsgn.lpk. Make sure you only install one docking package. Do not install the easydockmgr at the same time!
4646
4747### ToDos
4848
4949- Design time package: Add all layout files to menu
5050- simple way to make forms dockable at designtime (that means without writing any code)
5151- minimize button and Hide
5252- on show again: restore a default layout
5353- close button for pages
5454- Implement automatic menu merging in the LCL (when two forms with main menus are docked).
5555
5656## Usage I. (Add docking support to the Lazarus IDE)
5757
5858If you don't like the default "separate windows" look of the Lazarus IDE and prefer a single window (e.g., like RAD Studio™), you can install a package called **[[AnchorDockingDsgn]]** (lazarus/components/anchordocking/design/anchordockingdsgn.lpk). This package makes the IDE windows dockable.
5959
6060### Important to macOS users
6161
6262Before Lazarus 3.0, there are many bugs related to docking.
6363
6464If "anchordockingdsgn" has been installed before Lazarus 3.0, the wrong configuration may be saved and needs to be reset first.
6565
6666The configuration is saved in environmentoptions.xml, which usually located in ~/.lazarus
6767
6868Close the IDE, locate environmentoptions.xml, delete the content between \<AnchorDocking\> and \</AnchorDocking\>, and the initial setting of anchordockingdsgn will be automatically set when the IDE is started again.
6969
7070### Installation
7171
7272Uninstall any other docking managers (e.g., easydockmgrdsgn). From the Lazarus menu bar pick "Package" - "Install/Uninstall Packages" and install the "anchordockingdsgn" package.
7373
7474After you have successfully installed the package and restarted the IDE, the IDE starts with a default docked layout like shown below. Note that AnchorDocking headers and splitters have been added to all dockable windows of the IDE. In case the headers don't get visible, see the section "Options" below on how to enable/disable them.
7575
7676![](/wiki-images/a/ac/2021-12-02_10_21_07-Debian_10.8_Buster_-_VMware_Workstation.png)
7777
7878You can change the layout by grabbing a dockable window by the Anchordocking header and drag and dock it anywhere else, or resize docked windows via splitters added to the window borders. You can right-click on the headers too for other options, for example, to change the look of the headers, as shown farther down below.
7979
8080You can save the layout via the menu item **Tools / Save window layout as default**. This saves to ~/.lazarus/anchordocklayout.xml and restores it when the IDE starts up.
8181
8282You can also save the layout to a file and load them later.
8383
8484Since Lazarus 1.1 you can pass a command line parameter
8585
86-`./lazarus --anchordocklayout=<filename>`
86+```
87+./lazarus --anchordocklayout=<filename>
88+```
8789
8890The look (like the headers) can be further tweaked by right-clicking on them.
8991
9092![](/wiki-images/8/8a/Docking_headers.gif)
9193
9294### Options
9395
9496After the successful installation of the AnchorDockingDsgn package, an additional configuration menu is added to the Lazarus options dialog ("Tools" - "Options" - "Environment")
9597
9698![](/wiki-images/3/3a/Anchordocking_options.png)
9799
98100#### Scale on resize
99101
100102When resizing a window, all docked forms inside are scaled by the same percentage.
101103
102104#### Show headers
103105
104106To dock/undock a window, you need to enable this option.
105107
106108#### Flatten headers
107109
108110No 3d frame in headers.
109111
110112#### Header align top/left
111113
112114Headers are automatically put at top or left depending on these two values:
113115
114116- Move header to top, when (width/height)*100\<=HeaderAlignTop, default 80
115117- Move header to left, when (width/height)*100\>=HeaderAlignLeft, default 120
116118- Otherwise keep the header at the current position.
117119
118120Which means the header is by default put at the smaller side of a docked control. When the control is resized, the header switches to the smaller side.
119121
120122#### Allow dock sites to be minimized
121123
122124A extra minimize button is added, so a site can be minimized. Then sites next to it get more space. If needed, the minimized site then can easy be opened.
123125
124126#### Multine Tabs
125127
126128Feature added in Lazarus 2.2. If a OS support multiline tabs (like Windows), if there are no many tabs, they are shown in multiple lines.
127129
128130#### Floating windows on top
129131
130132Feature added in Lazarus 2.2. If there are undocked windows shown, show these on top of main window. In a own application, there can be set DockMaster.MainDockForm, then all floating windows are shown on top of it. If MainDockForm is not set, Application.MainForm is used.
131133
132134## Usage II. (Add docking support to your own applications)
133135
134136### Quick start
135137
136138Add the package **AnchorDocking** to the required packages in the project inspector.
137139
138140Then add the unit **AnchorDocking** to the uses section of your main unit (the unit with your main form).
139141
140142Add to the FormCreate of your main form:
141143
142144``` pascal
143145 DockMaster.MakeDockSite(Self,[akBottom],admrpChild);
144146```
145147
146148For the other dockable forms replace all calls of *Show* and *ShowOnTop* with
147149
148150``` pascal
149151 DockMaster.MakeDockable(Form1,true,true);
150152```
151153
152154For the other dockable forms replace all calls of *Visible:=true* with
153155
154156``` pascal
155157 DockMaster.MakeDockable(Form1,true);
156158```
157159
158160Remove or comment all *Hide*, *Visible:=false*, *WindowState:=* calls.
159161
160162### Prerequisites
161163
162164- Your project must use the package *AnchorDocking*.
163165- All dockable forms must be shown with MakeDockable or MakeDockSite. Do not use *Show*, *BringToFront*, *ShowOnTop*, *Visible:=true*. Of course you can have some non dockable forms, like hints or splashscreens.
164166- All dockable forms must be free resizable, that means no constraints, no BorderStyle bsDialog.
165167- No other docking system. AnchorDocking is incompatible with EasyDockMgr.
166168
167169### Making forms dockable
168170
169171There are two ways to make a form dockable. You can use **DockMaster.MakeDockable** to wrap a form into a dock site. Then your form can dock and be docked completely free. You will use that for the biggest part of your forms, except for the MainForm, because this is only supported for the MainForm on gtk2 and qt. The second way is to use **DockMaster.MakeDockSite**. The form can dock other sites, but can not be docked itself. You might want to use *MakeDockSite* for the *MainForm*. For most other forms you probably want to use this:
170172
171173``` pascal
172174// shows YourForm
173175// if not already done, wrap the form into a dock site
174176DockMaster.MakeDockable(YourForm);
175177```
176178
177179Here are some examples how a form looks like that was made dockable with **DockMaster.MakeDockable**:
178180
179181![](/wiki-images/b/bc/Docksite1.png) ![](/wiki-images/c/c6/Docksite2.png)
180182
181183The header position is normally done automatically and the user can with a right click put it to any of the four sides.
182184
183185### Example: miniide1
184186
185187The example can be found in *lazarus/components/anchordocking/miniide/miniide1.lpi*. It has a *mainform* in unit *unit1.pas* with a main menu which works as a docksite and several forms that are dockable.
186188
187189![](/wiki-images/a/a5/Anchordocking_miniide1.png)
188190
189191### Making the main form a dock site
190192
191193In the TMainIDE.FormCreate the main form **MainIDE** is turned into a docksite:
192194
193195``` pascal
194196DockMaster.MakeDockSite(Self,[akBottom],admrpChild);
195197```
196198
197199The parameter *\[akBottom\]* allows to dock other sites at the bottom of MainIDE. The parameter *admrpChild* tells the DockMaster that when the MainIDE is enlarged the extra space is distributed to the docked sites.
198200
199201When the user docks a site to the bottom of MainIDE the site is put onto MainIDE (Site.Parent:=MainIDE) with Site.Align=alBottom. The other controls on MainIDE must be compatible to this. MainIDE for example has a BtnPanel with Align=alLeft and a TNoteBook with Align=alClient. See [[Autosize / Layout|here]] how Align works.
200202
201203The DockMaster adds automatically a splitter:
202204
203205![](/wiki-images/c/ca/Anchordocking_miniide_dockedse1.png)
204206
205207As soon as a dockable form is docked its caption is shown in the dock header.
206208
207209#### Requirements for a custom dock site
208210
209211As long as nothing is docked into a custom dock site, your form can do everything as a normal form. But when a site is docked to it, some properties and methods need special care:
210212
211213- AutoSize=true may result in an endless loop. Disable it or override DoAutoSize/CalculatePreferredSize and take special care.
212214- Constraints: The DockMaster clears the maximum Constraints and will restore them on undock.
213215- Child controls anchored to the aligned site may overlap. See [[Autosize / Layout|here]] how Align works.
214216- ChildSizing: The spacing properties like LeftRightSpacing, TopBottomSpacing may interfere with the docking layout.
215217
216218### Procedure to create all other forms by name
217219
218220The MainIDE sets the **DockMaster.OnCreateControl** event, to enable the DockMaster to create forms by name. This is needed to restore layouts.
219221
220222``` pascal
221223DockMaster.OnCreateControl:=@DockMasterCreateControl;
222224```
223225
224226``` pascal
225227procedure TMainIDE.DockMasterCreateControl(Sender: TObject; aName: string; var
226228 AControl: TControl; DoDisableAutoSizing: boolean);
227229
228230 procedure CreateForm(Caption: string; NewBounds: TRect);
229231 begin
230232 AControl:=CreateSimpleForm(aName,Caption,NewBounds,DoDisableAutoSizing);
231233 end;
232234
233235begin
234236 // first check if the form already exists
235237 // the LCL Screen has a list of all existing forms.
236238 // Note: Remember that the LCL allows as form names only standard
237239 // pascal identifiers and compares them case insensitive
238240 AControl:=Screen.FindForm(aName);
239241 if AControl<>nil then begin
240242 // if it already exists, just disable autosizing if requested
241243 if DoDisableAutoSizing then
242244 AControl.DisableAutoSizing;
243245 exit;
244246 end;
245247 // if the form does not yet exist, create it
246248 if aName='CodeExplorer' then
247249 CreateForm('Code Explorer',Bounds(700,230,100,250))
248250 ...
249251 else if aName='DebugOutput' then
250252 CreateForm('Debug Output',Bounds(400,400,350,150));
251253end;
252254```
253255
254256Creating a form can be as simple as this:
255257
256258``` pascal
257259MyForm:=TMyForm.Create(Self);
258260if DoDisableAutoSizing then
259261 MyForm.DisableAutoSizing;
260262```
261263
262264But of course you can put there all kind of initialization code.
263265
264266The *DisableAutoSizing* reduces flickering. Make sure your forms **Visible** property is set to **false**.
265267
266268Remember to call **DisableAutoSizing** for the form if the parameter **DoDisableAutoSizing** is set to *true*, because *Disable-* and *EnableAutosizing* calls must be balanced. If you miss the *DisableAutosizing*, the LCL will raise an exception later:
267269
268-`TControl.EnableAutoSizing SourceEditor1:TSimpleForm: missing DisableAutoSizing`
270+```
271+TControl.EnableAutoSizing SourceEditor1:TSimpleForm: missing DisableAutoSizing
272+```
269273
270274![](/wiki-images/8/8e/MissingDisableAutoSizing.png)
271275
272276If you call **DisableAutoSizing** too much, your forms will not appear and/or not resize properly as the LCL is waiting for the *EnableAutoSizing* call that never comes.
273277
274278### Showing forms
275279
276280When a form should be shown, you have probably used something like *'MyForm.Show*. Instead you should now use
277281
278282``` pascal
279283DockMaster.MakeDockable(MyForm);
280284```
281285
282286This will wrap MyForm in a dock site and show it. It is clever enough to figure out if the form is already wrapped in a dock site.
283287
284288If you set the **DockMaster.OnCreateControl** event you can use this:
285289
286290``` pascal
287291DockMaster.ShowControl('MyForm',true);
288292```
289293
290294### Docking manually, via code
291295
292296Use the procedure **DockMaster.ManualDock** to dock a site to or into another.
293297
294298``` pascal
295299procedure ManualDock(SrcSite, TargetSite: TAnchorDockHostSite; Align: TAlign; TargetControl: TControl = nil);
296300```
297301
298302- **SrcSite** is the site to be docked. If SrcSite was docked it will be undocked first.
299303- **TargetSite** is the site where SrcSite will be docked into or docked as neighbor.
300304- **TargetControl** specifies if docking as neighbor (=nil), as inside neighbor (=TargetSide) or in front of a page (=a TAnchorDockPage).
301305
302306### Undocking manually, via code
303307
304308Use the procedure **DockMaster.ManualFloat** to undock a site from its neighbors and create a flotaing top level form. The embedded control stays embedded into a TAnchorDockHostSite.
305309
306310``` pascal
307311procedure ManualFloat(AControl: TControl);
308312```
309313
310314You can give as parameter a TAnchorDockHostSite or a control embedded into a TAnchorDockHostSite (for example a form made dockable with MakeDockable). The site will be put at the same screen location.
311315
312316### Save layout
313317
314318The DockMaster allows to save the current layout to a TConfigStorage. The miniide example uses the xml version:
315319
316320``` pascal
317321procedure TMainIDE.SaveLayout(Filename: string);
318322var
319323 XMLConfig: TXMLConfigStorage;
320324begin
321325 try
322326 // create a new xml config file
323327 XMLConfig:=TXMLConfigStorage.Create(Filename,false);
324328 try
325329 // save the current layout of all forms
326330 DockMaster.SaveLayoutToConfig(XMLConfig);
327331 XMLConfig.WriteToDisk;
328332 finally
329333 XMLConfig.Free;
330334 end;
331335 except
332336 on E: Exception do begin
333337 MessageDlg('Error',
334338 'Error saving layout to file '+Filename+':'#13+E.Message,mtError,
335339 [mbCancel],0);
336340 end;
337341 end;
338342end;
339343```
340344
341345### Load layout
342346
343347``` pascal
344348procedure TMainIDE.LoadLayout(Filename: string);
345349var
346350 XMLConfig: TXMLConfigStorage;
347351begin
348352 try
349353 // load the xml config file
350354 XMLConfig:=TXMLConfigStorage.Create(Filename,True);
351355 try
352356 // restore the layout
353357 // this will close unneeded forms and call OnCreateControl for all needed
354358 DockMaster.LoadLayoutFromConfig(XMLConfig);
355359 finally
356360 XMLConfig.Free;
357361 end;
358362 except
359363 on E: Exception do begin
360364 MessageDlg('Error',
361365 'Error loading layout from file '+Filename+':'#13+E.Message,mtError,
362366 [mbCancel],0);
363367 end;
364368 end;
365369end;
366370```
367371
368372### Enlarge/Shrink
369373
370374The anchor docking manager can enlarge/shrink docked neighbors. This is done via the popup menu of the dock header or in code with the function **DockMaster.ManualEnlarge**:
371375
372376``` pascal
373377function ManualEnlarge(Site: TAnchorDockHostSite; Side: TAnchorKind; OnlyCheckIfPossible: boolean): boolean;
374378```
375379
376380Site is a child site, Side the side of Site to expand. If you only want to test if enlarge is possible set OnlyCheckIfPossible to true.
377381
378382### Enlarge one, shrink another
379383
380384Shrink neighbor *Object Inspector*, enlarge *Messages*: Right click on the header of *Messages* and click on **Enlarge right side**. Two splitters are resized.
381385
382386![](/wiki-images/1/15/Anchordocking_before_enlarge1.png) enlarging *Messages* to the right side: ![](/wiki-images/f/f8/Anchordocking_after_enlarge1.png)
383387
384388### Enlarge one, shrink many
385389
386390Shrink splitter at one side, enlarge both neighbor splitters, rotate the splitter behind, enlarge Control, shrink controls at rotate splitter. Right click on header of *Source Editor 1* then click on **Enlarge bottom side**.
387391
388392![](/wiki-images/c/cb/Anchordocking_before_enlarge2.png) becomes ![](/wiki-images/4/42/Anchordocking_after_enlarge2.png)
389393
390394### General docking options
391395
392396The AnchorDocking package provides a dialog to setup some of the properties of the DockMaster. The dialog is in the unit AnchorDockOptionsDlg and you can use it simply:
393397
394398``` pascal
395399uses ... AnchorDockOptionsDlg;
396400...
397401procedure TMainIDE.FormCreate(Sender: TObject);
398402...
399403begin
400404 ...
401405 DockMaster.OnShowOptions:=@ShowAnchorDockOptions;
402406 ...
403407end;
404408```
405409
406410This will add a new menu item to the popup menus of the dock headers and pages called *Docking options*.
407411
408412## Why use Anchors instead of Align?
409413
410414Anchors allow to create any possible rectangular layout. Align is limited. Align with Panels can theoretically create a lot of layouts, but still not all. And some operations like enlarge/shrink are more complicated using Align/Panels than using Anchors. Align works good for a few forms, but the more forms are docked, the more the disadvantages of Align become visible.
411415
412416### Layouts that can be created with Align
413417
414418Align can only create the following layouts:
415419
416420![](/wiki-images/1/13/Anchordocking_align_layout1.png)
417421
418422The alTop controls are always at the top, filling the whole horizontal width. That's because the LCL first aligns all controls with alTop, then all alBottom, then alLeft, then alRight and finally alClient.
419423
420424### Layouts that can be created with Align and Panels
421425
422426It is possible to nest Align layouts by using hidden panels. Then any layout that can be recursively splitted in halves can be created. Then you can create for example:
423427
424428![](/wiki-images/0/0e/Anchordocking_align_panels_layout1.png)
425429
426430This requires only one hidden panel.
427431
428432### Changing Layouts
429433
430434Now the user wants to enlarge the *FPDocEditor* horizontally (and shrink *CodeExplorer*). With the *AnchorDocking* you can simply right click on the *FPDocEditor* header and click **Enlarge right side**.
431435
432436![](/wiki-images/8/88/Anchordocking_align_panels_layout2.png)
433437
434438Other docking engines need at least 2 panels. One for *SrcEditor*,*Messages*,*FPDocEdit*,*CodeExpl* and one for *SrcEdit* and *Messages*. Most docking layouters do not even provide a simple way to change the layout in this way. An algorithm to allow this layout change, must analyze the whole structure as if there were no panels and must reparent a lot of things. Basically the algorithm must do the same as the anchor docking algorithm, but with the extra work of translating the layout into Align plus hidden panels.
435439
436440### Layouts that are impossible with Align and Panels
437441
438442Now the user wants to enlarge *SourceEditor1* horizontally (and shrink *ObjectInspector*):
439443
440444![](/wiki-images/e/e9/Anchordocking_impossible_with_align_panels1.png)
441445
442446This layout is impossible with Align and panels, because you can not cut it in two halves. The same with
443447
444448![](/wiki-images/1/1e/Anchordocking_impossible_with_align_panels2.png)
445449
446450### Conclusion
447451
448452Align with hidden panels allows you to easily create a simple docking manager that works well for a few forms, but it will always limit the user. Align is useful for row and column layouts, not for tables. Anchor docking works even for complex forms.
449453
450454## Old/deprecated anchor docking
451455
452456### Create a TLazDockingManager
453457
454458``` pascal
455459 DockingManager:=TLazDockingManager.Create(Self);
456460```
457461
458462The Self as parameter is only used as Owner. That means, when the mainform is freed, the DockingManager is freed too. You can use *nil* and free the DockingManager yourself.
459463
460464### Optional: Loading a configuration
461465
462466You can load the user configuration from disk.
463467
464468``` pascal
465469 Config:=TXMLConfigStorage.Create('config.xml',true);
466470 DockingManager.LoadFromConfig(Config);
467471 Config.Free;
468472```
469473
470474This loads the file *config.xml*. The config can be created by the SaveToConfig function. See below.
471475
472476### Make a form/control dockable
473477
474478Create a TLazControlDocker for each form/control that should be dockable
475479
476480``` pascal
477481 ControlDocker1:=TLazControlDocker.Create(Self);
478482 ControlDocker1.Name:='DockerForm1';
479483 ControlDocker1.Manager:=DockingManager;
480484```
481485
482486### Optional: Saving the user configuration to disk
483487
484488When the program is closed you can save the user configuration to disk
485489
486490``` pascal
487491 Config:=TXMLConfigStorage.Create('config.xml',true);
488492 DockingManager.SaveToConfig(Config);
489493 Config.WriteToDisk;
490494 Config.Free;
491495```
492496
493497## See also
494498
495499- [[AnchorDockingDsgn]] AnchorDocking in the Lazarus IDE
496500- [[Anchor Docking Step by step example|Anchor Docking: A Step by step example]]
497501- [[Docking]] in general
498502- [[EasyDockingManager]] - an alternative docking implementation with a simpler layout engine and fewer options.
499503
500504---
501505*From the Free Pascal wiki, page "Anchor Docking", as it stood on 2024-01-27 after a change by Rich2014, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
502506Category: Docking
503507Category: Lazarus IDE plugins

Back to history