aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/GraphicObjects/Label.cs
blob: 94c13790a824ccff54fdbede3fa9a4aa30cbf470 (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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Cairo;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.ComponentModel;
using OpenTK.Input;

namespace go
{
    [Serializable]
    public class Label : GraphicObject
    {
		#region CTOR
		public Label()
		{ 

		}
		public Label(string _text)
			: base()
		{
			Text = _text;
		}
		#endregion

        //TODO:change protected to private
        
		#region private and protected fields
		protected string _text = "label";
        Alignment _textAlignment = Alignment.LeftCenter;        
		Font _font;
		bool _multiline = false;
		Color selColor;
		Color selFontColor;
		bool _selectable;
		Point mouseLocalPos;    //mouse coord in widget space, filled only when clicked        
		int _currentCol;        //0 based cursor position in string
		int _currentLine;
		double textCursorPos;   //cursor position in cairo units in widget client coord.
		double SelStartCursorPos = -1;
		double SelEndCursorPos = -1;
		bool SelectionInProgress = false;

        protected Rectangle rText;
		protected float widthRatio = 1f;
		protected float heightRatio = 1f;
		protected FontExtents fe;
		protected TextExtents te;
		#endregion

		[XmlAttributeAttribute()][DefaultValue("SkyBlue")]
		public virtual Color SelectionBackground {
			get { return selColor; }
			set {
				selColor = value;
				registerForGraphicUpdate ();
			}
		}
		[XmlAttributeAttribute()][DefaultValue("Black")]
		public virtual Color SelectionForeground {
			get { return selFontColor; }
			set {
				selFontColor = value;
				registerForGraphicUpdate ();
			}
		}
		[XmlAttributeAttribute()][DefaultValue(false)]
		public virtual bool Selectable {
			get { return _selectable; }
			set { _selectable = value; }
		}

        [XmlAttributeAttribute()][DefaultValue(Alignment.LeftCenter)]
		public Alignment TextAlignment
        {
            get { return _textAlignment; }
            set { _textAlignment = value; }
        }
		[XmlAttributeAttribute()][DefaultValue("label")]
        public string Text
        {
            get {				
				return lines == null ? 
					_text : lines.Aggregate((i, j) => i + Interface.LineBreak + j);
			}
            set
            {
                if (_text == value)
                    return;
					                
                registerForGraphicUpdate();
				this.RegisterForLayouting ((int)LayoutingType.Sizing);


                _text = value;

				if (string.IsNullOrEmpty(_text))
					_text = "";

				lines = getLines;
            }
        }
		[XmlAttributeAttribute()][DefaultValue("droid,10")]
		public Font Font {
			get { return _font; }
			set { _font = value; }
		}
		[XmlAttributeAttribute()][DefaultValue(false)]
		public bool Multiline
		{
			get { return _multiline; }
			set
			{
				_multiline = value;
				registerForGraphicUpdate();
			}
		}
		[XmlIgnore]public int currentCol{
			get { return _currentCol; }
			set { 
				if (value < 0)
					_currentCol = 0;
				else if (value > lines [_currentLine].Count ())
					_currentCol = lines [_currentLine].Count ();
				else
					_currentCol = value; 
			}
		}
		[XmlIgnore]public int currentLine{
			get { return _currentLine; }
			set { 
				if (value > lines.Count)
					_currentLine = lines.Count; 
				else if (value < 0)
					_currentLine = 0;
				else
					_currentLine = value; 
			}
		}
		[XmlIgnore]protected Point selBegin;
		[XmlIgnore]protected Point selRelease;
		[XmlIgnore]protected Point selectionStart   //ordered selection start and end positions
		{
			get { 
				return selRelease < 0 || selBegin.Y < selRelease.Y ? selBegin : 
					selBegin.Y > selRelease.Y ? selRelease :
					selBegin.X < selRelease.X ? selBegin : selRelease;
			}
		}
		[XmlIgnore]public Point selectionEnd
		{  
			get { 
				return selRelease < 0 || selBegin.Y > selRelease.Y ? selBegin : 
					selBegin.Y < selRelease.Y ? selRelease :
					selBegin.X > selRelease.X ? selBegin : selRelease;
			}		
		}
		[XmlIgnore]public string selectedText
		{ 
			get { 
				return null; 
					//selectionEnd < 0 ? null : 
					//Text.Substring(selectionStart, selectionEnd - selectionStart); 
			}			
		}
		[XmlIgnore]public bool selectionIsEmpty
		{ get { return selRelease < 0; } }

		List<string> lines;
		List<string> getLines {
			get {				
				return _multiline ?
					Regex.Split (_text, "\r\n|\r|\n").ToList() :
					new List<string>(new string[] { _text });
			}
		}

		public void DeleteChar()
		{
			if (selectionIsEmpty) {				
				if (currentCol == 0) {
					if (currentLine == 0)
						return;
					currentLine--;
					currentCol = lines [currentLine].Count ();
					lines [currentLine] += lines [currentLine + 1];
					lines.RemoveAt (currentLine + 1);
					return;
				}
				currentCol--;
				lines [currentLine] = lines [currentLine].Remove (currentCol, 1);
			} else {
				Debug.WriteLine (selectionEnd.ToString());
				int linesToRemove = selectionEnd.Y - selectionStart.Y;
				int l = selectionStart.Y;

				if (linesToRemove > 0) {
					lines [l] = lines [l].Remove (selectionStart.X, lines [l].Length - selectionStart.X) +
						lines [selectionEnd.Y].Substring (selectionEnd.X, lines [selectionEnd.Y].Length - selectionEnd.X);
					l++;
					for (int c = 0; c < linesToRemove-1; c++)
						lines.RemoveAt (l);
					currentCol = selectionStart.X;
					currentLine = selectionStart.Y;
				} else 
					lines [l] = lines [l].Remove (selectionStart.X, selectionEnd.X - selectionStart.X);
				currentCol = selectionStart.X;
				selBegin = -1;
				selRelease = -1;
			}
		}
		/// <summary>
		/// Insert new string at caret position, should be sure no line break is inside.
		/// </summary>
		/// <param name="str">String.</param>
		protected void Insert(string str)
		{
			lines [currentLine] = lines [currentLine].Insert (currentCol, str);
			currentCol += str.Length;
		}

		#region GraphicObject overrides
		[XmlAttributeAttribute()][DefaultValue(-1)]
		public override int Width {
			get { return base.Width; }
			set { base.Width = value; }
		}
		[XmlAttributeAttribute()][DefaultValue(-1)]
		public override int Height {
			get { return base.Height; }
			set { base.Height = value; }
		}

		protected override Size measureRawSize()
        {
			Size size;

			if (lines == null)
				lines = getLines;
				
			using (ImageSurface img = new ImageSurface (Format.Argb32, 10, 10)) {
				using (Context gr = new Context (img)) {
					//Cairo.FontFace cf = gr.GetContextFontFace ();

					gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
					gr.SetFontSize (Font.Size);

					te = new TextExtents();

					foreach (string s in lines) {
						string l = s.Replace("\t", new String (' ', Interface.TabSize));

#if _WIN32 || _WIN64
						TextExtents tmp = gr.TextExtents(str.ToUtf8());
#elif __linux__
						TextExtents tmp = gr.TextExtents (l);
#endif
						if (tmp.XAdvance > te.XAdvance)
							te = tmp;
					}
					fe = gr.FontExtents;
					int lc = lines.Count;
					//ensure minimal height = text line height
					if (lc == 0)
						lc = 1; 
					size = new Size ((int)Math.Ceiling (te.XAdvance) + Margin * 2, (int)(fe.Height * lc) + Margin*2);
				}
			}

            return size;;
        }
		protected override void onDraw (Context gr)
		{
			base.onDraw (gr);

			gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
			gr.SetFontSize (Font.Size);

			gr.Antialias = Antialias.Subpixel;
			//gr.FontOptions.Antialias = Antialias.Subpixel;
			//gr.FontOptions.HintMetrics = HintMetrics.On;

			rText = new Rectangle(measureRawSize());
			rText.Width -= 2 * Margin;
			rText.Height -= 2 * Margin;

			widthRatio = 1f;
			heightRatio = 1f;

			Rectangle cb = ClientRectangle;

			//ignore text alignment if size to content = true
			if (Bounds.Size < 0)
			{
				rText.X = cb.X;
				rText.Y = cb.Y;
			}else{
				switch (TextAlignment)
				{
				case Alignment.None:
					break;
				case Alignment.TopLeft:     //ok
					rText.X = cb.X;
					rText.Y = cb.Y;
					break;
				case Alignment.TopCenter:   //ok
					rText.Y = cb.Y;
					rText.X = cb.X + cb.Width / 2 - rText.Width / 2;
					break;
				case Alignment.TopRight:    //ok
					rText.X = cb.Right - rText.Width;
					rText.Y = cb.Y;
					break;
				case Alignment.TopStretch://ok
					heightRatio = widthRatio = (float)cb.Width / rText.Width;
					rText.X = cb.X;
					rText.Y = cb.Y;
					rText.Width = cb.Width;
					rText.Height = (int)(rText.Height * heightRatio);
					break;
				case Alignment.LeftCenter://ok
					rText.X = cb.X;
					rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
					break;
				case Alignment.LeftStretch://ok
					heightRatio = widthRatio = (float)cb.Height / rText.Height;
					rText.X = cb.X;
					rText.Y = cb.Y;
					rText.Height = cb.Height;
					rText.Width = (int)(widthRatio * cb.Width);
					break;
				case Alignment.RightCenter://ok
					rText.X = cb.X + cb.Width - rText.Width;
					rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
					break;
				case Alignment.RightStretch://ok
					heightRatio = widthRatio = (float)cb.Height / rText.Height;
					rText.Height = cb.Height;
					rText.Width = (int)(widthRatio * cb.Width);
					rText.X = cb.X;
					rText.Y = cb.Y;
					break;
				case Alignment.BottomCenter://ok
					rText.X = cb.Width / 2 - rText.Width / 2;
					rText.Y = cb.Height - rText.Height;
					break;
				case Alignment.BottomStretch://ok
					heightRatio = widthRatio = (float)cb.Width / rText.Width;
					rText.Width = cb.Width;
					rText.Height = (int)(rText.Height * heightRatio);
					rText.Y = cb.Bottom - rText.Height;
					rText.X = cb.X;
					break;
				case Alignment.BottomLeft://ok
					rText.X = cb.X;
					rText.Y = cb.Bottom - rText.Height;
					break;
				case Alignment.BottomRight://ok
					rText.Y = cb.Bottom - rText.Height;
					rText.X = cb.Right - rText.Width;
					break;
				case Alignment.Center://ok
					rText.X = cb.X + cb.Width / 2 - rText.Width / 2;
					rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
					break;
				case Alignment.Fit://ok, peut être mieu aligné                            
					widthRatio = (float)cb.Width / rText.Width;
					heightRatio = (float)cb.Height / rText.Height;
					rText = cb;
					break;
				case Alignment.HorizontalStretch://ok
					heightRatio = widthRatio = (float)cb.Width / rText.Width;
					rText.Width = cb.Width;
					rText.Height = (int)(heightRatio * rText.Height);
					rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
					rText.X = cb.X;
					break;
				case Alignment.VerticalStretch://ok
					heightRatio = widthRatio = (float)cb.Height / rText.Height;
					rText.Height = cb.Height;
					rText.Width = (int)(widthRatio * rText.Width);
					rText.X = cb.X + cb.Width / 2 - rText.Width / 2;
					rText.Y = cb.Y;
					break;
				default:
					break;
				}
			}

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

				if (SelectionInProgress)
				{
					selRelease = new Point(currentCol, currentLine);
					SelEndCursorPos = textCursorPos;
				}
				else if (selBegin < 0)
				{
					selBegin = new Point(currentCol, currentLine);
					SelStartCursorPos = textCursorPos;
					selRelease = -1;
				}
				else
					computeTextCursorPosition(gr);
			}
			else
				computeTextCursorPosition(gr);

			if (HasFocus && Selectable)
			{
				//TODO:
				gr.Color = Foreground;
				gr.LineWidth = 1.5;
				gr.MoveTo(new PointD(textCursorPos + rText.X, rText.Y + currentLine * fe.Height));
				gr.LineTo(new PointD(textCursorPos + rText.X, rText.Y + (currentLine + 1) * fe.Height));
				gr.Stroke();
			}
			#endregion

			//*******************
			//debug selection
//			gr.Color = Color.Green;
//			Rectangle R = new Rectangle(
//				rText.X +  (int)SelEndCursorPos-10,
//				rText.Y + (int)(selRelease.Y * fe.Height), 
//				20, 
//				(int)fe.Height);
//			gr.Rectangle(R);
//			gr.Fill();
//			gr.Color = Color.UnmellowYellow;
//			R = new Rectangle(
//				rText.X + (int)SelStartCursorPos-10,
//				rText.Y + (int)(selBegin.Y * fe.Height), 
//				20, 
//				(int)fe.Height);
//			gr.Rectangle(R);
//			gr.Fill();
			//*******************

			gr.FontMatrix = new Matrix(widthRatio * Font.Size, 0, 0, heightRatio * Font.Size, 0, 0);

			for (int i = 0; i < lines.Count; i++) {				
				string l = lines [i].Replace ("\t", new String (' ', Interface.TabSize));
				if (selRelease >= 0 && i >= selectionStart.Y && i <= selectionEnd.Y) {					
					gr.Color = selColor;
					int lineLength = (int)gr.TextExtents (l).XAdvance;
					Rectangle selRect = new Rectangle (
						rText.X,
						rText.Y + (int)(i * fe.Height), 
						lineLength, 
						(int)fe.Height);

					int cpStart = (int)SelStartCursorPos,
						cpEnd = (int)SelEndCursorPos;

					if (selBegin.Y > selRelease.Y) {
						cpStart = cpEnd;
						cpEnd = (int)SelStartCursorPos;
					}

					if (i == selectionStart.Y) {
						selRect.Width -= cpStart;
						selRect.Left += cpStart;
					}
					if (i == selectionEnd.Y)				
						selRect.Width -= (lineLength - cpEnd);					

					gr.Rectangle (selRect);
					gr.Fill ();
				} 

				if (string.IsNullOrWhiteSpace (l))
					continue;
			
//				double t = rText.Y;
//				gr.LineWidth = 1;
//				gr.Color = Color.Green;
//				gr.MoveTo (rText.X, t);
//				gr.LineTo (rText.X + rText.Width, t);
//				gr.Stroke ();
//				gr.MoveTo (rText.X, t + rText.Height);
//				gr.LineTo (rText.X + rText.Width, t + rText.Height);
//				gr.Stroke ();
//				gr.Rectangle (rText);
//				gr.Fill ();
//
//				t += fe.Ascent;
//				gr.Color = Color.Red;
//				gr.MoveTo (rText.X, t);
//				gr.LineTo (rText.X + rText.Width, t);
//				gr.Stroke ();

				gr.Color = Foreground;				
				gr.MoveTo (rText.X, rText.Y + fe.Ascent + fe.Height * i);

				#if _WIN32 || _WIN64
				gr.ShowText(l.ToUtf8());
				#elif __linux__
				gr.ShowText (l);
				#endif
				gr.Fill ();

			}						
		}
		#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);

			IGOLibHost glh = TopContainer;
			if (glh.activeWidget != this)
				return;
			if (!Selectable)
				return;
			
			SelectionInProgress = true;
			mouseLocalPos = e.Position - ScreenCoordinates(ClientRectangle).TopLeft;
			registerForGraphicUpdate();
		}
		public override void onMouseButtonDown (object sender, MouseButtonEventArgs e)
		{
			if (this.HasFocus && Selectable){
				mouseLocalPos = e.Position - ScreenCoordinates(ClientRectangle).TopLeft;
				selBegin = -1;
				selRelease = -1;
			}else{
//				selBeginPos = 0;
//				selReleasePos = new Point(lines[lines.Count].Length-1, lines.Count-1);
			}            

			//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;

			currentLine = (int)(mouseLocalPos.Y / fe.Height);

			//fix cu
			if (currentLine >= lines.Count)
				currentLine = lines.Count - 1;

			for (int i = 0; i < lines[currentLine].Length; i++)
			{
				string c = lines [currentLine].Substring (i, 1);
				if (c == "\t")
					c = new string (' ', Interface.TabSize);
				
				#if _WIN32 || _WIN64
				byte[] c = System.Text.UTF8Encoding.UTF8.GetBytes(Text.Substring(i, 1));
				te = gr.TextExtents(c);
				#elif __linux__
				te = gr.TextExtents(c);
				#endif
				double halfWidth = te.XAdvance / 2;

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

				cPos += te.XAdvance;
			}
			currentCol = lines[currentLine].Length;
			textCursorPos = cPos;

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

			double cPos = 0f;

			int limit = currentCol;

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

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

				if (i < lines[currentLine].Length)
				{
					string c = lines [currentLine].Substring (i, 1);
					if (c == "\t")
						c = new string (' ', Interface.TabSize);
					#if _WIN32 || _WIN64
					byte[] c = System.Text.UTF8Encoding.UTF8.GetBytes(Text.Substring(i, 1));
					te = gr.TextExtents(c);
					#elif __linux__
					te = gr.TextExtents(c);
					#endif
					cPos += te.XAdvance;
				}
			}

		}		
    }
}