2 * Silly demo program for the NCursesPanel class.
4 * written by Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
6 * Demo code for NCursesMenu and NCursesForm written by
9 * $Id: demo.cc,v 1.32 2005/08/13 18:14:44 tom Exp $
13 #include "cursesapp.h"
17 extern "C" unsigned int sleep(unsigned int);
19 #undef index // needed for NeXT
22 // -------------------------------------------------------------------------
27 void run(int sleeptime) {
29 NCursesPanel *std = new NCursesPanel();
31 // Make a few small demo panels
33 NCursesPanel *u = new NCursesPanel(8,20,12,4);
34 NCursesPanel *v = new NCursesPanel(8,20,10,6);
35 NCursesPanel *w = new NCursesPanel(8,20,8,8);
36 NCursesPanel *x = new NCursesPanel(8,20,6,10);
37 NCursesPanel *y = new NCursesPanel(8,20,4,12);
38 NCursesPanel *z = new NCursesPanel(8,30,2,14);
40 // Draw something on the main screen, so we can see what happens
41 // when panels get moved or deleted.
44 std->move(std->height()/2,1);
45 std->hline(std->width()-2);
46 std->move(1,std->width()/2);
47 std->vline(std->height()-2);
48 std->addch(0,std->width()/2,ACS_TTEE);
49 std->addch(std->height()-1,std->width()/2,ACS_BTEE);
50 std->addch(std->height()/2,0,ACS_LTEE);
51 std->addch(std->height()/2,std->width()-1,ACS_RTEE);
52 std->addch(std->height()/2,std->width()/2,ACS_PLUS);
54 // Draw frames with titles around panels so that we can see where
55 // the panels are located.
56 u->boldframe("Win U");
58 w->boldframe("Win W");
60 y->boldframe("Win Y");
62 if (NCursesApplication::getApplication()->useColors()) {
63 u->bkgd(' '|COLOR_PAIR(1));
64 w->bkgd(' '|COLOR_PAIR(1));
65 y->bkgd(' '|COLOR_PAIR(1));
66 v->bkgd(' '|COLOR_PAIR(2));
67 x->bkgd(' '|COLOR_PAIR(2));
68 z->bkgd(' '|COLOR_PAIR(2));
71 // A refresh to any valid panel updates all panels and refreshes
72 // the screen. Using std is just convenient - We know it's always
73 // valid until the end of the program.
78 // Show what happens when panels are deleted and moved.
92 // show how it looks when a panel moves
101 // show how it looks when you raise a panel
116 // Don't forget to clean up the main screen. Since this is the
117 // last thing using NCursesWindow, this has the effect of
118 // shutting down ncurses and restoring the terminal state.
130 UserData(int x) : u(x) {}
131 int sleeptime() const { return u; }
134 // -------------------------------------------------------------------------
136 template<class T> class MyAction : public NCursesUserItem<T>
139 MyAction (const char* p_name,
141 : NCursesUserItem<T>(p_name, static_cast<const char*>(0), p_UserData)
144 virtual ~MyAction() {}
148 a.run(NCursesUserItem<T>::UserData()->sleeptime());
153 template class MyAction<UserData>;
154 template class NCURSES_IMPEXP NCursesUserItem<UserData>;
156 class QuitItem : public NCursesMenuItem
159 QuitItem() : NCursesMenuItem("Quit") {
167 // -------------------------------------------------------------------------
169 class Label : public NCursesFormField
172 Label(const char* title,
174 : NCursesFormField(1, static_cast<int>(::strlen(title)), row, col) {
176 options_off(O_EDIT|O_ACTIVE);
180 // -------------------------------------------------------------------------
182 class MyFieldType : public UserDefinedFieldType
187 bool field_check(NCursesFormField& f) {
190 bool char_check(int c) {
191 return (c==chk?TRUE:FALSE);
194 MyFieldType(int x) : chk(x) {
198 // -------------------------------------------------------------------------
200 class TestForm : public NCursesForm
203 NCursesFormField** F;
206 Enumeration_Field *eft;
208 static const char *weekdays[];
212 : NCursesForm(13, 51, (lines() - 15)/2, (cols() - 53)/2),
219 F = new NCursesFormField*[10];
220 mft = new MyFieldType('X');
221 ift = new Integer_Field(0,1,10);
222 eft = new Enumeration_Field(weekdays);
224 F[0] = new Label("Demo Entry Form",0,16);
225 F[1] = new Label("Weekday Enum",2,1);
226 F[2] = new Label("Number(1-10)",2,21);
227 F[3] = new Label("Only 'X'",2,35);
228 F[4] = new Label("Multiline Field (Dynamic and Scrollable)",5,1);
229 F[5] = new NCursesFormField(1,18,3,1);
230 F[6] = new NCursesFormField(1,12,3,21);
231 F[7] = new NCursesFormField(1,12,3,35);
232 F[8] = new NCursesFormField(4,46,6,1,2);
233 F[9] = new NCursesFormField();
235 InitForm(F,TRUE,TRUE);
238 F[5]->set_fieldtype(*eft);
239 F[6]->set_fieldtype(*ift);
241 F[7]->set_fieldtype(*mft);
242 F[7]->set_maximum_growth(20); // max. 20 characters
243 F[7]->options_off(O_STATIC); // make field dynamic
245 F[8]->set_maximum_growth(10); // max. 10 lines
246 F[8]->options_off(O_STATIC); // make field dynamic
249 TestForm& operator=(const TestForm& rhs)
257 TestForm(const TestForm& rhs)
258 : NCursesForm(rhs), F(0), mft(0), ift(0), eft(0)
269 const char* TestForm::weekdays[] = {
270 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
271 "Friday", "Saturday", NULL };
273 // -------------------------------------------------------------------------
275 class FormAction : public NCursesMenuItem
278 FormAction(const char *s) : NCursesMenuItem(s) {
283 Soft_Label_Key_Set* S = new Soft_Label_Key_Set;
284 for(int i=1; i <= S->labels(); i++) {
286 ::sprintf(buf,"Frm%02d",i);
287 (*S)[i] = buf; // Text
288 (*S)[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
290 NCursesApplication::getApplication()->push(*S);
292 NCursesApplication::getApplication()->pop();
297 // -------------------------------------------------------------------------
299 class PadAction : public NCursesMenuItem
302 PadAction(const char* s) : NCursesMenuItem(s) {
306 const int GRIDSIZE = 3;
307 const int PADSIZE = 200;
308 unsigned gridcount = 0;
311 NCursesPanel P(std.lines()-2,std.cols()-2,1,1);
312 NCursesFramedPad FP(P,PADSIZE,PADSIZE);
314 for (int i=0; i < PADSIZE; i++) {
315 for (int j=0; j < PADSIZE; j++) {
316 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
320 FP.addch(static_cast<chtype>('A' + (gridcount++ % 26)));
322 else if (i % GRIDSIZE == 0)
324 else if (j % GRIDSIZE == 0)
331 P.label("Pad Demo",NULL);
339 // -------------------------------------------------------------------------
341 class PassiveItem : public NCursesMenuItem
344 PassiveItem(const char* text) : NCursesMenuItem(text) {
345 options_off(O_SELECTABLE);
350 // -------------------------------------------------------------------------
352 class ScanAction : public NCursesMenuItem
355 ScanAction(const char* s) : NCursesMenuItem(s) {
359 NCursesPanel *std = new NCursesPanel();
361 NCursesPanel *w = new NCursesPanel(std->lines() - 2, std->cols() - 2, 1, 1);
365 NCursesPanel *s = new NCursesPanel(w->lines() - 6, w->cols() - 6, 3, 3);
369 s->printw("Enter decimal integers. The running total will be shown\n");
372 while (nvalue != 0) {
374 s->scanw("%d", &nvalue);
376 s->printw("%d: ", result += nvalue);
380 s->printw("\nPress any key to continue...");
392 // -------------------------------------------------------------------------
394 class MyMenu : public NCursesMenu
404 : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2),
408 I = new NCursesMenuItem*[1+n_items];
409 I[0] = new PassiveItem("One");
410 I[1] = new PassiveItem("Two");
411 I[2] = new MyAction<UserData> ("Silly", u);
412 I[3] = new FormAction("Form");
413 I[4] = new PadAction("Pad");
414 I[5] = new ScanAction("Scan");
415 I[6] = new QuitItem();
416 I[7] = new NCursesMenuItem(); // Terminating empty item
418 InitMenu(I,TRUE,TRUE);
420 P = new NCursesPanel(1,n_items,LINES-1,1);
421 boldframe("Demo","Silly");
425 MyMenu& operator=(const MyMenu& rhs)
433 MyMenu(const MyMenu& rhs)
434 : NCursesMenu(rhs), P(0), I(0), u(0)
445 virtual void On_Menu_Init()
447 NCursesWindow W(::stdscr);
450 for(int i=1; i<=count(); i++)
452 P->bkgd(W.getbkgd());
456 virtual void On_Menu_Termination()
463 virtual void On_Item_Init(NCursesMenuItem& item)
465 P->move(0,item.index());
466 P->attron(A_REVERSE);
467 P->printw("%1d",1+item.index());
468 P->attroff(A_REVERSE);
472 virtual void On_Item_Termination(NCursesMenuItem& item)
474 P->move(0,item.index());
475 P->attroff(A_REVERSE);
476 P->printw("%1d",1+item.index());
481 // -------------------------------------------------------------------------
483 class TestApplication : public NCursesApplication
486 int titlesize() const { return 1; }
488 Soft_Label_Key_Set::Label_Layout useSLKs() const {
489 return Soft_Label_Key_Set::PC_Style_With_Index;
491 void init_labels(Soft_Label_Key_Set& S) const;
494 TestApplication() : NCursesApplication(TRUE) {
500 void TestApplication::init_labels(Soft_Label_Key_Set& S) const
502 for(int i=1; i <= S.labels(); i++) {
504 ::sprintf(buf,"Key%02d",i);
506 S[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
510 void TestApplication::title()
512 const char * const titleText = "Simple C++ Binding Demo";
513 const int len = ::strlen(titleText);
515 titleWindow->bkgd(screen_titles());
516 titleWindow->addstr(0,(titleWindow->cols() - len)/2, titleText);
517 titleWindow->noutrefresh();
521 int TestApplication::run()
529 // -------------------------------------------------------------------------
531 static TestApplication Demo;