* Copyright 2006-2009, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "InputTextView.h"
#include <stdio.h>
#include <stdlib.h>
#include <String.h>
InputTextView::InputTextView(BRect frame, const char* name,
BRect textRect,
uint32 resizingMode,
uint32 flags)
: BTextView(frame, name, textRect, resizingMode, flags),
fWasFocus(false),
fTextBeforeFocus("")
{
SetWordWrap(false);
}
InputTextView::~InputTextView()
{
}
void
InputTextView::MouseDown(BPoint where)
{
fWasFocus = IsFocus();
if (fWasFocus) {
BTextView::MouseDown(where);
} else {
if (BView* view = Parent()) {
view->MouseDown(ConvertToParent(where));
}
}
}
void
InputTextView::MouseUp(BPoint where)
{
if (fWasFocus)
BTextView::MouseUp(where);
}
void
InputTextView::KeyDown(const char* bytes, int32 numBytes)
{
bool handled = true;
if (numBytes > 0) {
switch (bytes[0]) {
case B_ESCAPE:
RevertChanges();
fTextBeforeFocus = Text();
break;
case B_RETURN:
ApplyChanges();
fTextBeforeFocus = Text();
break;
default:
handled = false;
break;
}
}
if (!handled)
BTextView::KeyDown(bytes, numBytes);
}
void
InputTextView::MakeFocus(bool focus)
{
if (focus == IsFocus())
return;
if (BView* view = Parent())
view->Invalidate();
BTextView::MakeFocus(focus);
if (focus) {
SelectAll();
fTextBeforeFocus = Text();
} else {
if (fTextBeforeFocus != Text())
Invoke();
}
}
status_t
InputTextView::Invoke(BMessage* message)
{
if (!message)
message = Message();
if (message) {
BMessage copy(*message);
copy.AddInt64("when", system_time());
copy.AddPointer("source", (BView*)this);
return BInvoker::Invoke(©);
}
return B_BAD_VALUE;
}