]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/demo.cc
29b60fd74f1469b3e52b068f88435070872680d5
[ncurses.git] / c++ / demo.cc
1 /*
2  *   Silly demo program for the NCursesPanel class.
3  *
4  *   written by Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
5  *
6  * $Id: demo.cc,v 1.7 1997/05/05 20:53:41 tom Exp $
7  */
8
9 #include <stdlib.h>
10
11 #include "cursesm.h"
12
13 #if HAVE_LIBC_H
14 #  include <libc.h>
15 #endif
16
17 class SillyDemo 
18 {
19   public:
20   void run(int sleeptime) {
21     //  We need to define a full screen panel for the main screen so
22     //  that when redraws happen, the main screen actually gets redrawn.
23     //  I think there may be a bug in the panels code which won't redraw
24     //  the main screen otherwise.  Maybe someone out there can find it...
25
26     NCursesPanel *std = new NCursesPanel();
27
28     //  Make a few small demo panels
29
30     NCursesPanel *u = new NCursesPanel(10,20,12,4);
31     NCursesPanel *v = new NCursesPanel(10,20,10,6);
32     NCursesPanel *w = new NCursesPanel(10,20,8,8);
33     NCursesPanel *x = new NCursesPanel(10,20,6,10);
34     NCursesPanel *y = new NCursesPanel(10,20,4,12);
35     NCursesPanel *z = new NCursesPanel(10,30,2,14);
36
37     //  Draw something on the main screen, so we can see what happens
38     //  when panels get moved or deleted.
39
40     std->box();
41     std->move(10,0);
42     std->hline('a',79);
43     std->move(0,40);
44     std->vline(20);
45
46     //  Draw frames with titles around panels so that we can see where
47     //  the panels are located.
48
49     u->boldframe("Win U");
50     v->frame("Win V");
51     w->boldframe("Win W");
52     x->frame("Win X");
53     y->boldframe("Win Y");
54     z->frame("Win Z");
55
56     //  A refresh to any valid panel updates all panels and refreshes
57     //  the screen.  Using std is just convenient - We know it's always
58     //  valid until the end of the program.
59
60     std->refresh();
61
62     //  Show that things actually come back correctly when the screen
63     //  is cleared and the global NCursesPanel::redraw() is called.
64
65     sleep(sleeptime);
66     ::clear();                  // call ncurses clear() directly
67     ::wrefresh(stdscr);         // call ncurses refresh directly
68     sleep(sleeptime);
69     NCursesPanel::redraw();
70
71     //  Show what happens when panels are deleted and moved.
72
73     sleep(sleeptime);
74     delete u;
75     std->refresh();
76
77     sleep(sleeptime);
78     delete z;
79     std->refresh();
80
81     sleep(sleeptime);
82     delete v;
83     std->refresh();
84
85     // show how it looks when a panel moves
86     sleep(sleeptime);
87     y->mvpan(5,30);
88     std->refresh();
89
90     sleep(sleeptime);
91     delete y;
92     std->refresh();
93
94     // show how it looks when you raise a panel
95     sleep(sleeptime);
96     w->top();
97     std->refresh();
98
99     sleep(sleeptime);
100     delete w;
101     std->refresh();
102
103     sleep(sleeptime);
104     delete x;
105     std->refresh();
106
107     //  Don't forget to clean up the main screen.  Since this is the
108     //  last thing using NCursesWindow, this has the effect of
109     //  shutting down ncurses and restoring the terminal state.
110
111     sleep(sleeptime);
112     delete std;
113   }
114 };
115
116
117 class UserData
118 {
119 private:
120   int u;
121 public:
122   UserData(int x) : u(x) {} 
123   int sleeptime() const { return u; }
124   
125 };
126
127 template<class T> class MyAction : public NCursesUserItem<T>
128 {
129 public:
130   MyAction (const T* p_UserData,
131             const char* p_name)
132     : NCursesUserItem<T>(p_UserData, p_name)
133   {};
134
135   ~MyAction() {}
136
137   bool action() {
138     SillyDemo a;
139     a.run(UserData()->sleeptime());
140     return FALSE;
141   }
142 };
143
144 class QuitItem : public NCursesMenuItem
145 {
146 public:
147   QuitItem() : NCursesMenuItem("Quit") {
148   }
149
150   bool action() {
151     endwin();
152     return TRUE;
153   }
154 };
155
156 class MyMenu : public NCursesMenu
157 {
158 private:
159   NCursesPanel* P;
160
161 public:
162   MyMenu (NCursesMenuItem* menu[]) 
163     : NCursesMenu (menu, 7, 8, 2, 2, TRUE)
164   {
165     if (NCursesWindow::NumberOfColors() > 2) {
166       setcolor(1);
167       setpalette(COLOR_YELLOW, COLOR_BLUE);
168     }
169
170     P = new NCursesPanel(1,COLS,LINES-1,0);
171     boldframe("Demo","Silly");
172     P->show();
173   }
174
175   ~MyMenu()
176   {
177     P->hide();
178     delete P;
179   }
180
181   virtual void On_Menu_Init()
182   {
183     P->move(0,0);
184     P->clrtoeol();
185     P->addstr("12345");
186     NCursesPanel::refresh();
187   }
188
189   virtual void On_Menu_Termination()
190   {
191     P->move(0,0);
192     P->clrtoeol();
193     P->addstr("Menu Exit");
194     NCursesPanel::refresh();
195   }
196
197   virtual void On_Item_Init(NCursesMenuItem& item)
198   {
199     P->move(0,item.index());
200     P->attron(A_REVERSE);
201     P->printw("%1d",1+item.index());
202     P->attroff(A_REVERSE);
203     NCursesPanel::refresh();
204   }
205
206   virtual void On_Item_Termination(NCursesMenuItem& item)
207   {
208     P->move(0,item.index());
209     P->attroff(A_REVERSE);
210     P->printw("%1d",1+item.index());
211     NCursesPanel::refresh();
212   }
213 };
214
215 main()
216 {
217   UserData* u = new UserData(1);
218
219   NCursesWindow::useColors();
220
221   NCursesMenuItem** I = new NCursesMenuItem*[6];
222   I[0] = new NCursesMenuItem("One");
223   I[1] = new NCursesMenuItem("Two");
224   I[2] = new MyAction<UserData> (u, "Silly");
225   I[3] = new NCursesMenuItem("Four");
226   I[4] = new QuitItem();
227   I[5] = new NCursesMenuItem();
228   
229   MyMenu m(I);
230
231   m();
232
233   for(int i=0; i < 6; i++) {
234     delete I[i];
235   }
236   delete I;
237   delete u;
238
239   exit(0);
240 }