PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/O2.Platform.VisualStudio_2010/ExtensionMethods/VisualStudio_2010_ExtensionMethods_Windows.cs

http://github.com/o2platform/O2.Platform.Projects
C# | 399 lines | 391 code | 6 blank | 2 comment | 4 complexity | 6e23e5024f9b29e635266f9a662bb71b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using Microsoft.VisualStudio;
  8. using Microsoft.VisualStudio.Shell;
  9. using Microsoft.VisualStudio.Shell.Interop;
  10. using Microsoft.VisualStudio.PlatformUI;
  11. using Microsoft.VisualStudio.Platform.WindowManagement;
  12. using Microsoft.VisualStudio.Platform.WindowManagement.DTE;
  13. using System.Windows.Forms.Integration;
  14. using WinForms = System.Windows.Forms;
  15. using EnvDTE;
  16. using EnvDTE80;
  17. using O2.DotNetWrappers.DotNet;
  18. using O2.DotNetWrappers.ExtensionMethods;
  19. using O2.FluentSharp.VisualStudio;
  20. namespace O2.FluentSharp.VisualStudio.ExtensionMethods
  21. {
  22. public static class VisualStudio_2010_ExtensionMethods_ToolWindowsPane
  23. {
  24. public static int lastWindowId = 0;
  25. public static Grid create_WPF_Window(this string title)
  26. {
  27. ToolWindowPane toolWindow = null;
  28. return title.create_WPF_Window(ref toolWindow);
  29. }
  30. public static Grid create_WPF_Window(this string title, ref ToolWindowPane toolWindow)
  31. {
  32. var visualStudio = new VisualStudio_2010();
  33. ToolWindowPane window = null;
  34. var grid = visualStudio.invokeOnThread(
  35. () =>
  36. {
  37. var type = typeof(O2.FluentSharp.VisualStudio.WindowPane_WPF);
  38. window = (ToolWindowPane)visualStudio.package().invoke("CreateToolWindow", type, ++lastWindowId);
  39. window.Caption = title;
  40. IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
  41. windowFrame.Show();
  42. var content = (Control_WPF)window.Content;
  43. return (Grid)content.Content;
  44. });
  45. toolWindow = window;
  46. return grid;
  47. }
  48. public static WinForms.Panel create_WinForms_Window(this string title)
  49. {
  50. return title.create_WinForms_Window(VSFRAMEMODE.VSFM_Dock);
  51. }
  52. public static WinForms.Panel create_WinForms_Window(this string title, VSFRAMEMODE frameMode)
  53. {
  54. var visualStudio = new VisualStudio_2010();
  55. var _panel = visualStudio.invokeOnThread(
  56. () =>
  57. {
  58. var type = typeof(O2.FluentSharp.VisualStudio.WindowPane_WinForms);
  59. var window = (ToolWindowPane)visualStudio.package().invoke("CreateToolWindow", type, 64000.random());
  60. window.Caption = title;
  61. IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
  62. //if(floating)
  63. // windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_FrameMode, VSFRAMEMODE.VSFM_Float);
  64. windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_FrameMode, frameMode);
  65. windowFrame.Show();
  66. var content = (Control_WinForms)window.Content;
  67. var windowsFormHost = (System.Windows.Forms.Integration.WindowsFormsHost)content.Content;
  68. var panel = new WinForms.Panel();
  69. panel.backColor("Control");
  70. windowsFormHost.Child = panel;
  71. return panel;
  72. });
  73. return _panel;
  74. }
  75. public static WinForms.Panel create_WinForms_Window_Float(this string title, int width, int height)
  76. {
  77. var panel = title.create_WinForms_Window_Float();
  78. panel.dte_Window().width(width).height(height);
  79. return panel;
  80. }
  81. public static WinForms.Panel create_WinForms_Window_Float(this string title)
  82. {
  83. return title.create_WinForms_Window(VSFRAMEMODE.VSFM_Float);
  84. }
  85. public static WinForms.Panel create_WinForms_Window_MdiChild(this string title)
  86. {
  87. return title.create_WinForms_Window(VSFRAMEMODE.VSFM_MdiChild);
  88. }
  89. public static WinForms.Panel create_WinForms_Window(this VisualStudio_2010 visualStudio, string title)
  90. {
  91. return title.create_WinForms_Window();
  92. }
  93. public static WinForms.Panel create_WinForms_Window_Float(this VisualStudio_2010 visualStudio, string title)
  94. {
  95. return title.create_WinForms_Window_Float();
  96. }
  97. public static string caption<T>(this T toolWindowPane) where T : ToolWindowPane
  98. {
  99. return new VisualStudio_2010().invokeOnThread(() => toolWindowPane.Caption);
  100. }
  101. public static T caption<T>(this T toolWindowPane, string title) where T : ToolWindowPane
  102. {
  103. return new VisualStudio_2010().invokeOnThread(() => { toolWindowPane.Caption = title; return toolWindowPane; });
  104. }
  105. public static ToolWindowPane as_MdiChild(this ToolWindowPane toolWindow)
  106. {
  107. if (toolWindow.notNull())
  108. {
  109. IVsWindowFrame windowFrame = (IVsWindowFrame)toolWindow.Frame;
  110. windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_FrameMode, VSFRAMEMODE.VSFM_MdiChild);
  111. }
  112. return toolWindow;
  113. }
  114. public static ToolWindowPane as_Float(this ToolWindowPane toolWindow)
  115. {
  116. if (toolWindow.notNull())
  117. {
  118. IVsWindowFrame windowFrame = (IVsWindowFrame)toolWindow.Frame;
  119. windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_FrameMode, VSFRAMEMODE.VSFM_Float);
  120. }
  121. return toolWindow;
  122. }
  123. public static ToolWindowPane as_Dock(this ToolWindowPane toolWindow)
  124. {
  125. if (toolWindow.notNull())
  126. {
  127. IVsWindowFrame windowFrame = (IVsWindowFrame)toolWindow.Frame;
  128. windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_FrameMode, VSFRAMEMODE.VSFM_Dock);
  129. }
  130. return toolWindow;
  131. }
  132. }
  133. public static class VisualStudio_2010_ExtensionMethods_WindowBase
  134. {
  135. public static WindowBase windowBase (this WinForms.Control control )
  136. {
  137. try
  138. {
  139. return (WindowBase)control.dte_Window();
  140. }
  141. catch (Exception ex)
  142. {
  143. ex.log("[in control.windowBase]");
  144. return null;
  145. }
  146. }
  147. public static List<WindowBase> windows (this VisualStudio_2010 visualStudio )
  148. {
  149. var windows = new List<WindowBase>();
  150. foreach (WindowBase window in visualStudio.dte().Windows)
  151. windows.Add(window);
  152. return windows;
  153. }
  154. public static WindowBase window (this VisualStudio_2010 visualStudio, string caption )
  155. {
  156. return visualStudio.windows().Where((window) => window.Caption == caption).first();
  157. }
  158. public static WindowBase get_Window (this VisualStudio_2010 visualStudio, string caption )
  159. {
  160. return visualStudio.window(caption);
  161. }
  162. public static List<string> names (this List<WindowBase> windows )
  163. {
  164. return windows.captions();
  165. }
  166. public static List<string> titles (this List<WindowBase> windows )
  167. {
  168. return windows.captions();
  169. }
  170. public static List<string> captions (this List<WindowBase> windows )
  171. {
  172. return windows.Select((window) => window.Caption).toList();
  173. }
  174. public static WindowBase title (this WindowBase window , string value )
  175. {
  176. try
  177. {
  178. window.Caption = value;
  179. }
  180. catch (Exception ex)
  181. {
  182. ex.log("[window.title]");
  183. }
  184. return window;
  185. }
  186. public static WindowBase floating (this WindowBase window, bool value )
  187. {
  188. try
  189. {
  190. window.IsFloating = value;
  191. }
  192. catch (Exception ex)
  193. {
  194. ex.log("[window.floating]");
  195. }
  196. return window;
  197. }
  198. public static WindowBase linkable (this WindowBase window, bool value )
  199. {
  200. try
  201. {
  202. window.Linkable = value;
  203. }
  204. catch (Exception ex)
  205. {
  206. ex.log("[window.linkable]");
  207. }
  208. return window;
  209. }
  210. public static WindowBase autoHide (this WindowBase window, bool value )
  211. {
  212. try
  213. {
  214. window.AutoHides = value;
  215. }
  216. catch (Exception ex)
  217. {
  218. ex.log("[window.autoHide]");
  219. }
  220. return window;
  221. }
  222. public static WindowBase visible (this WindowBase window, bool value )
  223. {
  224. try
  225. {
  226. window.Visible = value;
  227. }
  228. catch (Exception ex)
  229. {
  230. ex.log("[window.visible]");
  231. }
  232. return window;
  233. }
  234. public static WindowBase left (this WindowBase window, int value )
  235. {
  236. try
  237. {
  238. window.Left = value;
  239. }
  240. catch (Exception ex)
  241. {
  242. ex.log("[window.left]");
  243. }
  244. return window;
  245. }
  246. public static WindowBase top (this WindowBase window, int value )
  247. {
  248. try
  249. {
  250. window.Top = value;
  251. }
  252. catch (Exception ex)
  253. {
  254. ex.log("[window.top]");
  255. }
  256. return window;
  257. }
  258. public static WindowBase width (this WindowBase window, int value )
  259. {
  260. try
  261. {
  262. window.Width = value;
  263. }
  264. catch (Exception ex)
  265. {
  266. ex.log("[window.width]");
  267. }
  268. return window;
  269. }
  270. public static WindowBase height (this WindowBase window, int value )
  271. {
  272. try
  273. {
  274. window.Height = value;
  275. }
  276. catch (Exception ex)
  277. {
  278. ex.log("[window.height]");
  279. }
  280. return window;
  281. }
  282. public static WindowBase focus (this WindowBase window )
  283. {
  284. return window.show();
  285. }
  286. public static WindowBase show (this WindowBase window )
  287. {
  288. try
  289. {
  290. window.visible(true);
  291. window.Activate();
  292. }
  293. catch (Exception ex)
  294. {
  295. ex.log("[window.show]");
  296. }
  297. return window;
  298. }
  299. public static WindowBase hide (this WindowBase window )
  300. {
  301. window.visible(false);
  302. return window;
  303. }
  304. }
  305. public static class VisualStudio_2010_ExtensionMethods_DTE_Window
  306. {
  307. public static EnvDTE.Window dte_Window(this System.Windows.Forms.Control control)
  308. {
  309. return control.toolWindowPane().dte_Window();
  310. }
  311. public static EnvDTE.Window dte_Window(this ToolWindowPane toolWindowPane)
  312. {
  313. return new VisualStudio_2010().invokeOnThread(
  314. () =>
  315. {
  316. IVsWindowFrame windowFrame = (IVsWindowFrame)toolWindowPane.Frame;
  317. return VsShellUtilities.GetWindowObject(windowFrame);
  318. });
  319. }
  320. public static string title(this EnvDTE.Window window)
  321. {
  322. return new VisualStudio_2010().invokeOnThread(
  323. () => window.Caption);
  324. }
  325. public static EnvDTE.Window title(this EnvDTE.Window window, string value)
  326. {
  327. return new VisualStudio_2010().invokeOnThread(() =>
  328. {
  329. window.Caption = value;
  330. return window;
  331. });
  332. }
  333. public static EnvDTE.Window width(this EnvDTE.Window window, int value)
  334. {
  335. return new VisualStudio_2010().invokeOnThread(() =>
  336. {
  337. window.Width = value;
  338. return window;
  339. });
  340. }
  341. public static EnvDTE.Window height(this EnvDTE.Window window, int value)
  342. {
  343. return new VisualStudio_2010().invokeOnThread(() =>
  344. {
  345. window.Height = value;
  346. return window;
  347. });
  348. }
  349. public static bool close(this EnvDTE.Window window)
  350. {
  351. try
  352. {
  353. window.Close(); //will throw exeption if window has been closed
  354. return true;
  355. }
  356. catch (Exception ex)
  357. {
  358. ex.log("[in EnvDTE.window.close]");
  359. return false;
  360. }
  361. }
  362. public static bool visible(this EnvDTE.Window window)
  363. {
  364. try
  365. {
  366. return window.Visible; //will throw exeption if window has been closed
  367. }
  368. catch (Exception ex)
  369. {
  370. ex.log("[in EnvDTE.window.visible]");
  371. return false;
  372. }
  373. }
  374. public static EnvDTE.Window visible(this EnvDTE.Window window, bool value)
  375. {
  376. try
  377. {
  378. window.Visible = value; //will throw exeption if window has been closed
  379. return window;
  380. }
  381. catch (Exception ex)
  382. {
  383. ex.log("[in EnvDTE.window.visible]");
  384. return null;
  385. }
  386. }
  387. public static T close_in_NSeconds<T>(this T window, int seconds) where T : EnvDTE.Window
  388. {
  389. O2Thread.mtaThread(() => window.wait(seconds * 1000).close());
  390. return window;
  391. }
  392. }
  393. }