PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Blocks/CAB-WPFExtensions/Source/CompositeUI.WPF/Workspaces/DeckWorkspace.cs

http://scsf2012.codeplex.com
C# | 341 lines | 195 code | 50 blank | 96 comment | 12 complexity | f22c16ba467db8b498a1da1426ff3648 MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices
  3. // Smart Client Software Factory 2010
  4. //===============================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===============================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.ComponentModel;
  20. using System.Windows.Forms;
  21. using System.Collections.ObjectModel;
  22. using Microsoft.Practices.CompositeUI;
  23. using System.Windows.Forms.Integration;
  24. using System.Windows;
  25. using Microsoft.Practices.CompositeUI.WPF;
  26. using Microsoft.Practices.CompositeUI.SmartParts;
  27. namespace Microsoft.Practices.CompositeUI.WPF
  28. {
  29. /// <summary>
  30. /// Implements a workspace which shows <see cref="Control"/> layered as in a deck.
  31. /// </summary>
  32. [DesignerCategory("Code")]
  33. public partial class DeckWorkspace : Control, IComposableWorkspace<Control, WPFSmartPartInfo>
  34. {
  35. private ElementHostWorkspaceComposer<Control, WPFSmartPartInfo> composer;
  36. private bool isDisposing = false;
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="DeckWorkspace"/> class.
  39. /// </summary>
  40. public DeckWorkspace()
  41. {
  42. composer = new ElementHostWorkspaceComposer<Control, WPFSmartPartInfo>(this);
  43. }
  44. /// <summary>
  45. /// Gets the controls that the deck currently contains.
  46. /// </summary>
  47. [Browsable(false)]
  48. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  49. public ReadOnlyCollection<object> SmartParts
  50. {
  51. get
  52. {
  53. object[] controls = new object[composer.SmartParts.Count];
  54. composer.SmartParts.CopyTo(controls, 0);
  55. return new ReadOnlyCollection<object>(controls);
  56. }
  57. }
  58. /// <summary>
  59. /// Gets the currently active smart part.
  60. /// </summary>
  61. [Browsable(false)]
  62. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  63. public object ActiveSmartPart
  64. {
  65. get { return composer.ActiveSmartPart; }
  66. }
  67. /// <summary>
  68. /// Dependency injection setter property to get the <see cref="WorkItem"/> where the
  69. /// object is contained.
  70. /// </summary>
  71. [ServiceDependency]
  72. public WorkItem WorkItem
  73. {
  74. set { composer.WorkItem = value; }
  75. }
  76. /// <summary>
  77. /// Dependency injection setter property to get the <see cref="IWPFUIElementAdapter"/>
  78. /// </summary>
  79. [ServiceDependency]
  80. public IWPFUIElementAdapter WPFUIElementAdapter
  81. {
  82. set { composer.WPFUIElementAdapter = value; }
  83. }
  84. /// <summary>
  85. /// Overriden to control when the workspace is being disposed to disable the control activation logic.
  86. /// </summary>
  87. /// <param name="disposing">A flag that indicates if <see cref="IDisposable.Dispose"/> was called.</param>
  88. protected override void Dispose(bool disposing)
  89. {
  90. isDisposing = disposing;
  91. base.Dispose(disposing);
  92. }
  93. #region Private
  94. private void ControlDisposed(object sender, EventArgs e)
  95. {
  96. Control control = sender as Control;
  97. if (this.isDisposing == false && control != null)
  98. {
  99. composer.ForceClose(control);
  100. }
  101. }
  102. private void FireSmartPartActivated(object smartPart)
  103. {
  104. if (this.SmartPartActivated != null)
  105. {
  106. this.SmartPartActivated(this, new WorkspaceEventArgs(smartPart));
  107. }
  108. }
  109. private void ActivateTopmost()
  110. {
  111. if (this.Controls.Count != 0)
  112. {
  113. Activate(this.Controls[0]);
  114. }
  115. }
  116. #endregion
  117. #region Protected virtual implementation
  118. /// <summary>
  119. /// Activates the smart part.
  120. /// </summary>
  121. protected virtual void OnActivate(Control smartPart)
  122. {
  123. //this.Controls.SetChildIndex(smartPart, this.Controls.Count - 1);
  124. smartPart.BringToFront();
  125. smartPart.Show();
  126. }
  127. /// <summary>
  128. /// Applies the smart part display information to the smart part.
  129. /// </summary>
  130. protected virtual void OnApplySmartPartInfo(Control smartPart, WPFSmartPartInfo smartPartInfo)
  131. {
  132. // No op. We do not use the SPI for anything actually.
  133. }
  134. /// <summary>
  135. /// Closes the smart part.
  136. /// </summary>
  137. protected virtual void OnClose(Control smartPart)
  138. {
  139. this.Controls.Remove(smartPart);
  140. smartPart.Disposed -= ControlDisposed;
  141. ActivateTopmost();
  142. }
  143. /// <summary>
  144. /// Hides the smart part.
  145. /// </summary>
  146. protected virtual void OnHide(Control smartPart)
  147. {
  148. smartPart.SendToBack();
  149. ActivateTopmost();
  150. }
  151. /// <summary>
  152. /// Shows the control.
  153. /// </summary>
  154. protected virtual void OnShow(Control smartPart, WPFSmartPartInfo smartPartInfo)
  155. {
  156. smartPart.Dock = DockStyle.Fill;
  157. this.Controls.Add(smartPart);
  158. smartPart.Disposed += ControlDisposed;
  159. Activate(smartPart);
  160. }
  161. /// <summary>
  162. /// Raises the <see cref="SmartPartActivated"/> event.
  163. /// </summary>
  164. protected virtual void OnSmartPartActivated(WorkspaceEventArgs e)
  165. {
  166. if (SmartPartActivated != null)
  167. {
  168. SmartPartActivated(this, e);
  169. }
  170. }
  171. /// <summary>
  172. /// Raises the <see cref="SmartPartClosing"/> event.
  173. /// </summary>
  174. protected void OnSmartPartClosing(WorkspaceCancelEventArgs e)
  175. {
  176. if (SmartPartClosing != null)
  177. {
  178. SmartPartClosing(this, e);
  179. }
  180. }
  181. /// <summary>
  182. /// Converts a smart part information to a compatible one for the workspace.
  183. /// </summary>
  184. protected virtual WPFSmartPartInfo OnConvertFrom(ISmartPartInfo source)
  185. {
  186. return WPFSmartPartInfo.ConvertTo<WPFSmartPartInfo>(source);
  187. }
  188. #endregion
  189. #region IComposableWorkspace<Control,SmartPartInfo> Members
  190. void IComposableWorkspace<Control, WPFSmartPartInfo>.OnActivate(Control smartPart)
  191. {
  192. OnActivate(smartPart);
  193. }
  194. void IComposableWorkspace<Control, WPFSmartPartInfo>.OnApplySmartPartInfo(Control smartPart, WPFSmartPartInfo smartPartInfo)
  195. {
  196. OnApplySmartPartInfo(smartPart, smartPartInfo);
  197. }
  198. void IComposableWorkspace<Control, WPFSmartPartInfo>.OnClose(Control smartPart)
  199. {
  200. OnClose(smartPart);
  201. }
  202. void IComposableWorkspace<Control, WPFSmartPartInfo>.OnHide(Control smartPart)
  203. {
  204. OnHide(smartPart);
  205. }
  206. void IComposableWorkspace<Control, WPFSmartPartInfo>.OnShow(Control smartPart, WPFSmartPartInfo smartPartInfo)
  207. {
  208. OnShow(smartPart, smartPartInfo);
  209. }
  210. void IComposableWorkspace<Control, WPFSmartPartInfo>.RaiseSmartPartActivated(WorkspaceEventArgs e)
  211. {
  212. OnSmartPartActivated(e);
  213. }
  214. void IComposableWorkspace<Control, WPFSmartPartInfo>.RaiseSmartPartClosing(WorkspaceCancelEventArgs e)
  215. {
  216. OnSmartPartClosing(e);
  217. }
  218. WPFSmartPartInfo IComposableWorkspace<Control, WPFSmartPartInfo>.ConvertFrom(ISmartPartInfo source)
  219. {
  220. return OnConvertFrom(source);
  221. }
  222. #endregion
  223. #region IWorkspace Members
  224. /// <summary>
  225. /// See <see cref="IWorkspace.SmartPartClosing"/>.
  226. /// </summary>
  227. public event EventHandler<WorkspaceCancelEventArgs> SmartPartClosing;
  228. /// <summary>
  229. /// See <see cref="IWorkspace.SmartPartActivated"/>.
  230. /// </summary>
  231. public event EventHandler<WorkspaceEventArgs> SmartPartActivated;
  232. /// <summary>
  233. /// See <see cref="IWorkspace.SmartParts"/>.
  234. /// </summary>
  235. ReadOnlyCollection<object> IWorkspace.SmartParts
  236. {
  237. get { return composer.SmartParts; }
  238. }
  239. /// <summary>
  240. /// See <see cref="IWorkspace.ActiveSmartPart"/>.
  241. /// </summary>
  242. object IWorkspace.ActiveSmartPart
  243. {
  244. get { return composer.ActiveSmartPart; }
  245. }
  246. /// <summary>
  247. /// Shows the smart part in a new tab with the given information.
  248. /// </summary>
  249. public void Show(object smartPart, ISmartPartInfo smartPartInfo)
  250. {
  251. composer.Show(smartPart, smartPartInfo);
  252. }
  253. /// <summary>
  254. /// Shows the smart part in a new tab.
  255. /// </summary>
  256. public void Show(object smartPart)
  257. {
  258. composer.Show(smartPart);
  259. }
  260. /// <summary>
  261. /// Hides the smart part and its tab.
  262. /// </summary>
  263. public void Hide(object smartPart)
  264. {
  265. composer.Hide(smartPart);
  266. }
  267. /// <summary>
  268. /// Closes the smart part and removes its tab.
  269. /// </summary>
  270. public void Close(object smartPart)
  271. {
  272. composer.Close(smartPart);
  273. }
  274. /// <summary>
  275. /// Activates the tab the smart part is contained in.
  276. /// </summary>
  277. /// <param name="smartPart"></param>
  278. public void Activate(object smartPart)
  279. {
  280. composer.Activate(smartPart);
  281. }
  282. /// <summary>
  283. /// Applies new layout information on the tab of the smart part.
  284. /// </summary>
  285. public void ApplySmartPartInfo(object smartPart, ISmartPartInfo smartPartInfo)
  286. {
  287. composer.ApplySmartPartInfo(smartPart, smartPartInfo);
  288. }
  289. #endregion
  290. }
  291. }