From 6ecd265791fe58c6d466a1afdacda3f6c71a78b4 Mon Sep 17 00:00:00 2001 From: Kumar Devvrat Date: Thu, 20 May 2010 17:52:30 +0000 Subject: [PATCH] - Removed selection of RowDefinitions/ColumnDefinitions from GridRailAdorner - Display Splitter and GridUnitSelector on MouseEnter rather than if a particular Row/column is clicked and selected. - Fix bug : Invert Row lengths that appears in left GridRailAdorner - Fix bug : Update code which caused Row/Column lengths to be displayed at wrong positions after creation of new RowDefinition/ColumnDefinition. - Fix bug : Grid now remains selected while being adorned by GridRailAdorner. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/wpfdesigner@5837 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Controls/GridAdorner.cs | 286 +++++++++--------- .../Project/Controls/GridUnitSelector.xaml.cs | 134 ++++---- .../Project/Extensions/GridAdornerProvider.cs | 10 +- 3 files changed, 219 insertions(+), 211 deletions(-) diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/GridAdorner.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/GridAdorner.cs index 9fb765e6c3..200139cd76 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/GridAdorner.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/GridAdorner.cs @@ -13,7 +13,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; - +using ICSharpCode.WpfDesign.XamlDom; using ICSharpCode.WpfDesign.Adorners; namespace ICSharpCode.WpfDesign.Designer.Controls @@ -28,8 +28,8 @@ namespace ICSharpCode.WpfDesign.Designer.Controls bgBrush = new SolidColorBrush(Color.FromArgb(0x30, 0x20, 0x20, 0xff)); bgBrush.Freeze(); - selBrush = new SolidColorBrush(Color.FromArgb(0xC0, 0xff, 0xb7, 0x4f)); - selBrush.Freeze(); + //selBrush = new SolidColorBrush(Color.FromArgb(0xC0, 0xff, 0xb7, 0x4f)); + //selBrush.Freeze(); } readonly DesignItem gridItem; @@ -45,6 +45,8 @@ namespace ICSharpCode.WpfDesign.Designer.Controls public const double RailSize = 10; public const double RailDistance = 6; public const double SplitterWidth = 10; + + bool displayUnitSelector; // Indicates whether Grid UnitSeletor should be displayed. public GridRailAdorner(DesignItem gridItem, AdornerPanel adornerPanel, Orientation orientation) { @@ -55,9 +57,8 @@ namespace ICSharpCode.WpfDesign.Designer.Controls this.grid = (Grid)gridItem.Component; this.adornerPanel = adornerPanel; this.orientation = orientation; - + this.displayUnitSelector=false; this.unitSelector = new GridUnitSelector(this); - adornerPanel.Children.Add(unitSelector); if (orientation == Orientation.Horizontal) { @@ -70,24 +71,10 @@ namespace ICSharpCode.WpfDesign.Designer.Controls unitSelector.Orientation = orientation; previewAdorner.IsPreview = true; previewAdorner.IsHitTestVisible = false; + unitSelector.Visibility = Visibility.Hidden; + } - - public void SelectionChanged() - { - if (orientation == Orientation.Vertical) { - if (gridItem.Properties["RowDefinitions"].CollectionElements.Any(e => gridItem.Services.Selection.IsComponentSelected(e))) - unitSelector.Visibility = Visibility.Visible; - else - unitSelector.Visibility = Visibility.Hidden; - } else { - if (gridItem.Properties["ColumnDefinitions"].CollectionElements.Any(e => gridItem.Services.Selection.IsComponentSelected(e))) - unitSelector.Visibility = Visibility.Visible; - else - unitSelector.Visibility = Visibility.Hidden; - } - InvalidateVisual(); - } - + protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); @@ -100,14 +87,10 @@ namespace ICSharpCode.WpfDesign.Designer.Controls foreach (var colItem in colCollection.CollectionElements) { ColumnDefinition column = colItem.Component as ColumnDefinition; if (column.ActualWidth < 0) continue; - Rect selRect = new Rect(column.Offset, 0, column.ActualWidth, RailSize); GridLength len = (GridLength)column.GetValue(ColumnDefinition.WidthProperty); FormattedText text = new FormattedText(GridLengthToText(len), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Sergio UI"), 10, Brushes.Black); text.TextAlignment = TextAlignment.Center; - if (gridItem.Services.Selection.IsComponentSelected(colItem)) { - drawingContext.DrawRectangle(selBrush, null, selRect); - } drawingContext.DrawText(text, new Point(column.Offset + column.ActualWidth / 2, 0)); } } else { @@ -118,19 +101,12 @@ namespace ICSharpCode.WpfDesign.Designer.Controls foreach (var rowItem in rowCollection.CollectionElements) { RowDefinition row = rowItem.Component as RowDefinition; if (row.ActualHeight < 0) continue; - Rect selRect = new Rect(0, row.Offset, RailSize, row.ActualHeight); - GridLength len = (GridLength)row.GetValue(RowDefinition.HeightProperty); FormattedText text = new FormattedText(GridLengthToText(len), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Sergio UI"), 10, Brushes.Black); text.TextAlignment = TextAlignment.Center; - if (gridItem.Services.Selection.IsComponentSelected(rowItem)) { - drawingContext.DrawRectangle(selBrush, null, selRect); - } - drawingContext.PushTransform(new RotateTransform(-270)); - drawingContext.PushTransform(new TranslateTransform(0, -10)); - drawingContext.DrawText(text, new Point(row.Offset + row.ActualHeight / 2, 0)); - drawingContext.Pop(); + drawingContext.PushTransform(new RotateTransform(-90)); + drawingContext.DrawText(text, new Point((row.Offset + row.ActualHeight / 2)*-1, 0)); drawingContext.Pop(); } } @@ -140,91 +116,143 @@ namespace ICSharpCode.WpfDesign.Designer.Controls protected override void OnMouseEnter(MouseEventArgs e) { base.OnMouseEnter(e); - - if (orientation == Orientation.Vertical) { - double insertionPosition = e.GetPosition(this).Y; - RowDefinition current = grid.RowDefinitions - .FirstOrDefault(r => insertionPosition >= r.Offset && - insertionPosition <= (r.Offset + r.ActualHeight)); - - if (current == null || !gridItem.Services.Selection.IsComponentSelected(gridItem.Services.Component.GetDesignItem(current))) - return; - } else { - double insertionPosition = e.GetPosition(this).X; - ColumnDefinition current = grid.ColumnDefinitions - .FirstOrDefault(r => insertionPosition >= r.Offset && - insertionPosition <= (r.Offset + r.ActualWidth)); - - if (current == null || !gridItem.Services.Selection.IsComponentSelected(gridItem.Services.Component.GetDesignItem(current))) - return; - } - - adornerPanel.Children.Add(previewAdorner); + this.Cursor = Cursors.Cross; + RelativePlacement rpUnitSelector = new RelativePlacement(); + if (orientation == Orientation.Vertical) + { + double insertionPosition = e.GetPosition(this).Y; + RowDefinition current = grid.RowDefinitions + .FirstOrDefault(r => insertionPosition >= r.Offset && + insertionPosition <= (r.Offset + r.ActualHeight)); + if (current != null) + { + DesignItem component = this.gridItem.Services.Component.GetDesignItem(current); + rpUnitSelector.XOffset = -(RailSize + RailDistance) * 2.75; + rpUnitSelector.WidthOffset = RailSize + RailDistance; + rpUnitSelector.WidthRelativeToContentWidth = 1; + rpUnitSelector.HeightOffset = 55; + rpUnitSelector.YOffset = current.Offset + current.ActualHeight / 2 - 25; + unitSelector.SelectedItem = component; + unitSelector.Unit = ((GridLength)component.Properties[RowDefinition.HeightProperty].ValueOnInstance).GridUnitType; + displayUnitSelector = true; + } + else + { + displayUnitSelector = false; + } + } + else + { + double insertionPosition = e.GetPosition(this).X; + ColumnDefinition current = grid.ColumnDefinitions + .FirstOrDefault(r => insertionPosition >= r.Offset && + insertionPosition <= (r.Offset + r.ActualWidth)); + if (current != null) + { + DesignItem component = this.gridItem.Services.Component.GetDesignItem(current); + Debug.Assert(component != null); + rpUnitSelector.YOffset = -(RailSize + RailDistance) * 2.20; + rpUnitSelector.HeightOffset = RailSize + RailDistance; + rpUnitSelector.HeightRelativeToContentHeight = 1; + rpUnitSelector.WidthOffset = 75; + rpUnitSelector.XOffset = current.Offset + current.ActualWidth / 2 - 35; + unitSelector.SelectedItem = component; + unitSelector.Unit = ((GridLength)component.Properties[ColumnDefinition.WidthProperty].ValueOnInstance).GridUnitType; + displayUnitSelector = true; + } + else + { + displayUnitSelector = false; + } + } + if(displayUnitSelector) + unitSelector.Visibility = Visibility.Visible; + if(!adornerPanel.Children.Contains(previewAdorner)) + adornerPanel.Children.Add(previewAdorner); + } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); - RelativePlacement rp = new RelativePlacement(); - RelativePlacement rpUnitSelector = new RelativePlacement(); - if (orientation == Orientation.Vertical) { - double insertionPosition = e.GetPosition(this).Y; - RowDefinition current = grid.RowDefinitions - .FirstOrDefault(r => insertionPosition >= r.Offset && - insertionPosition <= (r.Offset + r.ActualHeight)); - - DesignItem component; - - if (current == null || !gridItem.Services.Selection.IsComponentSelected(component = gridItem.Services.Component.GetDesignItem(current))) - return; - - rp.XOffset = -(RailSize + RailDistance); - rp.WidthOffset = RailSize + RailDistance; - rp.WidthRelativeToContentWidth = 1; - rp.HeightOffset = SplitterWidth; - rp.YOffset = e.GetPosition(this).Y - SplitterWidth / 2; - - rpUnitSelector.XOffset = -(RailSize + RailDistance) * 2.75; - rpUnitSelector.WidthOffset = RailSize + RailDistance; - rpUnitSelector.WidthRelativeToContentWidth = 1; - rpUnitSelector.HeightOffset = 55; - rpUnitSelector.YOffset = current.Offset + current.ActualHeight / 2 - 25; - unitSelector.SelectedItem = component; - unitSelector.Unit = ((GridLength)component.Properties[RowDefinition.HeightProperty].ValueOnInstance).GridUnitType; - } else { - double insertionPosition = e.GetPosition(this).X; - ColumnDefinition current = grid.ColumnDefinitions - .FirstOrDefault(r => insertionPosition >= r.Offset && - insertionPosition <= (r.Offset + r.ActualWidth)); - - DesignItem component; - - if (current == null || !gridItem.Services.Selection.IsComponentSelected(component = gridItem.Services.Component.GetDesignItem(current))) - return; - - rp.YOffset = -(RailSize + RailDistance); - rp.HeightOffset = RailSize + RailDistance; - rp.HeightRelativeToContentHeight = 1; - rp.WidthOffset = SplitterWidth; - rp.XOffset = e.GetPosition(this).X - SplitterWidth / 2; - - rpUnitSelector.YOffset = -(RailSize + RailDistance) * 2.75; - rpUnitSelector.HeightOffset = RailSize + RailDistance; - rpUnitSelector.HeightRelativeToContentHeight = 1; - rpUnitSelector.WidthOffset = 75; - rpUnitSelector.XOffset = current.Offset + current.ActualWidth / 2 - 35; - unitSelector.SelectedItem = component; - unitSelector.Unit = ((GridLength)component.Properties[ColumnDefinition.WidthProperty].ValueOnInstance).GridUnitType; - } - - AdornerPanel.SetPlacement(previewAdorner, rp); - AdornerPanel.SetPlacement(unitSelector, rpUnitSelector); + RelativePlacement rp = new RelativePlacement(); + RelativePlacement rpUnitSelector = new RelativePlacement(); + if (orientation == Orientation.Vertical) + { + double insertionPosition = e.GetPosition(this).Y; + RowDefinition current = grid.RowDefinitions + .FirstOrDefault(r => insertionPosition >= r.Offset && + insertionPosition <= (r.Offset + r.ActualHeight)); + + rp.XOffset = -(RailSize + RailDistance); + rp.WidthOffset = RailSize + RailDistance; + rp.WidthRelativeToContentWidth = 1; + rp.HeightOffset = SplitterWidth; + rp.YOffset = e.GetPosition(this).Y - SplitterWidth / 2; + if (current != null) + { + DesignItem component = this.gridItem.Services.Component.GetDesignItem(current); + rpUnitSelector.XOffset = -(RailSize + RailDistance) * 2.75; + rpUnitSelector.WidthOffset = RailSize + RailDistance; + rpUnitSelector.WidthRelativeToContentWidth = 1; + rpUnitSelector.HeightOffset = 55; + rpUnitSelector.YOffset = current.Offset + current.ActualHeight / 2 - 25; + unitSelector.SelectedItem = component; + unitSelector.Unit = ((GridLength)component.Properties[RowDefinition.HeightProperty].ValueOnInstance).GridUnitType; + displayUnitSelector = true; + } + else + { + displayUnitSelector = false; + } + } + else + { + double insertionPosition = e.GetPosition(this).X; + ColumnDefinition current = grid.ColumnDefinitions + .FirstOrDefault(r => insertionPosition >= r.Offset && + insertionPosition <= (r.Offset + r.ActualWidth)); + + rp.YOffset = -(RailSize + RailDistance); + rp.HeightOffset = RailSize + RailDistance; + rp.HeightRelativeToContentHeight = 1; + rp.WidthOffset = SplitterWidth; + rp.XOffset = e.GetPosition(this).X - SplitterWidth / 2; + + if (current != null) + { + DesignItem component = this.gridItem.Services.Component.GetDesignItem(current); + Debug.Assert(component != null); + rpUnitSelector.YOffset = -(RailSize + RailDistance) * 2.20; + rpUnitSelector.HeightOffset = RailSize + RailDistance; + rpUnitSelector.HeightRelativeToContentHeight = 1; + rpUnitSelector.WidthOffset = 75; + rpUnitSelector.XOffset = current.Offset + current.ActualWidth / 2 - 35; + unitSelector.SelectedItem = component; + unitSelector.Unit = ((GridLength)component.Properties[ColumnDefinition.WidthProperty].ValueOnInstance).GridUnitType; + displayUnitSelector = true; + } + else + { + displayUnitSelector = false; + } + } + AdornerPanel.SetPlacement(previewAdorner, rp); + if (displayUnitSelector) + AdornerPanel.SetPlacement(unitSelector, rpUnitSelector); } protected override void OnMouseLeave(MouseEventArgs e) { base.OnMouseLeave(e); - adornerPanel.Children.Remove(previewAdorner); + Mouse.UpdateCursor(); + if (!unitSelector.IsMouseOver) + { + unitSelector.Visibility = Visibility.Hidden; + displayUnitSelector = false; + } + if(adornerPanel.Children.Contains(previewAdorner)) + adornerPanel.Children.Remove(previewAdorner); } protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) @@ -257,19 +285,7 @@ namespace ICSharpCode.WpfDesign.Designer.Controls if (currentRow == null) currentRow = gridItem.Services.Component.GetDesignItem(grid.RowDefinitions.Last()); - unitSelector.SelectedItem = currentRow; - - if (!gridItem.Services.Selection.IsComponentSelected(currentRow)) { - gridItem.Services.Selection.SetSelectedComponents(new DesignItem[] { currentRow }, SelectionTypes.Auto); - changeGroup.Commit(); - if (!adornerPanel.Children.Contains(previewAdorner)) - adornerPanel.Children.Add(previewAdorner); - - OnMouseMove(e); - InvalidateVisual(); - return; - } - + unitSelector.SelectedItem = currentRow; for (int i = 0; i < grid.RowDefinitions.Count; i++) { RowDefinition row = grid.RowDefinitions[i]; if (row.Offset > insertionPosition) continue; @@ -285,9 +301,9 @@ namespace ICSharpCode.WpfDesign.Designer.Controls newRowDefinition.Properties[RowDefinition.HeightProperty].SetValue(newLength2); FixIndicesAfterSplit(i, Grid.RowProperty, Grid.RowSpanProperty); + grid.UpdateLayout(); changeGroup.Commit(); - gridItem.Services.Selection.SetSelectedComponents(new DesignItem[] { newRowDefinition }, SelectionTypes.Auto); - break; + break; } } } else { @@ -315,17 +331,6 @@ namespace ICSharpCode.WpfDesign.Designer.Controls currentColumn = gridItem.Services.Component.GetDesignItem(grid.ColumnDefinitions.Last()); unitSelector.SelectedItem = currentColumn; - - if (!gridItem.Services.Selection.IsComponentSelected(currentColumn)) { - gridItem.Services.Selection.SetSelectedComponents(new DesignItem[] { currentColumn }, SelectionTypes.Auto); - changeGroup.Commit(); - if (!adornerPanel.Children.Contains(previewAdorner)) - adornerPanel.Children.Add(previewAdorner); - OnMouseMove(e); - InvalidateVisual(); - return; - } - for (int i = 0; i < grid.ColumnDefinitions.Count; i++) { ColumnDefinition column = grid.ColumnDefinitions[i]; if (column.Offset > insertionPosition) continue; @@ -341,11 +346,12 @@ namespace ICSharpCode.WpfDesign.Designer.Controls newColumnDefinition.Properties[ColumnDefinition.WidthProperty].SetValue(newLength2); FixIndicesAfterSplit(i, Grid.ColumnProperty, Grid.ColumnSpanProperty); changeGroup.Commit(); - gridItem.Services.Selection.SetSelectedComponents(new DesignItem[] { newColumnDefinition }, SelectionTypes.Auto); - break; + grid.UpdateLayout(); + break; } } } + InvalidateVisual(); } void FixIndicesAfterSplit(int splitIndex, DependencyProperty idxProperty, DependencyProperty spanProperty) @@ -373,8 +379,7 @@ namespace ICSharpCode.WpfDesign.Designer.Controls newLength2 = new GridLength(oldLength.Value - newLength1.Value, oldLength.GridUnitType); } #endregion - - string GridLengthToText(GridLength len) + string GridLengthToText(GridLength len) { switch (len.GridUnitType) { case GridUnitType.Auto: @@ -454,7 +459,6 @@ namespace ICSharpCode.WpfDesign.Designer.Controls e.Handled = true; if (CaptureMouse()) { Focus(); - gridItem.Services.Selection.SetSelectedComponents(new DesignItem[] { secondRow }, SelectionTypes.Auto); mouseStartPos = GetCoordinate(e.GetPosition(grid)); mouseIsDown = true; } diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/GridUnitSelector.xaml.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/GridUnitSelector.xaml.cs index b22219d1e5..48ff18dbd8 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/GridUnitSelector.xaml.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/GridUnitSelector.xaml.cs @@ -20,65 +20,77 @@ namespace ICSharpCode.WpfDesign.Designer.Controls /// /// Interaction logic for GridUnitSelector.xaml /// - public partial class GridUnitSelector : UserControl - { - GridRailAdorner rail; - - public GridUnitSelector(GridRailAdorner rail) - { - InitializeComponent(); - - this.rail = rail; - } - - void FixedChecked(object sender, RoutedEventArgs e) - { - this.rail.SetGridLengthUnit(Unit); - } - - void StarChecked(object sender, RoutedEventArgs e) - { - this.rail.SetGridLengthUnit(Unit); - } - - void AutoChecked(object sender, RoutedEventArgs e) - { - this.rail.SetGridLengthUnit(Unit); - } - - public static readonly DependencyProperty OrientationProperty = - DependencyProperty.Register("Orientation", typeof(Orientation), typeof(GridUnitSelector), - new FrameworkPropertyMetadata()); - - public Orientation Orientation { - get { return (Orientation)GetValue(OrientationProperty); } - set { SetValue(OrientationProperty, value); } - } - - public DesignItem SelectedItem { get; set; } - - public GridUnitType Unit { - get { - if (auto.IsChecked == true) - return GridUnitType.Auto; - if (star.IsChecked == true) - return GridUnitType.Star; - - return GridUnitType.Pixel; - } - set { - switch (value) { - case GridUnitType.Auto: - auto.IsChecked = true; - break; - case GridUnitType.Star: - star.IsChecked = true; - break; - default: - @fixed.IsChecked = true; - break; - } - } - } - } + public partial class GridUnitSelector : UserControl + { + GridRailAdorner rail; + + public GridUnitSelector(GridRailAdorner rail) + { + InitializeComponent(); + + this.rail = rail; + } + + void FixedChecked(object sender, RoutedEventArgs e) + { + this.rail.SetGridLengthUnit(Unit); + } + + void StarChecked(object sender, RoutedEventArgs e) + { + this.rail.SetGridLengthUnit(Unit); + } + + void AutoChecked(object sender, RoutedEventArgs e) + { + this.rail.SetGridLengthUnit(Unit); + } + + public static readonly DependencyProperty OrientationProperty = + DependencyProperty.Register("Orientation", typeof(Orientation), typeof(GridUnitSelector), + new FrameworkPropertyMetadata()); + + public Orientation Orientation + { + get { return (Orientation)GetValue(OrientationProperty); } + set { SetValue(OrientationProperty, value); } + } + + public DesignItem SelectedItem { get; set; } + + public GridUnitType Unit + { + get + { + if (auto.IsChecked == true) + return GridUnitType.Auto; + if (star.IsChecked == true) + return GridUnitType.Star; + + return GridUnitType.Pixel; + } + set + { + switch (value) + { + case GridUnitType.Auto: + auto.IsChecked = true; + break; + case GridUnitType.Star: + star.IsChecked = true; + break; + default: + @fixed.IsChecked = true; + break; + } + } + + } + protected override void OnMouseLeave(MouseEventArgs e) + { + base.OnMouseLeave(e); + this.Visibility = Visibility.Hidden; + } + } + } \ No newline at end of file diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/GridAdornerProvider.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/GridAdornerProvider.cs index ff048f853d..7e898ace19 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/GridAdornerProvider.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Extensions/GridAdornerProvider.cs @@ -73,21 +73,13 @@ namespace ICSharpCode.WpfDesign.Designer.Extensions CreateSplitter(); this.ExtendedItem.PropertyChanged += OnPropertyChanged; - this.ExtendedItem.Services.Selection.SelectionChanged += GridAdornerProviderSelectionChanged; - + base.OnInitialized(); } - void GridAdornerProviderSelectionChanged(object sender, DesignItemCollectionEventArgs e) - { - leftBar.SelectionChanged(); - topBar.SelectionChanged(); - } - protected override void OnRemove() { this.ExtendedItem.PropertyChanged -= OnPropertyChanged; - this.ExtendedItem.Services.Selection.SelectionChanged -= GridAdornerProviderSelectionChanged; base.OnRemove(); }