]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - c++/cursesw.cc
ncurses 5.5
[ncurses.git] / c++ / cursesw.cc
index baae046ef20460d9b40947f503574f0ca796d5e1..106202a5379d6c24d855c9bd6abf5f51c71c04d2 100644 (file)
   modified by Ulrich Drepper  (drepper@karlsruhe.gmd.de)
           and Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
 
-  modified by Juergen Pfeifer (juergen.pfeifer@gmx.net)
+  modified by Juergen Pfeifer
+  and Thomas Dickey (noting that more than 70% of this file has been changed)
 */
 
-#include "cursesw.h"
 #include "internal.h"
+#include "cursesw.h"
 
-MODULE_ID("$Id: cursesw.cc,v 1.16 1999/11/13 23:42:17 tom Exp $")
+MODULE_ID("$Id: cursesw.cc,v 1.32 2005/08/13 18:12:17 tom Exp $")
 
 #define COLORS_NEED_INITIALIZATION  -1
 #define COLORS_NOT_INITIALIZED       0
@@ -36,6 +37,14 @@ MODULE_ID("$Id: cursesw.cc,v 1.16 1999/11/13 23:42:17 tom Exp $")
 long NCursesWindow::count = 0L;
 bool NCursesWindow::b_initialized = FALSE;
 
+/*
+ * The ncurses library has a fallback for vsscanf(), which may work...
+ */
+#if !(USE_STRSTREAM_VSCAN || USE_STRSTREAM_VSCAN_CAST)
+#  undef  USE_STDIO_VSCAN
+#  define USE_STDIO_VSCAN 1
+#endif
+
 #if defined(__GNUG__)
 #  ifndef _IO_va_list
 #    define _IO_va_list char *
@@ -45,43 +54,56 @@ bool NCursesWindow::b_initialized = FALSE;
 int
 NCursesWindow::scanw(const char* fmt, ...)
 {
-#if defined(__GNUG__)
-    va_list args;
-    va_start(args, fmt);
+    int result = ERR;
     char buf[BUFSIZ];
-    int result = wgetstr(w, buf);
-    if (result == OK) {
+
+    if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
+       va_list args;
+       va_start(args, fmt);
+#if USE_STDIO_VSCAN
+       if (::vsscanf(buf, fmt, args) != -1)
+           result = OK;
+#elif USE_STRSTREAM_VSCAN      /* powerpc, os390 */
+       strstreambuf ss(buf, sizeof(buf));
+       if (ss.vscan(fmt, args) != -1)
+           result = OK;
+#elif USE_STRSTREAM_VSCAN_CAST /* pre-gcc 3.0 */
        strstreambuf ss(buf, sizeof(buf));
-       result = ss.vscan(fmt, (_IO_va_list)args);
+       if (ss.vscan(fmt, (_IO_va_list)args) != -1)
+           result = OK;
+#endif
+       va_end(args);
     }
-    va_end(args);
     return result;
-#else
-    return ERR;
-#endif
 }
 
 
 int
 NCursesWindow::scanw(int y, int x, const char* fmt, ...)
 {
-#if defined(__GNUG__)
-    va_list args;
-    va_start(args, fmt);
+    int result = ERR;
     char buf[BUFSIZ];
-    int result = wmove(w, y, x);
-    if (result == OK) {
-       result = wgetstr(w, buf);
-       if (result == OK) {
+
+    if (::wmove(w, y, x) != ERR) {
+       if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
+           va_list args;
+           va_start(args, fmt);
+#if USE_STDIO_VSCAN
+           if (::vsscanf(buf, fmt, args) != -1)
+               result = OK;
+#elif USE_STRSTREAM_VSCAN      /* powerpc, os390 */
+           strstreambuf ss(buf, sizeof(buf));
+           if (ss.vscan(fmt, args) != -1)
+               result = OK;
+#elif USE_STRSTREAM_VSCAN_CAST /* pre-gcc 3.0 */
            strstreambuf ss(buf, sizeof(buf));
-           result = ss.vscan(fmt, (_IO_va_list)args);
+           if (ss.vscan(fmt, (_IO_va_list)args) != -1)
+               result = OK;
+#endif
+           va_end(args);
        }
     }
-    va_end(args);
     return result;
-#else
-    return ERR;
-#endif
 }
 
 
@@ -91,7 +113,7 @@ NCursesWindow::printw(const char * fmt, ...)
     va_list args;
     va_start(args, fmt);
     char buf[BUFSIZ];
-    vsprintf(buf, fmt, args);
+    ::vsprintf(buf, fmt, args);
     va_end(args);
     return waddstr(w, buf);
 }
@@ -102,10 +124,10 @@ NCursesWindow::printw(int y, int x, const char * fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
-    int result = wmove(w, y, x);
+    int result = ::wmove(w, y, x);
     if (result == OK) {
        char buf[BUFSIZ];
-       vsprintf(buf, fmt, args);
+       ::vsprintf(buf, fmt, args);
        result = waddstr(w, buf);
     }
     va_end(args);
@@ -128,7 +150,8 @@ NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
 }
 
 void
-NCursesWindow::initialize() {
+NCursesWindow::initialize()
+{
   if (!b_initialized) {
     ::initscr();
     b_initialized = TRUE;
@@ -141,23 +164,24 @@ NCursesWindow::initialize() {
   }
 }
 
-NCursesWindow::NCursesWindow() {
-  if (!b_initialized)
-    initialize();
+NCursesWindow::NCursesWindow()
+  : w(0), alloced(0), par(0), subwins(0), sib(0)
+{
+  initialize();
 
-  w = (WINDOW *)0;
+  w = static_cast<WINDOW *>(0);
   init();
   alloced = FALSE;
   subwins = par = sib = 0;
   count++;
 }
 
-NCursesWindow::NCursesWindow(int lines, int cols, int begin_y, int begin_x)
+NCursesWindow::NCursesWindow(int nlines, int ncols, int begin_y, int begin_x)
+  : w(0), alloced(0), par(0), subwins(0), sib(0)
 {
-    if (!b_initialized)
-      initialize();
+    initialize();
 
-    w = ::newwin(lines, cols, begin_y, begin_x);
+    w = ::newwin(nlines, ncols, begin_y, begin_x);
     if (w == 0) {
        err_handler("Cannot construct window");
     }
@@ -169,9 +193,9 @@ NCursesWindow::NCursesWindow(int lines, int cols, int begin_y, int begin_x)
 }
 
 NCursesWindow::NCursesWindow(WINDOW* &window)
+  : w(0), alloced(0), par(0), subwins(0), sib(0)
 {
-    if (!b_initialized)
-      initialize();
+    initialize();
 
     w = window;
     init();
@@ -182,7 +206,9 @@ NCursesWindow::NCursesWindow(WINDOW* &window)
 
 NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
                             int begin_y, int begin_x, char absrel)
+  : w(0), alloced(0), par(0), subwins(0), sib(0)
 {
+    initialize();
     if (absrel == 'a') { // absolute origin
        begin_y -= win.begy();
        begin_x -= win.begx();
@@ -207,8 +233,12 @@ NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
 
 NCursesWindow::NCursesWindow(NCursesWindow& win,
                                bool do_box NCURSES_PARAM_INIT(TRUE))
+  : w(0), alloced(0), par(0), subwins(0), sib(0)
 {
-  w = :: derwin(win.w,win.height()-2,win.width()-2,1,1);
+  initialize();
+  int myHeight = win.height();
+  int myWidth  = win.width();
+  w = :: derwin(win.w, myHeight - 2, myWidth - 2, 1, 1);
   if (w == 0) {
     err_handler("Cannot construct subwindow");
   }
@@ -226,7 +256,8 @@ NCursesWindow::NCursesWindow(NCursesWindow& win,
   }
 }
 
-NCursesWindow NCursesWindow::Clone() {
+NCursesWindow NCursesWindow::Clone()
+{
   WINDOW *d = ::dupwin(w);
   NCursesWindow W(d);
   W.subwins = subwins;
@@ -241,30 +272,32 @@ static RIPOFFINIT R_INIT[5];       // There can't be more
 static int r_init_idx   = 0;
 static RIPOFFINIT* prip = R_INIT;
 
-extern "C" int _nc_ripoffline(int,int (*init)(WINDOW*,int));
-
-NCursesWindow::NCursesWindow(WINDOW *win, int cols) {
+NCursesWindow::NCursesWindow(WINDOW *win, int ncols)
+  : w(0), alloced(0), par(0), subwins(0), sib(0)
+{
+  initialize();
   w = win;
-  assert((w->_maxx+1)==cols);
+  assert((w->_maxx+1)==ncols);
   alloced = FALSE;
   subwins = par = sib = 0;
 }
 
-int NCursesWindow::ripoff_init(WINDOW *w, int cols)
+int _nc_xx_ripoff_init(WINDOW *w, int ncols)
 {
   int res = ERR;
 
   RIPOFFINIT init = *prip++;
   if (init) {
-    NCursesWindow* W = new NCursesWindow(w,cols);
+    NCursesWindow* W = new NCursesWindow(w,ncols);
     res = init(*W);
   }
   return res;
 }
 
 int NCursesWindow::ripoffline(int ripoff_lines,
-                             int (*init)(NCursesWindow& win)) {
-  int code = ::_nc_ripoffline(ripoff_lines,ripoff_init);
+                             int (*init)(NCursesWindow& win))
+{
+  int code = ::_nc_ripoffline(ripoff_lines,_nc_xx_ripoff_init);
   if (code==OK && init && ripoff_lines) {
     R_INIT[r_init_idx++] = init;
   }
@@ -272,7 +305,8 @@ int NCursesWindow::ripoffline(int ripoff_lines,
 }
 
 bool
-NCursesWindow::isDescendant(NCursesWindow& win) {
+NCursesWindow::isDescendant(NCursesWindow& win)
+{
   for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
     if (p==&win)
       return TRUE;
@@ -323,7 +357,7 @@ NCursesWindow::~NCursesWindow()
     }
 
     if (alloced && w != 0)
-       delwin(w);
+       ::delwin(w);
 
     if (alloced) {
       --count;
@@ -364,7 +398,7 @@ NCursesWindow::getcolor(int getback) const
     short fore, back;
 
     if (colorInitialized==COLORS_ARE_REALLY_THERE) {
-      if (pair_content((short)PAIR_NUMBER(w->_attrs), &fore, &back))
+      if (::pair_content(static_cast<short>(PAIR_NUMBER(w->_attrs)), &fore, &back))
        err_handler("Can't get color pair");
     }
     else {
@@ -396,7 +430,7 @@ int
 NCursesWindow::setpalette(short fore, short back, short pair)
 {
   if (colorInitialized==COLORS_ARE_REALLY_THERE)
-    return init_pair(pair, fore, back);
+    return ::init_pair(pair, fore, back);
   else
     return OK;
 }
@@ -405,7 +439,7 @@ int
 NCursesWindow::setpalette(short fore, short back)
 {
   if (colorInitialized==COLORS_ARE_REALLY_THERE)
-    return setpalette(fore, back, (short)PAIR_NUMBER(w->_attrs));
+    return setpalette(fore, back, static_cast<short>(PAIR_NUMBER(w->_attrs)));
   else
     return OK;
 }
@@ -424,10 +458,9 @@ NCursesWindow::setcolor(short pair)
   return OK;
 }
 
-#ifdef HAVE_HAS_KEY
-extern "C" int _nc_has_mouse(void);
-
-bool NCursesWindow::has_mouse() const {
+#if HAVE_HAS_KEY
+bool NCursesWindow::has_mouse() const
+{
   return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())
          ? TRUE : FALSE);
 }