#include <owl/applicat.h>
#include <owl/framewin.h>
#include <owl/dialog.h>
#include <owl/edit.h>
#include <owl/button.h>
#include <owl/radiobut.h>
#include "parse.h"
#include "expr.h"
#include "asgn4owl.rc"
//
// Constants.
//
const int MaxStrLen = 100;
//
// Module variables.
//
static expr * exprs[2];
//
// Forward declarations.
//
class ExprApp;
class ExprWindow;
class EnterDialog;
class ClearDialog;
class JoinDialog;
//
// Main application class.
//
class ExprApp : public TApplication {
public:
ExprApp();
~ExprApp();
void InitMainWindow();
};
//
// Main window class.
//
class ExprWindow : public TWindow {
public:
ExprWindow(TWindow * parent = 0);
protected:
bool CanClose();
void Paint(TDC&, bool, TRect&);
void DoQuit();
void DoEnter();
void DoClear();
void DoJoin();
void DoAbout();
DECLARE_RESPONSE_TABLE(ExprWindow);
};
DEFINE_RESPONSE_TABLE1(ExprWindow, TWindow)
EV_COMMAND(IDM_QUIT, DoQuit),
EV_COMMAND(IDM_ENTER, DoEnter),
EV_COMMAND(IDM_CLEAR, DoClear),
EV_COMMAND(IDM_JOIN, DoJoin),
EV_COMMAND(IDM_ABOUT, DoAbout),
END_RESPONSE_TABLE;
//
// "Enter Expression" dialog box class.
//
class EnterDialog : public TDialog {
public:
EnterDialog(TWindow *);
protected:
void EnterExpr1();
void EnterExpr2();
void EnterExpr(int i);
void DoCancel();
TEdit EditBox;
TButton Expr1Button;
TButton Expr2Button;
TButton CancelButton;
DECLARE_RESPONSE_TABLE(EnterDialog);
};
DEFINE_RESPONSE_TABLE1(EnterDialog, TDialog)
EV_COMMAND(IDC_EXPR_1_BUTTON, EnterExpr1),
EV_COMMAND(IDC_EXPR_2_BUTTON, EnterExpr2),
EV_COMMAND(IDC_CANCEL_BUTTON, DoCancel),
END_RESPONSE_TABLE;
//
// "Clear Expression" dialog box class.
//
class ClearDialog : public TDialog {
public:
ClearDialog(TWindow *);
protected:
void ClearExpr1();
void ClearExpr2();
void ClearExpr(int i);
void DoCancel();
TButton Expr1Button;
TButton Expr2Button;
TButton CancelButton;
DECLARE_RESPONSE_TABLE(ClearDialog);
};
DEFINE_RESPONSE_TABLE1(ClearDialog, TDialog)
EV_COMMAND(IDC_EXPR_1_BUTTON, ClearExpr1),
EV_COMMAND(IDC_EXPR_2_BUTTON, ClearExpr2),
EV_COMMAND(IDC_CANCEL_BUTTON, DoCancel),
END_RESPONSE_TABLE;
//
// "Join Expression" dialog box class.
//
class JoinDialog : public TDialog {
public:
JoinDialog(TWindow *);
protected:
void DoJoin();
void DoCancel();
TRadioButton AdditionButton;
TRadioButton SubtractionButton;
TRadioButton MultiplicationButton;
TRadioButton DivisionButton;
TButton JoinButton;
TButton CancelButton;
DECLARE_RESPONSE_TABLE(JoinDialog);
};
DEFINE_RESPONSE_TABLE1(JoinDialog, TDialog)
EV_COMMAND(IDC_OK_BUTTON, DoJoin),
EV_COMMAND(IDC_CANCEL_BUTTON, DoCancel),
END_RESPONSE_TABLE;
//
// ExprApp functions.
//
ExprApp::ExprApp() : TApplication() {
exprs[0] = new constant(0.0);
exprs[1] = new constant(0.0);
}
ExprApp::~ExprApp() {
delete exprs[0];
delete exprs[1];
}
void ExprApp::InitMainWindow() {
SetMainWindow(new TFrameWindow(0,
"CMPT 212 Assignment 4 (OWL)",
new ExprWindow));
GetMainWindow()->AssignMenu("MAINMENU");
}
//
// ExprWindow functions.
//
ExprWindow::ExprWindow(TWindow * parent) {
Init(parent, 0, 0);
}
bool ExprWindow::CanClose() {
return MessageBox("Do you want to quit?",
"Quit",
MB_ICONQUESTION | MB_YESNO) == IDYES;
}
void ExprWindow::Paint(TDC& dc, bool, TRect&) {
ostrstream * os;
TEXTMETRIC tm;
int x = 1;
int y = 1;
GetTextMetrics(dc, &tm);
for (int i=0; i<2; i++) {
os = new ostrstream;
(*os) << "Expression " << (i+1) << ":";
TextOut(dc, x, y, os->str(), os->pcount());
y = y + tm.tmHeight + tm.tmExternalLeading;
delete os;
TextOut(dc, x, y, "", 0);
y = y + tm.tmHeight + tm.tmExternalLeading;
os = new ostrstream;
exprs[i]->display(*os);
TextOut(dc, x, y, os->str(), os->pcount());
y = y + tm.tmHeight + tm.tmExternalLeading;
delete os;
os = new ostrstream;
(*os) << exprs[i]->evaluate();
TextOut(dc, x, y, os->str(), os->pcount());
y = y + tm.tmHeight + tm.tmExternalLeading;
delete os;
TextOut(dc, x, y, "", 0);
y = y + tm.tmHeight + tm.tmExternalLeading;
}
}
void ExprWindow::DoQuit() {
CloseWindow();
}
void ExprWindow::DoEnter() {
EnterDialog(this).Execute();
}
void ExprWindow::DoClear() {
ClearDialog(this).Execute();
}
void ExprWindow::DoJoin() {
JoinDialog(this).Execute();
}
void ExprWindow::DoAbout() {
TDialog(this, "ABOUT_DIALOG").Execute();
}
//
// EnterDialog functions.
//
EnterDialog::EnterDialog(TWindow * parent)
: TDialog(parent, "ENTER_EXPR_DIALOG", 0),
EditBox(this, ID_EXPR_STR),
Expr1Button(this, IDC_EXPR_1_BUTTON),
Expr2Button(this, IDC_EXPR_2_BUTTON),
CancelButton(this, IDC_CANCEL_BUTTON) {
}
void EnterDialog::EnterExpr1() {
EnterExpr(1);
}
void EnterDialog::EnterExpr2() {
EnterExpr(2);
}
void EnterDialog::EnterExpr(int i) {
char str[MaxStrLen+1];
EditBox.GetText(str, MaxStrLen);
delete exprs[i-1];
exprs[i-1] = parse(str);
GetParentO()->Invalidate();
CloseWindow();
}
void EnterDialog::DoCancel() {
CloseWindow();
}
//
// Clear Dialog functions.
//
ClearDialog::ClearDialog(TWindow * parent)
: TDialog(parent, "Clear_EXPR_DIALOG", 0),
Expr1Button(this, IDC_EXPR_1_BUTTON),
Expr2Button(this, IDC_EXPR_2_BUTTON),
CancelButton(this, IDC_CANCEL_BUTTON) {
}
void ClearDialog::ClearExpr1() {
ClearExpr(1);
}
void ClearDialog::ClearExpr2() {
ClearExpr(2);
}
void ClearDialog::ClearExpr(int i) {
delete exprs[i-1];
exprs[i-1] = new constant(0.0);
GetParentO()->Invalidate();
CloseWindow();
}
void ClearDialog::DoCancel() {
CloseWindow();
}
//
// JoinDialog functions.
//
JoinDialog::JoinDialog(TWindow * parent)
: TDialog(parent, "JOIN_EXPR_DIALOG", 0),
AdditionButton(this, IDC_ADDITION),
SubtractionButton(this, IDC_SUBTRACTION),
MultiplicationButton(this, IDC_MULTIPLICATION),
DivisionButton(this, IDC_DIVISION),
JoinButton(this, IDC_OK_BUTTON),
CancelButton(this, IDC_CANCEL_BUTTON) {
}
void JoinDialog::DoJoin() {
composite::operation oper;
bool operSelected = false;
if (AdditionButton.GetCheck() == BF_CHECKED) {
oper = composite::Addition;
operSelected = true;
}
if (SubtractionButton.GetCheck() == BF_CHECKED) {
oper = composite::Subtraction;
operSelected = true;
}
if (MultiplicationButton.GetCheck() == BF_CHECKED) {
oper = composite::Multiplication;
operSelected = true;
}
if (DivisionButton.GetCheck() == BF_CHECKED) {
oper = composite::Division;
operSelected = true;
}
if (!operSelected) {
MessageBox("You must select an operation.",
"No Operation",
MB_ICONEXCLAMATION | MB_OK);
return;
}
exprs[0] = new composite(oper, exprs[0], exprs[1]->clone());
GetParentO()->Invalidate();
CloseWindow();
}
void JoinDialog::DoCancel() {
CloseWindow();
}
//
// Main function.
//
int OwlMain(int, char* []) {
return ExprApp().Run();
}
//
// End of file.
//