From d2176a2d86634ed2e9c3c20e8daee5ad0b37e836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Wed, 7 Jun 2006 22:42:00 +0000 Subject: [PATCH] * fix leading spaces after evaluating * increase to 13 digits after decimal point * load and save the expression history git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17776 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/deskcalc/CalcView.cpp | 11 +++++- src/apps/deskcalc/ExpressionTextView.cpp | 50 +++++++++++++++++++++++- src/apps/deskcalc/ExpressionTextView.h | 3 ++ src/apps/deskcalc/InputTextView.cpp | 1 - 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/apps/deskcalc/CalcView.cpp b/src/apps/deskcalc/CalcView.cpp index 7e65c74f99..60b77065b3 100644 --- a/src/apps/deskcalc/CalcView.cpp +++ b/src/apps/deskcalc/CalcView.cpp @@ -639,12 +639,15 @@ CalcView::LoadSettings(BMessage* archive) // load display text const char* display; if (archive->FindString("displayText", &display) < B_OK) { - puts("Missing display text from CalcView archive.\n"); + puts("Missing expression text from CalcView archive.\n"); } else { // init expression text fExpressionTextView->SetText(display); } + // load expression history + fExpressionTextView->LoadSettings(archive); + // parse calculator description _ParseCalcDesc(fKeypadDescription); @@ -686,6 +689,10 @@ CalcView::SaveSettings(BMessage* archive) const if (ret == B_OK) ret = archive->AddString("displayText", fExpressionTextView->Text()); + // record expression history + if (ret == B_OK) + ret = fExpressionTextView->SaveSettings(archive); + // record calculator description if (ret == B_OK) ret = archive->AddString("calcDesc", fKeypadDescription); @@ -740,7 +747,7 @@ CalcView::Evaluate() } else if (((value < EXP_SWITCH_HI) && (value > EXP_SWITCH_LO)) || ((value > -EXP_SWITCH_HI) && (value < -EXP_SWITCH_LO))) { // print in std form - sprintf(buf, "%9f", value); + sprintf(buf, "%.13f", value); // hack to remove surplus zeros! if (strchr(buf, '.')) { diff --git a/src/apps/deskcalc/ExpressionTextView.cpp b/src/apps/deskcalc/ExpressionTextView.cpp index d3f986048a..bc6270553d 100644 --- a/src/apps/deskcalc/ExpressionTextView.cpp +++ b/src/apps/deskcalc/ExpressionTextView.cpp @@ -9,6 +9,7 @@ #include "ExpressionTextView.h" #include +#include #include #include @@ -72,6 +73,7 @@ ExpressionTextView::KeyDown(const char* bytes, int32 numBytes) NextExpression(); return; } + BString current = Text(); // handle in InputTextView, except B_TAB if (bytes[0] != B_TAB) @@ -83,7 +85,8 @@ ExpressionTextView::KeyDown(const char* bytes, int32 numBytes) // as soon as something is typed, we are at the // end of the expression history - fHistoryPos = fPreviousExpressions.CountItems(); + if (current != Text()) + fHistoryPos = fPreviousExpressions.CountItems(); } @@ -119,6 +122,9 @@ ExpressionTextView::ApplyChanges() } +// #pragma mark - + + void ExpressionTextView::AddKeypadLabel(const char* label) { @@ -156,6 +162,18 @@ ExpressionTextView::Clear() void ExpressionTextView::AddExpressionToHistory(const char* expression) { + // clean out old expressions that are the same as + // the one to be added + int32 count = fPreviousExpressions.CountItems(); + for (int32 i = 0; i < count; i++) { + BString* item = (BString*)fPreviousExpressions.ItemAt(i); + if (*item == expression && fPreviousExpressions.RemoveItem(i)) { + delete item; + i--; + count--; + } + } + BString* item = new (nothrow) BString(expression); if (!item) return; @@ -212,3 +230,33 @@ ExpressionTextView::NextExpression() SetExpression(item->String()); } + +// #pragma mark - + + +void +ExpressionTextView::LoadSettings(const BMessage* archive) +{ + const char* oldExpression; + for (int32 i = 0; + archive->FindString("previous expression", i, &oldExpression) == B_OK; + i++) { + AddExpressionToHistory(oldExpression); + } +} + + +status_t +ExpressionTextView::SaveSettings(BMessage* archive) const +{ + int32 count = fPreviousExpressions.CountItems(); + for (int32 i = 0; i < count; i++) { + BString* item = (BString*)fPreviousExpressions.ItemAtFast(i); + status_t ret = archive->AddString("previous expression", item->String()); + if (ret < B_OK) + return ret; + } + return B_OK; +} + + diff --git a/src/apps/deskcalc/ExpressionTextView.h b/src/apps/deskcalc/ExpressionTextView.h index 0ded2a96b8..e28fd3ca78 100644 --- a/src/apps/deskcalc/ExpressionTextView.h +++ b/src/apps/deskcalc/ExpressionTextView.h @@ -44,6 +44,9 @@ class ExpressionTextView : public InputTextView { void PreviousExpression(); void NextExpression(); + void LoadSettings(const BMessage* archive); + status_t SaveSettings(BMessage* archive) const; + private: CalcView* fCalcView; BString fKeypadLabels; diff --git a/src/apps/deskcalc/InputTextView.cpp b/src/apps/deskcalc/InputTextView.cpp index 6db540da57..e974c4ed38 100644 --- a/src/apps/deskcalc/InputTextView.cpp +++ b/src/apps/deskcalc/InputTextView.cpp @@ -95,7 +95,6 @@ InputTextView::MakeFocus(bool focus) BTextView::MakeFocus(focus); if (focus) SelectAll(); - ApplyChanges(); } }