home *** CD-ROM | disk | FTP | other *** search
- #include <ctype.h>
- #include <math.h>
- #include <stdio.h>
- #include <owl\applicat.h>
- #include <owl\button.h>
- #include <owl\edit.h>
- #include <owl\framewin.h>
- #include <owl\static.h>
- #include <owl\window.h>
- #include <owl\window.rh>
-
- #include "mrcalc.h"
-
- class TCalcWindow : public TWindow
- {
- public:
- TCalcWindow(TWindow *parent = 0);
- ~TCalcWindow();
-
- protected:
- virtual void SetupWindow();
- virtual void EvLButtonDown(UINT modKeys, TPoint &point);
-
- void CmCalc();
- void CmStore();
- void CmExit();
-
- private:
- TStatic *ErrMsgLabel;
- TEdit *Operand1, *Operator, *Operand2, *Result,
- *ErrMsg, *Variable;
- TButton *Store;
-
- double get_number(TEdit *edit);
- double get_var(int line);
- void put_var(double val);
-
- DECLARE_RESPONSE_TABLE(TCalcWindow);
- };
- DEFINE_RESPONSE_TABLE1(TCalcWindow, TWindow)
- EV_WM_LBUTTONDOWN,
- EV_COMMAND(CM_EXIT, CmExit),
- EV_BN_CLICKED(IDB_CALC, CmCalc),
- EV_BN_CLICKED(IDB_STORE, CmStore),
- EV_BN_CLICKED(IDB_EXIT, CmExit),
- END_RESPONSE_TABLE;
-
- TCalcWindow::TCalcWindow(TWindow *parent)
- {
- Init(parent, 0, 0);
-
- int wlblspacing = 40,
- hlblspacing = 5,
- wlbl = 100,
- hlbl = 20,
- wbox = 100,
- hbox = 30,
- wboxspacing = 40,
- hboxspacing = 40,
- wbtn = 80,
- hbtn = 30,
- wbtnspacing = 30;
- int wlongbox = 4 * (wbox + wboxspacing);
- int wvarbox = 2 * wbox,
- hvarbox = 3 * hbox;
- int x0 = 20, y0 = 30;
- int x, y;
-
- // First, create the labels for the edit text boxes.
- //
- x = x0;
- y = y0;
- new TStatic(this, -1, "Operand1", x, y, wlbl, hlbl);
- x += wlbl + wlblspacing;
- new TStatic(this, -1, "Operator", x, y, wlbl, hlbl);
- x += wlbl + wlblspacing;
- new TStatic(this, -1, "Operand2", x, y, wlbl, hlbl);
- x += wlbl + wlblspacing;
- new TStatic(this, -1, "Result", x, y, wlbl, hlbl);
- x += wlbl + wlblspacing;
-
- // Now create the edit text boxes
- //
- x = x0;
- y += hlbl + hlblspacing;
- if (NULL != (Operand1 = new TEdit(this, IDE_OPERAND1, "",
- x, y, wbox, hbox)))
- Operand1->Attr.Style |= ES_UPPERCASE;
- x += wbox + wboxspacing;
- if (NULL != (Operator = new TEdit(this, IDE_OPERATOR, "",
- x, y, wbox, hbox)))
- Operator->Attr.Style |= ES_UPPERCASE;
- x += wbox + wboxspacing;
- if (NULL != (Operand2 = new TEdit(this, IDE_OPERAND2, "",
- x, y, wbox, hbox)))
- Operand2->Attr.Style |= ES_UPPERCASE;
- x += wbox + wboxspacing;
- Result = new TEdit(this, IDE_RESULT, "", x, y, wbox, hbox);
- x += wbox + wboxspacing;
-
- // Now create the label and box for the error message
- //
- x = x0;
- y += hbox + hboxspacing;
- ErrMsgLabel = new TStatic( this, -1, "Error Message", x, y,
- wlbl, hlbl );
- y += hlbl + hlblspacing;
- ErrMsg = new TEdit(this, IDE_ERRMSG, "", x, y, wlongbox, hbox);
-
- // Create the label and box for the single-letter
- // variable selection
- //
- y += hbox + hboxspacing;
- new TStatic(this, -1, "Variables", x, y, wlbl, hlbl);
- y += hlbl + hlblspacing;
- char str[6 * ('Z' - 'A' + 1) + 1];
- char *p = str;
- for (char ch = 'A'; ch <= 'Z'; ++ch)
- p += sprintf(p, "%c: 0\r\n", ch);
- Variable = new TEdit(this, IDE_VARIABLE, str, x, y,
- wvarbox, hvarbox, 0, TRUE );
-
- // Finally create some buttons
- //
- x += wvarbox + wbtnspacing;
- new TButton(this, IDB_CALC, "Calc", x, y, wbtn, hbtn);
- x += wbtn + wbtnspacing;
- Store = new TButton(this, IDB_STORE, "Store", x, y, wbtn, hbtn);
- x += wbtn + wbtnspacing;
- new TButton(this, IDB_EXIT, "Exit", x, y, wbtn, hbtn);
- }
-
- TCalcWindow::~TCalcWindow()
- {
- }
-
- void TCalcWindow::SetupWindow()
- {
- TWindow::SetupWindow(); // Initialize the visual element
-
- // Keep the users out of the destination areas.
- //
- if (Result)
- Result->SetReadOnly(TRUE);
- if (ErrMsg)
- ErrMsg->SetReadOnly(TRUE);
- if (Variable)
- Variable->SetReadOnly(TRUE);
- }
-
- void TCalcWindow::EvLButtonDown(UINT /*modKeys*/, TPoint &point)
- {
- if ( ErrMsgLabel
- && (ErrMsgLabel->HWindow == ChildWindowFromPoint(point)) )
- {
- if (ErrMsg)
- ErrMsg->Clear();
- if (Store)
- Store->EnableWindow(TRUE);
- }
- }
-
- double TCalcWindow::get_number(TEdit *edit)
- {
- double rslt;
- char *str;
- int size;
-
- if (edit)
- {
- str = new char[size = edit->GetWindowTextLength() + 1];
- if (str)
- {
- edit->GetWindowText(str, size);
- if (isalpha(str[0]))
- rslt = get_var(tolower(str[0]) - 'a');
- else
- rslt = atof(str);
- delete str;
- }
- }
- return rslt;
- }
-
- double TCalcWindow::get_var(int line)
- {
- double rslt = 0;
-
- if (Variable)
- {
- int size = Variable->GetLineLength(line) + 1;
- char *str = new char[size];
- if (str)
- {
- Variable->GetLine(str, size, line);
- rslt = atof(str + 3); // Don't want first 3 chars
- delete str;
- }
- }
- return rslt;
- }
-
- void TCalcWindow::put_var(double var)
- {
- if (Variable)
- {
- UINT start, end;
- Variable->GetSelection(start, end);
- if (start != end)
- Variable->SetSelection(start, start);
- int line = Variable->GetLineFromPos(-1);
- int size = Variable->GetLineLength(line) + 1;
- char *str = new char[size];
- if (str)
- {
- Variable->GetLine(str, size, line);
- sprintf(str, "%c: %g", str[0], var);
- start = Variable->GetLineIndex(-1);
- end = start + Variable->GetLineLength(-1);
- Variable->SetSelection(start, end);
- Variable->Insert(str);
- delete str;
- }
- }
-
- }
-
- void TCalcWindow::CmCalc()
- {
- double x, y, z = 0;
- char *str, *err = NULL;
- int size;
-
- x = get_number(Operand1);
- y = get_number(Operand2);
-
- if (Operator)
- {
- str = new char[size = Operator->GetWindowTextLength() + 1];
- if (str)
- {
- Operator->GetWindowText(str, size);
- if (str[1] != '\0')
- err = "Invalid operator";
- else
- switch (str[0])
- {
- case '+':
- z = x + y;
- break;
- case '-':
- z = x - y;
- break;
- case '*':
- z = x * y;
- break;
- case '/':
- if (y)
- z = x / y;
- else
- err = "Division by zero error";
- break;
- case '^':
- if (x > 0)
- z = exp(y * log(x));
- else
- err = "Can't raise power of negative numbers";
- break;
- default:
- err = "Invalid operator";
- break;
- }
- if (ErrMsg)
- if (!err)
- ErrMsg->Clear();
- else
- ErrMsg->SetWindowText(err);
- if (Store)
- Store->EnableWindow(!err);
- if (!err && Result)
- {
- char dest[81];
- sprintf(dest, "%g", z);
- Result->SetWindowText(dest);
- }
- delete str;
- }
- }
- }
-
- void TCalcWindow::CmStore()
- {
- if (Result)
- {
- int size = Result->GetWindowTextLength() + 1;
- char *str = new char[size];
- if (str)
- {
- Result->GetWindowText(str, size);
- put_var(atof(str));
- delete str;
- }
- }
- }
-
- void TCalcWindow::CmExit()
- {
- SendMessage(WM_CLOSE);
- }
-
- class TCalcApp : public TApplication
- {
- public:
- TCalcApp() : TApplication()
- { nCmdShow = SW_SHOWMAXIMIZED; }
-
- void InitMainWindow()
- {
- SetMainWindow(new TFrameWindow( 0,
- "Mr. Calulator",
- new TCalcWindow ));
- GetMainWindow()->AssignMenu("EXITMENU");
- }
- };
-
- int OwlMain(int, char *[])
- {
- return TCalcApp().Run();
- }
-