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
7 * Juergen Pfeifer <juergen.pfeifer@gmx.net>
9 * $Id: demo.cc,v 1.18 1999/09/11 18:57:54 tom Exp $
12 #include "cursesapp.h"
20 extern "C" unsigned int sleep(unsigned int);
22 #undef index // needed for NeXT
25 // -------------------------------------------------------------------------
30 void run(int sleeptime) {
32 NCursesPanel *std = new NCursesPanel();
34 // Make a few small demo panels
36 NCursesPanel *u = new NCursesPanel(8,20,12,4);
37 NCursesPanel *v = new NCursesPanel(8,20,10,6);
38 NCursesPanel *w = new NCursesPanel(8,20,8,8);
39 NCursesPanel *x = new NCursesPanel(8,20,6,10);
40 NCursesPanel *y = new NCursesPanel(8,20,4,12);
41 NCursesPanel *z = new NCursesPanel(8,30,2,14);
43 // Draw something on the main screen, so we can see what happens
44 // when panels get moved or deleted.
47 std->move(std->height()/2,1);
48 std->hline(std->width()-2);
49 std->move(1,std->width()/2);
50 std->vline(std->height()-2);
51 std->addch(0,std->width()/2,ACS_TTEE);
52 std->addch(std->height()-1,std->width()/2,ACS_BTEE);
53 std->addch(std->height()/2,0,ACS_LTEE);
54 std->addch(std->height()/2,std->width()-1,ACS_RTEE);
55 std->addch(std->height()/2,std->width()/2,ACS_PLUS);
57 // Draw frames with titles around panels so that we can see where
58 // the panels are located.
59 u->boldframe("Win U");
61 w->boldframe("Win W");
63 y->boldframe("Win Y");
65 if (NCursesApplication::getApplication()->useColors()) {
66 u->bkgd(' '|COLOR_PAIR(1));
67 w->bkgd(' '|COLOR_PAIR(1));
68 y->bkgd(' '|COLOR_PAIR(1));
69 v->bkgd(' '|COLOR_PAIR(2));
70 x->bkgd(' '|COLOR_PAIR(2));
71 z->bkgd(' '|COLOR_PAIR(2));
74 // A refresh to any valid panel updates all panels and refreshes
75 // the screen. Using std is just convenient - We know it's always
76 // valid until the end of the program.
81 // Show what happens when panels are deleted and moved.
95 // show how it looks when a panel moves
104 // show how it looks when you raise a panel
119 // Don't forget to clean up the main screen. Since this is the
120 // last thing using NCursesWindow, this has the effect of
121 // shutting down ncurses and restoring the terminal state.
133 UserData(int x) : u(x) {}
134 int sleeptime() const { return u; }
137 // -------------------------------------------------------------------------
139 template<class T> class MyAction : public NCursesUserItem<T>
142 MyAction (const char* p_name,
144 : NCursesUserItem<T>(p_name, (const char*)0, p_UserData)
151 a.run(NCursesUserItem<T>::UserData()->sleeptime());
156 class QuitItem : public NCursesMenuItem
159 QuitItem() : NCursesMenuItem("Quit") {
167 // -------------------------------------------------------------------------
169 class Label : public NCursesFormField
172 Label(const char* title,
174 : NCursesFormField(1,(int)::strlen(title),row,col) {
176 options_off(O_EDIT|O_ACTIVE);
180 // -------------------------------------------------------------------------
182 class MyFieldType : public UserDefinedFieldType {
186 bool field_check(NCursesFormField& f) {
189 bool char_check(int c) {
190 return (c==chk?TRUE:FALSE);
193 MyFieldType(int x) : chk(x) {
197 // -------------------------------------------------------------------------
199 class TestForm : public NCursesForm
202 NCursesFormField** F;
205 Enumeration_Field *eft;
207 static char *weekdays[];
210 TestForm() : NCursesForm(13,51,(lines()-15)/2,(cols()-53)/2) {
212 F = new NCursesFormField*[10];
213 mft = new MyFieldType('X');
214 ift = new Integer_Field(0,1,10);
215 eft = new Enumeration_Field(weekdays);
217 F[0] = new Label("Demo Entry Form",0,16);
218 F[1] = new Label("Weekday Enum",2,1);
219 F[2] = new Label("Number(1-10)",2,21);
220 F[3] = new Label("Only 'X'",2,35);
221 F[4] = new Label("Multiline Field (Dynamic and Scrollable)",5,1);
222 F[5] = new NCursesFormField(1,18,3,1);
223 F[6] = new NCursesFormField(1,12,3,21);
224 F[7] = new NCursesFormField(1,12,3,35);
225 F[8] = new NCursesFormField(4,46,6,1,2);
226 F[9] = new NCursesFormField();
228 InitForm(F,TRUE,TRUE);
231 F[5]->set_fieldtype(*eft);
232 F[6]->set_fieldtype(*ift);
234 F[7]->set_fieldtype(*mft);
235 F[7]->set_maximum_growth(20); // max. 20 characters
236 F[7]->options_off(O_STATIC); // make field dynamic
238 F[8]->set_maximum_growth(10); // max. 10 lines
239 F[8]->options_off(O_STATIC); // make field dynamic
249 char* TestForm::weekdays[] = {
250 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
251 "Friday", "Saturday", (char *)0 };
253 // -------------------------------------------------------------------------
255 class FormAction : public NCursesMenuItem
258 FormAction(const char *s) : NCursesMenuItem(s) {
263 Soft_Label_Key_Set* S = new Soft_Label_Key_Set;
264 for(int i=1; i <= S->labels(); i++) {
266 ::sprintf(buf,"Frm%02d",i);
267 (*S)[i] = buf; // Text
268 (*S)[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
270 NCursesApplication::getApplication()->push(*S);
272 NCursesApplication::getApplication()->pop();
277 // -------------------------------------------------------------------------
279 class PadAction : public NCursesMenuItem
282 PadAction(const char* s) : NCursesMenuItem(s) {
286 const int GRIDSIZE = 3;
287 const int PADSIZE = 200;
288 unsigned gridcount = 0;
291 NCursesPanel P(std.lines()-2,std.cols()-2,1,1);
292 NCursesFramedPad FP(P,PADSIZE,PADSIZE);
294 for (int i=0; i < PADSIZE; i++) {
295 for (int j=0; j < PADSIZE; j++) {
296 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
300 FP.addch((chtype)('A' + (gridcount++ % 26)));
302 else if (i % GRIDSIZE == 0)
304 else if (j % GRIDSIZE == 0)
311 P.label("Pad Demo",NULL);
319 // -------------------------------------------------------------------------
321 class PassiveItem : public NCursesMenuItem {
323 PassiveItem(const char* text) : NCursesMenuItem(text) {
324 options_off(O_SELECTABLE);
328 // -------------------------------------------------------------------------
330 class MyMenu : public NCursesMenu
340 : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2)
343 I = new NCursesMenuItem*[1+n_items];
344 I[0] = new PassiveItem("One");
345 I[1] = new PassiveItem("Two");
346 I[2] = new MyAction<UserData> ("Silly", u);
347 I[3] = new FormAction("Form");
348 I[4] = new PadAction("Pad");
349 I[5] = new PassiveItem("Six");
350 I[6] = new QuitItem();
351 I[7] = new NCursesMenuItem(); // Terminating empty item
353 InitMenu(I,TRUE,TRUE);
355 P = new NCursesPanel(1,n_items,LINES-1,1);
356 boldframe("Demo","Silly");
367 virtual void On_Menu_Init()
369 NCursesWindow W(::stdscr);
372 for(int i=1; i<=count(); i++)
374 P->bkgd(W.getbkgd());
378 virtual void On_Menu_Termination()
385 virtual void On_Item_Init(NCursesMenuItem& item)
387 P->move(0,item.index());
388 P->attron(A_REVERSE);
389 P->printw("%1d",1+item.index());
390 P->attroff(A_REVERSE);
394 virtual void On_Item_Termination(NCursesMenuItem& item)
396 P->move(0,item.index());
397 P->attroff(A_REVERSE);
398 P->printw("%1d",1+item.index());
403 // -------------------------------------------------------------------------
405 class TestApplication : public NCursesApplication {
407 int titlesize() const { return 1; }
409 Soft_Label_Key_Set::Label_Layout useSLKs() const {
410 return Soft_Label_Key_Set::PC_Style_With_Index;
412 void init_labels(Soft_Label_Key_Set& S) const;
415 TestApplication() : NCursesApplication(TRUE) {
421 void TestApplication::init_labels(Soft_Label_Key_Set& S) const {
422 for(int i=1; i <= S.labels(); i++) {
424 ::sprintf(buf,"Key%02d",i);
426 S[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
430 void TestApplication::title() {
431 const char * const title = "Simple C++ Binding Demo";
432 const int len = ::strlen(title);
434 titleWindow->bkgd(screen_titles());
435 titleWindow->addstr(0,(titleWindow->cols()-len)/2,title);
436 titleWindow->noutrefresh();
440 int TestApplication::run() {
447 // -------------------------------------------------------------------------
449 static TestApplication Demo;