aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GraphicObjects/ComboBox.cs
blob: ed979d75437b8482a5b31f1ef1e9c17e2b90b7d5 (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
using System;


using System.Collections.Generic;
using System.Linq;
using System.Text;
//using OpenTK.Graphics.OpenGL;

using Cairo;

using winColors = System.Drawing.Color;
using System.Diagnostics;
using System.Xml.Serialization;
using OpenTK.Input;
using System.ComponentModel;
using System.Xml;
using System.IO;
using System.Collections;
using System.Threading;

namespace Crow
{
	[DefaultTemplate("#Crow.Templates.ComboBox.goml")]
	[DefaultOverlayTemplate("#Crow.Templates.ComboBoxOverlay.goml")]
	public class ComboBox : TemplatedContainer
    {		
		#region CTOR
		public ComboBox() : base(){	}	
		#endregion

		bool _isPopped;
		string caption;
		GraphicObject _overlay;
		Group _list;
		IList data;
		int _selectedIndex;
		object _selectedItem;
		string _itemTemplate;
		string _overlayTemplate;

		public event EventHandler Pop;
		public event EventHandler Unpop;
		public event EventHandler<SelectionChangeEventArgs> SelectedItemChanged;

		#region implemented abstract members of TemplatedControl
		protected override void loadTemplate (GraphicObject template)
		{
			base.loadTemplate (template);
			loadOverlayTemplate (null);
		}
		public override GraphicObject Content {
			get {
				throw new NotImplementedException ();
			}
			set {
				throw new NotImplementedException ();
			}
		}
		#endregion

		protected virtual void loadOverlayTemplate(GraphicObject overlayTemplate)
		{
			if (overlayTemplate == null) {
				DefaultOverlayTemplate dt = (DefaultOverlayTemplate)this.GetType ().GetCustomAttributes (typeof(DefaultOverlayTemplate), true).FirstOrDefault ();
				Overlay = Interface.Load (dt.Path);
				Overlay.ResolveBindings ();
			} else
				Overlay = overlayTemplate;
			_list = Overlay.FindByName ("List") as Group;
		}

		[XmlAttributeAttribute][DefaultValue("#Crow.Templates.ItemTemplate.goml")]
		public string ItemTemplate {
			get { return _itemTemplate; }
			set { 
				//TODO:reload list with new template?
				_itemTemplate = value; 
			}
		}
		[XmlAttributeAttribute][DefaultValue("#Crow.Templates.ComboBoxOverlay.goml")]
		public string OverlayTemplate {
			get { return _overlayTemplate; }
			set { 
				//TODO:reload list with new template?
				_overlayTemplate = value; 

				Overlay = Interface.Load (_overlayTemplate);
				Overlay.ResolveBindings ();
				_list = Overlay.FindByName ("List") as Group;
			}
		}
		[XmlAttributeAttribute][DefaultValue(-1)]
		public int SelectedIndex{
			get { return _selectedIndex; }
			set { 
				//store value event if data is null, because in xml parsing selindex is always
				//before data, so it's impossible without that trick to set a default index in goml
				_selectedIndex = value;	
	
				if (data == null)					
					return;
	
				if (_selectedIndex > data.Count - 1 || _selectedIndex < 0)
					throw new Exception ("Combobox SelectedIndex out of range");


				_selectedItem = data [_selectedIndex];
				NotifyValueChanged ("SelectedIndex", SelectedIndex);
				SelectedItemChanged.Raise (this, new SelectionChangeEventArgs(_selectedItem));

				if (SelectedItem == null)
					Caption = "";
				else
					Caption = _selectedItem.ToString ();
				}
		}
		public object SelectedItem{
			set {
				if (_selectedItem == value)
					return;

				_selectedItem = value;
				_selectedIndex = data.IndexOf (_selectedItem);
				NotifyValueChanged ("SelectedIndex", _selectedIndex);
				SelectedItemChanged.Raise (this, new SelectionChangeEventArgs(_selectedItem));

				if (SelectedItem == null)
					Caption = "";
				else
					Caption = _selectedItem.ToString ();
			}

			get { return _selectedItem; }
		}
		[XmlAttributeAttribute][DefaultValue(null)]
		public IList Data {
			get {
				return data;
			}
			set {				
				data = value;
				if (_list == null)
					return;

				foreach (GraphicObject c in _list.Children) {
					c.ClearBinding ();
				}
				_list.Children.Clear ();
				_list.registerForGraphicUpdate ();
				if (data == null)
					return;
				if (SelectedIndex < 0)
					return;

				//force raise of changes
				SelectedIndex = SelectedIndex;

				pendingChildrenAddition = new Queue<GraphicObject> ();
				threadedLoadingFinished = false;

				Thread t = new Thread (loadingThread);
				t.Start ();
				t.Join ();

			}
		}
		public override void UpdateLayout (LayoutingType layoutType)
		{
			CheckPendingChildrenAddition ();
			base.UpdateLayout (layoutType);
		}
		internal void CheckPendingChildrenAddition()
		{
			if (pendingChildrenAddition == null)
				return;
			lock (pendingChildrenAddition) {
				if (!threadedLoadingFinished && pendingChildrenAddition.Count < 50)
					return;
				while (pendingChildrenAddition.Count > 0) {
					_list.addChild (pendingChildrenAddition.Dequeue ());
				}
			}
		}

		volatile Queue<GraphicObject> pendingChildrenAddition;
		volatile bool threadedLoadingFinished = false;

		void loadingThread()
		{
			#if DEBUG_LOAD_TIME
			Stopwatch loadingTime = new Stopwatch ();
			loadingTime.Start ();
			#endif

			MemoryStream ms = new MemoryStream ();
			lock (ItemTemplate) {
				using (Stream stream = Interface.GetStreamFromPath (ItemTemplate))
					stream.CopyTo (ms);
			}

			Type t = Interface.GetTopContainerOfGOMLStream (ms);

			foreach (var item in data) {
				ms.Seek(0,SeekOrigin.Begin);
				GraphicObject g = Interface.Load (ms, t);
				g.DataSource = item;
				g.MouseClick += itemClick;

				lock (pendingChildrenAddition) {
					pendingChildrenAddition.Enqueue (g);
				}
			}

			ms.Dispose ();			

			threadedLoadingFinished = true;

			#if DEBUG_LOAD_TIME
			loadingTime.Stop ();
			Debug.WriteLine("Listbox {2} Loading: {0} ticks \t, {1} ms",
			loadingTime.ElapsedTicks,
			loadingTime.ElapsedMilliseconds, this.ToString());
			#endif
		}
		public GraphicObject Overlay {
			get { return _overlay; }
			set { 
				if (_overlay != null) {
					_overlay.LayoutChanged -= _overlay_LayoutChanged;
					_overlay.MouseLeave -= _overlay_MouseLeave;
					_overlay.LogicalParent = null;
				}

				_overlay = value; 

				if (_overlay == null)
					return;

				_overlay.LogicalParent = this;
				_overlay.Focusable = true;
				_overlay.LayoutChanged += _overlay_LayoutChanged;
				_overlay.MouseLeave += _overlay_MouseLeave;
			}
		}
		[XmlAttributeAttribute()][DefaultValue("Combobox")]
		public string Caption {
			get { return caption; } 
			set {
				if (caption == value)
					return;
				caption = value; 
				NotifyValueChanged ("Caption", caption);
			}
		}        
		[XmlAttributeAttribute()][DefaultValue(false)]
		public bool IsPopped
		{
			get { return _isPopped; }
			set
			{
				_isPopped = value;

				if (_isPopped) {
					onPop (this, null);
					NotifyValueChanged ("SvgSub", "expanded");
					return;
				}

				onUnpop (this, null);
				NotifyValueChanged ("SvgSub", "collapsed");
			}
		}

		void itemClick(object sender, OpenTK.Input.MouseButtonEventArgs e){
			object datasource = (sender as GraphicObject).DataSource;
			SelectedItem = datasource;
			IsPopped = false;
			//Debug.WriteLine ((sender as GraphicObject).DataSource);
		}
		void _overlay_MouseLeave (object sender, MouseMoveEventArgs e)
		{
			IsPopped = false;
		}
		void _overlay_LayoutChanged (object sender, LayoutChangeEventArgs e)
		{
			ILayoutable tc = Overlay.Parent as ILayoutable;
			if (tc == null)
				return;
			Rectangle r = this.ScreenCoordinates (this.Slot);
			if (e.LayoutType == LayoutingType.Width) {
				if (Overlay.Slot.Width < tc.ClientRectangle.Width) {
					if (r.Left + Overlay.Slot.Width > tc.ClientRectangle.Right)
						Overlay.Left = tc.ClientRectangle.Right - Overlay.Slot.Width;
					else
						Overlay.Left = r.Left;
				}else
					Overlay.Left = 0;
			}else if (e.LayoutType == LayoutingType.Height) {
				if (Overlay.Slot.Height < tc.ClientRectangle.Height) {
					if (r.Bottom + Overlay.Slot.Height > tc.ClientRectangle.Bottom)
						Overlay.Top = r.Top - Overlay.Slot.Height;
					else
						Overlay.Top = r.Bottom;
				}else
					Overlay.Top = 0;
			}
		}

		[XmlAttributeAttribute()][DefaultValue(true)]//overiden to get default to true
		public override bool Focusable
		{
			get { return base.Focusable; }
			set { base.Focusable = value; }
		}

			
		public virtual void onPop(object sender, EventArgs e)
		{
			IGOLibHost tc = HostContainer;
			if (tc == null)
				return;
			if (Overlay != null) {
				Overlay.Visible = true;
				if (Overlay.Parent == null)
					tc.AddWidget (Overlay);
				(tc as OpenTKGameWindow).PutOnTop (Overlay);
			}
			Pop.Raise (this, e);
		}
		public virtual void onUnpop(object sender, EventArgs e)
		{
			IGOLibHost tc = HostContainer;
			if (tc == null)
				return;
			Overlay.Visible = false;
			Unpop.Raise (this, e);
		}
			
		public override void onMouseClick (object sender, MouseButtonEventArgs e)
		{
			IsPopped = !IsPopped;
			base.onMouseClick (sender, e);
		}

		public override void ClearBinding ()
		{
			//ensure popped window is cleared
			if (Overlay != null) {
				if (Overlay.Parent != null) {
					IGOLibHost tc = Overlay.Parent as IGOLibHost;
					if (tc != null)
						tc.DeleteWidget (Overlay);
				}
			}
			base.ClearBinding ();

		}
		public override void ResolveBindings ()
		{
			base.ResolveBindings ();
			if (Overlay != null)
				Overlay.ResolveBindings ();
		}
	}
}