private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     // Close the toolbar host.
     if (null != toolbarHost)
     {
         toolbarHost.Close(0);
         toolbarHost = null;
     }
     // Close and dispose the main pane.
     if (null != containedForm)
     {
         ((IVsWindowPane)containedForm).ClosePane();
         containedForm = null;
     }
     // Check if we are still registered as priority command target
     if ((0 != commandTargetCookie) && (null != provider))
     {
         IVsRegisterPriorityCommandTarget registerCommandTarget = GetService(typeof(SVsRegisterPriorityCommandTarget)) as IVsRegisterPriorityCommandTarget;
         if (null != registerCommandTarget)
         {
             registerCommandTarget.UnregisterPriorityCommandTarget(commandTargetCookie);
         }
         commandTargetCookie = 0;
     }
     if (null != e)
     {
         e.Cancel = false;
     }
 }
        private void FormLoad(object sender, EventArgs e)
        {
            if (this.DesignMode)
            {
                return;
            }

            if (null == containedForm)
            {
                // Handle the case that the class was constructed with the parameterless
                // constructor, so no container control is created.
                // In this case we have to create a new control that will contain all the
                // controls contained by this form and use it to create the window pane.
                Control paneControl = new UserControl();
                while (this.Controls.Count > 0)
                {
                    Control ctl = this.Controls[0];
                    ctl.Parent = paneControl;
                }
                containedForm = new WindowPaneAdapter(this, paneControl);
                controlSize   = this.ClientSize;
            }

            System.Drawing.Size mySize = this.ClientSize;

            // Check if this window has a toolbar.
            if (null != toolbarCommandId)
            {
                Guid toolbarCommandSet = toolbarCommandId.Guid;
                IVsToolWindowToolbarHost2 toolBarHost2 = (IVsToolWindowToolbarHost2)ToolbarHost;
                NativeMethods.ThrowOnFailure(
                    toolBarHost2.AddToolbar2(toolbarLocation, ref toolbarCommandSet, (uint)toolbarCommandId.ID, toolbarDropTarget));
                NativeMethods.ThrowOnFailure(ToolbarHost.Show(0));
                NativeMethods.ThrowOnFailure(ToolbarHost.ForceUpdateUI());
            }

            // Now we have to resize the form to make room for the toolbar.
            mySize.Width    = controlSize.Width + toolbarRect.left + toolbarRect.right;
            mySize.Height   = controlSize.Height + toolbarRect.top + toolbarRect.bottom;
            this.ClientSize = mySize;

            // Find the coordinate of the main pane.
            int x      = toolbarRect.left;
            int y      = toolbarRect.top;
            int width  = mySize.Width - toolbarRect.left - toolbarRect.right;
            int height = mySize.Height - toolbarRect.top - toolbarRect.bottom;

            // Make sure that the pane is created.
            containedForm.Create(x, y, height, width);
            // Set the focus to the control
            containedForm.Focus();

            // Install the handler for the resize.
            this.Resize += new EventHandler(ResizeForm);
        }
        private void PrivateInit(IServiceProvider sp, Control contained, IOleCommandTarget parentTarget)
        {
            provider = sp;

            commandTargetCookie = 0;
            if (null == parentTarget)
            {
                commandService = new OleMenuCommandService(sp);
            }
            else
            {
                commandService = new OleMenuCommandService(sp, parentTarget);
            }
            if (null != sp)
            {
                // Now we have to register the IOleCommandTarget implemented by the OleCommandService
                // as a priority command target, so it will be called by the shell.
                RegisterCommandTarget();
            }

            // Set the defaults for the toolbar (empty toolbar placed at the top)
            toolbarRect.left = 0;
            toolbarRect.top = 0;
            toolbarRect.right = 0;
            toolbarRect.bottom = 0;
            toolbarCommandId = null;
            toolbarLocation = VSTWT_LOCATION.VSTWT_TOP;

            if (null == contained)
            {
                containedForm = null;
            }
            else
            {
                controlSize = contained.ClientSize;
                containedForm = new WindowPaneAdapter(this, contained);
                this.Site = contained.Site;
                Form innerForm = contained as Form;
                if (null != innerForm)
                {
                    // If the contained control is a form, then copy some
                    // of its property to this one.
                    this.AcceptButton = innerForm.AcceptButton;
                    this.AccessibleDefaultActionDescription = innerForm.AccessibleDefaultActionDescription;
                    this.AccessibleDescription = innerForm.AccessibleDescription;
                    this.AccessibleName = innerForm.AccessibleName;
                    this.AccessibleRole = innerForm.AccessibleRole;
                    this.AllowDrop = innerForm.AllowDrop;
                    this.AllowTransparency = innerForm.AllowTransparency;
                    this.AutoScaleDimensions = innerForm.AutoScaleDimensions;
                    this.AutoScaleMode = innerForm.AutoScaleMode;
                    this.AutoScroll = innerForm.AutoScroll;
                    this.AutoScrollMargin = innerForm.AutoScrollMargin;
                    this.AutoScrollMinSize = innerForm.AutoScrollMinSize;
                    this.AutoScrollPosition = innerForm.AutoScrollPosition;
                    this.BindingContext = innerForm.BindingContext;
                    this.Bounds = innerForm.Bounds;
                    this.CancelButton = innerForm.CancelButton;
                    this.ContextMenu = innerForm.ContextMenu;
                    this.ControlBox = innerForm.ControlBox;
                    this.Cursor = innerForm.Cursor;
                    this.DesktopBounds = innerForm.DesktopBounds;
                    this.DesktopLocation = innerForm.DesktopLocation;
                    this.Font = innerForm.Font;
                    this.FormBorderStyle = innerForm.FormBorderStyle;
                    this.Icon = innerForm.Icon;
                    this.IsAccessible = innerForm.IsAccessible;
                    this.MaximizeBox = innerForm.MaximizeBox;
                    this.MaximumSize = innerForm.MaximumSize;
                    this.Menu = innerForm.Menu;
                    this.MinimizeBox = innerForm.MinimizeBox;
                    this.MinimumSize = innerForm.MinimumSize;
                    this.Opacity = innerForm.Opacity;
                    this.Region = innerForm.Region;
                    this.RightToLeft = innerForm.RightToLeft;
                    this.ShowInTaskbar = innerForm.ShowInTaskbar;
                    this.SizeGripStyle = innerForm.SizeGripStyle;
                    this.StartPosition = innerForm.StartPosition;
                    this.Text = innerForm.Text;
                    this.TopLevel = innerForm.TopLevel;
                    this.TopMost = innerForm.TopMost;
                    this.TransparencyKey = innerForm.TransparencyKey;
                }
            }
            // At the end of the copy we have to set the properties that we want
            // to enforse (right now only the HelpButton on the command bar).
            this.HelpButton = true;

            // Set the callbacks for the events that this default implementation will handle.
            this.Load += new EventHandler(FormLoad);
            this.Closing += new System.ComponentModel.CancelEventHandler(OnClosing);
        }
 private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     // Close the toolbar host.
     if (null != toolbarHost)
     {
         toolbarHost.Close(0);
         toolbarHost = null;
     }
     // Close and dispose the main pane.
     if (null != containedForm)
     {
         ((IVsWindowPane)containedForm).ClosePane();
         containedForm = null;
     }
     // Check if we are still registered as priority command target
     if ( (0 != commandTargetCookie) && (null != provider) )
     {
         IVsRegisterPriorityCommandTarget registerCommandTarget = GetService(typeof(SVsRegisterPriorityCommandTarget)) as IVsRegisterPriorityCommandTarget;
         if (null != registerCommandTarget)
             registerCommandTarget.UnregisterPriorityCommandTarget(commandTargetCookie);
         commandTargetCookie = 0;
     }
     if (null != e)
         e.Cancel = false;
 }
        private void FormLoad(object sender, EventArgs e)
        {
            if (this.DesignMode)
                return;

            if (null == containedForm)
            {
                // Handle the case that the class was constructed with the parameterless
                // constructor, so no container control is created.
                // In this case we have to create a new control that will contain all the
                // controls contained by this form and use it to create the window pane.
                Control paneControl = new UserControl();
                while (this.Controls.Count > 0)
                {
                    Control ctl = this.Controls[0];
                    ctl.Parent = paneControl;
                }
                containedForm = new WindowPaneAdapter(this, paneControl);
                controlSize = this.ClientSize;
            }

            System.Drawing.Size mySize = this.ClientSize;

            // Check if this window has a toolbar.
            if (null != toolbarCommandId)
            {
                Guid toolbarCommandSet = toolbarCommandId.Guid;
                NativeMethods.ThrowOnFailure(
                    ToolbarHost.AddToolbar(toolbarLocation, ref toolbarCommandSet, (uint)toolbarCommandId.ID));
                NativeMethods.ThrowOnFailure(ToolbarHost.Show(0));
                NativeMethods.ThrowOnFailure(ToolbarHost.ForceUpdateUI());
            }

            // Now we have to resize the form to make room for the toolbar.
            mySize.Width = controlSize.Width + toolbarRect.left + toolbarRect.right;
            mySize.Height = controlSize.Height + toolbarRect.top + toolbarRect.bottom;
            this.ClientSize = mySize;

            // Find the coordinate of the main pane.
            int x = toolbarRect.left;
            int y = toolbarRect.top;
            int width = mySize.Width - toolbarRect.left - toolbarRect.right;
            int height = mySize.Height - toolbarRect.top - toolbarRect.bottom;

            // Make sure that the pane is created.
            containedForm.Create(x, y, height, width);
            // Set the focus to the control
            containedForm.Focus();

            // Install the handler for the resize.
            this.Resize += new EventHandler(ResizeForm);
        }
        private void PrivateInit(IServiceProvider sp, Control contained, IOleCommandTarget parentTarget)
        {
            provider = sp;

            commandTargetCookie = 0;
            if (null == parentTarget)
            {
                commandService = new OleMenuCommandService(sp);
            }
            else
            {
                commandService = new OleMenuCommandService(sp, parentTarget);
            }
            if (null != sp)
            {
                // Now we have to register the IOleCommandTarget implemented by the OleCommandService
                // as a priority command target, so it will be called by the shell.
                RegisterCommandTarget();
            }

            // Set the defaults for the toolbar (empty toolbar placed at the top)
            toolbarRect.left   = 0;
            toolbarRect.top    = 0;
            toolbarRect.right  = 0;
            toolbarRect.bottom = 0;
            toolbarCommandId   = null;
            toolbarLocation    = VSTWT_LOCATION.VSTWT_TOP;

            if (null == contained)
            {
                containedForm = null;
            }
            else
            {
                controlSize   = contained.ClientSize;
                containedForm = new WindowPaneAdapter(this, contained);
                this.Site     = contained.Site;
                Form innerForm = contained as Form;
                if (null != innerForm)
                {
                    // If the contained control is a form, then copy some
                    // of its property to this one.
                    this.AcceptButton = innerForm.AcceptButton;
                    this.AccessibleDefaultActionDescription = innerForm.AccessibleDefaultActionDescription;
                    this.AccessibleDescription = innerForm.AccessibleDescription;
                    this.AccessibleName        = innerForm.AccessibleName;
                    this.AccessibleRole        = innerForm.AccessibleRole;
                    this.AllowDrop             = innerForm.AllowDrop;
                    this.AllowTransparency     = innerForm.AllowTransparency;
                    this.AutoScaleDimensions   = innerForm.AutoScaleDimensions;
                    this.AutoScaleMode         = innerForm.AutoScaleMode;
                    this.AutoScroll            = innerForm.AutoScroll;
                    this.AutoScrollMargin      = innerForm.AutoScrollMargin;
                    this.AutoScrollMinSize     = innerForm.AutoScrollMinSize;
                    this.AutoScrollPosition    = innerForm.AutoScrollPosition;
                    this.BindingContext        = innerForm.BindingContext;
                    this.Bounds          = innerForm.Bounds;
                    this.CancelButton    = innerForm.CancelButton;
                    this.ContextMenu     = innerForm.ContextMenu;
                    this.ControlBox      = innerForm.ControlBox;
                    this.Cursor          = innerForm.Cursor;
                    this.DesktopBounds   = innerForm.DesktopBounds;
                    this.DesktopLocation = innerForm.DesktopLocation;
                    this.Font            = innerForm.Font;
                    this.FormBorderStyle = innerForm.FormBorderStyle;
                    this.Icon            = innerForm.Icon;
                    this.IsAccessible    = innerForm.IsAccessible;
                    this.MaximizeBox     = innerForm.MaximizeBox;
                    this.MaximumSize     = innerForm.MaximumSize;
                    this.Menu            = innerForm.Menu;
                    this.MinimizeBox     = innerForm.MinimizeBox;
                    this.MinimumSize     = innerForm.MinimumSize;
                    this.Opacity         = innerForm.Opacity;
                    this.Region          = innerForm.Region;
                    this.RightToLeft     = innerForm.RightToLeft;
                    this.ShowInTaskbar   = innerForm.ShowInTaskbar;
                    this.SizeGripStyle   = innerForm.SizeGripStyle;
                    this.StartPosition   = innerForm.StartPosition;
                    this.Text            = innerForm.Text;
                    this.TopLevel        = innerForm.TopLevel;
                    this.TopMost         = innerForm.TopMost;
                    this.TransparencyKey = innerForm.TransparencyKey;
                }
            }
            // At the end of the copy we have to set the properties that we want
            // to enforse (right now only the HelpButton on the command bar).
            this.HelpButton = true;

            // Set the callbacks for the events that this default implementation will handle.
            this.Load    += new EventHandler(FormLoad);
            this.Closing += new System.ComponentModel.CancelEventHandler(OnClosing);
        }