Ticket #9944: deskcalc.patch

File deskcalc.patch, 9.2 KB (added by SoulHunter, 11 years ago)

diff patch

  • 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;  
    7171static const float kMinimumHeightScientific = 200.0f;
    7272static const float kMaximumHeightScientific = 400.0f;
    7373
     74static const char kSeparator = '|';
     75
    7476// basic mode keypad layout (default)
    7577const char *kKeypadDescriptionBasic =
    76     "7   8   9   (   )  \n"
    77     "4   5   6   *   /  \n"
    78     "1   2   3   +   -  \n"
    79     "0   .   BS  =   C  \n";
     78    "7   8   9      (       )  \n"
     79    "4   5   6      *       /  \n"
     80    "1   2   3      +       -  \n"
     81    "0   .   BS|⌫   RET|=   C  \n";
    8082
    8183// scientific mode keypad layout
    8284const 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";
    9193
    9294
    9395enum {
    enum {  
    98100
    99101struct CalcView::CalcKey {
    100102    char        label[8];
     103    char        name[8];
    101104    char        code[8];
    102105    char        keymap[4];
    103106    uint32      flags;
    CalcView::FlashKey(const char* bytes, int32 numBytes)  
    769772{
    770773    BString temp;
    771774    temp.Append(bytes, numBytes);
    772     int32 key = _KeyForLabel(temp.String());
     775    int32 key = _KeyForCode(temp.String());
    773776    if (key >= 0)
    774777        _FlashKey(key, FLAGS_FLASH_KEY);
    775778}
    CalcView::_ParseCalcDesc(const char* keypadDescription)  
    988991    const char* p = keypadDescription;
    989992
    990993    while (*p != 0) {
    991         // copy label
     994        // copy code and label
     995        char* c = key->code;
    992996        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';
    9961015
    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));
    10021020
    10031021        // set keymap
    10041022        if (strlen(key->label) == 1)
    CalcView::_PressKey(int key)  
    10261044    assert(key < (fRows * fColumns));
    10271045    assert(key >= 0);
    10281046
    1029     if (strcmp(fKeypad[key].label, "BS") == 0) {
     1047    if (strcmp(fKeypad[key].code, "BS") == 0) {
    10301048        // 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) {
    10331051        // C means clear
    10341052        fExpressionTextView->Clear();
    1035     } else if (strcmp(fKeypad[key].label, "acos") == 0
    1036         || strcmp(fKeypad[key].label, "asin") == 0
    1037         || strcmp(fKeypad[key].label, "atan") == 0
    1038         || strcmp(fKeypad[key].label, "cbrt") == 0
    1039         || strcmp(fKeypad[key].label, "ceil") == 0
    1040         || strcmp(fKeypad[key].label, "cos") == 0
    1041         || strcmp(fKeypad[key].label, "cosh") == 0
    1042         || strcmp(fKeypad[key].label, "exp") == 0
    1043         || strcmp(fKeypad[key].label, "floor") == 0
    1044         || strcmp(fKeypad[key].label, "log") == 0
    1045         || strcmp(fKeypad[key].label, "ln") == 0
    1046         || strcmp(fKeypad[key].label, "sin") == 0
    1047         || strcmp(fKeypad[key].label, "sinh") == 0
    1048         || strcmp(fKeypad[key].label, "sqrt") == 0
    1049         || strcmp(fKeypad[key].label, "tan") == 0
    1050         || 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);
    10521070        int32 startSelection = 0;
    10531071        int32 endSelection = 0;
    10541072        fExpressionTextView->GetSelection(&startSelection, &endSelection);
    10551073        if (endSelection > startSelection) {
    10561074            // 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);
    10611079            // Put the cursor after the ending paren
    10621080            // Need to cast to BTextView because Select() is protected
    10631081            // in the InputTextView class
    10641082            static_cast<BTextView*>(fExpressionTextView)->Select(
    1065                 endSelection + labelLen + 2, endSelection + labelLen + 2);
     1083                endSelection + codeLen + 2, endSelection + codeLen + 2);
    10661084        } else {
    10671085            // There is no selected text, insert at the cursor location
    1068             fExpressionTextView->Insert(fKeypad[key].label);
     1086            fExpressionTextView->Insert(fKeypad[key].code);
    10691087            fExpressionTextView->Insert("()");
    10701088            // Put the cursor inside the parens so you can enter an argument
    10711089            // Need to cast to BTextView because Select() is protected
    10721090            // in the InputTextView class
    10731091            static_cast<BTextView*>(fExpressionTextView)->Select(
    1074                 endSelection + labelLen + 1, endSelection + labelLen + 1);
     1092                endSelection + codeLen + 1, endSelection + codeLen + 1);
    10751093        }
    10761094    } else {
    10771095        // check for evaluation order
    1078         if (fKeypad[key].code[0] == '\n') {
     1096        if (strcmp(fKeypad[key].code, "RET") == 0) {
    10791097            fExpressionTextView->ApplyChanges();
    10801098        } else {
    10811099            // insert into expression text
    CalcView::_PressKey(int key)  
    10881106
    10891107
    10901108void
    1091 CalcView::_PressKey(const char* label)
     1109CalcView::_PressKey(const char* code)
    10921110{
    1093     int32 key = _KeyForLabel(label);
     1111    int32 key = _KeyForCode(code);
    10941112    if (key >= 0)
    10951113        _PressKey(key);
    10961114}
    10971115
    10981116
    10991117int32
    1100 CalcView::_KeyForLabel(const char* label) const
     1118CalcView::_KeyForCode(const char* code) const
    11011119{
    11021120    int keys = fRows * fColumns;
    11031121    for (int i = 0; i < keys; i++) {
    1104         if (strcmp(fKeypad[i].label, label) == 0) {
     1122        if (strcmp(fKeypad[i].code, code) == 0)
    11051123            return i;
    1106         }
    11071124    }
    11081125    return -1;
    11091126}
  • 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 {  
    102102            void                _ParseCalcDesc(const char* keypadDescription);
    103103
    104104            void                _PressKey(int key);
    105             void                _PressKey(const char* label);
    106             int32               _KeyForLabel(const char* label) const;
     105            void                _PressKey(const char* code);
     106            int32               _KeyForCode(const char* code) const;
    107107            void                _FlashKey(int32 key, uint32 flashFlags);
    108108            void                _AudioFeedback(bool inBackGround);
    109109
  • 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)  
    7979        return;
    8080    }
    8181    BString current = Text();
    82 
    8382    // Handle in InputTextView, except B_TAB
    8483    if (bytes[0] == '=')
    8584        ApplyChanges();
    8685    else if (bytes[0] != B_TAB)
    8786        InputTextView::KeyDown(bytes, numBytes);
    88 
    8987    // Pass on to CalcView if this was a label on a key
    9088    if (fKeypadLabels.FindFirst(bytes[0]) >= 0)
    9189        fCalcView->FlashKey(bytes, numBytes);
    9290    else if (bytes[0] == B_BACKSPACE)
    93         fCalcView->FlashKey("BS", 2);
     91        fCalcView->FlashKey("BS", 2);       
    9492
    9593    // As soon as something is typed, we are at the end of the expression
    9694    // history.
    void  
    160158ExpressionTextView::ApplyChanges()
    161159{
    162160    AddExpressionToHistory(Text());
    163     fCalcView->FlashKey("=", 1);
     161    fCalcView->FlashKey("RET", 3);
    164162    fCalcView->Evaluate();
    165163    fChangesApplied = true;
    166164}
  • 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)  
    6666    if (numBytes > 0) {
    6767        switch (bytes[0]) {
    6868            case B_ESCAPE:
     69            case 'c':
     70            case 'C':
    6971                // revert any typing changes
    7072                RevertChanges();
    7173                break;