]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - c++/demo.cc
ncurses 6.1 - patch 20180818
[ncurses.git] / c++ / demo.cc
index f3fe3e46f7ef4fe75da1937a712a42de434e8060..2423111d3d81e69e2ffe124eb88e1c21987d6fd8 100644 (file)
@@ -1,3 +1,32 @@
+// * This makes emacs happy -*-Mode: C++;-*-
+/****************************************************************************
+ * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc.              *
+ *                                                                          *
+ * Permission is hereby granted, free of charge, to any person obtaining a  *
+ * copy of this software and associated documentation files (the            *
+ * "Software"), to deal in the Software without restriction, including      *
+ * without limitation the rights to use, copy, modify, merge, publish,      *
+ * distribute, distribute with modifications, sublicense, and/or sell       *
+ * copies of the Software, and to permit persons to whom the Software is    *
+ * furnished to do so, subject to the following conditions:                 *
+ *                                                                          *
+ * The above copyright notice and this permission notice shall be included  *
+ * in all copies or substantial portions of the Software.                   *
+ *                                                                          *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
+ * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
+ *                                                                          *
+ * Except as contained in this notice, the name(s) of the above copyright   *
+ * holders shall not be used in advertising or otherwise to promote the     *
+ * sale, use or other dealings in this Software without prior written       *
+ * authorization.                                                           *
+ ****************************************************************************/
+
 /*
  *   Silly demo program for the NCursesPanel class.
  *
  *   Demo code for NCursesMenu and NCursesForm written by
  *   Juergen Pfeifer
  *
- * $Id: demo.cc,v 1.24 2004/01/15 00:21:27 tom Exp $
+ * $Id: demo.cc,v 1.43 2018/06/24 00:07:22 tom Exp $
  */
 
+#include "internal.h"
 #include "cursesapp.h"
 #include "cursesm.h"
 #include "cursesf.h"
 
-#if HAVE_LIBC_H
-#  include <libc.h>
+#ifdef _WIN32
+#undef KEY_EVENT
 #endif
 
+#ifndef _WIN32
 extern "C" unsigned int sleep(unsigned int);
+#endif
 
 #undef index // needed for NeXT
 
@@ -29,30 +61,30 @@ class SillyDemo
   public:
   void run(int sleeptime) {
 
-    NCursesPanel *std = new NCursesPanel();
+    NCursesPanel *mystd = new NCursesPanel();
 
     //  Make a few small demo panels
 
-    NCursesPanel *u = new NCursesPanel(8,20,12,4);
-    NCursesPanel *v = new NCursesPanel(8,20,10,6);
-    NCursesPanel *w = new NCursesPanel(8,20,8,8);
-    NCursesPanel *x = new NCursesPanel(8,20,6,10);
-    NCursesPanel *y = new NCursesPanel(8,20,4,12);
-    NCursesPanel *z = new NCursesPanel(8,30,2,14);
+    NCursesPanel *u = new NCursesPanel(8, 20, 12, 4);
+    NCursesPanel *v = new NCursesPanel(8, 20, 10, 6);
+    NCursesPanel *w = new NCursesPanel(8, 20, 8, 8);
+    NCursesPanel *x = new NCursesPanel(8, 20, 6, 10);
+    NCursesPanel *y = new NCursesPanel(8, 20, 4, 12);
+    NCursesPanel *z = new NCursesPanel(8, 30, 2, 14);
 
     //  Draw something on the main screen, so we can see what happens
     //  when panels get moved or deleted.
 
-    std->box();
-    std->move(std->height()/2,1);
-    std->hline(std->width()-2);
-    std->move(1,std->width()/2);
-    std->vline(std->height()-2);
-    std->addch(0,std->width()/2,ACS_TTEE);
-    std->addch(std->height()-1,std->width()/2,ACS_BTEE);
-    std->addch(std->height()/2,0,ACS_LTEE);
-    std->addch(std->height()/2,std->width()-1,ACS_RTEE);
-    std->addch(std->height()/2,std->width()/2,ACS_PLUS);
+    mystd->box();
+    mystd->move(mystd->height()/2, 1);
+    mystd->hline(mystd->width()-2);
+    mystd->move(1, mystd->width()/2);
+    mystd->vline(mystd->height()-2);
+    mystd->addch(0, mystd->width()/2, ACS_TTEE);
+    mystd->addch(mystd->height()-1, mystd->width()/2, ACS_BTEE);
+    mystd->addch(mystd->height()/2, 0, ACS_LTEE);
+    mystd->addch(mystd->height()/2, mystd->width()-1, ACS_RTEE);
+    mystd->addch(mystd->height()/2, mystd->width()/2, ACS_PLUS);
 
     //  Draw frames with titles around panels so that we can see where
     //  the panels are located.
@@ -72,56 +104,56 @@ class SillyDemo
     }
 
     //  A refresh to any valid panel updates all panels and refreshes
-    //  the screen.  Using std is just convenient - We know it's always
+    //  the screen.  Using mystd is just convenient - We know it's always
     //  valid until the end of the program.
 
-    std->refresh();
+    mystd->refresh();
     sleep(sleeptime);
 
     //  Show what happens when panels are deleted and moved.
 
     sleep(sleeptime);
     delete u;
-    std->refresh();
+    mystd->refresh();
 
     sleep(sleeptime);
     delete z;
-    std->refresh();
+    mystd->refresh();
 
     sleep(sleeptime);
     delete v;
-    std->refresh();
+    mystd->refresh();
 
     // show how it looks when a panel moves
     sleep(sleeptime);
-    y->mvwin(5,30);
-    std->refresh();
+    y->mvwin(5, 30);
+    mystd->refresh();
 
     sleep(sleeptime);
     delete y;
-    std->refresh();
+    mystd->refresh();
 
     // show how it looks when you raise a panel
     sleep(sleeptime);
     w->top();
-    std->refresh();
+    mystd->refresh();
 
     sleep(sleeptime);
     delete w;
-    std->refresh();
+    mystd->refresh();
 
     sleep(sleeptime);
     delete x;
 
-    std->clear();
-    std->refresh();
+    mystd->clear();
+    mystd->refresh();
 
     //  Don't forget to clean up the main screen.  Since this is the
     //  last thing using NCursesWindow, this has the effect of
     //  shutting down ncurses and restoring the terminal state.
 
     sleep(sleeptime);
-    delete std;
+    delete mystd;
   }
 };
 
@@ -141,10 +173,10 @@ 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)
-  {};
+    : NCursesUserItem<T>(p_name, static_cast<const char*>(0), p_UserData)
+  {}
 
-  ~MyAction() {}
+  virtual ~MyAction() {}
 
   bool action() {
     SillyDemo a;
@@ -153,6 +185,9 @@ public:
   }
 };
 
+template class MyAction<UserData>;
+template class NCURSES_IMPEXP NCursesUserItem<UserData>;
+
 class QuitItem : public NCursesMenuItem
 {
 public:
@@ -171,7 +206,7 @@ class Label : public NCursesFormField
 public:
   Label(const char* title,
         int row, int col)
-    : NCursesFormField(1,(int)::strlen(title),row,col) {
+    : NCursesFormField(1, static_cast<int>(::strlen(title)), row, col) {
       set_value(title);
       options_off(O_EDIT|O_ACTIVE);
   }
@@ -179,11 +214,13 @@ public:
 //
 // -------------------------------------------------------------------------
 //
-class MyFieldType : public UserDefinedFieldType {
+class MyFieldType : public UserDefinedFieldType
+{
 private:
   int chk;
 protected:
   bool field_check(NCursesFormField& f) {
+    (void) f;
     return TRUE;
   }
   bool char_check(int c) {
@@ -207,25 +244,31 @@ private:
   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);
+    ift   = new Integer_Field(0, 1, 10);
     eft   = new Enumeration_Field(weekdays);
 
-    F[0]  = new Label("Demo Entry Form",0,16);
-    F[1]  = new Label("Weekday Enum",2,1);
-    F[2]  = new Label("Number(1-10)",2,21);
-    F[3]  = new Label("Only 'X'",2,35);
-    F[4]  = new Label("Multiline Field (Dynamic and Scrollable)",5,1);
-    F[5]  = new NCursesFormField(1,18,3,1);
-    F[6]  = new NCursesFormField(1,12,3,21);
-    F[7]  = new NCursesFormField(1,12,3,35);
-    F[8]  = new NCursesFormField(4,46,6,1,2);
+    F[0]  = new Label("Demo Entry Form", 0, 16);
+    F[1]  = new Label("Weekday Enum", 2, 1);
+    F[2]  = new Label("Number(1-10)", 2, 21);
+    F[3]  = new Label("Only 'X'", 2, 35);
+    F[4]  = new Label("Multiline Field (Dynamic and Scrollable)", 5, 1);
+    F[5]  = new NCursesFormField(1, 18, 3, 1);
+    F[6]  = new NCursesFormField(1, 12, 3, 21);
+    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);
+    InitForm(F, TRUE, TRUE);
     boldframe();
 
     F[5]->set_fieldtype(*eft);
@@ -239,6 +282,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;
@@ -248,7 +304,7 @@ public:
 
 const char* TestForm::weekdays[] = {
     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
-    "Friday", "Saturday", (const char *)0 };
+    "Friday", "Saturday", NULL };
 //
 // -------------------------------------------------------------------------
 //
@@ -262,14 +318,16 @@ public:
     TestForm F;
     Soft_Label_Key_Set* S = new Soft_Label_Key_Set;
     for(int i=1; i <= S->labels(); i++) {
-      char buf[5];
-      ::sprintf(buf,"Frm%02d",i);
+      char buf[8];
+      assert(i < 100);
+      ::_nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "Frm%02d", i % 100);
       (*S)[i] = buf;                                      // Text
       (*S)[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
     }
     NCursesApplication::getApplication()->push(*S);
     F();
     NCursesApplication::getApplication()->pop();
+    delete S;
     return FALSE;
   }
 };
@@ -287,9 +345,9 @@ public:
     const int PADSIZE  = 200;
     unsigned gridcount = 0;
 
-    NCursesPanel std;
-    NCursesPanel P(std.lines()-2,std.cols()-2,1,1);
-    NCursesFramedPad FP(P,PADSIZE,PADSIZE);
+    NCursesPanel mystd;
+    NCursesPanel P(mystd.lines()-2, mystd.cols()-2, 1, 1);
+    NCursesFramedPad FP(P, PADSIZE, PADSIZE);
 
     for (int i=0; i < PADSIZE; i++) {
       for (int j=0; j < PADSIZE; j++) {
@@ -297,7 +355,7 @@ public:
           if (i==0 || j==0)
             FP.addch('+');
           else
-            FP.addch((chtype)('A' + (gridcount++ % 26)));
+            FP.addch(static_cast<chtype>('A' + (gridcount++ % 26)));
         }
         else if (i % GRIDSIZE == 0)
           FP.addch('-');
@@ -308,7 +366,7 @@ public:
       }
     }
 
-    P.label("Pad Demo",NULL);
+    P.label("Pad Demo", NULL);
     FP();
     P.clear();
     return FALSE;
@@ -318,7 +376,8 @@ public:
 //
 // -------------------------------------------------------------------------
 //
-class PassiveItem : public NCursesMenuItem {
+class PassiveItem : public NCursesMenuItem
+{
 public:
   PassiveItem(const char* text) : NCursesMenuItem(text) {
     options_off(O_SELECTABLE);
@@ -335,9 +394,9 @@ public:
   }
 
   bool action() {
-    NCursesPanel *std = new NCursesPanel();
+    NCursesPanel *mystd = new NCursesPanel();
 
-    NCursesPanel *w = new NCursesPanel(std->lines() - 2, std->cols() - 2, 1, 1);
+    NCursesPanel *w = new NCursesPanel(mystd->lines() - 2, mystd->cols() - 2, 1, 1);
     w->box();
     w->refresh();
 
@@ -346,13 +405,13 @@ public:
     ::echo();
 
     s->printw("Enter decimal integers.  The running total will be shown\n");
-    int value = -1;
+    int nvalue = -1;
     int result = 0;
-    while (value != 0) {
-      value = 0;
-      s->scanw("%d", &value);
-      if (value != 0) {
-        s->printw("%d: ", result += value);
+    while (nvalue != 0) {
+      nvalue = 0;
+      s->scanw("%d", &nvalue);
+      if (nvalue != 0) {
+        s->printw("%d: ", result += nvalue);
       }
       s->refresh();
     }
@@ -361,7 +420,7 @@ public:
 
     delete s;
     delete w;
-    delete std;
+    delete mystd;
     ::noecho();
     return FALSE;
   }
@@ -380,7 +439,8 @@ private:
 
 public:
   MyMenu ()
-    : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2)
+    : NCursesMenu (n_items+2, 8, (lines()-10)/2, (cols()-10)/2),
+      P(0), I(0), u(0)
   {
     u = new UserData(1);
     I = new NCursesMenuItem*[1+n_items];
@@ -393,13 +453,26 @@ public:
     I[6] = new QuitItem();
     I[7] = new NCursesMenuItem(); // Terminating empty item
 
-    InitMenu(I,TRUE,TRUE);
+    InitMenu(I, TRUE, TRUE);
 
-    P = new NCursesPanel(1,n_items,LINES-1,1);
-    boldframe("Demo","Silly");
+    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();
@@ -410,7 +483,7 @@ public:
   virtual void On_Menu_Init()
   {
     NCursesWindow W(::stdscr);
-    P->move(0,0);
+    P->move(0, 0);
     P->clrtoeol();
     for(int i=1; i<=count(); i++)
       P->addch('0' + i);
@@ -420,32 +493,33 @@ public:
 
   virtual void On_Menu_Termination()
   {
-    P->move(0,0);
+    P->move(0, 0);
     P->clrtoeol();
     refresh();
   }
 
   virtual void On_Item_Init(NCursesMenuItem& item)
   {
-    P->move(0,item.index());
+    P->move(0, item.index());
     P->attron(A_REVERSE);
-    P->printw("%1d",1+item.index());
+    P->printw("%1d", 1+item.index());
     P->attroff(A_REVERSE);
     refresh();
   }
 
   virtual void On_Item_Termination(NCursesMenuItem& item)
   {
-    P->move(0,item.index());
+    P->move(0, item.index());
     P->attroff(A_REVERSE);
-    P->printw("%1d",1+item.index());
+    P->printw("%1d", 1+item.index());
     refresh();
   }
 };
 //
 // -------------------------------------------------------------------------
 //
-class TestApplication : public NCursesApplication {
+class TestApplication : public NCursesApplication
+{
 protected:
   int titlesize() const { return 1; }
   void title();
@@ -461,26 +535,30 @@ 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);
+    char buf[8];
+    assert(i < 100);
+    ::_nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "Key%02d", i % 100);
     S[i] = buf;                                      // Text
     S[i] = Soft_Label_Key_Set::Soft_Label_Key::Left; // Justification
   }
 }
 
-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->addstr(0, (titleWindow->cols() - len)/2, titleText);
   titleWindow->noutrefresh();
 }
 
 
-int TestApplication::run() {
+int TestApplication::run()
+{
   MyMenu M;
   M();
   return 0;
@@ -489,4 +567,4 @@ int TestApplication::run() {
 //
 // -------------------------------------------------------------------------
 //
-static TestApplication Demo;
+static TestApplication *Demo = new TestApplication();