LayoutAnchorablePane.cs 7.6 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. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Collections.ObjectModel;
  15. using System.Windows.Markup;
  16. using System.ComponentModel;
  17. using System.Xml.Serialization;
  18. namespace Xceed.Wpf.AvalonDock.Layout
  19. {
  20. [ContentProperty("Children")]
  21. [Serializable]
  22. public class LayoutAnchorablePane : LayoutPositionableGroup<LayoutAnchorable>, ILayoutAnchorablePane, ILayoutPositionableElement, ILayoutContentSelector, ILayoutPaneSerializable
  23. {
  24. public LayoutAnchorablePane()
  25. {
  26. }
  27. public LayoutAnchorablePane(LayoutAnchorable anchorable)
  28. {
  29. Children.Add(anchorable);
  30. }
  31. protected override bool GetVisibility()
  32. {
  33. return Children.Count > 0 && Children.Any(c => c.IsVisible);
  34. }
  35. #region SelectedContentIndex
  36. private int _selectedIndex = -1;
  37. public int SelectedContentIndex
  38. {
  39. get { return _selectedIndex; }
  40. set
  41. {
  42. if (value < 0 ||
  43. value >= Children.Count)
  44. value = -1;
  45. if (_selectedIndex != value)
  46. {
  47. RaisePropertyChanging("SelectedContentIndex");
  48. RaisePropertyChanging("SelectedContent");
  49. if (_selectedIndex >= 0 &&
  50. _selectedIndex < Children.Count)
  51. Children[_selectedIndex].IsSelected = false;
  52. _selectedIndex = value;
  53. if (_selectedIndex >= 0 &&
  54. _selectedIndex < Children.Count)
  55. Children[_selectedIndex].IsSelected = true;
  56. RaisePropertyChanged("SelectedContentIndex");
  57. RaisePropertyChanged("SelectedContent");
  58. }
  59. }
  60. }
  61. protected override void ChildMoved(int oldIndex, int newIndex)
  62. {
  63. if (_selectedIndex == oldIndex)
  64. {
  65. RaisePropertyChanging("SelectedContentIndex");
  66. _selectedIndex = newIndex;
  67. RaisePropertyChanged("SelectedContentIndex");
  68. }
  69. base.ChildMoved(oldIndex, newIndex);
  70. }
  71. public LayoutContent SelectedContent
  72. {
  73. get
  74. {
  75. return _selectedIndex == -1 ? null : Children[_selectedIndex];
  76. }
  77. }
  78. #endregion
  79. protected override void OnChildrenCollectionChanged()
  80. {
  81. AutoFixSelectedContent();
  82. for (int i = 0; i < Children.Count; i++)
  83. {
  84. if (Children[i].IsSelected)
  85. {
  86. SelectedContentIndex = i;
  87. break;
  88. }
  89. }
  90. RaisePropertyChanged("CanClose");
  91. RaisePropertyChanged("CanHide");
  92. RaisePropertyChanged("IsDirectlyHostedInFloatingWindow");
  93. base.OnChildrenCollectionChanged();
  94. }
  95. [XmlIgnore]
  96. bool _autoFixSelectedContent = true;
  97. void AutoFixSelectedContent()
  98. {
  99. if (_autoFixSelectedContent)
  100. {
  101. if (SelectedContentIndex >= ChildrenCount)
  102. SelectedContentIndex = Children.Count - 1;
  103. if (SelectedContentIndex == -1 && ChildrenCount > 0)
  104. SelectedContentIndex = 0;
  105. }
  106. }
  107. public int IndexOf(LayoutContent content)
  108. {
  109. var anchorableChild = content as LayoutAnchorable;
  110. if (anchorableChild == null)
  111. return -1;
  112. return Children.IndexOf(anchorableChild);
  113. }
  114. public bool IsDirectlyHostedInFloatingWindow
  115. {
  116. get
  117. {
  118. var parentFloatingWindow = this.FindParent<LayoutAnchorableFloatingWindow>();
  119. if (parentFloatingWindow != null)
  120. return parentFloatingWindow.IsSinglePane;
  121. return false;
  122. //return Parent != null && Parent.ChildrenCount == 1 && Parent.Parent is LayoutFloatingWindow;
  123. }
  124. }
  125. internal void UpdateIsDirectlyHostedInFloatingWindow()
  126. {
  127. RaisePropertyChanged("IsDirectlyHostedInFloatingWindow");
  128. }
  129. public bool IsHostedInFloatingWindow
  130. {
  131. get
  132. {
  133. return this.FindParent<LayoutFloatingWindow>() != null;
  134. }
  135. }
  136. protected override void OnParentChanged(ILayoutContainer oldValue, ILayoutContainer newValue)
  137. {
  138. var oldGroup = oldValue as ILayoutGroup;
  139. if (oldGroup != null)
  140. oldGroup.ChildrenCollectionChanged -= new EventHandler(OnParentChildrenCollectionChanged);
  141. RaisePropertyChanged("IsDirectlyHostedInFloatingWindow");
  142. var newGroup = newValue as ILayoutGroup;
  143. if (newGroup != null)
  144. newGroup.ChildrenCollectionChanged += new EventHandler(OnParentChildrenCollectionChanged);
  145. base.OnParentChanged(oldValue, newValue);
  146. }
  147. void OnParentChildrenCollectionChanged(object sender, EventArgs e)
  148. {
  149. RaisePropertyChanged("IsDirectlyHostedInFloatingWindow");
  150. }
  151. string _id;
  152. string ILayoutPaneSerializable.Id
  153. {
  154. get
  155. {
  156. return _id;
  157. }
  158. set
  159. {
  160. _id = value;
  161. }
  162. }
  163. #region Name
  164. private string _name = null;
  165. public string Name
  166. {
  167. get { return _name; }
  168. set
  169. {
  170. if (_name != value)
  171. {
  172. _name = value;
  173. RaisePropertyChanged("Name");
  174. }
  175. }
  176. }
  177. #endregion
  178. public override void WriteXml(System.Xml.XmlWriter writer)
  179. {
  180. if (_id != null)
  181. writer.WriteAttributeString("Id", _id);
  182. if (_name != null)
  183. writer.WriteAttributeString("Name", _name);
  184. base.WriteXml(writer);
  185. }
  186. public override void ReadXml(System.Xml.XmlReader reader)
  187. {
  188. if (reader.MoveToAttribute("Id"))
  189. _id = reader.Value;
  190. if (reader.MoveToAttribute("Name"))
  191. _name = reader.Value;
  192. _autoFixSelectedContent = false;
  193. base.ReadXml(reader);
  194. _autoFixSelectedContent = true;
  195. AutoFixSelectedContent();
  196. }
  197. public bool CanHide
  198. {
  199. get { return Children.All(a => a.CanHide); }
  200. }
  201. public bool CanClose
  202. {
  203. get { return Children.All(a => a.CanClose);}
  204. }
  205. #if TRACE
  206. public override void ConsoleDump(int tab)
  207. {
  208. System.Diagnostics.Trace.Write( new string( ' ', tab * 4 ) );
  209. System.Diagnostics.Trace.WriteLine( "AnchorablePane()" );
  210. foreach (LayoutElement child in Children)
  211. child.ConsoleDump(tab + 1);
  212. }
  213. #endif
  214. }
  215. }