1 // * This makes emacs happy -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
31 * Silly demo program for the NCursesPanel class.
33 * written by Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
35 * Demo code for NCursesMenu and NCursesForm written by
38 * $Id: demo.cc,v 1.36 2007/04/07 20:24:49 tom Exp $
42 #include "cursesapp.h"
46 extern "C" unsigned int sleep(unsigned int);
48 #undef index // needed for NeXT
51 // -------------------------------------------------------------------------
56 void run(int sleeptime) {
58 NCursesPanel *mystd = new NCursesPanel();
60 // Make a few small demo panels
62 NCursesPanel *u = new NCursesPanel(8, 20, 12, 4);
63 NCursesPanel *v = new NCursesPanel(8, 20, 10, 6);
64 NCursesPanel *w = new NCursesPanel(8, 20, 8, 8);
65 NCursesPanel *x = new NCursesPanel(8, 20, 6, 10);
66 NCursesPanel *y = new NCursesPanel(8, 20, 4, 12);
67 NCursesPanel *z = new NCursesPanel(8, 30, 2, 14);
69 // Draw something on the main screen, so we can see what happens
70 // when panels get moved or deleted.
73 mystd->move(mystd->height()/2, 1);
74 mystd->hline(mystd->width()-2);
75 mystd->move(1, mystd->width()/2);
76 mystd->vline(mystd->height()-2);
77 mystd->addch(0, mystd->width()/2, ACS_TTEE);
78 mystd->addch(mystd->height()-1, mystd->width()/2, ACS_BTEE);
79 mystd->addch(mystd->height()/2, 0, ACS_LTEE);
80 mystd->addch(mystd->height()/2, mystd->width()-1, ACS_RTEE);
81 mystd->addch(mystd->height()/2, mystd->width()/2, ACS_PLUS);
83 // Draw frames with titles around panels so that we can see where
84 // the panels are located.
85 u->boldframe("Win U");
87 w->boldframe("Win W");
89 y->boldframe("Win Y");
91 if (NCursesApplication::getApplication()->useColors()) {
92 u->bkgd(' '|COLOR_PAIR(1));
93 w->bkgd(' '|COLOR_PAIR(1));
94 y->bkgd(' '|COLOR_PAIR(1));
95 v->bkgd(' '|COLOR_PAIR(2));
96 x->bkgd(' '|COLOR_PAIR(2));
97 z->bkgd(' '|COLOR_PAIR(2));
100 // A refresh to any valid panel updates all panels and refreshes
101 // the screen. Using mystd is just convenient - We know it's always
102 // valid until the end of the program.
107 // Show what happens when panels are deleted and moved.
121 // show how it looks when a panel moves
130 // show how it looks when you raise a panel
145 // Don't forget to clean up the main screen. Since this is the
146 // last thing using NCursesWindow, this has the effect of
147 // shutting down ncurses and restoring the terminal state.
159 UserData(int x) : u(x) {}
160 int sleeptime() const { return u; }
163 // -------------------------------------------------------------------------
165 template<class T> class MyAction : public NCursesUserItem<T>
168 MyAction (const char* p_name,
170 : NCursesUserItem<T>(p_name, static_cast<const char*>(0), p_UserData)
173 virtual ~MyAction() {}
177 a.run(NCursesUserItem<T>::UserData()->sleeptime());
182 template class MyAction<UserData>;
183 template class NCURSES_IMPEXP NCursesUserItem<UserData>;
185 class QuitItem : public NCursesMenuItem
188 QuitItem() : NCursesMenuItem("Quit") {
196 // -------------------------------------------------------------------------
198 class Label : public NCursesFormField
201 Label(const char* title,
203 : NCursesFormField(1, static_cast<int>(::strlen(title)), row, col) {
205 options_off(O_EDIT|O_ACTIVE);
209 // -------------------------------------------------------------------------
211 class MyFieldType : public UserDefinedFieldType
216 bool field_check(NCursesFormField& f) {
219 bool char_check(int c) {
220 return (c==chk?TRUE:FALSE);
223 MyFieldType(int x) : chk(x) {
227 // -------------------------------------------------------------------------
229 class TestForm : public NCursesForm
232 NCursesFormField** F;
235 Enumeration_Field *eft;
237 static const char *weekdays[];
241 : NCursesForm(13, 51, (lines() - 15)/2, (cols() - 53)/2),
248 F = new NCursesFormField*[10];
249 mft = new MyFieldType('X');
250 ift = new Integer_Field(0, 1, 10);
251 eft = new Enumeration_Field(weekdays);
253 F[0] = new Label("Demo Entry Form", 0, 16);
254 F[1] = new Label("Weekday Enum", 2, 1);
255 F[2] = new Label("Number(1-10)", 2, 21);
256 F[3] = new Label("Only 'X'", 2, 35);
257 F[4] = new Label("Multiline Field (Dynamic and Scrollable)", 5, 1);
258 F[5] = new NCursesFormField(1, 18, 3, 1);
259 F[6] = new NCursesFormField(1, 12, 3, 21);
260 F[7] = new NCursesFormField(1, 12, 3, 35);
261 F[8] = new NCursesFormField(4, 46, 6, 1, 2);
262 F[9] = new NCursesFormField();
264 InitForm(F, TRUE, TRUE);
267 F[5]->set_fieldtype(*eft);
268 F[6]->set_fieldtype(*ift);
270 F[7]->set_fieldtype(*mft);
271 F[7]->set_maximum_growth(20); // max. 20 characters
272 F[7]->options_off(O_STATIC); // make field dynamic
274 F[8]->set_maximum_growth(10); // max. 10 lines
275 F[8]->options_off(O_STATIC); // make field dynamic
278 TestForm& operator=(const TestForm& rhs)
286 TestForm(const TestForm& rhs)
287 : NCursesForm(rhs), F(0), mft(0), ift(0), eft(0)
298 const char* TestForm::weekdays[] = {
299 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
300 "Friday", "Saturday", NULL };
302 // -------------------------------------------------------------------------
304 class FormAction : public NCursesMenuItem
307 FormAction(const char *s) : NCursesMenuItem(s) {
312 Soft_Label_Key_Set* S = new Soft_Label_Key_Set;
313 for(int i=1; i <= S->labels(); i++) {
315 ::sprintf(buf, "Frm%02d", i);
316 (*S)[i] = buf; // Text
317 (*S)[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
319 NCursesApplication::getApplication()->push(*S);
321 NCursesApplication::getApplication()->pop();
326 // -------------------------------------------------------------------------
328 class PadAction : public NCursesMenuItem
331 PadAction(const char* s) : NCursesMenuItem(s) {
335 const int GRIDSIZE = 3;
336 const int PADSIZE = 200;
337 unsigned gridcount = 0;
340 NCursesPanel P(mystd.lines()-2, mystd.cols()-2, 1, 1);
341 NCursesFramedPad FP(P, PADSIZE, PADSIZE);
343 for (int i=0; i < PADSIZE; i++) {
344 for (int j=0; j < PADSIZE; j++) {
345 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
349 FP.addch(static_cast<chtype>('A' + (gridcount++ % 26)));
351 else if (i % GRIDSIZE == 0)
353 else if (j % GRIDSIZE == 0)
360 P.label("Pad Demo", NULL);
368 // -------------------------------------------------------------------------
370 class PassiveItem : public NCursesMenuItem
373 PassiveItem(const char* text) : NCursesMenuItem(text) {
374 options_off(O_SELECTABLE);
379 // -------------------------------------------------------------------------
381 class ScanAction : public NCursesMenuItem
384 ScanAction(const char* s) : NCursesMenuItem(s) {
388 NCursesPanel *mystd = new NCursesPanel();
390 NCursesPanel *w = new NCursesPanel(mystd->lines() - 2, mystd->cols() - 2, 1, 1);
394 NCursesPanel *s = new NCursesPanel(w->lines() - 6, w->cols() - 6, 3, 3);
398 s->printw("Enter decimal integers. The running total will be shown\n");
401 while (nvalue != 0) {
403 s->scanw("%d", &nvalue);
405 s->printw("%d: ", result += nvalue);
409 s->printw("\nPress any key to continue...");
421 // -------------------------------------------------------------------------
423 class MyMenu : public NCursesMenu
433 : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2),
437 I = new NCursesMenuItem*[1+n_items];
438 I[0] = new PassiveItem("One");
439 I[1] = new PassiveItem("Two");
440 I[2] = new MyAction<UserData> ("Silly", u);
441 I[3] = new FormAction("Form");
442 I[4] = new PadAction("Pad");
443 I[5] = new ScanAction("Scan");
444 I[6] = new QuitItem();
445 I[7] = new NCursesMenuItem(); // Terminating empty item
447 InitMenu(I, TRUE, TRUE);
449 P = new NCursesPanel(1, n_items, LINES-1, 1);
450 boldframe("Demo", "Silly");
454 MyMenu& operator=(const MyMenu& rhs)
462 MyMenu(const MyMenu& rhs)
463 : NCursesMenu(rhs), P(0), I(0), u(0)
474 virtual void On_Menu_Init()
476 NCursesWindow W(::stdscr);
479 for(int i=1; i<=count(); i++)
481 P->bkgd(W.getbkgd());
485 virtual void On_Menu_Termination()
492 virtual void On_Item_Init(NCursesMenuItem& item)
494 P->move(0, item.index());
495 P->attron(A_REVERSE);
496 P->printw("%1d", 1+item.index());
497 P->attroff(A_REVERSE);
501 virtual void On_Item_Termination(NCursesMenuItem& item)
503 P->move(0, item.index());
504 P->attroff(A_REVERSE);
505 P->printw("%1d", 1+item.index());
510 // -------------------------------------------------------------------------
512 class TestApplication : public NCursesApplication
515 int titlesize() const { return 1; }
517 Soft_Label_Key_Set::Label_Layout useSLKs() const {
518 return Soft_Label_Key_Set::PC_Style_With_Index;
520 void init_labels(Soft_Label_Key_Set& S) const;
523 TestApplication() : NCursesApplication(TRUE) {
529 void TestApplication::init_labels(Soft_Label_Key_Set& S) const
531 for(int i=1; i <= S.labels(); i++) {
533 ::sprintf(buf, "Key%02d", i);
535 S[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
539 void TestApplication::title()
541 const char * const titleText = "Simple C++ Binding Demo";
542 const int len = ::strlen(titleText);
544 titleWindow->bkgd(screen_titles());
545 titleWindow->addstr(0, (titleWindow->cols() - len)/2, titleText);
546 titleWindow->noutrefresh();
550 int TestApplication::run()
558 // -------------------------------------------------------------------------
560 static TestApplication *Demo = new TestApplication();