void toolbar_Click(object sender, EventArgs e)
 {
     try
     {
         ToolBar toolbar = (ToolBar)sender;
         System.Reflection.BindingFlags getflags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance;
         int           hotItem = (int)typeof(ToolBar).InvokeMember("hotItem", getflags, null, toolbar, null);
         ToolBarButton button  = toolbar.Buttons[hotItem];
         //if (button.ToolTipText == "Script View")
         MenuCommandToolBarButton menubutton = button as MenuCommandToolBarButton;
         if (menubutton == null)
         {
             return;
         }
         if (menubutton.AssociatedCommandID.ID == (int)BIDSToolbarButtonID.ScriptView)
         {
             ScriptViewDefault = true;
         }
         else if (menubutton.AssociatedCommandID.ID == (int)BIDSToolbarButtonID.FormView)//if (button.ToolTipText == "Form View")
         {
             ScriptViewDefault = false;
         }
     }
     catch { }
 }
        public override void OnWindowActivated(Window GotFocus, Window LostFocus)
        {
            try
            {
                package.Log.Debug("CalcHelpersPlugin OnWindowActivated Fired");
                if (GotFocus == null)
                {
                    return;
                }
                IDesignerHost designer = GotFocus.Object as IDesignerHost;
                if (designer == null)
                {
                    return;
                }
                ProjectItem pi = GotFocus.ProjectItem;
                if ((pi == null) || (!(pi.Object is Cube)))
                {
                    return;
                }
                EditorWindow   win     = (EditorWindow)designer.GetService(typeof(IComponentNavigator));
                VsStyleToolBar toolbar = (VsStyleToolBar)win.SelectedView.GetType().InvokeMember("ToolBar", getflags, null, win.SelectedView, null);

                IntPtr ptr     = win.Handle;
                string sHandle = ptr.ToInt64().ToString();

                if (!windowHandlesFixedForCalcProperties.ContainsKey(sHandle))
                {
                    windowHandlesFixedForCalcProperties.Add(sHandle, win);
                    win.ActiveViewChanged += new EventHandler(win_ActiveViewChanged);
                }

                if (win.SelectedView.MenuItemCommandID.ID == (int)BIDSViewMenuItemCommandID.Calculations)
                //if (win.SelectedView.Caption == "Calculations")
                {
                    int  iMicrosoftCalcPropertiesIndex = 0;
                    bool bFlipScriptViewButton         = false;
                    foreach (ToolBarButton b in toolbar.Buttons)
                    {
                        MenuCommandToolBarButton tbb = b as MenuCommandToolBarButton;
                        if (tbb != null && tbb.AssociatedCommandID.ID == (int)BIDSToolbarButtonID.CalculationProperties)
                        //if (b.ToolTipText.StartsWith("Calculation Properties"))
                        {
                            if (b.Tag == null || b.Tag.ToString() != this.FullName + ".CommandProperties")
                            {
                                if (!toolbar.Buttons.ContainsKey(this.FullName + ".CommandProperties"))
                                {
                                    //if we haven't created it yet
                                    iMicrosoftCalcPropertiesIndex = toolbar.Buttons.IndexOf(b);
                                    b.Visible = false;

                                    newCalcPropButton             = new ToolBarButton();
                                    newCalcPropButton.ToolTipText = "Calculation Properties (BIDS Helper)";
                                    newCalcPropButton.Name        = this.FullName + ".CommandProperties";
                                    newCalcPropButton.Tag         = newCalcPropButton.Name;

                                    newCalcPropButton.ImageIndex = 12;
                                    newCalcPropButton.Enabled    = true;
                                    newCalcPropButton.Style      = ToolBarButtonStyle.PushButton;

                                    toolbar.ImageList.Images.Add(BIDSHelper.Resources.Common.DeployMdxScriptIcon);

                                    if (pi.Name.ToLower().EndsWith(".cube")) //only show feature if we're in offline mode
                                    {
                                        // TODO - does not disable if Deploy plugin is disabled after the button has been added
                                        if (BIDSHelperPackage.Plugins[DeployMdxScriptPlugin.BaseName + typeof(DeployMdxScriptPlugin).Name].Enabled)
                                        {
                                            newDeployMdxScriptButton             = new ToolBarButton();
                                            newDeployMdxScriptButton.ToolTipText = "Deploy MDX Script (BIDS Helper)";
                                            newDeployMdxScriptButton.Name        = this.FullName + ".DeployMdxScript";
                                            newDeployMdxScriptButton.Tag         = newDeployMdxScriptButton.Name;
                                            newDeployMdxScriptButton.ImageIndex  = toolbar.ImageList.Images.Count - 1;
                                            newDeployMdxScriptButton.Enabled     = true;
                                            newDeployMdxScriptButton.Style       = ToolBarButtonStyle.PushButton;
                                        }
                                    }

                                    //catch the button clicks of the new buttons we just added
                                    toolbar.ButtonClick += new ToolBarButtonClickEventHandler(toolbar_ButtonClick);

                                    //catch the mouse clicks... the only way to catch the button click for the Microsoft buttons
                                    toolbar.Click += new EventHandler(toolbar_Click);
                                }
                            }
                        }
                        else if (tbb != null && tbb.AssociatedCommandID.ID == 12854 && ScriptViewDefault && !windowHandlesFixedDefaultCalcScriptView.ContainsKey(sHandle))
                        //else if (b.ToolTipText == "Form View" && ScriptViewDefault && !windowHandlesFixedDefaultCalcScriptView.ContainsKey(sHandle)) //12854
                        {
                            Control control = (Control)win.SelectedView.GetType().InvokeMember("ViewControl", getflags, null, win.SelectedView, null);
                            System.Reflection.BindingFlags getfieldflags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance;
                            object controlMgr = control.GetType().InvokeMember("calcControlMgr", getfieldflags, null, control, null);
                            System.Reflection.BindingFlags getmethodflags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance;
                            controlMgr.GetType().InvokeMember("ViewScript", getmethodflags, null, controlMgr, new object[] { });
                            bFlipScriptViewButton = true;
                            b.Pushed = false;
                            windowHandlesFixedDefaultCalcScriptView.Add(sHandle, win);
                        }
                        else if (tbb != null && tbb.AssociatedCommandID.ID == (int)BIDSHelper.BIDSToolbarButtonID.ScriptView && bFlipScriptViewButton)  //12853
                        //else if (b.ToolTipText == "Script View" && bFlipScriptViewButton) //12853
                        {
                            b.Pushed = true;
                        }
                    }
                    if (newDeployMdxScriptButton != null && !toolbar.Buttons.Contains(newDeployMdxScriptButton))
                    {
                        toolbar.Buttons.Insert(iMicrosoftCalcPropertiesIndex, newDeployMdxScriptButton);
                    }
                    if (newCalcPropButton != null && !toolbar.Buttons.Contains(newCalcPropButton))
                    {
                        toolbar.Buttons.Insert(iMicrosoftCalcPropertiesIndex, newCalcPropButton);
                    }
                }
            }
            catch (Exception ex) {
                package.Log.Error("CalcHelpersPlugin Exception: " + ex.Message);
            }
        }