-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_box.cpp
199 lines (148 loc) · 4.46 KB
/
message_box.cpp
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
#include <cstdio>
#include <cstring>
#include <string>
#include "app.h"
#include "message_box.h"
//int MessageBox::numberOfButtons = 0;
//int MessageBox::buttonPressed = 0;
//ToolBar *MessageBox::frame = NULL;
//Label *MessageBox::textLabel = NULL;
//TextBox *MessageBox::textBox = NULL;
//Button *MessageBox::buttons[3] = {NULL, NULL, NULL};
//char *MessageBox::title = NULL;
//char *MessageBox::text = NULL;
MessageBox::MessageBox() {
}
MessageBox::~MessageBox() {
}
MessageBox::MessageBox(const char *theTitle, const char *theText, int theNumberOfButtons, bool haveTextBox) {
init(theTitle, theText, theNumberOfButtons, haveTextBox);
}
void MessageBox::init(const char *theTitle, const char *theText, int theNumberOfButtons, bool haveTextBox) {
title = theTitle;
text = theText;
numberOfButtons = theNumberOfButtons;
buttonPressed = 0;
// Set up widgets
// set up frame
int frameX = (app->windowWidth - strlen(theText) - 2) / 2;
int frameY = (app->windowHeight - 8) / 2;
frame = new ToolBar(frameX, frameY, title.c_str());
// the text of the message
textLabel = new Label(0, 0, text.c_str());
int textBoxWidth = 30;
textBox = new TextBox(0, 0, textBoxWidth, 1024, "", "");
HBox *hbox = new HBox(0, 0, 1);
// set up buttons
buttons[0] = new Button("Yes",
NULL,
MessageBoxButtonCbk,
new MessageBoxButtonCbkInfo(this, 1));
hbox->addWidget(buttons[0]);
buttons[1] = new Button("No",
NULL,
MessageBoxButtonCbk,
new MessageBoxButtonCbkInfo(this, 2));
hbox->addWidget(buttons[1]);
buttons[2] = new Button("Cancel",
NULL,
MessageBoxButtonCbk,
new MessageBoxButtonCbkInfo(this, 3));
hbox->addWidget(buttons[2]);
// Put the text label and buttons in a vbox
VBox *vbox = new VBox(0, 0, 1);
customWidgetContainer = new VBox(0, 0, 0);
customWidgetContainer->setVisible(false);
vbox->addWidget(textLabel);
vbox->addWidget(customWidgetContainer);
vbox->addWidget(textBox);
vbox->addWidget(hbox);
frame->addWidget(vbox);
if(haveTextBox)
textBox->setVisible(true);
else
textBox->setVisible(false);
// Make all buttons invisble
buttons[0]->setVisible(false);
buttons[1]->setVisible(false);
buttons[2]->setVisible(false);
// depending on the number of buttons we show the buttons and set their names
switch(numberOfButtons) {
case 1:
buttons[0]->setLabel("OK");
buttons[0]->setVisible(true);
break;
case 2:
buttons[0]->setLabel("OK");
buttons[0]->setVisible(true);
buttons[1]->setLabel("Cancel");
buttons[1]->setVisible(true);
break;
case 3:
buttons[0]->setLabel("Yes");
buttons[0]->setVisible(true);
buttons[1]->setLabel("No");
buttons[1]->setVisible(true);
buttons[2]->setLabel("Cancel");
buttons[2]->setVisible(true);
break;
}
frame->setVisible(false);
}
void MessageBox::setButtonText(int buttonNumber, const char *text) {
buttons[buttonNumber - 1]->setLabel(text);
}
void MessageBox::addCustomWidget(Widget *wid) {
customWidgetContainer->setVisible(true);
customWidgetContainer->addWidget(wid);
}
void MessageBox::show() {
TCODConsole *con = TCODConsole::root;
Widget::setConsole(con);
frame->setVisible(true);
if(textBox->isVisible())
Widget::keyboardFocus = textBox;
TCOD_key_t key;
// We loop until the user presses a button (or presses escape)
while(buttonPressed == 0) {
key = TCODConsole::checkForKeypress();
// Warning: for the next line to work we needed to change gui/widget.hpp
Widget::mouse = TCODMouse::getStatus();
frame->computeSize();
frame->update(key);
frame->render();
con->flush();
if(key.vk == TCODK_ESCAPE)
break;
// Pressing enter is equivalent to pressing button 1
if(key.vk == TCODK_ENTER) {
buttonPressed = 1;
break;
}
}
frame->setVisible(false);
Widget::focus = false; // Not having this line causes a bug where the focus stays true
}
int MessageBox::getButtonPressed() {
return buttonPressed;
}
void MessageBox::setButtonPressed(int buttonNumber) {
buttonPressed = buttonNumber;
}
const char *MessageBox::getString() {
if(strlen(textBox->getValue()) > 0)
return textBox->getValue();
return NULL;
}
void MessageBox::setString(const char *str) {
textBox->setText(str);
}
//--------------- Callback stuff
MessageBoxButtonCbkInfo::MessageBoxButtonCbkInfo(MessageBox *msg, int bn) {
messageBox = msg;
buttonNumber = bn;
}
void MessageBoxButtonCbk(Widget *wid, void *data) {
MessageBoxButtonCbkInfo *info = reinterpret_cast<MessageBoxButtonCbkInfo*>(data);
info->messageBox->setButtonPressed(info->buttonNumber);
}