1 // * This makes emacs happy -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright 2018-2021,2023 Thomas E. Dickey *
4 * Copyright 1998-2012,2017 Free Software Foundation, Inc. *
6 * Permission is hereby granted, free of charge, to any person obtaining a *
7 * copy of this software and associated documentation files (the *
8 * "Software"), to deal in the Software without restriction, including *
9 * without limitation the rights to use, copy, modify, merge, publish, *
10 * distribute, distribute with modifications, sublicense, and/or sell *
11 * copies of the Software, and to permit persons to whom the Software is *
12 * furnished to do so, subject to the following conditions: *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * Except as contained in this notice, the name(s) of the above copyright *
26 * holders shall not be used in advertising or otherwise to promote the *
27 * sale, use or other dealings in this Software without prior written *
29 ****************************************************************************/
32 * Silly demo program for the NCursesPanel class.
34 * written by Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
36 * Demo code for NCursesMenu and NCursesForm written by
39 * $Id: demo.cc,v 1.52 2023/08/26 19:14:14 tom Exp $
43 #include "cursesapp.h"
47 #if (defined(_WIN32) || defined(_WIN64))
50 #define sleep(n) Sleep(n)
52 extern "C" unsigned int sleep(unsigned int);
55 #undef index // needed for NeXT
58 // -------------------------------------------------------------------------
63 void run(int sleeptime) {
65 NCursesPanel *mystd = new NCursesPanel();
67 // Make a few small demo panels
69 NCursesPanel *u = new NCursesPanel(8, 20, 12, 4);
70 NCursesPanel *v = new NCursesPanel(8, 20, 10, 6);
71 NCursesPanel *w = new NCursesPanel(8, 20, 8, 8);
72 NCursesPanel *x = new NCursesPanel(8, 20, 6, 10);
73 NCursesPanel *y = new NCursesPanel(8, 20, 4, 12);
74 NCursesPanel *z = new NCursesPanel(8, 30, 2, 14);
76 // Draw something on the main screen, so we can see what happens
77 // when panels get moved or deleted.
80 mystd->move(mystd->height()/2, 1);
81 mystd->hline(mystd->width()-2);
82 mystd->move(1, mystd->width()/2);
83 mystd->vline(mystd->height()-2);
84 mystd->addch(0, mystd->width()/2, ACS_TTEE);
85 mystd->addch(mystd->height()-1, mystd->width()/2, ACS_BTEE);
86 mystd->addch(mystd->height()/2, 0, ACS_LTEE);
87 mystd->addch(mystd->height()/2, mystd->width()-1, ACS_RTEE);
88 mystd->addch(mystd->height()/2, mystd->width()/2, ACS_PLUS);
90 // Draw frames with titles around panels so that we can see where
91 // the panels are located.
92 u->boldframe("Win U");
94 w->boldframe("Win W");
96 y->boldframe("Win Y");
98 if (NCursesApplication::getApplication()->useColors()) {
99 u->bkgd(' '|COLOR_PAIR(1));
100 w->bkgd(' '|COLOR_PAIR(1));
101 y->bkgd(' '|COLOR_PAIR(1));
102 v->bkgd(' '|COLOR_PAIR(2));
103 x->bkgd(' '|COLOR_PAIR(2));
104 z->bkgd(' '|COLOR_PAIR(2));
107 // A refresh to any valid panel updates all panels and refreshes
108 // the screen. Using mystd is just convenient - We know it is always
109 // valid until the end of the program.
114 // Show what happens when panels are deleted and moved.
128 // show how it looks when a panel moves
137 // show how it looks when you raise a panel
152 // Don't forget to clean up the main screen. Since this is the
153 // last thing using NCursesWindow, this has the effect of
154 // shutting down ncurses and restoring the terminal state.
166 UserData(int x) : u(x) {}
167 int sleeptime() const { return u; }
170 // -------------------------------------------------------------------------
172 template<class T> class NCURSES_CXX_IMPEXP MyAction : public NCursesUserItem<UserData>
175 MyAction (const char* p_name,
177 : NCursesUserItem<T>(p_name, static_cast<const char*>(0), p_UserData)
180 virtual ~MyAction() THROWS(NCursesException) {}
184 a.run(NCursesUserItem<T>::UserData()->sleeptime());
189 class QuitItem : public NCursesMenuItem
192 QuitItem() : NCursesMenuItem("Quit") {
200 // -------------------------------------------------------------------------
202 class Label : public NCursesFormField
205 Label(const char* title,
207 : NCursesFormField(1, static_cast<int>(::strlen(title)), row, col) {
209 options_off(O_EDIT|O_ACTIVE);
213 // -------------------------------------------------------------------------
215 class MyFieldType : public UserDefinedFieldType
220 bool field_check(NCursesFormField& f) {
224 bool char_check(int c) {
225 return (c==chk?TRUE:FALSE);
228 MyFieldType(int x) : chk(x) {
232 // -------------------------------------------------------------------------
234 class TestForm : public NCursesForm
237 NCursesFormField** F;
240 Enumeration_Field *eft;
242 static const char *weekdays[];
246 : NCursesForm(13, 51, (lines() - 15)/2, (cols() - 53)/2),
253 F = new NCursesFormField*[10];
254 mft = new MyFieldType('X');
255 ift = new Integer_Field(0, 1, 10);
256 eft = new Enumeration_Field(weekdays);
258 F[0] = new Label("Demo Entry Form", 0, 16);
259 F[1] = new Label("Weekday Enum", 2, 1);
260 F[2] = new Label("Number(1-10)", 2, 21);
261 F[3] = new Label("Only 'X'", 2, 35);
262 F[4] = new Label("Multiline Field (Dynamic and Scrollable)", 5, 1);
263 F[5] = new NCursesFormField(1, 18, 3, 1);
264 F[6] = new NCursesFormField(1, 12, 3, 21);
265 F[7] = new NCursesFormField(1, 12, 3, 35);
266 F[8] = new NCursesFormField(4, 46, 6, 1, 2);
267 F[9] = new NCursesFormField();
269 InitForm(F, TRUE, TRUE);
272 F[5]->set_fieldtype(*eft);
273 F[6]->set_fieldtype(*ift);
275 F[7]->set_fieldtype(*mft);
276 F[7]->set_maximum_growth(20); // max. 20 characters
277 F[7]->options_off(O_STATIC); // make field dynamic
279 F[8]->set_maximum_growth(10); // max. 10 lines
280 F[8]->options_off(O_STATIC); // make field dynamic
283 TestForm& operator=(const TestForm& rhs)
291 TestForm(const TestForm& rhs)
292 : NCursesForm(rhs), F(0), mft(0), ift(0), eft(0)
296 ~TestForm() THROWS(NCursesException) {
303 const char* TestForm::weekdays[] = {
304 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
305 "Friday", "Saturday", NULL };
307 // -------------------------------------------------------------------------
309 class FormAction : public NCursesMenuItem
312 FormAction(const char *s) : NCursesMenuItem(s) {
317 Soft_Label_Key_Set* S = new Soft_Label_Key_Set;
318 for(int i=1; i <= S->labels(); i++) {
321 ::_nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "Frm%02d", i % 100);
322 (*S)[i] = buf; // Text
323 (*S)[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
325 NCursesApplication::getApplication()->push(*S);
327 NCursesApplication::getApplication()->pop();
333 // -------------------------------------------------------------------------
335 class PadAction : public NCursesMenuItem
338 PadAction(const char* s) : NCursesMenuItem(s) {
342 const int GRIDSIZE = 3;
343 const int PADSIZE = 200;
344 unsigned gridcount = 0;
347 NCursesPanel P(mystd.lines()-2, mystd.cols()-2, 1, 1);
348 NCursesFramedPad FP(P, PADSIZE, PADSIZE);
350 for (int i=0; i < PADSIZE; i++) {
351 for (int j=0; j < PADSIZE; j++) {
352 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
356 FP.addch(static_cast<chtype>('A' + (gridcount++ % 26)));
358 else if (i % GRIDSIZE == 0)
360 else if (j % GRIDSIZE == 0)
367 P.label("Pad Demo", NULL);
375 // -------------------------------------------------------------------------
377 class PassiveItem : public NCursesMenuItem
380 PassiveItem(const char* text) : NCursesMenuItem(text) {
381 options_off(O_SELECTABLE);
386 // -------------------------------------------------------------------------
388 class ScanAction : public NCursesMenuItem
391 ScanAction(const char* s) : NCursesMenuItem(s) {
395 NCursesPanel *mystd = new NCursesPanel();
397 NCursesPanel *w = new NCursesPanel(mystd->lines() - 2, mystd->cols() - 2, 1, 1);
401 NCursesPanel *s = new NCursesPanel(w->lines() - 6, w->cols() - 6, 3, 3);
405 s->printw("Enter decimal integers. The running total will be shown\n");
408 while (nvalue != 0) {
410 s->scanw("%d", &nvalue);
412 s->printw("%d: ", result += nvalue);
416 s->printw("\nPress any key to continue...");
428 // -------------------------------------------------------------------------
430 class MyMenu : public NCursesMenu
440 : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2),
444 I = new NCursesMenuItem*[1+n_items];
445 I[0] = new PassiveItem("One");
446 I[1] = new PassiveItem("Two");
447 I[2] = new MyAction<UserData> ("Silly", u);
448 I[3] = new FormAction("Form");
449 I[4] = new PadAction("Pad");
450 I[5] = new ScanAction("Scan");
451 I[6] = new QuitItem();
452 I[7] = new NCursesMenuItem(); // Terminating empty item
454 InitMenu(I, TRUE, TRUE);
456 P = new NCursesPanel(1, n_items, LINES-1, 1);
457 boldframe("Demo", "Silly");
461 MyMenu& operator=(const MyMenu& rhs)
469 MyMenu(const MyMenu& rhs)
470 : NCursesMenu(rhs), P(0), I(0), u(0)
474 ~MyMenu() THROWS(NCursesException)
481 virtual void On_Menu_Init()
483 NCursesWindow W(::stdscr);
486 for(int i=1; i<=count(); i++)
488 P->bkgd(W.getbkgd());
492 virtual void On_Menu_Termination()
499 virtual void On_Item_Init(NCursesMenuItem& item)
501 P->move(0, item.index());
502 P->attron(A_REVERSE);
503 P->printw("%1d", 1+item.index());
504 P->attroff(A_REVERSE);
508 virtual void On_Item_Termination(NCursesMenuItem& item)
510 P->move(0, item.index());
511 P->attroff(A_REVERSE);
512 P->printw("%1d", 1+item.index());
517 // -------------------------------------------------------------------------
519 class TestApplication : public NCursesApplication
522 int titlesize() const { return 1; }
524 Soft_Label_Key_Set::Label_Layout useSLKs() const {
525 return Soft_Label_Key_Set::PC_Style_With_Index;
527 void init_labels(Soft_Label_Key_Set& S) const;
530 TestApplication() : NCursesApplication(TRUE) {
536 void TestApplication::init_labels(Soft_Label_Key_Set& S) const
538 for(int i=1; i <= S.labels(); i++) {
541 ::_nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "Key%02d", i % 100);
543 S[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
547 void TestApplication::title()
549 const char * const titleText = "Simple C++ Binding Demo";
550 const int len = ::strlen(titleText);
552 getTitleWindow()->bkgd(screen_titles());
553 getTitleWindow()->addstr(0, (getTitleWindow()->cols() - len)/2, titleText);
554 getTitleWindow()->noutrefresh();
558 int TestApplication::run()
566 // -------------------------------------------------------------------------
568 static TestApplication *Demo = new TestApplication();
570 #if (defined(_WIN32) || defined(_WIN64))
571 // This is actually only needed when ncurses is a dll