Beispiel #1
0
        /// <summary>
        /// Called by the ParentControlDesigner when creating a new control - this will update the new control's bounds with the proper toolbox/snapline information that has been stored off
        /// isMirrored - Is the ParentControlDesigner mirrored? If so, we need to offset for that. This is because all snapline stuff is done using a LTR coordinate system
        /// </summary>
        public static Rectangle GetBoundsFromToolboxSnapDragDropInfo(ToolboxSnapDragDropEventArgs e, Rectangle originalBounds, bool isMirrored)
        {
            Rectangle newBounds = originalBounds;

            // this should always be the case 'cause we don't create 'e' unless we have an offset
            if (e.Offset != Point.Empty)
            {
                //snap either up or down depending on offset
                if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Top) != 0)
                {
                    newBounds.Y += e.Offset.Y;//snap to top - so move up our bounds
                }
                else if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Bottom) != 0)
                {
                    newBounds.Y = originalBounds.Y - originalBounds.Height + e.Offset.Y;
                }

                //snap either left or right depending on offset
                if (!isMirrored)
                {
                    if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Left) != 0)
                    {
                        newBounds.X += e.Offset.X;//snap to left-
                    }
                    else if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Right) != 0)
                    {
                        newBounds.X = originalBounds.X - originalBounds.Width + e.Offset.X;
                    }
                }
                else
                {
                    // ParentControlDesigner is RTL, that means that the origin is upper-right, not upper-left
                    if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Left) != 0)
                    {
                        // e.Offset.X is negative when we snap to left
                        newBounds.X = originalBounds.X - originalBounds.Width - e.Offset.X;
                    }
                    else if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Right) != 0)
                    {
                        // e.Offset.X is positive when we snao to right
                        newBounds.X -= e.Offset.X;
                    }
                }
            }
            return(newBounds);
        }
Beispiel #2
0
        public static Rectangle GetBoundsFromToolboxSnapDragDropInfo(ToolboxSnapDragDropEventArgs e, Rectangle originalBounds, bool isMirrored)
        {
            Rectangle rectangle = originalBounds;

            if (e.Offset != Point.Empty)
            {
                if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Top) != ToolboxSnapDragDropEventArgs.SnapDirection.None)
                {
                    rectangle.Y += e.Offset.Y;
                }
                else if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Bottom) != ToolboxSnapDragDropEventArgs.SnapDirection.None)
                {
                    rectangle.Y = (originalBounds.Y - originalBounds.Height) + e.Offset.Y;
                }
                if (!isMirrored)
                {
                    if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Left) != ToolboxSnapDragDropEventArgs.SnapDirection.None)
                    {
                        rectangle.X += e.Offset.X;
                        return(rectangle);
                    }
                    if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Right) != ToolboxSnapDragDropEventArgs.SnapDirection.None)
                    {
                        rectangle.X = (originalBounds.X - originalBounds.Width) + e.Offset.X;
                    }
                    return(rectangle);
                }
                if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Left) != ToolboxSnapDragDropEventArgs.SnapDirection.None)
                {
                    rectangle.X = (originalBounds.X - originalBounds.Width) - e.Offset.X;
                    return(rectangle);
                }
                if ((e.SnapDirections & ToolboxSnapDragDropEventArgs.SnapDirection.Right) != ToolboxSnapDragDropEventArgs.SnapDirection.None)
                {
                    rectangle.X -= e.Offset.X;
                }
            }
            return(rectangle);
        }
        public IComponent[] CreateTool(ToolboxItem tool, Control parent, int x, int y, int width, int height, bool hasLocation, bool hasSize, ToolboxSnapDragDropEventArgs e)
        {
            // Services we will need
            //
            IToolboxService   toolboxSvc = (IToolboxService)GetService(typeof(IToolboxService));
            ISelectionService selSvc     = (ISelectionService)GetService(typeof(ISelectionService));
            IDesignerHost     host       = (IDesignerHost)GetService(typeof(IDesignerHost));

            IComponent[] comps = Array.Empty <IComponent>();

            Cursor oldCursor = Cursor.Current;

            Cursor.Current = Cursors.WaitCursor;
            DesignerTransaction trans = null;

            try
            {
                try
                {
                    if (host != null)
                    {
                        trans = host.CreateTransaction(string.Format(SR.DesignerBatchCreateTool, tool.ToString()));
                    }
                }
                catch (CheckoutException cxe)
                {
                    if (cxe == CheckoutException.Canceled)
                    {
                        return(comps);
                    }

                    throw;
                }

                try
                {
                    try
                    {
                        // First check if we are currently in localization mode (i.e., language is non-default).
                        // If so, we should not permit addition of new components. This is an intentional
                        // change from Everett - see VSWhidbey #292249.
                        if (host != null && CurrentlyLocalizing(host.RootComponent))
                        {
                            IUIService uiService = (IUIService)GetService(typeof(IUIService));
                            if (uiService != null)
                            {
                                uiService.ShowMessage(SR.LocalizingCannotAdd);
                            }

                            comps = Array.Empty <IComponent>();
                            return(comps);
                        }

                        // Create a dictionary of default values that the designer can
                        // use to initialize a control with.
                        Hashtable defaultValues = new Hashtable();
                        if (parent != null)
                        {
                            defaultValues["Parent"] = parent;
                        }

                        // adjust the location if we are in a mirrored parent. That is because the origin
                        // will then be in the upper right rather than upper left.
                        if (parent != null && parent.IsMirrored)
                        {
                            x += width;
                        }

                        if (hasLocation)
                        {
                            defaultValues["Location"] = new Point(x, y);
                        }
                        if (hasSize)
                        {
                            defaultValues["Size"] = new Size(width, height);
                        }
                        //store off extra behavior drag/drop information
                        if (e != null)
                        {
                            defaultValues["ToolboxSnapDragDropEventArgs"] = e;
                        }

                        comps = tool.CreateComponents(host, defaultValues);
                    }
                    catch (CheckoutException checkoutEx)
                    {
                        if (checkoutEx == CheckoutException.Canceled)
                        {
                            comps = Array.Empty <IComponent>();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    catch (ArgumentException argumentEx)
                    {
                        IUIService uiService = (IUIService)GetService(typeof(IUIService));
                        if (uiService != null)
                        {
                            uiService.ShowError(argumentEx);
                        }
                    }
                    catch (Exception ex)
                    {
                        IUIService uiService = (IUIService)GetService(typeof(IUIService));

                        string exceptionMessage = string.Empty;
                        if (ex.InnerException != null)
                        {
                            exceptionMessage = ex.InnerException.ToString();
                        }

                        if (string.IsNullOrEmpty(exceptionMessage))
                        {
                            exceptionMessage = ex.ToString();
                        }

                        if (ex is InvalidOperationException)
                        {
                            exceptionMessage = ex.Message;
                        }

                        if (uiService != null)
                        {
                            uiService.ShowError(ex, string.Format(SR.FailedToCreateComponent, tool.DisplayName, exceptionMessage));
                        }
                        else
                        {
                            throw;
                        }
                    }

                    if (comps == null)
                    {
                        comps = Array.Empty <IComponent>();
                    }
                }
                finally
                {
                    if (toolboxSvc != null && tool.Equals(toolboxSvc.GetSelectedToolboxItem(host)))
                    {
                        toolboxSvc.SelectedToolboxItemUsed();
                    }
                }
            }
            finally
            {
                if (trans != null)
                {
                    trans.Commit();
                }

                Cursor.Current = oldCursor;
            }

            // Finally, select the newly created components.
            //
            if (selSvc != null && comps.Length > 0)
            {
                if (host != null)
                {
                    host.Activate();
                }

                ArrayList selectComps = new ArrayList(comps);

                for (int i = 0; i < comps.Length; i++)
                {
                    if (!TypeDescriptor.GetAttributes(comps[i]).Contains(DesignTimeVisibleAttribute.Yes))
                    {
                        selectComps.Remove(comps[i]);
                    }
                }

                selSvc.SetSelectedComponents(selectComps.ToArray(), SelectionTypes.Replace);
            }

            codemarkers.CodeMarker((int)CodeMarkerEvent.perfFXDesignCreateComponentEnd);
            return(comps);
        }
Beispiel #4
0
 public IComponent[] CreateTool(ToolboxItem tool, Control parent, int x, int y, int width, int height,
                                bool hasLocation, bool hasSize, ToolboxSnapDragDropEventArgs e)
 {
     throw new NotImplementedException(SR.NotImplementedByDesign);
 }