* Repurposed the calculator icon. The icon only shows when the keypad is hidden,

so someone using it to trigger evaluation is highly unlikely, since he can
  just press return/enter. But when DeskCalc is embedded into the Desktop, it
  is convenient to clear the text view. (Clicking anywhere in DeskCalc already
  gives the text view focus.)
* Automatic whitespace cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30697 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2009-05-10 11:26:03 +00:00
parent 1c0f89a7fd
commit b4dce77e73
+61 -51
View File
@@ -82,7 +82,7 @@ struct CalcView::CalcKey {
CalcView *CalcView::Instantiate(BMessage *archive) CalcView *CalcView::Instantiate(BMessage *archive)
{ {
if (!validate_instantiation(archive, "CalcView")) if (!validate_instantiation(archive, "CalcView"))
return NULL; return NULL;
return new CalcView(archive); return new CalcView(archive);
} }
@@ -130,7 +130,7 @@ CalcView::CalcView(BRect frame, rgb_color rgbBaseColor, BMessage *settings)
// parse calculator description // parse calculator description
_ParseCalcDesc(fKeypadDescription); _ParseCalcDesc(fKeypadDescription);
// colorize based on base color. // colorize based on base color.
_Colorize(); _Colorize();
@@ -182,7 +182,7 @@ CalcView::CalcView(BMessage* archive)
_LoadSettings(archive); _LoadSettings(archive);
// replicant means size and window limit dont need changes // replicant means size and window limit dont need changes
fShowKeypad = fOptions->show_keypad; fShowKeypad = fOptions->show_keypad;
// create pop-up menu system // create pop-up menu system
_CreatePopUpMenu(); _CreatePopUpMenu();
@@ -225,28 +225,28 @@ CalcView::MessageReceived(BMessage* message)
// act on posted message type // act on posted message type
switch (message->what) { switch (message->what) {
// handle "cut" // handle "cut"
case B_CUT: case B_CUT:
Cut(); Cut();
break; break;
// handle copy // handle copy
case B_COPY: case B_COPY:
Copy(); Copy();
break; break;
// handle paste // handle paste
case B_PASTE: case B_PASTE:
// access system clipboard // access system clipboard
if (be_clipboard->Lock()) { if (be_clipboard->Lock()) {
BMessage *clipper = be_clipboard->Data(); BMessage *clipper = be_clipboard->Data();
//clipper->PrintToStream(); //clipper->PrintToStream();
Paste(clipper); Paste(clipper);
be_clipboard->Unlock(); be_clipboard->Unlock();
} }
break; break;
// (replicant) about box requested // (replicant) about box requested
case B_ABOUT_REQUESTED: case B_ABOUT_REQUESTED:
AboutRequested(); AboutRequested();
@@ -342,10 +342,10 @@ CalcView::Draw(BRect updateRect)
updateRect, fBaseColor, flags); updateRect, fBaseColor, flags);
} else { } else {
BeginLineArray(8); BeginLineArray(8);
rgb_color lightShadow = tint_color(fBaseColor, B_DARKEN_1_TINT); rgb_color lightShadow = tint_color(fBaseColor, B_DARKEN_1_TINT);
rgb_color darkShadow = tint_color(fBaseColor, B_DARKEN_3_TINT); rgb_color darkShadow = tint_color(fBaseColor, B_DARKEN_3_TINT);
AddLine(BPoint(expressionRect.left, expressionRect.bottom), AddLine(BPoint(expressionRect.left, expressionRect.bottom),
BPoint(expressionRect.left, expressionRect.top), BPoint(expressionRect.left, expressionRect.top),
lightShadow); lightShadow);
@@ -358,7 +358,7 @@ CalcView::Draw(BRect updateRect)
AddLine(BPoint(expressionRect.left + 1, expressionRect.bottom), AddLine(BPoint(expressionRect.left + 1, expressionRect.bottom),
BPoint(expressionRect.right - 1, expressionRect.bottom), BPoint(expressionRect.right - 1, expressionRect.bottom),
fLightColor); fLightColor);
expressionRect.InsetBy(1, 1); expressionRect.InsetBy(1, 1);
AddLine(BPoint(expressionRect.left, expressionRect.bottom), AddLine(BPoint(expressionRect.left, expressionRect.bottom),
BPoint(expressionRect.left, expressionRect.top), BPoint(expressionRect.left, expressionRect.top),
@@ -372,7 +372,7 @@ CalcView::Draw(BRect updateRect)
AddLine(BPoint(expressionRect.left + 1, expressionRect.bottom), AddLine(BPoint(expressionRect.left + 1, expressionRect.bottom),
BPoint(expressionRect.right - 1, expressionRect.bottom), BPoint(expressionRect.right - 1, expressionRect.bottom),
fBaseColor); fBaseColor);
EndLineArray(); EndLineArray();
} }
} }
@@ -441,10 +441,10 @@ CalcView::Draw(BRect updateRect)
// paint keypad b/g // paint keypad b/g
SetHighColor(fBaseColor); SetHighColor(fBaseColor);
FillRect(updateRect & keypadRect); FillRect(updateRect & keypadRect);
// render key main grid // render key main grid
BeginLineArray(((fColums + fRows) << 1) + 1); BeginLineArray(((fColums + fRows) << 1) + 1);
// render cols // render cols
AddLine(BPoint(0.0, sizeDisp), AddLine(BPoint(0.0, sizeDisp),
BPoint(0.0, fHeight), BPoint(0.0, fHeight),
@@ -460,7 +460,7 @@ CalcView::Draw(BRect updateRect)
AddLine(BPoint(fColums * sizeCol, sizeDisp), AddLine(BPoint(fColums * sizeCol, sizeDisp),
BPoint(fColums * sizeCol, fHeight), BPoint(fColums * sizeCol, fHeight),
fDarkColor); fDarkColor);
// render rows // render rows
for (int row = 0; row < fRows; row++) { for (int row = 0; row < fRows; row++) {
AddLine(BPoint(0.0, sizeDisp + row * sizeRow - 1.0), AddLine(BPoint(0.0, sizeDisp + row * sizeRow - 1.0),
@@ -473,10 +473,10 @@ CalcView::Draw(BRect updateRect)
AddLine(BPoint(0.0, sizeDisp + fRows * sizeRow), AddLine(BPoint(0.0, sizeDisp + fRows * sizeRow),
BPoint(fWidth, sizeDisp + fRows * sizeRow), BPoint(fWidth, sizeDisp + fRows * sizeRow),
fDarkColor); fDarkColor);
// main grid complete // main grid complete
EndLineArray(); EndLineArray();
// render key symbols // render key symbols
float halfSizeCol = sizeCol * 0.5f; float halfSizeCol = sizeCol * 0.5f;
SetHighColor(fButtonTextColor); SetHighColor(fButtonTextColor);
@@ -502,8 +502,11 @@ void
CalcView::MouseDown(BPoint point) CalcView::MouseDown(BPoint point)
{ {
// ensure this view is the current focus // ensure this view is the current focus
if (!IsFocus()) if (!fExpressionTextView->IsFocus()) {
// Call our version of MakeFocus(), since that will also apply the
// num_lock setting.
MakeFocus(); MakeFocus();
}
// read mouse buttons state // read mouse buttons state
int32 buttons = 0; int32 buttons = 0;
@@ -513,8 +516,9 @@ CalcView::MouseDown(BPoint point)
if ((B_PRIMARY_MOUSE_BUTTON & buttons) == 0) { if ((B_PRIMARY_MOUSE_BUTTON & buttons) == 0) {
BMenuItem* selected; BMenuItem* selected;
if ((selected = fPopUpMenu->Go(ConvertToScreen(point))) != NULL if ((selected = fPopUpMenu->Go(ConvertToScreen(point))) != NULL
&& selected->Message() != NULL) && selected->Message() != NULL) {
MessageReceived(selected->Message()); Window()->PostMessage(selected->Message(), this);
}
return; return;
} }
@@ -524,7 +528,7 @@ CalcView::MouseDown(BPoint point)
bounds.left = bounds.right - fCalcIcon->Bounds().Width(); bounds.left = bounds.right - fCalcIcon->Bounds().Width();
if (bounds.Contains(point)) { if (bounds.Contains(point)) {
// user clicked on calculator icon // user clicked on calculator icon
fExpressionTextView->ApplyChanges(); fExpressionTextView->Clear();
} }
} }
return; return;
@@ -534,15 +538,15 @@ CalcView::MouseDown(BPoint point)
float sizeDisp = fHeight * kDisplayScaleY; float sizeDisp = fHeight * kDisplayScaleY;
float sizeCol = fWidth / (float)fColums; float sizeCol = fWidth / (float)fColums;
float sizeRow = (fHeight - sizeDisp) / (float)fRows; float sizeRow = (fHeight - sizeDisp) / (float)fRows;
// calculate location within grid // calculate location within grid
int gridCol = (int)floorf(point.x / sizeCol); int gridCol = (int)floorf(point.x / sizeCol);
int gridRow = (int)floorf((point.y - sizeDisp) / sizeRow); int gridRow = (int)floorf((point.y - sizeDisp) / sizeRow);
// check limits // check limits
if ((gridCol >= 0) && (gridCol < fColums) && if ((gridCol >= 0) && (gridCol < fColums) &&
(gridRow >= 0) && (gridRow < fRows)) { (gridRow >= 0) && (gridRow < fRows)) {
// process key press // process key press
int key = gridRow * fColums + gridCol; int key = gridRow * fColums + gridCol;
_FlashKey(key, FLAGS_MOUSE_DOWN); _FlashKey(key, FLAGS_MOUSE_DOWN);
@@ -557,6 +561,9 @@ CalcView::MouseDown(BPoint point)
void void
CalcView::MouseUp(BPoint point) CalcView::MouseUp(BPoint point)
{ {
if (!fShowKeypad)
return;
int keys = fRows * fColums; int keys = fRows * fColums;
for (int i = 0; i < keys; i++) { for (int i = 0; i < keys; i++) {
if (fKeypad[i].flags & FLAGS_MOUSE_DOWN) { if (fKeypad[i].flags & FLAGS_MOUSE_DOWN) {
@@ -571,41 +578,41 @@ void
CalcView::KeyDown(const char *bytes, int32 numBytes) CalcView::KeyDown(const char *bytes, int32 numBytes)
{ {
// if single byte character... // if single byte character...
if (numBytes == 1) { if (numBytes == 1) {
//printf("Key pressed: %c\n", bytes[0]); //printf("Key pressed: %c\n", bytes[0]);
switch (bytes[0]) { switch (bytes[0]) {
case B_ENTER: case B_ENTER:
// translate to evaluate key // translate to evaluate key
_PressKey("="); _PressKey("=");
break; break;
case B_LEFT_ARROW: case B_LEFT_ARROW:
case B_BACKSPACE: case B_BACKSPACE:
// translate to backspace key // translate to backspace key
_PressKey("BS"); _PressKey("BS");
break; break;
case B_SPACE: case B_SPACE:
case B_ESCAPE: case B_ESCAPE:
case 'c': case 'c':
// translate to clear key // translate to clear key
_PressKey("C"); _PressKey("C");
break; break;
// bracket translation // bracket translation
case '[': case '[':
case '{': case '{':
_PressKey("("); _PressKey("(");
break; break;
case ']': case ']':
case '}': case '}':
_PressKey(")"); _PressKey(")");
break; break;
default: { default: {
// scan the keymap array for match // scan the keymap array for match
int keys = fRows * fColums; int keys = fRows * fColums;
@@ -628,8 +635,8 @@ CalcView::MakeFocus(bool focused)
if (focused) { if (focused) {
// set num lock // set num lock
if (fOptions->auto_num_lock) { if (fOptions->auto_num_lock) {
set_keyboard_locks(B_NUM_LOCK | set_keyboard_locks(B_NUM_LOCK
(modifiers() & (B_CAPS_LOCK | B_SCROLL_LOCK))); | (modifiers() & (B_CAPS_LOCK | B_SCROLL_LOCK)));
} }
} }
@@ -732,16 +739,16 @@ CalcView::Copy()
expression.String(), expression.String(),
expression.Length()); expression.Length());
//clipper->PrintToStream(); //clipper->PrintToStream();
be_clipboard->Commit(); be_clipboard->Commit();
be_clipboard->Unlock(); be_clipboard->Unlock();
} }
} }
void void
CalcView::Paste(BMessage *message) CalcView::Paste(BMessage *message)
{ {
// handle color drops first // handle color drops first
// read incoming color // read incoming color
const rgb_color* dropColor = NULL; const rgb_color* dropColor = NULL;
ssize_t dataSize; ssize_t dataSize;
@@ -750,14 +757,14 @@ CalcView::Paste(BMessage *message)
(const void**)&dropColor, (const void**)&dropColor,
&dataSize) == B_OK &dataSize) == B_OK
&& dataSize == sizeof(rgb_color)) { && dataSize == sizeof(rgb_color)) {
// calculate view relative drop point // calculate view relative drop point
BPoint dropPoint = ConvertFromScreen(message->DropPoint()); BPoint dropPoint = ConvertFromScreen(message->DropPoint());
// calculate current keypad area // calculate current keypad area
float sizeDisp = fHeight * kDisplayScaleY; float sizeDisp = fHeight * kDisplayScaleY;
BRect keypadRect(0.0, sizeDisp, fWidth, fHeight); BRect keypadRect(0.0, sizeDisp, fWidth, fHeight);
// check location of color drop // check location of color drop
if (keypadRect.Contains(dropPoint) && dropColor) { if (keypadRect.Contains(dropPoint) && dropColor) {
fBaseColor = *dropColor; fBaseColor = *dropColor;
@@ -802,7 +809,7 @@ CalcView::_LoadSettings(BMessage* archive)
fColums = 5; fColums = 5;
if (archive->FindInt16("rows", &fRows) < B_OK) if (archive->FindInt16("rows", &fRows) < B_OK)
fRows = 4; fRows = 4;
// read color scheme // read color scheme
const rgb_color* color; const rgb_color* color;
ssize_t size; ssize_t size;
@@ -814,7 +821,7 @@ CalcView::_LoadSettings(BMessage* archive)
} else { } else {
fBaseColor = *color; fBaseColor = *color;
} }
if (archive->FindData("rgbDisplay", B_RGB_COLOR_TYPE, if (archive->FindData("rgbDisplay", B_RGB_COLOR_TYPE,
(const void**)&color, &size) < B_OK (const void**)&color, &size) < B_OK
|| size != sizeof(rgb_color)) { || size != sizeof(rgb_color)) {
@@ -823,7 +830,7 @@ CalcView::_LoadSettings(BMessage* archive)
} else { } else {
fExpressionBGColor = *color; fExpressionBGColor = *color;
} }
// load options // load options
fOptions->LoadSettings(archive); fOptions->LoadSettings(archive);
@@ -841,7 +848,7 @@ CalcView::_LoadSettings(BMessage* archive)
// parse calculator description // parse calculator description
_ParseCalcDesc(fKeypadDescription); _ParseCalcDesc(fKeypadDescription);
// colorize based on base color. // colorize based on base color.
_Colorize(); _Colorize();
@@ -859,7 +866,7 @@ CalcView::SaveSettings(BMessage* archive) const
ret = archive->AddInt16("cols", fColums); ret = archive->AddInt16("cols", fColums);
if (ret == B_OK) if (ret == B_OK)
ret = archive->AddInt16("rows", fRows); ret = archive->AddInt16("rows", fRows);
// record color scheme // record color scheme
if (ret == B_OK) if (ret == B_OK)
ret = archive->AddData("rgbBaseColor", B_RGB_COLOR_TYPE, ret = archive->AddData("rgbBaseColor", B_RGB_COLOR_TYPE,
@@ -967,7 +974,7 @@ CalcView::_ParseCalcDesc(const char* keypadDescription)
// will forward the respective KeyDown event to us // will forward the respective KeyDown event to us
fExpressionTextView->AddKeypadLabel(key->label); fExpressionTextView->AddKeypadLabel(key->label);
// advance // advance
while (isspace(*p)) while (isspace(*p))
++p; ++p;
key++; key++;
@@ -1026,6 +1033,9 @@ CalcView::_KeyForLabel(const char *label) const
void void
CalcView::_FlashKey(int32 key, uint32 flashFlags) CalcView::_FlashKey(int32 key, uint32 flashFlags)
{ {
if (!fShowKeypad)
return;
if (flashFlags != 0) if (flashFlags != 0)
fKeypad[key].flags |= flashFlags; fKeypad[key].flags |= flashFlags;
else else
@@ -1065,7 +1075,7 @@ CalcView::_Colorize()
fLightColor.green = (uint8)(fBaseColor.green * 1.25); fLightColor.green = (uint8)(fBaseColor.green * 1.25);
fLightColor.blue = (uint8)(fBaseColor.blue * 1.25); fLightColor.blue = (uint8)(fBaseColor.blue * 1.25);
fLightColor.alpha = 255; fLightColor.alpha = 255;
fDarkColor.red = (uint8)(fBaseColor.red * 0.75); fDarkColor.red = (uint8)(fBaseColor.red * 0.75);
fDarkColor.green = (uint8)(fBaseColor.green * 0.75); fDarkColor.green = (uint8)(fBaseColor.green * 0.75);
fDarkColor.blue = (uint8)(fBaseColor.blue * 0.75); fDarkColor.blue = (uint8)(fBaseColor.blue * 0.75);
@@ -1167,7 +1177,7 @@ CalcView::_ShowKeypad(bool show)
} }
} }
void void
CalcView::_FetchAppIcon(BBitmap* into) CalcView::_FetchAppIcon(BBitmap* into)
{ {