checkbox working
[fanfix.git] / demos / Demo1.java
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 */
33
34 import jexer.*;
35 import jexer.menu.*;
36
37 class DemoCheckboxWindow extends TWindow {
38
39 /**
40 * Constructor
41 */
42 DemoCheckboxWindow(TApplication parent) {
43 this(parent, CENTERED | RESIZABLE);
44 }
45
46 /**
47 * Constructor
48 */
49 DemoCheckboxWindow(TApplication parent, int flags) {
50 // Construct a demo window. X and Y don't matter because it
51 // will be centered on screen.
52 super(parent, "Radiobuttons and Checkboxes", 0, 0, 60, 15, flags);
53
54 int row = 1;
55
56 // Add some widgets
57 addLabel("Check box example 1", 1, row);
58 addCheckbox(35, row++, "Checkbox 1", false);
59 addLabel("Check box example 2", 1, row);
60 addCheckbox(35, row++, "Checkbox 2", true);
61 row += 2;
62
63 /*
64 auto group = addRadioGroup(1, row, "Group 1");
65 group.addRadioButton("Radio option 1");
66 group.addRadioButton("Radio option 2");
67 group.addRadioButton("Radio option 3");
68
69 addButton("&Close Window", (width - 14) / 2, height - 4,
70 {
71 application.closeWindow(this);
72 }
73
74 );
75 */
76 }
77
78 }
79
80
81 class DemoMsgBoxWindow extends TWindow {
82 /*
83 private void openYNCMessageBox() {
84 application.messageBox("Yes/No/Cancel MessageBox",
85 q"EOS
86 This is an example of a Yes/No/Cancel MessageBox.
87
88 Note that the MessageBox text can span multiple
89 lines.
90
91 The default result (if someone hits the top-left
92 close button) is CANCEL.
93 EOS",
94 TMessageBox.Type.YESNOCANCEL);
95 }
96
97 private void openYNMessageBox() {
98 application.messageBox("Yes/No MessageBox",
99 q"EOS
100 This is an example of a Yes/No MessageBox.
101
102 Note that the MessageBox text can span multiple
103 lines.
104
105 The default result (if someone hits the top-left
106 close button) is NO.
107 EOS",
108 TMessageBox.Type.YESNO);
109 }
110
111 private void openOKCMessageBox() {
112 application.messageBox("OK/Cancel MessageBox",
113 q"EOS
114 This is an example of a OK/Cancel MessageBox.
115
116 Note that the MessageBox text can span multiple
117 lines.
118
119 The default result (if someone hits the top-left
120 close button) is CANCEL.
121 EOS",
122 TMessageBox.Type.OKCANCEL);
123 }
124
125 private void openOKMessageBox() {
126 application.messageBox("OK MessageBox",
127 q"EOS
128 This is an example of a OK MessageBox. This is the
129 default MessageBox.
130
131 Note that the MessageBox text can span multiple
132 lines.
133
134 The default result (if someone hits the top-left
135 close button) is OK.
136 EOS",
137 TMessageBox.Type.OK);
138 }
139
140 */
141
142 /**
143 * Constructor.
144 */
145 DemoMsgBoxWindow(final TApplication parent) {
146 this(parent, TWindow.CENTERED | TWindow.RESIZABLE);
147 }
148
149 /**
150 * Constructor.
151 */
152 DemoMsgBoxWindow(final TApplication parent, final int flags) {
153 // Construct a demo window. X and Y don't matter because it
154 // will be centered on screen.
155 super(parent, "Message Boxes", 0, 0, 60, 15, flags);
156 /*
157 uint row = 1;
158
159 // Add some widgets
160 addLabel("Default OK message box", 1, row);
161 addButton("Open O&K MB", 35, row, &openOKMessageBox);
162 row += 2;
163
164 addLabel("OK/Cancel message box", 1, row);
165 addButton("O&pen OKC MB", 35, row, &openOKCMessageBox);
166 row += 2;
167
168 addLabel("Yes/No message box", 1, row);
169 addButton("Open &YN MB", 35, row, &openYNMessageBox);
170 row += 2;
171
172 addLabel("Yes/No/Cancel message box", 1, row);
173 addButton("Ope&n YNC MB", 35, row, &openYNCMessageBox);
174 row += 2;
175
176 addLabel("Input box", 1, row);
177 addButton("Open &input box", 35, row,
178 {
179 application.inputBox("Input Box",
180 q"EOS
181 This is an example of an InputBox.
182
183 Note that the InputBox text can span multiple
184 lines.
185 EOS",
186 "some input text");
187 }
188 );
189
190 addButton("&Close Window", (width - 14) / 2, height - 4,
191 {
192 application.closeWindow(this);
193 }
194 );
195 */
196 }
197 }
198
199
200 class DemoMainWindow extends TWindow {
201 /*
202 // Timer that increments a number
203 private TTimer timer;
204
205 // The modal window is a more low-level example of controlling a window
206 // "from the outside". Most windows will probably subclass TWindow and
207 // do this kind of logic on their own.
208 private TWindow modalWindow;
209 private void openModalWindow() {
210 modalWindow = application.addWindow("Demo Modal Window", 0, 0,
211 58, 15, TWindow.Flag.MODAL);
212 modalWindow.addLabel("This is an example of a very braindead modal window.", 1, 1);
213 modalWindow.addLabel("Modal windows are centered by default.", 1, 2);
214 modalWindow.addButton("&Close", (modalWindow.width - 8)/2,
215 modalWindow.height - 4, &modalWindowClose);
216 }
217 private void modalWindowClose() {
218 application.closeWindow(modalWindow);
219 }
220
221 /// We need to override onClose so that the timer will no longer be
222 /// called after we close the window. TTimers currently are completely
223 /// unaware of the rest of the UI classes.
224 override public void onClose() {
225 application.removeTimer(timer);
226 }
227 */
228
229 /**
230 * Construct demo window. It will be centered on screen.
231 */
232 public DemoMainWindow(TApplication parent) {
233 this(parent, CENTERED | RESIZABLE);
234 }
235
236 /**
237 * Constructor.
238 */
239 private DemoMainWindow(TApplication parent, int flags) {
240 // Construct a demo window. X and Y don't matter because it will be
241 // centered on screen.
242 super(parent, "Demo Window", 0, 0, 60, 23, flags);
243
244 int row = 1;
245
246 // Add some widgets
247 if (!isModal()) {
248 addLabel("Message Boxes", 1, row);
249 addButton("&MessageBoxes", 35, row,
250 new TAction() {
251 public void DO() {
252 new DemoMsgBoxWindow(getApplication());
253 }
254 }
255 );
256 }
257 row += 2;
258
259 addLabel("Open me as modal", 1, row);
260 addButton("W&indow", 35, row,
261 new TAction() {
262 public void DO() {
263 new DemoMainWindow(getApplication(), MODAL);
264 }
265 }
266 );
267
268 row += 2;
269
270 /*
271 addLabel("Variable-width text field:", 1, row);
272 addField(35, row++, 15, false, "Field text");
273
274 addLabel("Fixed-width text field:", 1, row);
275 addField(35, row, 15, true);
276 row += 2;
277 */
278
279 if (!isModal()) {
280 addLabel("Radio buttons and checkboxes", 1, row);
281 addButton("&Checkboxes", 35, row,
282 new TAction() {
283 public void DO() {
284 new DemoCheckboxWindow(getApplication(), MODAL);
285 }
286 }
287 );
288 }
289 row += 2;
290
291 /*
292 if (!isModal()) {
293 addLabel("Editor window", 1, row);
294 addButton("Edito&r", 35, row,
295 {
296 new TEditor(application, 0, 0, 60, 15);
297 }
298 );
299 }
300 row += 2;
301
302 if (!isModal()) {
303 addLabel("Text areas", 1, row);
304 addButton("&Text", 35, row,
305 {
306 new DemoTextWindow(application);
307 }
308 );
309 }
310 row += 2;
311
312 if (!isModal()) {
313 addLabel("Tree views", 1, row);
314 addButton("Tree&View", 35, row,
315 {
316 new DemoTreeViewWindow(application);
317 }
318 );
319 }
320 row += 2;
321
322 if (!isModal()) {
323 addLabel("Terminal", 1, row);
324 addButton("Termi&nal", 35, row,
325 {
326 application.openTerminal(0, 0);
327 }
328 );
329 }
330 row += 2;
331
332 TProgressBar bar = addProgressBar(1, row, 22);
333 row++;
334 TLabel timerLabel = addLabel("Timer", 1, row);
335 timer = parent.addTimer(100,
336 {
337 static int i = 0;
338 auto writer = appender!dstring();
339 formattedWrite(writer, "Timer: %d", i);
340 timerLabel.text = writer.data;
341 timerLabel.width = cast(uint)timerLabel.text.length;
342 if (i < 100) {
343 i++;
344 }
345 bar.value = i;
346 parent.repaint = true;
347 }, true);
348 */
349 }
350 }
351
352 /**
353 * The demo application itself.
354 */
355 class DemoApplication extends TApplication {
356 /**
357 * Public constructor
358 */
359 public DemoApplication() throws Exception {
360 super(null, null);
361 new DemoMainWindow(this);
362
363 // Add the menus
364 addFileMenu();
365 addEditMenu();
366
367 TMenu demoMenu = addMenu("&Demo");
368 TMenuItem item = demoMenu.addItem(2000, "&Checkable");
369 item.setCheckable(true);
370 item = demoMenu.addItem(2001, "Disabled");
371 item.setEnabled(false);
372 item = demoMenu.addItem(2002, "&Normal");
373 TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu");
374 item = demoMenu.addItem(2010, "N&ormal A&&D");
375
376 item = subMenu.addItem(2000, "&Checkable (sub)");
377 item.setCheckable(true);
378 item = subMenu.addItem(2001, "Disabled (sub)");
379 item.setEnabled(false);
380 item = subMenu.addItem(2002, "&Normal (sub)");
381
382 subMenu = subMenu.addSubMenu("Sub-&Menu");
383 item = subMenu.addItem(2000, "&Checkable (sub)");
384 item.setCheckable(true);
385 item = subMenu.addItem(2001, "Disabled (sub)");
386 item.setEnabled(false);
387 item = subMenu.addItem(2002, "&Normal (sub)");
388
389 addWindowMenu();
390
391 }
392 }
393
394 /**
395 * This class provides a simple demonstration of Jexer's capabilities.
396 */
397 public class Demo1 {
398 /**
399 * Main entry point.
400 *
401 * @param args Command line arguments
402 */
403 public static void main(String [] args) {
404 try {
405 DemoApplication app = new DemoApplication();
406 app.run();
407 } catch (Exception e) {
408 e.printStackTrace();
409 }
410 }
411
412 }