WindowChrome.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. /**************************************************************************\
  11. Copyright Microsoft Corporation. All Rights Reserved.
  12. \**************************************************************************/
  13. namespace Microsoft.Windows.Shell
  14. {
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics.CodeAnalysis;
  18. using System.Windows;
  19. using System.Windows.Data;
  20. using Standard;
  21. public class WindowChrome : Freezable
  22. {
  23. private struct _SystemParameterBoundProperty
  24. {
  25. public string SystemParameterPropertyName { get; set; }
  26. public DependencyProperty DependencyProperty { get; set; }
  27. }
  28. // Named property available for fully extending the glass frame.
  29. public static Thickness GlassFrameCompleteThickness { get { return new Thickness(-1); } }
  30. #region Attached Properties
  31. public static readonly DependencyProperty WindowChromeProperty = DependencyProperty.RegisterAttached(
  32. "WindowChrome",
  33. typeof(WindowChrome),
  34. typeof(WindowChrome),
  35. new PropertyMetadata(null, _OnChromeChanged));
  36. private static void _OnChromeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  37. {
  38. // The different design tools handle drawing outside their custom window objects differently.
  39. // Rather than try to support this concept in the design surface let the designer draw its own
  40. // chrome anyways.
  41. // There's certainly room for improvement here.
  42. if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(d))
  43. {
  44. return;
  45. }
  46. var window = (Window)d;
  47. var newChrome = (WindowChrome)e.NewValue;
  48. Assert.IsNotNull(window);
  49. // Update the ChromeWorker with this new object.
  50. // If there isn't currently a worker associated with the Window then assign a new one.
  51. // There can be a many:1 relationship of to Window to WindowChrome objects, but a 1:1 for a Window and a WindowChromeWorker.
  52. WindowChromeWorker chromeWorker = WindowChromeWorker.GetWindowChromeWorker(window);
  53. if (chromeWorker == null)
  54. {
  55. chromeWorker = new WindowChromeWorker();
  56. WindowChromeWorker.SetWindowChromeWorker(window, chromeWorker);
  57. }
  58. chromeWorker.SetWindowChrome(newChrome);
  59. }
  60. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
  61. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  62. public static WindowChrome GetWindowChrome(Window window)
  63. {
  64. Verify.IsNotNull(window, "window");
  65. return (WindowChrome)window.GetValue(WindowChromeProperty);
  66. }
  67. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
  68. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  69. public static void SetWindowChrome(Window window, WindowChrome chrome)
  70. {
  71. Verify.IsNotNull(window, "window");
  72. window.SetValue(WindowChromeProperty, chrome);
  73. }
  74. public static readonly DependencyProperty IsHitTestVisibleInChromeProperty = DependencyProperty.RegisterAttached(
  75. "IsHitTestVisibleInChrome",
  76. typeof(bool),
  77. typeof(WindowChrome),
  78. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));
  79. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
  80. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  81. public static bool GetIsHitTestVisibleInChrome(IInputElement inputElement)
  82. {
  83. Verify.IsNotNull(inputElement, "inputElement");
  84. var dobj = inputElement as DependencyObject;
  85. if (dobj == null)
  86. {
  87. throw new ArgumentException("The element must be a DependencyObject", "inputElement");
  88. }
  89. return (bool)dobj.GetValue(IsHitTestVisibleInChromeProperty);
  90. }
  91. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
  92. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  93. public static void SetIsHitTestVisibleInChrome(IInputElement inputElement, bool hitTestVisible)
  94. {
  95. Verify.IsNotNull(inputElement, "inputElement");
  96. var dobj = inputElement as DependencyObject;
  97. if (dobj == null)
  98. {
  99. throw new ArgumentException("The element must be a DependencyObject", "inputElement");
  100. }
  101. dobj.SetValue(IsHitTestVisibleInChromeProperty, hitTestVisible);
  102. }
  103. #endregion
  104. #region Dependency Properties
  105. public static readonly DependencyProperty CaptionHeightProperty = DependencyProperty.Register(
  106. "CaptionHeight",
  107. typeof(double),
  108. typeof(WindowChrome),
  109. new PropertyMetadata(
  110. 0d,
  111. (d, e) => ((WindowChrome)d)._OnPropertyChangedThatRequiresRepaint()),
  112. value => (double)value >= 0d);
  113. /// <summary>The extent of the top of the window to treat as the caption.</summary>
  114. public double CaptionHeight
  115. {
  116. get { return (double)GetValue(CaptionHeightProperty); }
  117. set { SetValue(CaptionHeightProperty, value); }
  118. }
  119. public static readonly DependencyProperty ResizeBorderThicknessProperty = DependencyProperty.Register(
  120. "ResizeBorderThickness",
  121. typeof(Thickness),
  122. typeof(WindowChrome),
  123. new PropertyMetadata(default(Thickness)),
  124. (value) => Utility.IsThicknessNonNegative((Thickness)value));
  125. public Thickness ResizeBorderThickness
  126. {
  127. get { return (Thickness)GetValue(ResizeBorderThicknessProperty); }
  128. set { SetValue(ResizeBorderThicknessProperty, value); }
  129. }
  130. public static readonly DependencyProperty GlassFrameThicknessProperty = DependencyProperty.Register(
  131. "GlassFrameThickness",
  132. typeof(Thickness),
  133. typeof(WindowChrome),
  134. new PropertyMetadata(
  135. default(Thickness),
  136. (d, e) => ((WindowChrome)d)._OnPropertyChangedThatRequiresRepaint(),
  137. (d, o) => _CoerceGlassFrameThickness((Thickness)o)));
  138. private static object _CoerceGlassFrameThickness(Thickness thickness)
  139. {
  140. // If it's explicitly set, but set to a thickness with at least one negative side then
  141. // coerce the value to the stock GlassFrameCompleteThickness.
  142. if (!Utility.IsThicknessNonNegative(thickness))
  143. {
  144. return GlassFrameCompleteThickness;
  145. }
  146. return thickness;
  147. }
  148. public Thickness GlassFrameThickness
  149. {
  150. get { return (Thickness)GetValue(GlassFrameThicknessProperty); }
  151. set { SetValue(GlassFrameThicknessProperty, value); }
  152. }
  153. public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(
  154. "CornerRadius",
  155. typeof(CornerRadius),
  156. typeof(WindowChrome),
  157. new PropertyMetadata(
  158. default(CornerRadius),
  159. (d, e) => ((WindowChrome)d)._OnPropertyChangedThatRequiresRepaint()),
  160. (value) => Utility.IsCornerRadiusValid((CornerRadius)value));
  161. public CornerRadius CornerRadius
  162. {
  163. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  164. set { SetValue(CornerRadiusProperty, value); }
  165. }
  166. #region ShowSystemMenu
  167. /// <summary>
  168. /// Gets or sets the ShowSystemMenu property. This dependency property
  169. /// indicates if the system menu should be shown at right click on the caption.
  170. /// </summary>
  171. public bool ShowSystemMenu
  172. {
  173. get;
  174. set;
  175. }
  176. #endregion
  177. #endregion
  178. protected override Freezable CreateInstanceCore()
  179. {
  180. return new WindowChrome();
  181. }
  182. private static readonly List<_SystemParameterBoundProperty> _BoundProperties = new List<_SystemParameterBoundProperty>
  183. {
  184. new _SystemParameterBoundProperty { DependencyProperty = CornerRadiusProperty, SystemParameterPropertyName = "WindowCornerRadius" },
  185. new _SystemParameterBoundProperty { DependencyProperty = CaptionHeightProperty, SystemParameterPropertyName = "WindowCaptionHeight" },
  186. new _SystemParameterBoundProperty { DependencyProperty = ResizeBorderThicknessProperty, SystemParameterPropertyName = "WindowResizeBorderThickness" },
  187. new _SystemParameterBoundProperty { DependencyProperty = GlassFrameThicknessProperty, SystemParameterPropertyName = "WindowNonClientFrameThickness" },
  188. };
  189. public WindowChrome()
  190. {
  191. // Effective default values for some of these properties are set to be bindings
  192. // that set them to system defaults.
  193. // A more correct way to do this would be to Coerce the value iff the source of the DP was the default value.
  194. // Unfortunately with the current property system we can't detect whether the value being applied at the time
  195. // of the coersion is the default.
  196. foreach (var bp in _BoundProperties)
  197. {
  198. // This list must be declared after the DP's are assigned.
  199. Assert.IsNotNull(bp.DependencyProperty);
  200. BindingOperations.SetBinding(
  201. this,
  202. bp.DependencyProperty,
  203. new Binding
  204. {
  205. Source = SystemParameters2.Current,
  206. Path = new PropertyPath(bp.SystemParameterPropertyName),
  207. Mode = BindingMode.OneWay,
  208. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
  209. });
  210. }
  211. }
  212. private void _OnPropertyChangedThatRequiresRepaint()
  213. {
  214. var handler = PropertyChangedThatRequiresRepaint;
  215. if (handler != null)
  216. {
  217. handler(this, EventArgs.Empty);
  218. }
  219. }
  220. internal event EventHandler PropertyChangedThatRequiresRepaint;
  221. }
  222. }