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

namespace go
{
	public class Group : GraphicObject, IXmlSerializable
    {
		#region CTOR
		public Group()
			: base()
		{            
		}
		#endregion

		#region EVENT HANDLERS
		public event EventHandler<EventArgs> ChildrenCleared;
		#endregion

        bool _multiSelect = false;
		List<GraphicObject> children = new List<GraphicObject>();

        public GraphicObject activeWidget;

        public virtual List<GraphicObject> Children {
			get { return children; }
			set { 
				children = value; 
			}
		}
		[XmlAttributeAttribute()][DefaultValue(false)]
        public bool MultiSelect
        {
            get { return _multiSelect; }
            set { _multiSelect = value; }
        }
			
			
        public virtual T addChild<T>(T child)
        {
			GraphicObject g = child as GraphicObject;
            Children.Add(g);
            g.Parent = this as GraphicObject;            
			g.RegisterForLayouting ((int)LayoutingType.Sizing);
            return (T)child;
        }
        public virtual void removeChild(GraphicObject child)        
		{			
			child.ClearBinding ();
			child.Parent = null;
            Children.Remove(child);
			this.RegisterForLayouting ((int)LayoutingType.Sizing);
        }
		public virtual void ClearChildren()
		{
			int lim = children.Count;
			for (int i = 0; i < lim; i++) {
				GraphicObject g = Children [0];
				g.ClearBinding ();
				g.Parent = null;
				Children.Remove(g);				
			}
			this.RegisterForLayouting ((int)LayoutingType.Sizing);
			ChildrenCleared.Raise (this, new EventArgs ());
		}
		public void putWidgetOnTop(GraphicObject w)
		{
			if (Children.Contains(w))
			{
				Children.Remove(w);
				Children.Add(w);
			}
		}
		public void putWidgetOnBottom(GraphicObject w)
		{
			if (Children.Contains(w))
			{
				Children.Remove(w);
				Children.Insert(0, w);
			}
		}
			
		#region GraphicObject overrides
		[XmlIgnore]public override bool DrawingIsValid {
			get {
				if (!base.DrawingIsValid)
					return false;
				foreach (GraphicObject g in Children) {
					if (!g.DrawingIsValid)
						return false;
				}
				return true;
			}
		}
		public override void ResolveBindings ()
		{
			base.ResolveBindings ();
			foreach (GraphicObject w in Children)
				w.ResolveBindings ();
		}
		public override GraphicObject FindByName (string nameToFind)
		{
			if (Name == nameToFind)
				return this;

			foreach (GraphicObject w in Children) {
				GraphicObject r = w.FindByName (nameToFind);
				if (r != null)
					return r;
			}
			return null;
		}
		public override bool Contains (GraphicObject goToFind)
		{
			foreach (GraphicObject w in Children) {
				if (w == goToFind)
					return true;
				if (w.Contains (goToFind))
					return true;
			}
			return false;
		}
		protected override Size measureRawSize ()
		{
			Size tmp = new Size ();

			foreach (GraphicObject c in Children.Where(ch=>ch.Visible)) {
				tmp.Width = Math.Max (tmp.Width, c.Slot.Right);
				tmp.Height = Math.Max (tmp.Height, c.Slot.Bottom);
			}

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

			return tmp;
		}
			
		public override void OnLayoutChanges (LayoutingType layoutType)
		{
			base.OnLayoutChanges (layoutType);

			//position smaller objects in group when group size is fit
			switch (layoutType) {
			case LayoutingType.Width:				
				if (Width < 0) {
					int crw = ClientRectangle.Width;
					foreach (GraphicObject c in Children.Where(ch => ch.Slot.Width != crw && ch.Visible))
						c.RegisterForLayouting ((int)LayoutingType.X);						
				} else {
					foreach (GraphicObject c in Children.Where(ch => ch.Width == 0 && ch.Visible))
						c.RegisterForLayouting ((int)LayoutingType.Width);											
				}
				break;
			case LayoutingType.Height:
				if (Height < 0) {
					int crh = ClientRectangle.Height;
					foreach (GraphicObject c in Children.Where(ch => ch.Slot.Height != crh && ch.Visible))
						c.RegisterForLayouting ((int)LayoutingType.Y);						
				} else {
					foreach (GraphicObject c in Children.Where(ch => ch.Height == 0 && ch.Visible))
						c.RegisterForLayouting ((int)LayoutingType.Height);											
				}
				break;
			}
		}

		public override Rectangle ContextCoordinates(Rectangle r){
			return r + ClientRectangle.Position;
		}	

		protected override void onDraw (Context gr)
		{
			Rectangle rBack = new Rectangle (Slot.Size);
			if (BackgroundImage != null)				
				BackgroundImage.Paint (gr, rBack, BackImgSub);
			gr.Color = Background;
			CairoHelpers.CairoRectangle(gr,rBack,CornerRadius);
			gr.Fill ();

			foreach (GraphicObject g in Children) {
				g.Paint (ref gr);
			}
		}

		public override void Paint(ref Context ctx, Rectangles clip = null)
		{
			if ( !Visible )
				return;

			if (bmp == null)
				UpdateGraphic ();
			else {

				Rectangle rb = Parent.ContextCoordinates (Slot);

				if (clip != null)
					clip.Rebase (this);									
				else {
					clip = new Rectangles ();
					//TODO:added lately slot to empty clip,
					//should rework this precise case causing expandable not
					//to show image changes
					clip.AddRectangle (ContextCoordinates (Slot.Size));
				}

				if (!DrawingIsValid || clip != null) {//false when 1 child has changed
					//child having their content changed has to be repainted
					//and those with slot intersecting clip rectangle have also to be repainted

					using (ImageSurface cache =
						      new ImageSurface (bmp, Format.Argb32, Slot.Width, Slot.Height, Slot.Width * 4)) {
						Context gr = new Context (cache);
						clip.clearAndClip (gr);

						Rectangle rBack = Slot.Size;
						gr.Color = Background;
						CairoHelpers.CairoRectangle(gr,rBack,CornerRadius);
						gr.Fill ();
						#if DEBUG_CLIP_RECTANGLE
						clip.stroke (gr, Color.Amaranth.AdjustAlpha (0.8));
						#endif
						foreach (GraphicObject c in Children.Where(ch=>ch.Visible)) {
							Rectangles childClip = clip.intersectingRects (ContextCoordinates(c.Slot));
							if (!c.DrawingIsValid || childClip.count > 0)
								c.Paint (ref gr, childClip);//, localClip);
						}

						gr.Dispose ();
					}
				}
			}
				
			base.Paint (ref ctx, clip);
		}
		#endregion

	
		#region Mouse handling
		public override void checkHoverWidget (OpenTK.Input.MouseMoveEventArgs e)
		{
			if (TopContainer.hoverWidget != this) {
				TopContainer.hoverWidget = this;
				onMouseEnter (this, e);
			}
			foreach (GraphicObject g in Children)
			{
				if (g.MouseIsIn(e.Position))
				{
					g.checkHoverWidget (e);
					return;
				}
			}
			base.checkHoverWidget (e);
		}
		#endregion

		#region IXmlSerializable

        public override System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }
        public override void ReadXml(System.Xml.XmlReader reader)
        {
            base.ReadXml(reader);

            using (System.Xml.XmlReader subTree = reader.ReadSubtree())
            {
                subTree.Read();

                while (!subTree.EOF)
                {
                    subTree.Read();

                    if (!subTree.IsStartElement())
                        break;

                    Type t = Type.GetType("go." + subTree.Name);
                    GraphicObject go = (GraphicObject)Activator.CreateInstance(t);
                    (go as IXmlSerializable).ReadXml(subTree);                    
                    addChild(go);
                }
            }
        }
        public override void WriteXml(System.Xml.XmlWriter writer)
        {
            base.WriteXml(writer);

            foreach (GraphicObject go in Children)
            {
                writer.WriteStartElement(go.GetType().Name);
                (go as IXmlSerializable).WriteXml(writer);
                writer.WriteEndElement();
            }
        }
    
		#endregion

		public override void ClearBinding(){
			foreach (GraphicObject c in children)
				c.ClearBinding ();
			base.ClearBinding ();
		}
	}
}