Fixed the coding style issues that axel pointed out.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36409 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2010-04-22 10:05:28 +00:00
parent 01e63caf83
commit 8af2493444
2 changed files with 50 additions and 53 deletions
+49 -52
View File
@@ -178,68 +178,64 @@ ExpressionTextView::SetExpression(const char* expression)
void void
ExpressionTextView::SetValue(const char* value) ExpressionTextView::SetValue(BString value)
{ {
// copy the value string so we can modify it
BString val(value);
// save the value // save the value
fCurrentValue = val; fCurrentValue = value;
// calculate the width of the string // calculate the width of the string
BFont font; BFont font;
uint32 mode = B_FONT_ALL; uint32 mode = B_FONT_ALL;
GetFontAndColor(&font, &mode); GetFontAndColor(&font, &mode);
float stringWidth = font.StringWidth(val); float stringWidth = font.StringWidth(value);
// make the string shorter if it does not fit in the view // make the string shorter if it does not fit in the view
float viewWidth = Frame().Width(); float viewWidth = Frame().Width();
if (val.CountChars() > 3 && stringWidth > viewWidth) { if (value.CountChars() > 3 && stringWidth > viewWidth) {
// get the position of the first digit // get the position of the first digit
int32 firstDigit = 0; int32 firstDigit = 0;
if (val[0] == '-') if (value[0] == '-')
firstDigit++; firstDigit++;
// calculate the value of the exponent // calculate the value of the exponent
int32 exponent = 0; int32 exponent = 0;
int32 offset = val.FindFirst('.'); int32 offset = value.FindFirst('.');
if (offset == B_ERROR) { if (offset == B_ERROR) {
exponent = val.CountChars() - 1 - firstDigit; exponent = value.CountChars() - 1 - firstDigit;
val.Insert('.', 1, firstDigit + 1); value.Insert('.', 1, firstDigit + 1);
} else { } else {
if (offset == firstDigit + 1) { if (offset == firstDigit + 1) {
// if the value is 0.01 or larger then scientific notation // if the value is 0.01 or larger then scientific notation
// won't shorten the string // won't shorten the string
if (val[firstDigit] != '0' || val[firstDigit+2] != '0' if (value[firstDigit] != '0' || value[firstDigit+2] != '0'
|| val[firstDigit+3] != '0') { || value[firstDigit + 3] != '0') {
exponent = 0; exponent = 0;
} else { } else {
// remove the period // remove the period
val.Remove(offset, 1); value.Remove(offset, 1);
// check for negative exponent value // check for negative exponent value
exponent = 0; exponent = 0;
while (val[firstDigit] == '0') { while (value[firstDigit] == '0') {
val.Remove(firstDigit, 1); value.Remove(firstDigit, 1);
exponent--; exponent--;
} }
//add the period // add the period
val.Insert('.', 1, firstDigit + 1); value.Insert('.', 1, firstDigit + 1);
} }
} else { } else {
// if the period + 1 digit fits in the view scientific notation // if the period + 1 digit fits in the view scientific notation
// won't shorten the string // won't shorten the string
BString temp = val; BString temp = value;
temp.Truncate(offset + 2); temp.Truncate(offset + 2);
stringWidth = font.StringWidth(temp); stringWidth = font.StringWidth(temp);
if (stringWidth < viewWidth) if (stringWidth < viewWidth)
exponent = 0; exponent = 0;
else { else {
// move the period // move the period
val.Remove(offset, 1); value.Remove(offset, 1);
val.Insert('.', 1, firstDigit + 1); value.Insert('.', 1, firstDigit + 1);
exponent = offset - (firstDigit + 1); exponent = offset - (firstDigit + 1);
} }
@@ -247,72 +243,73 @@ ExpressionTextView::SetValue(const char* value)
} }
// add the exponent // add the exponent
offset = val.CountChars() - 1; offset = value.CountChars() - 1;
if (exponent != 0) if (exponent != 0)
val << "E" << exponent; value << "E" << exponent;
// reduce the number of digits until the string fits or can not be // reduce the number of digits until the string fits or can not be
// made any shorter // made any shorter
stringWidth = font.StringWidth(val); stringWidth = font.StringWidth(value);
char lastRemovedDigit = '0'; char lastRemovedDigit = '0';
while (offset > firstDigit && stringWidth > viewWidth) { while (offset > firstDigit && stringWidth > viewWidth) {
if (val[offset] != '.') if (value[offset] != '.')
lastRemovedDigit = val[offset]; lastRemovedDigit = value[offset];
val.Remove(offset--, 1); value.Remove(offset--, 1);
stringWidth = font.StringWidth(val); stringWidth = font.StringWidth(value);
} }
// there is no need to keep the period if no digits follow // there is no need to keep the period if no digits follow
if (val[offset] == '.') { if (value[offset] == '.') {
val.Remove(offset, 1); value.Remove(offset, 1);
offset--; offset--;
} }
// take care of proper rounding of the result // take care of proper rounding of the result
int digit = (int)lastRemovedDigit - 48; // ascii to int int digit = (int)lastRemovedDigit - '0'; // ascii to int
if (digit >= 5) { if (digit >= 5) {
for (; offset >= firstDigit; offset--) { for (; offset >= firstDigit; offset--) {
if (val[offset] == '.') if (value[offset] == '.')
continue; continue;
digit = (int)(val[offset]) - 47; // ascii to int + 1 digit = (int)(value[offset]) - '0' + 1; // ascii to int + 1
if (digit != 10) break; if (digit != 10)
val.Remove(offset, 1); break;
value.Remove(offset, 1);
} }
if (digit == 10) { if (digit == 10) {
// carry over, shift the result // carry over, shift the result
if (val[firstDigit+1] == '.') { if (value[firstDigit+1] == '.') {
val[firstDigit+1] = '0'; value[firstDigit+1] = '0';
val[firstDigit] = '.'; value[firstDigit] = '.';
} }
val.Insert('1', 1, firstDigit); value.Insert('1', 1, firstDigit);
//remove the exponent value and the last digit // remove the exponent value and the last digit
offset = val.FindFirst('E'); offset = value.FindFirst('E');
if (offset == B_ERROR) if (offset == B_ERROR)
offset = val.CountChars(); offset = value.CountChars();
val.Truncate(--offset); value.Truncate(--offset);
offset--; //offset now points to the last digit offset--; // offset now points to the last digit
// increase the exponent and add it back to the string // increase the exponent and add it back to the string
exponent++; exponent++;
val << 'E' << exponent; value << 'E' << exponent;
} else { } else {
// increase the current digit value with one // increase the current digit value with one
val[offset] = char(digit + 48); value[offset] = char(digit + 48);
} }
} }
// remove trailing zeros // remove trailing zeros
while (val[offset] == '0') while (value[offset] == '0')
val.Remove(offset--, 1); value.Remove(offset--, 1);
// there is no need to keep the period if no digits follow // there is no need to keep the period if no digits follow
if (val[offset] == '.') if (value[offset] == '.')
val.Remove(offset, 1); value.Remove(offset, 1);
} }
// set the new value // set the new value
SetExpression(val); SetExpression(value);
} }
+1 -1
View File
@@ -42,7 +42,7 @@ class ExpressionTextView : public InputTextView {
void AddKeypadLabel(const char* label); void AddKeypadLabel(const char* label);
void SetExpression(const char* expression); void SetExpression(const char* expression);
void SetValue(const char* value); void SetValue(BString value);
void BackSpace(); void BackSpace();
void Clear(); void Clear();