Ticket #9944: deskcalc.patch
File deskcalc.patch, 9.2 KB (added by , 11 years ago) |
---|
-
src/apps/deskcalc/CalcView.cpp
diff --git src/apps/deskcalc/CalcView.cpp src/apps/deskcalc/CalcView.cpp index b0ceda2..f312062 100644
static const float kMaximumWidthScientific = 400.0f; 71 71 static const float kMinimumHeightScientific = 200.0f; 72 72 static const float kMaximumHeightScientific = 400.0f; 73 73 74 static const char kSeparator = '|'; 75 74 76 // basic mode keypad layout (default) 75 77 const char *kKeypadDescriptionBasic = 76 "7 8 9 () \n"77 "4 5 6 */ \n"78 "1 2 3 +- \n"79 "0 . BS 78 "7 8 9 ( ) \n" 79 "4 5 6 * / \n" 80 "1 2 3 + - \n" 81 "0 . BS|⌫ RET|= C \n"; 80 82 81 83 // scientific mode keypad layout 82 84 const char *kKeypadDescriptionScientific = 83 "ln sin cos tan π\n"84 "log asin acos atan sqrt\n"85 "exp sinh cosh tanh cbrt\n"86 "! ceil floor E ^\n"87 "7 8 9 ( )\n"88 "4 5 6 * /\n"89 "1 2 3 + -\n"90 "0 . BS =C \n";85 "ln|Ln sin cos tan π \n" 86 "log asin|sin⁻¹ acos|cos⁻¹ atan|tan⁻¹ sqrt|√ \n" 87 "exp sinh cosh tanh cbrt|∛ \n" 88 "! ceil floor E ^ \n" 89 "7 8 9 ( ) \n" 90 "4 5 6 * / \n" 91 "1 2 3 + - \n" 92 "0 . BS|⌫ RET|= C \n"; 91 93 92 94 93 95 enum { … … enum { 98 100 99 101 struct CalcView::CalcKey { 100 102 char label[8]; 103 char name[8]; 101 104 char code[8]; 102 105 char keymap[4]; 103 106 uint32 flags; … … CalcView::FlashKey(const char* bytes, int32 numBytes) 769 772 { 770 773 BString temp; 771 774 temp.Append(bytes, numBytes); 772 int32 key = _KeyFor Label(temp.String());775 int32 key = _KeyForCode(temp.String()); 773 776 if (key >= 0) 774 777 _FlashKey(key, FLAGS_FLASH_KEY); 775 778 } … … CalcView::_ParseCalcDesc(const char* keypadDescription) 988 991 const char* p = keypadDescription; 989 992 990 993 while (*p != 0) { 991 // copy label 994 // copy code and label 995 char* c = key->code; 992 996 char* l = key->label; 993 while (!isspace(*p)) 994 *l++ = *p++; 995 *l = '\0'; 997 998 // a bool to tell that a different label is used 999 bool otherLabel = false; 1000 1001 while (!isspace(*p)){ 1002 1003 if(p[0] == kSeparator){ 1004 otherLabel = true ; 1005 ++p; 1006 while(!isspace(*p)){ 1007 *l++ = *p++ ; 1008 } 1009 } else { 1010 *c++ = *p++; 1011 } 1012 } 1013 1014 *c = '\0'; 996 1015 997 // set code 998 if (strcmp(key->label, "=") == 0) 999 strlcpy(key->code, "\n", sizeof(key->code)); 1000 else 1001 strlcpy(key->code, key->label, sizeof(key->code)); 1016 if (otherLabel) 1017 *l ='\0'; 1018 else 1019 strlcpy(key->label, key->code, sizeof(key->label)); 1002 1020 1003 1021 // set keymap 1004 1022 if (strlen(key->label) == 1) … … CalcView::_PressKey(int key) 1026 1044 assert(key < (fRows * fColumns)); 1027 1045 assert(key >= 0); 1028 1046 1029 if (strcmp(fKeypad[key]. label, "BS") == 0) {1047 if (strcmp(fKeypad[key].code, "BS") == 0) { 1030 1048 // BS means backspace 1031 fExpressionTextView->BackSpace(); 1032 } else if (strcmp(fKeypad[key]. label, "C") == 0) {1049 fExpressionTextView->BackSpace(); 1050 } else if (strcmp(fKeypad[key].code, "C") == 0) { 1033 1051 // C means clear 1034 1052 fExpressionTextView->Clear(); 1035 } else if (strcmp(fKeypad[key]. label, "acos") == 01036 || strcmp(fKeypad[key]. label, "asin") == 01037 || strcmp(fKeypad[key]. label, "atan") == 01038 || strcmp(fKeypad[key]. label, "cbrt") == 01039 || strcmp(fKeypad[key]. label, "ceil") == 01040 || strcmp(fKeypad[key]. label, "cos") == 01041 || strcmp(fKeypad[key]. label, "cosh") == 01042 || strcmp(fKeypad[key]. label, "exp") == 01043 || strcmp(fKeypad[key]. label, "floor") == 01044 || strcmp(fKeypad[key]. label, "log") == 01045 || strcmp(fKeypad[key]. label, "ln") == 01046 || strcmp(fKeypad[key]. label, "sin") == 01047 || strcmp(fKeypad[key]. label, "sinh") == 01048 || strcmp(fKeypad[key]. label, "sqrt") == 01049 || strcmp(fKeypad[key]. label, "tan") == 01050 || strcmp(fKeypad[key]. label, "tanh") == 0) {1051 int32 labelLen = strlen(fKeypad[key].label);1053 } else if (strcmp(fKeypad[key].code, "acos") == 0 1054 || strcmp(fKeypad[key].code, "asin") == 0 1055 || strcmp(fKeypad[key].code, "atan") == 0 1056 || strcmp(fKeypad[key].code, "cbrt") == 0 1057 || strcmp(fKeypad[key].code, "ceil") == 0 1058 || strcmp(fKeypad[key].code, "cos") == 0 1059 || strcmp(fKeypad[key].code, "cosh") == 0 1060 || strcmp(fKeypad[key].code, "exp") == 0 1061 || strcmp(fKeypad[key].code, "floor") == 0 1062 || strcmp(fKeypad[key].code, "log") == 0 1063 || strcmp(fKeypad[key].code, "ln") == 0 1064 || strcmp(fKeypad[key].code, "sin") == 0 1065 || strcmp(fKeypad[key].code, "sinh") == 0 1066 || strcmp(fKeypad[key].code, "sqrt") == 0 1067 || strcmp(fKeypad[key].code, "tan") == 0 1068 || strcmp(fKeypad[key].code, "tanh") == 0) { 1069 int32 codeLen = strlen(fKeypad[key].code); 1052 1070 int32 startSelection = 0; 1053 1071 int32 endSelection = 0; 1054 1072 fExpressionTextView->GetSelection(&startSelection, &endSelection); 1055 1073 if (endSelection > startSelection) { 1056 1074 // There is selected text, put it inbetween the parens 1057 fExpressionTextView->Insert(startSelection, fKeypad[key]. label,1058 labelLen);1059 fExpressionTextView->Insert(startSelection + labelLen, "(", 1);1060 fExpressionTextView->Insert(endSelection + labelLen + 1, ")", 1);1075 fExpressionTextView->Insert(startSelection, fKeypad[key].code, 1076 codeLen); 1077 fExpressionTextView->Insert(startSelection + codeLen, "(", 1); 1078 fExpressionTextView->Insert(endSelection + codeLen + 1, ")", 1); 1061 1079 // Put the cursor after the ending paren 1062 1080 // Need to cast to BTextView because Select() is protected 1063 1081 // in the InputTextView class 1064 1082 static_cast<BTextView*>(fExpressionTextView)->Select( 1065 endSelection + labelLen + 2, endSelection + labelLen + 2);1083 endSelection + codeLen + 2, endSelection + codeLen + 2); 1066 1084 } else { 1067 1085 // There is no selected text, insert at the cursor location 1068 fExpressionTextView->Insert(fKeypad[key]. label);1086 fExpressionTextView->Insert(fKeypad[key].code); 1069 1087 fExpressionTextView->Insert("()"); 1070 1088 // Put the cursor inside the parens so you can enter an argument 1071 1089 // Need to cast to BTextView because Select() is protected 1072 1090 // in the InputTextView class 1073 1091 static_cast<BTextView*>(fExpressionTextView)->Select( 1074 endSelection + labelLen + 1, endSelection + labelLen + 1);1092 endSelection + codeLen + 1, endSelection + codeLen + 1); 1075 1093 } 1076 1094 } else { 1077 1095 // check for evaluation order 1078 if ( fKeypad[key].code[0] == '\n') {1096 if (strcmp(fKeypad[key].code, "RET") == 0) { 1079 1097 fExpressionTextView->ApplyChanges(); 1080 1098 } else { 1081 1099 // insert into expression text … … CalcView::_PressKey(int key) 1088 1106 1089 1107 1090 1108 void 1091 CalcView::_PressKey(const char* label)1109 CalcView::_PressKey(const char* code) 1092 1110 { 1093 int32 key = _KeyFor Label(label);1111 int32 key = _KeyForCode(code); 1094 1112 if (key >= 0) 1095 1113 _PressKey(key); 1096 1114 } 1097 1115 1098 1116 1099 1117 int32 1100 CalcView::_KeyFor Label(const char* label) const1118 CalcView::_KeyForCode(const char* code) const 1101 1119 { 1102 1120 int keys = fRows * fColumns; 1103 1121 for (int i = 0; i < keys; i++) { 1104 if (strcmp(fKeypad[i]. label, label) == 0) {1122 if (strcmp(fKeypad[i].code, code) == 0) 1105 1123 return i; 1106 }1107 1124 } 1108 1125 return -1; 1109 1126 } -
src/apps/deskcalc/CalcView.h
diff --git src/apps/deskcalc/CalcView.h src/apps/deskcalc/CalcView.h index a4a45bc..07b9115 100644
class CalcView : public BView { 102 102 void _ParseCalcDesc(const char* keypadDescription); 103 103 104 104 void _PressKey(int key); 105 void _PressKey(const char* label);106 int32 _KeyFor Label(const char* label) const;105 void _PressKey(const char* code); 106 int32 _KeyForCode(const char* code) const; 107 107 void _FlashKey(int32 key, uint32 flashFlags); 108 108 void _AudioFeedback(bool inBackGround); 109 109 -
src/apps/deskcalc/ExpressionTextView.cpp
diff --git src/apps/deskcalc/ExpressionTextView.cpp src/apps/deskcalc/ExpressionTextView.cpp index e303731..af6fb5e 100644
ExpressionTextView::KeyDown(const char* bytes, int32 numBytes) 79 79 return; 80 80 } 81 81 BString current = Text(); 82 83 82 // Handle in InputTextView, except B_TAB 84 83 if (bytes[0] == '=') 85 84 ApplyChanges(); 86 85 else if (bytes[0] != B_TAB) 87 86 InputTextView::KeyDown(bytes, numBytes); 88 89 87 // Pass on to CalcView if this was a label on a key 90 88 if (fKeypadLabels.FindFirst(bytes[0]) >= 0) 91 89 fCalcView->FlashKey(bytes, numBytes); 92 90 else if (bytes[0] == B_BACKSPACE) 93 fCalcView->FlashKey("BS", 2); 91 fCalcView->FlashKey("BS", 2); 94 92 95 93 // As soon as something is typed, we are at the end of the expression 96 94 // history. … … void 160 158 ExpressionTextView::ApplyChanges() 161 159 { 162 160 AddExpressionToHistory(Text()); 163 fCalcView->FlashKey(" =", 1);161 fCalcView->FlashKey("RET", 3); 164 162 fCalcView->Evaluate(); 165 163 fChangesApplied = true; 166 164 } -
src/apps/deskcalc/InputTextView.cpp
diff --git src/apps/deskcalc/InputTextView.cpp src/apps/deskcalc/InputTextView.cpp index 9bd60a2..c733d15 100644
InputTextView::KeyDown(const char* bytes, int32 numBytes) 66 66 if (numBytes > 0) { 67 67 switch (bytes[0]) { 68 68 case B_ESCAPE: 69 case 'c': 70 case 'C': 69 71 // revert any typing changes 70 72 RevertChanges(); 71 73 break;