aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/WpfExtendedToolKit/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Panels/SwitchPresenter.cs
blob: ef5fa2f3afbd30e37dca530cad432ccf150911db (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
/*************************************************************************************

   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.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Threading;
using Xceed.Wpf.Toolkit.Core.Utilities;

namespace Xceed.Wpf.Toolkit.Panels
{
  public class SwitchPresenter : FrameworkElement
  {
    #region Constructors

    public SwitchPresenter()
      : base()
    {
      this.AddVisualChild( _contentPresenter );

      this.Loaded += new RoutedEventHandler( this.SwitchPresenter_Loaded );
      this.Unloaded += new RoutedEventHandler( this.SwitchPresenter_Unloaded );
    }

    #endregion

    #region DelaySwitch Property

    // Using a DependencyProperty as the backing store for DelaySwitch.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DelaySwitchProperty =
      DependencyProperty.Register( "DelaySwitch", typeof( bool ), typeof( SwitchPresenter ), 
        new UIPropertyMetadata( false ) );

    public bool DelaySwitch
    {
      get
      {
        return ( bool )this.GetValue( SwitchPresenter.DelaySwitchProperty );
      }
      set
      {
        this.SetValue( SwitchPresenter.DelaySwitchProperty, value );
      }
    }

    #endregion

    #region DelayPriority Property

    // Using a DependencyProperty as the backing store for DelayPriority.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DelayPriorityProperty =
      DependencyProperty.Register( "DelayPriority", typeof( DispatcherPriority ), typeof( SwitchPresenter ),
        new UIPropertyMetadata( DispatcherPriority.Background ) );

    public DispatcherPriority DelayPriority
    {
      get
      {
        return ( DispatcherPriority )this.GetValue( SwitchPresenter.DelayPriorityProperty );
      }
      set
      {
        this.SetValue( SwitchPresenter.DelayPriorityProperty, value );
      }
    }

    #endregion

    #region SwitchParent Internal Property

    internal static readonly DependencyProperty SwitchParentProperty =
      DependencyProperty.Register( "SwitchParent", typeof( SwitchPanel ), typeof( SwitchPresenter ),
        new FrameworkPropertyMetadata( null, 
          new PropertyChangedCallback( SwitchPresenter.OnSwitchParentChanged ) ) );

    internal SwitchPanel SwitchParent
    {
      get
      {
        return ( SwitchPanel )this.GetValue( SwitchPresenter.SwitchParentProperty );
      }
      set
      {
        this.SetValue( SwitchPresenter.SwitchParentProperty, value );
      }
    }

    private static void OnSwitchParentChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
      ( ( SwitchPresenter )d ).OnSwitchParentChanged( e );
    }

    protected virtual void OnSwitchParentChanged( DependencyPropertyChangedEventArgs e )
    {
      if( e.OldValue != null )
      {
        ( e.OldValue as SwitchPanel ).UnregisterPresenter( this, _switchRoot );
        _switchRoot = null;
        BindingOperations.ClearAllBindings( _contentPresenter );
      }

      if( e.NewValue != null )
      {
        _contentPresenter.SetBinding( ContentPresenter.ContentProperty, new Binding() );
        _switchRoot = ( e.NewValue as SwitchPanel ).RegisterPresenter( this );
      }
    }

    private static void OnLoaded( object sender, RoutedEventArgs e )
    {
      SwitchPresenter sp = sender as SwitchPresenter;
      if( sp._switchRoot == null )
      {
        sp.SwitchParent = VisualTreeHelperEx.FindAncestorByType( sp, typeof( SwitchPanel ), false ) as SwitchPanel;
      }
    }

    private static void OnUnloaded( object sender, RoutedEventArgs e )
    {
      ( sender as SwitchPresenter ).SwitchParent = null;
    }

    #endregion

    #region VisualChildrenCount Protected Property

    protected override int VisualChildrenCount
    {
      get
      {
        return 1;
      }
    }

    #endregion

    internal void RegisterID( string id, FrameworkElement element )
    {
      if( element == null )
        return;

      _knownIDs[ id ] = element;
    }

    internal void SwapTheTemplate( DataTemplate template, bool beginAnimation )
    {
      if( this.DelaySwitch )
      {
        _currentTemplate = template;

        this.Dispatcher.BeginInvoke( new Action<DelaySwitchParams>( this.OnSwapTemplate ),
          this.DelayPriority,
          new DelaySwitchParams()
          {
            Template = template,
            BeginAnimation = beginAnimation
          } );
      }
      else
      {
        this.DoSwapTemplate( template, beginAnimation );
      }
    }

    protected override Size MeasureOverride( Size constraint )
    {
      // if first pass, resolve SwitchParent
      if( !_isMeasured && _switchRoot == null )
      {
        SwitchPresenter.OnLoaded( this, null );
        _isMeasured = true;
      }

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

    protected override Size ArrangeOverride( Size arrangeBounds )
    {
      _contentPresenter.Arrange( new Rect( arrangeBounds ) );
      return arrangeBounds;
    }

    protected override Visual GetVisualChild( int index )
    {
      if( index != 0 )
        throw new ArgumentOutOfRangeException( "index", index, "" );

      return _contentPresenter;
    }

    private void OnSwapTemplate( DelaySwitchParams data )
    {
      // If we are switching the templates fast the invokes will lag. So ignore old invokes.
      if( data.Template == _currentTemplate )
      {
        this.DoSwapTemplate( data.Template, data.BeginAnimation );
        _currentTemplate = null;
      }
    }

    private void DoSwapTemplate( DataTemplate template, bool beginAnimation )
    {
      // cache transforms for known ID'd elements in the current template
      Dictionary<string, Rect> knownLocations = null;
      if( beginAnimation && _knownIDs.Count > 0 )
      {
        knownLocations = new Dictionary<string, Rect>();
        foreach( KeyValuePair<string, FrameworkElement> entry in _knownIDs )
        {
          Size size = entry.Value.RenderSize;
          Matrix m = ( entry.Value.TransformToAncestor( SwitchParent ) as MatrixTransform ).Matrix;
          Point[] points = { new Point(), new Point( size.Width, size.Height ) };
          m.Transform( points );
          knownLocations[ entry.Key ] = new Rect( points[ 0 ], points[ 1 ] );
        }
      }

      // clear the known IDs because the new template will have all new IDs
      _knownIDs.Clear();

      // set and apply the new template
      _contentPresenter.ContentTemplate = template;
      if( template != null )
      {
        _contentPresenter.ApplyTemplate();
      }

      // determine locations of ID'd elements in new template
      // and begin animation to new location
      if( knownLocations != null && _knownIDs.Count > 0 )
      {
        Dictionary<string, Rect> newLocations = null;
        RoutedEventHandler onLoaded = null;
        onLoaded = delegate( object sender, RoutedEventArgs e )
        {
          FrameworkElement element = sender as FrameworkElement;
          element.Loaded -= onLoaded;
          string id = SwitchTemplate.GetID( element );
          if( knownLocations.ContainsKey( id ) )
          {
            // ensure that the new locations have been resolved
            if( newLocations == null )
            {
              newLocations = this.SwitchParent.ActiveLayout.GetNewLocationsBasedOnTargetPlacement( this, _switchRoot );
            }

            UIElement parent = VisualTreeHelper.GetParent( element ) as UIElement;
            if( parent != null )
            {
              Rect previousLocation = knownLocations[ id ];
              Point[] points = { previousLocation.TopLeft, previousLocation.BottomRight };
              Matrix m = ( SwitchParent.TransformToDescendant( parent ) as MatrixTransform ).Matrix;
              m.Transform( points );
              Rect oldLocation = new Rect( points[ 0 ], points[ 1 ] );
              Rect newLocation = newLocations[ id ];
              this.SwitchParent.ActiveLayout.BeginGrandchildAnimation( element, oldLocation, newLocation );
            }
          }
        };

        foreach( KeyValuePair<string, FrameworkElement> entry in _knownIDs )
        {
          entry.Value.Loaded += onLoaded;
        }
      }
    }

    private void SwitchPresenter_Unloaded( object sender, RoutedEventArgs e )
    {
      this.SwitchParent = null;
    }

    private void SwitchPresenter_Loaded( object sender, RoutedEventArgs e )
    {
      if( _switchRoot == null )
      {
        this.SwitchParent = VisualTreeHelperEx.FindAncestorByType( this, typeof( SwitchPanel ), false ) as SwitchPanel;
      }
    }

    #region Private Fields

    // track our topmost ancestor that is the direct child of the SwitchPanel
    internal UIElement _switchRoot = null;
    internal Dictionary<string, FrameworkElement> _knownIDs = new Dictionary<string, FrameworkElement>();

    private ContentPresenter _contentPresenter = new ContentPresenter();
    private bool _isMeasured = false;

    private DataTemplate _currentTemplate;

    #endregion

    #region DelaySwitchParams Nested Type

    private struct DelaySwitchParams
    {
      public DataTemplate Template;
      public bool BeginAnimation;
    }

    #endregion
  }
}