1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.Activities.Presentation.View
{
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Runtime;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
delegate void ExtensionWindowCloseEventHandler(object sender, RoutedEventArgs e);
delegate void ExtensionWindowClosingEventHandler(object sender, ExtensionWindowClosingRoutedEventArgs e);
//This class provides PopupWindow like expirience while editing data on designer surface. It
//behaves like ordinary popup, with additional functionality - allows resizing, moving, and
//easier styling.
[TemplatePart(Name = "PART_Content"),
TemplatePart(Name = "PART_ShapeBorder")]
class ExtensionWindow : ContentControl
{
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
"Data",
typeof(object),
typeof(ExtensionWindow),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnDataChanged)));
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
"Title",
typeof(string),
typeof(ExtensionWindow),
new UIPropertyMetadata(string.Empty));
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
"Icon",
typeof(DrawingBrush),
typeof(ExtensionWindow),
new UIPropertyMetadata(null));
public static readonly DependencyProperty ShowWindowHeaderProperty = DependencyProperty.Register(
"ShowWindowHeader",
typeof(bool),
typeof(ExtensionWindow),
new UIPropertyMetadata(true));
public static readonly DependencyProperty ShowResizeGripProperty = DependencyProperty.Register(
"ShowResizeGrip",
typeof(bool),
typeof(ExtensionWindow),
new UIPropertyMetadata(true));
public static readonly DependencyProperty MenuItemsProperty = DependencyProperty.Register(
"MenuItems",
typeof(ObservableCollection<MenuItem>),
typeof(ExtensionWindow));
public static readonly DependencyProperty IsResizableProperty = DependencyProperty.Register(
"IsResizable",
typeof(bool),
typeof(ExtensionWindow),
new UIPropertyMetadata(false));
public static readonly RoutedEvent ClosingEvent = EventManager.RegisterRoutedEvent("Closing",
RoutingStrategy.Bubble,
typeof(ExtensionWindowClosingEventHandler),
typeof(ExtensionWindow));
public static readonly RoutedEvent CloseEvent = EventManager.RegisterRoutedEvent("Close",
RoutingStrategy.Bubble,
typeof(ExtensionWindowCloseEventHandler),
typeof(ExtensionWindow));
public static readonly RoutedEvent VisibilityChangedEvent = EventManager.RegisterRoutedEvent("VisibilityChanged",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(ExtensionWindow));
static readonly double BorderOffset = 20.0;
ContentPresenter presenter;
ExtensionSurface surface;
Border border;
ResizeValues resizeOption = ResizeValues.NONE;
Point bottomRight;
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.InitializeReferenceTypeStaticFieldsInline,
Justification = "Overriding metadata for dependency properties in static constructor is the way suggested by WPF")]
static ExtensionWindow()
{
VisibilityProperty.AddOwner(typeof(ExtensionWindow), new PropertyMetadata(OnVisibilityChanged));
DefaultStyleKeyProperty.OverrideMetadata(
typeof(ExtensionWindow),
new FrameworkPropertyMetadata(typeof(ExtensionWindow)));
}
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "This is internal code with no derived class")]
public ExtensionWindow()
{
this.MenuItems = new ObservableCollection<MenuItem>();
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
this.MenuItems.CollectionChanged += new NotifyCollectionChangedEventHandler(OnMenuItemsChanged);
this.DataContext = this;
}
public event ExtensionWindowClosingEventHandler Closing
{
add
{
AddHandler(ClosingEvent, value);
}
remove
{
RemoveHandler(ClosingEvent, value);
}
}
public event ExtensionWindowCloseEventHandler Close
{
add
{
AddHandler(CloseEvent, value);
}
remove
{
RemoveHandler(CloseEvent, value);
}
}
public event RoutedEventHandler VisibilityChanged
{
add
{
AddHandler(VisibilityChangedEvent, value);
}
remove
{
RemoveHandler(VisibilityChangedEvent, value);
}
}
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
[Fx.Tag.KnownXamlExternal]
public DrawingBrush Icon
{
get { return (DrawingBrush)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public bool ShowWindowHeader
{
get { return (bool)GetValue(ShowWindowHeaderProperty); }
set { SetValue(ShowWindowHeaderProperty, value); }
}
public bool ShowResizeGrip
{
get { return (bool)GetValue(ShowResizeGripProperty); }
set { SetValue(ShowResizeGripProperty, value); }
}
[Fx.Tag.KnownXamlExternal]
public ObservableCollection<MenuItem> MenuItems
{
get { return (ObservableCollection<MenuItem>)GetValue(MenuItemsProperty); }
private set { SetValue(MenuItemsProperty, value); }
}
public ExtensionSurface Surface
{
get { return this.surface; }
}
public bool IsResizable
{
get { return (bool)GetValue(IsResizableProperty); }
set { SetValue(IsResizableProperty, value); }
}
protected ContentPresenter ContentPresenter
{
get { return this.presenter; }
}
public Point GetPlacementTargetOffset()
{
Point offset = new Point();
FrameworkElement target = ExtensionSurface.GetPlacementTarget(this);
if (null != target)
{
FrameworkElement commonRoot = target.FindCommonVisualAncestor(this) as FrameworkElement;
MatrixTransform transform = (MatrixTransform)target.TransformToAncestor(commonRoot);
Point targetPosition = transform.Transform(new Point());
Point windowPosition = ExtensionSurface.GetPosition(this);
offset.X = targetPosition.X - windowPosition.X;
offset.Y = targetPosition.Y - windowPosition.Y;
}
return offset;
}
static void OnDataChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
((ExtensionWindow)sender).OnDataChanged(args.OldValue, args.NewValue);
}
static void OnPositionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
((ExtensionWindow)sender).OnPositionChanged((Point)args.NewValue);
}
static void OnVisibilityChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
((ExtensionWindow)sender).OnVisibilityChanged((Visibility)e.OldValue, (Visibility)e.NewValue);
}
protected override void OnVisualParentChanged(DependencyObject oldParent)
{
base.OnVisualParentChanged(oldParent);
ExtensionWindow dummy;
if (!DesignerProperties.GetIsInDesignMode(this) && !TryGetParentExtensionWindow(this, out dummy, out this.surface))
{
Fx.Assert(string.Format(CultureInfo.InvariantCulture, "ExtensionWindow '{0}' cannot be used outside ExtensionSurface", this.Name));
}
}
protected virtual void OnDataChanged(object oldData, object newData)
{
}
protected virtual void OnVisibilityChanged(Visibility oldValue, Visibility newValue)
{
RaiseEvent(new RoutedEventArgs(VisibilityChangedEvent, this));
}
protected virtual void OnPositionChanged(Point position)
{
if (null != this.surface)
{
this.surface.SetWindowPosition(this, position);
}
}
public bool TryFindElement(string elementName, out object element)
{
//helper method - it looks for named visual elements in the template provided by the user
element = null;
if (null != this.presenter)
{
element = this.presenter.ContentTemplate.FindName(elementName, this.presenter);
}
return (null != element);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
//lookup for content presenter (should always be there)
this.presenter = this.Template.FindName("PART_Content", this) as ContentPresenter;
if (!DesignerProperties.GetIsInDesignMode(this) && null != this.presenter && null == this.presenter.Content)
{
//setup bindings - datacontext and content may possibly change, so i want them beeing set via databinding
Binding binding = new Binding();
binding.Source = this;
this.presenter.SetBinding(ContentPresenter.ContentProperty, binding);
binding = new Binding();
binding.Source = this;
this.presenter.SetBinding(ContentPresenter.DataContextProperty, binding);
this.presenter.ApplyTemplate();
}
this.border = this.Template.FindName("PART_ShapeBorder", this) as Border;
if (null != this.border)
{
this.border.MouseMove += OnBorderMouseMove;
this.border.MouseDown += OnBorderMouseDown;
this.border.MouseUp += OnBorderMouseUp;
this.border.MouseLeave += OnBorderMouseLeave;
}
}
static internal void RaiseWindowCloseEvent(ExtensionWindow sender)
{
ExtensionWindowClosingRoutedEventArgs args = new ExtensionWindowClosingRoutedEventArgs(ClosingEvent, sender);
sender.RaiseEvent(args);
if (!args.Cancel)
{
sender.RaiseEvent(new RoutedEventArgs(CloseEvent, sender));
}
}
void OnBorderMouseMove(object sender, MouseEventArgs e)
{
if (!this.border.IsMouseCaptured)
{
if (this.border.IsMouseDirectlyOver && this.IsResizable && ExtensionSurface.GetMode(this) == ExtensionSurface.PlacementMode.Absolute)
{
Point position = e.GetPosition(this.border);
if (position.X <= BorderOffset && position.Y <= BorderOffset)
{
this.resizeOption = ResizeValues.TopLeft;
Mouse.OverrideCursor = Cursors.SizeNWSE;
}
else if (position.X >= this.border.ActualWidth - BorderOffset && position.Y <= BorderOffset)
{
this.resizeOption = ResizeValues.TopRight;
Mouse.OverrideCursor = Cursors.SizeNESW;
}
else if (position.X <= BorderOffset && position.Y >= this.border.ActualHeight - BorderOffset)
{
this.resizeOption = ResizeValues.BottomLeft;
Mouse.OverrideCursor = Cursors.SizeNESW;
}
else if (position.X >= this.border.ActualWidth - BorderOffset && position.Y >= this.border.ActualHeight - BorderOffset)
{
this.resizeOption = ResizeValues.BottomRight;
Mouse.OverrideCursor = Cursors.SizeNWSE;
}
else if (position.Y <= (BorderOffset / 2.0))
{
this.resizeOption = ResizeValues.Top;
Mouse.OverrideCursor = Cursors.SizeNS;
}
else if (position.Y >= this.border.ActualHeight - (BorderOffset / 2.0))
{
this.resizeOption = ResizeValues.Bottom;
Mouse.OverrideCursor = Cursors.SizeNS;
}
else if (position.X <= (BorderOffset / 2.0))
{
this.resizeOption = ResizeValues.Left;
Mouse.OverrideCursor = Cursors.SizeWE;
}
else if (position.X >= this.border.ActualWidth - (BorderOffset / 2.0))
{
this.resizeOption = ResizeValues.Right;
Mouse.OverrideCursor = Cursors.SizeWE;
}
else
{
Mouse.OverrideCursor = null;
this.resizeOption = ResizeValues.NONE;
}
Point topLeft = ExtensionSurface.GetPosition(this);
this.bottomRight = new Point(topLeft.X + Width, topLeft.Y + Height);
}
else if (Mouse.OverrideCursor != null)
{
Mouse.OverrideCursor = null;
this.resizeOption = ResizeValues.NONE;
}
}
else if (e.LeftButton == MouseButtonState.Pressed)
{
this.HandleWindowResize();
}
}
void OnBorderMouseDown(object sender, MouseButtonEventArgs e)
{
if (this.resizeOption != ResizeValues.NONE && sender == this.border)
{
Mouse.Capture(this.border);
}
}
void OnBorderMouseUp(object sender, MouseButtonEventArgs e)
{
if (this.border.IsMouseCaptured)
{
Mouse.Capture(null);
}
}
void OnBorderMouseLeave(object sender, MouseEventArgs e)
{
this.resizeOption = ResizeValues.NONE;
Mouse.OverrideCursor = null;
}
void HandleWindowResize()
{
switch (this.resizeOption)
{
case ResizeValues.Top:
CalculateSize(false, true, false, true);
break;
case ResizeValues.TopLeft:
CalculateSize(true, true, true, true);
break;
case ResizeValues.Left:
CalculateSize(true, false, true, false);
break;
case ResizeValues.BottomLeft:
CalculateSize(true, false, true, true);
break;
case ResizeValues.TopRight:
CalculateSize(false, true, true, true);
break;
case ResizeValues.Bottom:
CalculateSize(false, false, false, true);
break;
case ResizeValues.Right:
CalculateSize(false, false, true, false);
break;
case ResizeValues.BottomRight:
CalculateSize(false, false, true, true);
break;
default:
Fx.Assert("not supported resize option " + this.resizeOption);
break;
};
}
void CalculateSize(bool changeX, bool changeY, bool changeWidth, bool changeHeight)
{
Point current = Mouse.GetPosition(this);
Point absolutePosition = Mouse.GetPosition(this.surface);
Point topLeft = ExtensionSurface.GetPosition(this);
double initialHeight = this.bottomRight.Y - topLeft.Y;
double initialWidth = this.bottomRight.X - topLeft.X;
Size size = new Size(initialWidth, initialHeight);
if (changeX)
{
if (bottomRight.X > absolutePosition.X)
{
if ((double.IsNaN(MinWidth) || double.IsInfinity(MinWidth) || bottomRight.X - absolutePosition.X >= MinWidth) &&
(double.IsNaN(MaxWidth) || double.IsInfinity(MaxWidth) || bottomRight.X - absolutePosition.X <= MaxWidth))
{
size.Width = this.bottomRight.X - absolutePosition.X;
topLeft.X = absolutePosition.X;
}
}
}
else
{
if (changeWidth)
{
size.Width = Math.Min(Math.Max(MinWidth, current.X), MaxWidth);
}
}
if (changeY)
{
if (bottomRight.Y > absolutePosition.Y)
{
if ((double.IsNaN(MinHeight) || double.IsInfinity(MinHeight) || bottomRight.Y - absolutePosition.Y >= MinHeight) &&
(double.IsNaN(MaxHeight) || double.IsInfinity(MaxHeight) || bottomRight.Y - absolutePosition.Y <= MaxHeight))
{
size.Height = this.bottomRight.Y - absolutePosition.Y;
topLeft.Y = absolutePosition.Y;
}
}
}
else
{
if (changeHeight)
{
size.Height = Math.Min(Math.Max(MinHeight, current.Y), MaxHeight);
}
}
if (changeX || changeY)
{
this.surface.SetWindowPosition(this, topLeft);
}
this.surface.SetSize(this, size);
}
protected override void OnKeyDown(KeyEventArgs e)
{
//if ESC - close me
if (e.Key == Key.Escape)
{
RaiseWindowCloseEvent(this);
}
base.OnKeyDown(e);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
this.SelectWindow();
}
public void SelectWindow()
{
this.surface.SelectWindow(this);
}
void OnMenuItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (null != e.NewItems)
{
foreach (MenuItem item in e.NewItems)
{
item.DataContext = this;
}
}
}
internal static bool TryGetParentExtensionWindow(FrameworkElement element, out ExtensionWindow window, out ExtensionSurface surface)
{
window = null;
surface = null;
if (null != element)
{
FrameworkElement current = element;
window = element.TemplatedParent as ExtensionWindow;
while (null == window && null != current)
{
window = current as ExtensionWindow;
current = (FrameworkElement)current.Parent;
}
if (null != window)
{
current = window;
surface = window.TemplatedParent as ExtensionSurface;
while (null == surface && null != current)
{
surface = current as ExtensionSurface;
current = (FrameworkElement)current.Parent;
}
}
}
return (null != window && null != surface);
}
enum ResizeValues
{
NONE,
TopLeft,
Left,
BottomLeft,
Bottom,
BottomRight,
Right,
TopRight,
Top,
};
}
}
|