summaryrefslogtreecommitdiff
path: root/calculator.cpp
blob: 8a5c5b38fa9a80d2059f1e4e680c49abf9e101cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <cstdlib>
#include "calculator.h"

CalculatorWindow::CalculatorWindow(	const Rect &rFrame,
						const String& szTitle,
						unsigned int nWindowLayoutFlags,
						unsigned int uiFlags
	): ApplicationWindow(rFrame, szTitle, nWindowLayoutFlags)
{
	pText = new TextView(this, Rect(15, 30, 160, 50), WINDOW_LAYOUT_FOLLOW_RIGHT , TEXTVIEW_FLAG_SINGLE_LINE);
	pText->Enable(false);
	pText->Set("0.0");

	Button	* pButton;

	pButton	= new Button(this, Rect(Point(15, 60), Point(40, 82)), "7", 0, new MessageCommand(BUTTON_SEVEN));	
	pButton	= new Button(this, Rect(Point(45, 60), Point(70, 82)), "8", 0, new MessageCommand(BUTTON_EIGHT));
	pButton	= new Button(this, Rect(Point(75, 60), Point(100, 82)), "9", 0, new MessageCommand(BUTTON_NINE));
	pButton = new Button(this, Rect(Point(105, 60), Point(160, 82)), "Clear", 0, new MessageCommand(BUTTON_CLEAR));

	pButton	= new Button(this, Rect(Point(15, 87), Point(40, 109)), "4", 0, new MessageCommand(BUTTON_FOUR));
	pButton	= new Button(this, Rect(Point(45, 87), Point(70, 109)), "5", 0, new MessageCommand(BUTTON_FIVE));
	pButton	= new Button(this, Rect(Point(75, 87), Point(100, 109)), "6", 0, new MessageCommand(BUTTON_SIX));
	pButton	= new Button(this, Rect(Point(105, 87), Point(130, 109)), "+", 0, new MessageCommand(BUTTON_PLUS));
	pButton	= new Button(this, Rect(Point(135, 87), Point(160, 109)), "-", 0, new MessageCommand(BUTTON_MINUS));

	pButton	= new Button(this, Rect(Point(15, 114), Point(40, 136)), "1", 0, new MessageCommand(BUTTON_ONE));
	pButton	= new Button(this, Rect(Point(45, 114), Point(70, 136)), "2", 0, new MessageCommand(BUTTON_TWO));
	pButton	= new Button(this, Rect(Point(75, 114), Point(100, 136)), "3", 0, new MessageCommand(BUTTON_THREE));
	pButton	= new Button(this, Rect(Point(105, 114), Point(130, 136)), "\u00D7", 0, new MessageCommand(BUTTON_TIMES));
	pButton	= new Button(this, Rect(Point(135, 114), Point(160, 136)), "\u00F7", 0, new MessageCommand(BUTTON_DIVIDE));

	pButton	= new Button(this, Rect(Point(15, 141), Point(40, 163)), "0", 0, new MessageCommand(BUTTON_ZERO));
	pButton	= new Button(this, Rect(Point(45, 141), Point(70, 163)), ".", 0, new MessageCommand(BUTTON_POINT));
	pButton	= new Button(this, Rect(Point(75, 141), Point(100, 164)), "\u00B1", 0, new MessageCommand(BUTTON_SIGN));
	pButton = new Button(this, Rect(Point(105, 141), Point(160, 164)), "=", 0, new MessageCommand(BUTTON_EQUALS));
}

void CalculatorWindow::OnCommand(const MessageCommand *pMessage) {
	const String minus = String("-");
	const String point = String(".");
	static char operation = 'n';
	static double first_operand = 0.0;
	char c[2] = {'0', '\0'};
	int message = pMessage->GetID();
	String * line = pText->GetDocument()->GetLine(0);
	
	switch (message) {
		case BUTTON_ZERO:
		case BUTTON_ONE:
		case BUTTON_TWO:
		case BUTTON_THREE:
		case BUTTON_FOUR:
		case BUTTON_FIVE:
		case BUTTON_SIX:
		case BUTTON_SEVEN:
		case BUTTON_EIGHT:
		case BUTTON_NINE:
			c[0] += message;
			if (line->Compare("0.0")) {
				pText->Append((const char *)&c);
			} else {
				pText->Set((const char *)&c);
			}
			break;
		case BUTTON_PLUS:
			operation = '+';
			break;
		case BUTTON_MINUS:
			operation = '-';
			break;
		case BUTTON_TIMES:
			operation = '*';
			break;
		case BUTTON_DIVIDE:
			operation = '/';
			break;
		case BUTTON_EQUALS:
		case BUTTON_POINT:
/*			if(line->find(point)) { // FIXME: figure out how to do this more directly
				pText->Append(point);
			}*/
			if (!strchr(line->c_str(), '.')) {
				pText->Append(point);
			}
			break;
		case BUTTON_SIGN:
			if (line->c_str()[0] != '-') {
				pText->Insert(minus, Point(0, 0));
			} else {
				pText->Remove(Point(0,0));
			}
			break;
		case BUTTON_CLEAR:
			operation = 'n';
			first_operand = 0.0;
			pText->Set("0.0");
			break; 
	}
	printf("line: %s; first_operand: %f; operator: %c\n", line->c_str(), first_operand, operation);
}

Calculator::Calculator(int argc, char* argv[]):
	Application("application/x-vnd.Calculator", argc, argv) {
	
	Rect r(Point(100, 100), Point(275, 275));

	CalculatorWindow * pCalculatorWindow =
		new CalculatorWindow(r, "Calculator", WINDOW_LAYOUT_NOTHING,
					APPLICATION_WINDOW_NO_VIEW);
	
	pCalculatorWindow->GetTitleWindow()->SetFlags(
		(TitleWindowFlags)(pCalculatorWindow->GetTitleWindow()->GetFlags()
	));

	pCalculatorWindow->Show();
}

int main(int argc, char *argv[]) {
	Calculator pCalculator(argc, argv);

	return pCalculator.Run();
}