Example #1
0
        private void OnItemDragDrop(object sender, DragEventArgs e)
        {
            DragDropObject data           = new DragDropObject(e.Data);
            DragDropOption action         = DragDropOption.None;
            DragDropOption allowedActions = ConvertEnum.GetDragDropAction(e.AllowedEffect);
            ModifierFlags  modifiers      = ConvertEnum.GetModifierFlags(e);
            Point          clientPoint    = _listView.PointToClient(new Point(e.X, e.Y));
            IGalleryItem   targetItem;
            bool           skipNearestItem = false;

            if (_component.AllowsDropOnItem)
            {
                targetItem = GetTargetItemAt(clientPoint, false);
                if (targetItem != null)
                {
                    action          = _component.PerformDrop(data, targetItem, allowedActions, modifiers);
                    skipNearestItem = true;
                    if (action != DragDropOption.None)
                    {
                        DrawInsertionMark(_gallery.IndexOf(targetItem), true, clientPoint);
                    }
                }
            }
            if (_component.AllowsDropAtIndex && action == DragDropOption.None)
            {
                int targetIndex = GetNearestTargetIndexAt(clientPoint);
                if (targetIndex >= 0)
                {
                    action = _component.PerformDrop(data, targetIndex, allowedActions, modifiers);
                    if (action != DragDropOption.None)
                    {
                        DrawInsertionMark(targetIndex, false, clientPoint);
                    }
                }
            }
            if (!skipNearestItem && _component.AllowsDropOnItem && action == DragDropOption.None)
            {
                targetItem = GetTargetItemAt(clientPoint, true);
                if (targetItem != null)
                {
                    action = _component.PerformDrop(data, targetItem, allowedActions, modifiers);
                    if (action != DragDropOption.None)
                    {
                        DrawInsertionMark(_gallery.IndexOf(targetItem), true, clientPoint);
                    }
                }
            }

            e.Effect = ConvertEnum.GetDragDropEffects(action);

            DrawInsertionMark(-1, false, Point.Empty);
        }
Example #2
0
        private void OnItemDrag(object sender, ItemDragEventArgs e)
        {
            List <IGalleryItem> list                = new List <IGalleryItem>();
            List <object>       objectList          = new List <object>();
            ListViewItem        specificLvi         = (ListViewItem)e.Item;
            IGalleryItem        specificDraggedItem = (IGalleryItem)specificLvi.Tag;

            // save selection and focus
            IList  savedSelection = new List <IGalleryItem>();
            object savedFocus     = _listView.FocusedItem.Tag;

            if (_listView.SelectedItems.Count > 0)
            {
                foreach (ListViewItem lvi in _listView.SelectedItems)
                {
                    IGalleryItem item = (IGalleryItem)lvi.Tag;
                    list.Add(item);
                    objectList.Add(item.Item);
                    savedSelection.Add(item);
                }
            }
            else
            {
                list.Add(specificDraggedItem);
                objectList.Add(specificDraggedItem.Item);
            }

            _selectionEventsEnabled = false;

            IList <IGalleryItem> draggedItems   = list.AsReadOnly();
            DragDropOption       allowedActions = _component.BeginDrag(draggedItems);
            DragDropOption       actualAction   = DragDropOption.None;

            if (allowedActions != DragDropOption.None)
            {
                DataObject data = new DataObject();
                data.SetData(draggedItems);                 // for GalleryComponent consumers, provide a list of the items that were dragged
                data.SetData(objectList.ToArray());         // for foreign consumers, provide an array of the objects wrapped by the dragged/selected items
                data.SetData(specificDraggedItem);          // for foreign consumers, provide the actual item that was dragged, not the selected ones

                DragDropEffects allowedEffects = ConvertEnum.GetDragDropEffects(allowedActions);
                DragDropEffects actualEffect   = _listView.DoDragDrop(data, allowedEffects);
                actualAction = ConvertEnum.GetDragDropAction(actualEffect);
            }
            _component.EndDrag(draggedItems, actualAction);

            // restore selection and focus
            _listView.SelectedIndices.Clear();
            for (int n = 0; n < _listView.Items.Count; n++)
            {
                ListViewItem lvi = _listView.Items[n];
                if (savedSelection.Contains(lvi.Tag))
                {
                    _listView.SelectedIndices.Add(n);
                    savedSelection.Remove(lvi.Tag);
                }

                if (lvi.Tag == savedFocus)
                {
                    lvi.Focused = true;
                }
            }

            _selectionEventsEnabled = true;

            // if any previously-selected items are left in the list, then the selection has changed overall and we need to update the component selection
            if (savedSelection.Count > 0)
            {
                OnSelectionChanged(null, null);
            }
        }