1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dgui.core.controls.textcontrol;
10 
11 public import dgui.core.controls.subclassedcontrol;
12 
13 abstract class TextControl: SubclassedControl
14 {
15 	public Event!(Control, EventArgs) textChanged;
16 
17 	public void appendText(string s)
18 	{
19 		if(this.created)
20 		{
21 			this.sendMessage(EM_REPLACESEL, true, cast(LPARAM)toUTFz!(wchar*)(s));
22 		}
23 		else
24 		{
25 			this._text ~= s;
26 		}
27 	}
28 
29 	@property public final bool readOnly()
30 	{
31 		return cast(bool)(this.getStyle() & ES_READONLY);
32 	}
33 
34 	@property public final void readOnly(bool b)
35 	{
36 		this.setStyle(ES_READONLY, b);
37 	}
38 
39 	public void undo()
40 	{
41 		this.sendMessage(EM_UNDO, 0, 0);
42 	}
43 
44 	public void cut()
45 	{
46 		this.sendMessage(WM_CUT, 0, 0);
47 	}
48 
49 	public void copy()
50 	{
51 		this.sendMessage(WM_COPY, 0, 0);
52 	}
53 
54 	public void paste()
55 	{
56 		this.sendMessage(WM_PASTE, 0, 0);
57 	}
58 
59 	public void selectAll()
60 	{
61 		this.sendMessage(EM_SETSEL, 0, -1);
62 	}
63 
64 	public void clear()
65 	{
66 		this.sendMessage(WM_CLEAR, 0, 0);
67 	}
68 
69 	@property public bool modified()
70 	{
71 		if(this.created)
72 		{
73 			return cast(bool)this.sendMessage(EM_GETMODIFY, 0, 0);
74 		}
75 
76 		return false;
77 	}
78 
79 	@property public void modified(bool b)
80 	{
81 		this.sendMessage(EM_SETMODIFY, b, 0);
82 	}
83 
84 	@property public int textLength()
85 	{
86 		if(this.created)
87 		{
88 			return getWindowTextLength(this._handle);
89 		}
90 
91 		return this._text.length;
92 	}
93 
94 	@property public final string selectedText()
95 	{
96 		CHARRANGE chrg = void; //Inizializzata sotto
97 
98 		this.sendMessage(EM_EXGETSEL, 0, cast(LPARAM)&chrg);
99 		return this.text[chrg.cpMin..chrg.cpMax];
100 	}
101 
102 	@property public final int selectionStart()
103 	{
104 		CHARRANGE chrg = void; //Inizializzata sotto
105 
106 		this.sendMessage(EM_EXGETSEL, 0, cast(LPARAM)&chrg);
107 		return chrg.cpMin;
108 	}
109 
110 	@property public final int selectionLength()
111 	{
112 		CHARRANGE chrg = void; //Inizializzata sotto
113 
114 		this.sendMessage(EM_EXGETSEL, 0, cast(LPARAM)&chrg);
115 		return chrg.cpMax - chrg.cpMin;
116 	}
117 
118 	protected override void createControlParams(ref CreateControlParams ccp)
119 	{
120 		this.setStyle(WS_TABSTOP, true);
121 		ccp.defaultBackColor = SystemColors.colorWindow;
122 
123 		super.createControlParams(ccp);
124 	}
125 
126 	protected override void onReflectedMessage(ref Message m)
127 	{
128 		if(m.msg == WM_COMMAND && HIWORD(m.wParam) == EN_CHANGE && TextControl.hasBit(this._cBits, ControlBits.canNotify))
129 		{
130 			this.onTextChanged(EventArgs.empty);
131 		}
132 
133 		super.onReflectedMessage(m);
134 	}
135 
136 	protected override void onHandleCreated(EventArgs e)
137 	{
138 		this.modified = false; // Force to 'False'
139 
140 		super.onHandleCreated(e);
141 	}
142 
143 	protected void onTextChanged(EventArgs e)
144 	{
145 		this.textChanged(this, e);
146 	}
147 }