LayoutDocumentPane.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. namespace Xceed.Wpf.AvalonDock.Layout
  17. {
  18. [ContentProperty("Children")]
  19. [Serializable]
  20. public class LayoutDocumentPane : LayoutPositionableGroup<LayoutContent>, ILayoutDocumentPane, ILayoutPositionableElement, ILayoutContentSelector, ILayoutPaneSerializable
  21. {
  22. public LayoutDocumentPane()
  23. {
  24. }
  25. public LayoutDocumentPane(LayoutContent firstChild)
  26. {
  27. Children.Add(firstChild);
  28. }
  29. protected override bool GetVisibility()
  30. {
  31. if (Parent is LayoutDocumentPaneGroup)
  32. return ChildrenCount > 0;
  33. return true;
  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 { return _selectedIndex == -1 ? null : Children[_selectedIndex]; }
  74. }
  75. #endregion
  76. protected override void OnChildrenCollectionChanged()
  77. {
  78. if (SelectedContentIndex >= ChildrenCount)
  79. SelectedContentIndex = Children.Count - 1;
  80. if (SelectedContentIndex == -1 && ChildrenCount > 0)
  81. {
  82. if (Root == null)//if I'm not yet connected just switch to first document
  83. SelectedContentIndex = 0;
  84. else
  85. {
  86. var childrenToSelect = Children.OrderByDescending(c => c.LastActivationTimeStamp.GetValueOrDefault()).First();
  87. SelectedContentIndex = Children.IndexOf(childrenToSelect);
  88. childrenToSelect.IsActive = true;
  89. }
  90. }
  91. base.OnChildrenCollectionChanged();
  92. RaisePropertyChanged("ChildrenSorted");
  93. }
  94. public int IndexOf(LayoutContent content)
  95. {
  96. return Children.IndexOf(content);
  97. }
  98. protected override void OnIsVisibleChanged()
  99. {
  100. UpdateParentVisibility();
  101. base.OnIsVisibleChanged();
  102. }
  103. void UpdateParentVisibility()
  104. {
  105. var parentPane = Parent as ILayoutElementWithVisibility;
  106. if (parentPane != null)
  107. parentPane.ComputeVisibility();
  108. }
  109. public IEnumerable<LayoutContent> ChildrenSorted
  110. {
  111. get
  112. {
  113. var listSorted = Children.ToList();
  114. listSorted.Sort();
  115. return listSorted;
  116. }
  117. }
  118. string _id;
  119. string ILayoutPaneSerializable.Id
  120. {
  121. get
  122. {
  123. return _id;
  124. }
  125. set
  126. {
  127. _id = value;
  128. }
  129. }
  130. public override void WriteXml(System.Xml.XmlWriter writer)
  131. {
  132. if (_id != null)
  133. writer.WriteAttributeString("Id", _id);
  134. base.WriteXml(writer);
  135. }
  136. public override void ReadXml(System.Xml.XmlReader reader)
  137. {
  138. if (reader.MoveToAttribute("Id"))
  139. _id = reader.Value;
  140. base.ReadXml(reader);
  141. }
  142. #if TRACE
  143. public override void ConsoleDump(int tab)
  144. {
  145. System.Diagnostics.Trace.Write( new string( ' ', tab * 4 ) );
  146. System.Diagnostics.Trace.WriteLine( "DocumentPane()" );
  147. foreach (LayoutElement child in Children)
  148. child.ConsoleDump(tab + 1);
  149. }
  150. #endif
  151. }
  152. }