Ticket #5203: coding_style.diff
File coding_style.diff, 6.1 KB (added by , 14 years ago) |
---|
-
ExpressionTextView.cpp
178 178 179 179 180 180 void 181 ExpressionTextView::SetValue( const char*value)181 ExpressionTextView::SetValue(BString value) 182 182 { 183 // copy the value string so we can modify it184 BString val(value);185 186 183 // save the value 187 fCurrentValue = val ;184 fCurrentValue = value; 188 185 189 186 // calculate the width of the string 190 187 BFont font; 191 188 uint32 mode = B_FONT_ALL; 192 189 GetFontAndColor(&font, &mode); 193 float stringWidth = font.StringWidth(val );190 float stringWidth = font.StringWidth(value); 194 191 195 192 // make the string shorter if it does not fit in the view 196 193 float viewWidth = Frame().Width(); 197 if (val.CountChars() > 3 && stringWidth > viewWidth) { 198 194 if (value.CountChars() > 3 && stringWidth > viewWidth) { 199 195 // get the position of the first digit 200 196 int32 firstDigit = 0; 201 if (val [0] == '-')197 if (value[0] == '-') 202 198 firstDigit++; 203 199 204 200 // calculate the value of the exponent 205 201 int32 exponent = 0; 206 int32 offset = val .FindFirst('.');202 int32 offset = value.FindFirst('.'); 207 203 if (offset == B_ERROR) { 208 exponent = val .CountChars() - 1 - firstDigit;209 val .Insert('.', 1, firstDigit + 1);204 exponent = value.CountChars() - 1 - firstDigit; 205 value.Insert('.', 1, firstDigit + 1); 210 206 } else { 211 207 if (offset == firstDigit + 1) { 212 208 // if the value is 0.01 or larger then scientific notation 213 209 // won't shorten the string 214 if (val [firstDigit] != '0' || val[firstDigit+2] != '0'215 || val [firstDigit+3] != '0') {210 if (value[firstDigit] != '0' || value[firstDigit+2] != '0' 211 || value[firstDigit + 3] != '0') { 216 212 exponent = 0; 217 213 } else { 218 214 // remove the period 219 val .Remove(offset, 1);215 value.Remove(offset, 1); 220 216 221 217 // check for negative exponent value 222 218 exponent = 0; 223 while (val [firstDigit] == '0') {224 val .Remove(firstDigit, 1);219 while (value[firstDigit] == '0') { 220 value.Remove(firstDigit, 1); 225 221 exponent--; 226 222 } 227 223 228 // add the period229 val .Insert('.', 1, firstDigit + 1);224 // add the period 225 value.Insert('.', 1, firstDigit + 1); 230 226 } 231 227 } else { 232 228 // if the period + 1 digit fits in the view scientific notation 233 229 // won't shorten the string 234 BString temp = val ;230 BString temp = value; 235 231 temp.Truncate(offset + 2); 236 232 stringWidth = font.StringWidth(temp); 237 233 if (stringWidth < viewWidth) 238 234 exponent = 0; 239 235 else { 240 236 // move the period 241 val .Remove(offset, 1);242 val .Insert('.', 1, firstDigit + 1);237 value.Remove(offset, 1); 238 value.Insert('.', 1, firstDigit + 1); 243 239 244 240 exponent = offset - (firstDigit + 1); 245 241 } … … 247 243 } 248 244 249 245 // add the exponent 250 offset = val .CountChars() - 1;246 offset = value.CountChars() - 1; 251 247 if (exponent != 0) 252 val << "E" << exponent;248 value << "E" << exponent; 253 249 254 250 // reduce the number of digits until the string fits or can not be 255 251 // made any shorter 256 stringWidth = font.StringWidth(val );252 stringWidth = font.StringWidth(value); 257 253 char lastRemovedDigit = '0'; 258 254 while (offset > firstDigit && stringWidth > viewWidth) { 259 if (val [offset] != '.')260 lastRemovedDigit = val [offset];261 val .Remove(offset--, 1);262 stringWidth = font.StringWidth(val );255 if (value[offset] != '.') 256 lastRemovedDigit = value[offset]; 257 value.Remove(offset--, 1); 258 stringWidth = font.StringWidth(value); 263 259 } 264 260 265 261 // there is no need to keep the period if no digits follow 266 if (val [offset] == '.') {267 val .Remove(offset, 1);262 if (value[offset] == '.') { 263 value.Remove(offset, 1); 268 264 offset--; 269 265 } 270 266 271 267 // take care of proper rounding of the result 272 int digit = (int)lastRemovedDigit - 48; // ascii to int268 int digit = (int)lastRemovedDigit - '0'; // ascii to int 273 269 if (digit >= 5) { 274 270 for (; offset >= firstDigit; offset--) { 275 if (val [offset] == '.')271 if (value[offset] == '.') 276 272 continue; 277 digit = (int)(val[offset]) - 47; // ascii to int + 1 278 if (digit != 10) break; 279 val.Remove(offset, 1); 273 digit = (int)(value[offset]) - '0' + 1; // ascii to int + 1 274 if (digit != 10) 275 break; 276 value.Remove(offset, 1); 280 277 } 281 278 if (digit == 10) { 282 279 // carry over, shift the result 283 if (val [firstDigit+1] == '.') {284 val [firstDigit+1] = '0';285 val [firstDigit] = '.';280 if (value[firstDigit+1] == '.') { 281 value[firstDigit+1] = '0'; 282 value[firstDigit] = '.'; 286 283 } 287 val .Insert('1', 1, firstDigit);284 value.Insert('1', 1, firstDigit); 288 285 289 // remove the exponent value and the last digit290 offset = val .FindFirst('E');286 // remove the exponent value and the last digit 287 offset = value.FindFirst('E'); 291 288 if (offset == B_ERROR) 292 offset = val .CountChars();293 val .Truncate(--offset);294 offset--; // offset now points to the last digit289 offset = value.CountChars(); 290 value.Truncate(--offset); 291 offset--; // offset now points to the last digit 295 292 296 293 // increase the exponent and add it back to the string 297 294 exponent++; 298 val << 'E' << exponent;295 value << 'E' << exponent; 299 296 } else { 300 297 // increase the current digit value with one 301 val [offset] = char(digit + 48);298 value[offset] = char(digit + 48); 302 299 } 303 300 } 304 301 305 302 // remove trailing zeros 306 while (val [offset] == '0')307 val .Remove(offset--, 1);303 while (value[offset] == '0') 304 value.Remove(offset--, 1); 308 305 309 306 // there is no need to keep the period if no digits follow 310 if (val [offset] == '.')311 val .Remove(offset, 1);307 if (value[offset] == '.') 308 value.Remove(offset, 1); 312 309 } 313 310 314 311 // set the new value 315 SetExpression(val );312 SetExpression(value); 316 313 } 317 314 318 315 -
ExpressionTextView.h
42 42 void AddKeypadLabel(const char* label); 43 43 44 44 void SetExpression(const char* expression); 45 void SetValue( const char*value);45 void SetValue(BString value); 46 46 47 47 void BackSpace(); 48 48 void Clear();