Debugger: Implement float value editor.

FloatValueFormatter:
- Implement parsing/validation hooks.

FloatValueHandler:
- Implement GetTableCellValueEditor() to accordingly return a
  (newly implemented) TableCellFloatEditor. This allows variable
  value editing for float/double variables.
This commit is contained in:
Rene Gollent 2015-07-24 19:22:08 -04:00
parent 5c5c25163d
commit f5d564a1d3
7 changed files with 204 additions and 1 deletions

View File

@ -311,6 +311,7 @@ local sources =
# user_interface/gui/value
TableCellBoolEditor.cpp
TableCellEnumerationEditor.cpp
TableCellFloatEditor.cpp
TableCellFormattedValueEditor.cpp
TableCellFormattedValueRenderer.cpp
TableCellIntegerEditor.cpp

View File

@ -0,0 +1,52 @@
/*
* Copyright 2015, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#include "TableCellFloatEditor.h"
#include <ctype.h>
#include <Variant.h>
#include "IntegerValue.h"
#include "IntegerValueFormatter.h"
TableCellFloatEditor::TableCellFloatEditor(::Value* initialValue,
ValueFormatter* formatter)
:
TableCellTextControlEditor(initialValue, formatter)
{
}
TableCellFloatEditor::~TableCellFloatEditor()
{
}
bool
TableCellFloatEditor::ValidateInput() const
{
BVariant variantValue;
if (!InitialValue()->ToVariant(variantValue))
return false;
return GetValueFormatter()->ValidateFormattedValue(Text(),
variantValue.Type());
}
status_t
TableCellFloatEditor::GetValueForInput(::Value*& _output) const
{
BVariant variantValue;
if (!InitialValue()->ToVariant(variantValue))
return B_NO_MEMORY;
return GetValueFormatter()->GetValueFromFormattedInput(Text(),
variantValue.Type(), _output);
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2015, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#ifndef TABLE_CELL_FLOAT_EDITOR_H
#define TABLE_CELL_FLOAT_EDITOR_H
#include <TextControl.h>
#include "TableCellTextControlEditor.h"
class TableCellFloatEditor : public TableCellTextControlEditor {
public:
TableCellFloatEditor(::Value* initialValue,
ValueFormatter* formatter);
virtual ~TableCellFloatEditor();
virtual bool ValidateInput() const;
virtual status_t GetValueForInput(::Value*& _output) const;
// returns reference
};
#endif // TABLE_CELL_INTEGER_EDITOR_H

View File

@ -5,7 +5,11 @@
*/
#include "FloatValueFormatter.h"
#include <new>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "FloatValue.h"
@ -30,9 +34,80 @@ FloatValueFormatter::FormatValue(Value* _value, BString& _output)
return B_BAD_VALUE;
char buffer[64];
snprintf(buffer, sizeof(buffer), "%g", value->GetValue());
BVariant variantValue = value->GetValue();
switch (variantValue.Type()) {
case B_FLOAT_TYPE:
{
snprintf(buffer, sizeof(buffer), "%f", variantValue.ToFloat());
break;
}
case B_DOUBLE_TYPE:
{
snprintf(buffer, sizeof(buffer), "%g", variantValue.ToDouble());
break;
}
}
_output.SetTo(buffer);
return B_OK;
}
bool
FloatValueFormatter::SupportsValidation() const
{
return true;
}
bool
FloatValueFormatter::ValidateFormattedValue(const BString& input,
type_code type) const
{
::Value* value = NULL;
return _PerformValidation(input, type, value, false) == B_OK;
}
status_t
FloatValueFormatter::GetValueFromFormattedInput(const BString& input,
type_code type, Value*& _output) const
{
return _PerformValidation(input, type, _output, true);
}
status_t
FloatValueFormatter::_PerformValidation(const BString& input, type_code type,
::Value*& _output, bool wantsValue) const
{
const char* text = input.String();
char *parseEnd = NULL;
double parsedValue = strtod(text, &parseEnd);
if (parseEnd - text < input.Length() && !isspace(*parseEnd))
return B_NO_MEMORY;
BVariant newValue;
switch (type) {
case B_FLOAT_TYPE:
{
newValue.SetTo((float)parsedValue);
break;
}
case B_DOUBLE_TYPE:
{
newValue.SetTo(parsedValue);
break;
}
default:
return B_BAD_VALUE;
}
if (wantsValue) {
_output = new(std::nothrow) FloatValue(newValue);
if (_output == NULL)
return B_NO_MEMORY;
}
return B_OK;
}

View File

@ -22,6 +22,22 @@ public:
{ return NULL; }
virtual status_t FormatValue(Value* value, BString& _output);
virtual bool SupportsValidation() const;
virtual bool ValidateFormattedValue(
const BString& input,
type_code type) const;
virtual status_t GetValueFromFormattedInput(
const BString& input, type_code type,
Value*& _output) const;
private:
status_t _PerformValidation(const BString& input,
type_code type,
::Value*& _output,
bool wantsValue) const;
};

View File

@ -10,6 +10,7 @@
#include "FloatValue.h"
#include "FloatValueFormatter.h"
#include "TableCellFloatEditor.h"
#include "TableCellFormattedValueRenderer.h"
@ -75,3 +76,33 @@ FloatValueHandler::GetTableCellValueRenderer(Value* value,
_renderer = renderer;
return B_OK;
}
status_t
FloatValueHandler::GetTableCellValueEditor(Value* _value, Settings* settings,
TableCellValueEditor*& _editor)
{
FloatValue* value = dynamic_cast<FloatValue*>(_value);
if (value == NULL)
return B_BAD_VALUE;
ValueFormatter* formatter;
status_t error = GetValueFormatter(value, formatter);
if (error != B_OK)
return error;
BReference<ValueFormatter> formatterReference(formatter, true);
TableCellFloatEditor* editor = new(std::nothrow)
TableCellFloatEditor(value, formatter);
if (editor == NULL)
return B_NO_MEMORY;
BReference<TableCellFloatEditor> editorReference(editor, true);
error = editor->Init();
if (error != B_OK)
return error;
editorReference.Detach();
_editor = editor;
return B_OK;
}

View File

@ -21,6 +21,9 @@ public:
ValueFormatter*& _formatter);
virtual status_t GetTableCellValueRenderer(Value* value,
TableCellValueRenderer*& _renderer);
virtual status_t GetTableCellValueEditor(Value* value,
Settings* settings,
TableCellValueEditor*& _editor);
};