Index: src/DeltaShell/DeltaShell.Gui/Forms/ViewManager/AvalonDockDockingManager.cs =================================================================== diff -u -r8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9 -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/DeltaShell/DeltaShell.Gui/Forms/ViewManager/AvalonDockDockingManager.cs (.../AvalonDockDockingManager.cs) (revision 8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9) +++ src/DeltaShell/DeltaShell.Gui/Forms/ViewManager/AvalonDockDockingManager.cs (.../AvalonDockDockingManager.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -20,26 +20,27 @@ using Xceed.Wpf.AvalonDock; using Xceed.Wpf.AvalonDock.Layout; using MouseEventArgs = System.Windows.Forms.MouseEventArgs; +using Orientation = System.Windows.Controls.Orientation; using Point = System.Drawing.Point; namespace DeltaShell.Gui.Forms.ViewManager { public class AvalonDockDockingManager : IDockingManager { - private static readonly Bitmap LockImage = Properties.Resources.lock_edit; + public event EventHandler ViewBarClosing; - private DockingManager dockingManager; - private ViewLocation[] dockingLocations; + public event EventHandler ViewActivated; - private List views; + public event Action ViewSelectionMouseDown; + private static readonly Bitmap LockImage = Resources.lock_edit; + + private readonly DockingManager dockingManager; + private readonly ViewLocation[] dockingLocations; + + private readonly List views; private IView activeView; - - private List hostControls; - public IEnumerable Views - { - get { return views; } - } + private readonly List hostControls; public AvalonDockDockingManager(DockingManager dockingManager, ViewLocation[] dockingLocations) { @@ -54,35 +55,239 @@ this.dockingManager.ActiveContentChanged += DockingManagerOnActiveContentChanged; this.dockingManager.DocumentContextMenu = null; // don't show avalons own context menu for documents this.dockingManager.LostKeyboardFocus += (sender, args) => + { + // Validate activeView when switching to other wpf control like ribbon + var control = activeView as UserControl; + if (control != null) { - // Validate activeView when switching to other wpf control like ribbon - var control = activeView as UserControl; - if (control != null) - { - control.Validate(); - } - }; + control.Validate(); + } + }; } - void DockingManagerMouseDown(object sender, MouseButtonEventArgs e) + public IEnumerable Views { + get + { + return views; + } + } + + /// + /// Called after layout is changed (e.g. loaded). + /// + public void OnLayoutChange() + { + var existingContents = dockingManager.Layout.Descendents().OfType().ToArray(); + var existingHostControls = existingContents.Select(c => c.Content as WindowsFormsHost); + + foreach (var view in views.ToArray()) + { + if (existingHostControls.Any(c => c != null && c.Child == view)) + { + continue; + } + + // view removed + var index = views.IndexOf(view); + var control = view as Control; + if (control != null) + { + control.TextChanged -= ControlOnTextChanged; + } + + view.Dispose(); + view.Data = null; + views.RemoveAt(index); + hostControls.RemoveAt(index); + + if (views.Count != hostControls.Count) + { + throw new InvalidOperationException(); + } + } + + // subscribe to anchorables (this must be much simpler) + var anchorables = dockingManager.Layout.Descendents().OfType().ToArray(); + + foreach (var layoutAnchorable in anchorables) + { + layoutAnchorable.PropertyChanged -= AnchorableOnPropertyChanged; + layoutAnchorable.PropertyChanged += AnchorableOnPropertyChanged; + } + } + + public void Dispose() {} + + public void Add(IView view, ViewLocation location) + { + if (views.Contains(view)) + { + throw new InvalidOperationException(Resources.AvalonDockDockingManager_Add_View_was_already_added__activate_it_instead_of_add); + } + + var control = view as Control; + if (control != null) + { + control.TextChanged += ControlOnTextChanged; + } + + views.Add(view); + + if (dockingLocations.Contains(ViewLocation.Document)) + { + AddDocumentView(view); + } + else + { + AddToolView(view, location); + } + + if (ViewActivated != null) + { + ViewActivated(this, new ActiveViewChangeEventArgs + { + View = view + }); + } + } + + public void Remove(IView view, bool removeTabFromDockingbar) + { + if (removeTabFromDockingbar) + { + var layoutContent = GetLayoutContent(view); + var index = views.IndexOf(view); + + CleanupHostControl(index); + + if (layoutContent != null) + { + layoutContent.Close(); + } + } + else + { + var index = views.IndexOf(view); + CleanupHostControl(index); + } + + var control = view as Control; + if (control != null) + { + control.TextChanged -= ControlOnTextChanged; + } + + var reusable = view as IReusableView; + if (reusable != null) + { + reusable.LockedChanged -= ReusableLockedChanged; + } + + views.Remove(view); + + if (views.Count != hostControls.Count) + { + throw new InvalidOperationException(); + } + + view.Dispose(); + + activeView = GetView(dockingManager.ActiveContent); + + if (ViewActivated != null) + { + ViewActivated(this, new ActiveViewChangeEventArgs + { + View = activeView + }); + } + } + + public void SetToolTip(IView view, string tooltip) + { + var layoutDocument = GetLayoutContent(view); + if (layoutDocument != null) + { + layoutDocument.ToolTip = tooltip; + } + } + + public void ActivateView(IView view) + { + if (view == null || !views.Contains(view)) + { + return; + } + + if (dockingLocations.Contains(ViewLocation.Document)) + { + var layoutDocument = dockingManager.Layout.Descendents().OfType().FirstOrDefault(a => ContainsView(a, view)); + + // fast fix - check why it is null! + if (layoutDocument == null) + { + return; + } + + if (!layoutDocument.IsActive) + { + dockingManager.Layout.ActiveContent = layoutDocument; + } + } + else + { + var layoutAnchorable = dockingManager.Layout.Descendents().OfType().FirstOrDefault(a => ContainsView(a, view)); + + // fast fix - check why it is null! + if (layoutAnchorable == null) + { + return; + } + + if (layoutAnchorable.IsHidden) + { + layoutAnchorable.Show(); + } + else if (layoutAnchorable.IsVisible) + { + layoutAnchorable.IsActive = true; + } + else + { + throw new NotImplementedException(); + } + // anchorable.AddToLayout(dockManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most); + } + } + + private void DockingManagerMouseDown(object sender, MouseButtonEventArgs e) + { if (e.MiddleButton == MouseButtonState.Pressed) + { return; // view already being closed by avalon dock itself + } if (ViewSelectionMouseDown == null) + { return; + } var layoutDocument = GetLayoutDocumentFromUIElement(Mouse.DirectlyOver); - if (layoutDocument == null) + if (layoutDocument == null) + { return; + } var view = ((WindowsFormsHost) layoutDocument.Content).Child as IView; - if (view == null) + if (view == null) + { return; - + } + var position = ToViewCoordinate(e, view); ViewSelectionMouseDown(view, new MouseEventArgs(GetMouseButtons(e.LeftButton, e.MiddleButton, e.RightButton), - 1, position.X, position.Y, 0), view); + 1, position.X, position.Y, 0), view); } private static Point ToViewCoordinate(MouseButtonEventArgs e, IView view) @@ -95,23 +300,33 @@ { var frameworkElement = inputElement as FrameworkElement; if (frameworkElement != null) + { return frameworkElement.DataContext as LayoutDocument; + } var frameworkContentElement = inputElement as FrameworkContentElement; if (frameworkContentElement != null) + { return frameworkContentElement.DataContext as LayoutDocument; + } return null; } private static MouseButtons GetMouseButtons(MouseButtonState leftButton, MouseButtonState middleButton, MouseButtonState rightButton) { if (leftButton == MouseButtonState.Pressed) + { return MouseButtons.Left; + } if (middleButton == MouseButtonState.Pressed) + { return MouseButtons.Middle; + } if (rightButton == MouseButtonState.Pressed) + { return MouseButtons.Right; + } return MouseButtons.None; } @@ -140,7 +355,10 @@ if (ViewActivated != null) { - ViewActivated(this, new ActiveViewChangeEventArgs { View = view }); + ViewActivated(this, new ActiveViewChangeEventArgs + { + View = view + }); } } @@ -161,7 +379,10 @@ { if (ViewBarClosing != null) { - var viewEventArgs = new DockTabClosingEventArgs {View = view}; + var viewEventArgs = new DockTabClosingEventArgs + { + View = view + }; ViewBarClosing(this, viewEventArgs); if (viewEventArgs.Cancel) { @@ -172,62 +393,27 @@ } } - public void Dispose() - { - } - - public event EventHandler ViewBarClosing; - - public event EventHandler ViewActivated; - - public void Add(IView view, ViewLocation location) - { - if (views.Contains(view)) - { - throw new InvalidOperationException(Resources.AvalonDockDockingManager_Add_View_was_already_added__activate_it_instead_of_add); - } - - var control = view as Control; - if (control != null) - { - control.TextChanged += ControlOnTextChanged; - } - - views.Add(view); - - if (dockingLocations.Contains(ViewLocation.Document)) - { - AddDocumentView(view); - } - else - { - AddToolView(view, location); - } - - if (ViewActivated != null) - { - ViewActivated(this, new ActiveViewChangeEventArgs { View = view }); - } - } - private void AddToolView(IView view, ViewLocation location) { var control = (Control) view; control.Dock = DockStyle.Fill; - var hostControl = new WindowsFormsHost { Child = control }; + var hostControl = new WindowsFormsHost + { + Child = control + }; hostControls.Add(hostControl); var anchorable = new LayoutAnchorable - { - Content = hostControl, - Title = view.Text, - ContentId = view.Text - }; + { + Content = hostControl, + Title = view.Text, + ContentId = view.Text + }; anchorable.Closing += AnchorableOnClosing; anchorable.Hiding += AnchorableOnHiding; anchorable.PropertyChanged += AnchorableOnPropertyChanged; - + AnchorSide anchorSide; // TODO: this logic is messy - find a better way @@ -245,8 +431,14 @@ if (anchorablePane == null) { - var paneGroup = new LayoutAnchorablePaneGroup { Orientation = System.Windows.Controls.Orientation.Vertical, DockWidth = new GridLength(220) }; - anchorablePane = new LayoutAnchorablePane { DockWidth = new GridLength(220.0, GridUnitType.Pixel) }; + var paneGroup = new LayoutAnchorablePaneGroup + { + Orientation = Orientation.Vertical, DockWidth = new GridLength(220) + }; + anchorablePane = new LayoutAnchorablePane + { + DockWidth = new GridLength(220.0, GridUnitType.Pixel) + }; paneGroup.Children.Add(anchorablePane); dockingManager.Layout.RootPanel.Children.Insert(0, paneGroup); @@ -265,8 +457,14 @@ if (anchorablePane == null) { - var paneGroup = new LayoutAnchorablePaneGroup { Orientation = System.Windows.Controls.Orientation.Vertical, DockWidth = new GridLength(220) }; - anchorablePane = new LayoutAnchorablePane { DockWidth = new GridLength(220.0, GridUnitType.Pixel) }; + var paneGroup = new LayoutAnchorablePaneGroup + { + Orientation = Orientation.Vertical, DockWidth = new GridLength(220) + }; + anchorablePane = new LayoutAnchorablePane + { + DockWidth = new GridLength(220.0, GridUnitType.Pixel) + }; paneGroup.Children.Add(anchorablePane); dockingManager.Layout.RootPanel.Children.Add(paneGroup); @@ -279,13 +477,20 @@ if (anchorablePane == null) { - anchorablePane = new LayoutAnchorablePane {DockWidth = new GridLength(220.0, GridUnitType.Pixel)}; + anchorablePane = new LayoutAnchorablePane + { + DockWidth = new GridLength(220.0, GridUnitType.Pixel) + }; var subLayoutPanels = dockingManager.Layout.RootPanel.Children.OfType().FirstOrDefault(); if (subLayoutPanels != null) + { subLayoutPanels.Children.Add(anchorablePane); + } else + { dockingManager.Layout.RootPanel.Children.Add(anchorablePane); + } } } else if (location.HasFlag(ViewLocation.Top)) @@ -298,9 +503,7 @@ throw new NotImplementedException(string.Format(Resources.AvalonDockDockingManager_AddToolView_View_location__0__is_not_implemented_yet, location)); } - if (anchorablePane == null) - { - } + if (anchorablePane == null) {} anchorablePane.Children.Add(anchorable); @@ -367,16 +570,19 @@ reusable.LockedChanged += ReusableLockedChanged; } - var hostControl = new WindowsFormsHost {Child = (Control) view}; + var hostControl = new WindowsFormsHost + { + Child = (Control) view + }; hostControls.Add(hostControl); var layoutDocument = new LayoutDocument - { - Title = view.Text, - Content = hostControl, - ContentId = view.Text, - IconSource = GetImage(view) - }; + { + Title = view.Text, + Content = hostControl, + ContentId = view.Text, + IconSource = GetImage(view) + }; var firstDocumentPane = dockingManager.Layout.Descendents().OfType().First(); firstDocumentPane.Children.Add(layoutDocument); @@ -386,7 +592,9 @@ private static BitmapImage BitmapImageFromBitmap(Image bitmap) { if (bitmap == null) + { return null; + } using (var memory = new MemoryStream()) { @@ -404,62 +612,15 @@ } } - public void Remove(IView view, bool removeTabFromDockingbar) - { - if (removeTabFromDockingbar) - { - var layoutContent = GetLayoutContent(view); - var index = views.IndexOf(view); - - CleanupHostControl(index); - - if (layoutContent != null) - { - layoutContent.Close(); - } - } - else - { - var index = views.IndexOf(view); - CleanupHostControl(index); - } - - var control = view as Control; - if (control != null) - { - control.TextChanged -= ControlOnTextChanged; - } - - var reusable = view as IReusableView; - if (reusable != null) - { - reusable.LockedChanged -= ReusableLockedChanged; - } - - views.Remove(view); - - if (views.Count != hostControls.Count) - throw new InvalidOperationException(); - - view.Dispose(); - - activeView = GetView(dockingManager.ActiveContent); - - if (ViewActivated != null) - { - ViewActivated(this, new ActiveViewChangeEventArgs { View = activeView }); - } - } - private void CleanupHostControl(int index) { var hostControl = hostControls[index]; if (hostControl != null) { object adapter = - typeof (WindowsFormsHost).GetProperty("HostContainerInternal", - BindingFlags.GetProperty | BindingFlags.NonPublic | - BindingFlags.Instance).GetValue(hostControl, null); + typeof(WindowsFormsHost).GetProperty("HostContainerInternal", + BindingFlags.GetProperty | BindingFlags.NonPublic | + BindingFlags.Instance).GetValue(hostControl, null); var disposable = adapter as IDisposable; if (disposable != null) { @@ -472,83 +633,36 @@ hostControls.RemoveAt(index); } - void ReusableLockedChanged(object sender, EventArgs e) + private void ReusableLockedChanged(object sender, EventArgs e) { var view = sender as IView; if (view == null) + { return; + } var document = GetLayoutContent(view) as LayoutDocument; if (document == null) + { return; + } document.IconSource = GetImage(view); } - + private ImageSource GetImage(IView view) { var reusable = view as IReusableView; var isLocked = reusable != null && reusable.Locked; return BitmapImageFromBitmap(isLocked ? LockImage : view.Image); } - public void SetToolTip(IView view, string tooltip) - { - var layoutDocument = GetLayoutContent(view); - if (layoutDocument != null) - { - layoutDocument.ToolTip = tooltip; - } - } - private LayoutContent GetLayoutContent(IView view) { var descendents = dockingManager.Layout.Descendents(); - return descendents.OfType().FirstOrDefault(d => d.Content is WindowsFormsHost && ((WindowsFormsHost)d.Content).Child == view); + return descendents.OfType().FirstOrDefault(d => d.Content is WindowsFormsHost && ((WindowsFormsHost) d.Content).Child == view); } - public void ActivateView(IView view) - { - if (view == null || !views.Contains(view)) - { - return; - } - - if (dockingLocations.Contains(ViewLocation.Document)) - { - var layoutDocument = dockingManager.Layout.Descendents().OfType().FirstOrDefault(a => ContainsView(a, view)); - - // fast fix - check why it is null! - if (layoutDocument == null) - { - return; - } - - if (!layoutDocument.IsActive) - { - dockingManager.Layout.ActiveContent = layoutDocument; - } - } - else - { - var layoutAnchorable = dockingManager.Layout.Descendents().OfType().FirstOrDefault(a => ContainsView(a, view)); - - // fast fix - check why it is null! - if (layoutAnchorable == null) - { - return; - } - - if (layoutAnchorable.IsHidden) - layoutAnchorable.Show(); - else if (layoutAnchorable.IsVisible) - layoutAnchorable.IsActive = true; - else - throw new NotImplementedException(); - // anchorable.AddToLayout(dockManager, AnchorableShowStrategy.Bottom | AnchorableShowStrategy.Most); - } - } - private static IView GetView(object content) { var host = content as WindowsFormsHost; @@ -562,61 +676,23 @@ private static bool ContainsView(LayoutContent content, IView view) { - return content.Content is WindowsFormsHost && ((WindowsFormsHost)content.Content).Child == view; + return content.Content is WindowsFormsHost && ((WindowsFormsHost) content.Content).Child == view; } - public event Action ViewSelectionMouseDown; - - /// - /// Called after layout is changed (e.g. loaded). - /// - public void OnLayoutChange() + private void ControlOnTextChanged(object sender, EventArgs eventArgs) { - var existingContents = dockingManager.Layout.Descendents().OfType().ToArray(); - var existingHostControls = existingContents.Select(c => c.Content as WindowsFormsHost); - - foreach (var view in views.ToArray()) + var view = sender as IView; + if (view == null) { - if (existingHostControls.Any(c => c != null && c.Child == view)) - { - continue; - } - - // view removed - var index = views.IndexOf(view); - var control = view as Control; - if (control != null) - { - control.TextChanged -= ControlOnTextChanged; - } - - view.Dispose(); - view.Data = null; - views.RemoveAt(index); - hostControls.RemoveAt(index); - - if (views.Count != hostControls.Count) - throw new InvalidOperationException(); + return; } - // subscribe to anchorables (this must be much simpler) - var anchorables = dockingManager.Layout.Descendents().OfType().ToArray(); - - foreach (var layoutAnchorable in anchorables) + var layoutContent = GetLayoutContent(view); + if (layoutContent == null) { - layoutAnchorable.PropertyChanged -= AnchorableOnPropertyChanged; - layoutAnchorable.PropertyChanged += AnchorableOnPropertyChanged; + return; } - } - private void ControlOnTextChanged(object sender, EventArgs eventArgs) - { - var view = sender as IView; - if (view == null) return; - - var layoutContent = GetLayoutContent(view); - if(layoutContent == null) return; - layoutContent.Title = view.Text; layoutContent.IconSource = GetImage(view); //update image aswell }