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
|
/*************************************************************************************
Extended WPF Toolkit
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
For more features, controls, and fast professional support,
pick up the Plus Edition at http://xceed.com/wpf_toolkit
Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
***********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Xceed.Wpf.Toolkit.Core;
namespace Xceed.Wpf.Toolkit.Primitives
{
public class WindowContainer : Canvas
{
#region Constructors
static WindowContainer()
{
// The default background must be transparent in order to be able to trap
// all mouse events when a modal window is displayed.
var defaultModalBackgroundBrush = new SolidColorBrush( Colors.Transparent );
defaultModalBackgroundBrush.Freeze();
ModalBackgroundBrushProperty = DependencyProperty.Register( "ModalBackgroundBrush", typeof( Brush ), typeof( WindowContainer ), new UIPropertyMetadata( defaultModalBackgroundBrush, OnModalBackgroundBrushChanged ) );
}
public WindowContainer()
{
this.SizeChanged += new SizeChangedEventHandler( this.WindowContainer_SizeChanged );
this.LayoutUpdated += new EventHandler( this.WindowContainer_LayoutUpdated );
this.Loaded += new RoutedEventHandler( WindowContainer_Loaded );
this.ClipToBounds = true;
}
void WindowContainer_Loaded( object sender, RoutedEventArgs e )
{
foreach( WindowControl window in this.Children )
{
window.SetIsActiveInternal( false );
}
this.SetNextActiveWindow( null );
}
#endregion //Constructors
#region Members
private Brush _defaultBackgroundBrush;
private bool _isModalBackgroundApplied;
#endregion
#region Properties
#region ModalBackgroundBrush
/// <summary>
/// Identifies the ModalBackgroundBrush dependency property.
/// </summary>
// Initialized in the static constructor.
public static readonly DependencyProperty ModalBackgroundBrushProperty;
/// <summary>
/// When using a modal window in the WindowContainer, a ModalBackgroundBrush can be set
/// for the WindowContainer.
/// </summary>
public Brush ModalBackgroundBrush
{
get
{
return ( Brush )GetValue( ModalBackgroundBrushProperty );
}
set
{
SetValue( ModalBackgroundBrushProperty, value );
}
}
private static void OnModalBackgroundBrushChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
WindowContainer windowContainer = ( WindowContainer )d;
if( windowContainer != null )
windowContainer.OnModalBackgroundBrushChanged( ( Brush )e.OldValue, ( Brush )e.NewValue );
}
protected virtual void OnModalBackgroundBrushChanged( Brush oldValue, Brush newValue )
{
this.SetModalBackground();
}
#endregion //ModalBackgroundBrush
#endregion
#region Base Class Override
/// <summary>
/// Measure the size of the WindowContainer based on its children.
/// </summary>
protected override Size MeasureOverride( Size constraint )
{
Size size = base.MeasureOverride( constraint );
if( this.Children.Count > 0 )
{
double width = double.IsNaN( this.Width )
? this.Children.OfType<WindowControl>().Max( ( w ) => w.Left + w.DesiredSize.Width )
: this.Width;
double height = double.IsNaN( this.Height )
? this.Children.OfType<WindowControl>().Max( ( w ) => w.Top + w.DesiredSize.Height )
: this.Height;
return new Size( Math.Min( width, constraint.Width), Math.Min( height, constraint.Height) );
}
return size;
}
/// <summary>
/// Register and unregister to children events.
/// </summary>
protected override void OnVisualChildrenChanged( DependencyObject visualAdded, DependencyObject visualRemoved )
{
base.OnVisualChildrenChanged( visualAdded, visualRemoved );
if( visualAdded != null && !( visualAdded is WindowControl ) )
throw new InvalidOperationException( "WindowContainer can only contain WindowControl types." );
if( visualRemoved != null )
{
WindowControl removedChild = ( WindowControl )visualRemoved;
removedChild.LeftChanged -= new EventHandler<EventArgs>( this.Child_LeftChanged );
removedChild.TopChanged -= new EventHandler<EventArgs>( this.Child_TopChanged );
removedChild.PreviewMouseLeftButtonDown -= new MouseButtonEventHandler( this.Child_PreviewMouseLeftButtonDown );
removedChild.IsVisibleChanged -= new DependencyPropertyChangedEventHandler( this.Child_IsVisibleChanged );
removedChild.IsKeyboardFocusWithinChanged -= new DependencyPropertyChangedEventHandler( this.Child_IsKeyboardFocusWithinChanged );
if( removedChild is ChildWindow )
{
( ( ChildWindow )removedChild ).IsModalChanged -= new EventHandler<EventArgs>( this.Child_IsModalChanged );
}
}
if( visualAdded != null )
{
WindowControl addedChild = ( WindowControl )visualAdded;
addedChild.LeftChanged += new EventHandler<EventArgs>( this.Child_LeftChanged );
addedChild.TopChanged += new EventHandler<EventArgs>( this.Child_TopChanged );
addedChild.PreviewMouseLeftButtonDown += new MouseButtonEventHandler( this.Child_PreviewMouseLeftButtonDown );
addedChild.IsVisibleChanged += new DependencyPropertyChangedEventHandler( this.Child_IsVisibleChanged );
addedChild.IsKeyboardFocusWithinChanged += new DependencyPropertyChangedEventHandler( this.Child_IsKeyboardFocusWithinChanged );
if( addedChild is ChildWindow )
{
( ( ChildWindow )addedChild ).IsModalChanged += new EventHandler<EventArgs>( this.Child_IsModalChanged );
}
}
}
#endregion
#region Event Handler
private void Child_LeftChanged( object sender, EventArgs e )
{
WindowControl windowControl = ( WindowControl )sender;
if( windowControl != null )
{
windowControl.Left = this.GetRestrictedLeft( windowControl );
}
Canvas.SetLeft( windowControl, windowControl.Left );
}
private void Child_TopChanged( object sender, EventArgs e )
{
WindowControl windowControl = ( WindowControl )sender;
if( windowControl != null )
{
windowControl.Top = this.GetRestrictedTop( windowControl );
}
Canvas.SetTop( windowControl, windowControl.Top );
}
private void Child_PreviewMouseLeftButtonDown( object sender, RoutedEventArgs e )
{
WindowControl windowControl = ( WindowControl )sender;
WindowControl modalWindow = this.GetModalWindow();
if( modalWindow == null )
{
this.SetNextActiveWindow( windowControl );
}
}
private void Child_IsModalChanged( object sender, EventArgs e )
{
this.SetModalBackground();
}
private void Child_IsVisibleChanged( object sender, DependencyPropertyChangedEventArgs e )
{
WindowControl windowControl = ( WindowControl )sender;
//Do not give access to data behind the WindowContainer as long as any child of WindowContainer is visible.
WindowControl firstVisibleChild = this.Children.OfType<WindowControl>().FirstOrDefault( ( x ) => x.Visibility == Visibility.Visible );
this.IsHitTestVisible = ( firstVisibleChild != null );
if( ( bool )e.NewValue )
{
this.SetChildPos( windowControl );
this.SetNextActiveWindow( windowControl );
}
else
{
this.SetNextActiveWindow( null );
}
WindowControl modalWindow = this.GetModalWindow();
foreach( WindowControl window in this.Children )
{
window.IsBlockMouseInputsPanelActive = ( modalWindow != null ) && !object.Equals( modalWindow, window );
}
this.SetModalBackground();
}
private void Child_IsKeyboardFocusWithinChanged( object sender, DependencyPropertyChangedEventArgs e )
{
WindowControl windowControl = ( WindowControl )sender;
if( ( bool )e.NewValue )
{
this.SetNextActiveWindow( windowControl );
}
}
private void WindowContainer_LayoutUpdated( object sender, EventArgs e )
{
foreach( WindowControl windowControl in this.Children )
{
//we only want to set the start position if this is the first time the control has bee initialized
if( !windowControl.IsStartupPositionInitialized && ( windowControl.ActualWidth != 0 ) && ( windowControl.ActualHeight != 0 ) )
{
this.SetChildPos( windowControl );
windowControl.IsStartupPositionInitialized = true;
}
}
}
private void WindowContainer_SizeChanged( object sender, SizeChangedEventArgs e )
{
foreach( WindowControl windowControl in this.Children )
{
//reposition our windows
windowControl.Left = this.GetRestrictedLeft( windowControl );
windowControl.Top = this.GetRestrictedTop( windowControl );
}
}
private void ExpandWindowControl( WindowControl windowControl )
{
if( windowControl != null )
{
windowControl.Left = 0;
windowControl.Top = 0;
windowControl.Width = Math.Min( this.ActualWidth, windowControl.MaxWidth );
windowControl.Height = Math.Min( this.ActualHeight, windowControl.MaxHeight );
}
}
#endregion
#region Private Methods
private void SetChildPos( WindowControl windowControl )
{
// A MessageBox with no X and Y will be centered.
// A ChildWindow with WindowStartupLocation == Center will be centered.
if( ( ( windowControl is MessageBox ) && ( windowControl.Left == 0 ) && ( windowControl.Top == 0 ) )
|| ( ( windowControl is ChildWindow ) && ( ( ( ChildWindow )windowControl ).WindowStartupLocation == WindowStartupLocation.Center ) ) )
{
this.CenterChild( windowControl );
}
else
{
Canvas.SetLeft( windowControl, windowControl.Left );
Canvas.SetTop( windowControl, windowControl.Top );
}
}
private void CenterChild( WindowControl windowControl )
{
windowControl.UpdateLayout();
if( ( windowControl.ActualWidth != 0 ) && ( windowControl.ActualHeight != 0 ) )
{
windowControl.Left = ( this.ActualWidth - windowControl.ActualWidth ) / 2.0;
windowControl.Left += (windowControl.Margin.Left - windowControl.Margin.Right);
windowControl.Top = ( this.ActualHeight - windowControl.ActualHeight ) / 2.0;
windowControl.Top += ( windowControl.Margin.Top - windowControl.Margin.Bottom );
}
}
private void SetNextActiveWindow( WindowControl windowControl )
{
if( !this.IsLoaded )
return;
if( this.IsModalWindow( windowControl ) )
{
this.BringToFront( windowControl );
}
else
{
WindowControl modalWindow = this.GetModalWindow();
// Modal window is always in front
if( modalWindow != null )
{
this.BringToFront( modalWindow );
}
else if( windowControl != null )
{
this.BringToFront( windowControl );
}
else
{
this.BringToFront( this.Children.OfType<WindowControl>()
.OrderByDescending( ( x ) => Canvas.GetZIndex( x ) )
.FirstOrDefault( ( x ) => x.Visibility == Visibility.Visible ) );
}
}
}
private void BringToFront( WindowControl windowControl )
{
if( windowControl != null )
{
int maxZIndez = this.Children.OfType<WindowControl>().Max( ( x ) => Canvas.GetZIndex( x ) );
Canvas.SetZIndex( windowControl, maxZIndez + 1 );
this.SetActiveWindow( windowControl );
}
}
private void SetActiveWindow( WindowControl windowControl )
{
if( windowControl.IsActive )
return;
foreach( WindowControl window in this.Children )
{
window.SetIsActiveInternal( false );
}
windowControl.SetIsActiveInternal( true );
}
private bool IsModalWindow( WindowControl windowControl )
{
return ( ( ( windowControl is MessageBox ) && (windowControl.Visibility == Visibility.Visible) )
|| ( ( windowControl is ChildWindow ) && ( ( ChildWindow )windowControl ).IsModal && ( ( ChildWindow )windowControl).WindowState == WindowState.Open ) );
}
private WindowControl GetModalWindow()
{
return this.Children.OfType<WindowControl>()
.OrderByDescending( ( x ) => Canvas.GetZIndex( x ) )
.FirstOrDefault( ( x ) => IsModalWindow( x ) && (x.Visibility == Visibility.Visible) );
}
private double GetRestrictedLeft( WindowControl windowControl )
{
if( windowControl.Left < 0 )
return 0;
if( ( ( windowControl.Left + windowControl.ActualWidth ) > this.ActualWidth ) && ( this.ActualWidth != 0 ) )
{
double x = this.ActualWidth - windowControl.ActualWidth;
return x < 0 ? 0 : x;
}
return windowControl.Left;
}
private double GetRestrictedTop( WindowControl windowControl )
{
if( windowControl.Top < 0 )
return 0;
if( ( ( windowControl.Top + windowControl.ActualHeight ) > this.ActualHeight ) && ( this.ActualHeight != 0 ) )
{
double y = this.ActualHeight - windowControl.ActualHeight;
return y < 0 ? 0 : y;
}
return windowControl.Top;
}
private void SetModalBackground()
{
// We have a modal window and a ModalBackgroundBrush set.
if( ( this.GetModalWindow() != null ) && ( this.ModalBackgroundBrush != null ) )
{
if( !_isModalBackgroundApplied )
{
_defaultBackgroundBrush = this.Background;
_isModalBackgroundApplied = true;
}
this.Background = this.ModalBackgroundBrush;
}
else
{
if( _isModalBackgroundApplied )
{
this.Background = _defaultBackgroundBrush;
_defaultBackgroundBrush = null;
_isModalBackgroundApplied = false;
}
}
}
#endregion
}
}
|