aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GraphicObjects/GenericStack.cs
blob: 925f908a49cac3b0ab9926e15cca4e174ec2670c (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
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;

namespace Crow
{
    public class GenericStack : Group
    {
		#region CTOR
		public GenericStack()
			: base()
		{            
		}
		#endregion

		#region Private fields
        int _spacing;
        Orientation _orientation;
		#endregion

		#region Public Properties
        [XmlAttributeAttribute()][DefaultValue(2)]
        public int Spacing
        {
			get { return _spacing; }
            set { 
				if (_spacing == value)
					return;
				_spacing = value; 
				NotifyValueChanged ("Spacing", Spacing);
			}
        }
        [XmlAttributeAttribute()][DefaultValue(Orientation.Horizontal)]
        public virtual Orientation Orientation
        {
            get { return _orientation; }
            set { _orientation = value; }
        }
		#endregion

		#region GraphicObject Overrides
		public override bool ArrangeChildren { get { return true; } }
		public override void ChildrenLayoutingConstraints (ref LayoutingType layoutType)
		{
			//Prevent child repositionning in the direction of stacking
			if (Orientation == Orientation.Horizontal)
				layoutType &= (~LayoutingType.X);
			else
				layoutType &= (~LayoutingType.Y);			
		}
		protected override int measureRawSize (LayoutingType lt)
		{
			int tmp = 0;
			switch (lt) {
			case LayoutingType.Width:
				if (Orientation == Orientation.Horizontal) {
					if (Children.Count > 0) {
						foreach (GraphicObject c in Children) {
							if (!c.Visible)
								continue;
							if (c.RegisteredLayoutings.HasFlag (LayoutingType.Width))
								return -1;
							tmp += c.Slot.Width + Spacing;
						}
						tmp -= Spacing;
					}
					break;
				} 
				return base.measureRawSize (lt);				
			case LayoutingType.Height:
				if (Orientation == Orientation.Vertical) {
					if (Children.Count > 0) {
						foreach (GraphicObject c in Children) {
							if (!c.Visible)
								continue;
							if (c.RegisteredLayoutings.HasFlag (LayoutingType.Height))
								return -1;
							tmp += c.Slot.Height + Spacing;
						}
						tmp -= Spacing;
					}
					break;
				}
				return base.measureRawSize (lt);
			}

			return tmp + 2 * Margin;
		}
		public virtual void ComputeChildrenPositions()
		{
			int d = 0;
			if (Orientation == Orientation.Horizontal) {
				foreach (GraphicObject c in Children) {
					if (!c.Visible)
						continue;
					c.Slot.X = d;
					d += c.Slot.Width + Spacing;
					c.RegisterForLayouting (LayoutingType.Y);
				}
			} else {
				foreach (GraphicObject c in Children) {
					if (!c.Visible)
						continue;					
					c.Slot.Y = d;
					d += c.Slot.Height + Spacing;
					c.RegisterForLayouting (LayoutingType.X);
				}
			}
			bmp = null;
		}
			
		public override bool UpdateLayout (LayoutingType layoutType)
        {
			RegisteredLayoutings &= (~layoutType);

			if (layoutType == LayoutingType.ArrangeChildren) {
				//allow 1 child to have size to 0 if stack has fixed or streched size policy,
				//this child will occupy remaining space
				//if stack size policy is Fit, no child may have stretch enabled
				//in the direction of stacking.
				if (Orientation == Orientation.Horizontal) {
					GraphicObject stretchedGO = null;
					int tmpWidth = Slot.Width;
					int cptChildren = 0;
					for (int i = 0; i < Children.Count; i++) {
						if (!Children [i].Visible)
							continue;
						//requeue Positionning if child is not layouted
						if (Children [i].RegisteredLayoutings.HasFlag (LayoutingType.Width))
							return false;
						cptChildren++;
						if (Children [i].Width == 0) {
							if (!(stretchedGO == null && Width >= 0)) {
								//change size policy of other stretched children
								Children [i].Width = -1;
								return false;
							}
							stretchedGO = Children [i];
							if (i < Children.Count - 1)
								tmpWidth -= Spacing;
							continue;
						}
						tmpWidth -= Children [i].Slot.Width + Spacing;
					}
					if (stretchedGO != null && Width >= 0) {
						tmpWidth += (Spacing - 2 * Margin);
						if (tmpWidth < MinimumSize.Width)
							tmpWidth = MinimumSize.Width;
						else if (tmpWidth > MaximumSize.Width && MaximumSize.Width > 0)
							tmpWidth = MaximumSize.Width;
						if (stretchedGO.LastSlots.Width != tmpWidth) {
							stretchedGO.Slot.Width = tmpWidth;
							stretchedGO.bmp = null;
							#if DEBUG_LAYOUTING
							Debug.WriteLine ("\tAdjusting Width of " + stretchedGO.ToString());
							#endif
							stretchedGO.OnLayoutChanges (LayoutingType.Width);
							stretchedGO.LastSlots.Width = stretchedGO.Slot.Width;
						}
					}
				} else {
					GraphicObject stretchedGO = null;
					int tmpHeight = Slot.Height;
					int cptChildren = 0;
					for (int i = 0; i < Children.Count; i++) {
						if (!Children [i].Visible)
							continue;
						if (Children [i].RegisteredLayoutings.HasFlag (LayoutingType.Height))
							return false;
						cptChildren++;
						if (Children [i].Height == 0) {
							if (!(stretchedGO == null && Height >= 0)){
								Children [i].Height = -1;
								return false;
							}
							stretchedGO = Children [i];
							if (i < Children.Count - 1)
								tmpHeight -= Spacing;
							continue;
						}
						tmpHeight -= Children[i].Slot.Height + Spacing;
					}
					if (stretchedGO != null && Height >= 0) {
						tmpHeight += (Spacing - 2 * Margin);
						if (tmpHeight < MinimumSize.Height)
							tmpHeight = MinimumSize.Height;
						else if (tmpHeight > MaximumSize.Height && MaximumSize.Height > 0)
							tmpHeight = MaximumSize.Height;
						if (stretchedGO.LastSlots.Height != tmpHeight) {
							stretchedGO.Slot.Height = tmpHeight;
							stretchedGO.bmp = null;
							#if DEBUG_LAYOUTING
							Debug.WriteLine ("\tAdjusting Height of " + stretchedGO.ToString());
							#endif
							stretchedGO.OnLayoutChanges (LayoutingType.Height);
							stretchedGO.LastSlots.Height = stretchedGO.Slot.Height;
						}
					}
				}

				ComputeChildrenPositions ();

				//if no layouting remains in queue for item, registre for redraw
				if (RegisteredLayoutings == LayoutingType.None && bmp==null)
					this.AddToRedrawList ();

				return true;
			}

			return base.UpdateLayout(layoutType);
        }

		public override void OnChildLayoutChanges (object sender, LayoutingEventArgs arg)
		{
			base.OnChildLayoutChanges (sender, arg);

			switch (arg.LayoutType) {
			case LayoutingType.Width:
				if (Orientation == Orientation.Horizontal) {
					if (Width < 0)
						this.RegisterForLayouting (LayoutingType.Width);
					this.RegisterForLayouting (LayoutingType.ArrangeChildren);
				}
				break;
			case LayoutingType.Height:
				if (Orientation == Orientation.Vertical) {
					if (Height < 0)
						this.RegisterForLayouting (LayoutingType.Height);
					this.RegisterForLayouting (LayoutingType.ArrangeChildren);
				}
				break;
			}
		}
		#endregion

    
	}
}