Exemplo n.º 1
0
        /// <summary>
        /// Handle a toolbar button being dropped, either from the customisation panel onto the
        /// toolbar or from the toolbar itself.
        /// </summary>
        private void InternalHandleCustomDrop()
        {
            Point   screenDropLocation = Cursor.Position;
            Control form = TopLevelControl;

            if (form == null || !form.Bounds.Contains(screenDropLocation))
            {
                return;
            }
            Control dropControl = WindowFromPoint(form, form.PointToClient(screenDropLocation));

            if (dropControl != null)
            {
                // Change parent if we dropped on the search field's text box or icon.
                if (dropControl.Parent is CRSearchField)
                {
                    dropControl = dropControl.Parent;
                }

                // If we dropped on a toolbar button, separator control or the search field, switch to
                // the parent for consistency.
                if (dropControl is CRRoundButton || dropControl is PictureBox || dropControl is CRSearchField)
                {
                    dropControl = dropControl.Parent;
                }

                // Dropping on the toolbar at this point means the new button is being inserted.
                // Otherwise it is being deleted.
                Point dropLocation = dropControl.PointToClient(screenDropLocation);
                CRToolbarItemCollection collection = CRToolbarItemCollection.DefaultCollection;

                if (dropControl is CRToolbar)
                {
                    if (_dragSource != null)
                    {
                        collection.Remove(_dragSource);
                        Controls.Remove(_dragSource.Control);
                        Relayout();
                    }
                    else
                    {
                        CRToolbarItem button = MatchingItem(_draggedButton);
                        if (button != null && button.Type != CRToolbarItemType.Space && button.Type != CRToolbarItemType.FlexibleSpace)
                        {
                            collection.Remove(button);
                            Relayout();
                        }
                    }

                    // Find the index of where to insert the button.
                    int index = IndexOfItemWithPoint(dropLocation);

                    // Add to the collection at the index.
                    collection.Insert(index, _draggedButton);

                    // Reload the toolbar.
                    Load();
                    Update();
                }
                else if (_dragSource != null)
                {
                    collection.Remove(_dragSource);
                    Controls.Remove(_dragSource.Control);
                    Relayout();
                    Update();
                }
            }

            _draggedButton.Control.Capture = false;

            _hasDragItem   = false;
            _draggedButton = null;
        }