Sign in Join
Wiki copytemporary

Anchor Docking Step by step example: what changed

Revision 2 to 3. 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-```
11+```text
1212 +-----------------------------+
1313 | Main menu |
1414 +-----------------------------+
1515 | Toolbar (open, save etc.) |
1616 +-----+-----------------------+
1717 | __= | |
1818 | | |
1919 | T | Main Application |
2020 | o | |
2121 | o | |
2222 | l +-----------------------+
2323 | s | Output Messages |
2424 +-----+-----------------------+
2525```
2626
2727With 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.)
2828
2929# Step 1: Create the main page
3030
3131In 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.
3232
3333- Create a new project
3434- In Lazarus go to File -\> New ... -\> Select "Application"
3535- The first form (unit1.pas) will be the "Main Application" part.
3636 - Drop a TMainMenu component onto the form
3737 - Double click on the TMainMenu component, and create:
3838 - Main - File
3939 - FileSub - Exit
4040 - Main - Windows
4141 - WindowsSub - Dock/Undock Active Window (add a shortcut to F11 - so that we can have )
4242 - WindowsSub - Save Window Positions
4343 - Create an "OnCreate" event for the Form
4444 - Select the form in the form designer
4545 - In the object inspector, click the "events" tab, and double click the "OnCreate" event
4646 - Create the docking manager
4747 - Add this code to the OnCreate event: \`**DockingManager:=TLazDockingManager.Create(Self);**\`
4848 - Add the dockmanager variable to the main form's class.
4949 - Press Ctrl-Shift-Up (this will take you to the OnCreate event's definition in the form's class
5050 - Add this code to the Form's **public** class definition : \`**DockingManager : TLazDockingManager;**\`
5151 - Add the Docking unit and the XMLPropStorage to the uses section,
5252 - Scroll up to the uses section
5353 - Add \`**, LDockCtrl, XMLPropStorage**\` to the uses section
5454 - Now we'll just add stuff to make it look like an application
5555 - Add a StringGrid component
5656 - Make **Align = Client** in the object inspector
5757
5858# Step 2: Create our tool bar
5959
6060I'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.
6161
6262- Create a new form (File -\> New Form)
6363 - Resize it to look "realistic" in size. Mine's height is 61.
6464 - 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!
6565 - Add the control docker
6666 - On the OnCreate event add:
6767
6868``` pascal
6969 ControlDocker1:=TLazControlDocker.Create(Self);
7070 ControlDocker1.Name:='ToolbarForm';
7171 ControlDocker1.Manager:=Form1.DockingManager;
7272```
7373
7474- - Add the appropriate units to the uses part. (**unit1, LDockCtrl**)
7575 - Add the variable to the Form's **public** class definition: **ControlDocker1 : TLazControlDocker;**
7676
7777# Step 3: Our tools pages
7878
7979We'll just create 2 different forms which are *identifiably* different from each other. And add the dock control to it. Do the following twice:
8080
8181- Create a new form (File -\> New Form)
8282 - Resize it to look "realistic" in size. (I just made it about 150 wide)
8383 - Identify it. (I put a label on, saying tool window 1 and 2)
8484 - Add the control docker
8585 - On the OnCreate event add:
8686
8787``` pascal
8888
8989 ControlDocker1:=TLazControlDocker.Create(Self);
9090 ControlDocker1.Name:='Toolwindow1Form'; // (or 2 for the second one)
9191 ControlDocker1.Manager:=Form1.DockingManager;
9292```
9393
9494- - Add the appropriate units to the uses part. (**unit1, LDockCtrl**)
9595 - Add the variable to the Form's **public** class definition: **ControlDocker1 : TLazControlDocker;**
9696
9797# Step 4: Our Message window
9898
9999Just our last form... The output form. Follow the same steps as before...
100100
101101- Create a new form (File -\> New Form)
102102 - Resize it to look "realistic" in size. (I just made it about 100 high, and quite wide)
103103 - Add a memo component, and **align = client**
104104 - Add the control docker
105105 - On the OnCreate event add:
106106
107107``` pascal
108108 ControlDocker1:=TLazControlDocker.Create(Self);
109109 ControlDocker1.Name:='OutputForm'; // (or 2 for the second one)
110110 ControlDocker1.Manager:=Form1.DockingManager;
111111```
112112
113113- - Add the appropriate units to the uses part. (**unit1, LDockCtrl**)
114114 - Add the variable to the Form's **public** class definition: **ControlDocker1 : TLazControlDocker;**
115115
116116# Step 5: Putting it all together
117117
118118Now we'll just add everything together on the mainform... and see how it comes out.
119119
120120- Add all the units to the uses section in your main unit. (**unit2, unit3, unit4, unit5**
121121- 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)
122122- So: Add an Initialized variable to form1's class: **Initialized : boolean;**
123123- At OnCreate we'll set it to "False", and then set it to true during the OnActivate Method.
124124- The current "OnActivate" method.
125125
126126``` pascal
127127procedure TForm1.FormActivate(Sender: TObject);
128128begin
129129 if initialized then exit;
130130 Initialized := true;
131131 // Sets the toolbaar to the top
132132 DockingManager.Manager.InsertControl(Form2,alTop,Form1);
133133
134134 // Set the tool window to the left
135135 DockingManager.Manager.InsertControl(Form3,alLeft,Form1);
136136
137137 // Set the second tool window IN the first
138138 DockingManager.Manager.InsertControl(Form4,alClient,Form3);
139139
140140 // Set the second Message Control to the bottom
141141 DockingManager.Manager.InsertControl(Form5,alBottom,Form1);
142142end;
143143```
144144
145145# Debugging
146146
147147This did not work 100% as expected.
148148
149149Stuff that worked as expected:
150150
1511511. The Toolbar looked correct and awesome
1521522. The Tool Windows' width was as expected
153153
154154Stuff that didn't work as expected:
155155
1561561. 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
1571572. The Main Menu was missing
1581583. The Memo was gone... though Form5 was there (maybe not? the "title" didn't read "Form5")
1591594. Form5 took over all the space that was meant for the main application. Probably adding constraints to the main window would help.
160160
161161---
162162*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).*
163163Category: Docking
164164Category: Lazarus
165165Category: Tutorials

Back to history