Sign in Join
Wiki copytemporary

LCL Drag Drop: what changed

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

OldNewLine
1-<h2>
2-
31DoDi's Guide to Dragging, Dropping and Docking
4-
5-</h2>
62
73Controls in a GUI can be grabbed with the mouse, dragged to other locations, and dropped onto other controls or onto the desktop. When the dragged control is moved to a different location, the operation is called [[LCL Drag Dock|"docking"]]. Otherwise the source and target control can exchange information or interact in any other way, what's called a "drag-drop" operation.
84
95But all that does not work by itself, an application must set up the allowed operations so that the LCL knows what to do with every control in a GUI. Sophisticated operations require further code for managing the visual feedback to the application user, and what will happen when the dragged control finally is dropped.
106
117For simple docking support in entire applications you can use either [[EasyDockingManager]] or [[Anchor Docking]], that reduces the required adaptations to a minimum.
128
139## Drag Drop
1410
1511This is the simple and originally only dragging action. The players are:
1612
1713- A draggable source control in the GUI
1814- A drag object, derived from TDragObject
1915- Helper functions
2016- Events
2117
2218Now let's have a look at how these are working together.
2319
2420### The Drag Source
2521
2622A control can be made draggable by setting its **DragMode** property to **dmAutomatic**. When the user presses the left mouse button over such a control, the mouse pointer changes into an dragging indicator until the user releases the button, or cancels the operation by pressing the Escape key. The indicator now follows the mouse movements and changes its shape, according to what will happen when the control is dropped in the current location.
2723
2824Dragging also can be started in code, by calling *SourceControl*.**BeginDrag**. In either case the helper procedure *DragManager.DragStart* is called to start dragging. The **DragManager** is a global instance implementing the actual dragging. It is initialized with the default *TDragManagerDefault* and can be replaced by the programmer (You). Depending on the control's **DragKind** a drag or a dock operation is started. The source control triggers an **OnStartDrag** event, where the programmer can provide a custom drag object. If no such object is provided, a default object (*TDragControlObject* or *TDragDockObject*) is created. Internal data structures for visual feedback and more are initialized.
2925
3026### The Drag Object (TDragObject defined in unit controls)
3127
3228A drag object receives all user input while dragging. In the default implementation it reacts by calling either DragMove or finally DragStop.
3329
3430The drag object also is responsible for the visual feedback:
3531
3632- **GetDragCursor** returns the mouse pointer shape.
3733- **GetDragImages** returns a list of drag images, attachable to the mouse pointer.
3834- **ShowDragImage** and **HideDragImage** implement the visual feedback.
3935
4036The **EndDrag** method notifies the source control when dragging stops. In case of a cancel'd operation it invokes *SourceControl*.**DragCanceled**, followed by *SourceControl*.**DoEndDrag** in either case, which raises an **OnEndDrag** event.
4137
4238### Events
4339
4440#### OnStartDrag/Dock
4541
4642This event occurs when the operation starts. The handler can provide a custom TDragObject or TDragDockObject for the operation.
4743
4844#### OnDrag/DockOver
4945
5046This event notifies a possible target, when an object is dragged over it. The handler can signal to accept or reject a drop.
5147
5248The event signals not only mouse moves, but also when the mouse enters or leaves a target control.
5349
5450An OnDockOver event occurs on the DockSite, not on the target control under the mouse. The handler can signal to deny an drop, and can adjust the visual feedback. The event occurs when the mouse enters or leaves a control, and with every single movement of the mouse.
5551
5652#### OnDrag/DockDrop
5753
5854This event notifies the target control of the end of a drag operation.
5955
6056#### OnEndDrag/Dock
6157
6258This event notifies the source control of the end of a drag operation.
6359
6460## Major Shortcomings
6561
6662- The same flaws as Delphi's VCL.
6763- No LCL framework (cross-platform) support for dragging from an LCL application to any other application. For this to work, LCL needs support for the XDND protocol under X11, OLE Drag-n-Drop support under Windows and Mac Drag Manager under macOS.
6864- No LCL framework (cross-platform) support for receiving drops inside an LCL application from another application (desktop, file manager, etc). The OnDragDrop event handler always expects the Source of the drop to come through as a TObject descendant which will **not** be the case when dragging an object from another application to a LCL application.
6965
7066## Delphi
7167
7268### Drag Drop
7369
7470This is the simple and originally only dragging action. The players are:
7571
7672- A draggable source control in the GUI
7773- A drag object, derived from TDragObject
7874- Helper functions
7975- Events
8076
8177Now let's have a look at how these are working together, based on the original Delphi implementation for clarity. The LCL implementation is somewhat different in the use of helper objects, details will be discussed later.
8278
8379#### The Drag Source
8480
8581A control can be made draggable by setting its **DragMode** property to **dmAutomatic**. When the user presses the left mouse button over such a control, the mouse pointer changes into an dragging indicator until the user releases the button, or cancels the operation by pressing the Escape key. The indicator now follows the mouse movements and changes its shape, according to what will happen when the control is dropped in the current location.
8682
8783Dragging also can be started in code, by calling source.BeginDrag. In either case the helper procedure DragInitControl is called to start dragging. The source control raises an StartDrag event, where the application can provide a custom drag object. If no such object is provided, a default object is created. Internal data structures for visual feedback and more are initialized. Then DragTo is called to show that dragging has started.
8884
8985#### The Drag Object (TDragObject defined in unit controls)
9086
9187A drag object receives all user input while dragging. In the default implementation it reacts by calling either DragTo or finally DragDone.
9288
9389The drag object also is responsible for the visual feedback:
9490
9591- GetDragCursor returns the mouse pointer shape.
9692- GetDragImages returns a list of drag images, to be attached to the mouse pointer.
9793- ShowDragImage and HideDragImage implement the visual feedback.
9894
9995The Finished method notifies the source control when dragging stops. In case of a cancel'd operation it invokes source.DragCanceled, followed by source.DoEndDrag in either case, which raises an EndDrag event.
10096
10197#### LCL Modifications
10298
10399The current LCL implementation has removed input processing from the drag object, so that the application can no more customize the UI. And the LCL implementation is far from being okay :-(
104100
105101Also the drag object is bypassed in the presentation of the visual feedback.
106102
107103### Helper Functions
108104
109105Helper functions primarily allow for access to protected properties and methods of the controls and other classes, from outside the Controls unit. They also can perform common tasks, and can encapsulate platform specific and other implementation details.
110106
111107#### General Helpers
112108
113109**DragTo** (Delphi only, not present in LCL) determines the current drag target, sends notifications to the involved controls, and manages the visual feedback. It also handles the delayed start of a dragging operation, i.e. can wait for the mouse moving away far enough from the starting point.
114110
115111**DragDone** (Delphi only, not present in LCL) terminates dragging, either with a drop or cancel of the operation.
116112
117113**CancelDrag** (Delphi only, do not confuse with the LCL global procedure CancelDrag) is protected against false calls, otherwise it defers further actions to DragDone.
118114
119115#### Special Helpers
120116
121117Some more public helper procedures exist, which are undocumented in Delphi and are not required for customized dragging operations.
122118
123119**DragFindWindow** searches for a drag target window (platform specific).
124120
125121**DragInit** initializes the internal management, like mouse capture, and the visual feedback.
126122
127123**DragInitControl** creates the drag object, then calls DragInit.
128124
129125**SetCaptureControl** handles the mouse capture.
130126
131127### Events
132128
133129#### OnStartDrag/Dock
134130
135131This event occurs when the operation starts. The handler can provide a custom DockObject for the operation.
136132
137133#### OnDrag/DockOver
138134
139135This event notifies a possible target, when an object is dragged over it. The handler can signal to accept or reject an drop.
140136
141137The event signals not only mouse moves, but also when the mouse enters or leaves a target control.
142138
143139An OnDockOver event occurs on the DockSite, not on the target control under the mouse. The handler can signal to deny an drop, and can adjust the visual feedback. The event occurs when the mouse enters or leaves a control, and with every single movement of the mouse.
144140
145141#### OnDrag/DockDrop
146142
147143This event notifies the target control of the end of a drag operation.
148144
149145#### OnEndDrag/Dock
150146
151147This event notifies the source control of the end of a drag operation.
152148
153149### Docking specific Events
154150
155151(this section should be moved to docking topic)
156152
157153#### OnGetSiteInfo
158154
159155This event occurs during the search for a qualifying dock site. Multiple dock sites can overlap each other, reside in overlapping windows, or can be invisible. Every candidate must supply an influence rectangle (catching area), and can signal to reject an drop. The drag manager selects the most appropriate dock site as the target for subsequent OnDockOver events.
160156
161157#### OnUnDock
162158
163159This event occurs when the dragged control has to be removed from its host dock site.
164160
165161It's unclear what should happen when the handler denies to undock the control.
166162
167163## LCL Implementation
168164
169165The Delphi implementation of dragging has many flaws, in detail with regards to docking. Until now nobody seems to understand the details of the Delphi dragging model, most code was modeled after the VCL and consequently implements exactly the same misbehaviour. This section, and more important the docking specific page, shall shed some light on the dragging model in general and the severe design and implementation flaws in the Delphi implementation.
170166
171167The current LCL implementation uses a DragManager object and drag/dock performers instead of helper functions. While the Delphi model must be provided for compatibility reasons, the introduction of a DragManager class and object (singleton) allows for the implementation of an alternative drag manager, with improved behaviour, and allows an application to select the most appropriate manager.
172168
173169### DragManager
174170
175171This object handles the captured input, provides helper methods, and stores dragging and docking parameters.
176172
177173It's questionable whether the drag object shall not be allowed to handle user input. It makes the system safer, but disallows applications to extend the user interface for specific dragging operations.
178174
179175The helper methods require different calls, but this is harmless as long as they are only called from LCL components.
180176
181177More critical is the storage of dragging parameters in the DragManager object, which vanish when a different drag manager is installed. This is fatal in case of the list of registered dock sites :-(
182178
183179### Drag Performers
184180
185181The internal use of different classes for dragging and docking is okay in so far, as the procedures differ significantly between drag-drop and drag-dock operations. Nonetheless common actions should use common code, e.g. inherited from the common base class.
186182
187183## Delphi Message Sequence Chart or UML Sequence Diagram
188184
189185Provided by Tom Verhoeff
190186
191187Something along the lines (this is for Delphi; view with monospaced font):
192188
193189`                  Main            Drag          (Optional)        Drag`\
194190`  User            Event          Source           Drag           Target`\
195191` (Mouse)          Loop           Control          Object         Control`\
196192`    =               =               =                               =`\
197193`    |               |               |                               |`\
198194`   1|--MouseDown--->|               |                               |`\
199195`    |              2#--OnMouseDown->|                               |`\
200196`    |               |              3#                               |`\
201197`    |              4#<-BeginDrag()--#                               |`\
202198`    |               #               #                               |`\
203199`    |               |               |                               |`\
204200`    |              5#--OnStartDrag->|                               |`\
205201`    |               #              6#----Create---->=               |`\
206202`    |               |               #               |               |`\
207203`   7|--MouseMove--->|               |               |               |`\
208204`    |              8#-----------------------------------OnDragOver->|`\
209205`    |               #               |               |              9#`\
210206`    |               #               |               |               #`\
211207`    |               |               |               |               |`\
212208`  10|---MouseUp---->|               |               |               |`\
213209`    |             11#-----------------------------------OnDragDrop->|`\
214210`    |               #               |               |             12#`\
215211`    |               #               |               |               #`\
216212`    |               #---OnEndDrag-->|               |               |`\
217213`    |               |             13#----Destroy--->=               |`\
218214`    |               |               |                               |`
219215
220216The DragObject is optional. There could be multiple candidate targets. The Main Event Loop determines which event to send to which object, depending on the user action, the cursor location, and the current state of the GUI.
221217
2222184 somehow triggers 5 (there is a delay depending on the Boolean parameter Immediate to BeginDrag).
223219
2242206 determines the location of the MouseDown and may use this to decide on what gets dragged (through GetCursorPos, ScreenToClient, MouseToCell). It can call SetDragImage to visualize a thing being dragged.
225221
226222The chain 7, 8, 9 can occur repeatedly; 9 indicates acceptability of a drop at this target via the Boolean var parameter Accepted.
227223
22822412 is called only if the immediately preceding OnDragOver of this target returned Accepted = True; i.e. Accepted is a precondition of 12.
229225
23022613 receives the parameter Target to know where the drop was accepted; if Target = nil, then the drop was cancelled by the user (MouseUp at an unacceptable location), and OnDragDrop was not performed; if Target \<\> nil, then OnDragDrop was already performed at the Target. The work associated with an actual drop is thus divided over the OnDragDrop (for actions at the target) and OnEndDrag (for actions at the source). Since a Drag Object needs to be destroyed, independent of whether the drop was successful or canceled, it is best to call Free (or Destroy) only in the OnEndDrag handler.
231227
232228Only one drag can be active at any moment; it either ends in a drop or is canceled.
233229
234230There are various other things to be included, but this is the fundamental part. (Maybe also see [Delphi-Central](http://www.delphi-central.com/drag.aspx) or ["Implementing drag-and-drop in controls"](http://www.podgoretsky.com/ftp/Docs/Delphi/D5/dg/controls.html).)
235231
236232I hope this is helpful. Tom
237233
238234## See also
239235
240236- [[Drag and Drop sample]]
241237
242238---
243239*From the Free Pascal wiki, page "LCL Drag Drop", as it stood on 2023-12-13 after a change by Coz, 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).*

Back to history