2 * Copyright 2001-2015, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Frans van Nispen (xlr8@tref.nl)
7 * Marc Flerackers (mflerackers@androme.be)
11 #include "TextInput.h"
17 #include <ControlLook.h>
18 #include <InterfaceDefs.h>
19 #include <LayoutUtils.h>
22 #include <TextControl.h>
30 _BTextInput_::_BTextInput_(BRect frame
, BRect textRect
, uint32 resizeMask
,
33 BTextView(frame
, "_input_", textRect
, resizeMask
, flags
),
40 _BTextInput_::_BTextInput_(BMessage
* archive
)
49 _BTextInput_::~_BTextInput_()
56 _BTextInput_::Instantiate(BMessage
* archive
)
58 if (validate_instantiation(archive
, "_BTextInput_"))
59 return new _BTextInput_(archive
);
66 _BTextInput_::Archive(BMessage
* data
, bool deep
) const
68 return BTextView::Archive(data
, true);
73 _BTextInput_::MouseDown(BPoint where
)
80 // only pass through to base class if we already have focus
81 BTextView::MouseDown(where
);
86 _BTextInput_::FrameResized(float width
, float height
)
88 BTextView::FrameResized(width
, height
);
95 _BTextInput_::KeyDown(const char* bytes
, int32 numBytes
)
100 if (!TextControl()->IsEnabled())
103 if (fPreviousText
== NULL
|| strcmp(Text(), fPreviousText
) != 0) {
104 TextControl()->Invoke();
106 fPreviousText
= strdup(Text());
114 BView::KeyDown(bytes
, numBytes
);
118 BTextView::KeyDown(bytes
, numBytes
);
125 _BTextInput_::MakeFocus(bool state
)
127 if (state
== IsFocus())
130 BTextView::MakeFocus(state
);
136 if (strcmp(Text(), fPreviousText
) != 0)
137 TextControl()->Invoke();
140 fPreviousText
= NULL
;
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
);
155 _BTextInput_::MinSize()
158 min
.height
= ceilf(LineHeight(0) + 2.0);
159 // we always add at least one pixel vertical inset top/bottom for
161 min
.width
= min
.height
* 3;
162 return BLayoutUtils::ComposeSize(ExplicitMinSize(), min
);
167 _BTextInput_::AlignTextRect()
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));
177 float textFontWidth
= TextRect().right
;
179 if (be_control_look
!= NULL
) {
180 switch (Alignment()) {
182 hInset
= be_control_look
->DefaultLabelSpacing();
186 hInset
= textRect
.right
- textFontWidth
;
187 hInset
-= be_control_look
->DefaultLabelSpacing();
191 hInset
= (textRect
.right
- textFontWidth
) / 2.0;
199 textRect
.InsetBy(hInset
, vInset
);
200 SetTextRect(textRect
);
205 _BTextInput_::SetInitialText()
208 fPreviousText
= NULL
;
211 fPreviousText
= strdup(Text());
216 _BTextInput_::Paste(BClipboard
* clipboard
)
218 BTextView::Paste(clipboard
);
224 _BTextInput_::InsertText(const char* inText
, int32 inLength
,
225 int32 inOffset
, const text_run_array
* inRuns
)
227 // Filter all line breaks, note that inText is not terminated.
229 if (*inText
== '\n' || *inText
== '\r')
230 BTextView::InsertText(" ", 1, inOffset
, inRuns
);
232 BTextView::InsertText(inText
, 1, inOffset
, inRuns
);
234 BString
filteredText(inText
, inLength
);
235 filteredText
.ReplaceAll('\n', ' ');
236 filteredText
.ReplaceAll('\r', ' ');
237 BTextView::InsertText(filteredText
.String(), inLength
, inOffset
,
241 TextControl()->InvokeNotify(TextControl()->ModificationMessage(),
247 _BTextInput_::DeleteText(int32 fromOffset
, int32 toOffset
)
249 BTextView::DeleteText(fromOffset
, toOffset
);
251 TextControl()->InvokeNotify(TextControl()->ModificationMessage(),
257 _BTextInput_::TextControl()
259 BTextControl
* textControl
= NULL
;
260 if (Parent() != NULL
)
261 textControl
= dynamic_cast<BTextControl
*>(Parent());
263 if (textControl
== NULL
)
264 debugger("_BTextInput_ should have a BTextControl as parent");
270 } // namespace BPrivate