-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.cc
209 lines (194 loc) · 5.93 KB
/
gui.cc
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <gtkmm.h>
#include "main.h"
#include "utils.h"
class Calculator : public Gtk::Window
{
public:
Calculator();
~Calculator() override;
protected:
void on_btn0_click();
void on_btn1_click();
void on_btn2_click();
void on_btn3_click();
void on_btn4_click();
void on_btn5_click();
void on_btn6_click();
void on_btn7_click();
void on_btn8_click();
void on_btn9_click();
void on_btnPlus_click();
void on_btnMinus_click();
void on_btnTimes_click();
void on_btnDivision_click();
void on_btnEquals_click();
void on_btnClear_click();
void on_btnOpnParnth_click();
void on_btnClsParnth_click();
Gtk::Frame frame;
Gtk::Grid grid;
Gtk::TextView textView;
Glib::RefPtr<Gtk::TextBuffer> textBuffer;
Gtk::Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnPlus, btnMinus, btnTimes, btnDivision, btnEquals, btnClear, btnOpnParnth, btnClsParnth;
};
Calculator::Calculator()
: btn0("0"),
btn1("1"),
btn2("2"),
btn3("3"),
btn4("4"),
btn5("5"),
btn6("6"),
btn7("7"),
btn8("8"),
btn9("9"),
btnPlus("+"),
btnMinus("-"),
btnTimes("*"),
btnDivision("/"),
btnEquals("="),
btnClear("CLR"),
btnOpnParnth("("),
btnClsParnth(")")
{
set_child(frame);
frame.set_child(grid);
// Append visible components,
set_title("Calculator");
grid.attach(btn1, 0, 0);
grid.attach(btn2, 1, 0);
grid.attach(btn3, 2, 0);
grid.attach(btn4, 0, 1);
grid.attach(btn5, 1, 1);
grid.attach(btn6, 2, 1);
grid.attach(btn7, 0, 2);
grid.attach(btn8, 1, 2);
grid.attach(btn9, 2, 2);
grid.attach(btn0, 1, 3);
grid.attach(btnPlus, 3, 0);
grid.attach(btnMinus, 3, 1);
grid.attach(btnTimes, 3, 2);
grid.attach(btnDivision, 3, 3);
grid.attach(btnClear, 4, 0);
grid.attach(btnOpnParnth, 4, 1);
grid.attach(btnClsParnth, 4, 2);
grid.attach(btnEquals, 4, 3);
textBuffer = Gtk::TextBuffer::create();
textView.set_buffer(textBuffer);
textView.set_margin(10);
textView.set_size_request(20, 20);
textView.set_valign(Gtk::Align::CENTER);
grid.attach_next_to(textView, btn1, Gtk::PositionType::TOP, 4);
// Attach click signals to buttons
btn0.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn0_click));
btn1.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn1_click));
btn2.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn2_click));
btn3.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn3_click));
btn4.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn4_click));
btn5.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn5_click));
btn6.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn6_click));
btn7.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn7_click));
btn8.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn8_click));
btn9.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btn9_click));
btnPlus.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btnPlus_click));
btnMinus.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btnMinus_click));
btnTimes.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btnTimes_click));
btnDivision.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btnDivision_click));
btnClear.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btnClear_click));
btnOpnParnth.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btnOpnParnth_click));
btnClsParnth.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btnClsParnth_click));
btnEquals.signal_clicked().connect(sigc::mem_fun(*this, &Calculator::on_btnEquals_click));
}
Calculator::~Calculator()
{
}
void Calculator::on_btn0_click()
{
global_expression += "0";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn1_click()
{
global_expression += "1";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn2_click()
{
global_expression += "2";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn3_click()
{
global_expression += "3";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn4_click()
{
global_expression += "4";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn5_click()
{
global_expression += "5";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn6_click()
{
global_expression += "6";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn7_click()
{
global_expression += "7";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn8_click()
{
global_expression += "8";
textBuffer->set_text(global_expression);
}
void Calculator::on_btn9_click()
{
global_expression += "9";
textBuffer->set_text(global_expression);
}
void Calculator::on_btnPlus_click()
{
global_expression += " + ";
textBuffer->set_text(global_expression);
}
void Calculator::on_btnMinus_click()
{
global_expression += " - ";
textBuffer->set_text(global_expression);
}
void Calculator::on_btnTimes_click()
{
global_expression += " * ";
textBuffer->set_text(global_expression);
}
void Calculator::on_btnDivision_click()
{
global_expression += " / ";
textBuffer->set_text(global_expression);
}
void Calculator::on_btnOpnParnth_click()
{
global_expression += "( ";
textBuffer->set_text(global_expression);
}
void Calculator::on_btnClsParnth_click()
{
global_expression += " )";
textBuffer->set_text(global_expression);
}
void Calculator::on_btnClear_click()
{
global_expression = "";
textBuffer->set_text(global_expression);
}
void Calculator::on_btnEquals_click()
{
global_expression = evaluateExpression(global_expression);
textBuffer->set_text(global_expression);
}