aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GraphicObjects/GenericStack.cs
blob: d23383628fdad449040d164e1db953b26e8e81d6 (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
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
    {
		internal class Page
		{
			public byte[] bmp;
			public int Size;
		}

		#region CTOR
		public GenericStack()
			: base()
		{            
		}
		#endregion

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

		public override T addChild<T> (T child)
		{
			T tmp = base.addChild (child);
			this.RegisterForLayouting (LayoutingType.PositionChildren);
			return tmp;
		}
		public override void removeChild (GraphicObject child)
		{
			base.removeChild (child);
			this.RegisterForLayouting (LayoutingType.PositionChildren);
		}

		#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
		protected override Size measureRawSize ()
		{
			Size tmp = new Size ();

			if (Orientation == Orientation.Horizontal) {
				foreach (GraphicObject c in Children) {
					if (!c.Visible)
						continue;					
					tmp.Width += c.Slot.Width + Spacing;
					tmp.Height = Math.Max (tmp.Height, Math.Max(c.Slot.Bottom, c.Slot.Height));
				}
				if (tmp.Width > 0)
					tmp.Width -= Spacing;
			} else {
				foreach (GraphicObject c in Children) {
					if (!c.Visible)
						continue;					
					tmp.Width = Math.Max (tmp.Width, Math.Max(c.Slot.Right, c.Slot.Width));
					tmp.Height += c.Slot.Height + Spacing;
				}
				if (tmp.Height > 0)
					tmp.Height -= Spacing;
			}

			tmp.Width += 2 * Margin;
			tmp.Height += 2 * Margin;

			return tmp;
		}
		public virtual void ComputeChildrenPositions()
		{
			#if DEBUG_LAYOUTING
			Debug.WriteLine("ComputeChildrenPosition: " + this.ToString());
			#endif
			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);

			LayoutingType childSizeToCheck;
			if (Orientation == Orientation.Horizontal)
				childSizeToCheck = LayoutingType.Width;
			else
				childSizeToCheck = LayoutingType.Height;

			foreach (GraphicObject g in Children) {
				if (!g.Visible)
					continue;
				if (g.RegisteredLayoutings.HasFlag (childSizeToCheck))
					return false;						
			}

			if (layoutType == LayoutingType.PositionChildren) {
				//allow 1 child to have size to 0 if stack has fixed or streched size,
				//this child will occupy remaining space
				if (Orientation == Orientation.Horizontal) {
					if (Width >= 0) {
						GraphicObject stretchedGO = null;
						int tmpWidth = Slot.Width;
						int cptChildren = 0;
						for (int i = 0; i < Children.Count; i++) {
							if (!Children [i].Visible)
								continue;
							cptChildren++;
							if (Children [i].Width == 0) {
								if (stretchedGO != null)
									throw new Exception ("Only one child in stack may have size to stretched");
								stretchedGO = Children [i];
								if (i < Children.Count - 1)
									tmpWidth -= Spacing;
								continue;
							}
							tmpWidth -= Children[i].Slot.Width + Spacing;
						}
						if (stretchedGO != null) {
							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;
								stretchedGO.OnLayoutChanges (LayoutingType.Width);
								stretchedGO.LastSlots.Width = stretchedGO.Slot.Width;
							}
						}
					}					
				} else {
					if (Height >= 0) {
						GraphicObject stretchedGO = null;
						int tmpHeight = Slot.Height;
						int cptChildren = 0;
						for (int i = 0; i < Children.Count; i++) {
							if (!Children [i].Visible)
								continue;
							cptChildren++;
							if (Children [i].Height == 0) {
								if (stretchedGO != null)
									throw new Exception ("Only one child in stack may have size to stretched");
								stretchedGO = Children [i];
								if (i < Children.Count - 1)
									tmpHeight -= Spacing;
								continue;
							}
							tmpHeight -= Children[i].Slot.Height + Spacing;
						}
						if (stretchedGO != null) {
							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;
								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.RegisterForRedraw ();

				return true;
			}

			return base.UpdateLayout(layoutType);
        }

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

			GraphicObject g = sender as GraphicObject;
			switch (arg.LayoutType) {
			case LayoutingType.Width:
				if (Orientation == Orientation.Horizontal) {
					if (this.Bounds.Width < 0)
						this.RegisterForLayouting (LayoutingType.Width);
					this.RegisterForLayouting (LayoutingType.PositionChildren);
				}
				break;
			case LayoutingType.Height:
				if (Orientation == Orientation.Vertical) {
					if (this.Bounds.Height < 0)
						this.RegisterForLayouting (LayoutingType.Height);
					this.RegisterForLayouting (LayoutingType.PositionChildren);
				}
				break;
			}
		}
		#endregion

    
	}
}