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@T-Online.de>
9 * $Id: demo.cc,v 1.13 1998/02/19 16:54:54 florian Exp $
12 #include "cursesapp.h"
20 extern "C" unsigned int sleep(unsigned int);
23 // -------------------------------------------------------------------------
28 void run(int sleeptime) {
30 NCursesPanel *std = new NCursesPanel();
32 // Make a few small demo panels
34 NCursesPanel *u = new NCursesPanel(8,20,12,4);
35 NCursesPanel *v = new NCursesPanel(8,20,10,6);
36 NCursesPanel *w = new NCursesPanel(8,20,8,8);
37 NCursesPanel *x = new NCursesPanel(8,20,6,10);
38 NCursesPanel *y = new NCursesPanel(8,20,4,12);
39 NCursesPanel *z = new NCursesPanel(8,30,2,14);
41 // Draw something on the main screen, so we can see what happens
42 // when panels get moved or deleted.
45 std->move(std->height()/2,1);
46 std->hline(std->width()-2);
47 std->move(1,std->width()/2);
48 std->vline(std->height()-2);
49 std->addch(0,std->width()/2,ACS_TTEE);
50 std->addch(std->height()-1,std->width()/2,ACS_BTEE);
51 std->addch(std->height()/2,0,ACS_LTEE);
52 std->addch(std->height()/2,std->width()-1,ACS_RTEE);
53 std->addch(std->height()/2,std->width()/2,ACS_PLUS);
55 // Draw frames with titles around panels so that we can see where
56 // the panels are located.
57 u->boldframe("Win U");
59 w->boldframe("Win W");
61 y->boldframe("Win Y");
63 if (NCursesApplication::getApplication()->useColors()) {
64 u->bkgd(' '|COLOR_PAIR(1));
65 w->bkgd(' '|COLOR_PAIR(1));
66 y->bkgd(' '|COLOR_PAIR(1));
67 v->bkgd(' '|COLOR_PAIR(2));
68 x->bkgd(' '|COLOR_PAIR(2));
69 z->bkgd(' '|COLOR_PAIR(2));
72 // A refresh to any valid panel updates all panels and refreshes
73 // the screen. Using std is just convenient - We know it's always
74 // valid until the end of the program.
79 // Show what happens when panels are deleted and moved.
93 // show how it looks when a panel moves
102 // show how it looks when you raise a panel
117 // Don't forget to clean up the main screen. Since this is the
118 // last thing using NCursesWindow, this has the effect of
119 // shutting down ncurses and restoring the terminal state.
131 UserData(int x) : u(x) {}
132 int sleeptime() const { return u; }
135 // -------------------------------------------------------------------------
137 template<class T> class MyAction : public NCursesUserItem<T>
140 MyAction (const char* p_name,
142 : NCursesUserItem<T>(p_name, (const char*)0, p_UserData)
149 a.run(UserData()->sleeptime());
154 class QuitItem : public NCursesMenuItem
157 QuitItem() : NCursesMenuItem("Quit") {
165 // -------------------------------------------------------------------------
167 class Label : public NCursesFormField
170 Label(const char*title,
172 : NCursesFormField(1,(int)::strlen(title),row,col) {
174 options_off(O_EDIT|O_ACTIVE);
178 // -------------------------------------------------------------------------
180 class MyFieldType : public UserDefinedFieldType {
184 bool field_check(NCursesFormField& f) {
187 bool char_check(int c) {
188 return (c==chk?TRUE:FALSE);
191 MyFieldType(int x) : chk(x) {
195 // -------------------------------------------------------------------------
197 class TestForm : public NCursesForm
200 NCursesFormField** F;
203 Enumeration_Field *eft;
205 static char *weekdays[];
208 TestForm() : NCursesForm(13,51,(lines()-15)/2,(cols()-53)/2) {
210 F = new NCursesFormField*[10];
211 mft = new MyFieldType('X');
212 ift = new Integer_Field(0,1,10);
213 eft = new Enumeration_Field(weekdays);
215 F[0] = new Label("Demo Entry Form",0,16);
216 F[1] = new Label("Weekday Enum",2,1);
217 F[2] = new Label("Number(1-10)",2,21);
218 F[3] = new Label("Only 'X'",2,35);
219 F[4] = new Label("Multiline Field (Dynamic and Scrollable)",5,1);
220 F[5] = new NCursesFormField(1,18,3,1);
221 F[6] = new NCursesFormField(1,12,3,21);
222 F[7] = new NCursesFormField(1,12,3,35);
223 F[8] = new NCursesFormField(4,46,6,1,2);
224 F[9] = new NCursesFormField();
226 InitForm(F,TRUE,TRUE);
229 F[5]->set_fieldtype(*eft);
230 F[6]->set_fieldtype(*ift);
232 F[7]->set_fieldtype(*mft);
233 F[7]->set_maximum_growth(20); // max. 20 characters
234 F[7]->options_off(O_STATIC); // make field dynamic
236 F[8]->set_maximum_growth(10); // max. 10 lines
237 F[8]->options_off(O_STATIC); // make field dynamic
247 char* TestForm::weekdays[] = {
248 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
249 "Friday", "Saturday", (char *)0 };
251 // -------------------------------------------------------------------------
253 class FormAction : public NCursesMenuItem
256 FormAction(const char *s) : NCursesMenuItem(s) {
261 Soft_Label_Key_Set* S = new Soft_Label_Key_Set;
262 for(int i=1; i <= S->labels(); i++) {
264 ::sprintf(buf,"Frm%02d",i);
265 (*S)[i] = buf; // Text
266 (*S)[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
268 NCursesApplication::getApplication()->push(*S);
270 NCursesApplication::getApplication()->pop();
275 // -------------------------------------------------------------------------
277 class PassiveItem : public NCursesMenuItem {
279 PassiveItem(const char* text) : NCursesMenuItem(text) {
280 options_off(O_SELECTABLE);
284 // -------------------------------------------------------------------------
286 class MyMenu : public NCursesMenu
295 : NCursesMenu (8, 8, (lines()-10)/2, (cols()-10)/2)
298 I = new NCursesMenuItem*[7];
299 I[0] = new PassiveItem("One");
300 I[1] = new PassiveItem("Two");
301 I[2] = new MyAction<UserData> ("Silly", u);
302 I[3] = new FormAction("Form");
303 I[4] = new PassiveItem("Five");
304 I[5] = new QuitItem();
305 I[6] = new NCursesMenuItem(); // Terminating empty item
307 InitMenu(I,TRUE,TRUE);
309 P = new NCursesPanel(1,6,LINES-1,1);
310 boldframe("Demo","Silly");
321 virtual void On_Menu_Init()
323 NCursesWindow W(::stdscr);
326 for(int i=1; i<=count(); i++)
328 P->bkgd(W.getbkgd());
332 virtual void On_Menu_Termination()
339 virtual void On_Item_Init(NCursesMenuItem& item)
341 P->move(0,item.index());
342 P->attron(A_REVERSE);
343 P->printw("%1d",1+item.index());
344 P->attroff(A_REVERSE);
348 virtual void On_Item_Termination(NCursesMenuItem& item)
350 P->move(0,item.index());
351 P->attroff(A_REVERSE);
352 P->printw("%1d",1+item.index());
357 // -------------------------------------------------------------------------
359 class TestApplication : public NCursesApplication {
361 int titlesize() const { return 1; }
363 Soft_Label_Key_Set::Label_Layout useSLKs() const {
364 return Soft_Label_Key_Set::PC_Style_With_Index;
366 void init_labels(Soft_Label_Key_Set& S) const;
369 TestApplication() : NCursesApplication(TRUE) {
375 void TestApplication::init_labels(Soft_Label_Key_Set& S) const {
376 for(int i=1; i <= S.labels(); i++) {
378 ::sprintf(buf,"Key%02d",i);
380 S[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
384 void TestApplication::title() {
385 const char * const title = "Simple C++ Binding Demo";
386 const int len = ::strlen(title);
388 titleWindow->bkgd(screen_titles());
389 titleWindow->addstr(0,(titleWindow->cols()-len)/2,title);
390 titleWindow->noutrefresh();
394 int TestApplication::run() {
401 // -------------------------------------------------------------------------
403 static TestApplication Demo;