-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
ExpressionTextView.cpp
452 lines (363 loc) · 9.58 KB
/
ExpressionTextView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright 2006-2011 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "ExpressionTextView.h"
#include <new>
#include <stdio.h>
#include <Beep.h>
#include <Window.h>
#include "CalcView.h"
using std::nothrow;
static const int32 kMaxPreviousExpressions = 20;
ExpressionTextView::ExpressionTextView(BRect frame, CalcView* calcView)
:
InputTextView(frame, "expression text view",
(frame.OffsetToCopy(B_ORIGIN)).InsetByCopy(2, 2),
B_FOLLOW_NONE, B_WILL_DRAW),
fCalcView(calcView),
fKeypadLabels(""),
fPreviousExpressions(20),
fHistoryPos(0),
fCurrentExpression(""),
fCurrentValue(""),
fChangesApplied(false)
{
SetStylable(false);
SetDoesUndo(true);
SetColorSpace(B_RGB32);
SetFontAndColor(be_bold_font, B_FONT_ALL);
}
ExpressionTextView::~ExpressionTextView()
{
int32 count = fPreviousExpressions.CountItems();
for (int32 i = 0; i < count; i++)
delete (BString*)fPreviousExpressions.ItemAtFast(i);
}
void
ExpressionTextView::MakeFocus(bool focused)
{
if (focused == IsFocus()) {
// stop endless loop when CalcView calls us again
return;
}
// NOTE: order of lines important!
InputTextView::MakeFocus(focused);
fCalcView->MakeFocus(focused);
}
void
ExpressionTextView::KeyDown(const char* bytes, int32 numBytes)
{
// Handle expression history
if (bytes[0] == B_UP_ARROW) {
PreviousExpression();
return;
}
if (bytes[0] == B_DOWN_ARROW) {
NextExpression();
return;
}
BString current = Text();
// Handle in InputTextView, except B_TAB
if (bytes[0] == '=')
ApplyChanges();
else if (bytes[0] != B_TAB)
InputTextView::KeyDown(bytes, numBytes);
// Pass on to CalcView if this was a label on a key
if (fKeypadLabels.FindFirst(bytes[0]) >= 0)
fCalcView->FlashKey(bytes, numBytes);
else if (bytes[0] == B_BACKSPACE)
fCalcView->FlashKey("BS", 2);
// As soon as something is typed, we are at the end of the expression
// history.
if (current != Text())
fHistoryPos = fPreviousExpressions.CountItems();
// If changes where not applied the value has become a new expression
// note that even if only the left or right arrow keys are pressed the
// fCurrentValue string will be cleared.
if (!fChangesApplied)
fCurrentValue.SetTo("");
else
fChangesApplied = false;
}
void
ExpressionTextView::MouseDown(BPoint where)
{
uint32 buttons;
Window()->CurrentMessage()->FindInt32("buttons", (int32*)&buttons);
if (buttons & B_PRIMARY_MOUSE_BUTTON) {
InputTextView::MouseDown(where);
return;
}
where = ConvertToParent(where);
fCalcView->MouseDown(where);
}
void
ExpressionTextView::GetDragParameters(BMessage* dragMessage,
BBitmap** bitmap, BPoint* point, BHandler** handler)
{
InputTextView::GetDragParameters(dragMessage, bitmap, point, handler);
dragMessage->AddString("be:clip_name", "DeskCalc clipping");
}
void
ExpressionTextView::SetTextRect(BRect rect)
{
InputTextView::SetTextRect(rect);
int32 count = fPreviousExpressions.CountItems();
if (fHistoryPos == count && fCurrentValue.CountChars() > 0)
SetValue(fCurrentValue.String());
}
// #pragma mark -
void
ExpressionTextView::RevertChanges()
{
Clear();
}
void
ExpressionTextView::ApplyChanges()
{
AddExpressionToHistory(Text());
fCalcView->FlashKey("=", 1);
fCalcView->Evaluate();
fChangesApplied = true;
}
// #pragma mark -
void
ExpressionTextView::AddKeypadLabel(const char* label)
{
fKeypadLabels << label;
}
void
ExpressionTextView::SetExpression(const char* expression)
{
SetText(expression);
int32 lastPos = strlen(expression);
Select(lastPos, lastPos);
}
void
ExpressionTextView::SetValue(BString value)
{
// save the value
fCurrentValue = value;
// calculate the width of the string
BFont font;
uint32 mode = B_FONT_ALL;
GetFontAndColor(&font, &mode);
float stringWidth = font.StringWidth(value);
// make the string shorter if it does not fit in the view
float viewWidth = Frame().Width();
if (value.CountChars() > 3 && stringWidth > viewWidth) {
// get the position of the first digit
int32 firstDigit = 0;
if (value[0] == '-')
firstDigit++;
// calculate the value of the exponent
int32 exponent = 0;
int32 offset = value.FindFirst('.');
if (offset == B_ERROR) {
exponent = value.CountChars() - 1 - firstDigit;
value.Insert('.', 1, firstDigit + 1);
} else {
if (offset == firstDigit + 1) {
// if the value is 0.01 or larger then scientific notation
// won't shorten the string
if (value[firstDigit] != '0' || value[firstDigit + 2] != '0'
|| value[firstDigit + 3] != '0') {
exponent = 0;
} else {
// remove the period
value.Remove(offset, 1);
// check for negative exponent value
exponent = 0;
while (value[firstDigit] == '0') {
value.Remove(firstDigit, 1);
exponent--;
}
// add the period
value.Insert('.', 1, firstDigit + 1);
}
} else {
// if the period + 1 digit fits in the view scientific notation
// won't shorten the string
BString temp = value;
temp.Truncate(offset + 2);
stringWidth = font.StringWidth(temp);
if (stringWidth < viewWidth)
exponent = 0;
else {
// move the period
value.Remove(offset, 1);
value.Insert('.', 1, firstDigit + 1);
exponent = offset - (firstDigit + 1);
}
}
}
// add the exponent
offset = value.CountChars() - 1;
if (exponent != 0)
value << "E" << exponent;
// reduce the number of digits until the string fits or can not be
// made any shorter
stringWidth = font.StringWidth(value);
char lastRemovedDigit = '0';
while (offset > firstDigit && stringWidth > viewWidth) {
if (value[offset] != '.')
lastRemovedDigit = value[offset];
value.Remove(offset--, 1);
stringWidth = font.StringWidth(value);
}
// no need to keep the period if no digits follow
if (value[offset] == '.') {
value.Remove(offset, 1);
offset--;
}
// take care of proper rounding of the result
int digit = (int)lastRemovedDigit - '0'; // ascii to int
if (digit >= 5) {
for (; offset >= firstDigit; offset--) {
if (value[offset] == '.')
continue;
digit = (int)(value[offset]) - '0' + 1; // ascii to int + 1
if (digit != 10)
break;
value[offset] = '0';
}
if (digit == 10) {
// carry over, shift the result
if (value[firstDigit+1] == '.') {
value[firstDigit+1] = '0';
value[firstDigit] = '.';
}
value.Insert('1', 1, firstDigit);
// remove the exponent value and the last digit
offset = value.FindFirst('E');
if (offset == B_ERROR)
offset = value.CountChars();
value.Truncate(--offset);
offset--; // offset now points to the last digit
// increase the exponent and add it back to the string
exponent++;
value << 'E' << exponent;
} else {
// increase the current digit value with one
value[offset] = char(digit + 48);
// set offset to last digit
offset = value.FindFirst('E');
if (offset == B_ERROR)
offset = value.CountChars();
offset--;
}
}
// clean up decimal part if we have one
if (value.FindFirst('.') != B_ERROR) {
// remove trailing zeros
while (value[offset] == '0')
value.Remove(offset--, 1);
// no need to keep the period if no digits follow
if (value[offset] == '.')
value.Remove(offset, 1);
}
}
// set the new value
SetExpression(value);
}
void
ExpressionTextView::BackSpace()
{
const char bytes[1] = { B_BACKSPACE };
KeyDown(bytes, 1);
fCalcView->FlashKey("BS", 2);
}
void
ExpressionTextView::Clear()
{
SetText("");
fCalcView->FlashKey("C", 1);
}
// #pragma mark -
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;
if (!fPreviousExpressions.AddItem(item)) {
delete item;
return;
}
while (fPreviousExpressions.CountItems() > kMaxPreviousExpressions)
delete (BString*)fPreviousExpressions.RemoveItem((int32)0);
fHistoryPos = fPreviousExpressions.CountItems();
}
void
ExpressionTextView::PreviousExpression()
{
int32 count = fPreviousExpressions.CountItems();
if (fHistoryPos == count) {
// save current expression
fCurrentExpression = Text();
}
fHistoryPos--;
if (fHistoryPos < 0) {
fHistoryPos = 0;
return;
}
BString* item = (BString*)fPreviousExpressions.ItemAt(fHistoryPos);
if (item)
SetExpression(item->String());
}
void
ExpressionTextView::NextExpression()
{
int32 count = fPreviousExpressions.CountItems();
fHistoryPos++;
if (fHistoryPos == count) {
SetExpression(fCurrentExpression.String());
return;
}
if (fHistoryPos > count) {
fHistoryPos = count;
return;
}
BString* item = (BString*)fPreviousExpressions.ItemAt(fHistoryPos);
if (item)
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;
}