1 // * This makes emacs happy -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright (c) 1998-2007,2008 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.39 2008/12/07 02:07:34 juergen Exp $
42 #include "cursesapp.h"
51 extern "C" unsigned int sleep(unsigned int);
54 #undef index // needed for NeXT
57 // -------------------------------------------------------------------------
62 void run(int sleeptime) {
64 NCursesPanel *mystd = new NCursesPanel();
66 // Make a few small demo panels
68 NCursesPanel *u = new NCursesPanel(8, 20, 12, 4);
69 NCursesPanel *v = new NCursesPanel(8, 20, 10, 6);
70 NCursesPanel *w = new NCursesPanel(8, 20, 8, 8);
71 NCursesPanel *x = new NCursesPanel(8, 20, 6, 10);
72 NCursesPanel *y = new NCursesPanel(8, 20, 4, 12);
73 NCursesPanel *z = new NCursesPanel(8, 30, 2, 14);
75 // Draw something on the main screen, so we can see what happens
76 // when panels get moved or deleted.
79 mystd->move(mystd->height()/2, 1);
80 mystd->hline(mystd->width()-2);
81 mystd->move(1, mystd->width()/2);
82 mystd->vline(mystd->height()-2);
83 mystd->addch(0, mystd->width()/2, ACS_TTEE);
84 mystd->addch(mystd->height()-1, mystd->width()/2, ACS_BTEE);
85 mystd->addch(mystd->height()/2, 0, ACS_LTEE);
86 mystd->addch(mystd->height()/2, mystd->width()-1, ACS_RTEE);
87 mystd->addch(mystd->height()/2, mystd->width()/2, ACS_PLUS);
89 // Draw frames with titles around panels so that we can see where
90 // the panels are located.
91 u->boldframe("Win U");
93 w->boldframe("Win W");
95 y->boldframe("Win Y");
97 if (NCursesApplication::getApplication()->useColors()) {
98 u->bkgd(' '|COLOR_PAIR(1));
99 w->bkgd(' '|COLOR_PAIR(1));
100 y->bkgd(' '|COLOR_PAIR(1));
101 v->bkgd(' '|COLOR_PAIR(2));
102 x->bkgd(' '|COLOR_PAIR(2));
103 z->bkgd(' '|COLOR_PAIR(2));
106 // A refresh to any valid panel updates all panels and refreshes
107 // the screen. Using mystd is just convenient - We know it's always
108 // valid until the end of the program.
113 // Show what happens when panels are deleted and moved.
127 // show how it looks when a panel moves
136 // show how it looks when you raise a panel
151 // Don't forget to clean up the main screen. Since this is the
152 // last thing using NCursesWindow, this has the effect of
153 // shutting down ncurses and restoring the terminal state.
165 UserData(int x) : u(x) {}
166 int sleeptime() const { return u; }
169 // -------------------------------------------------------------------------
171 template<class T> class MyAction : public NCursesUserItem<T>
174 MyAction (const char* p_name,
176 : NCursesUserItem<T>(p_name, static_cast<const char*>(0), p_UserData)
179 virtual ~MyAction() {}
183 a.run(NCursesUserItem<T>::UserData()->sleeptime());
188 template class MyAction<UserData>;
189 template class NCURSES_IMPEXP NCursesUserItem<UserData>;
191 class QuitItem : public NCursesMenuItem
194 QuitItem() : NCursesMenuItem("Quit") {
202 // -------------------------------------------------------------------------
204 class Label : public NCursesFormField
207 Label(const char* title,
209 : NCursesFormField(1, static_cast<int>(::strlen(title)), row, col) {
211 options_off(O_EDIT|O_ACTIVE);
215 // -------------------------------------------------------------------------
217 class MyFieldType : public UserDefinedFieldType
222 bool field_check(NCursesFormField& f) {
225 bool char_check(int c) {
226 return (c==chk?TRUE:FALSE);
229 MyFieldType(int x) : chk(x) {
233 // -------------------------------------------------------------------------
235 class TestForm : public NCursesForm
238 NCursesFormField** F;
241 Enumeration_Field *eft;
243 static const char *weekdays[];
247 : NCursesForm(13, 51, (lines() - 15)/2, (cols() - 53)/2),
254 F = new NCursesFormField*[10];
255 mft = new MyFieldType('X');
256 ift = new Integer_Field(0, 1, 10);
257 eft = new Enumeration_Field(weekdays);
259 F[0] = new Label("Demo Entry Form", 0, 16);
260 F[1] = new Label("Weekday Enum", 2, 1);
261 F[2] = new Label("Number(1-10)", 2, 21);
262 F[3] = new Label("Only 'X'", 2, 35);
263 F[4] = new Label("Multiline Field (Dynamic and Scrollable)", 5, 1);
264 F[5] = new NCursesFormField(1, 18, 3, 1);
265 F[6] = new NCursesFormField(1, 12, 3, 21);
266 F[7] = new NCursesFormField(1, 12, 3, 35);
267 F[8] = new NCursesFormField(4, 46, 6, 1, 2);
268 F[9] = new NCursesFormField();
270 InitForm(F, TRUE, TRUE);
273 F[5]->set_fieldtype(*eft);
274 F[6]->set_fieldtype(*ift);
276 F[7]->set_fieldtype(*mft);
277 F[7]->set_maximum_growth(20); // max. 20 characters
278 F[7]->options_off(O_STATIC); // make field dynamic
280 F[8]->set_maximum_growth(10); // max. 10 lines
281 F[8]->options_off(O_STATIC); // make field dynamic
284 TestForm& operator=(const TestForm& rhs)
292 TestForm(const TestForm& rhs)
293 : NCursesForm(rhs), F(0), mft(0), ift(0), eft(0)
304 const char* TestForm::weekdays[] = {
305 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
306 "Friday", "Saturday", NULL };
308 // -------------------------------------------------------------------------
310 class FormAction : public NCursesMenuItem
313 FormAction(const char *s) : NCursesMenuItem(s) {
318 Soft_Label_Key_Set* S = new Soft_Label_Key_Set;
319 for(int i=1; i <= S->labels(); i++) {
322 ::sprintf(buf, "Frm%02d", i);
323 (*S)[i] = buf; // Text
324 (*S)[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
326 NCursesApplication::getApplication()->push(*S);
328 NCursesApplication::getApplication()->pop();
334 // -------------------------------------------------------------------------
336 class PadAction : public NCursesMenuItem
339 PadAction(const char* s) : NCursesMenuItem(s) {
343 const int GRIDSIZE = 3;
344 const int PADSIZE = 200;
345 unsigned gridcount = 0;
348 NCursesPanel P(mystd.lines()-2, mystd.cols()-2, 1, 1);
349 NCursesFramedPad FP(P, PADSIZE, PADSIZE);
351 for (int i=0; i < PADSIZE; i++) {
352 for (int j=0; j < PADSIZE; j++) {
353 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
357 FP.addch(static_cast<chtype>('A' + (gridcount++ % 26)));
359 else if (i % GRIDSIZE == 0)
361 else if (j % GRIDSIZE == 0)
368 P.label("Pad Demo", NULL);
376 // -------------------------------------------------------------------------
378 class PassiveItem : public NCursesMenuItem
381 PassiveItem(const char* text) : NCursesMenuItem(text) {
382 options_off(O_SELECTABLE);
387 // -------------------------------------------------------------------------
389 class ScanAction : public NCursesMenuItem
392 ScanAction(const char* s) : NCursesMenuItem(s) {
396 NCursesPanel *mystd = new NCursesPanel();
398 NCursesPanel *w = new NCursesPanel(mystd->lines() - 2, mystd->cols() - 2, 1, 1);
402 NCursesPanel *s = new NCursesPanel(w->lines() - 6, w->cols() - 6, 3, 3);
406 s->printw("Enter decimal integers. The running total will be shown\n");
409 while (nvalue != 0) {
411 s->scanw("%d", &nvalue);
413 s->printw("%d: ", result += nvalue);
417 s->printw("\nPress any key to continue...");
429 // -------------------------------------------------------------------------
431 class MyMenu : public NCursesMenu
441 : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2),
445 I = new NCursesMenuItem*[1+n_items];
446 I[0] = new PassiveItem("One");
447 I[1] = new PassiveItem("Two");
448 I[2] = new MyAction<UserData> ("Silly", u);
449 I[3] = new FormAction("Form");
450 I[4] = new PadAction("Pad");
451 I[5] = new ScanAction("Scan");
452 I[6] = new QuitItem();
453 I[7] = new NCursesMenuItem(); // Terminating empty item
455 InitMenu(I, TRUE, TRUE);
457 P = new NCursesPanel(1, n_items, LINES-1, 1);
458 boldframe("Demo", "Silly");
462 MyMenu& operator=(const MyMenu& rhs)
470 MyMenu(const MyMenu& rhs)
471 : NCursesMenu(rhs), P(0), I(0), u(0)
482 virtual void On_Menu_Init()
484 NCursesWindow W(::stdscr);
487 for(int i=1; i<=count(); i++)
489 P->bkgd(W.getbkgd());
493 virtual void On_Menu_Termination()
500 virtual void On_Item_Init(NCursesMenuItem& item)
502 P->move(0, item.index());
503 P->attron(A_REVERSE);
504 P->printw("%1d", 1+item.index());
505 P->attroff(A_REVERSE);
509 virtual void On_Item_Termination(NCursesMenuItem& item)
511 P->move(0, item.index());
512 P->attroff(A_REVERSE);
513 P->printw("%1d", 1+item.index());
518 // -------------------------------------------------------------------------
520 class TestApplication : public NCursesApplication
523 int titlesize() const { return 1; }
525 Soft_Label_Key_Set::Label_Layout useSLKs() const {
526 return Soft_Label_Key_Set::PC_Style_With_Index;
528 void init_labels(Soft_Label_Key_Set& S) const;
531 TestApplication() : NCursesApplication(TRUE) {
537 void TestApplication::init_labels(Soft_Label_Key_Set& S) const
539 for(int i=1; i <= S.labels(); i++) {
542 ::sprintf(buf, "Key%02d", i);
544 S[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
548 void TestApplication::title()
550 const char * const titleText = "Simple C++ Binding Demo";
551 const int len = ::strlen(titleText);
553 titleWindow->bkgd(screen_titles());
554 titleWindow->addstr(0, (titleWindow->cols() - len)/2, titleText);
555 titleWindow->noutrefresh();
559 int TestApplication::run()
567 // -------------------------------------------------------------------------
569 static TestApplication *Demo = new TestApplication();