aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/WpfExtendedToolKit/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.AvalonDock/Controls/LayoutFloatingWindowControl.cs
blob: badc94bd9077c03598d47eadf0c864b0e5ef7b4f (plain)
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
582
583
584
585
586
587
/*************************************************************************************

   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.Linq;
using System.Windows;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Xceed.Wpf.AvalonDock.Layout;
using System.Windows.Documents;
using Xceed.Wpf.AvalonDock.Themes;

namespace Xceed.Wpf.AvalonDock.Controls
{
  public abstract class LayoutFloatingWindowControl : Window, ILayoutControl
  {
    #region Members

    private ResourceDictionary currentThemeResourceDictionary; // = null
    private bool _isInternalChange; //false
    private ILayoutElement _model;
    private bool _attachDrag = false;
    private HwndSource _hwndSrc;
    private HwndSourceHook _hwndSrcHook;
    private DragService _dragService = null;
    private bool _internalCloseFlag = false;

    #endregion

    #region Constructors

    static LayoutFloatingWindowControl()
    {
      LayoutFloatingWindowControl.ContentProperty.OverrideMetadata( typeof( LayoutFloatingWindowControl ), new FrameworkPropertyMetadata( null, null, new CoerceValueCallback( CoerceContentValue ) ) );
      AllowsTransparencyProperty.OverrideMetadata( typeof( LayoutFloatingWindowControl ), new FrameworkPropertyMetadata( false ) );
      ShowInTaskbarProperty.OverrideMetadata( typeof( LayoutFloatingWindowControl ), new FrameworkPropertyMetadata( false ) );
    }

    protected LayoutFloatingWindowControl( ILayoutElement model )
    {
      this.Loaded += new RoutedEventHandler( OnLoaded );
      this.Unloaded += new RoutedEventHandler( OnUnloaded );
      _model = model;
    }

    #endregion

    #region Properties

    #region Model

    public abstract ILayoutElement Model
    {
      get;
    }

    #endregion

    #region IsDragging

    /// <summary>
    /// IsDragging Read-Only Dependency Property
    /// </summary>
    private static readonly DependencyPropertyKey IsDraggingPropertyKey = DependencyProperty.RegisterReadOnly( "IsDragging", typeof( bool ), typeof( LayoutFloatingWindowControl ),
            new FrameworkPropertyMetadata( ( bool )false, new PropertyChangedCallback( OnIsDraggingChanged ) ) );

    public static readonly DependencyProperty IsDraggingProperty = IsDraggingPropertyKey.DependencyProperty;

    /// <summary>
    /// Gets the IsDragging property.  This dependency property 
    /// indicates that this floating window is being dragged.
    /// </summary>
    public bool IsDragging
    {
      get
      {
        return ( bool )GetValue( IsDraggingProperty );
      }
    }

    /// <summary>
    /// Provides a secure method for setting the IsDragging property.  
    /// This dependency property indicates that this floating window is being dragged.
    /// </summary>
    /// <param name="value">The new value for the property.</param>
    protected void SetIsDragging( bool value )
    {
      SetValue( IsDraggingPropertyKey, value );
    }

    /// <summary>
    /// Handles changes to the IsDragging property.
    /// </summary>
    private static void OnIsDraggingChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
      ( ( LayoutFloatingWindowControl )d ).OnIsDraggingChanged( e );
    }

    /// <summary>
    /// Provides derived classes an opportunity to handle changes to the IsDragging property.
    /// </summary>
    protected virtual void OnIsDraggingChanged( DependencyPropertyChangedEventArgs e )
    {
      if( ( bool )e.NewValue )
      {
        CaptureMouse();
      }
      else
      {
        ReleaseMouseCapture();
      }
    }

    #endregion

    #region CloseInitiatedByUser

    protected bool CloseInitiatedByUser
    {
      get
      {
        return !_internalCloseFlag;
      }
    }

    #endregion

    #region KeepContentVisibleOnClose

    internal bool KeepContentVisibleOnClose
    {
      get;
      set;
    }

    #endregion

    #region IsMaximized

    /// <summary>
    /// IsMaximized Dependency Property
    /// </summary>
    public static readonly DependencyProperty IsMaximizedProperty = DependencyProperty.Register( "IsMaximized", typeof( bool ), typeof( LayoutFloatingWindowControl ),
              new FrameworkPropertyMetadata( ( bool )false ) );

    /// <summary>
    /// Gets/sets the IsMaximized property.  This dependency property 
    /// indicates if the window is maximized.
    /// </summary>
    public bool IsMaximized
    {
      get
      {
        return ( bool )GetValue( IsMaximizedProperty );
      }
      private set
      {
        SetValue( IsMaximizedProperty, value );
        UpdatePositionAndSizeOfPanes();
      }
    }

    /// <summary>
    /// Provides a secure method for setting the IsMaximized property.  
    /// This dependency property indicates if the window is maximized.
    /// </summary>
    /// <param name="value">The new value for the property.</param>

    protected override void OnStateChanged( EventArgs e )
    {
      if( !_isInternalChange )
      {
        if( WindowState == WindowState.Maximized )
        {
          UpdateMaximizedState( true );
        }
        else
        {
          WindowState = IsMaximized ? WindowState.Maximized : WindowState.Normal;
        }
      }

      base.OnStateChanged( e );
    }

    #endregion

    #endregion

    #region Overrides

    protected override void OnClosed( EventArgs e )
    {
      if( Content != null )
      {
        var host = Content as FloatingWindowContentHost;
        host.Dispose();

        if( _hwndSrc != null )
        {
          _hwndSrc.RemoveHook( _hwndSrcHook );
          _hwndSrc.Dispose();
          _hwndSrc = null;
        }
      }

      base.OnClosed( e );
    }

    protected override void OnInitialized( EventArgs e )
    {
      CommandBindings.Add( new CommandBinding( Microsoft.Windows.Shell.SystemCommands.CloseWindowCommand,
          new ExecutedRoutedEventHandler( ( s, args ) => Microsoft.Windows.Shell.SystemCommands.CloseWindow( ( Window )args.Parameter ) ) ) );
      CommandBindings.Add( new CommandBinding( Microsoft.Windows.Shell.SystemCommands.MaximizeWindowCommand,
          new ExecutedRoutedEventHandler( ( s, args ) => Microsoft.Windows.Shell.SystemCommands.MaximizeWindow( ( Window )args.Parameter ) ) ) );
      CommandBindings.Add( new CommandBinding( Microsoft.Windows.Shell.SystemCommands.MinimizeWindowCommand,
          new ExecutedRoutedEventHandler( ( s, args ) => Microsoft.Windows.Shell.SystemCommands.MinimizeWindow( ( Window )args.Parameter ) ) ) );
      CommandBindings.Add( new CommandBinding( Microsoft.Windows.Shell.SystemCommands.RestoreWindowCommand,
          new ExecutedRoutedEventHandler( ( s, args ) => Microsoft.Windows.Shell.SystemCommands.RestoreWindow( ( Window )args.Parameter ) ) ) );
      //Debug.Assert(this.Owner != null);
      base.OnInitialized( e );
    }


    #endregion

    #region Internal Methods

    internal virtual void UpdateThemeResources( Theme oldTheme = null )
    {
      if( oldTheme != null )
      {
        if( oldTheme is DictionaryTheme )
        {
          if( currentThemeResourceDictionary != null )
          {
            Resources.MergedDictionaries.Remove( currentThemeResourceDictionary );
            currentThemeResourceDictionary = null;
          }
        }
        else
        {
          var resourceDictionaryToRemove =
              Resources.MergedDictionaries.FirstOrDefault( r => r.Source == oldTheme.GetResourceUri() );
          if( resourceDictionaryToRemove != null )
            Resources.MergedDictionaries.Remove(
                resourceDictionaryToRemove );
        }
      }

      var manager = _model.Root.Manager;
      if( manager.Theme != null )
      {
        if( manager.Theme is DictionaryTheme )
        {
          currentThemeResourceDictionary = ( ( DictionaryTheme )manager.Theme ).ThemeResourceDictionary;
          Resources.MergedDictionaries.Add( currentThemeResourceDictionary );
        }
        else
        {
          Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = manager.Theme.GetResourceUri() } );
        }
      }
    }

    internal void AttachDrag( bool onActivated = true )
    {
      if( onActivated )
      {
        _attachDrag = true;
        this.Activated += new EventHandler( OnActivated );
      }
      else
      {
        IntPtr windowHandle = new WindowInteropHelper( this ).Handle;
        IntPtr lParam = new IntPtr( ( ( int )Left & ( int )0xFFFF ) | ( ( ( int )Top ) << 16 ) );
        Win32Helper.SendMessage( windowHandle, Win32Helper.WM_NCLBUTTONDOWN, new IntPtr( Win32Helper.HT_CAPTION ), lParam );
      }
    }

    protected virtual IntPtr FilterMessage( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
    {
      handled = false;

      switch( msg )
      {
        case Win32Helper.WM_ACTIVATE:
          if( ( ( int )wParam & 0xFFFF ) == Win32Helper.WA_INACTIVE )
          {
            if( lParam == this.GetParentWindowHandle() )
            {
              Win32Helper.SetActiveWindow( _hwndSrc.Handle );
              handled = true;
            }
          }
          break;
        case Win32Helper.WM_EXITSIZEMOVE:
          UpdatePositionAndSizeOfPanes();

          if( _dragService != null )
          {
            bool dropFlag;
            var mousePosition = this.TransformToDeviceDPI( Win32Helper.GetMousePosition() );
            _dragService.Drop( mousePosition, out dropFlag );
            _dragService = null;
            SetIsDragging( false );

            if( dropFlag )
              InternalClose();
          }

          break;
        case Win32Helper.WM_MOVING:
          {
            UpdateDragPosition();
            if( this.IsMaximized )
            {
              this.UpdateMaximizedState( false );
            }
          }
          break;
        case Win32Helper.WM_LBUTTONUP: //set as handled right button click on title area (after showing context menu)
          if( _dragService != null && Mouse.LeftButton == MouseButtonState.Released )
          {
            _dragService.Abort();
            _dragService = null;
            SetIsDragging( false );
          }
          break;
        case Win32Helper.WM_SYSCOMMAND:
          int command = ( int )wParam & 0xFFF0;
          if( command == Win32Helper.SC_MAXIMIZE || command == Win32Helper.SC_RESTORE )
          {
            UpdateMaximizedState( command == Win32Helper.SC_MAXIMIZE );
          }
          break;
      }



      return IntPtr.Zero;
    }

    internal void InternalClose()
    {
      _internalCloseFlag = true;
      Close();
    }

    #endregion

    #region Private Methods

    private static object CoerceContentValue( DependencyObject sender, object content )
    {
      return new FloatingWindowContentHost( sender as LayoutFloatingWindowControl ) { Content = content as UIElement };
    }

    private void OnLoaded( object sender, RoutedEventArgs e )
    {
      this.Loaded -= new RoutedEventHandler( OnLoaded );

      this.SetParentToMainWindowOf( Model.Root.Manager );

      _hwndSrc = HwndSource.FromDependencyObject( this ) as HwndSource;
      _hwndSrcHook = new HwndSourceHook( FilterMessage );
      _hwndSrc.AddHook( _hwndSrcHook );

      // Restore maximize state
      var maximized = Model.Descendents().OfType<ILayoutElementForFloatingWindow>().Any( l => l.IsMaximized );
      UpdateMaximizedState( maximized );
    }

    private void OnUnloaded( object sender, RoutedEventArgs e )
    {
      this.Unloaded -= new RoutedEventHandler( OnUnloaded );

      if( _hwndSrc != null )
      {
        _hwndSrc.RemoveHook( _hwndSrcHook );
        InternalClose();
      }
    }

    private void OnActivated( object sender, EventArgs e )
    {
      this.Activated -= new EventHandler( OnActivated );

      if( _attachDrag && Mouse.LeftButton == MouseButtonState.Pressed )
      {
        IntPtr windowHandle = new WindowInteropHelper( this ).Handle;
        var mousePosition = this.PointToScreenDPI( Mouse.GetPosition( this ) );
        var clientArea = Win32Helper.GetClientRect( windowHandle );
        var windowArea = Win32Helper.GetWindowRect( windowHandle );

        Left = mousePosition.X - windowArea.Width / 2.0;
        Top = mousePosition.Y - ( windowArea.Height - clientArea.Height ) / 2.0;
        _attachDrag = false;

        IntPtr lParam = new IntPtr( ( ( int )mousePosition.X & ( int )0xFFFF ) | ( ( ( int )mousePosition.Y ) << 16 ) );
        Win32Helper.SendMessage( windowHandle, Win32Helper.WM_NCLBUTTONDOWN, new IntPtr( Win32Helper.HT_CAPTION ), lParam );
      }
    }

    private void UpdatePositionAndSizeOfPanes()
    {
      foreach( var posElement in Model.Descendents().OfType<ILayoutElementForFloatingWindow>() )
      {
        posElement.FloatingLeft = Left;
        posElement.FloatingTop = Top;
        posElement.FloatingWidth = Width;
        posElement.FloatingHeight = Height;
      }
    }

    private void UpdateMaximizedState( bool isMaximized )
    {
      foreach( var posElement in Model.Descendents().OfType<ILayoutElementForFloatingWindow>() )
      {
        posElement.IsMaximized = isMaximized;
      }
      IsMaximized = isMaximized;
      _isInternalChange = true;
      WindowState = isMaximized ? WindowState.Maximized : WindowState.Normal;
      _isInternalChange = false;
    }

    private void UpdateDragPosition()
    {
      if( _dragService == null )
      {
        _dragService = new DragService( this );
        SetIsDragging( true );
      }

      var mousePosition = this.TransformToDeviceDPI( Win32Helper.GetMousePosition() );
      _dragService.UpdateMouseLocation( mousePosition );
    }

    #endregion

    #region Internal Classes

    protected internal class FloatingWindowContentHost : HwndHost
    {
      #region Members

      private LayoutFloatingWindowControl _owner;
      private HwndSource _wpfContentHost = null;
      private Border _rootPresenter = null;
      private DockingManager _manager = null;

      #endregion

      #region Constructors

      public FloatingWindowContentHost( LayoutFloatingWindowControl owner )
      {
        _owner = owner;
        var manager = _owner.Model.Root.Manager;
      }

      #endregion

      #region Properties

      #region RootVisual

      public Visual RootVisual
      {
        get
        {
          return _rootPresenter;
        }
      }

      #endregion

      #region Content

      /// <summary>
      /// Content Dependency Property
      /// </summary>
      public static readonly DependencyProperty ContentProperty = DependencyProperty.Register( "Content", typeof( UIElement ), typeof( FloatingWindowContentHost ),
              new FrameworkPropertyMetadata( ( UIElement )null, new PropertyChangedCallback( OnContentChanged ) ) );

      /// <summary>
      /// Gets or sets the Content property.  This dependency property 
      /// indicates ....
      /// </summary>
      public UIElement Content
      {
        get
        {
          return ( UIElement )GetValue( ContentProperty );
        }
        set
        {
          SetValue( ContentProperty, value );
        }
      }

      /// <summary>
      /// Handles changes to the Content property.
      /// </summary>
      private static void OnContentChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
      {
        ( ( FloatingWindowContentHost )d ).OnContentChanged( e );
      }

      /// <summary>
      /// Provides derived classes an opportunity to handle changes to the Content property.
      /// </summary>
      protected virtual void OnContentChanged( DependencyPropertyChangedEventArgs e )
      {
        if( _rootPresenter != null )
          _rootPresenter.Child = Content;
      }

      #endregion

      #endregion

      #region Overrides

      protected override System.Runtime.InteropServices.HandleRef BuildWindowCore( System.Runtime.InteropServices.HandleRef hwndParent )
      {
        _wpfContentHost = new HwndSource( new HwndSourceParameters()
        {
          ParentWindow = hwndParent.Handle,
          WindowStyle = Win32Helper.WS_CHILD | Win32Helper.WS_VISIBLE | Win32Helper.WS_CLIPSIBLINGS | Win32Helper.WS_CLIPCHILDREN,
          Width = 1,
          Height = 1
        } );

        _rootPresenter = new Border() { Child = new AdornerDecorator() { Child = Content }, Focusable = true };
        _rootPresenter.SetBinding( Border.BackgroundProperty, new Binding( "Background" ) { Source = _owner } );
        _wpfContentHost.RootVisual = _rootPresenter;
        _wpfContentHost.SizeToContent = SizeToContent.Manual;
        _manager = _owner.Model.Root.Manager;
        _manager.InternalAddLogicalChild( _rootPresenter );

        return new HandleRef( this, _wpfContentHost.Handle );
      }

      protected override void DestroyWindowCore( HandleRef hwnd )
      {
        _manager.InternalRemoveLogicalChild( _rootPresenter );
        if( _wpfContentHost != null )
        {
          _wpfContentHost.Dispose();
          _wpfContentHost = null;
        }
      }

      protected override Size MeasureOverride( Size constraint )
      {
        if( Content == null )
          return base.MeasureOverride( constraint );

        Content.Measure( constraint );
        return Content.DesiredSize;
      }

      #endregion
    }

    #endregion
  }
}