]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - c++/demo.cc
ncurses 5.5
[ncurses.git] / c++ / demo.cc
index d0728eff98992985db1ecc4e4d258d8766b7a780..ffc46373b361d38f85b5f51cd86c94cc49424dce 100644 (file)
@@ -4,25 +4,24 @@
  *   written by Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
  *
  *   Demo code for NCursesMenu and NCursesForm written by
- *   Juergen Pfeifer <Juergen.Pfeifer@T-Online.de>
+ *   Juergen Pfeifer
  *
- * $Id: demo.cc,v 1.13 1998/02/19 16:54:54 florian Exp $
+ * $Id: demo.cc,v 1.32 2005/08/13 18:14:44 tom Exp $
  */
 
+#include "internal.h"
 #include "cursesapp.h"
 #include "cursesm.h"
 #include "cursesf.h"
 
-#if HAVE_LIBC_H
-#  include <libc.h>
-#endif
-
 extern "C" unsigned int sleep(unsigned int);
 
+#undef index // needed for NeXT
+
 //
 // -------------------------------------------------------------------------
 //
-class SillyDemo 
+class SillyDemo
 {
   public:
   void run(int sleeptime) {
@@ -128,8 +127,8 @@ class UserData
 private:
   int u;
 public:
-  UserData(int x) : u(x) {} 
-  int sleeptime() const { return u; }  
+  UserData(int x) : u(x) {}
+  int sleeptime() const { return u; }
 };
 //
 // -------------------------------------------------------------------------
@@ -138,19 +137,22 @@ template<class T> class MyAction : public NCursesUserItem<T>
 {
 public:
   MyAction (const char* p_name,
-           const T* p_UserData)
-    : NCursesUserItem<T>(p_name, (const char*)0, p_UserData)
-  {};
+            const T* p_UserData)
+    : NCursesUserItem<T>(p_name, static_cast<const char*>(0), p_UserData)
+  {}
 
-  ~MyAction() {}
+  virtual ~MyAction() {}
 
   bool action() {
     SillyDemo a;
-    a.run(UserData()->sleeptime());
+    a.run(NCursesUserItem<T>::UserData()->sleeptime());
     return FALSE;
   }
 };
 
+template class MyAction<UserData>;
+template class NCURSES_IMPEXP NCursesUserItem<UserData>;
+
 class QuitItem : public NCursesMenuItem
 {
 public:
@@ -167,9 +169,9 @@ public:
 class Label : public NCursesFormField
 {
 public:
-  Label(const char*title,
-       int row, int col)
-    : NCursesFormField(1,(int)::strlen(title),row,col) {
+  Label(const char* title,
+        int row, int col)
+    : NCursesFormField(1, static_cast<int>(::strlen(title)), row, col) {
       set_value(title);
       options_off(O_EDIT|O_ACTIVE);
   }
@@ -177,7 +179,8 @@ public:
 //
 // -------------------------------------------------------------------------
 //
-class MyFieldType : public UserDefinedFieldType {
+class MyFieldType : public UserDefinedFieldType
+{
 private:
   int chk;
 protected:
@@ -202,11 +205,17 @@ private:
   Integer_Field *ift;
   Enumeration_Field *eft;
 
-  static char *weekdays[];
-  
+  static const char *weekdays[];
+
 public:
-  TestForm() : NCursesForm(13,51,(lines()-15)/2,(cols()-53)/2) {
-    
+  TestForm()
+    : NCursesForm(13, 51, (lines() - 15)/2, (cols() - 53)/2),
+      F(0),
+      mft(0),
+      ift(0),
+      eft(0)
+  {
+
     F     = new NCursesFormField*[10];
     mft   = new MyFieldType('X');
     ift   = new Integer_Field(0,1,10);
@@ -222,10 +231,10 @@ public:
     F[7]  = new NCursesFormField(1,12,3,35);
     F[8]  = new NCursesFormField(4,46,6,1,2);
     F[9]  = new NCursesFormField();
-  
+
     InitForm(F,TRUE,TRUE);
     boldframe();
-    
+
     F[5]->set_fieldtype(*eft);
     F[6]->set_fieldtype(*ift);
 
@@ -237,6 +246,19 @@ public:
     F[8]->options_off(O_STATIC);  // make field dynamic
   }
 
+  TestForm& operator=(const TestForm& rhs)
+  {
+    if (this != &rhs) {
+      *this = rhs;
+    }
+    return *this;
+  }
+
+  TestForm(const TestForm& rhs)
+    : NCursesForm(rhs), F(0), mft(0), ift(0), eft(0)
+  {
+  }
+
   ~TestForm() {
     delete mft;
     delete ift;
@@ -244,9 +266,9 @@ public:
   }
 };
 
-char* TestForm::weekdays[] = {
+const char* TestForm::weekdays[] = {
     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
-    "Friday", "Saturday", (char *)0 };
+    "Friday", "Saturday", NULL };
 //
 // -------------------------------------------------------------------------
 //
@@ -274,12 +296,98 @@ public:
 //
 // -------------------------------------------------------------------------
 //
-class PassiveItem : public NCursesMenuItem {
+class PadAction : public NCursesMenuItem
+{
+public:
+  PadAction(const char* s) : NCursesMenuItem(s) {
+  }
+
+  bool action() {
+    const int GRIDSIZE = 3;
+    const int PADSIZE  = 200;
+    unsigned gridcount = 0;
+
+    NCursesPanel std;
+    NCursesPanel P(std.lines()-2,std.cols()-2,1,1);
+    NCursesFramedPad FP(P,PADSIZE,PADSIZE);
+
+    for (int i=0; i < PADSIZE; i++) {
+      for (int j=0; j < PADSIZE; j++) {
+        if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
+          if (i==0 || j==0)
+            FP.addch('+');
+          else
+            FP.addch(static_cast<chtype>('A' + (gridcount++ % 26)));
+        }
+        else if (i % GRIDSIZE == 0)
+          FP.addch('-');
+        else if (j % GRIDSIZE == 0)
+          FP.addch('|');
+        else
+          FP.addch(' ');
+      }
+    }
+
+    P.label("Pad Demo",NULL);
+    FP();
+    P.clear();
+    return FALSE;
+  }
+};
+
+//
+// -------------------------------------------------------------------------
+//
+class PassiveItem : public NCursesMenuItem
+{
 public:
   PassiveItem(const char* text) : NCursesMenuItem(text) {
     options_off(O_SELECTABLE);
   }
 };
+
+//
+// -------------------------------------------------------------------------
+//
+class ScanAction : public NCursesMenuItem
+{
+public:
+  ScanAction(const char* s) : NCursesMenuItem(s) {
+  }
+
+  bool action() {
+    NCursesPanel *std = new NCursesPanel();
+
+    NCursesPanel *w = new NCursesPanel(std->lines() - 2, std->cols() - 2, 1, 1);
+    w->box();
+    w->refresh();
+
+    NCursesPanel *s = new NCursesPanel(w->lines() - 6, w->cols() - 6, 3, 3);
+    s->scrollok(TRUE);
+    ::echo();
+
+    s->printw("Enter decimal integers.  The running total will be shown\n");
+    int nvalue = -1;
+    int result = 0;
+    while (nvalue != 0) {
+      nvalue = 0;
+      s->scanw("%d", &nvalue);
+      if (nvalue != 0) {
+        s->printw("%d: ", result += nvalue);
+      }
+      s->refresh();
+    }
+    s->printw("\nPress any key to continue...");
+    s->getch();
+
+    delete s;
+    delete w;
+    delete std;
+    ::noecho();
+    return FALSE;
+  }
+};
+
 //
 // -------------------------------------------------------------------------
 //
@@ -289,28 +397,44 @@ private:
   NCursesPanel* P;
   NCursesMenuItem** I;
   UserData *u;
+  #define n_items 7
 
 public:
-  MyMenu () 
-    : NCursesMenu (8, 8, (lines()-10)/2, (cols()-10)/2)
+  MyMenu ()
+    : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2),
+      P(0), I(0), u(0)
   {
     u = new UserData(1);
-    I = new NCursesMenuItem*[7];
+    I = new NCursesMenuItem*[1+n_items];
     I[0] = new PassiveItem("One");
     I[1] = new PassiveItem("Two");
     I[2] = new MyAction<UserData> ("Silly", u);
     I[3] = new FormAction("Form");
-    I[4] = new PassiveItem("Five");
-    I[5] = new QuitItem();
-    I[6] = new NCursesMenuItem(); // Terminating empty item
+    I[4] = new PadAction("Pad");
+    I[5] = new ScanAction("Scan");
+    I[6] = new QuitItem();
+    I[7] = new NCursesMenuItem(); // Terminating empty item
 
     InitMenu(I,TRUE,TRUE);
-    
-    P = new NCursesPanel(1,6,LINES-1,1);
+
+    P = new NCursesPanel(1,n_items,LINES-1,1);
     boldframe("Demo","Silly");
     P->show();
   }
 
+  MyMenu& operator=(const MyMenu& rhs)
+  {
+    if (this != &rhs) {
+      *this = rhs;
+    }
+    return *this;
+  }
+
+  MyMenu(const MyMenu& rhs)
+    : NCursesMenu(rhs), P(0), I(0), u(0)
+  {
+  }
+
   ~MyMenu()
   {
     P->hide();
@@ -356,7 +480,8 @@ public:
 //
 // -------------------------------------------------------------------------
 //
-class TestApplication : public NCursesApplication {
+class TestApplication : public NCursesApplication
+{
 protected:
   int titlesize() const { return 1; }
   void title();
@@ -372,7 +497,8 @@ public:
   int run();
 };
 
-void TestApplication::init_labels(Soft_Label_Key_Set& S) const {
+void TestApplication::init_labels(Soft_Label_Key_Set& S) const
+{
   for(int i=1; i <= S.labels(); i++) {
     char buf[5];
     ::sprintf(buf,"Key%02d",i);
@@ -381,17 +507,19 @@ void TestApplication::init_labels(Soft_Label_Key_Set& S) const {
   }
 }
 
-void TestApplication::title() {
-  const char * const title = "Simple C++ Binding Demo";
-  const int len = ::strlen(title);
+void TestApplication::title()
+{
+  const char * const titleText = "Simple C++ Binding Demo";
+  const int len = ::strlen(titleText);
 
   titleWindow->bkgd(screen_titles());
-  titleWindow->addstr(0,(titleWindow->cols()-len)/2,title);
-  titleWindow->noutrefresh();  
+  titleWindow->addstr(0,(titleWindow->cols() - len)/2, titleText);
+  titleWindow->noutrefresh();
 }
 
 
-int TestApplication::run() {
+int TestApplication::run()
+{
   MyMenu M;
   M();
   return 0;