private void HideMenu()
 {
     if (this.menuItem != null)
     {
         if ((this.parentMenu != null) && (this.parentFormDesigner != null))
         {
             this.parentFormDesigner.Menu = this.parentMenu;
         }
         this.selected = false;
         if (this.host.RootComponent is Control)
         {
             this.menuItem.DropDown.AutoClose = true;
             this.menuItem.HideDropDown();
             this.menuItem.Visible   = false;
             this.designMenu.Visible = false;
             ToolStripAdornerWindowService service = (ToolStripAdornerWindowService)this.GetService(typeof(ToolStripAdornerWindowService));
             if (service != null)
             {
                 service.Invalidate();
             }
             if (((BehaviorService)this.GetService(typeof(BehaviorService))) != null)
             {
                 if (this.dummyToolStripGlyph != null)
                 {
                     SelectionManager manager = (SelectionManager)this.GetService(typeof(SelectionManager));
                     if (manager != null)
                     {
                         if (manager.BodyGlyphAdorner.Glyphs.Contains(this.dummyToolStripGlyph))
                         {
                             manager.BodyGlyphAdorner.Glyphs.Remove(this.dummyToolStripGlyph);
                         }
                         manager.Refresh();
                     }
                 }
                 this.dummyToolStripGlyph = null;
             }
             if (this.menuItem != null)
             {
                 ToolStripMenuItemDesigner designer = this.host.GetDesigner(this.menuItem) as ToolStripMenuItemDesigner;
                 if (designer != null)
                 {
                     designer.UnHookEvents();
                     designer.RemoveTypeHereNode(this.menuItem);
                 }
             }
         }
     }
 }
 public void HandleKeyChar()
 {
     if ((this.toolDesigner != null) || (this.toolItemDesigner != null))
     {
         if (this.toolDesigner != null)
         {
             this.toolDesigner.ShowEditNode(false);
         }
         else if (this.toolItemDesigner != null)
         {
             ToolStripMenuItemDesigner toolItemDesigner = this.toolItemDesigner as ToolStripMenuItemDesigner;
             if (toolItemDesigner != null)
             {
                 ISelectionService service = (ISelectionService)this.sp.GetService(typeof(ISelectionService));
                 if (service != null)
                 {
                     object primarySelection = service.PrimarySelection;
                     if (primarySelection == null)
                     {
                         primarySelection = this.ToolStripKeyBoardService.SelectedDesignerControl;
                     }
                     if ((primarySelection is DesignerToolStripControlHost) || (primarySelection is ToolStripDropDown))
                     {
                         toolItemDesigner.EditTemplateNode(false);
                     }
                     else
                     {
                         toolItemDesigner.ShowEditNode(false);
                     }
                 }
             }
             else
             {
                 this.toolItemDesigner.ShowEditNode(false);
             }
         }
     }
 }
예제 #3
0
        private void InsertToolStripItem(System.Type t)
        {
            IDesignerHost       host        = (IDesignerHost)this.serviceProvider.GetService(typeof(IDesignerHost));
            ToolStrip           parentTool  = this.ParentTool;
            int                 index       = parentTool.Items.IndexOf(this.currentItem);
            IDesigner           designer    = null;
            DesignerTransaction transaction = host.CreateTransaction(System.Design.SR.GetString("ToolStripAddingItem"));

            try
            {
                ToolStripDesigner._autoAddNewItems = false;
                IComponent component = host.CreateComponent(t);
                designer = host.GetDesigner(component);
                if (designer is ComponentDesigner)
                {
                    ((ComponentDesigner)designer).InitializeNewComponent(null);
                }
                if (((component is ToolStripButton) || (component is ToolStripSplitButton)) || (component is ToolStripDropDownButton))
                {
                    Image image = null;
                    try
                    {
                        image = new Bitmap(typeof(ToolStripButton), "blank.bmp");
                    }
                    catch (Exception exception)
                    {
                        if (System.Windows.Forms.ClientUtils.IsCriticalException(exception))
                        {
                            throw;
                        }
                    }
                    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(component)["Image"];
                    if ((descriptor != null) && (image != null))
                    {
                        descriptor.SetValue(component, image);
                    }
                    PropertyDescriptor descriptor2 = TypeDescriptor.GetProperties(component)["DisplayStyle"];
                    if (descriptor2 != null)
                    {
                        descriptor2.SetValue(component, ToolStripItemDisplayStyle.Image);
                    }
                    PropertyDescriptor descriptor3 = TypeDescriptor.GetProperties(component)["ImageTransparentColor"];
                    if (descriptor3 != null)
                    {
                        descriptor3.SetValue(component, Color.Magenta);
                    }
                }
                parentTool.Items.Insert(index, (ToolStripItem)component);
                ISelectionService service = (ISelectionService)this.serviceProvider.GetService(typeof(ISelectionService));
                if (service != null)
                {
                    service.SetSelectedComponents(new object[] { component }, SelectionTypes.Replace);
                }
            }
            catch (Exception exception2)
            {
                if (transaction != null)
                {
                    transaction.Cancel();
                    transaction = null;
                }
                if (System.Windows.Forms.ClientUtils.IsCriticalException(exception2))
                {
                    throw;
                }
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Commit();
                    transaction = null;
                }
                ToolStripDesigner._autoAddNewItems = true;
                ToolStripDropDown down = parentTool as ToolStripDropDown;
                if ((down != null) && down.Visible)
                {
                    ToolStripDropDownItem ownerItem = down.OwnerItem as ToolStripDropDownItem;
                    if (ownerItem != null)
                    {
                        ToolStripMenuItemDesigner designer2 = host.GetDesigner(ownerItem) as ToolStripMenuItemDesigner;
                        if (designer2 != null)
                        {
                            designer2.ResetGlyphs(ownerItem);
                        }
                    }
                }
            }
        }
 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 OnMouseMove(Glyph g, MouseButtons button, Point mouseLoc)
        {
            bool flag = false;
            ToolStripItemGlyph glyph            = g as ToolStripItemGlyph;
            ToolStripItem      item             = glyph.Item;
            ISelectionService  selectionService = this.GetSelectionService(item);

            if (((selectionService != null) && (item.Site != null)) && !this.MouseHandlerPresent(item))
            {
                if (!selectionService.GetComponentSelected(item))
                {
                    this.PaintInsertionMark(item);
                    flag = false;
                }
                if (((button != MouseButtons.Left) || (glyph == null)) || ((glyph.ItemDesigner == null) || glyph.ItemDesigner.IsEditorActive))
                {
                    return(flag);
                }
                Rectangle     empty   = Rectangle.Empty;
                IDesignerHost service = (IDesignerHost)item.Site.GetService(typeof(IDesignerHost));
                if ((item.Placement == ToolStripItemPlacement.Overflow) || ((item.Placement == ToolStripItemPlacement.Main) && !item.IsOnDropDown))
                {
                    ToolStrip         mainToolStrip = glyph.ItemDesigner.GetMainToolStrip();
                    ToolStripDesigner designer      = service.GetDesigner(mainToolStrip) as ToolStripDesigner;
                    if (designer != null)
                    {
                        empty = designer.DragBoxFromMouseDown;
                    }
                }
                else if (item.IsOnDropDown)
                {
                    ToolStripDropDown owner = item.Owner as ToolStripDropDown;
                    if (owner != null)
                    {
                        ToolStripItem         ownerItem = owner.OwnerItem;
                        ToolStripItemDesigner designer3 = service.GetDesigner(ownerItem) as ToolStripItemDesigner;
                        if (designer3 != null)
                        {
                            empty = designer3.dragBoxFromMouseDown;
                        }
                    }
                }
                if (!(empty != Rectangle.Empty) || empty.Contains(mouseLoc.X, mouseLoc.Y))
                {
                    return(flag);
                }
                if (this._timer != null)
                {
                    this._timer.Enabled = false;
                    this._timer.Tick   -= new EventHandler(this.OnDoubleClickTimerTick);
                    this._timer.Dispose();
                    this._timer = null;
                }
                try
                {
                    ArrayList dragComponents = new ArrayList();
                    foreach (IComponent component in selectionService.GetSelectedComponents())
                    {
                        ToolStripItem item3 = component as ToolStripItem;
                        if (item3 != null)
                        {
                            dragComponents.Add(item3);
                        }
                    }
                    ToolStripItem primarySelection = selectionService.PrimarySelection as ToolStripItem;
                    if (primarySelection != null)
                    {
                        ToolStrip strip2             = primarySelection.Owner;
                        ToolStripItemDataObject data = new ToolStripItemDataObject(dragComponents, primarySelection, strip2);
                        this.DropSource.QueryContinueDrag += new QueryContinueDragEventHandler(this.QueryContinueDrag);
                        ToolStripDropDownItem item5 = item as ToolStripDropDownItem;
                        if (item5 != null)
                        {
                            ToolStripMenuItemDesigner designer4 = service.GetDesigner(item5) as ToolStripMenuItemDesigner;
                            if (designer4 != null)
                            {
                                designer4.InitializeBodyGlyphsForItems(false, item5);
                                item5.HideDropDown();
                            }
                        }
                        else if (item.IsOnDropDown && !item.IsOnOverflow)
                        {
                            ToolStripDropDown     currentParent = item.GetCurrentParent() as ToolStripDropDown;
                            ToolStripDropDownItem item6         = currentParent.OwnerItem as ToolStripDropDownItem;
                            selectionService.SetSelectedComponents(new IComponent[] { item6 }, SelectionTypes.Replace);
                        }
                        this.DropSource.DoDragDrop(data, DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll);
                    }
                }
                finally
                {
                    this.DropSource.QueryContinueDrag -= new QueryContinueDragEventHandler(this.QueryContinueDrag);
                    this.SetParentDesignerValuesForDragDrop(item, false, Point.Empty);
                    ToolStripDesigner.dragItem = null;
                    this.dropSource            = null;
                }
            }
            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;
                        }
                    }
                }
            }
        }
예제 #8
0
        private void OnSelectionChanged(object sender, EventArgs e)
        {
            ISelectionService service = sender as ISelectionService;

            if (service != null)
            {
                System.Windows.Forms.ToolStripItem primarySelection = service.PrimarySelection as System.Windows.Forms.ToolStripItem;
                System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject accessibilityObject = this.ToolStripItem.AccessibilityObject as System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject;
                if (accessibilityObject != null)
                {
                    accessibilityObject.AddState(AccessibleStates.None);
                    ToolStrip mainToolStrip = this.GetMainToolStrip();
                    if (service.GetComponentSelected(this.ToolStripItem))
                    {
                        ToolStrip immediateParent = this.ImmediateParent as ToolStrip;
                        int       index           = 0;
                        if (immediateParent != null)
                        {
                            index = immediateParent.Items.IndexOf(primarySelection);
                        }
                        accessibilityObject.AddState(AccessibleStates.Selected);
                        if (mainToolStrip != null)
                        {
                            System.Design.UnsafeNativeMethods.NotifyWinEvent(0x8007, new HandleRef(immediateParent, immediateParent.Handle), -4, index + 1);
                        }
                        if (primarySelection == this.ToolStripItem)
                        {
                            accessibilityObject.AddState(AccessibleStates.Focused);
                            if (mainToolStrip != null)
                            {
                                System.Design.UnsafeNativeMethods.NotifyWinEvent(0x8005, new HandleRef(immediateParent, immediateParent.Handle), -4, index + 1);
                            }
                        }
                    }
                }
                if ((((primarySelection != null) && (this.ToolStripItem != null)) && (primarySelection.IsOnDropDown && this.ToolStripItem.Equals(primarySelection))) && !(this.ToolStripItem is ToolStripMenuItem))
                {
                    IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
                    if (host != null)
                    {
                        ToolStripDropDown owner = primarySelection.Owner as ToolStripDropDown;
                        if ((owner != null) && !owner.Visible)
                        {
                            ToolStripDropDownItem ownerItem = owner.OwnerItem as ToolStripDropDownItem;
                            if (ownerItem != null)
                            {
                                ToolStripMenuItemDesigner designer = (ToolStripMenuItemDesigner)host.GetDesigner(ownerItem);
                                if (designer != null)
                                {
                                    designer.InitializeDropDown();
                                }
                                SelectionManager manager = (SelectionManager)this.GetService(typeof(SelectionManager));
                                if (manager != null)
                                {
                                    manager.Refresh();
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #9
0
        internal virtual System.Windows.Forms.ToolStripItem MorphCurrentItem(System.Type t)
        {
            System.Windows.Forms.ToolStripItem component = null;
            IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));

            if (host != null)
            {
                DesignerTransaction transaction     = host.CreateTransaction(System.Design.SR.GetString("ToolStripMorphingItemTransaction"));
                ToolStrip           immediateParent = (ToolStrip)this.ImmediateParent;
                if (immediateParent is ToolStripOverflow)
                {
                    immediateParent = this.ToolStripItem.Owner;
                }
                ToolStripMenuItemDesigner designer = null;
                int    index = immediateParent.Items.IndexOf(this.ToolStripItem);
                string name  = this.ToolStripItem.Name;
                System.Windows.Forms.ToolStripItem ownerItem = null;
                if (this.ToolStripItem.IsOnDropDown)
                {
                    ToolStripDropDown down = this.ImmediateParent as ToolStripDropDown;
                    if (down != null)
                    {
                        ownerItem = down.OwnerItem;
                        if (ownerItem != null)
                        {
                            designer = (ToolStripMenuItemDesigner)host.GetDesigner(ownerItem);
                        }
                    }
                }
                try
                {
                    ToolStripDesigner._autoAddNewItems = false;
                    ComponentSerializationService service = this.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;
                    SerializationStore            store   = null;
                    if (service != null)
                    {
                        store = service.CreateStore();
                        service.Serialize(store, base.Component);
                        SerializationStore    store2        = null;
                        ToolStripDropDownItem toolStripItem = this.ToolStripItem as ToolStripDropDownItem;
                        if ((toolStripItem != null) && typeof(ToolStripDropDownItem).IsAssignableFrom(t))
                        {
                            toolStripItem.HideDropDown();
                            store2 = service.CreateStore();
                            this.SerializeDropDownItems(toolStripItem, ref store2, service);
                            store2.Close();
                        }
                        store.Close();
                        IComponentChangeService service2 = (IComponentChangeService)this.GetService(typeof(IComponentChangeService));
                        if (service2 != null)
                        {
                            if (immediateParent.Site != null)
                            {
                                service2.OnComponentChanging(immediateParent, TypeDescriptor.GetProperties(immediateParent)["Items"]);
                            }
                            else if (ownerItem != null)
                            {
                                service2.OnComponentChanging(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"]);
                                service2.OnComponentChanged(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"], null, null);
                            }
                        }
                        this.FireComponentChanging(toolStripItem);
                        immediateParent.Items.Remove(this.ToolStripItem);
                        host.DestroyComponent(this.ToolStripItem);
                        System.Windows.Forms.ToolStripItem item4 = (System.Windows.Forms.ToolStripItem)host.CreateComponent(t, name);
                        if ((item4 is ToolStripDropDownItem) && (store2 != null))
                        {
                            service.Deserialize(store2);
                        }
                        service.DeserializeTo(store, host.Container, false, true);
                        component = (System.Windows.Forms.ToolStripItem)host.Container.Components[name];
                        if ((component.Image == null) && (component is ToolStripButton))
                        {
                            Image image = null;
                            try
                            {
                                image = new Bitmap(typeof(ToolStripButton), "blank.bmp");
                            }
                            catch (Exception exception)
                            {
                                if (System.Windows.Forms.ClientUtils.IsCriticalException(exception))
                                {
                                    throw;
                                }
                            }
                            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(component)["Image"];
                            if ((descriptor != null) && (image != null))
                            {
                                descriptor.SetValue(component, image);
                            }
                            PropertyDescriptor descriptor2 = TypeDescriptor.GetProperties(component)["DisplayStyle"];
                            if (descriptor2 != null)
                            {
                                descriptor2.SetValue(component, ToolStripItemDisplayStyle.Image);
                            }
                            PropertyDescriptor descriptor3 = TypeDescriptor.GetProperties(component)["ImageTransparentColor"];
                            if (descriptor3 != null)
                            {
                                descriptor3.SetValue(component, Color.Magenta);
                            }
                        }
                        immediateParent.Items.Insert(index, component);
                        if (service2 != null)
                        {
                            if (immediateParent.Site != null)
                            {
                                service2.OnComponentChanged(immediateParent, TypeDescriptor.GetProperties(immediateParent)["Items"], null, null);
                            }
                            else if (ownerItem != null)
                            {
                                service2.OnComponentChanging(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"]);
                                service2.OnComponentChanged(ownerItem, TypeDescriptor.GetProperties(ownerItem)["DropDownItems"], null, null);
                            }
                        }
                        this.FireComponentChanged(toolStripItem);
                        if (component.IsOnDropDown && (designer != null))
                        {
                            designer.RemoveItemBodyGlyph(component);
                            designer.AddItemBodyGlyph(component);
                        }
                        ToolStripDesigner._autoAddNewItems = true;
                        if (component != null)
                        {
                            if (component is ToolStripSeparator)
                            {
                                immediateParent.PerformLayout();
                            }
                            BehaviorService service3 = (BehaviorService)component.Site.GetService(typeof(BehaviorService));
                            if (service3 != null)
                            {
                                service3.Invalidate();
                            }
                            ISelectionService service4 = (ISelectionService)component.Site.GetService(typeof(ISelectionService));
                            if (service4 != null)
                            {
                                service4.SetSelectedComponents(new object[] { component }, SelectionTypes.Replace);
                            }
                        }
                    }
                    return(component);
                }
                catch
                {
                    host.Container.Add(this.ToolStripItem);
                    immediateParent.Items.Insert(index, this.ToolStripItem);
                    if (transaction != null)
                    {
                        transaction.Cancel();
                        transaction = null;
                    }
                }
                finally
                {
                    if (transaction != null)
                    {
                        transaction.Commit();
                        transaction = null;
                    }
                }
            }
            return(component);
        }