aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GraphicObjects/TextBox.cs
blob: 4df0b3f7a4358aa9b14bba20d2f2564f166c9ba1 (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
409
410
411
412
413
414
415
416
417
418
419
420
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Input;
using Cairo;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Globalization;

namespace go
{
    public class TextBoxWidget : Label
    {
		public static KeyboardDevice Keyboard;
		public static char decimalSeparator = '.';//????? should follow locale policy
		static bool _capitalOn = false;	//????????????????
		public static bool capitalOn
		{
			get
			{
				return _capitalOn;
					//Keyboard[Key.ShiftLeft] || Keyboard[Key.ShiftRight] ?
					//!_capitalOn : _capitalOn;
			}
			set { _capitalOn = value; }
		}

        Color selColor = Color.BlueBell;
        Color selFontColor = Color.WhiteSmoke;
        Point mouseLocalPos;    //mouse coord in widget space, filled only when clicked        
        int _currentPos;        //0 based cursor position in string
        double textCursorPos;   //cursor position in cairo units in widget client coord.
        double SelStartCursorPos = -1;
        double SelEndCursorPos = -1;
        bool SelectionInProgress = false;

		[XmlIgnore]
        public override bool HasFocus   //trigger update when lost focus to errase text beam
        {
            get
            {
                return base.HasFocus;
            }
            set
            {
                base.HasFocus = value;
                registerForGraphicUpdate();
            }
        }

		[System.Xml.Serialization.XmlAttributeAttribute()]
		[System.ComponentModel.DefaultValue(true)]
		public override bool Focusable
		{
			get { return base.Focusable; }
			set { base.Focusable = value; }
		}

        [XmlIgnore]
        public GOEvent onTextChanged;

        [XmlIgnore]
        public int currentPos
        {
            get { return _currentPos; }
            set { _currentPos = value; }
        }

        [XmlIgnore]
        public int selBeginPos;
        [XmlIgnore]
        public int selReleasePos;
        [XmlIgnore]
        public int selectionStart   //ordered selection start and end positions
        {
            get { return selReleasePos < 0 ? selBeginPos : Math.Min(selBeginPos, selReleasePos); }
        }
        [XmlIgnore]
        public int selectionEnd
        { get { return selReleasePos < 0 ? selReleasePos : Math.Max(selBeginPos, selReleasePos); } }
        [XmlIgnore]
        public string selectedText
        { get { return selectionEnd < 0 ? null : Text.Substring(selectionStart, selectionEnd - selectionStart); } }
        [XmlIgnore]
        public bool selectionIsEmpty
        { get { return string.IsNullOrEmpty(selectedText); } }

        public TextBoxWidget(string _initialValue, GOEvent _onTextChanged = null)
            : base(_initialValue)
        {
            onTextChanged = _onTextChanged;
            Focusable = true;
            selColor = Color.SkyBlue;
            selFontColor = Color.Black;
            Foreground = Color.Black;
            Background = Color.White;
            TextAlignment = Alignment.LeftCenter;
        }

        public TextBoxWidget()
        { }

		public override void onDraw (Context gr)
		{
			base.onDraw (gr);
//			gr.FontOptions.Antialias = Antialias.Subpixel;
//			gr.FontOptions.HintMetrics = HintMetrics.On;
			gr.SetFontSize(FontSize);
			FontExtents fe = gr.FontExtents;

			#region draw text cursor
			if (mouseLocalPos > 0)
			{
				computeTextCursor(gr);

				if (SelectionInProgress)
				{
					selReleasePos = currentPos;
					SelEndCursorPos = textCursorPos;
				}
				else if (selBeginPos < 0)
				{
					selBeginPos = currentPos;
					SelStartCursorPos = textCursorPos;
					selReleasePos = -1;
				}
				else
					computeTextCursorPosition(gr);

			}
			else
				computeTextCursorPosition(gr);

			if (HasFocus)
			{
				//TODO:
				gr.Color = Color.White;
				gr.LineWidth = 2;
				gr.MoveTo(new PointD(textCursorPos + rText.X, rText.Y ));
				gr.LineTo(new PointD(textCursorPos + rText.X, rText.Y + fe.Height));
				gr.Stroke();
			}

			#endregion

			if (selReleasePos >= 0)
			{
				gr.Color = selColor;
				Rectangle selRect =
					new Rectangle((int)SelStartCursorPos + rText.X, (int)(rText.Y), (int)(SelEndCursorPos - SelStartCursorPos), (int)fe.Height);
				gr.Rectangle(selRect);
				gr.Fill();

				gr.Color = selFontColor;
			}
			else
				gr.Color = Foreground;

			gr.MoveTo(rText.X, rText.Y + fe.Ascent);
			#if _WIN32 || _WIN64
			gr.ShowText(txt);
			#elif __linux__
			gr.ShowText(Text);
			#endif
			gr.Fill();

		}
			
        #region Keyboard handling
		public override void onKeyDown (object sender, KeyboardKeyEventArgs e)
		{
			base.onKeyDown (sender, e);

			Key key = e.Key;

			switch (key)
			{
			case Key.Back:
				if (!selectionIsEmpty)
				{
					Text = Text.Remove(selectionStart, selectionEnd - selectionStart);
					selReleasePos = -1;
					currentPos = selBeginPos;
				}
				else if (currentPos > 0)
				{
					currentPos--;
					Text = Text.Remove(currentPos, 1);
				}
				break;
			case Key.Clear:
				break;
			case Key.Delete:
				if (!selectionIsEmpty)
				{
					Text = Text.Remove(selectionStart, selectionEnd - selectionStart);
					selReleasePos = -1;
					currentPos = selBeginPos;
				}
				else if (currentPos < Text.Length)
					Text = Text.Remove(currentPos, 1);
				break;
			case Key.Down:
				break;
			case Key.End:
				currentPos = Text.Length;
				break;
			case Key.Enter:
			case Key.KeypadEnter:
				if (onTextChanged != null)
					onTextChanged(this);
				break;
			case Key.Escape:
				Text = "";
				currentPos = 0;
				selReleasePos = -1;
				break;
			case Key.Home:
				currentPos = 0;
				break;
			case Key.Insert:
				break;
			case Key.Left:
				if (currentPos > 0)
					currentPos--;
				break;
			case Key.Menu:
				break;
			case Key.NumLock:
				break;
			case Key.PageDown:
				break;
			case Key.PageUp:
				break;
			case Key.RWin:
				break;
			case Key.Right:
				if (currentPos < Text.Length)
					currentPos++;
				break;
			case Key.Tab:
				Text = Text.Insert(currentPos, "\t");
				currentPos++;
				break;
			case Key.Up:
				break;
			case Key.KeypadDecimal:
				Text = Text.Insert(currentPos, new string(new char[] 
					{ Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) }));
				currentPos++;
				break;
			case Key.Space:
				Text = Text.Insert(currentPos, " ");
				currentPos++;
				break;
			case Key.KeypadDivide:
			case Key.Slash:
				Text = Text.Insert(currentPos, "/");
				currentPos++;
				break;
			case Key.KeypadMultiply:
				Text = Text.Insert(currentPos, "*");
				currentPos++;
				break;
			case Key.KeypadMinus:
			case Key.Minus:
				Text = Text.Insert(currentPos, "-");
				currentPos++;
				break;
			case Key.KeypadPlus:
			case Key.Plus:
				Text = Text.Insert(currentPos, "+");
				currentPos++;
				break;
			default:
				if (!selectionIsEmpty)
				{
					Text = Text.Remove(selectionStart, selectionEnd - selectionStart);
					currentPos = selBeginPos;
				}

				string k = "?";
				if ((char)key >= 67 && (char)key <= 76)
					k = ((int)key - 67).ToString();
				else if ((char)key >= 109 && (char)key <= 118)
					k = ((int)key - 109).ToString();
				else if (capitalOn)
					k = key.ToString();
				else
					k = key.ToString().ToLower();

				Text = Text.Insert(currentPos, k);
				currentPos++;

				selReleasePos = -1;
				selBeginPos = currentPos;

				break;
			}
			registerForGraphicUpdate();
		}
        #endregion

        #region Mouse handling
		public override void onMouseEnter (object sender, MouseMoveEventArgs e)
		{
//			SelectionInProgress = true;                
//			mouseLocalPos = e.Position - ScreenCoordBounds.TopLeft - rText.TopLeft;
//			registerForGraphicUpdate();

			base.onMouseEnter (sender, e);
		}
		public override void onMouseLeave (object sender, MouseMoveEventArgs e)
		{
			base.onMouseLeave (sender, e);
		}
		public override void onMouseMove (object sender, MouseMoveEventArgs e)
		{
			base.onMouseMove (sender, e);

			if ((sender as OpenTKGameWindow).activeWidget != this)
				return;

			SelectionInProgress = true;
			mouseLocalPos = e.Position - ScreenCoordinates(rText).TopLeft;
			registerForGraphicUpdate();
		
		}
		public override void onMouseButtonDown (object sender, MouseButtonEventArgs e)
		{
            if (this.HasFocus){
				mouseLocalPos = Mouse.Position - ScreenCoordinates(rText).TopLeft - rText.TopLeft;
				selBeginPos = -1;
				selReleasePos = -1;
			}else{
				selBeginPos = 0;
				selReleasePos = Text.Length;
			}            

			//done at the end to set 'hasFocus' value after testing it
			base.onMouseButtonDown (sender, e);

            registerForGraphicUpdate();
		}
		public override void onMouseButtonUp (object sender, MouseButtonEventArgs e)
		{
			base.onMouseButtonUp (sender, e);
			SelectionInProgress = false;
		}
        #endregion

        void computeTextCursor(Context gr)
        {
            FontExtents fe = gr.FontExtents;
            TextExtents te;

            double cPos = 0f;
            for (int i = 0; i < _text.Length; i++)
            {
#if _WIN32 || _WIN64
                byte[] c = System.Text.UTF8Encoding.UTF8.GetBytes(Text.Substring(i, 1));
                te = gr.TextExtents(c);
#elif __linux__
                te = gr.TextExtents(Text.Substring(i, 1));
#endif
                double halfWidth = te.XAdvance / 2;

                if (mouseLocalPos.X <= cPos + halfWidth)
                {
                    currentPos = i;
                    textCursorPos = cPos;
                    mouseLocalPos = -1;
                    return;
                }

                cPos += te.XAdvance;
            }
            currentPos = _text.Length;
            textCursorPos = cPos;

            //reset mouseLocalPos
            mouseLocalPos = -1;
        }
        void computeTextCursorPosition(Context gr)
        {
            FontExtents fe = gr.FontExtents;
            TextExtents te;

            double cPos = 0f;

            int limit = currentPos;

            if (selectionEnd > 0)
                limit = Math.Max(currentPos, selectionEnd);

            for (int i = 0; i <= limit; i++)
            {
                if (i == currentPos)
                    textCursorPos = cPos;
                if (i == selectionStart)
                    SelStartCursorPos = cPos;
                if (i == selectionEnd)
                    SelEndCursorPos = cPos;

                if (i < Text.Length)
                {
#if _WIN32 || _WIN64
                    byte[] c = System.Text.UTF8Encoding.UTF8.GetBytes(Text.Substring(i, 1));
                    te = gr.TextExtents(c);
#elif __linux__
					te = gr.TextExtents(Text.Substring(i,1));
#endif
                    cPos += te.XAdvance;
                }
            }

        }			
	} 
}