AnchorablePaneTitle.cs 6.3 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.Windows.Controls;
  15. using System.Windows;
  16. using System.Windows.Input;
  17. using Xceed.Wpf.AvalonDock.Layout;
  18. namespace Xceed.Wpf.AvalonDock.Controls
  19. {
  20. public class AnchorablePaneTitle : Control
  21. {
  22. static AnchorablePaneTitle()
  23. {
  24. IsHitTestVisibleProperty.OverrideMetadata(typeof(AnchorablePaneTitle), new FrameworkPropertyMetadata(true));
  25. FocusableProperty.OverrideMetadata(typeof(AnchorablePaneTitle), new FrameworkPropertyMetadata(false));
  26. DefaultStyleKeyProperty.OverrideMetadata(typeof(AnchorablePaneTitle), new FrameworkPropertyMetadata(typeof(AnchorablePaneTitle)));
  27. }
  28. public AnchorablePaneTitle()
  29. {
  30. }
  31. #region Model
  32. /// <summary>
  33. /// Model Dependency Property
  34. /// </summary>
  35. public static readonly DependencyProperty ModelProperty =
  36. DependencyProperty.Register("Model", typeof(LayoutAnchorable), typeof(AnchorablePaneTitle),
  37. new FrameworkPropertyMetadata((LayoutAnchorable)null, new PropertyChangedCallback(_OnModelChanged)));
  38. /// <summary>
  39. /// Gets or sets the Model property. This dependency property
  40. /// indicates model attached to this view.
  41. /// </summary>
  42. public LayoutAnchorable Model
  43. {
  44. get { return (LayoutAnchorable)GetValue(ModelProperty); }
  45. set { SetValue(ModelProperty, value); }
  46. }
  47. static void _OnModelChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  48. {
  49. ((AnchorablePaneTitle)sender).OnModelChanged(e);
  50. }
  51. /// <summary>
  52. /// Provides derived classes an opportunity to handle changes to the Model property.
  53. /// </summary>
  54. protected virtual void OnModelChanged(DependencyPropertyChangedEventArgs e)
  55. {
  56. if (Model != null)
  57. SetLayoutItem(Model.Root.Manager.GetLayoutItemFromModel(Model));
  58. else
  59. SetLayoutItem(null);
  60. }
  61. #endregion
  62. #region LayoutItem
  63. /// <summary>
  64. /// LayoutItem Read-Only Dependency Property
  65. /// </summary>
  66. private static readonly DependencyPropertyKey LayoutItemPropertyKey
  67. = DependencyProperty.RegisterReadOnly("LayoutItem", typeof(LayoutItem), typeof(AnchorablePaneTitle),
  68. new FrameworkPropertyMetadata((LayoutItem)null));
  69. public static readonly DependencyProperty LayoutItemProperty
  70. = LayoutItemPropertyKey.DependencyProperty;
  71. /// <summary>
  72. /// Gets the LayoutItem property. This dependency property
  73. /// indicates the LayoutItem attached to this tag item.
  74. /// </summary>
  75. public LayoutItem LayoutItem
  76. {
  77. get { return (LayoutItem)GetValue(LayoutItemProperty); }
  78. }
  79. /// <summary>
  80. /// Provides a secure method for setting the LayoutItem property.
  81. /// This dependency property indicates the LayoutItem attached to this tag item.
  82. /// </summary>
  83. /// <param name="value">The new value for the property.</param>
  84. protected void SetLayoutItem(LayoutItem value)
  85. {
  86. SetValue(LayoutItemPropertyKey, value);
  87. }
  88. #endregion
  89. private void OnHide()
  90. {
  91. Model.Hide();
  92. }
  93. private void OnToggleAutoHide()
  94. {
  95. Model.ToggleAutoHide();
  96. }
  97. protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
  98. {
  99. if (e.LeftButton != MouseButtonState.Pressed)
  100. {
  101. _isMouseDown = false;
  102. }
  103. base.OnMouseMove(e);
  104. }
  105. protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
  106. {
  107. base.OnMouseLeave(e);
  108. if (_isMouseDown && e.LeftButton == MouseButtonState.Pressed)
  109. {
  110. var pane = this.FindVisualAncestor<LayoutAnchorablePaneControl>();
  111. if (pane != null)
  112. {
  113. var paneModel = pane.Model as LayoutAnchorablePane;
  114. var manager = paneModel.Root.Manager;
  115. manager.StartDraggingFloatingWindowForPane(paneModel);
  116. }
  117. }
  118. _isMouseDown = false;
  119. }
  120. bool _isMouseDown = false;
  121. protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
  122. {
  123. base.OnMouseLeftButtonDown(e);
  124. if (!e.Handled)
  125. {
  126. bool attachFloatingWindow = false;
  127. var parentFloatingWindow = Model.FindParent<LayoutAnchorableFloatingWindow>();
  128. if (parentFloatingWindow != null)
  129. {
  130. attachFloatingWindow = parentFloatingWindow.Descendents().OfType<LayoutAnchorablePane>().Count() == 1;
  131. }
  132. if (attachFloatingWindow)
  133. {
  134. //the pane is hosted inside a floating window that contains only an anchorable pane so drag the floating window itself
  135. var floatingWndControl = Model.Root.Manager.FloatingWindows.Single(fwc => fwc.Model == parentFloatingWindow);
  136. floatingWndControl.AttachDrag(false);
  137. }
  138. else
  139. _isMouseDown = true;//normal drag
  140. }
  141. }
  142. protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
  143. {
  144. _isMouseDown = false;
  145. base.OnMouseLeftButtonUp(e);
  146. if (Model != null)
  147. Model.IsActive = true;//FocusElementManager.SetFocusOnLastElement(Model);
  148. }
  149. }
  150. }