Commit | Line | Data |
---|---|---|
7d4115a5 KL |
1 | /** |
2 | * Jexer - Java Text User Interface - demonstration program | |
3 | * | |
4 | * Version: $Id$ | |
5 | * | |
6 | * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a> | |
7 | * | |
8 | * License: LGPLv3 or later | |
9 | * | |
10 | * Copyright: This module is licensed under the GNU Lesser General | |
11 | * Public License Version 3. Please see the file "COPYING" in this | |
12 | * directory for more information about the GNU Lesser General Public | |
13 | * License Version 3. | |
14 | * | |
15 | * Copyright (C) 2015 Kevin Lamonte | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or | |
18 | * modify it under the terms of the GNU Lesser General Public License | |
19 | * as published by the Free Software Foundation; either version 3 of | |
20 | * the License, or (at your option) any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, but | |
23 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 | * General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU Lesser General Public | |
28 | * License along with this program; if not, see | |
29 | * http://www.gnu.org/licenses/, or write to the Free Software | |
30 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
31 | * 02110-1301 USA | |
32 | */ | |
2420f903 | 33 | |
a06459bd | 34 | import jexer.*; |
8e688b92 | 35 | import jexer.menu.*; |
a06459bd | 36 | |
30d336cc KL |
37 | class DemoMsgBoxWindow extends TWindow { |
38 | /* | |
39 | private void openYNCMessageBox() { | |
40 | application.messageBox("Yes/No/Cancel MessageBox", | |
41 | q"EOS | |
42 | This is an example of a Yes/No/Cancel MessageBox. | |
43 | ||
44 | Note that the MessageBox text can span multiple | |
45 | lines. | |
46 | ||
47 | The default result (if someone hits the top-left | |
48 | close button) is CANCEL. | |
49 | EOS", | |
50 | TMessageBox.Type.YESNOCANCEL); | |
51 | } | |
52 | ||
53 | private void openYNMessageBox() { | |
54 | application.messageBox("Yes/No MessageBox", | |
55 | q"EOS | |
56 | This is an example of a Yes/No MessageBox. | |
57 | ||
58 | Note that the MessageBox text can span multiple | |
59 | lines. | |
60 | ||
61 | The default result (if someone hits the top-left | |
62 | close button) is NO. | |
63 | EOS", | |
64 | TMessageBox.Type.YESNO); | |
65 | } | |
66 | ||
67 | private void openOKCMessageBox() { | |
68 | application.messageBox("OK/Cancel MessageBox", | |
69 | q"EOS | |
70 | This is an example of a OK/Cancel MessageBox. | |
71 | ||
72 | Note that the MessageBox text can span multiple | |
73 | lines. | |
74 | ||
75 | The default result (if someone hits the top-left | |
76 | close button) is CANCEL. | |
77 | EOS", | |
78 | TMessageBox.Type.OKCANCEL); | |
79 | } | |
80 | ||
81 | private void openOKMessageBox() { | |
82 | application.messageBox("OK MessageBox", | |
83 | q"EOS | |
84 | This is an example of a OK MessageBox. This is the | |
85 | default MessageBox. | |
86 | ||
87 | Note that the MessageBox text can span multiple | |
88 | lines. | |
89 | ||
90 | The default result (if someone hits the top-left | |
91 | close button) is OK. | |
92 | EOS", | |
93 | TMessageBox.Type.OK); | |
94 | } | |
95 | ||
96 | */ | |
97 | ||
98 | /** | |
99 | * Constructor. | |
100 | */ | |
101 | DemoMsgBoxWindow(final TApplication parent) { | |
102 | this(parent, TWindow.CENTERED | TWindow.RESIZABLE); | |
103 | } | |
104 | ||
105 | /** | |
106 | * Constructor. | |
107 | */ | |
108 | DemoMsgBoxWindow(final TApplication parent, final int flags) { | |
109 | // Construct a demo window. X and Y don't matter because it | |
110 | // will be centered on screen. | |
111 | super(parent, "Message Boxes", 0, 0, 60, 15, flags); | |
112 | /* | |
113 | uint row = 1; | |
114 | ||
115 | // Add some widgets | |
116 | addLabel("Default OK message box", 1, row); | |
117 | addButton("Open O&K MB", 35, row, &openOKMessageBox); | |
118 | row += 2; | |
119 | ||
120 | addLabel("OK/Cancel message box", 1, row); | |
121 | addButton("O&pen OKC MB", 35, row, &openOKCMessageBox); | |
122 | row += 2; | |
123 | ||
124 | addLabel("Yes/No message box", 1, row); | |
125 | addButton("Open &YN MB", 35, row, &openYNMessageBox); | |
126 | row += 2; | |
127 | ||
128 | addLabel("Yes/No/Cancel message box", 1, row); | |
129 | addButton("Ope&n YNC MB", 35, row, &openYNCMessageBox); | |
130 | row += 2; | |
131 | ||
132 | addLabel("Input box", 1, row); | |
133 | addButton("Open &input box", 35, row, | |
134 | { | |
135 | application.inputBox("Input Box", | |
136 | q"EOS | |
137 | This is an example of an InputBox. | |
138 | ||
139 | Note that the InputBox text can span multiple | |
140 | lines. | |
141 | EOS", | |
142 | "some input text"); | |
143 | } | |
144 | ); | |
145 | ||
146 | addButton("&Close Window", (width - 14) / 2, height - 4, | |
147 | { | |
148 | application.closeWindow(this); | |
149 | } | |
150 | ); | |
151 | */ | |
152 | } | |
153 | } | |
154 | ||
155 | ||
a06459bd KL |
156 | class DemoMainWindow extends TWindow { |
157 | /* | |
158 | // Timer that increments a number | |
159 | private TTimer timer; | |
160 | ||
161 | // The modal window is a more low-level example of controlling a window | |
162 | // "from the outside". Most windows will probably subclass TWindow and | |
163 | // do this kind of logic on their own. | |
164 | private TWindow modalWindow; | |
165 | private void openModalWindow() { | |
8e688b92 KL |
166 | modalWindow = application.addWindow("Demo Modal Window", 0, 0, |
167 | 58, 15, TWindow.Flag.MODAL); | |
168 | modalWindow.addLabel("This is an example of a very braindead modal window.", 1, 1); | |
169 | modalWindow.addLabel("Modal windows are centered by default.", 1, 2); | |
170 | modalWindow.addButton("&Close", (modalWindow.width - 8)/2, | |
171 | modalWindow.height - 4, &modalWindowClose); | |
a06459bd KL |
172 | } |
173 | private void modalWindowClose() { | |
8e688b92 | 174 | application.closeWindow(modalWindow); |
a06459bd KL |
175 | } |
176 | ||
177 | /// This is an example of having a button call a function. | |
178 | private void openCheckboxWindow() { | |
8e688b92 | 179 | new DemoCheckboxWindow(application); |
a06459bd KL |
180 | } |
181 | ||
182 | /// We need to override onClose so that the timer will no longer be | |
183 | /// called after we close the window. TTimers currently are completely | |
184 | /// unaware of the rest of the UI classes. | |
185 | override public void onClose() { | |
8e688b92 | 186 | application.removeTimer(timer); |
a06459bd KL |
187 | } |
188 | */ | |
189 | ||
fca67db0 KL |
190 | /** |
191 | * Construct demo window. It will be centered on screen. | |
192 | */ | |
a06459bd | 193 | public DemoMainWindow(TApplication parent) { |
8e688b92 | 194 | this(parent, CENTERED | RESIZABLE); |
a06459bd KL |
195 | } |
196 | ||
fca67db0 KL |
197 | /** |
198 | * Constructor. | |
199 | */ | |
200 | private DemoMainWindow(TApplication parent, int flags) { | |
8e688b92 KL |
201 | // Construct a demo window. X and Y don't matter because it will be |
202 | // centered on screen. | |
203 | super(parent, "Demo Window", 0, 0, 60, 23, flags); | |
a06459bd | 204 | |
8e688b92 KL |
205 | int row = 1; |
206 | ||
207 | // Add some widgets | |
30d336cc | 208 | if (!isModal()) { |
8e688b92 KL |
209 | addLabel("Message Boxes", 1, row); |
210 | addButton("&MessageBoxes", 35, row, | |
30d336cc KL |
211 | new TAction() { |
212 | public void DO() { | |
213 | new DemoMsgBoxWindow(getApplication()); | |
214 | } | |
8e688b92 KL |
215 | } |
216 | ); | |
217 | } | |
218 | row += 2; | |
219 | ||
30d336cc | 220 | /* |
8e688b92 KL |
221 | addLabel("Open me as modal", 1, row); |
222 | addButton("W&indow", 35, row, | |
223 | { | |
224 | new DemoMainWindow(application, Flag.MODAL); | |
225 | } | |
226 | ); | |
227 | ||
228 | row += 2; | |
229 | ||
230 | addLabel("Variable-width text field:", 1, row); | |
231 | addField(35, row++, 15, false, "Field text"); | |
232 | ||
233 | addLabel("Fixed-width text field:", 1, row); | |
234 | addField(35, row, 15, true); | |
235 | row += 2; | |
236 | ||
237 | if (!isModal) { | |
238 | addLabel("Radio buttons and checkboxes", 1, row); | |
239 | addButton("&Checkboxes", 35, row, &openCheckboxWindow); | |
240 | } | |
241 | row += 2; | |
242 | ||
243 | if (!isModal) { | |
244 | addLabel("Editor window", 1, row); | |
245 | addButton("Edito&r", 35, row, | |
246 | { | |
247 | new TEditor(application, 0, 0, 60, 15); | |
248 | } | |
249 | ); | |
250 | } | |
251 | row += 2; | |
252 | ||
253 | if (!isModal) { | |
254 | addLabel("Text areas", 1, row); | |
255 | addButton("&Text", 35, row, | |
256 | { | |
257 | new DemoTextWindow(application); | |
258 | } | |
259 | ); | |
260 | } | |
261 | row += 2; | |
262 | ||
263 | if (!isModal) { | |
264 | addLabel("Tree views", 1, row); | |
265 | addButton("Tree&View", 35, row, | |
266 | { | |
267 | new DemoTreeViewWindow(application); | |
268 | } | |
269 | ); | |
270 | } | |
271 | row += 2; | |
272 | ||
273 | version(Posix) { | |
274 | if (!isModal) { | |
275 | addLabel("Terminal", 1, row); | |
276 | addButton("Termi&nal", 35, row, | |
277 | { | |
278 | application.openTerminal(0, 0); | |
279 | } | |
280 | ); | |
281 | } | |
282 | row += 2; | |
283 | } | |
284 | ||
285 | TProgressBar bar = addProgressBar(1, row, 22); | |
286 | row++; | |
287 | TLabel timerLabel = addLabel("Timer", 1, row); | |
288 | timer = parent.addTimer(100, | |
289 | { | |
290 | static int i = 0; | |
291 | auto writer = appender!dstring(); | |
292 | formattedWrite(writer, "Timer: %d", i); | |
293 | timerLabel.text = writer.data; | |
294 | timerLabel.width = cast(uint)timerLabel.text.length; | |
295 | if (i < 100) { | |
296 | i++; | |
297 | } | |
298 | bar.value = i; | |
299 | parent.repaint = true; | |
300 | }, true); | |
a06459bd KL |
301 | */ |
302 | } | |
303 | } | |
7d4115a5 KL |
304 | |
305 | /** | |
306 | * The demo application itself. | |
307 | */ | |
308 | class DemoApplication extends TApplication { | |
2420f903 KL |
309 | /** |
310 | * Public constructor | |
311 | */ | |
4328bb42 | 312 | public DemoApplication() throws Exception { |
8e688b92 KL |
313 | super(null, null); |
314 | new DemoMainWindow(this); | |
315 | ||
8e688b92 KL |
316 | // Add the menus |
317 | addFileMenu(); | |
318 | addEditMenu(); | |
319 | ||
320 | TMenu demoMenu = addMenu("&Demo"); | |
321 | TMenuItem item = demoMenu.addItem(2000, "&Checkable"); | |
322 | item.setCheckable(true); | |
323 | item = demoMenu.addItem(2001, "Disabled"); | |
324 | item.setEnabled(false); | |
325 | item = demoMenu.addItem(2002, "&Normal"); | |
326 | TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu"); | |
327 | item = demoMenu.addItem(2010, "N&ormal A&&D"); | |
328 | ||
329 | item = subMenu.addItem(2000, "&Checkable (sub)"); | |
330 | item.setCheckable(true); | |
331 | item = subMenu.addItem(2001, "Disabled (sub)"); | |
332 | item.setEnabled(false); | |
333 | item = subMenu.addItem(2002, "&Normal (sub)"); | |
334 | ||
335 | subMenu = subMenu.addSubMenu("Sub-&Menu"); | |
336 | item = subMenu.addItem(2000, "&Checkable (sub)"); | |
337 | item.setCheckable(true); | |
338 | item = subMenu.addItem(2001, "Disabled (sub)"); | |
339 | item.setEnabled(false); | |
340 | item = subMenu.addItem(2002, "&Normal (sub)"); | |
341 | ||
342 | addWindowMenu(); | |
343 | ||
2420f903 | 344 | } |
7d4115a5 KL |
345 | } |
346 | ||
347 | /** | |
348 | * This class provides a simple demonstration of Jexer's capabilities. | |
349 | */ | |
350 | public class Demo1 { | |
351 | /** | |
352 | * Main entry point. | |
353 | * | |
354 | * @param args Command line arguments | |
355 | */ | |
356 | public static void main(String [] args) { | |
8e688b92 KL |
357 | try { |
358 | DemoApplication app = new DemoApplication(); | |
359 | app.run(); | |
360 | } catch (Exception e) { | |
361 | e.printStackTrace(); | |
362 | } | |
7d4115a5 KL |
363 | } |
364 | ||
365 | } |