xref: /haiku/src/kits/interface/TextInput.cpp (revision efafab643ce980e3f3c916795ed302599f6b4f66)
1 /*
2  * Copyright 2001-2015, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Frans van Nispen (xlr8@tref.nl)
7  *		Marc Flerackers (mflerackers@androme.be)
8  */
9 
10 
11 #include "TextInput.h"
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include <ControlLook.h>
18 #include <InterfaceDefs.h>
19 #include <LayoutUtils.h>
20 #include <Message.h>
21 #include <String.h>
22 #include <TextControl.h>
23 #include <TextView.h>
24 #include <Window.h>
25 
26 
27 namespace BPrivate {
28 
29 
30 _BTextInput_::_BTextInput_(BRect frame, BRect textRect, uint32 resizeMask,
31 	uint32 flags)
32 	:
33 	BTextView(frame, "_input_", textRect, resizeMask, flags),
34 	fPreviousText(NULL)
35 {
36 	MakeResizable(true);
37 }
38 
39 
40 _BTextInput_::_BTextInput_(BMessage* archive)
41 	:
42 	BTextView(archive),
43 	fPreviousText(NULL)
44 {
45 	MakeResizable(true);
46 }
47 
48 
49 _BTextInput_::~_BTextInput_()
50 {
51 	free(fPreviousText);
52 }
53 
54 
55 BArchivable*
56 _BTextInput_::Instantiate(BMessage* archive)
57 {
58 	if (validate_instantiation(archive, "_BTextInput_"))
59 		return new _BTextInput_(archive);
60 
61 	return NULL;
62 }
63 
64 
65 status_t
66 _BTextInput_::Archive(BMessage* data, bool deep) const
67 {
68 	return BTextView::Archive(data, true);
69 }
70 
71 
72 void
73 _BTextInput_::MouseDown(BPoint where)
74 {
75 	if (!IsFocus()) {
76 		MakeFocus(true);
77 		return;
78 	}
79 
80 	// only pass through to base class if we already have focus
81 	BTextView::MouseDown(where);
82 }
83 
84 
85 void
86 _BTextInput_::FrameResized(float width, float height)
87 {
88 	BTextView::FrameResized(width, height);
89 
90 	AlignTextRect();
91 }
92 
93 
94 void
95 _BTextInput_::KeyDown(const char* bytes, int32 numBytes)
96 {
97 	switch (*bytes) {
98 		case B_ENTER:
99 		{
100 			if (!TextControl()->IsEnabled())
101 				break;
102 
103 			if (fPreviousText == NULL || strcmp(Text(), fPreviousText) != 0) {
104 				TextControl()->Invoke();
105 				free(fPreviousText);
106 				fPreviousText = strdup(Text());
107 			}
108 
109 			SelectAll();
110 			break;
111 		}
112 
113 		case B_TAB:
114 			BView::KeyDown(bytes, numBytes);
115 			break;
116 
117 		default:
118 			BTextView::KeyDown(bytes, numBytes);
119 			break;
120 	}
121 }
122 
123 
124 void
125 _BTextInput_::MakeFocus(bool state)
126 {
127 	if (state == IsFocus())
128 		return;
129 
130 	BTextView::MakeFocus(state);
131 
132 	if (state) {
133 		SetInitialText();
134 		SelectAll();
135 	} else {
136 		if (strcmp(Text(), fPreviousText) != 0)
137 			TextControl()->Invoke();
138 
139 		free(fPreviousText);
140 		fPreviousText = NULL;
141 	}
142 
143 	if (Window() != NULL) {
144 		// Invalidate parent to draw or remove the focus mark
145 		if (BTextControl* parent = dynamic_cast<BTextControl*>(Parent())) {
146 			BRect frame = Frame();
147 			frame.InsetBy(-1.0, -1.0);
148 			parent->Invalidate(frame);
149 		}
150 	}
151 }
152 
153 
154 BSize
155 _BTextInput_::MinSize()
156 {
157 	BSize min;
158 	min.height = ceilf(LineHeight(0) + 2.0);
159 	// we always add at least one pixel vertical inset top/bottom for
160 	// the text rect.
161 	min.width = min.height * 3;
162 	return BLayoutUtils::ComposeSize(ExplicitMinSize(), min);
163 }
164 
165 
166 void
167 _BTextInput_::AlignTextRect()
168 {
169 	// the label font could require the control to be higher than
170 	// necessary for the text view, we compensate this by layouting
171 	// the text rect to be in the middle, normally this means there
172 	// is one pixel spacing on each side
173 	BRect textRect(Bounds());
174 	float vInset = max_c(1,
175 			floorf((textRect.Height() - LineHeight(0)) / 2.0));
176 	float hInset = 2;
177 	float textFontWidth = TextRect().right;
178 
179 	switch (Alignment()) {
180 		case B_ALIGN_LEFT:
181 			hInset = be_control_look->DefaultLabelSpacing();
182 			break;
183 
184 		case B_ALIGN_RIGHT:
185 			hInset  = textRect.right - textFontWidth;
186 			hInset -= be_control_look->DefaultLabelSpacing();
187 			break;
188 
189 		case B_ALIGN_CENTER:
190 			hInset = (textRect.right - textFontWidth) / 2.0;
191 			break;
192 
193 		default:
194 			break;
195 	}
196 
197 	textRect.InsetBy(hInset, vInset);
198 	SetTextRect(textRect);
199 }
200 
201 
202 void
203 _BTextInput_::SetInitialText()
204 {
205 	free(fPreviousText);
206 	fPreviousText = NULL;
207 
208 	if (Text() != NULL)
209 		fPreviousText = strdup(Text());
210 }
211 
212 
213 void
214 _BTextInput_::Paste(BClipboard* clipboard)
215 {
216 	BTextView::Paste(clipboard);
217 	Invalidate();
218 }
219 
220 
221 void
222 _BTextInput_::InsertText(const char* inText, int32 inLength,
223 	int32 inOffset, const text_run_array* inRuns)
224 {
225 	// Filter all line breaks, note that inText is not terminated.
226 	if (inLength == 1) {
227 		if (*inText == '\n' || *inText == '\r')
228 			BTextView::InsertText(" ", 1, inOffset, inRuns);
229 		else
230 			BTextView::InsertText(inText, 1, inOffset, inRuns);
231 	} else {
232 		BString filteredText(inText, inLength);
233 		filteredText.ReplaceAll('\n', ' ');
234 		filteredText.ReplaceAll('\r', ' ');
235 		BTextView::InsertText(filteredText.String(), inLength, inOffset,
236 			inRuns);
237 	}
238 
239 	TextControl()->InvokeNotify(TextControl()->ModificationMessage(),
240 		B_CONTROL_MODIFIED);
241 }
242 
243 
244 void
245 _BTextInput_::DeleteText(int32 fromOffset, int32 toOffset)
246 {
247 	BTextView::DeleteText(fromOffset, toOffset);
248 
249 	TextControl()->InvokeNotify(TextControl()->ModificationMessage(),
250 		B_CONTROL_MODIFIED);
251 }
252 
253 
254 BTextControl*
255 _BTextInput_::TextControl()
256 {
257 	BTextControl* textControl = NULL;
258 	if (Parent() != NULL)
259 		textControl = dynamic_cast<BTextControl*>(Parent());
260 
261 	if (textControl == NULL)
262 		debugger("_BTextInput_ should have a BTextControl as parent");
263 
264 	return textControl;
265 }
266 
267 
268 }	// namespace BPrivate
269 
270