private void PaintInsertionMark(ToolStripItem item)
 {
     if ((ToolStripDesigner.LastCursorPosition == Point.Empty) || (ToolStripDesigner.LastCursorPosition != Cursor.Position))
     {
         ToolStripKeyboardHandlingService keyBoardHandlingService = this.GetKeyBoardHandlingService(item);
         if (((keyBoardHandlingService == null) || !keyBoardHandlingService.TemplateNodeActive) && ((item != null) && (item.Site != null)))
         {
             ToolStripDesigner.LastCursorPosition = Cursor.Position;
             IDesignerHost service = (IDesignerHost)item.Site.GetService(typeof(IDesignerHost));
             if (service != null)
             {
                 Rectangle       paintingBounds  = GetPaintingBounds(service, item);
                 BehaviorService behaviorService = this.GetBehaviorService(item);
                 if (behaviorService != null)
                 {
                     using (Graphics graphics = behaviorService.AdornerWindowGraphics)
                     {
                         using (Pen pen = new Pen(new SolidBrush(Color.Black)))
                         {
                             pen.DashStyle = DashStyle.Dot;
                             graphics.DrawRectangle(pen, paintingBounds);
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        internal override void ShowContextMenu(int x, int y)
        {
            ToolStripKeyboardHandlingService service = (ToolStripKeyboardHandlingService)this.GetService(typeof(ToolStripKeyboardHandlingService));

            if (service != null)
            {
                if (!service.ContextMenuShownByKeyBoard)
                {
                    BehaviorService service2 = (BehaviorService)this.GetService(typeof(BehaviorService));
                    Point           empty    = Point.Empty;
                    if (service2 != null)
                    {
                        empty = service2.ScreenToAdornerWindow(new Point(x, y));
                    }
                    if (this.GetGlyphBounds().Contains(empty))
                    {
                        this.DesignerContextMenu.Show(x, y);
                    }
                }
                else
                {
                    service.ContextMenuShownByKeyBoard = false;
                    this.DesignerContextMenu.Show(x, y);
                }
            }
        }
Exemplo n.º 3
0
 internal void SelectNextItem(ISelectionService service, bool enterKeyPressed, ToolStripDesigner designer)
 {
     if (this.ToolStripItem is ToolStripDropDownItem)
     {
         this.SetSelection(enterKeyPressed);
     }
     else
     {
         ToolStrip immediateParent = (ToolStrip)this.ImmediateParent;
         if (immediateParent is ToolStripOverflow)
         {
             immediateParent = this.ToolStripItem.Owner;
         }
         int index = immediateParent.Items.IndexOf(this.ToolStripItem);
         System.Windows.Forms.ToolStripItem item2    = immediateParent.Items[index + 1];
         ToolStripKeyboardHandlingService   service2 = (ToolStripKeyboardHandlingService)this.GetService(typeof(ToolStripKeyboardHandlingService));
         if (service2 != null)
         {
             if (item2 == designer.EditorNode)
             {
                 service2.SelectedDesignerControl = item2;
                 this.selSvc.SetSelectedComponents(null, SelectionTypes.Replace);
             }
             else
             {
                 service2.SelectedDesignerControl = null;
                 this.selSvc.SetSelectedComponents(new object[] { item2 });
             }
         }
     }
 }
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            this.host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            if (((ToolStripKeyboardHandlingService)this.GetService(typeof(ToolStripKeyboardHandlingService))) == null)
            {
                ToolStripKeyboardHandlingService service = new ToolStripKeyboardHandlingService(component.Site);
            }
            if (((ISupportInSituService)this.GetService(typeof(ISupportInSituService))) == null)
            {
                ISupportInSituService service2 = new ToolStripInSituService(base.Component.Site);
            }
            this.dropDown         = (ToolStripDropDown)base.Component;
            this.dropDown.Visible = false;
            this.AutoClose        = this.dropDown.AutoClose;
            this.AllowDrop        = this.dropDown.AllowDrop;
            this.selSvc           = (ISelectionService)this.GetService(typeof(ISelectionService));
            if (this.selSvc != null)
            {
                if ((this.host != null) && !this.host.Loading)
                {
                    this.selSvc.SetSelectedComponents(new IComponent[] { this.host.RootComponent }, SelectionTypes.Replace);
                }
                this.selSvc.SelectionChanging += new EventHandler(this.OnSelectionChanging);
                this.selSvc.SelectionChanged  += new EventHandler(this.OnSelectionChanged);
            }
            this.designMenu          = new MenuStrip();
            this.designMenu.Visible  = false;
            this.designMenu.AutoSize = false;
            this.designMenu.Dock     = DockStyle.Top;
            Control rootComponent = this.host.RootComponent as Control;

            if (rootComponent != null)
            {
                this.menuItem           = new ToolStripMenuItem();
                this.menuItem.BackColor = SystemColors.Window;
                this.menuItem.Name      = base.Component.Site.Name;
                this.menuItem.Text      = (this.dropDown != null) ? this.dropDown.GetType().Name : this.menuItem.Name;
                this.designMenu.Items.Add(this.menuItem);
                rootComponent.Controls.Add(this.designMenu);
                this.designMenu.SendToBack();
                this.nestedContainer = this.GetService(typeof(INestedContainer)) as INestedContainer;
                if (this.nestedContainer != null)
                {
                    this.nestedContainer.Add(this.menuItem, "ContextMenuStrip");
                }
            }
            new EditorServiceContext(this, TypeDescriptor.GetProperties(base.Component)["Items"], System.Design.SR.GetString("ToolStripItemCollectionEditorVerb"));
            if (this.undoEngine == null)
            {
                this.undoEngine = this.GetService(typeof(UndoEngine)) as UndoEngine;
                if (this.undoEngine != null)
                {
                    this.undoEngine.Undone += new EventHandler(this.OnUndone);
                }
            }
        }
 // Gets the ToolStripKeyBoardHandling Service.
 private ToolStripKeyboardHandlingService GetKeyBoardHandlingService(ToolStripItem item)
 {
     Debug.Assert(item != null, "Item passed is null, ToolStripKeyBoardHandlingService cannot be obtained");
     if (item.Site != null)
     {
         ToolStripKeyboardHandlingService keyBoardSvc = (ToolStripKeyboardHandlingService)item.Site.GetService(typeof(ToolStripKeyboardHandlingService));
         Debug.Assert(keyBoardSvc != null, "Failed to get ToolStripKeyboardHandlingService!");
         return(keyBoardSvc);
     }
     return(null);
 }
Exemplo n.º 6
0
        /// <summary>
        ///  Paints the insertion mark when items are being reordered
        /// </summary>
        private void PaintInsertionMark(ToolStripItem item)
        {
            // Dont paint if cursor hasnt moved.
            if (ToolStripDesigner.s_lastCursorPosition != Point.Empty && ToolStripDesigner.s_lastCursorPosition == Cursor.Position)
            {
                return;
            }

            // Dont paint any "MouseOver" glyohs if TemplateNode is ACTIVE !
            ToolStripKeyboardHandlingService keyService = GetKeyBoardHandlingService(item);

            if (keyService != null && keyService.TemplateNodeActive)
            {
                return;
            }

            //Start from fresh State...
            if (item != null && item.Site != null)
            {
                ToolStripDesigner.s_lastCursorPosition = Cursor.Position;
                IDesignerHost designerHost = (IDesignerHost)item.Site.GetService(typeof(IDesignerHost));
                if (designerHost != null)
                {
                    Rectangle       bounds = GetPaintingBounds(designerHost, item);
                    BehaviorService bSvc   = GetBehaviorService(item);
                    if (bSvc != null)
                    {
                        Graphics g = bSvc.AdornerWindowGraphics;
                        try
                        {
                            using (Pen p = new Pen(new SolidBrush(Color.Black)))
                            {
                                p.DashStyle = DashStyle.Dot;
                                g.DrawRectangle(p, bounds);
                            }
                        }
                        finally
                        {
                            g.Dispose();
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///  Clears the insertion mark when items are being reordered
        /// </summary>
        private void ClearInsertionMark(ToolStripItem item)
        {
            // Dont paint if cursor hasnt moved.
            if (ToolStripDesigner.s_lastCursorPosition != Point.Empty && ToolStripDesigner.s_lastCursorPosition == Cursor.Position)
            {
                return;
            }

            // Dont paint any "MouseOver" glyohs if TemplateNode is ACTIVE !
            ToolStripKeyboardHandlingService keyService = GetKeyBoardHandlingService(item);

            if (keyService != null && keyService.TemplateNodeActive)
            {
                return;
            }

            // stuff away the lastInsertionMarkRect  and clear it out _before_ we call paint OW the call to invalidate wont help as it will get repainted.
            if (item != null && item.Site != null)
            {
                IDesignerHost designerHost = (IDesignerHost)item.Site.GetService(typeof(IDesignerHost));
                if (designerHost != null)
                {
                    Rectangle bounds = GetPaintingBounds(designerHost, item);
                    bounds.Inflate(GLYPHBORDER, GLYPHBORDER);
                    Region rgn = new Region(bounds);
                    try
                    {
                        bounds.Inflate(-GLYPHINSET, -GLYPHINSET);
                        rgn.Exclude(bounds);
                        BehaviorService bSvc = GetBehaviorService(item);
                        if (bSvc != null && bounds != Rectangle.Empty)
                        {
                            bSvc.Invalidate(rgn);
                        }
                    }
                    finally
                    {
                        rgn.Dispose();
                        rgn = null;
                    }
                }
            }
        }
 private void ClearInsertionMark(ToolStripItem item)
 {
     if ((ToolStripDesigner.LastCursorPosition == Point.Empty) || (ToolStripDesigner.LastCursorPosition != Cursor.Position))
     {
         ToolStripKeyboardHandlingService keyBoardHandlingService = this.GetKeyBoardHandlingService(item);
         if ((keyBoardHandlingService == null) || !keyBoardHandlingService.TemplateNodeActive)
         {
             Rectangle empty = Rectangle.Empty;
             if ((item != null) && (item.Site != null))
             {
                 IDesignerHost service = (IDesignerHost)item.Site.GetService(typeof(IDesignerHost));
                 if (service != null)
                 {
                     empty = GetPaintingBounds(service, item);
                     empty.Inflate(1, 1);
                     Region r = new Region(empty);
                     try
                     {
                         empty.Inflate(-2, -2);
                         r.Exclude(empty);
                         BehaviorService behaviorService = this.GetBehaviorService(item);
                         if ((behaviorService != null) && (empty != Rectangle.Empty))
                         {
                             behaviorService.Invalidate(r);
                         }
                     }
                     finally
                     {
                         r.Dispose();
                         r = null;
                     }
                 }
             }
         }
     }
 }
        //  OLE DragDrop virtual methods
        /// <summary>
        ///  OnDragDrop can be overridden so that a Behavior can specify its own Drag/Drop rules.
        /// </summary>
        public override void OnDragDrop(Glyph g, DragEventArgs e)
        {
            ToolStripItem currentDropItem = ToolStripDesigner.s_dragItem;

            // Ensure that the list item index is contained in the data.
            if (e.Data is ToolStripItemDataObject && currentDropItem != null)
            {
                ToolStripItemDataObject data = (ToolStripItemDataObject)e.Data;
                // Get the PrimarySelection before the Drag operation...
                ToolStripItem selectedItem = data.PrimarySelection;
                IDesignerHost designerHost = (IDesignerHost)currentDropItem.Site.GetService(typeof(IDesignerHost));
                Debug.Assert(designerHost != null, "Invalid DesignerHost");
                //Do DragDrop only if currentDropItem has changed.
                if (currentDropItem != selectedItem && designerHost != null)
                {
                    ArrayList components      = data.DragComponents;
                    ToolStrip parentToolStrip = currentDropItem.GetCurrentParent() as ToolStrip;
                    int       primaryIndex    = -1;
                    string    transDesc;
                    bool      copy = (e.Effect == DragDropEffects.Copy);
                    if (components.Count == 1)
                    {
                        string name = TypeDescriptor.GetComponentName(components[0]);
                        if (name == null || name.Length == 0)
                        {
                            name = components[0].GetType().Name;
                        }
                        transDesc = string.Format(copy ? SR.BehaviorServiceCopyControl : SR.BehaviorServiceMoveControl, name);
                    }
                    else
                    {
                        transDesc = string.Format(copy ? SR.BehaviorServiceCopyControls : SR.BehaviorServiceMoveControls, components.Count);
                    }

                    DesignerTransaction designerTransaction = designerHost.CreateTransaction(transDesc);
                    try
                    {
                        IComponentChangeService changeSvc = (IComponentChangeService)currentDropItem.Site.GetService(typeof(IComponentChangeService));
                        if (changeSvc != null)
                        {
                            if (parentToolStrip is ToolStripDropDown dropDown)
                            {
                                ToolStripItem ownerItem = dropDown.OwnerItem;
                                changeSvc.OnComponentChanging(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"]);
                            }
                            else
                            {
                                changeSvc.OnComponentChanging(parentToolStrip, TypeDescriptor.GetProperties(parentToolStrip)["Items"]);
                            }
                        }

                        // If we are copying, then we want to make a copy of the components we are dragging
                        if (copy)
                        {
                            // Remember the primary selection if we had one
                            if (selectedItem != null)
                            {
                                primaryIndex = components.IndexOf(selectedItem);
                            }
                            ToolStripKeyboardHandlingService keyboardHandlingService = GetKeyBoardHandlingService(selectedItem);
                            if (keyboardHandlingService != null)
                            {
                                keyboardHandlingService.CopyInProgress = true;
                            }
                            components = DesignerUtils.CopyDragObjects(components, currentDropItem.Site) as ArrayList;
                            if (keyboardHandlingService != null)
                            {
                                keyboardHandlingService.CopyInProgress = false;
                            }
                            if (primaryIndex != -1)
                            {
                                selectedItem = components[primaryIndex] as ToolStripItem;
                            }
                        }

                        if (e.Effect == DragDropEffects.Move || copy)
                        {
                            ISelectionService selSvc = GetSelectionService(currentDropItem);
                            if (selSvc != null)
                            {
                                // Insert the item.
                                if (parentToolStrip is ToolStripOverflow)
                                {
                                    parentToolStrip = (((ToolStripOverflow)parentToolStrip).OwnerItem).Owner;
                                }

                                int indexOfItemUnderMouseToDrop = parentToolStrip.Items.IndexOf(ToolStripDesigner.s_dragItem);
                                if (indexOfItemUnderMouseToDrop != -1)
                                {
                                    int indexOfPrimarySelection = 0;
                                    if (selectedItem != null)
                                    {
                                        indexOfPrimarySelection = parentToolStrip.Items.IndexOf(selectedItem);
                                    }

                                    if (indexOfPrimarySelection != -1 && indexOfItemUnderMouseToDrop > indexOfPrimarySelection)
                                    {
                                        indexOfItemUnderMouseToDrop--;
                                    }
                                    foreach (ToolStripItem item in components)
                                    {
                                        parentToolStrip.Items.Insert(indexOfItemUnderMouseToDrop, item);
                                    }
                                }
                                selSvc.SetSelectedComponents(new IComponent[] { selectedItem }, SelectionTypes.Primary | SelectionTypes.Replace);
                            }
                        }
                        if (changeSvc != null)
                        {
                            ToolStripDropDown dropDown = parentToolStrip as ToolStripDropDown;
                            if (dropDown != null)
                            {
                                ToolStripItem ownerItem = dropDown.OwnerItem;
                                changeSvc.OnComponentChanged(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"], null, null);
                            }
                            else
                            {
                                changeSvc.OnComponentChanged(parentToolStrip, TypeDescriptor.GetProperties(parentToolStrip)["Items"], null, null);
                            }

                            //fire extra changing/changed events.
                            if (copy)
                            {
                                if (dropDown != null)
                                {
                                    ToolStripItem ownerItem = dropDown.OwnerItem;
                                    changeSvc.OnComponentChanging(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"]);
                                    changeSvc.OnComponentChanged(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"], null, null);
                                }
                                else
                                {
                                    changeSvc.OnComponentChanging(parentToolStrip, TypeDescriptor.GetProperties(parentToolStrip)["Items"]);
                                    changeSvc.OnComponentChanged(parentToolStrip, TypeDescriptor.GetProperties(parentToolStrip)["Items"], null, null);
                                }
                            }
                        }

                        //If Parent is DropDown... we have to manage the Glyphs ....
                        foreach (ToolStripItem item in components)
                        {
                            if (item is ToolStripDropDownItem)
                            {
                                if (designerHost.GetDesigner(item) is ToolStripMenuItemDesigner itemDesigner)
                                {
                                    itemDesigner.InitializeDropDown();
                                }
                            }
                            if (item.GetCurrentParent() is ToolStripDropDown dropDown && !(dropDown is ToolStripOverflow))
                            {
                                if (dropDown.OwnerItem is ToolStripDropDownItem ownerItem)
                                {
                                    if (designerHost.GetDesigner(ownerItem) is ToolStripMenuItemDesigner ownerDesigner)
                                    {
                                        ownerDesigner.InitializeBodyGlyphsForItems(false, ownerItem);
                                        ownerDesigner.InitializeBodyGlyphsForItems(true, ownerItem);
                                    }
                                }
                            }
                        }
                        // Refresh on SelectionManager...
                        BehaviorService bSvc = GetBehaviorService(currentDropItem);
                        if (bSvc != null)
                        {
                            bSvc.SyncSelection();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (designerTransaction != null)
                        {
                            designerTransaction.Cancel();
                            designerTransaction = null;
                        }
                        if (ClientUtils.IsCriticalException(ex))
                        {
                            throw;
                        }
                    }
                    finally
                    {
                        if (designerTransaction != null)
                        {
                            designerTransaction.Commit();
                            designerTransaction = null;
                        }
                    }
                }
            }
        }
        // Occurs when MouseDown on the TooLStripItem glyph
        public override bool OnMouseDown(Glyph g, MouseButtons button, Point mouseLoc)
        {
            ToolStripItemGlyph glyph     = g as ToolStripItemGlyph;
            ToolStripItem      glyphItem = glyph.Item;
            ISelectionService  selSvc    = GetSelectionService(glyphItem);
            BehaviorService    bSvc      = GetBehaviorService(glyphItem);
            ToolStripKeyboardHandlingService keyService = GetKeyBoardHandlingService(glyphItem);

            if ((button == MouseButtons.Left) && (keyService != null) && (keyService.TemplateNodeActive))
            {
                if (keyService.ActiveTemplateNode.IsSystemContextMenuDisplayed)
                {
                    // skip behaviors when the context menu is displayed
                    return(false);
                }
            }

            IDesignerHost designerHost = (IDesignerHost)glyphItem.Site.GetService(typeof(IDesignerHost));

            Debug.Assert(designerHost != null, "Invalid DesignerHost");

            //Cache original selection
            ICollection originalSelComps = null;

            if (selSvc != null)
            {
                originalSelComps = selSvc.GetSelectedComponents();
            }

            // Add the TemplateNode to the Selection if it is currently Selected as the GetSelectedComponents wont do it for us.
            ArrayList origSel = new ArrayList(originalSelComps);

            if (origSel.Count == 0)
            {
                if (keyService != null && keyService.SelectedDesignerControl != null)
                {
                    origSel.Add(keyService.SelectedDesignerControl);
                }
            }

            if (keyService != null)
            {
                keyService.SelectedDesignerControl = null;
                if (keyService.TemplateNodeActive)
                {
                    // If templateNode Active .. commit and Select it
                    keyService.ActiveTemplateNode.CommitAndSelect();
                    // if the selected item is clicked .. then commit the node and reset the selection (refer 488002)
                    if (selSvc.PrimarySelection is ToolStripItem currentSel && currentSel == glyphItem)
                    {
                        selSvc.SetSelectedComponents(null, SelectionTypes.Replace);
                    }
                }
            }

            if (selSvc == null || MouseHandlerPresent(glyphItem))
            {
                return(false);
            }

            if (glyph != null && button == MouseButtons.Left)
            {
                ToolStripItem selectedItem = selSvc.PrimarySelection as ToolStripItem;
                // Always set the Drag-Rect for Drag-Drop...
                SetParentDesignerValuesForDragDrop(glyphItem, true, mouseLoc);
                // Check if this item is already selected ...
                if (selectedItem != null && selectedItem == glyphItem)
                {
                    // If the Selecteditem is already in editmode ... bail out
                    if (selectedItem != null)
                    {
                        ToolStripItemDesigner selectedItemDesigner = glyph.ItemDesigner;
                        if (selectedItemDesigner != null && selectedItemDesigner.IsEditorActive)
                        {
                            return(false);
                        }
                    }

                    // Check if this is CTRL + Click or SHIFT + Click, if so then just remove the selection
                    bool removeSel = (Control.ModifierKeys & (Keys.Control | Keys.Shift)) > 0;
                    if (removeSel)
                    {
                        selSvc.SetSelectedComponents(new IComponent[] { selectedItem }, SelectionTypes.Remove);
                        return(false);
                    }

                    //start Double Click Timer
                    // This is required for the second down in selection which can be the first down of a Double click on the glyph confusing... hence this comment ...
                    // Heres the scenario ....
                    // DOWN 1 - selects the ITEM
                    // DOWN 2 - ITEM goes into INSITU....
                    // DOUBLE CLICK - dont show code..
                    // Open INSITU after the double click time
                    if (selectedItem is ToolStripMenuItem)
                    {
                        _timer = new Timer
                        {
                            Interval = SystemInformation.DoubleClickTime
                        };
                        _timer.Tick   += new EventHandler(OnDoubleClickTimerTick);
                        _timer.Enabled = true;
                        _selectedGlyph = glyph;
                    }
                }
                else
                {
                    bool shiftPressed = (Control.ModifierKeys & Keys.Shift) > 0;
                    // We should process MouseDown only if we are not yet selected....
                    if (!selSvc.GetComponentSelected(glyphItem))
                    {
                        //Reset the State... On the Glpyhs .. we get MouseDown - Mouse UP (for single Click) And we get MouseDown - MouseUp - DoubleClick - Up (for double Click) Hence reset the state at start....
                        _mouseUpFired     = false;
                        _doubleClickFired = false;
                        //Implementing Shift + Click....
                        // we have 2 items, namely, selectedItem (current PrimarySelection) and glyphItem (item which has received mouseDown) FIRST check if they have common parent...  IF YES then get the indices of the two and SELECT all items from LOWER index to the HIGHER index.
                        if (shiftPressed && (selectedItem != null && CommonParent(selectedItem, glyphItem)))
                        {
                            ToolStrip parent = null;
                            if (glyphItem.IsOnOverflow)
                            {
                                parent = glyphItem.Owner;
                            }
                            else
                            {
                                parent = glyphItem.GetCurrentParent();
                            }
                            int startIndexOfSelection = Math.Min(parent.Items.IndexOf(selectedItem), parent.Items.IndexOf(glyphItem));
                            int endIndexOfSelection   = Math.Max(parent.Items.IndexOf(selectedItem), parent.Items.IndexOf(glyphItem));
                            int countofItemsSelected  = (endIndexOfSelection - startIndexOfSelection) + 1;

                            // if two adjacent items are selected ...
                            if (countofItemsSelected == 2)
                            {
                                selSvc.SetSelectedComponents(new IComponent[] { glyphItem });
                            }
                            else
                            {
                                object[] totalObjects = new object[countofItemsSelected];
                                int      j            = 0;
                                for (int i = startIndexOfSelection; i <= endIndexOfSelection; i++)
                                {
                                    totalObjects[j++] = parent.Items[i];
                                }
                                selSvc.SetSelectedComponents(new IComponent[] { parent }, SelectionTypes.Replace);
                                ToolStripDesigner.s_shiftState = true;
                                selSvc.SetSelectedComponents(totalObjects, SelectionTypes.Replace);
                            }
                        }
                        //End Implmentation
                        else
                        {
                            if (glyphItem.IsOnDropDown && ToolStripDesigner.s_shiftState)
                            {
                                //Invalidate glyh only if we are in ShiftState...
                                ToolStripDesigner.s_shiftState = false;
                                if (bSvc != null)
                                {
                                    bSvc.Invalidate(glyphItem.Owner.Bounds);
                                }
                            }
                            selSvc.SetSelectedComponents(new IComponent[] { glyphItem }, SelectionTypes.Auto);
                        }
                        // Set the appropriate object.
                        if (keyService != null)
                        {
                            keyService.ShiftPrimaryItem = glyphItem;
                        }
                    }
                    // we are already selected and if shiftpressed...
                    else if (shiftPressed || (Control.ModifierKeys & Keys.Control) > 0)
                    {
                        selSvc.SetSelectedComponents(new IComponent[] { glyphItem }, SelectionTypes.Remove);
                    }
                }
            }

            if (glyph != null && button == MouseButtons.Right)
            {
                if (!selSvc.GetComponentSelected(glyphItem))
                {
                    selSvc.SetSelectedComponents(new IComponent[] { glyphItem });
                }
            }

            // finally Invalidate all selections
            ToolStripDesignerUtils.InvalidateSelection(origSel, glyphItem, glyphItem.Site, false);
            return(false);
        }
 public void ShowMenu()
 {
     if (this.menuItem != null)
     {
         Control parent    = this.designMenu.Parent;
         Form    component = parent as Form;
         if (component != null)
         {
             this.parentFormDesigner = this.host.GetDesigner(component) as FormDocumentDesigner;
             if ((this.parentFormDesigner != null) && (this.parentFormDesigner.Menu != null))
             {
                 this.parentMenu = this.parentFormDesigner.Menu;
                 this.parentFormDesigner.Menu = null;
             }
         }
         this.selected           = true;
         this.designMenu.Visible = true;
         this.designMenu.BringToFront();
         this.menuItem.Visible = true;
         if ((this.currentParent != null) && (this.currentParent != this.menuItem))
         {
             ToolStripMenuItemDesigner designer = this.host.GetDesigner(this.currentParent) as ToolStripMenuItemDesigner;
             if (designer != null)
             {
                 designer.RemoveTypeHereNode(this.currentParent);
             }
         }
         this.menuItem.DropDown           = this.dropDown;
         this.menuItem.DropDown.OwnerItem = this.menuItem;
         if (this.dropDown.Items.Count > 0)
         {
             ToolStripItem[] array = new ToolStripItem[this.dropDown.Items.Count];
             this.dropDown.Items.CopyTo(array, 0);
             foreach (ToolStripItem item in array)
             {
                 if (item is DesignerToolStripControlHost)
                 {
                     this.dropDown.Items.Remove(item);
                 }
             }
         }
         ToolStripMenuItemDesigner designer2 = (ToolStripMenuItemDesigner)this.host.GetDesigner(this.menuItem);
         BehaviorService           service   = (BehaviorService)this.GetService(typeof(BehaviorService));
         if (service != null)
         {
             if ((designer2 != null) && (parent != null))
             {
                 Rectangle parentBounds = service.ControlRectInAdornerWindow(parent);
                 if (ToolStripDesigner.IsGlyphTotallyVisible(service.ControlRectInAdornerWindow(this.designMenu), parentBounds))
                 {
                     designer2.InitializeDropDown();
                 }
             }
             if (this.dummyToolStripGlyph == null)
             {
                 Point     pos    = service.ControlToAdornerWindow(this.designMenu);
                 Rectangle bounds = this.designMenu.Bounds;
                 bounds.Offset(pos);
                 this.dummyToolStripGlyph = new ControlBodyGlyph(bounds, Cursor.Current, this.menuItem, new ContextMenuStripBehavior(this.menuItem));
                 SelectionManager manager = (SelectionManager)this.GetService(typeof(SelectionManager));
                 if (manager != null)
                 {
                     manager.BodyGlyphAdorner.Glyphs.Insert(0, this.dummyToolStripGlyph);
                 }
             }
             ToolStripKeyboardHandlingService service2 = (ToolStripKeyboardHandlingService)this.GetService(typeof(ToolStripKeyboardHandlingService));
             if (service2 != null)
             {
                 int num = this.dropDown.Items.Count - 1;
                 if (num >= 0)
                 {
                     service2.SelectedDesignerControl = this.dropDown.Items[num];
                 }
             }
         }
     }
 }
        private bool IsContextMenuStripItemSelected(ISelectionService selectionService)
        {
            bool flag = false;

            if (this.menuItem != null)
            {
                ToolStripDropDown dropDown         = null;
                IComponent        primarySelection = (IComponent)selectionService.PrimarySelection;
                if ((primarySelection == null) && this.dropDown.Visible)
                {
                    ToolStripKeyboardHandlingService service = (ToolStripKeyboardHandlingService)this.GetService(typeof(ToolStripKeyboardHandlingService));
                    if (service != null)
                    {
                        primarySelection = (IComponent)service.SelectedDesignerControl;
                    }
                }
                if (primarySelection is ToolStripDropDownItem)
                {
                    ToolStripDropDownItem item = primarySelection as ToolStripDropDownItem;
                    if ((item != null) && (item == this.menuItem))
                    {
                        dropDown = this.menuItem.DropDown;
                    }
                    else
                    {
                        ToolStripMenuItemDesigner designer = (ToolStripMenuItemDesigner)this.host.GetDesigner(primarySelection);
                        if (designer != null)
                        {
                            dropDown = designer.GetFirstDropDown((ToolStripDropDownItem)primarySelection);
                        }
                    }
                }
                else if (primarySelection is ToolStripItem)
                {
                    ToolStripDropDown currentParent = ((ToolStripItem)primarySelection).GetCurrentParent() as ToolStripDropDown;
                    if (currentParent == null)
                    {
                        currentParent = ((ToolStripItem)primarySelection).Owner as ToolStripDropDown;
                    }
                    if ((currentParent != null) && currentParent.Visible)
                    {
                        ToolStripItem ownerItem = currentParent.OwnerItem;
                        if ((ownerItem != null) && (ownerItem == this.menuItem))
                        {
                            dropDown = this.menuItem.DropDown;
                        }
                        else
                        {
                            ToolStripMenuItemDesigner designer2 = (ToolStripMenuItemDesigner)this.host.GetDesigner(ownerItem);
                            if (designer2 != null)
                            {
                                dropDown = designer2.GetFirstDropDown((ToolStripDropDownItem)ownerItem);
                            }
                        }
                    }
                }
                if ((dropDown != null) && (dropDown.OwnerItem == this.menuItem))
                {
                    flag = true;
                }
            }
            return(flag);
        }
        public override bool OnMouseDown(Glyph g, MouseButtons button, Point mouseLoc)
        {
            ToolStripItemGlyph glyph            = g as ToolStripItemGlyph;
            ToolStripItem      item             = glyph.Item;
            ISelectionService  selectionService = this.GetSelectionService(item);
            BehaviorService    behaviorService  = this.GetBehaviorService(item);
            ToolStripKeyboardHandlingService keyBoardHandlingService = this.GetKeyBoardHandlingService(item);

            if (((button != MouseButtons.Left) || (keyBoardHandlingService == null)) || (!keyBoardHandlingService.TemplateNodeActive || !keyBoardHandlingService.ActiveTemplateNode.IsSystemContextMenuDisplayed))
            {
                IDesignerHost service          = (IDesignerHost)item.Site.GetService(typeof(IDesignerHost));
                ToolStripItem primarySelection = selectionService.PrimarySelection as ToolStripItem;
                ICollection   c = null;
                if (selectionService != null)
                {
                    c = selectionService.GetSelectedComponents();
                }
                ArrayList originalSelComps = new ArrayList(c);
                if (((originalSelComps.Count == 0) && (keyBoardHandlingService != null)) && (keyBoardHandlingService.SelectedDesignerControl != null))
                {
                    originalSelComps.Add(keyBoardHandlingService.SelectedDesignerControl);
                }
                if (keyBoardHandlingService != null)
                {
                    keyBoardHandlingService.SelectedDesignerControl = null;
                    if (keyBoardHandlingService.TemplateNodeActive)
                    {
                        keyBoardHandlingService.ActiveTemplateNode.CommitAndSelect();
                        if ((primarySelection != null) && (primarySelection == item))
                        {
                            selectionService.SetSelectedComponents(null, SelectionTypes.Replace);
                        }
                    }
                }
                if ((selectionService == null) || this.MouseHandlerPresent(item))
                {
                    return(false);
                }
                if ((glyph != null) && (button == MouseButtons.Left))
                {
                    ToolStripItem oldSelection = selectionService.PrimarySelection as ToolStripItem;
                    this.SetParentDesignerValuesForDragDrop(item, true, mouseLoc);
                    if ((oldSelection != null) && (oldSelection == item))
                    {
                        if (oldSelection != null)
                        {
                            ToolStripItemDesigner itemDesigner = glyph.ItemDesigner;
                            if ((itemDesigner != null) && itemDesigner.IsEditorActive)
                            {
                                return(false);
                            }
                        }
                        if ((Control.ModifierKeys & (Keys.Control | Keys.Shift)) > Keys.None)
                        {
                            selectionService.SetSelectedComponents(new IComponent[] { oldSelection }, SelectionTypes.Remove);
                            return(false);
                        }
                        if (oldSelection is ToolStripMenuItem)
                        {
                            this._timer          = new Timer();
                            this._timer.Interval = SystemInformation.DoubleClickTime;
                            this._timer.Tick    += new EventHandler(this.OnDoubleClickTimerTick);
                            this._timer.Enabled  = true;
                            this.selectedGlyph   = glyph;
                        }
                    }
                    else
                    {
                        bool flag2 = (Control.ModifierKeys & Keys.Shift) > Keys.None;
                        if (!selectionService.GetComponentSelected(item))
                        {
                            this.mouseUpFired     = false;
                            this.doubleClickFired = false;
                            if ((flag2 && (oldSelection != null)) && this.CommonParent(oldSelection, item))
                            {
                                ToolStrip owner = null;
                                if (item.IsOnOverflow)
                                {
                                    owner = item.Owner;
                                }
                                else
                                {
                                    owner = item.GetCurrentParent();
                                }
                                int num  = Math.Min(owner.Items.IndexOf(oldSelection), owner.Items.IndexOf(item));
                                int num2 = Math.Max(owner.Items.IndexOf(oldSelection), owner.Items.IndexOf(item));
                                int num3 = (num2 - num) + 1;
                                if (num3 == 2)
                                {
                                    selectionService.SetSelectedComponents(new IComponent[] { item });
                                }
                                else
                                {
                                    object[] components = new object[num3];
                                    int      num4       = 0;
                                    for (int i = num; i <= num2; i++)
                                    {
                                        components[num4++] = owner.Items[i];
                                    }
                                    selectionService.SetSelectedComponents(new IComponent[] { owner }, SelectionTypes.Replace);
                                    ToolStripDesigner.shiftState = true;
                                    selectionService.SetSelectedComponents(components, SelectionTypes.Replace);
                                }
                            }
                            else
                            {
                                if (item.IsOnDropDown && ToolStripDesigner.shiftState)
                                {
                                    ToolStripDesigner.shiftState = false;
                                    if (behaviorService != null)
                                    {
                                        behaviorService.Invalidate(item.Owner.Bounds);
                                    }
                                }
                                selectionService.SetSelectedComponents(new IComponent[] { item }, SelectionTypes.Auto);
                            }
                            if (keyBoardHandlingService != null)
                            {
                                keyBoardHandlingService.ShiftPrimaryItem = item;
                            }
                        }
                        else if (flag2 || ((Control.ModifierKeys & Keys.Control) > Keys.None))
                        {
                            selectionService.SetSelectedComponents(new IComponent[] { item }, SelectionTypes.Remove);
                        }
                    }
                }
                if (((glyph != null) && (button == MouseButtons.Right)) && !selectionService.GetComponentSelected(item))
                {
                    selectionService.SetSelectedComponents(new IComponent[] { item });
                }
                ToolStripDesignerUtils.InvalidateSelection(originalSelComps, item, item.Site, false);
            }
            return(false);
        }
        public override void OnDragDrop(Glyph g, DragEventArgs e)
        {
            ToolStripItem dragItem = ToolStripDesigner.dragItem;

            if ((e.Data is ToolStripItemDataObject) && (dragItem != null))
            {
                ToolStripItemDataObject data             = (ToolStripItemDataObject)e.Data;
                ToolStripItem           primarySelection = data.PrimarySelection;
                IDesignerHost           host             = (IDesignerHost)dragItem.Site.GetService(typeof(IDesignerHost));
                if ((dragItem != primarySelection) && (host != null))
                {
                    string    str;
                    ArrayList dragComponents = data.DragComponents;
                    ToolStrip currentParent  = dragItem.GetCurrentParent();
                    int       index          = -1;
                    bool      flag           = e.Effect == DragDropEffects.Copy;
                    if (dragComponents.Count == 1)
                    {
                        string componentName = TypeDescriptor.GetComponentName(dragComponents[0]);
                        if ((componentName == null) || (componentName.Length == 0))
                        {
                            componentName = dragComponents[0].GetType().Name;
                        }
                        str = System.Design.SR.GetString(flag ? "BehaviorServiceCopyControl" : "BehaviorServiceMoveControl", new object[] { componentName });
                    }
                    else
                    {
                        str = System.Design.SR.GetString(flag ? "BehaviorServiceCopyControls" : "BehaviorServiceMoveControls", new object[] { dragComponents.Count });
                    }
                    DesignerTransaction transaction = host.CreateTransaction(str);
                    try
                    {
                        IComponentChangeService service = (IComponentChangeService)dragItem.Site.GetService(typeof(IComponentChangeService));
                        if (service != null)
                        {
                            ToolStripDropDown down = currentParent as ToolStripDropDown;
                            if (down != null)
                            {
                                ToolStripItem ownerItem = down.OwnerItem;
                                service.OnComponentChanging(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"]);
                            }
                            else
                            {
                                service.OnComponentChanging(currentParent, TypeDescriptor.GetProperties(currentParent)["Items"]);
                            }
                        }
                        if (flag)
                        {
                            if (primarySelection != null)
                            {
                                index = dragComponents.IndexOf(primarySelection);
                            }
                            ToolStripKeyboardHandlingService keyBoardHandlingService = this.GetKeyBoardHandlingService(primarySelection);
                            if (keyBoardHandlingService != null)
                            {
                                keyBoardHandlingService.CopyInProgress = true;
                            }
                            dragComponents = DesignerUtils.CopyDragObjects(dragComponents, dragItem.Site) as ArrayList;
                            if (keyBoardHandlingService != null)
                            {
                                keyBoardHandlingService.CopyInProgress = false;
                            }
                            if (index != -1)
                            {
                                primarySelection = dragComponents[index] as ToolStripItem;
                            }
                        }
                        if ((e.Effect == DragDropEffects.Move) || flag)
                        {
                            ISelectionService selectionService = this.GetSelectionService(dragItem);
                            if (selectionService != null)
                            {
                                if (currentParent is ToolStripOverflow)
                                {
                                    currentParent = ((ToolStripOverflow)currentParent).OwnerItem.Owner;
                                }
                                int num2 = currentParent.Items.IndexOf(ToolStripDesigner.dragItem);
                                if (num2 != -1)
                                {
                                    int num3 = 0;
                                    if (primarySelection != null)
                                    {
                                        num3 = currentParent.Items.IndexOf(primarySelection);
                                    }
                                    if ((num3 != -1) && (num2 > num3))
                                    {
                                        num2--;
                                    }
                                    foreach (ToolStripItem item4 in dragComponents)
                                    {
                                        currentParent.Items.Insert(num2, item4);
                                    }
                                }
                                selectionService.SetSelectedComponents(new IComponent[] { primarySelection }, SelectionTypes.Click | SelectionTypes.Replace);
                            }
                        }
                        if (service != null)
                        {
                            ToolStripDropDown down2 = currentParent as ToolStripDropDown;
                            if (down2 != null)
                            {
                                ToolStripItem component = down2.OwnerItem;
                                service.OnComponentChanged(component, TypeDescriptor.GetProperties(component)["DropDownItems"], null, null);
                            }
                            else
                            {
                                service.OnComponentChanged(currentParent, TypeDescriptor.GetProperties(currentParent)["Items"], null, null);
                            }
                            if (flag)
                            {
                                if (down2 != null)
                                {
                                    ToolStripItem item6 = down2.OwnerItem;
                                    service.OnComponentChanging(item6, TypeDescriptor.GetProperties(item6)["DropDownItems"]);
                                    service.OnComponentChanged(item6, TypeDescriptor.GetProperties(item6)["DropDownItems"], null, null);
                                }
                                else
                                {
                                    service.OnComponentChanging(currentParent, TypeDescriptor.GetProperties(currentParent)["Items"]);
                                    service.OnComponentChanged(currentParent, TypeDescriptor.GetProperties(currentParent)["Items"], null, null);
                                }
                            }
                        }
                        foreach (ToolStripItem item7 in dragComponents)
                        {
                            if (item7 is ToolStripDropDownItem)
                            {
                                ToolStripMenuItemDesigner designer = host.GetDesigner(item7) as ToolStripMenuItemDesigner;
                                if (designer != null)
                                {
                                    designer.InitializeDropDown();
                                }
                            }
                            ToolStripDropDown down3 = item7.GetCurrentParent() as ToolStripDropDown;
                            if ((down3 != null) && !(down3 is ToolStripOverflow))
                            {
                                ToolStripDropDownItem item8 = down3.OwnerItem as ToolStripDropDownItem;
                                if (item8 != null)
                                {
                                    ToolStripMenuItemDesigner designer2 = host.GetDesigner(item8) as ToolStripMenuItemDesigner;
                                    if (designer2 != null)
                                    {
                                        designer2.InitializeBodyGlyphsForItems(false, item8);
                                        designer2.InitializeBodyGlyphsForItems(true, item8);
                                    }
                                }
                            }
                        }
                        BehaviorService behaviorService = this.GetBehaviorService(dragItem);
                        if (behaviorService != null)
                        {
                            behaviorService.SyncSelection();
                        }
                    }
                    catch (Exception exception)
                    {
                        if (transaction != null)
                        {
                            transaction.Cancel();
                            transaction = null;
                        }
                        if (System.Windows.Forms.ClientUtils.IsCriticalException(exception))
                        {
                            throw;
                        }
                    }
                    finally
                    {
                        if (transaction != null)
                        {
                            transaction.Commit();
                            transaction = null;
                        }
                    }
                }
            }
        }