Sign in Join
Wiki copytemporary

Anchor Docking Step by step example: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# Anchor Docking : A step by step example
22
33> **Warning:** THIS EXAMPLE DOES NOT WORK 100% as intended''' see Debugging for more info.
44
55We have [[Anchor Docking|anchor docking]] now... but I know that humans learn by doing, rather than reading... so, lets do it.
66
77First, I'll be creating a new application from scratch, and then (hopefully) we can see if we can "convert" an existing application to it (one that worked on the old "Panel" align way).
88
99For the first project, I'll be creating an application that should look like this:
1010
11-` +-----------------------------+`\
12-` | Main menu                   |`\
13-` +-----------------------------+`\
14-` | Toolbar (open, save etc.)   |`\
15-` +-----+-----------------------+`\
16-` | __= |                       |`\
17-` |     |                       |`\
18-` |  T  |   Main Application    |`\
19-` |  o  |                       | `\
20-` |  o  |                       |`\
21-` |  l  +-----------------------+`\
22-` |  s  |   Output Messages     |`\
23-` +-----+-----------------------+`
11+```
12+ +-----------------------------+
13+ | Main menu |
14+ +-----------------------------+
15+ | Toolbar (open, save etc.) |
16+ +-----+-----------------------+
17+ | __= | |
18+ | | |
19+ | T | Main Application |
20+ | o | |
21+ | o | |
22+ | l +-----------------------+
23+ | s | Output Messages |
24+ +-----+-----------------------+
25+```
2426
2527With the Toolbar, Tools (multiple pages), Output Messages parts all being "dockable". Because "drag" and "drop" isn't implemented yet, I'm not going to try to add functionality to move the parts around. (Compiling the example shows how this is possible)... This project will only be able to say "docked" or "not docked" per each of those parts. (This project would be able to be done with only panels.)
2628
2729# Step 1: Create the main page
2830
2931In essence, we'll just be creating a new form, adding a main menu to the form, and then adding the dockmanager to the form. I also add a stringgrid just to show what it will look like.
3032
3133- Create a new project
3234- In Lazarus go to File -\> New ... -\> Select "Application"
3335- The first form (unit1.pas) will be the "Main Application" part.
3436 - Drop a TMainMenu component onto the form
3537 - Double click on the TMainMenu component, and create:
3638 - Main - File
3739 - FileSub - Exit
3840 - Main - Windows
3941 - WindowsSub - Dock/Undock Active Window (add a shortcut to F11 - so that we can have )
4042 - WindowsSub - Save Window Positions
4143 - Create an "OnCreate" event for the Form
4244 - Select the form in the form designer
4345 - In the object inspector, click the "events" tab, and double click the "OnCreate" event
4446 - Create the docking manager
4547 - Add this code to the OnCreate event: \`**DockingManager:=TLazDockingManager.Create(Self);**\`
4648 - Add the dockmanager variable to the main form's class.
4749 - Press Ctrl-Shift-Up (this will take you to the OnCreate event's definition in the form's class
4850 - Add this code to the Form's **public** class definition : \`**DockingManager : TLazDockingManager;**\`
4951 - Add the Docking unit and the XMLPropStorage to the uses section,
5052 - Scroll up to the uses section
5153 - Add \`**, LDockCtrl, XMLPropStorage**\` to the uses section
5254 - Now we'll just add stuff to make it look like an application
5355 - Add a StringGrid component
5456 - Make **Align = Client** in the object inspector
5557
5658# Step 2: Create our tool bar
5759
5860I'm not going to pretend that I know how to create a proper toolbar... ala excel or even open office's stuff. So I'll just create a form with speed buttons that are all left aligned, and add some icons to it. Also add the dock control to it.
5961
6062- Create a new form (File -\> New Form)
6163 - Resize it to look "realistic" in size. Mine's height is 61.
6264 - Add some load/save buttons to it (add some icons too, if you like) and **Align = Left** them. I added a Load and Save speedbuttons, got some glyphs from Lazarus/images/menu. In my other project I've added a new panel, and put in a combobox there. Do what you want!
6365 - Add the control docker
6466 - On the OnCreate event add:
6567
6668``` pascal
6769 ControlDocker1:=TLazControlDocker.Create(Self);
6870 ControlDocker1.Name:='ToolbarForm';
6971 ControlDocker1.Manager:=Form1.DockingManager;
7072```
7173
7274- - Add the appropriate units to the uses part. (**unit1, LDockCtrl**)
7375 - Add the variable to the Form's **public** class definition: **ControlDocker1 : TLazControlDocker;**
7476
7577# Step 3: Our tools pages
7678
7779We'll just create 2 different forms which are *identifiably* different from each other. And add the dock control to it. Do the following twice:
7880
7981- Create a new form (File -\> New Form)
8082 - Resize it to look "realistic" in size. (I just made it about 150 wide)
8183 - Identify it. (I put a label on, saying tool window 1 and 2)
8284 - Add the control docker
8385 - On the OnCreate event add:
8486
8587``` pascal
8688
8789 ControlDocker1:=TLazControlDocker.Create(Self);
8890 ControlDocker1.Name:='Toolwindow1Form'; // (or 2 for the second one)
8991 ControlDocker1.Manager:=Form1.DockingManager;
9092```
9193
9294- - Add the appropriate units to the uses part. (**unit1, LDockCtrl**)
9395 - Add the variable to the Form's **public** class definition: **ControlDocker1 : TLazControlDocker;**
9496
9597# Step 4: Our Message window
9698
9799Just our last form... The output form. Follow the same steps as before...
98100
99101- Create a new form (File -\> New Form)
100102 - Resize it to look "realistic" in size. (I just made it about 100 high, and quite wide)
101103 - Add a memo component, and **align = client**
102104 - Add the control docker
103105 - On the OnCreate event add:
104106
105107``` pascal
106108 ControlDocker1:=TLazControlDocker.Create(Self);
107109 ControlDocker1.Name:='OutputForm'; // (or 2 for the second one)
108110 ControlDocker1.Manager:=Form1.DockingManager;
109111```
110112
111113- - Add the appropriate units to the uses part. (**unit1, LDockCtrl**)
112114 - Add the variable to the Form's **public** class definition: **ControlDocker1 : TLazControlDocker;**
113115
114116# Step 5: Putting it all together
115117
116118Now we'll just add everything together on the mainform... and see how it comes out.
117119
118120- Add all the units to the uses section in your main unit. (**unit2, unit3, unit4, unit5**
119121- Next, we'll want to add it to the docking manager... but the problem is that the forms are not created yet at the "OnCreate" event for the main form. So, I'll move the "assigning" of the docking to the "OnActivate" event, and just set a flag that it's been done (so it doesn't do it again)
120122- So: Add an Initialized variable to form1's class: **Initialized : boolean;**
121123- At OnCreate we'll set it to "False", and then set it to true during the OnActivate Method.
122124- The current "OnActivate" method.
123125
124126``` pascal
125127procedure TForm1.FormActivate(Sender: TObject);
126128begin
127129 if initialized then exit;
128130 Initialized := true;
129131 // Sets the toolbaar to the top
130132 DockingManager.Manager.InsertControl(Form2,alTop,Form1);
131133
132134 // Set the tool window to the left
133135 DockingManager.Manager.InsertControl(Form3,alLeft,Form1);
134136
135137 // Set the second tool window IN the first
136138 DockingManager.Manager.InsertControl(Form4,alClient,Form3);
137139
138140 // Set the second Message Control to the bottom
139141 DockingManager.Manager.InsertControl(Form5,alBottom,Form1);
140142end;
141143```
142144
143145# Debugging
144146
145147This did not work 100% as expected.
146148
147149Stuff that worked as expected:
148150
1491511. The Toolbar looked correct and awesome
1501522. The Tool Windows' width was as expected
151153
152154Stuff that didn't work as expected:
153155
1541561. The Second tool window didn't work properly, there was a page control, but the second page wasn't assigned, and the first page had the SECOND tool window assigned
1551572. The Main Menu was missing
1561583. The Memo was gone... though Form5 was there (maybe not? the "title" didn't read "Form5")
1571594. Form5 took over all the space that was meant for the main application. Probably adding constraints to the main window would help.
158160
159161---
160162*From the Free Pascal wiki, page "Anchor Docking Step by step example", as it stood on 2019-12-30 after a change by Trev, 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).*
161163Category: Docking
162164Category: Lazarus
163165Category: Tutorials

Back to history