1 // $Id$
2 //
3 // Test application
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #include <unistd.h>
9 #include <cassert>
10 #include <iostream>
11 #include <sstream>
12
13 #include "yacurs.h"
14
15 // Used when preloading libtestpreload.so
16 #ifdef YACURS_USE_WCHAR
17 wint_t
18 #else
19 int
20 #endif
21 __test_data[10][5] = {
22 // OK_ONLY
23 {'\n', '\n', 0},
24 // YES_ONLY
25 {'\t', '\n', '\n', 0},
26 // OKCANCEL: OK
27 {'\t', '\n', '\n', 0},
28 // OKCANCEL: Cancel
29 {'\n', '\t', '\n', 0},
30 // YESNO: Yes
31 {'\t', '\n', '\n', 0},
32 // YESNO: No
33 {'\n', '\t', '\n', 0},
34 // YESNOCANCEL: Yes
35 {'\t', '\n', '\n', 0},
36 // YESNOCANCEL: No
37 {'\n', '\t', '\n', 0},
38 // YESNOCANCEL: Cancel
39 {'\n', '\t', '\t', '\n', 0},
40 // QUIT
41 {'\t', '\n', 0}};
42
43 YACURS::DIALOG_STATE expectation[9] = {
44 YACURS::DIALOG_OK, YACURS::DIALOG_YES, YACURS::DIALOG_OK,
45 YACURS::DIALOG_CANCEL, YACURS::DIALOG_YES, YACURS::DIALOG_NO,
46 YACURS::DIALOG_YES, YACURS::DIALOG_NO, YACURS::DIALOG_CANCEL};
47
48 static YACURS::DIALOG_STATE last_dialog_state;
49
50 #ifdef YACURS_USE_WCHAR
__test_wget_wch(void *,wint_t * i)51 extern "C" int __test_wget_wch(void*, wint_t* i) {
52 static int row = 0;
53 static int col = 0;
54 static YACURS::DIALOG_STATE* exptr = expectation;
55
56 #ifdef SLOW_TESTS
57 usleep(70000);
58 #endif
59
60 if (__test_data[row][col] == 0) {
61 // Test expectation
62 if (*exptr != last_dialog_state) abort();
63
64 // Next round
65 exptr++;
66 if (++row > 9) abort();
67
68 col = 0;
69 }
70
71 *i = __test_data[row][col++];
72
73 return OK;
74 }
75 #else
__test_wgetch(void *)76 extern "C" int __test_wgetch(void*) {
77 static int row = 0;
78 static int col = 0;
79 static YACURS::DIALOG_STATE* exptr = expectation;
80
81 #ifdef SLOW_TESTS
82 usleep(70000);
83 #endif
84
85 if (__test_data[row][col] == 0) {
86 // Test expectation
87 if (*exptr != last_dialog_state) abort();
88
89 // Next round
90 exptr++;
91 if (++row > 9) abort();
92
93 col = 0;
94 }
95
96 return __test_data[row][col++];
97 }
98 #endif
99
100 class MainWindow : public YACURS::Window {
101 private:
102 YACURS::VPack* vpack1;
103 YACURS::HPack* hpack1;
104 YACURS::Button* button1;
105 YACURS::Button* button2;
106 YACURS::Button* button3;
107 YACURS::Button* button4;
108 YACURS::Button* button5;
109 YACURS::Button* button6;
110 YACURS::Label* label1;
111 YACURS::Dialog* dialog;
112
113 protected:
window_close_handler(YACURS::Event & _e)114 void window_close_handler(YACURS::Event& _e) {
115 assert(_e == YACURS::EVT_WINDOW_CLOSE);
116 YACURS::EventEx<YACURS::WindowBase*>& evt =
117 dynamic_cast<YACURS::EventEx<YACURS::WindowBase*>&>(_e);
118
119 if (dialog != 0 && evt.data() == dialog) {
120 #ifdef YACURS_USE_WCHAR
121 YACURS::Curses::statusbar()->push("Dialog 1 clösed");
122 #else
123 YACURS::Curses::statusbar()->push("Dialog 1 closed");
124 #endif
125
126 switch (last_dialog_state = dialog->dialog_state()) {
127 case YACURS::DIALOG_OK:
128 label1->label("DIALOG_OK");
129 break;
130 case YACURS::DIALOG_YES:
131 label1->label("DIALOG_YES");
132 break;
133 case YACURS::DIALOG_CANCEL:
134 label1->label("DIALOG_CANCEL");
135 break;
136 case YACURS::DIALOG_NO:
137 label1->label("DIALOG_NO");
138 break;
139 }
140
141 delete dialog;
142 dialog = 0;
143 }
144 }
145
button_press_handler(YACURS::Event & _e)146 void button_press_handler(YACURS::Event& _e) {
147 assert(_e == YACURS::EVT_BUTTON_PRESS);
148 YACURS::EventEx<YACURS::Button*>& e =
149 dynamic_cast<YACURS::EventEx<YACURS::Button*>&>(_e);
150
151 if (e.data() == button1) {
152 assert(dialog == 0);
153
154 dialog = new YACURS::Dialog("OK_ONLY Dialog", YACURS::OK_ONLY);
155 dialog->show();
156 return;
157 }
158
159 if (e.data() == button2) {
160 assert(dialog == 0);
161
162 dialog = new YACURS::Dialog("YES_ONLY Dialog", YACURS::YES_ONLY);
163 dialog->show();
164 return;
165 }
166
167 if (e.data() == button3) {
168 assert(dialog == 0);
169
170 dialog = new YACURS::Dialog("OKCANCEL Dialog", YACURS::OKCANCEL);
171 dialog->show();
172 return;
173 }
174
175 if (e.data() == button4) {
176 assert(dialog == 0);
177
178 dialog = new YACURS::Dialog("YESNO Dialog", YACURS::YESNO);
179 dialog->show();
180 return;
181 }
182
183 if (e.data() == button5) {
184 assert(dialog == 0);
185
186 dialog =
187 new YACURS::Dialog("YESNOCANCEL Dialog", YACURS::YESNOCANCEL);
188 dialog->show();
189 return;
190 }
191
192 if (e.data() == button6) {
193 YACURS::EventQueue::submit(YACURS::EVT_QUIT);
194 return;
195 }
196 }
197
198 public:
MainWindow()199 MainWindow() : YACURS::Window(YACURS::Margin(1, 0, 1, 0)), dialog(0) {
200 button1 = new YACURS::Button("OK_ONLY");
201 button2 = new YACURS::Button("YES_ONLY");
202 button3 = new YACURS::Button("OKCANCEL");
203 button4 = new YACURS::Button("YESNO");
204 button5 = new YACURS::Button("YESNOCANCEL");
205 button6 = new YACURS::Button("Quit");
206 vpack1 = new YACURS::VPack;
207 hpack1 = new YACURS::HPack;
208 label1 = new YACURS::Label("dialog state");
209 hpack1->add_back(button1);
210 hpack1->add_back(button2);
211 hpack1->add_back(button3);
212 hpack1->add_back(button4);
213 hpack1->add_back(button5);
214 hpack1->add_back(button6);
215 vpack1->add_front(label1);
216 vpack1->add_back(hpack1);
217 widget(vpack1);
218
219 YACURS::EventQueue::connect_event(
220 YACURS::EventConnectorMethod1<MainWindow>(
221 YACURS::EVT_BUTTON_PRESS, this,
222 &MainWindow::button_press_handler));
223 YACURS::EventQueue::connect_event(
224 YACURS::EventConnectorMethod1<MainWindow>(
225 YACURS::EVT_WINDOW_CLOSE, this,
226 &MainWindow::window_close_handler));
227 }
228
~MainWindow()229 ~MainWindow() {
230 if (dialog) delete dialog;
231
232 delete button1;
233 delete button2;
234 delete button3;
235 delete button4;
236 delete button5;
237 delete button6;
238 delete hpack1;
239 delete vpack1;
240 delete label1;
241
242 YACURS::EventQueue::disconnect_event(
243 YACURS::EventConnectorMethod1<MainWindow>(
244 YACURS::EVT_BUTTON_PRESS, this,
245 &MainWindow::button_press_handler));
246 YACURS::EventQueue::disconnect_event(
247 YACURS::EventConnectorMethod1<MainWindow>(
248 YACURS::EVT_WINDOW_CLOSE, this,
249 &MainWindow::window_close_handler));
250 }
251 };
252
main()253 int main() {
254 // test will not be run if stdout or stdin is not a tty.
255 if (isatty(STDOUT_FILENO) != 1 || isatty(STDIN_FILENO) != 1) exit(77);
256
257 #if 0
258 std::cout << getpid() << std::endl;
259 sleep(15);
260 #endif
261
262 #ifdef YACURS_USE_WCHAR
263 if (setlocale(LC_ALL, "en_US.UTF-8") == 0) exit(77);
264 #endif
265
266 try {
267 YACURS::Curses::init();
268
269 YACURS::Curses::title(
270 new YACURS::TitleBar(YACURS::TitleBar::POS_TOP, "Dialog 1"));
271 YACURS::Curses::statusbar(new YACURS::StatusBar);
272
273 YACURS::Curses::mainwindow(new MainWindow);
274 YACURS::Curses::mainwindow()->frame(true);
275
276 YACURS::Curses::run();
277
278 delete YACURS::Curses::mainwindow();
279 delete YACURS::Curses::title();
280 delete YACURS::Curses::statusbar();
281
282 YACURS::Curses::end();
283 } catch (std::exception& e) {
284 YACURS::Curses::end();
285 std::cerr << e.what() << std::endl;
286 return 1;
287 }
288
289 return 0;
290 }
291