* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "NummericalTextView.h"
#include <stdio.h>
#include <stdlib.h>
#include <String.h>
NummericalTextView::NummericalTextView(BRect frame, const char* name,
BRect textRect,
uint32 resizingMode,
uint32 flags)
: InputTextView(frame, name, textRect, resizingMode, flags)
{
for (uint32 i = 0; i < '0'; i++) {
DisallowChar(i);
}
for (uint32 i = '9' + 1; i < 255; i++) {
DisallowChar(i);
}
AllowChar('-');
}
NummericalTextView::~NummericalTextView()
{
}
status_t
NummericalTextView::Invoke(BMessage* message)
{
if (!message)
message = Message();
if (message) {
BMessage copy(*message);
copy.AddInt32("be:value", IntValue());
copy.AddFloat("float value", FloatValue());
return InputTextView::Invoke(©);
}
return B_BAD_VALUE;
}
void
NummericalTextView::RevertChanges()
{
if (fFloatMode)
SetValue(fFloatValueCache);
else
SetValue(fIntValueCache);
}
void
NummericalTextView::ApplyChanges()
{
int32 i = atoi(Text());
float f = atof(Text());
if ((fFloatMode && f != fFloatValueCache) ||
(!fFloatMode && i != fIntValueCache)) {
Invoke();
}
}
void
NummericalTextView::SetFloatMode(bool floatingPoint)
{
fFloatMode = floatingPoint;
if (floatingPoint)
AllowChar('.');
else
DisallowChar('.');
}
void
NummericalTextView::SetValue(int32 value)
{
BString helper;
helper << value;
SetText(helper.String());
IntValue();
FloatValue();
if (IsFocus())
SelectAll();
}
void
NummericalTextView::SetValue(float value)
{
BString helper;
helper << value;
SetText(helper.String());
IntValue();
FloatValue();
if (IsFocus())
SelectAll();
}
int32
NummericalTextView::IntValue() const
{
fIntValueCache = atoi(Text());
return fIntValueCache;
}
float
NummericalTextView::FloatValue() const
{
fFloatValueCache = atof(Text());
return fFloatValueCache;
}
void
NummericalTextView::Select(int32 start, int32 finish)
{
InputTextView::Select(start, finish);
_CheckMinusAllowed();
_CheckDotAllowed();
}
void
NummericalTextView::InsertText(const char* inText, int32 inLength, int32 inOffset,
const text_run_array* inRuns)
{
InputTextView::InsertText(inText, inLength, inOffset, inRuns);
_CheckMinusAllowed();
_CheckDotAllowed();
}
void
NummericalTextView::DeleteText(int32 fromOffset, int32 toOffset)
{
InputTextView::DeleteText(fromOffset, toOffset);
_CheckMinusAllowed();
_CheckDotAllowed();
}
void
NummericalTextView::_ToggleAllowChar(char c)
{
const char* text = Text();
if (text) {
bool found = false;
int32 selectionStart;
int32 selectionEnd;
GetSelection(&selectionStart, &selectionEnd);
int32 pos = 0;
while (text[pos]) {
if (selectionStart < selectionEnd
&& pos == selectionStart) {
pos = selectionEnd;
}
if (text[pos] == c) {
found = true;
break;
}
pos++;
}
if (found)
DisallowChar(c);
else
AllowChar(c);
}
}
void
NummericalTextView::_CheckMinusAllowed()
{
_ToggleAllowChar('-');
}
void
NummericalTextView::_CheckDotAllowed()
{
if (fFloatMode) {
_ToggleAllowChar('.');
}
}