Internal and External Drag & Drop Operations
- 4 minutes to read
The NavBar control provides functionality to support internal and external drag & drop operations.
Internal drag & drop operations relate to dragging an individual link/group and dropping it onto a new position within the same NavBar control.
External operations imply:
dragging an object from another control (source control) and dropping it onto the NavBar as a new link or group;
dragging a link or group from the NavBar to another control (target control) and accepting it there as the control’s new child object.
You can control the NavBar control’s behavior for drag & drop operations by using the OptionsBehavior.Common.DragDropFlags property. This property contains a set of flags specifying which elements in the NavBar control can be dragged and/or dropped. For instance, you may not wish to allow dragging groups from/within the NavBar control and dropping links onto it. The following code line will do that.
DragDropFlags := DragDropFlags - [fAllowDragGroup, fAllowDropLink];
Note that holding down the CTRL key during the drag & drop operation results in duplicating the dragged link. In this case, a link being dragged is copied from the source to the target position. If the CTRL key is not pressed, the dragged link is just moved to the target position.
Implementation of drag & drop operations applied to a NavBar control is entirely based on use of the NavBar control’s drag object (TdxNavBarDragObject type). This object contains properties for handling internal and external drag & drop operations. Reading these properties, you can gain access to the group or link being dragged and obtain the target position of the dragged object should it be dropped. This object can be accessed via the dxNavBarDragObject global variable.
An instance of the TdxNavBarDragObject object is created and initialized automatically each time a dragging operation is initiated by the NavBar control (for instance, when a user starts dragging a NavBar link or group). In this case, for external drag & drop operations you should analyze property values of the existent NavBar control’s drag object and implement the accept logic handling the drag-over and drag-and-drop events of the target control. The following code presents handler implementations for the OnDragOver and OnDragDrop events of a list view control (ListView1) which can accept links dragged from a NavBar control. Note that, in this example, the NavBar control and list view controls use the same image list control as the source of their images.
procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
Accept := (dxNavBarDragObject <> nil);
end;
procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var AListItem: TListItem;
begin
if dxNavBarDragObject <> nil then
begin
if dxNavBarDragObject.SourceLink <> nil then
begin
AListItem := ListView1.Items.Add;
AListItem.Caption := dxNavBarDragObject.SourceLink.Item.Caption;
AListItem.ImageIndex := dxNavBarDragObject.SourceLink.Item.SmallImageIndex;
end;
end;
end;
However, if another control is the source of drag & drop operations (external drag & drop), you should instantiate the TdxNavBarDragObject object manually to ensure the proper response of the NavBar control involved in the operation. For this purpose, you can provide a handler for the source control’s start-dragging event and place the required initialization code there. Example code is shown below for such a handler for the OnStartDrag event of a list view control (in this example, the NavBar control and list view controls also use the same image list control as the source of their images).
procedure TForm1.ListView1StartDrag(Sender: TObject; var DragObject: TDragObject);
var AItem : TdxNavBarItem;
begin
if ListView1.Selected <> nil then
begin
AItem := TdxNavBarItem.Create(dxNavBar1);
AItem.Caption := ListView1.Selected.Caption;
AItem.SmallImageIndex := ListView1.Selected.ImageIndex;
dxNavBarDragObject := TdxNavBarDragObject.Create(dxNavBar1, DragObject, nil, nil, AItem);
end;
end;
The TdxNavBarDragObject object instance only exists during the drag & drop operation. In both cases, this object is automatically destroyed after the drag & drop operation is completed (either by accepting a dragged object or canceling it).
We can summarize the handling of NavBar control’s drag & drop operations as follows:
External drag & drop operations (where the NavBar control can represent either a source or target control) can be processed by manually handling specific start-dragging, drag-over, drag-and-drop and end-dragging events of the controls involved in the operation.
Internal drag & drop operations performed within the same NavBar control are handled internally. The NavBar object will create an instance of the TdxNavBarDragObject object and implement the required logic for dragging and dropping links/groups automatically. Thus, normally you do not need to make use of the NavBar control’s drag object and its properties to for standard internal operation.
For more information about processing NavBar control’s drag & drop operations see the Drag and Drop demo included in the installation.