aboutsummaryrefslogtreecommitdiff
path: root/source/ShiftUI/ToolStrip/ToolStripTextBox.cs
blob: eeabbbae23869617f0102af927127dd7d2fd6f9b (plain) (blame)
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
//
// ToolStripTextBox.cs
//
// 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.
//
// Copyright (c) 2006 Jonathan Pobst
//
// Authors:
//	Jonathan Pobst ([email protected])
//

using System.Drawing;
using System.ComponentModel;
using ShiftUI.Design;
using System.Runtime.InteropServices;
using System;

namespace ShiftUI
{
	[ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
	public class ToolStripTextBox : ToolStripWidgetHost
	{
		private BorderStyle border_style;

		#region Public Constructors
		public ToolStripTextBox () : base (new ToolStripTextBoxWidget ())
		{
			ToolStripTextBoxWidget text_box = TextBox as ToolStripTextBoxWidget;
			text_box.OwnerItem = this;
			text_box.border_style = BorderStyle.None;
			text_box.TopMargin = 3; // need to explicitly set the margin
			text_box.Border = BorderStyle.Fixed3D; // ToolStripTextBoxWidget impl, not TextBox
			this.border_style = BorderStyle.Fixed3D;
		}

		//[EditorBrowsable (EditorBrowsableState.Never)]
		public ToolStripTextBox (Widget c) : base (c)
		{
			throw new NotSupportedException ("This construtor cannot be used.");
		}

		public ToolStripTextBox (string name) : this ()
		{
			base.Name = name;
		}
		#endregion

		#region Public Properties
		[DefaultValue (false)]
		public bool AcceptsReturn {
			get { return this.TextBox.AcceptsReturn; }
			set { this.TextBox.AcceptsReturn = value; }
		}

		[DefaultValue (false)]
		public bool AcceptsTab {
			get { return this.TextBox.AcceptsTab; }
			set { this.TextBox.AcceptsTab = value; }
		}

		[MonoTODO ("AutoCompletion algorithm is currently not implemented.")]
		[Browsable (true)]
		[Localizable (true)]
		//[EditorBrowsable (EditorBrowsableState.Always)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
		//[Editor ("ShiftUI.Design.ListWidgetStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
		public AutoCompleteStringCollection AutoCompleteCustomSource {
			get { return this.TextBox.AutoCompleteCustomSource; }
			set { this.TextBox.AutoCompleteCustomSource = value; }
		}
		
		[MonoTODO("AutoCompletion algorithm is currently not implemented.")]
		[Browsable (true)]
		[DefaultValue (AutoCompleteMode.None)]
		//[EditorBrowsable (EditorBrowsableState.Always)]
		public AutoCompleteMode AutoCompleteMode {
			get { return this.TextBox.AutoCompleteMode; }
			set { this.TextBox.AutoCompleteMode = value; }
		}

		[MonoTODO("AutoCompletion algorithm is currently not implemented.")]
		[Browsable (true)]
		[DefaultValue (AutoCompleteSource.None)]
		//[EditorBrowsable (EditorBrowsableState.Always)]
		public AutoCompleteSource AutoCompleteSource {
			get { return this.TextBox.AutoCompleteSource; }
			set { this.TextBox.AutoCompleteSource = value; }
		}

		[Browsable (false)]
		//[EditorBrowsable (EditorBrowsableState.Never)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public override Image BackgroundImage {
			get { return base.BackgroundImage; }
			set { base.BackgroundImage = value; }
		}

		[Browsable (false)]
		//[EditorBrowsable (EditorBrowsableState.Never)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public override ImageLayout BackgroundImageLayout {
			get { return base.BackgroundImageLayout; }
			set { base.BackgroundImageLayout = value; }
		}

		[DefaultValue (BorderStyle.Fixed3D)]
		[DispId (-504)]
		public BorderStyle BorderStyle {
			get { return this.border_style; }
			set { 
				if (this.border_style != value) {
					this.border_style = value;
					(base.Widget as ToolStripTextBoxWidget).Border = value;
					this.OnBorderStyleChanged (EventArgs.Empty);
				}
			}
		}

		[Browsable (false)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public bool CanUndo {
			get { return this.TextBox.CanUndo; }
		}

		[DefaultValue (CharacterCasing.Normal)]
		public CharacterCasing CharacterCasing {
			get { return this.TextBox.CharacterCasing; }
			set { this.TextBox.CharacterCasing = value; }
		}

		[DefaultValue (true)]
		public bool HideSelection {
			get { return this.TextBox.HideSelection; }
			set { this.TextBox.HideSelection = value; }
		}

		[Localizable (true)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		//[Editor ("ShiftUI.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
		public string[] Lines {
			get { return this.TextBox.Lines; }
			set { this.TextBox.Lines = value; }
		}

		[Localizable (true)]
		[DefaultValue (32767)]
		public int MaxLength {
			get { return this.TextBox.MaxLength; }
			set { this.TextBox.MaxLength = value; }
		}

		[Browsable (false)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public bool Modified {
			get { return this.TextBox.Modified; }
			set { this.TextBox.Modified = value; }
		}

		[Localizable (true)]
		[Browsable (false)]
		//[EditorBrowsable (EditorBrowsableState.Never)]
		[DefaultValue (false)]
		[RefreshProperties (RefreshProperties.All)]
		public bool Multiline {
			get { return this.TextBox.Multiline; }
			set { this.TextBox.Multiline = value; }
		}

		[DefaultValue (false)]
		public bool ReadOnly {
			get { return this.TextBox.ReadOnly; }
			set { this.TextBox.ReadOnly = value; }
		}

		[Browsable (false)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public string SelectedText {
			get { return this.TextBox.SelectedText; }
			set { this.TextBox.SelectedText = value; }
		}

		[Browsable (false)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public int SelectionLength {
			get { return this.TextBox.SelectionLength == -1 ? 0 : this.TextBox.SelectionLength; }
			set { this.TextBox.SelectionLength = value; }
		}

		[Browsable (false)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public int SelectionStart {
			get { return this.TextBox.SelectionStart; }
			set { this.TextBox.SelectionStart = value; }
		}

		[DefaultValue (true)]
		public bool ShortcutsEnabled {
			get { return this.TextBox.ShortcutsEnabled; }
			set { this.TextBox.ShortcutsEnabled = value; }
		}
		
		[Browsable (false)]
		//[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public TextBox TextBox {
			get { return (TextBox)base.Widget; }
		}

		[Localizable (true)]
		[DefaultValue (HorizontalAlignment.Left)]
		public HorizontalAlignment TextBoxTextAlign {
			get { return this.TextBox.TextAlign; }
			set { this.TextBox.TextAlign = value; }
		}

		[Browsable (false)]
		public int TextLength {
			get { return this.TextBox.TextLength; }
		}

		[Localizable (true)]
		[Browsable (false)]
		//[EditorBrowsable (EditorBrowsableState.Never)]
		[DefaultValue (true)]
		public bool WordWrap {
			get { return this.TextBox.WordWrap; }
			set { this.TextBox.WordWrap = value; }
		}
		#endregion

		#region Protected Properties
		protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
		protected override Size DefaultSize { get { return new Size (100, 22); } }
		#endregion

		#region Public Methods
		public void AppendText (string text)
		{
			this.TextBox.AppendText (text);
		}

		public void Clear ()
		{
			this.TextBox.Clear ();
		}

		public void ClearUndo ()
		{
			this.TextBox.ClearUndo ();
		}

		public void Copy ()
		{
			this.TextBox.Copy ();
		}

		public void Cut ()
		{
			this.TextBox.Cut ();
		}

		public void DeselectAll ()
		{
			this.TextBox.DeselectAll ();
		}
		
		public char GetCharFromPosition (Point pt)
		{
			return this.TextBox.GetCharFromPosition (pt);
		}
		
		public int GetCharIndexFromPosition (Point pt)
		{
			return this.TextBox.GetCharIndexFromPosition (pt);
		}
		
		public int GetFirstCharIndexFromLine (int lineNumber)
		{
			return this.TextBox.GetFirstCharIndexFromLine (lineNumber);
		}
		
		public int GetFirstCharIndexOfCurrentLine ()
		{
			return this.TextBox.GetFirstCharIndexOfCurrentLine ();
		}
		
		public int GetLineFromCharIndex (int index)
		{
			return this.TextBox.GetLineFromCharIndex (index);
		}
		
		public Point GetPositionFromCharIndex (int index)
		{
			return this.TextBox.GetPositionFromCharIndex (index);
		}
		
		public override Size GetPreferredSize (Size constrainingSize)
		{
			return base.GetPreferredSize (constrainingSize);
		}

		public void Paste ()
		{
			this.TextBox.Paste ();
		}

		public void ScrollToCaret ()
		{
			this.TextBox.ScrollToCaret ();
		}

		public void Select (int start, int length)
		{
			this.TextBox.Select (start, length);
		}

		public void SelectAll ()
		{
			this.TextBox.SelectAll ();
		}

		public void Undo ()
		{
			this.TextBox.Undo ();
		}
		#endregion

		#region Protected Methods
		protected virtual void OnAcceptsTabChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)Events [AcceptsTabChangedEvent];
			if (eh != null)
				eh (this, e);
		}

		protected virtual void OnBorderStyleChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)Events [BorderStyleChangedEvent];
			if (eh != null)
				eh (this, e);
		}

		protected virtual void OnHideSelectionChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)Events [HideSelectionChangedEvent];
			if (eh != null)
				eh (this, e);
		}

		protected virtual void OnModifiedChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)Events [ModifiedChangedEvent];
			if (eh != null)
				eh (this, e);
		}

		protected virtual void OnMultilineChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)Events [MultilineChangedEvent];
			if (eh != null)
				eh (this, e);
		}

		protected virtual void OnReadOnlyChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)Events [ReadOnlyChangedEvent];
			if (eh != null)
				eh (this, e);
		}

		protected override void OnSubscribeWidgetEvents (Widget Widget)
		{
			base.OnSubscribeWidgetEvents (Widget);

			this.TextBox.AcceptsTabChanged += new EventHandler (HandleAcceptsTabChanged);
			this.TextBox.HideSelectionChanged += new EventHandler (HandleHideSelectionChanged);
			this.TextBox.ModifiedChanged += new EventHandler (HandleModifiedChanged);
			this.TextBox.MultilineChanged += new EventHandler (HandleMultilineChanged);
			this.TextBox.ReadOnlyChanged += new EventHandler (HandleReadOnlyChanged);
			this.TextBox.TextAlignChanged += new EventHandler (HandleTextAlignChanged);
			this.TextBox.TextChanged += new EventHandler (HandleTextChanged);
		}

		protected override void OnUnsubscribeWidgetEvents (Widget Widget)
		{
			base.OnUnsubscribeWidgetEvents (Widget);
		}
		#endregion

		#region Public Events
		static object AcceptsTabChangedEvent = new object ();
		static object BorderStyleChangedEvent = new object ();
		static object HideSelectionChangedEvent = new object ();
		static object ModifiedChangedEvent = new object ();
		static object MultilineChangedEvent = new object ();
		static object ReadOnlyChangedEvent = new object ();
		static object TextBoxTextAlignChangedEvent = new object ();

		public event EventHandler AcceptsTabChanged {
			add { Events.AddHandler (AcceptsTabChangedEvent, value); }
			remove {Events.RemoveHandler (AcceptsTabChangedEvent, value); }
		}
		public event EventHandler BorderStyleChanged {
			add { Events.AddHandler (BorderStyleChangedEvent, value); }
			remove {Events.RemoveHandler (BorderStyleChangedEvent, value); }
		}
		public event EventHandler HideSelectionChanged {
			add { Events.AddHandler (HideSelectionChangedEvent, value); }
			remove {Events.RemoveHandler (HideSelectionChangedEvent, value); }
		}
		public event EventHandler ModifiedChanged {
			add { Events.AddHandler (ModifiedChangedEvent, value); }
			remove {Events.RemoveHandler (ModifiedChangedEvent, value); }
		}
		[Browsable (false)]
		//[EditorBrowsable (EditorBrowsableState.Never)]
		public event EventHandler MultilineChanged {
			add { Events.AddHandler (MultilineChangedEvent, value); }
			remove {Events.RemoveHandler (MultilineChangedEvent, value); }
		}
		public event EventHandler ReadOnlyChanged {
			add { Events.AddHandler (ReadOnlyChangedEvent, value); }
			remove {Events.RemoveHandler (ReadOnlyChangedEvent, value); }
		}
		public event EventHandler TextBoxTextAlignChanged {
			add { Events.AddHandler (TextBoxTextAlignChangedEvent, value); }
			remove {Events.RemoveHandler (TextBoxTextAlignChangedEvent, value); }
		}
		#endregion

		#region Private Methods
		private void HandleTextAlignChanged (object sender, EventArgs e)
		{
			EventHandler eh = (EventHandler)(Events [TextBoxTextAlignChangedEvent]);
			if (eh != null)
				eh (this, e);
		}

		private void HandleReadOnlyChanged (object sender, EventArgs e)
		{
			OnReadOnlyChanged (e);
		}

		private void HandleMultilineChanged (object sender, EventArgs e)
		{
			OnMultilineChanged (e);
		}

		private void HandleModifiedChanged (object sender, EventArgs e)
		{
			OnModifiedChanged (e);
		}

		private void HandleHideSelectionChanged (object sender, EventArgs e)
		{
			OnHideSelectionChanged (e);
		}

		private void HandleAcceptsTabChanged (object sender, EventArgs e)
		{
			OnAcceptsTabChanged (e);
		}

		private void HandleTextChanged (object sender, EventArgs e)
		{
			OnTextChanged (e);
		}
		#endregion

		private class ToolStripTextBoxWidget : TextBox
		{
			private BorderStyle border;
			private Timer tooltip_timer;
			private ToolTip tooltip_window;
			private ToolStripItem owner_item;
			
			public ToolStripTextBoxWidget () : base ()
			{
			}

			protected override void OnLostFocus (EventArgs e)
			{
				base.OnLostFocus (e);
				Invalidate ();
			}
			
			protected override void OnMouseEnter (EventArgs e)
			{
				base.OnMouseEnter (e);
				Invalidate ();

				if (ShowToolTips)
					ToolTipTimer.Start ();

			}

			protected override void OnMouseLeave (EventArgs e)
			{
				base.OnMouseLeave (e);
				Invalidate ();

				ToolTipTimer.Stop ();
				ToolTipWindow.Hide (this);
			}

			internal override void OnPaintInternal (PaintEventArgs e)
			{
				base.OnPaintInternal (e);

				if ((this.Focused || this.Entered || border == BorderStyle.FixedSingle) && border != BorderStyle.None) {
					ToolStripRenderer tsr = (this.Parent as ToolStrip).Renderer;

					if (tsr is ToolStripProfessionalRenderer)
						using (Pen p = new Pen ((tsr as ToolStripProfessionalRenderer).ColorTable.ButtonSelectedBorder))
							e.Graphics.DrawRectangle (p, new Rectangle (0, 0, this.Width - 1, this.Height - 1));
				}
			}
			
			internal BorderStyle Border {
				set {
					border = value;
					Invalidate ();
				}
			}

			internal ToolStripItem OwnerItem {
				set { owner_item = value; }
			}
					
			#region Stuff for ToolTips
			private bool ShowToolTips {
				get {
					if (Parent == null)
						return false;
						
					return (Parent as ToolStrip).ShowItemToolTips;
				}
			}

			private Timer ToolTipTimer {
				get {
					if (tooltip_timer == null) {
						tooltip_timer = new Timer ();
						tooltip_timer.Enabled = false;
						tooltip_timer.Interval = 500;
						tooltip_timer.Tick += new EventHandler (ToolTipTimer_Tick);
					}

					return tooltip_timer;
				}
			}

			private ToolTip ToolTipWindow {
				get {
					if (tooltip_window == null)
						tooltip_window = new ToolTip ();

					return tooltip_window;
				}
			}

			private void ToolTipTimer_Tick (object o, EventArgs args)
			{
				string tooltip = owner_item.GetToolTip ();

				if (!string.IsNullOrEmpty (tooltip))
					ToolTipWindow.Present (this, tooltip);

				ToolTipTimer.Stop ();
			}
			#endregion
		}
	}
}