LayoutDocumentFloatingWindowControl.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.Data;
  15. using Xceed.Wpf.AvalonDock.Layout;
  16. using System.Windows;
  17. using System.Windows.Controls.Primitives;
  18. using System.Windows.Media;
  19. using Microsoft.Windows.Shell;
  20. namespace Xceed.Wpf.AvalonDock.Controls
  21. {
  22. public class LayoutDocumentFloatingWindowControl : LayoutFloatingWindowControl
  23. {
  24. static LayoutDocumentFloatingWindowControl()
  25. {
  26. DefaultStyleKeyProperty.OverrideMetadata(typeof(LayoutDocumentFloatingWindowControl), new FrameworkPropertyMetadata(typeof(LayoutDocumentFloatingWindowControl)));
  27. }
  28. internal LayoutDocumentFloatingWindowControl(LayoutDocumentFloatingWindow model)
  29. :base(model)
  30. {
  31. _model = model;
  32. }
  33. LayoutDocumentFloatingWindow _model;
  34. public override ILayoutElement Model
  35. {
  36. get { return _model; }
  37. }
  38. public LayoutItem RootDocumentLayoutItem
  39. {
  40. get { return _model.Root.Manager.GetLayoutItemFromModel(_model.RootDocument); }
  41. }
  42. protected override void OnInitialized(EventArgs e)
  43. {
  44. base.OnInitialized(e);
  45. if (_model.RootDocument == null)
  46. {
  47. InternalClose();
  48. }
  49. else
  50. {
  51. var manager = _model.Root.Manager;
  52. Content = manager.CreateUIElementForModel(_model.RootDocument);
  53. _model.RootDocumentChanged += new EventHandler(_model_RootDocumentChanged);
  54. }
  55. }
  56. void _model_RootDocumentChanged(object sender, EventArgs e)
  57. {
  58. if (_model.RootDocument == null)
  59. {
  60. InternalClose();
  61. }
  62. }
  63. protected override IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  64. {
  65. switch (msg)
  66. {
  67. case Win32Helper.WM_NCLBUTTONDOWN: //Left button down on title -> start dragging over docking manager
  68. if (wParam.ToInt32() == Win32Helper.HT_CAPTION)
  69. {
  70. if (_model.RootDocument != null)
  71. _model.RootDocument.IsActive = true;
  72. }
  73. break;
  74. case Win32Helper.WM_NCRBUTTONUP:
  75. if (wParam.ToInt32() == Win32Helper.HT_CAPTION)
  76. {
  77. if (OpenContextMenu())
  78. handled = true;
  79. if (_model.Root.Manager.ShowSystemMenu)
  80. WindowChrome.GetWindowChrome(this).ShowSystemMenu = !handled;
  81. else
  82. WindowChrome.GetWindowChrome(this).ShowSystemMenu = false;
  83. }
  84. break;
  85. }
  86. return base.FilterMessage(hwnd, msg, wParam, lParam, ref handled);
  87. }
  88. bool OpenContextMenu()
  89. {
  90. var ctxMenu = _model.Root.Manager.DocumentContextMenu;
  91. if (ctxMenu != null && RootDocumentLayoutItem != null)
  92. {
  93. ctxMenu.PlacementTarget = null;
  94. ctxMenu.Placement = PlacementMode.MousePoint;
  95. ctxMenu.DataContext = RootDocumentLayoutItem;
  96. ctxMenu.IsOpen = true;
  97. return true;
  98. }
  99. return false;
  100. }
  101. protected override void OnClosed(EventArgs e)
  102. {
  103. var root = Model.Root;
  104. root.Manager.RemoveFloatingWindow(this);
  105. root.CollectGarbage();
  106. base.OnClosed(e);
  107. if (!CloseInitiatedByUser)
  108. {
  109. root.FloatingWindows.Remove(_model);
  110. }
  111. _model.RootDocumentChanged -= new EventHandler(_model_RootDocumentChanged);
  112. }
  113. }
  114. }