diff --git a/src/apps/debugger/Jamfile b/src/apps/debugger/Jamfile index 83f10bbcb9..0985601c1f 100644 --- a/src/apps/debugger/Jamfile +++ b/src/apps/debugger/Jamfile @@ -311,6 +311,7 @@ local sources = # user_interface/gui/value TableCellBoolEditor.cpp TableCellEnumerationEditor.cpp + TableCellFloatEditor.cpp TableCellFormattedValueEditor.cpp TableCellFormattedValueRenderer.cpp TableCellIntegerEditor.cpp diff --git a/src/apps/debugger/user_interface/gui/value/TableCellFloatEditor.cpp b/src/apps/debugger/user_interface/gui/value/TableCellFloatEditor.cpp new file mode 100644 index 0000000000..787c566d79 --- /dev/null +++ b/src/apps/debugger/user_interface/gui/value/TableCellFloatEditor.cpp @@ -0,0 +1,52 @@ +/* + * Copyright 2015, Rene Gollent, rene@gollent.com. + * Distributed under the terms of the MIT License. + */ + +#include "TableCellFloatEditor.h" + +#include + +#include + +#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); +} + + diff --git a/src/apps/debugger/user_interface/gui/value/TableCellFloatEditor.h b/src/apps/debugger/user_interface/gui/value/TableCellFloatEditor.h new file mode 100644 index 0000000000..a618ea2c13 --- /dev/null +++ b/src/apps/debugger/user_interface/gui/value/TableCellFloatEditor.h @@ -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 + +#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 diff --git a/src/apps/debugger/value/value_formatters/FloatValueFormatter.cpp b/src/apps/debugger/value/value_formatters/FloatValueFormatter.cpp index 4024a2ea90..db3a28fe99 100644 --- a/src/apps/debugger/value/value_formatters/FloatValueFormatter.cpp +++ b/src/apps/debugger/value/value_formatters/FloatValueFormatter.cpp @@ -5,7 +5,11 @@ */ #include "FloatValueFormatter.h" +#include + +#include #include +#include #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; +} diff --git a/src/apps/debugger/value/value_formatters/FloatValueFormatter.h b/src/apps/debugger/value/value_formatters/FloatValueFormatter.h index 4d59a2ef04..2e26126167 100644 --- a/src/apps/debugger/value/value_formatters/FloatValueFormatter.h +++ b/src/apps/debugger/value/value_formatters/FloatValueFormatter.h @@ -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; }; diff --git a/src/apps/debugger/value/value_handlers/FloatValueHandler.cpp b/src/apps/debugger/value/value_handlers/FloatValueHandler.cpp index 29c13a646a..039620a82a 100644 --- a/src/apps/debugger/value/value_handlers/FloatValueHandler.cpp +++ b/src/apps/debugger/value/value_handlers/FloatValueHandler.cpp @@ -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(_value); + if (value == NULL) + return B_BAD_VALUE; + + ValueFormatter* formatter; + status_t error = GetValueFormatter(value, formatter); + if (error != B_OK) + return error; + BReference formatterReference(formatter, true); + + TableCellFloatEditor* editor = new(std::nothrow) + TableCellFloatEditor(value, formatter); + if (editor == NULL) + return B_NO_MEMORY; + + BReference editorReference(editor, true); + error = editor->Init(); + if (error != B_OK) + return error; + + editorReference.Detach(); + _editor = editor; + return B_OK; +} diff --git a/src/apps/debugger/value/value_handlers/FloatValueHandler.h b/src/apps/debugger/value/value_handlers/FloatValueHandler.h index 153bce78c6..7a9f3a4944 100644 --- a/src/apps/debugger/value/value_handlers/FloatValueHandler.h +++ b/src/apps/debugger/value/value_handlers/FloatValueHandler.h @@ -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); };