Пример #1
0
        public void DoOleDragDrop(DragEventArgs de)
        {
            // ASURT 43757: By the time we come here, it means that the user completed the drag-drop and
            // we compute the new location/size of the controls if needed and set the property values.
            // We have to stop freezePainting right here, so that controls can get a chance to validate
            // their new rects.
            //
            freezePainting = false;

            if (selectionHandler == null)
            {
                Debug.Fail("selectionHandler should not be null");
                de.Effect = DragDropEffects.None;
                return;
            }

            // make sure we've actually moved
            if ((localDrag && de.X == dragBase.X && de.Y == dragBase.Y) ||
                de.AllowedEffect == DragDropEffects.None ||
                (!localDrag && !dragOk))
            {
                de.Effect = DragDropEffects.None;
                return;
            }

            bool localMoveOnly = ((int)de.AllowedEffect & AllowLocalMoveOnly) != 0 && localDragInside;

            // if we are dragging inside the local dropsource/target, and and AllowLocalMoveOnly flag is set,
            // we just consider this a normal move.
            //
            bool moveAllowed = (de.AllowedEffect & DragDropEffects.Move) != DragDropEffects.None || localMoveOnly;
            bool copyAllowed = (de.AllowedEffect & DragDropEffects.Copy) != DragDropEffects.None;

            if ((de.Effect & DragDropEffects.Move) != 0 && !moveAllowed)
            {
                // Try copy instead?
                de.Effect = DragDropEffects.Copy;
            }

            // make sure the copy is allowed
            if ((de.Effect & DragDropEffects.Copy) != 0 && !copyAllowed)
            {
                // if copy isn't allowed, don't do anything

                de.Effect = DragDropEffects.None;
                return;
            }

            if (localMoveOnly && (de.Effect & DragDropEffects.Move) != 0)
            {
                de.Effect |= (DragDropEffects)AllowLocalMoveOnly | DragDropEffects.Move;
            }
            else if ((de.Effect & DragDropEffects.Copy) != 0)
            {
                de.Effect = DragDropEffects.Copy;
            }

            if (forceDrawFrames || localDragInside)
            {
                // undraw the drag rect
                localDragOffset = DrawDragFrames(dragComps, localDragOffset, localDragEffect,
                                                 Point.Empty, DragDropEffects.None, forceDrawFrames);
                forceDrawFrames = false;
            }

            Cursor oldCursor = Cursor.Current;

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (dragOk || (localDragInside && de.Effect == DragDropEffects.Copy))
                {
                    // add em to this parent.
                    IDesignerHost host      = (IDesignerHost)GetService(typeof(IDesignerHost));
                    IContainer    container = host.RootComponent.Site.Container;

                    object[]    components;
                    IDataObject dataObj        = de.Data;
                    bool        updateLocation = false;

                    if (dataObj is ComponentDataObjectWrapper)
                    {
                        dataObj = ((ComponentDataObjectWrapper)dataObj).InnerData;
                        ComponentDataObject cdo = (ComponentDataObject)dataObj;

                        // if we're moving ot a different container, do a full serialization
                        // to make sure we pick up design time props, etc.
                        //
                        IComponent dragOwner        = GetDragOwnerComponent(de.Data);
                        bool       newContainer     = dragOwner == null || client.Component == null || dragOwner.Site.Container != client.Component.Site.Container;
                        bool       collapseChildren = false;
                        if (de.Effect == DragDropEffects.Copy || newContainer)
                        {
                            // this causes new elements to be created
                            //
                            cdo.Deserialize(serviceProvider, (de.Effect & DragDropEffects.Copy) == 0);
                        }
                        else
                        {
                            collapseChildren = true;
                        }

                        updateLocation = true;
                        components     = cdo.Components;

                        if (collapseChildren)
                        {
                            components = GetTopLevelComponents(components);
                        }
                    }
                    else
                    {
                        object serializationData = dataObj.GetData(DataFormat, true);

                        if (serializationData == null)
                        {
                            Debug.Fail("data object didn't return any data, so how did we allow the drop?");
                            components = Array.Empty <IComponent>();
                        }
                        else
                        {
                            dataObj        = new ComponentDataObject(client, serviceProvider, serializationData);
                            components     = ((ComponentDataObject)dataObj).Components;
                            updateLocation = true;
                        }
                    }

                    // now we need to offset the components locations from the drop mouse
                    // point to the parent, since their current locations are relative
                    // the the mouse pointer
                    if (components != null && components.Length > 0)
                    {
                        Debug.Assert(container != null, "Didn't get a container from the site!");
                        string     name;
                        IComponent comp = null;

                        DesignerTransaction trans = null;

                        try
                        {
                            trans = host.CreateTransaction(SR.DragDropDropComponents);
                            if (!localDrag)
                            {
                                host.Activate();
                            }

                            ArrayList selectComps = new ArrayList();

                            for (int i = 0; i < components.Length; i++)
                            {
                                comp = components[i] as IComponent;

                                if (comp == null)
                                {
                                    comp = null;
                                    continue;
                                }

                                try
                                {
                                    name = null;
                                    if (comp.Site != null)
                                    {
                                        name = comp.Site.Name;
                                    }

                                    Control oldDesignerControl = null;
                                    if (updateLocation)
                                    {
                                        oldDesignerControl = client.GetDesignerControl();
                                        User32.SendMessageW(oldDesignerControl.Handle, User32.WM.SETREDRAW);
                                    }

                                    Point dropPt = client.GetDesignerControl().PointToClient(new Point(de.X, de.Y));

                                    // First check if the component we are dropping have a TrayLocation, and if so, use it
                                    PropertyDescriptor loc = TypeDescriptor.GetProperties(comp)["TrayLocation"];
                                    if (loc == null)
                                    {
                                        // it didn't, so let's check for the regular Location
                                        loc = TypeDescriptor.GetProperties(comp)["Location"];
                                    }

                                    if (loc != null && !loc.IsReadOnly)
                                    {
                                        Rectangle bounds = new Rectangle();
                                        Point     pt     = (Point)loc.GetValue(comp);
                                        bounds.X = dropPt.X + pt.X;
                                        bounds.Y = dropPt.Y + pt.Y;
                                        bounds   = selectionHandler.GetUpdatedRect(Rectangle.Empty, bounds, false);
                                    }

                                    if (!client.AddComponent(comp, name, false))
                                    {
                                        // this means that we just moved the control
                                        // around in the same designer.

                                        de.Effect = DragDropEffects.None;
                                    }
                                    else
                                    {
                                        // make sure the component was added to this client
                                        if (client.GetControlForComponent(comp) == null)
                                        {
                                            updateLocation = false;
                                        }
                                    }

                                    if (updateLocation)
                                    {
                                        ParentControlDesigner parentDesigner = client as ParentControlDesigner;
                                        if (parentDesigner != null)
                                        {
                                            Control c = client.GetControlForComponent(comp);
                                            dropPt     = parentDesigner.GetSnappedPoint(c.Location);
                                            c.Location = dropPt;
                                        }
                                    }

                                    if (oldDesignerControl != null)
                                    {
                                        //((ComponentDataObject)dataObj).ShowControls();
                                        User32.SendMessageW(oldDesignerControl.Handle, User32.WM.SETREDRAW, (IntPtr)1);
                                        oldDesignerControl.Invalidate(true);
                                    }

                                    if (TypeDescriptor.GetAttributes(comp).Contains(DesignTimeVisibleAttribute.Yes))
                                    {
                                        selectComps.Add(comp);
                                    }
                                }
                                catch (CheckoutException ceex)
                                {
                                    if (ceex == CheckoutException.Canceled)
                                    {
                                        break;
                                    }

                                    throw;
                                }
                            }

                            if (host != null)
                            {
                                host.Activate();
                            }

                            // select the newly added components
                            ISelectionService selService = (ISelectionService)GetService(typeof(ISelectionService));

                            selService.SetSelectedComponents((object[])selectComps.ToArray(typeof(IComponent)), SelectionTypes.Replace);
                            localDragInside = false;
                        }
                        finally
                        {
                            if (trans != null)
                            {
                                trans.Commit();
                            }
                        }
                    }
                }

                if (localDragInside)
                {
                    ISelectionUIService selectionUISvc = (ISelectionUIService)GetService(typeof(ISelectionUIService));
                    Debug.Assert(selectionUISvc != null, "Unable to get selection ui service when adding child control");

                    if (selectionUISvc != null)
                    {
                        // We must check to ensure that UI service is still in drag mode.  It is
                        // possible that the user hit escape, which will cancel drag mode.
                        //
                        if (selectionUISvc.Dragging && moveAllowed)
                        {
                            Rectangle offset = new Rectangle(de.X - dragBase.X, de.Y - dragBase.Y, 0, 0);
                            selectionUISvc.DragMoved(offset);
                        }
                    }
                }

                dragOk = false;
            }
            finally
            {
                Cursor.Current = oldCursor;
            }
        }
Пример #2
0
        /// <include file='doc\ControlCommandSet.uex' path='docs/doc[@for="ControlCommandSet.OnKeySize"]/*' />
        /// <devdoc>
        ///     Called for the various sizing commands we support.
        /// </devdoc>
        protected void OnKeySize(object sender, EventArgs e)
        {
            // Arrow keys.  Begin a drag if the selection isn't locked.
            //
            ISelectionService   selSvc = SelectionService;
            ISelectionUIService uiSvc  = SelectionUIService;

            if (uiSvc != null && selSvc != null)
            {
                //added to remove the selection rectangle: bug(54692)
                //
                uiSvc.Visible = false;
                object comp = selSvc.PrimarySelection;
                if (comp != null && comp is IComponent)
                {
                    PropertyDescriptor lockedProp = TypeDescriptor.GetProperties(comp)["Locked"];
                    if (lockedProp == null || (lockedProp.PropertyType == typeof(bool) && ((bool)lockedProp.GetValue(comp))) == false)
                    {
                        SelectionRules rules       = SelectionRules.Visible;
                        CommandID      cmd         = ((MenuCommand)sender).CommandID;
                        bool           invertSnap  = false;
                        int            moveOffsetX = 0;
                        int            moveOffsetY = 0;

                        if (cmd.Equals(MenuCommands.KeySizeHeightDecrease))
                        {
                            moveOffsetY = -1;
                            rules      |= SelectionRules.BottomSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeySizeHeightIncrease))
                        {
                            moveOffsetY = 1;
                            rules      |= SelectionRules.BottomSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeySizeWidthDecrease))
                        {
                            moveOffsetX = -1;
                            rules      |= SelectionRules.RightSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeySizeWidthIncrease))
                        {
                            moveOffsetX = 1;
                            rules      |= SelectionRules.RightSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeyNudgeHeightDecrease))
                        {
                            moveOffsetY = -1;
                            invertSnap  = true;
                            rules      |= SelectionRules.BottomSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeyNudgeHeightIncrease))
                        {
                            moveOffsetY = 1;
                            invertSnap  = true;
                            rules      |= SelectionRules.BottomSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeyNudgeWidthDecrease))
                        {
                            moveOffsetX = -1;
                            invertSnap  = true;
                            rules      |= SelectionRules.RightSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeyNudgeWidthIncrease))
                        {
                            moveOffsetX = 1;
                            invertSnap  = true;
                            rules      |= SelectionRules.RightSizeable;
                        }
                        else
                        {
                            Debug.Fail("Unknown command mapped to OnKeySize: " + cmd.ToString());
                        }

                        if (uiSvc.BeginDrag(rules, 0, 0))
                        {
                            bool               snapOn        = false;
                            Size               snapSize      = Size.Empty;
                            IComponent         snapComponent = null;
                            PropertyDescriptor snapProperty  = null;

                            // Gets the needed snap information
                            //
                            IDesignerHost       host  = (IDesignerHost)GetService(typeof(IDesignerHost));
                            DesignerTransaction trans = null;

                            try {
                                if (host != null)
                                {
                                    GetSnapInformation(host, (IComponent)comp, out snapSize, out snapComponent, out snapProperty);

                                    if (selSvc.SelectionCount > 1)
                                    {
                                        trans = host.CreateTransaction(SR.GetString(SR.DragDropSizeComponents, selSvc.SelectionCount));
                                    }
                                    else
                                    {
                                        trans = host.CreateTransaction(SR.GetString(SR.DragDropSizeComponent, ((IComponent)comp).Site.Name));
                                    }

                                    if (snapProperty != null)
                                    {
                                        snapOn = (bool)snapProperty.GetValue(snapComponent);

                                        if (invertSnap)
                                        {
                                            snapOn = !snapOn;
                                            snapProperty.SetValue(snapComponent, snapOn);
                                        }
                                    }
                                }

                                if (snapOn && !snapSize.IsEmpty)
                                {
                                    moveOffsetX *= snapSize.Width;
                                    moveOffsetY *= snapSize.Height;
                                }


                                // Now move the controls the correct # of pixels.
                                //
                                uiSvc.DragMoved(new Rectangle(0, 0, moveOffsetX, moveOffsetY));
                                uiSvc.EndDrag(false);

                                if (host != null)
                                {
                                    if (invertSnap && snapProperty != null)
                                    {
                                        snapOn = !snapOn;
                                        snapProperty.SetValue(snapComponent, snapOn);
                                    }
                                }
                            }
                            finally {
                                if (trans != null)
                                {
                                    trans.Commit();
                                    uiSvc.Visible = true;
                                }
                            }
                        }
                    }
                }

                uiSvc.Visible = true;
            }
        }