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
8 * Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en
10 * $Id: demo.cc,v 1.22 2002/07/06 15:47:52 juergen Exp $
13 #include "cursesapp.h"
21 extern "C" unsigned int sleep(unsigned int);
23 #undef index // needed for NeXT
26 // -------------------------------------------------------------------------
31 void run(int sleeptime) {
33 NCursesPanel *std = new NCursesPanel();
35 // Make a few small demo panels
37 NCursesPanel *u = new NCursesPanel(8,20,12,4);
38 NCursesPanel *v = new NCursesPanel(8,20,10,6);
39 NCursesPanel *w = new NCursesPanel(8,20,8,8);
40 NCursesPanel *x = new NCursesPanel(8,20,6,10);
41 NCursesPanel *y = new NCursesPanel(8,20,4,12);
42 NCursesPanel *z = new NCursesPanel(8,30,2,14);
44 // Draw something on the main screen, so we can see what happens
45 // when panels get moved or deleted.
48 std->move(std->height()/2,1);
49 std->hline(std->width()-2);
50 std->move(1,std->width()/2);
51 std->vline(std->height()-2);
52 std->addch(0,std->width()/2,ACS_TTEE);
53 std->addch(std->height()-1,std->width()/2,ACS_BTEE);
54 std->addch(std->height()/2,0,ACS_LTEE);
55 std->addch(std->height()/2,std->width()-1,ACS_RTEE);
56 std->addch(std->height()/2,std->width()/2,ACS_PLUS);
58 // Draw frames with titles around panels so that we can see where
59 // the panels are located.
60 u->boldframe("Win U");
62 w->boldframe("Win W");
64 y->boldframe("Win Y");
66 if (NCursesApplication::getApplication()->useColors()) {
67 u->bkgd(' '|COLOR_PAIR(1));
68 w->bkgd(' '|COLOR_PAIR(1));
69 y->bkgd(' '|COLOR_PAIR(1));
70 v->bkgd(' '|COLOR_PAIR(2));
71 x->bkgd(' '|COLOR_PAIR(2));
72 z->bkgd(' '|COLOR_PAIR(2));
75 // A refresh to any valid panel updates all panels and refreshes
76 // the screen. Using std is just convenient - We know it's always
77 // valid until the end of the program.
82 // Show what happens when panels are deleted and moved.
96 // show how it looks when a panel moves
105 // show how it looks when you raise a panel
120 // Don't forget to clean up the main screen. Since this is the
121 // last thing using NCursesWindow, this has the effect of
122 // shutting down ncurses and restoring the terminal state.
134 UserData(int x) : u(x) {}
135 int sleeptime() const { return u; }
138 // -------------------------------------------------------------------------
140 template<class T> class MyAction : public NCursesUserItem<T>
143 MyAction (const char* p_name,
145 : NCursesUserItem<T>(p_name, (const char*)0, p_UserData)
152 a.run(NCursesUserItem<T>::UserData()->sleeptime());
157 class QuitItem : public NCursesMenuItem
160 QuitItem() : NCursesMenuItem("Quit") {
168 // -------------------------------------------------------------------------
170 class Label : public NCursesFormField
173 Label(const char* title,
175 : NCursesFormField(1,(int)::strlen(title),row,col) {
177 options_off(O_EDIT|O_ACTIVE);
181 // -------------------------------------------------------------------------
183 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 char *weekdays[];
211 TestForm() : NCursesForm(13,51,(lines()-15)/2,(cols()-53)/2) {
213 F = new NCursesFormField*[10];
214 mft = new MyFieldType('X');
215 ift = new Integer_Field(0,1,10);
216 eft = new Enumeration_Field(weekdays);
218 F[0] = new Label("Demo Entry Form",0,16);
219 F[1] = new Label("Weekday Enum",2,1);
220 F[2] = new Label("Number(1-10)",2,21);
221 F[3] = new Label("Only 'X'",2,35);
222 F[4] = new Label("Multiline Field (Dynamic and Scrollable)",5,1);
223 F[5] = new NCursesFormField(1,18,3,1);
224 F[6] = new NCursesFormField(1,12,3,21);
225 F[7] = new NCursesFormField(1,12,3,35);
226 F[8] = new NCursesFormField(4,46,6,1,2);
227 F[9] = new NCursesFormField();
229 InitForm(F,TRUE,TRUE);
232 F[5]->set_fieldtype(*eft);
233 F[6]->set_fieldtype(*ift);
235 F[7]->set_fieldtype(*mft);
236 F[7]->set_maximum_growth(20); // max. 20 characters
237 F[7]->options_off(O_STATIC); // make field dynamic
239 F[8]->set_maximum_growth(10); // max. 10 lines
240 F[8]->options_off(O_STATIC); // make field dynamic
250 char* TestForm::weekdays[] = {
251 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
252 "Friday", "Saturday", (char *)0 };
254 // -------------------------------------------------------------------------
256 class FormAction : public NCursesMenuItem
259 FormAction(const char *s) : NCursesMenuItem(s) {
264 Soft_Label_Key_Set* S = new Soft_Label_Key_Set;
265 for(int i=1; i <= S->labels(); i++) {
267 ::sprintf(buf,"Frm%02d",i);
268 (*S)[i] = buf; // Text
269 (*S)[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
271 NCursesApplication::getApplication()->push(*S);
273 NCursesApplication::getApplication()->pop();
278 // -------------------------------------------------------------------------
280 class PadAction : public NCursesMenuItem
283 PadAction(const char* s) : NCursesMenuItem(s) {
287 const int GRIDSIZE = 3;
288 const int PADSIZE = 200;
289 unsigned gridcount = 0;
292 NCursesPanel P(std.lines()-2,std.cols()-2,1,1);
293 NCursesFramedPad FP(P,PADSIZE,PADSIZE);
295 for (int i=0; i < PADSIZE; i++) {
296 for (int j=0; j < PADSIZE; j++) {
297 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
301 FP.addch((chtype)('A' + (gridcount++ % 26)));
303 else if (i % GRIDSIZE == 0)
305 else if (j % GRIDSIZE == 0)
312 P.label("Pad Demo",NULL);
320 // -------------------------------------------------------------------------
322 class PassiveItem : public NCursesMenuItem {
324 PassiveItem(const char* text) : NCursesMenuItem(text) {
325 options_off(O_SELECTABLE);
330 // -------------------------------------------------------------------------
332 class ScanAction : public NCursesMenuItem
335 ScanAction(const char* s) : NCursesMenuItem(s) {
339 NCursesPanel *std = new NCursesPanel();
341 NCursesPanel *w = new NCursesPanel(std->lines() - 2, std->cols() - 2, 1, 1);
345 NCursesPanel *s = new NCursesPanel(w->lines() - 6, w->cols() - 6, 3, 3);
349 s->printw("Enter decimal integers. The running total will be shown\n");
354 s->scanw("%d", &value);
356 s->printw("%d: ", result += value);
360 s->printw("\nPress any key to continue...");
372 // -------------------------------------------------------------------------
374 class MyMenu : public NCursesMenu
384 : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2)
387 I = new NCursesMenuItem*[1+n_items];
388 I[0] = new PassiveItem("One");
389 I[1] = new PassiveItem("Two");
390 I[2] = new MyAction<UserData> ("Silly", u);
391 I[3] = new FormAction("Form");
392 I[4] = new PadAction("Pad");
393 I[5] = new ScanAction("Scan");
394 I[6] = new QuitItem();
395 I[7] = new NCursesMenuItem(); // Terminating empty item
397 InitMenu(I,TRUE,TRUE);
399 P = new NCursesPanel(1,n_items,LINES-1,1);
400 boldframe("Demo","Silly");
411 virtual void On_Menu_Init()
413 NCursesWindow W(::stdscr);
416 for(int i=1; i<=count(); i++)
418 P->bkgd(W.getbkgd());
422 virtual void On_Menu_Termination()
429 virtual void On_Item_Init(NCursesMenuItem& item)
431 P->move(0,item.index());
432 P->attron(A_REVERSE);
433 P->printw("%1d",1+item.index());
434 P->attroff(A_REVERSE);
438 virtual void On_Item_Termination(NCursesMenuItem& item)
440 P->move(0,item.index());
441 P->attroff(A_REVERSE);
442 P->printw("%1d",1+item.index());
447 // -------------------------------------------------------------------------
449 class TestApplication : public NCursesApplication {
451 int titlesize() const { return 1; }
453 Soft_Label_Key_Set::Label_Layout useSLKs() const {
454 return Soft_Label_Key_Set::PC_Style_With_Index;
456 void init_labels(Soft_Label_Key_Set& S) const;
459 TestApplication() : NCursesApplication(TRUE) {
465 void TestApplication::init_labels(Soft_Label_Key_Set& S) const {
466 for(int i=1; i <= S.labels(); i++) {
468 ::sprintf(buf,"Key%02d",i);
470 S[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
474 void TestApplication::title() {
475 const char * const title = "Simple C++ Binding Demo";
476 const int len = ::strlen(title);
478 titleWindow->bkgd(screen_titles());
479 titleWindow->addstr(0,(titleWindow->cols()-len)/2,title);
480 titleWindow->noutrefresh();
484 int TestApplication::run() {
491 // -------------------------------------------------------------------------
493 static TestApplication Demo;