Beispiel #1
0
        protected override void OnDragDrop(DragEventArgs de)
        {
            base.OnDragDrop(de);

            var insertionIndex = (_insertionBeforeControl == null)
                ? Panel.Controls.Count
                : Panel.Controls.IndexOf(_insertionBeforeControl);

            var transaction = DesignerHost.CreateTransaction($"Move {GetSelectedControlsString()} to position {insertionIndex} in {Panel.Name}");

            using (transaction)
                using (Panel.ChangingComponentProperty(nameof(Panel.Controls)))
                {
                    SelectionService.GetSelectedComponents()
                    .OfType <Control>()
                    .ToList()
                    .ForEach(control =>
                    {
                        var currentIndex = Panel.Controls.GetChildIndex(control);
                        if (currentIndex >= 0 && currentIndex < insertionIndex)
                        {
                            insertionIndex--;
                        }

                        Panel.Controls.SetChildIndex(control, insertionIndex++);
                    });

                    transaction.Commit();
                }

            ClearInsertionDisplay();
        }
Beispiel #2
0
        private string GetSelectedControlsString()
        {
            if (SelectionService.SelectionCount == 1 &&
                SelectionService.PrimarySelection is Control selectedControl)
            {
                return(selectedControl.Name);
            }

            return(SelectionService.GetSelectedComponents().OfType <Control>().Count() + " Controls");
        }
Beispiel #3
0
 private void SelectionService_SelectionChanged(object sender, EventArgs e)
 {
     controlSelected = false;
     foreach (var component in SelectionService.GetSelectedComponents())
     {
         if (ReferenceEquals(component, Control) || (component is Tab tab && ReferenceEquals(tab.Parent, Control)))
         {
             controlSelected = true;
             break;
         }
     }
 }
Beispiel #4
0
        /// <include file='doc\ControlCommandSet.uex' path='docs/doc[@for="ControlCommandSet.OnStatusZOrder"]/*' />
        /// <devdoc>
        ///     Determines the status of a menu command.  Commands with this event
        ///     handler are enabled for zordering.  The rules are:
        ///
        ///      1) More than one component on the form
        ///      2) At least one Control-derived component must be selected
        ///      3) The form must not be selected
        /// </devdoc>
        private void OnStatusZOrder(object sender, EventArgs e)
        {
            MenuCommand   cmd  = (MenuCommand)sender;
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));

            if (host != null)
            {
                ComponentCollection comps = host.Container.Components;
                object baseComp           = host.RootComponent;

                // The form by itself is one component, so this means
                // we need more than two.
                bool enable = (comps != null && comps.Count > 2 && controlsOnlySelection);

                if (enable)
                {
                    Debug.Assert(SelectionService != null, "Need SelectionService for sizing command");

                    if (SelectionService == null)
                    {
                        return;
                    }

                    // There must also be a control in the mix, and not the base component, and
                    // it cannot be privately inherited.
                    //
                    ICollection selectedComponents = SelectionService.GetSelectedComponents();

                    enable = false;
                    foreach (object obj in selectedComponents)
                    {
                        if (obj is Control &&
                            !TypeDescriptor.GetAttributes(obj)[typeof(InheritanceAttribute)].Equals(InheritanceAttribute.InheritedReadOnly))
                        {
                            enable = true;
                        }

                        // if the form is in there we're always false.
                        if (obj == baseComp)
                        {
                            enable = false;
                            break;
                        }
                    }
                }


                cmd.Enabled = enable;
                return;
            }
            cmd.Enabled = false;
        }
        void SelectionService_SelectionChanged(object sender, EventArgs e)
        {
            IList selectedComponents = (IList)SelectionService.GetSelectedComponents();

            if (selectedComponents.Count == 1)
            {
                Tab tab = selectedComponents[0] as Tab;
                if (tab != null)
                {
                    SetProperty("SelectedTabStripPage", tab.TabStripPage);
                    SetProperty(tab, "Checked", true);
                }
            }
        }
Beispiel #6
0
        protected override void OnDragOver(DragEventArgs de)
        {
            base.OnDragOver(de);

            var insertionBeforeControl = Panel.Controls
                                         .Cast <Control>()
                                         .Except(SelectionService.GetSelectedComponents().OfType <Control>())
                                         .FirstOrDefault(child =>
            {
                var childRect = child.RectangleToScreen(child.ClientRectangle);
                return((Panel.Orientation == Orientation.Horizontal)
                        ? de.X <= childRect.GetCenter()
                        : de.Y <= childRect.GetMiddle());
            });

            UpdateInsertionDisplay(insertionBeforeControl);
        }
Beispiel #7
0
        /// <include file='doc\ControlCommandSet.uex' path='docs/doc[@for="ControlCommandSet.OnMenuZOrderSelection"]/*' />
        /// <devdoc>
        ///     Called when the zorder->send to back menu item is selected.
        /// </devdoc>
        private void OnMenuZOrderSelection(object sender, EventArgs e)
        {
            MenuCommand cmd   = (MenuCommand)sender;
            CommandID   cmdID = cmd.CommandID;

            Debug.Assert(SelectionService != null, "Need SelectionService for sizing command");

            if (SelectionService == null)
            {
                return;
            }

            Cursor oldCursor = Cursor.Current;

            try {
                Cursor.Current = Cursors.WaitCursor;


                IComponentChangeService ccs          = (IComponentChangeService)GetService(typeof(IComponentChangeService));
                IDesignerHost           designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
                DesignerTransaction     trans        = null;

                try {
                    string batchString;

                    // NOTE: this only works on Control types
                    ICollection sel = SelectionService.GetSelectedComponents();
                    object[]    selectedComponents = new object[sel.Count];
                    sel.CopyTo(selectedComponents, 0);

                    if (cmdID == MenuCommands.BringToFront)
                    {
                        batchString = SR.GetString(SR.CommandSetBringToFront, selectedComponents.Length);
                    }
                    else
                    {
                        batchString = SR.GetString(SR.CommandSetSendToBack, selectedComponents.Length);
                    }

                    // sort the components by their current zOrder
                    Array.Sort(selectedComponents, new ControlComparer());

                    trans = designerHost.CreateTransaction(batchString);

                    int len = selectedComponents.Length;
                    for (int i = len - 1; i >= 0; i--)
                    {
                        if (selectedComponents[i] is Control)
                        {
                            Control            parent       = ((Control)selectedComponents[i]).Parent;
                            PropertyDescriptor controlsProp = null;
                            if (ccs != null && parent != null)
                            {
                                try {
                                    controlsProp = TypeDescriptor.GetProperties(parent)["Controls"];
                                    if (controlsProp != null)
                                    {
                                        ccs.OnComponentChanging(parent, controlsProp);
                                    }
                                }
                                catch (CheckoutException ex) {
                                    if (ex == CheckoutException.Canceled)
                                    {
                                        return;
                                    }
                                    throw ex;
                                }
                            }


                            if (cmdID == MenuCommands.BringToFront)
                            {
                                // we do this backwards to maintain zorder
                                Control otherControl = selectedComponents[len - i - 1] as Control;
                                if (otherControl != null)
                                {
                                    otherControl.BringToFront();
                                }
                            }
                            else if (cmdID == MenuCommands.SendToBack)
                            {
                                ((Control)selectedComponents[i]).SendToBack();
                            }

                            if (ccs != null)
                            {
                                if (controlsProp != null && parent != null)
                                {
                                    ccs.OnComponentChanged(parent, controlsProp, null, null);
                                }
                            }
                        }
                    }
                }
                finally {
                    // now we need to regenerate so the ordering is right.
                    if (trans != null)
                    {
                        trans.Commit();
                    }
                }
            }
            finally {
                Cursor.Current = oldCursor;
            }
        }