Exemple #1
0
        /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
        /// <param term='application'>Root object of the host application.</param>
        /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
        /// <param term='addInInst'>Object representing this Add-in.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;

            AStyle = new AStyleInterface();
            AStyle.LoadDefaults();
            AStyleInterface.LoadSettings(ref AStyle);

            if (connectMode == ext_ConnectMode.ext_cm_UISetup)
            {
                object[] contextGUIDS = new object[] { };
                Commands2 commands = (Commands2)_applicationObject.Commands;

                #region Set up toolbar

                var commandBars = (CommandBars)_applicationObject.CommandBars;

                // Add a new toolbar
                CommandBar myTemporaryToolbar = null;
                try
                {
                    myTemporaryToolbar = commandBars[_toolBarName];
                }
                catch { }

                if (myTemporaryToolbar == null)
                    myTemporaryToolbar = commandBars.Add(_toolBarName, MsoBarPosition.msoBarTop, System.Type.Missing, false);

                Command optionsCommand = commands.AddNamedCommand2(_addInInstance, _optionsCommandStr, "Options", "Configure AstyleWrapper",
                     true, 2946, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                     (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);

                Command executeCommand = commands.AddNamedCommand2(_addInInstance, _execCommandStr, "Format selection", "Format selection",
                     true, 611, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                     (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);

                Command switchCommand = commands.AddNamedCommand2(_addInInstance, _switchCommandStr, "Switcher", AStyle.working ? _switchOffStr : _switchOnStr,
                    true, AStyle.working ? 1087 : 1088, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                     (int)vsCommandStyle.vsCommandStylePict, vsCommandControlType.vsCommandControlTypeButton);

                // Add a new button on that toolbar

                //_switchCommandBtn = (CommandBarButton)switchCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1);
                _switchCommandBtn = (CommandBarButton)switchCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1);
                _optionsCommandBtn = (CommandBarButton)optionsCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1);
                _execCommandBtn = (CommandBarButton)executeCommand.AddControl(myTemporaryToolbar, myTemporaryToolbar.Controls.Count + 1);

                // Make visible the toolbar
                myTemporaryToolbar.Visible = true;

                #endregion
            }
            else
            {
                // try get buttons
                var commandBars = (CommandBars)_applicationObject.CommandBars;
                try
                {
                    var myTemporaryToolbar = commandBars[_toolBarName];
                    _switchCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[1];
                    _execCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[2];
                    _optionsCommandBtn = (CommandBarButton)myTemporaryToolbar.Controls[3];
                }
                catch { }

                _hashes = new HashSet<int>();

                _textEditorEvents = _applicationObject.Events.get_TextEditorEvents(null);
                _textEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler(_textEditorEvents_LineChanged);
            }
        }