aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GraphicObjects/Group.cs
blob: 1b5847d497119f76573eeee2243e5162460963fa (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//
// Group.cs
//
// Author:
//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using Cairo;
using System.Diagnostics;
using System.Reflection;


namespace Crow
{
	public class Group : GraphicObject
    {
		#region CTOR
		public Group () : base() {}
		public Group(Interface iface) : base(iface){}
		#endregion

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

		internal GraphicObject largestChild = null;
		internal GraphicObject tallestChild = null;

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

        public virtual List<GraphicObject> Children {
			get { return children; }
		}
		[XmlAttributeAttribute()][DefaultValue(false)]
        public bool MultiSelect
        {
            get { return _multiSelect; }
            set { _multiSelect = value; }
        }
		public virtual void AddChild(GraphicObject g){
			lock (Children) {
				g.Parent = this;
				Children.Add (g);
			}
			g.RegisteredLayoutings = LayoutingType.None;
			g.LayoutChanged += OnChildLayoutChanges;
			g.RegisterForLayouting (LayoutingType.Sizing | LayoutingType.ArrangeChildren);
		}
        public virtual void RemoveChild(GraphicObject child)
		{
			child.LayoutChanged -= OnChildLayoutChanges;
			//check if HoverWidget is removed from Tree
			if (CurrentInterface.HoverWidget != null) {
				if (this.Contains (CurrentInterface.HoverWidget))
					CurrentInterface.HoverWidget = null;
			}

			lock (Children)
				Children.Remove(child);

			if (child == largestChild && Width == Measure.Fit)
				searchLargestChild ();
			if (child == tallestChild && Height == Measure.Fit)
				searchTallestChild ();

			this.RegisterForLayouting (LayoutingType.Sizing | LayoutingType.ArrangeChildren);

		}
        public virtual void DeleteChild(GraphicObject child)
		{
			RemoveChild (child);
			child.Dispose ();
        }
		public virtual void InsertChild (int idx, GraphicObject g) {
			lock (Children) {
				g.Parent = this;
				Children.Insert (idx, g);
			}
			g.RegisteredLayoutings = LayoutingType.None;
			g.LayoutChanged += OnChildLayoutChanges;
			g.RegisterForLayouting (LayoutingType.Sizing | LayoutingType.ArrangeChildren);
		}
		public virtual void DeleteChild (int idx) {
			DeleteChild (children[idx]);
		}
		public virtual void ClearChildren()
		{
			lock (Children) {
				while (Children.Count > 0) {
					GraphicObject g = Children [Children.Count - 1];
					g.LayoutChanged -= OnChildLayoutChanges;
					Children.RemoveAt (Children.Count - 1);
					g.Dispose ();
				}
			}

			resetChildrenMaxSize ();

			this.RegisterForLayouting (LayoutingType.Sizing);
			ChildrenCleared.Raise (this, new EventArgs ());
		}

		public void putWidgetOnTop(GraphicObject w)
		{
			if (Children.Contains(w))
			{
				lock (Children) {
					Children.Remove (w);
					Children.Add (w);
				}
			}
		}
		public void putWidgetOnBottom(GraphicObject w)
		{
			if (Children.Contains(w))
			{
				lock (Children) {
					Children.Remove (w);
					Children.Insert (0, w);
				}
			}
		}

		#region GraphicObject overrides
		public override void OnDataSourceChanged (object sender, DataSourceChangeEventArgs e)
		{
			base.OnDataSourceChanged (this, e);
			lock (Children) {
				foreach (GraphicObject g in children)
					if (g.localDataSourceIsNull & g.localLogicalParentIsNull)
						g.OnDataSourceChanged (g, e);
			}
		}
		public override GraphicObject FindByName (string nameToFind)
		{
			if (Name == nameToFind)
				return this;
			GraphicObject tmp = null;
			lock (Children) {
				foreach (GraphicObject w in Children) {
					tmp = w.FindByName (nameToFind);
					if (tmp != null)
						break;
				}
			}
			return tmp;
		}
		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 int measureRawSize (LayoutingType lt)
		{
			if (Children.Count > 0) {
				if (lt == LayoutingType.Width) {
					if (largestChild == null)
						searchLargestChild ();
					if (largestChild == null) {
						//if still null, not possible to determine a width
						//because all children are stretched, force first one to fit
						Children [0].Width = Measure.Fit;
						return -1;//cancel actual sizing to let child computation take place
					}
				} else {
					if (tallestChild == null)
						searchTallestChild ();
					if (tallestChild == null) {
						Children [0].Height = Measure.Fit;
						return -1;
					}
				}
			}
			return base.measureRawSize (lt);
		}

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

			//position smaller objects in group when group size is fit
			switch (layoutType) {
			case LayoutingType.Width:
				foreach (GraphicObject c in Children){
					if (c.Width.Units == Unit.Percent)
						c.RegisterForLayouting (LayoutingType.Width);
					else
						c.RegisterForLayouting (LayoutingType.X);
				}
				break;
			case LayoutingType.Height:
				foreach (GraphicObject c in Children) {
					if (c.Height.Units == Unit.Percent)
						c.RegisterForLayouting (LayoutingType.Height);
					else
						c.RegisterForLayouting (LayoutingType.Y);
				}
				break;
			}
		}
		protected override void onDraw (Context gr)
		{
			base.onDraw (gr);

			gr.Save ();

			if (ClipToClientRect) {
				//clip to client zone
				CairoHelpers.CairoRectangle (gr, ClientRectangle, CornerRadius);
				gr.Clip ();
			}

			lock (Children) {
				foreach (GraphicObject g in Children) {
					g.Paint (ref gr);
				}
			}
			gr.Restore ();
		}
		protected override void UpdateCache (Context ctx)
		{
			Rectangle rb = Slot + Parent.ClientRectangle.Position;


			Context gr = new Context (bmp);

			if (!Clipping.IsEmpty) {
				for (int i = 0; i < Clipping.NumRectangles; i++)
					gr.Rectangle(Clipping.GetRectangle(i));
				gr.ClipPreserve();
				gr.Operator = Operator.Clear;
				gr.Fill();
				gr.Operator = Operator.Over;

				base.onDraw (gr);

				if (ClipToClientRect) {
					CairoHelpers.CairoRectangle (gr, ClientRectangle, CornerRadius);
					gr.Clip ();
				}

				lock (Children) {
					foreach (GraphicObject c in Children) {
						if (!c.Visible)
							continue;
						if (Clipping.Contains (c.Slot + ClientRectangle.Position) == RegionOverlap.Out)
							continue;
						c.Paint (ref gr);
					}
				}

				#if DEBUG_CLIP_RECTANGLE
				Clipping.stroke (gr, Color.Amaranth.AdjustAlpha (0.8));
				#endif
			}
			gr.Dispose ();

			ctx.SetSourceSurface (bmp, rb.X, rb.Y);
			ctx.Paint ();

			Clipping.Dispose();
			Clipping = new Region ();
		}
		#endregion

		public virtual void OnChildLayoutChanges (object sender, LayoutingEventArgs arg)
		{
			GraphicObject g = sender as GraphicObject;

			switch (arg.LayoutType) {
			case LayoutingType.Width:
				if (Width != Measure.Fit)
					return;
				if (g.Slot.Width > contentSize.Width) {
					largestChild = g;
					contentSize.Width = g.Slot.Width;
				} else if (g == largestChild)
					searchLargestChild ();

				this.RegisterForLayouting (LayoutingType.Width);
				break;
			case LayoutingType.Height:
				if (Height != Measure.Fit)
					return;
				if (g.Slot.Height > contentSize.Height) {
					tallestChild = g;
					contentSize.Height = g.Slot.Height;
				} else if (g == tallestChild)
					searchTallestChild ();

				this.RegisterForLayouting (LayoutingType.Height);
				break;
			}
		}
		//TODO: x,y position should be taken in account for computation of width and height
		void resetChildrenMaxSize(){
			largestChild = null;
			tallestChild = null;
			contentSize = 0;
		}
		void searchLargestChild(){
			#if DEBUG_LAYOUTING
			Debug.WriteLine("\tSearch largest child");
			#endif
			largestChild = null;
			contentSize.Width = 0;
			for (int i = 0; i < Children.Count; i++) {
				if (!Children [i].Visible)
					continue;
				if (children [i].RegisteredLayoutings.HasFlag (LayoutingType.Width))
					continue;
				if (Children [i].Slot.Width > contentSize.Width) {
					contentSize.Width = Children [i].Slot.Width;
					largestChild = Children [i];
				}
			}
		}
		void searchTallestChild(){
			#if DEBUG_LAYOUTING
			Debug.WriteLine("\tSearch tallest child");
			#endif
			tallestChild = null;
			contentSize.Height = 0;
			for (int i = 0; i < Children.Count; i++) {
				if (!Children [i].Visible)
					continue;
				if (children [i].RegisteredLayoutings.HasFlag (LayoutingType.Height))
					continue;
				if (Children [i].Slot.Height > contentSize.Height) {
					contentSize.Height = Children [i].Slot.Height;
					tallestChild = Children [i];
				}
			}
		}


		#region Mouse handling
		public override void checkHoverWidget (MouseMoveEventArgs e)
		{
			if (CurrentInterface.HoverWidget != this) {
				CurrentInterface.HoverWidget = this;
				onMouseEnter (this, e);
			}
			for (int i = Children.Count - 1; i >= 0; i--) {
				if (Children[i].MouseIsIn(e.Position))
				{
					Children[i].checkHoverWidget (e);
					return;
				}
			}
			base.checkHoverWidget (e);
		}
//		public override bool PointIsIn (ref Point m)
//		{
//			if (!base.PointIsIn (ref m))
//				return false;
//			if (CurrentInterface.HoverWidget == this)
//				return true;
//			lock (Children) {
//				for (int i = Children.Count - 1; i >= 0; i--) {
//					if (Children [i].Slot.ContainsOrIsEqual (m) && !(bool)CurrentInterface.HoverWidget?.IsOrIsInside(Children[i])) {						
//						return false;
//					}
//				}
//			}
//			return true;
//		}
		#endregion

		protected override void Dispose (bool disposing)
		{
			if (disposing) {
				foreach (GraphicObject c in children)
					c.Dispose ();
			}
			base.Dispose (disposing);
		}
	}
}