Beispiel #1
0
        private XlCommandBarControl ReturnHelper(MsoControlType type, object comRef)
        {
            switch (type)
            {
            case MsoControlType.msoControlButton:

                XlCommandBarButton newButton = new XlCommandBarButton(this, comRef);
                ListChildReferences.Add(newButton);
                return(newButton);

            case MsoControlType.msoControlPopup:

                XlCommandBarPopup newPopup = new XlCommandBarPopup(this, comRef);
                ListChildReferences.Add(newPopup);
                return(newPopup);

            case MsoControlType.msoControlComboBox:

                XlCommandBarComboBox newBox = new XlCommandBarComboBox(this, comRef);
                ListChildReferences.Add(newBox);
                return(newBox);

            default:

                XlCommandBarControl newClass = new XlCommandBarControl(this, comRef);
                ListChildReferences.Add(newClass);
                return(newClass);
            }
        }
Beispiel #2
0
        /// <summary>
        ///		Create a new control of a particular type.
        /// </summary>
        /// <param name="type">The type of control to create.</param>
        /// <returns>One of ToolbarPopup, ToolbarButton and ToolbarComboBox depending on the type given.</returns>
        private ToolbarControl CreateControl(MsoControlType type)
        {
            ToolbarControl control = null;

            switch (type)
            {
            case MsoControlType.msoControlPopup:
                control = new ToolbarPopup(this);
                break;

            case MsoControlType.msoControlButton:
                control = new ToolbarButton(this);
                break;

            case MsoControlType.msoControlEdit:
            case MsoControlType.msoControlDropdown:
            case MsoControlType.msoControlComboBox:
                control = new ToolbarComboBox(this, type);
                break;
            }
            if (control != null)
            {
                control.InternalTag = InternalTag + "_" + NEXT_TAG;
                NEXT_TAG++;
            }
            return(control);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the menu bar with the specified english name.
        /// </summary>
        /// <seealso cref="http://www.mztools.com/articles/2007/mz2007002.aspx">HOWTO: Locate commandbars in international versions of Visual Studio</seealso>
        /// <param name="application">The visual studio application object.</param>
        /// <param name="menuBarName">Then english name of the menu bar, e.g. Tools, Data, Window...</param>
        /// <returns>
        /// The menu bar with the specified english name or <c>null</c>
        /// if the menu bar was not found.
        /// </returns>
        public static bool TryGetMenuBar(this _DTE application, string menuBarName, out CommandBar menuBar)
        {
            CommandBars commandBars = (CommandBars)application.CommandBars;
            CommandBar  mainMenuBar = (CommandBar)commandBars["MenuBar"];

            foreach (CommandBarControl ctrl in mainMenuBar.Controls)
            {
                MsoControlType type = ctrl.Type;
                if (type != MsoControlType.msoControlPopup)
                {
                    continue;
                }

                CommandBarPopup ctrlPopup = (CommandBarPopup)ctrl;
                menuBar = ctrlPopup.CommandBar;
                string subMenuName = menuBar.Name;
                if (subMenuName.Equals(menuBarName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(true);
                }
            }
            Debug.WriteLine("VsApplicationExtensions.GetMenuBar - Menu bar not found: " + menuBarName);
            menuBar = null;
            return(false);
        }
Beispiel #4
0
        internal CommandBarControl FindOrAdd(MsoControlType controlType, string name, object Id, object Parameter, object Before, object Temporary)
        {
            if (name != null)
            {
                // Try to find an existing control with this name
                string findName = name.Replace("&", "");
                for (int i = 1; i <= Count(); i++)
                {
                    CommandBarControl control = this[i];
                    string            caption = control.Caption;
                    if (!String.IsNullOrEmpty(caption))
                    {
                        if (caption.Replace("&", "") == findName)
                        {
                            return(control);
                        }
                    }
                }
            }

            object /*CommandBarControl*/ newControl = _type.InvokeMember("Add", BindingFlags.InvokeMethod, null, _object,
                                                                         new object[] { controlType, Id, Parameter, Before, Temporary });

            return(CommandBarControl.CreateCommandBarControl(controlType, newControl));
        }
Beispiel #5
0
        /// <summary>
        ///		Adds a new control to the popup menu.
        /// </summary>
        /// <param name="type">The type of control to add.</param>
        /// <returns>The newly created control.</returns>
        public ToolbarControl Add(MsoControlType type)
        {
            ToolbarControl control = CreateControl(type);

            controls.Add(control);
            controlMap[control.InternalTag] = control;
            return(control);
        }
Beispiel #6
0
 /// <summary>
 /// constructor 2
 /// </summary>
 /// <param name="caption"></param>
 /// <param name="controlType"></param>
 /// <param name="key"></param>
 /// <param name="position"></param>
 /// <param name="tooltip"></param>
 public MenuAttribute(string caption, MsoControlType controlType, string key, int position, string tooltip)
 {
     Caption = caption;
     ControlType = controlType;
     Key = key;
     Position = position;
     Tooltip = tooltip;
 }
Beispiel #7
0
        /// <summary>
        ///		Creates a new control in a particular position on the menu.
        /// </summary>
        /// <param name="index">The position to add the control.</param>
        /// <param name="type">The type of control to add.</param>
        /// <returns>The added control.</returns>
        public ToolbarControl Insert(int index, MsoControlType type)
        {
            ToolbarControl control = CreateControl(type);

            controls.Insert(index, control);
            controlMap[control.InternalTag] = control;
            return(control);
        }
Beispiel #8
0
 /// <summary>
 /// constructor 2
 /// </summary>
 /// <param name="caption"></param>
 /// <param name="controlType"></param>
 /// <param name="key"></param>
 /// <param name="position"></param>
 /// <param name="tooltip"></param>
 public MenuAttribute(string caption, MsoControlType controlType, string key, int position, string tooltip)
 {
     Caption     = caption;
     ControlType = controlType;
     Key         = key;
     Position    = position;
     Tooltip     = tooltip;
 }
Beispiel #9
0
 /// <summary>
 /// constructor 2
 /// </summary>
 /// <param name="key">唯一键值</param>
 /// <param name="controlType">控件类型</param>
 /// <param name="iconResource">图标资源索引</param>
 /// <param name="useMsoButton">使用msobutton</param>
 /// <param name="position">插入位置</param>
 /// <param name="tooltip">提示</param>
 /// <param name="caption">显示文字</param>
 /// <param name="initViewStatus">初始状态</param>
 public CommandAttribute(string key, MsoControlType controlType, int iconResource, bool useMsoButton, int position, string tooltip, string caption, CommandViewStatus initViewStatus)
 {
     Key          = key;
     ControlType  = controlType;
     IconResource = iconResource;
     UseMsoButton = useMsoButton;
     Position     = position;
     Tooltip      = tooltip;
     Caption      = caption;
 }
Beispiel #10
0
 /// <summary>
 /// constructor 2
 /// </summary>
 /// <param name="key">唯一键值</param>
 /// <param name="controlType">控件类型</param>
 /// <param name="iconResource">图标资源索引</param>
 /// <param name="useMsoButton">使用msobutton</param>
 /// <param name="position">插入位置</param>
 /// <param name="tooltip">提示</param>
 /// <param name="caption">显示文字</param>
 /// <param name="initViewStatus">初始状态</param>
 public CommandAttribute(string key, MsoControlType controlType, int iconResource, bool useMsoButton, int position, string tooltip, string caption, CommandViewStatus initViewStatus)
 {
     Key = key;
     ControlType = controlType;
     IconResource = iconResource;
     UseMsoButton = useMsoButton;
     Position = position;
     Tooltip = tooltip;
     Caption = caption;
 }
Beispiel #11
0
        private CommandBarButton DefineShortcutMenu(string menuItemName, int menuItemId)
        {
            MsoControlType   menuItem = MsoControlType.msoControlButton;
            CommandBarButton command  =
                (CommandBarButton)Application.CommandBars["Cell"].Controls.Add(menuItem, missing, missing, 1, true);

            command.Style   = MsoButtonStyle.msoButtonCaption;
            command.Caption = menuItemName;
            command.Tag     = menuItemId.ToString(CultureInfo.InvariantCulture);

            return(command);
        }
Beispiel #12
0
 private void Remove(MsoControlType controlType, object id)
 {
     for (int i = 1; i <= Count(); i++)
     {
         if (!String.IsNullOrEmpty(this[i].Caption))
         {
             if (this[i].Caption.Replace("&", "") == id.ToString().Replace("&", ""))
             {
                 this[i].Delete(true);
             }
         }
     }
 }
        /// <summary> Create a menu and its submenus and manage their actions</summary>
        /// <param name="parentControls">Control where the menu must be inserted</param>
        /// <param name="contextualMenu">Menu to insert</param>
        private void CreateMenus(CommandBarControls parentControls, IContextualMenu contextualMenu)
        {
            if (contextualMenu != null && contextualMenu.Items != null)
            {
                foreach (IContextualPart part in contextualMenu.Items)
                {
                    if (part is IContextualMenu)
                    {
                        IContextualMenu contextualSubMenu = part as IContextualMenu;
                        CommandBarPopup subMenu           = (CommandBarPopup)parentControls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, true);
                        subMenu.Caption    = contextualSubMenu.Caption;
                        subMenu.BeginGroup = contextualSubMenu.BeginGroup;
                        CreateMenus(subMenu.Controls, contextualSubMenu);
                    }
                    else
                    {
                        ContextualMenuItem contextualMenuItem = part as ContextualMenuItem;
                        if (contextualMenuItem != null)
                        {
                            MsoControlType   menuItem         = MsoControlType.msoControlButton;
                            CommandBarButton commandBarButton = (CommandBarButton)parentControls.Add(menuItem, Type.Missing, Type.Missing, Type.Missing, true);
                            commandBarButton.Style      = MsoButtonStyle.msoButtonIconAndCaption;
                            commandBarButton.Caption    = contextualMenuItem.Caption;
                            commandBarButton.BeginGroup = contextualMenuItem.BeginGroup;
                            if (contextualMenuItem.FaceId != 0)
                            {
                                commandBarButton.FaceId = contextualMenuItem.FaceId;
                            }

                            commandBarButton.Click += (CommandBarButton ctrl, ref bool cancel1) =>
                            {
                                try
                                {
                                    if (contextualMenuItem.Action != null)
                                    {
                                        contextualMenuItem.Action();
                                    }
                                }
                                catch (Exception ex)
                                {
                                    string methodName = contextualMenuItem.MethodInfo == null ? string.Empty : contextualMenuItem.MethodInfo.Name;
                                    throw new EtkException($"Contextual menu: '{methodName}' invocation failed: {ex.Message}.");
                                }
                            };
                        }
                    }
                }
            }
        }
Beispiel #14
0
        public override CommandBarControl Add(MsoControlType msoControlButton, object v1, object v2, object missing, bool v3)
        {
            switch (msoControlButton)
            {
            case MsoControlType.msoControlPopup:
                return(new CommandBarImpl());

            case MsoControlType.msoControlButton:
                MenuItem             mi  = DTE2Impl.mainForm.Menu.MenuItems.Add("");
                CommandBarButtonImpl btn = new CommandBarButtonImpl();
                btn.mItem = mi;
                mi.Tag    = btn;
                return(btn);
            }
            return(null);
        }
Beispiel #15
0
 internal static CommandBarControl CreateCommandBarControl(MsoControlType controlType, object commandBarControl)
 {
     if (controlType == MsoControlType.msoControlButton)
     {
         return(new CommandBarButton(commandBarControl));
     }
     if (controlType == MsoControlType.msoControlPopup)
     {
         return(new CommandBarPopup(commandBarControl));
     }
     if (controlType == MsoControlType.msoControlComboBox)
     {
         return(new CommandBarComboBox(commandBarControl));
     }
     return(new CommandBarControl(commandBarControl));
 }
Beispiel #16
0
        public XlCommandBarControl Add(MsoControlType type, object id, object parameter, object before, bool temporary)
        {
            object[] paramArray = new object[5];
            paramArray[0] = type;
            paramArray[1] = id;
            paramArray[2] = parameter;
            paramArray[3] = before;
            paramArray[4] = temporary;
            object returnValue = InstanceType.InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, ComReference, paramArray, XlLateBindingApiSettings.XlThreadCulture);

            if (null == returnValue)
            {
                return(null);
            }
            return(ReturnHelper(type, returnValue));
        }
Beispiel #17
0
        public XlCommandBarControl Add(MsoControlType type)
        {
            object[] paramArray = new object[5];
            paramArray[0] = type;
            paramArray[1] = Missing.Value;
            paramArray[2] = Missing.Value;
            paramArray[3] = Missing.Value;
            paramArray[4] = Missing.Value;
            object returnValue = InstanceType.InvokeMember("Add", BindingFlags.InvokeMethod, null, ComReference, paramArray, XlLateBindingApiSettings.XlThreadCulture);

            if (null == returnValue)
            {
                return(null);
            }
            return(ReturnHelper(type, returnValue));
        }
Beispiel #18
0
        public VSDevExpressMenu(DTE dte, string menuName = DevExpressMenuName, VSDevExpressMenuLocation location = VSDevExpressMenuLocation.MenuBar, bool directItem = false)
        {
            Header = menuName;
            var            commandBars = dte.CommandBars as CommandBars;
            CommandBar     mainMenuBar = commandBars[MenusCache[location]];
            MsoControlType controlType = GetControlType(directItem);

            if (controlType == MsoControlType.msoControlPopup)
            {
                CommandBarPopup devExpressMenu = null;
                foreach (CommandBarControl commandBarControl in mainMenuBar.Controls)
                {
                    if (commandBarControl.Type == MsoControlType.msoControlPopup)
                    {
                        var commandBarPopup = (CommandBarPopup)commandBarControl;
                        if (commandBarPopup.CommandBar.Name == menuName)
                        {
                            devExpressMenu = commandBarPopup;
                            break;
                        }
                    }
                }
                if (devExpressMenu == null)
                {
                    devExpressMenu = mainMenuBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, Type.Missing) as CommandBarPopup;
                }
                VsSource = devExpressMenu;
                CreateChildrenFromSource();
            }
            else if (controlType == MsoControlType.msoControlButton)
            {
                foreach (CommandBarControl commandBarControl in mainMenuBar.Controls)
                {
                    if (commandBarControl.Type == MsoControlType.msoControlButton)
                    {
                        CommandBarButton button = (CommandBarButton)commandBarControl;
                        if (button.Caption == menuName)
                        {
                            throw new ArgumentException("button already registerer");
                        }
                    }
                }
                var msoButton = mainMenuBar.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, Type.Missing) as CommandBarButton;
                msoButton.Caption = menuName;
                VsSource          = msoButton;
            }
        }
Beispiel #19
0
 public CommandBarControl Add(MsoControlType controlType, object Id, object Parameter, object Before, object Temporary)
 {
     return FindOrAdd(controlType, null, Id, Parameter, Before, Temporary);
 }
Beispiel #20
0
 internal static CommandBarControl CreateCommandBarControl(MsoControlType controlType, object commandBarControl)
 {
     if (controlType == MsoControlType.msoControlButton)
     {
         return new CommandBarButton(commandBarControl);
     }
     if (controlType == MsoControlType.msoControlPopup)
     {
         return new CommandBarPopup(commandBarControl);
     }
     if (controlType == MsoControlType.msoControlComboBox)
     {
         return new CommandBarComboBox(commandBarControl);
     }
     return new CommandBarControl(commandBarControl);
 }
Beispiel #21
0
 public CommandBarControl Add(MsoControlType controlType, object Id, object Parameter, object Before, object Temporary)
 {
     return(FindOrAdd(controlType, null, Id, Parameter, Before, Temporary));
 }
Beispiel #22
0
 /// <summary>
 ///		Creates a new toolbar control.
 /// </summary>
 /// <param name="parent">The popup menu holding the control.</param>
 /// <param name="type">The type of the control.</param>
 protected ToolbarControl(ToolbarPopup parent, MsoControlType type)
 {
     this.parent = parent;
     this.type   = type;
 }
Beispiel #23
0
 //n.Add(Type, Id, Parameter, Before, Temporary)
 public abstract CommandBarControl Add(MsoControlType msoControlButton, object v1, object v2, object missing, bool v3);
Beispiel #24
0
        internal CommandBarControl FindOrAdd(MsoControlType controlType, string name, object Id, object Parameter, object Before, object Temporary)
        {
            if (name != null)
            {
                // Try to find an existing control with this name
                string findName = name.Replace("&", "");
                for (int i = 1; i <= Count(); i++)
                {
                    CommandBarControl control = this[i];
                    string caption = control.Caption;
                    if (!String.IsNullOrEmpty(caption))
                        if (caption.Replace("&", "") == findName)
                            return control;
                }
            }

            object /*CommandBarControl*/ newControl = ComObjectTtpe.InvokeMember("Add", BindingFlags.InvokeMethod, null, ComObject,
                new object[] { controlType, Id, Parameter, Before, Temporary });

            return CommandBarControl.CreateCommandBarControl(controlType, newControl);
        }
Beispiel #25
0
 private void Remove(MsoControlType controlType, object id)
 {
     for (int i = 1; i <= Count(); i++)
     {
         if (!String.IsNullOrEmpty(this[i].Caption))
             if (this[i].Caption.Replace("&", "") == id.ToString().Replace("&", ""))
                 this[i].Delete(true);
     }
 }
Beispiel #26
0
 /// <summary>
 ///		Creates a new control.
 /// </summary>
 /// <param name="parent">The popup menu holding the control.</param>
 /// <param name="type">The type of control to create.</param>
 internal ToolbarComboBox(ToolbarPopup parent, MsoControlType type) : base(parent, type)
 {
     items = new ArrayList();
 }