LayoutAnchorableTabItem.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.Windows;
  15. using System.Collections.ObjectModel;
  16. using System.Windows.Controls;
  17. using System.Windows.Input;
  18. using Xceed.Wpf.AvalonDock.Layout;
  19. using System.Reflection;
  20. using System.Diagnostics;
  21. namespace Xceed.Wpf.AvalonDock.Controls
  22. {
  23. public class LayoutAnchorableTabItem : Control
  24. {
  25. static LayoutAnchorableTabItem()
  26. {
  27. DefaultStyleKeyProperty.OverrideMetadata(typeof(LayoutAnchorableTabItem), new FrameworkPropertyMetadata(typeof(LayoutAnchorableTabItem)));
  28. }
  29. public LayoutAnchorableTabItem()
  30. {
  31. }
  32. #region Model
  33. /// <summary>
  34. /// Model Dependency Property
  35. /// </summary>
  36. public static readonly DependencyProperty ModelProperty =
  37. DependencyProperty.Register("Model", typeof(LayoutContent), typeof(LayoutAnchorableTabItem),
  38. new FrameworkPropertyMetadata((LayoutContent)null,
  39. new PropertyChangedCallback(OnModelChanged)));
  40. /// <summary>
  41. /// Gets or sets the Model property. This dependency property
  42. /// indicates model attached to the anchorable tab item.
  43. /// </summary>
  44. public LayoutContent Model
  45. {
  46. get { return (LayoutContent)GetValue(ModelProperty); }
  47. set { SetValue(ModelProperty, value); }
  48. }
  49. /// <summary>
  50. /// Handles changes to the Model property.
  51. /// </summary>
  52. private static void OnModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  53. {
  54. ((LayoutAnchorableTabItem)d).OnModelChanged(e);
  55. }
  56. /// <summary>
  57. /// Provides derived classes an opportunity to handle changes to the Model property.
  58. /// </summary>
  59. protected virtual void OnModelChanged(DependencyPropertyChangedEventArgs e)
  60. {
  61. if (Model != null)
  62. SetLayoutItem(Model.Root.Manager.GetLayoutItemFromModel(Model));
  63. else
  64. SetLayoutItem(null);
  65. //UpdateLogicalParent();
  66. }
  67. #endregion
  68. #region LayoutItem
  69. /// <summary>
  70. /// LayoutItem Read-Only Dependency Property
  71. /// </summary>
  72. private static readonly DependencyPropertyKey LayoutItemPropertyKey
  73. = DependencyProperty.RegisterReadOnly("LayoutItem", typeof(LayoutItem), typeof(LayoutAnchorableTabItem),
  74. new FrameworkPropertyMetadata((LayoutItem)null));
  75. public static readonly DependencyProperty LayoutItemProperty
  76. = LayoutItemPropertyKey.DependencyProperty;
  77. /// <summary>
  78. /// Gets the LayoutItem property. This dependency property
  79. /// indicates the LayoutItem attached to this tag item.
  80. /// </summary>
  81. public LayoutItem LayoutItem
  82. {
  83. get { return (LayoutItem)GetValue(LayoutItemProperty); }
  84. }
  85. /// <summary>
  86. /// Provides a secure method for setting the LayoutItem property.
  87. /// This dependency property indicates the LayoutItem attached to this tag item.
  88. /// </summary>
  89. /// <param name="value">The new value for the property.</param>
  90. protected void SetLayoutItem(LayoutItem value)
  91. {
  92. SetValue(LayoutItemPropertyKey, value);
  93. }
  94. #endregion
  95. bool _isMouseDown = false;
  96. static LayoutAnchorableTabItem _draggingItem = null;
  97. internal static bool IsDraggingItem()
  98. {
  99. return _draggingItem != null;
  100. }
  101. internal static LayoutAnchorableTabItem GetDraggingItem()
  102. {
  103. return _draggingItem;
  104. }
  105. internal static void ResetDraggingItem()
  106. {
  107. _draggingItem = null;
  108. }
  109. protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
  110. {
  111. base.OnMouseLeftButtonDown(e);
  112. _isMouseDown = true;
  113. _draggingItem = this;
  114. }
  115. protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
  116. {
  117. base.OnMouseMove(e);
  118. if (e.LeftButton != MouseButtonState.Pressed)
  119. {
  120. _isMouseDown = false;
  121. _draggingItem = null;
  122. }
  123. }
  124. protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
  125. {
  126. _isMouseDown = false;
  127. base.OnMouseLeftButtonUp(e);
  128. Model.IsActive = true;
  129. }
  130. protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
  131. {
  132. base.OnMouseLeave(e);
  133. if (_isMouseDown && e.LeftButton == MouseButtonState.Pressed)
  134. {
  135. _draggingItem = this;
  136. }
  137. _isMouseDown = false;
  138. }
  139. protected override void OnMouseEnter(MouseEventArgs e)
  140. {
  141. base.OnMouseEnter(e);
  142. if (_draggingItem != null &&
  143. _draggingItem != this &&
  144. e.LeftButton == MouseButtonState.Pressed)
  145. {
  146. //Trace.WriteLine("Dragging item from {0} to {1}", _draggingItem, this);
  147. var model = Model;
  148. var container = model.Parent as ILayoutContainer;
  149. var containerPane = model.Parent as ILayoutPane;
  150. var childrenList = container.Children.ToList();
  151. containerPane.MoveChild(childrenList.IndexOf(_draggingItem.Model), childrenList.IndexOf(model));
  152. }
  153. }
  154. protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
  155. {
  156. base.OnPreviewGotKeyboardFocus(e);
  157. }
  158. }
  159. }