private ToolWindowPane FindToolWindow(Type toolWindowType, int id, bool create, ProvideToolWindowAttribute tool) { if (toolWindowType == null) throw new ArgumentNullException("toolWindowType"); ToolWindowPane window = null; int hash = GetHash(toolWindowType.GUID, id); if (_toolWindows != null && _toolWindows.ContainsKey(hash)) window = (ToolWindowPane)_toolWindows[hash]; else if (create) { if (tool != null) window = CreateToolWindow(toolWindowType, id, tool); else window = CreateToolWindow(toolWindowType, id); } return window; }
/// <summary> /// This is the only method that should be calling IVsUiShell.CreateToolWindow() /// </summary> private ToolWindowPane CreateToolWindow(Type toolWindowType, int id, ProvideToolWindowAttribute tool) { if (toolWindowType == null) throw new ArgumentNullException("toolWindowType"); if (id < 0) throw new ArgumentOutOfRangeException(SR.GetString(SR.Package_InvalidInstanceID, id)); if (!toolWindowType.IsSubclassOf(typeof(ToolWindowPane))) throw new ArgumentException(SR.GetString(SR.Package_InvalidToolWindowClass)); if (tool == null) throw new ArgumentNullException("tool"); // First create an instance of the ToolWindowPane ToolWindowPane window = (ToolWindowPane)Activator.CreateInstance(toolWindowType); // Check if this window has a ToolBar bool hasToolBar = (window.ToolBar != null); uint flags = (uint)__VSCREATETOOLWIN.CTW_fInitNew; if (!tool.Transient) flags |= (uint)__VSCREATETOOLWIN.CTW_fForceCreate; if (hasToolBar) flags |= (uint)__VSCREATETOOLWIN.CTW_fToolbarHost; if (tool.MultiInstances) flags |= (uint)__VSCREATETOOLWIN.CTW_fMultiInstance; Guid emptyGuid = Guid.Empty; Guid toolClsid = window.ToolClsid; IVsWindowPane windowPane = null; if (toolClsid.CompareTo(Guid.Empty) == 0) { // If a tool CLSID is not specified, then host the IVsWindowPane windowPane = window.GetIVsWindowPane() as IVsWindowPane; } Guid persistenceGuid = toolWindowType.GUID; IVsWindowFrame windowFrame; // Use IVsUIShell to create frame. IVsUIShell vsUiShell = (IVsUIShell)GetService(typeof(SVsUIShell)); if (vsUiShell == null) throw new Exception(SR.GetString(SR.General_MissingService, typeof(SVsUIShell).FullName)); int hr = vsUiShell.CreateToolWindow(flags, // flags (uint)id, // instance ID windowPane, // IVsWindowPane to host in the toolwindow (null if toolClsid is specified) ref toolClsid, // toolClsid to host in the toolwindow (Guid.Empty if windowPane is not null) ref persistenceGuid, // persistence Guid ref emptyGuid, // auto activate Guid null, // service provider window.Caption, // Window title null, out windowFrame); NativeMethods.ThrowOnFailure(hr); window.Package = this; // If the toolwindow is a component, site it. IComponent component = null; if (window.Window is IComponent) component = (IComponent)window.Window; else if (windowPane is IComponent) component = (IComponent)windowPane; if (component != null) { if (_componentToolWindows == null) _componentToolWindows = new PackageContainer(this); _componentToolWindows.Add((IComponent)component); } // This generates the OnToolWindowCreated event on the ToolWindowPane window.Frame = windowFrame; if (hasToolBar && windowFrame != null) { // Set the toolbar IVsToolWindowToolbarHost toolBarHost; object obj; NativeMethods.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_ToolbarHost, out obj)); toolBarHost = (IVsToolWindowToolbarHost)obj; if (toolBarHost != null) { Guid toolBarCommandSet = window.ToolBar.Guid; NativeMethods.ThrowOnFailure(toolBarHost.AddToolbar((VSTWT_LOCATION)window.ToolBarLocation, ref toolBarCommandSet, (uint)window.ToolBar.ID)); } } window.OnToolBarAdded(); // If the ToolWindow was created successfully, keep track of it if (window != null) { if (_toolWindows == null) _toolWindows = new Hashtable(); _toolWindows.Add(GetHash(toolWindowType.GUID, id), window); } return window; }
private WindowPane FindToolWindow(Type toolWindowType, int id, bool create, ProvideToolWindowAttribute tool) { if (toolWindowType == null) throw new ArgumentNullException("toolWindowType"); WindowPane window = null; if (null != _toolWindows) { window = _toolWindows.GetToolWindowPane(toolWindowType.GUID, id); } if ((null == window) && create) { if (tool != null) window = CreateToolWindow(toolWindowType, id, tool); else window = CreateToolWindow(toolWindowType, id); } return window; }
private ToolWindowPane FindToolWindow(Type toolWindowType, int id, bool create, ProvideToolWindowAttribute tool) { if (toolWindowType == null) throw new ArgumentNullException("toolWindowType"); ToolWindowPane window = null; // Try to get the window from the cache. if (null != _toolWindows) { // This will return null if the window is not in the collection. window = _toolWindows.GetToolWindowPane(toolWindowType.GUID, id); } if ((null == window) && create) { if (tool != null) window = CreateToolWindow(toolWindowType, id, tool); else window = CreateToolWindow(toolWindowType, id); } return window; }
/// <devdoc> /// This is the only method that should be calling IVsUiShell.CreateToolWindow() /// </devdoc> private WindowPane CreateToolWindow(Type toolWindowType, int id, ProvideToolWindowAttribute tool) { if (toolWindowType == null) throw new ArgumentNullException("toolWindowType"); if (id < 0) throw new ArgumentOutOfRangeException(string.Format(Resources.Culture, Resources.Package_InvalidInstanceID, id)); if (!toolWindowType.IsSubclassOf(typeof(WindowPane))) throw new ArgumentException(Resources.Package_InvalidToolWindowClass); if (tool == null) throw new ArgumentNullException("tool"); // First create an instance of the ToolWindowPane WindowPane window = (WindowPane)Activator.CreateInstance(toolWindowType); ToolWindowPane toolwindow = window as ToolWindowPane; // Check if this window has a ToolBar bool hasToolBar = false; Guid emptyGuid = Guid.Empty; Guid toolClsid = Guid.Empty; string caption = null; if (toolwindow != null) { toolClsid = toolwindow.ToolClsid; caption = toolwindow.Caption; hasToolBar = (toolwindow.ToolBar != null); toolwindow.Package = this; } uint flags = (uint)__VSCREATETOOLWIN.CTW_fInitNew; if (!tool.Transient) flags |= (uint)__VSCREATETOOLWIN.CTW_fForceCreate; if (hasToolBar) flags |= (uint)__VSCREATETOOLWIN.CTW_fToolbarHost; if (tool.MultiInstances) flags |= (uint)__VSCREATETOOLWIN.CTW_fMultiInstance; object windowPane = null; if (toolClsid.CompareTo(Guid.Empty) == 0) { // If a tool CLSID is not specified, then host the IVsWindowPane if (toolwindow != null) // This method makes it possible to provide an IVsWindowPane not derived from ToolWindowPane windowPane = toolwindow.GetIVsWindowPane(); else windowPane = window; } Guid persistenceGuid = toolWindowType.GUID; IVsWindowFrame windowFrame; // Use IVsUIShell to create frame. IVsUIShell vsUiShell = (IVsUIShell)GetService(typeof(SVsUIShell)); if (vsUiShell == null) throw new Exception(string.Format(Resources.Culture, Resources.General_MissingService, typeof(SVsUIShell).FullName)); int hr = vsUiShell.CreateToolWindow(flags, // flags (uint)id, // instance ID windowPane, // IVsWindowPane to host in the toolwindow (null if toolClsid is specified) ref toolClsid, // toolClsid to host in the toolwindow (Guid.Empty if windowPane is not null) ref persistenceGuid, // persistence Guid ref emptyGuid, // auto activate Guid null, // service provider caption, // Window title null, out windowFrame); NativeMethods.ThrowOnFailure(hr); // If the toolwindow is a component, site it. IComponent component = null; // Check first if the content is a component, preferring IVsUIElementPane over IVsWindowPane-style pane if (window.Content != null) { component = window.Content as IComponent; } else { component = window.Window as IComponent; } // If we don't have a component yet, see if the toolwindow itself is a component if (component == null) { component = windowPane as IComponent; } if (component != null) { if (_componentToolWindows == null) _componentToolWindows = new PackageContainer(this); _componentToolWindows.Add((IComponent)component); } // This generates the OnToolWindowCreated event on the ToolWindowPane if (toolwindow != null) toolwindow.Frame = windowFrame; if (hasToolBar && windowFrame != null && toolwindow != null) { // Set the toolbar IVsToolWindowToolbarHost2 toolBarHost; object obj; NativeMethods.ThrowOnFailure(windowFrame.GetProperty((int)__VSFPROPID.VSFPROPID_ToolbarHost, out obj)); toolBarHost = (IVsToolWindowToolbarHost2)obj; if (toolBarHost != null) { Guid toolBarCommandSet = toolwindow.ToolBar.Guid; NativeMethods.ThrowOnFailure(toolBarHost.AddToolbar2((VSTWT_LOCATION)toolwindow.ToolBarLocation, ref toolBarCommandSet, (uint)toolwindow.ToolBar.ID, toolwindow.ToolBarDropTarget)); } toolwindow.OnToolBarAdded(); } // If the ToolWindow was created successfully, keep track of it if (toolwindow != null) { if (_toolWindows == null) _toolWindows = new ToolWindowCollection(); _toolWindows.Add(toolWindowType.GUID, id, toolwindow); } return window; }