Extentions.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.Windows.Media;
  16. using System.Windows.Media.Media3D;
  17. using System.Diagnostics;
  18. using System.Runtime.InteropServices;
  19. namespace Xceed.Wpf.AvalonDock.Layout
  20. {
  21. public static class Extensions
  22. {
  23. public static IEnumerable<ILayoutElement> Descendents(this ILayoutElement element)
  24. {
  25. var container = element as ILayoutContainer;
  26. if (container != null)
  27. {
  28. foreach (var childElement in container.Children)
  29. {
  30. yield return childElement;
  31. foreach (var childChildElement in childElement.Descendents())
  32. yield return childChildElement;
  33. }
  34. }
  35. }
  36. public static T FindParent<T>(this ILayoutElement element) //where T : ILayoutContainer
  37. {
  38. var parent = element.Parent;
  39. while (parent != null &&
  40. !(parent is T))
  41. parent = parent.Parent;
  42. return (T)parent;
  43. }
  44. public static ILayoutRoot GetRoot(this ILayoutElement element) //where T : ILayoutContainer
  45. {
  46. if (element is ILayoutRoot)
  47. return element as ILayoutRoot;
  48. var parent = element.Parent;
  49. while (parent != null &&
  50. !(parent is ILayoutRoot))
  51. parent = parent.Parent;
  52. return (ILayoutRoot)parent;
  53. }
  54. public static bool ContainsChildOfType<T>(this ILayoutContainer element)
  55. {
  56. foreach (var childElement in element.Descendents())
  57. if (childElement is T)
  58. return true;
  59. return false;
  60. }
  61. public static bool ContainsChildOfType<T, S>(this ILayoutContainer container)
  62. {
  63. foreach (var childElement in container.Descendents())
  64. if (childElement is T || childElement is S)
  65. return true;
  66. return false;
  67. }
  68. public static bool IsOfType<T, S>(this ILayoutContainer container)
  69. {
  70. return container is T || container is S;
  71. }
  72. public static AnchorSide GetSide(this ILayoutElement element)
  73. {
  74. var parentContainer = element.Parent as ILayoutOrientableGroup;
  75. if (parentContainer != null)
  76. {
  77. if (!parentContainer.ContainsChildOfType<LayoutDocumentPaneGroup, LayoutDocumentPane>())
  78. return GetSide(parentContainer);
  79. foreach (var childElement in parentContainer.Children)
  80. {
  81. if (childElement == element ||
  82. childElement.Descendents().Contains(element))
  83. return parentContainer.Orientation == System.Windows.Controls.Orientation.Horizontal ?
  84. AnchorSide.Left : AnchorSide.Top;
  85. var childElementAsContainer = childElement as ILayoutContainer;
  86. if (childElementAsContainer != null &&
  87. (childElementAsContainer.IsOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>() ||
  88. childElementAsContainer.ContainsChildOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>()))
  89. {
  90. return parentContainer.Orientation == System.Windows.Controls.Orientation.Horizontal ?
  91. AnchorSide.Right : AnchorSide.Bottom;
  92. }
  93. }
  94. }
  95. Debug.Fail("Unable to find the side for an element, possible layout problem!");
  96. return AnchorSide.Right;
  97. }
  98. internal static void KeepInsideNearestMonitor(this ILayoutElementForFloatingWindow paneInsideFloatingWindow)
  99. {
  100. Win32Helper.RECT r = new Win32Helper.RECT();
  101. r.Left = (int)paneInsideFloatingWindow.FloatingLeft;
  102. r.Top = (int)paneInsideFloatingWindow.FloatingTop;
  103. r.Bottom = r.Top + (int)paneInsideFloatingWindow.FloatingHeight;
  104. r.Right = r.Left + (int)paneInsideFloatingWindow.FloatingWidth;
  105. uint MONITOR_DEFAULTTONEAREST = 0x00000002;
  106. uint MONITOR_DEFAULTTONULL = 0x00000000;
  107. System.IntPtr monitor = Win32Helper.MonitorFromRect(ref r, MONITOR_DEFAULTTONULL);
  108. if (monitor == System.IntPtr.Zero)
  109. {
  110. System.IntPtr nearestmonitor = Win32Helper.MonitorFromRect(ref r, MONITOR_DEFAULTTONEAREST);
  111. if (nearestmonitor != System.IntPtr.Zero)
  112. {
  113. Win32Helper.MonitorInfo monitorInfo = new Win32Helper.MonitorInfo();
  114. monitorInfo.Size = Marshal.SizeOf(monitorInfo);
  115. Win32Helper.GetMonitorInfo(nearestmonitor, monitorInfo);
  116. if (paneInsideFloatingWindow.FloatingLeft < monitorInfo.Work.Left)
  117. {
  118. paneInsideFloatingWindow.FloatingLeft = monitorInfo.Work.Left + 10;
  119. }
  120. if (paneInsideFloatingWindow.FloatingLeft + paneInsideFloatingWindow.FloatingWidth > monitorInfo.Work.Right)
  121. {
  122. paneInsideFloatingWindow.FloatingLeft = monitorInfo.Work.Right - (paneInsideFloatingWindow.FloatingWidth + 10);
  123. }
  124. if (paneInsideFloatingWindow.FloatingTop < monitorInfo.Work.Top)
  125. {
  126. paneInsideFloatingWindow.FloatingTop = monitorInfo.Work.Top + 10;
  127. }
  128. if (paneInsideFloatingWindow.FloatingTop + paneInsideFloatingWindow.FloatingHeight > monitorInfo.Work.Bottom)
  129. {
  130. paneInsideFloatingWindow.FloatingTop = monitorInfo.Work.Bottom - (paneInsideFloatingWindow.FloatingHeight + 10);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }