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

   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.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Xceed.Wpf.Toolkit.Core.Utilities;

namespace Xceed.Wpf.Toolkit.Panels
{
  public class WrapPanel : AnimationPanel
  {
    #region Orientation Property

    public static readonly DependencyProperty OrientationProperty =
      StackPanel.OrientationProperty.AddOwner( typeof( WrapPanel ),
        new FrameworkPropertyMetadata( Orientation.Horizontal, 
          new PropertyChangedCallback( WrapPanel.OnOrientationChanged ) ) );

    public Orientation Orientation
    {
      get
      {
        return _orientation;
      }
      set
      {
        base.SetValue( WrapPanel.OrientationProperty, value );
      }
    }

    private static void OnOrientationChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
      WrapPanel panel = ( WrapPanel )d;
      panel._orientation = ( Orientation )e.NewValue;
      panel.InvalidateMeasure();
    }

    private Orientation _orientation;

    #endregion

    #region ItemWidth Property

    public static readonly DependencyProperty ItemWidthProperty =
      DependencyProperty.Register( "ItemWidth", typeof( double ), typeof( WrapPanel ),
        new FrameworkPropertyMetadata( double.NaN,
          new PropertyChangedCallback( WrapPanel.OnInvalidateMeasure ) ), new ValidateValueCallback( WrapPanel.IsWidthHeightValid ) );

    [TypeConverter( typeof( LengthConverter ) )]
    public double ItemWidth
    {
      get
      {
        return ( double )base.GetValue( WrapPanel.ItemWidthProperty );
      }
      set
      {
        base.SetValue( WrapPanel.ItemWidthProperty, value );
      }
    }

    #endregion

    #region ItemHeight Property

    public static readonly DependencyProperty ItemHeightProperty =
      DependencyProperty.Register( "ItemHeight", typeof( double ), typeof( WrapPanel ),
        new FrameworkPropertyMetadata( double.NaN,
          new PropertyChangedCallback( WrapPanel.OnInvalidateMeasure ) ), new ValidateValueCallback( WrapPanel.IsWidthHeightValid ) );

    [TypeConverter( typeof( LengthConverter ) )]
    public double ItemHeight
    {
      get
      {
        return ( double )base.GetValue( WrapPanel.ItemHeightProperty );
      }
      set
      {
        base.SetValue( WrapPanel.ItemHeightProperty, value );
      }
    }

    #endregion

    #region IsChildOrderReversed Property

    public static readonly DependencyProperty IsStackReversedProperty =
      DependencyProperty.Register( "IsChildOrderReversed", typeof( bool ), typeof( WrapPanel ),
        new FrameworkPropertyMetadata( false, 
          new PropertyChangedCallback( WrapPanel.OnInvalidateMeasure ) ) );

    public bool IsChildOrderReversed
    {
      get
      {
        return ( bool )this.GetValue( WrapPanel.IsStackReversedProperty );
      }
      set
      {
        this.SetValue( WrapPanel.IsStackReversedProperty, value );
      }
    }

    #endregion

    protected override Size MeasureChildrenOverride( UIElementCollection children, Size constraint )
    {
      double desiredExtent = 0;
      double desiredStack = 0;

      bool isHorizontal = ( this.Orientation == Orientation.Horizontal );
      double constraintExtent = ( isHorizontal ? constraint.Width : constraint.Height );

      double itemWidth = ItemWidth;
      double itemHeight = ItemHeight;
      double itemExtent = ( isHorizontal ? itemWidth : itemHeight );

      bool hasExplicitItemWidth = !double.IsNaN( itemWidth );
      bool hasExplicitItemHeight = !double.IsNaN( itemHeight );
      bool useItemExtent = ( isHorizontal ? hasExplicitItemWidth : hasExplicitItemHeight );

      double lineExtent = 0;
      double lineStack = 0;

      Size childConstraint = new Size( ( hasExplicitItemWidth ? itemWidth : constraint.Width ),
          ( hasExplicitItemHeight ? itemHeight : constraint.Height ) );

      bool isReversed = this.IsChildOrderReversed;
      int from = isReversed ? children.Count - 1 : 0;
      int to = isReversed ? 0 : children.Count - 1;
      int step = isReversed ? -1 : 1;

      for( int i = from, pass = 0; pass < children.Count; i += step, pass++ )
      {
        UIElement child = children[ i ] as UIElement;

        child.Measure( childConstraint );

        double childExtent = isHorizontal
            ? ( hasExplicitItemWidth ? itemWidth : child.DesiredSize.Width )
            : ( hasExplicitItemHeight ? itemHeight : child.DesiredSize.Height );
        double childStack = isHorizontal
            ? ( hasExplicitItemHeight ? itemHeight : child.DesiredSize.Height )
            : ( hasExplicitItemWidth ? itemWidth : child.DesiredSize.Width );

        if( lineExtent + childExtent > constraintExtent )
        {
          desiredExtent = Math.Max( lineExtent, desiredExtent );
          desiredStack += lineStack;
          lineExtent = childExtent;
          lineStack = childStack;

          if( childExtent > constraintExtent )
          {
            desiredExtent = Math.Max( childExtent, desiredExtent );
            desiredStack += childStack;
            lineExtent = 0;
            lineStack = 0;
          }
        }
        else
        {
          lineExtent += childExtent;
          lineStack = Math.Max( childStack, lineStack );
        }
      }

      desiredExtent = Math.Max( lineExtent, desiredExtent );
      desiredStack += lineStack;

      return isHorizontal
        ? new Size( desiredExtent, desiredStack )
        : new Size( desiredStack, desiredExtent );
    }

    protected override Size ArrangeChildrenOverride( UIElementCollection children, Size finalSize )
    {
      bool isHorizontal = ( this.Orientation == Orientation.Horizontal );
      double finalExtent = ( isHorizontal ? finalSize.Width : finalSize.Height );

      double itemWidth = this.ItemWidth;
      double itemHeight = this.ItemHeight;
      double itemExtent = ( isHorizontal ? itemWidth : itemHeight );

      bool hasExplicitItemWidth = !double.IsNaN( itemWidth );
      bool hasExplicitItemHeight = !double.IsNaN( itemHeight );
      bool useItemExtent = ( isHorizontal ? hasExplicitItemWidth : hasExplicitItemHeight );

      double lineExtent = 0;
      double lineStack = 0;
      double lineStackSum = 0;

      int from = this.IsChildOrderReversed ? children.Count - 1 : 0;
      int to = this.IsChildOrderReversed ? 0 : children.Count - 1;
      int step = this.IsChildOrderReversed ? -1 : 1;

      Collection<UIElement> childrenInLine = new Collection<UIElement>();

      for( int i = from, pass = 0; pass < children.Count; i += step, pass++ )
      {
        UIElement child = children[ i ] as UIElement;

        double childExtent = isHorizontal
            ? ( hasExplicitItemWidth ? itemWidth : child.DesiredSize.Width )
            : ( hasExplicitItemHeight ? itemHeight : child.DesiredSize.Height );
        double childStack = isHorizontal
            ? ( hasExplicitItemHeight ? itemHeight : child.DesiredSize.Height )
            : ( hasExplicitItemWidth ? itemWidth : child.DesiredSize.Width );

        if( lineExtent + childExtent > finalExtent )
        {
          this.ArrangeLineOfChildren( childrenInLine, isHorizontal, lineStack, lineStackSum, itemExtent, useItemExtent );

          lineStackSum += lineStack;
          lineExtent = childExtent;

          if( childExtent > finalExtent )
          {
            childrenInLine.Add( child );
            this.ArrangeLineOfChildren( childrenInLine, isHorizontal, childStack, lineStackSum, itemExtent, useItemExtent );
            lineStackSum += childStack;
            lineExtent = 0;
          }
          childrenInLine.Add( child );
        }
        else
        {
          childrenInLine.Add( child );
          lineExtent += childExtent;
          lineStack = Math.Max( childStack, lineStack );
        }
      }

      if( childrenInLine.Count > 0 )
      {
        this.ArrangeLineOfChildren( childrenInLine, isHorizontal, lineStack, lineStackSum, itemExtent, useItemExtent );
      }

      return finalSize;
    }

    private void ArrangeLineOfChildren( Collection<UIElement> children, bool isHorizontal, double lineStack, double lineStackSum, double itemExtent, bool useItemExtent )
    {
      double extent = 0;
      foreach( UIElement child in children )
      {
        double childExtent = ( isHorizontal ? child.DesiredSize.Width : child.DesiredSize.Height );
        double elementExtent = ( useItemExtent ? itemExtent : childExtent );
        this.ArrangeChild( child, isHorizontal ? new Rect( extent, lineStackSum, elementExtent, lineStack )
          : new Rect( lineStackSum, extent, lineStack, elementExtent ) );
        extent += elementExtent;
      }
      children.Clear();
    }

    private static void OnInvalidateMeasure( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
      ( ( AnimationPanel )d ).InvalidateMeasure();
    }

    private static bool IsWidthHeightValid( object value )
    {
      double num = ( double )value;
      return ( DoubleHelper.IsNaN( num ) || ( ( num >= 0d ) && !double.IsPositiveInfinity( num ) ) );
    }
  }
}