home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / curseswi.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  4.8 KB  |  235 lines

  1. /* 
  2. Copyright (C) 1989 Free Software Foundation
  3.     written by Eric Newton (newton@rocky.oswego.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  
  21. */
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <builtin.h>
  25. #include "libconfig.h"
  26. #include <CursesWindow.h>
  27.  
  28.  
  29. /*
  30.  * C++ interface to curses library.
  31.  *
  32.  */
  33.  
  34. /*
  35.  * varargs functions are handled conservatively:
  36.  * They interface directly into the underlying 
  37.  * _doscan, _doprnt and/or vfprintf routines rather than
  38.  * assume that such things are handled compatibly in the curses library
  39.  */
  40.  
  41. int CursesWindow::scanw(const char * fmt, ...)
  42. {
  43.   va_list args;
  44.   va_start(args, fmt);
  45.   char buf[BUFSIZ];
  46.   FILE b;
  47.   b._flag = _IOREAD|_IOSTRG;
  48.   b._ptr = buf;
  49.   b._cnt = BUFSIZ;
  50.   int result = wgetstr(w, buf);
  51.   if (result != ERR)
  52.     result = _doscan(&b, fmt, args);
  53.   va_end(args);
  54.   return result;
  55. }
  56.  
  57. int CursesWindow::mvscanw(int y, int x, const char * fmt, ...)
  58. {
  59.   va_list args;
  60.   va_start(args, fmt);
  61.   char buf[BUFSIZ];
  62.   FILE b;
  63.   b._flag = _IOREAD|_IOSTRG;
  64.   b._ptr = buf;
  65.   b._cnt = BUFSIZ;
  66.   int result = wmove(w, y, x);
  67.   if (result != ERR)
  68.   {
  69.     result = wgetstr(w, buf);
  70.     if (result != ERR)
  71.       result = _doscan(&b, fmt, args);
  72.   }
  73.   va_end(args);
  74.   return result;
  75. }
  76.  
  77. int CursesWindow::printw(const char * fmt, ...)
  78. {
  79.   va_list args;
  80.   va_start(args, fmt);
  81.   char buf[BUFSIZ];
  82. #ifndef HAVE_VPRINTF
  83.   FILE b;
  84.   b._flag = _IOWRT|_IOSTRG;
  85.   b._ptr = buf;
  86.   b._cnt = BUFSIZ;
  87.   _doprnt(fmt, args, &b);
  88.   putc('\0', &b);
  89. #else
  90.   vsprintf(buf, fmt, args);
  91. #endif
  92.   va_end(args);
  93.   return waddstr(w, buf);
  94. }
  95.  
  96.  
  97. int CursesWindow::mvprintw(int y, int x, const char * fmt, ...)
  98. {
  99.   va_list args;
  100.   va_start(args, fmt);
  101.   char buf[BUFSIZ];
  102.   int result = wmove(w, y, x);
  103.   if (result != ERR)
  104.   {
  105. #ifndef HAVE_VPRINTF
  106.     FILE b;
  107.     b._flag = _IOWRT|_IOSTRG;
  108.     b._ptr = buf;
  109.     b._cnt = BUFSIZ;
  110.     _doprnt(fmt, args, &b);
  111.     putc('\0', &b);
  112. #else
  113.     vsprintf(buf, fmt, args);
  114. #endif
  115.     result = waddstr(w, buf);
  116.   }
  117.   va_end(args);
  118.   return result;
  119. }
  120.  
  121. CursesWindow::CursesWindow(int lines, int cols, int begin_y, int begin_x)
  122. {
  123.   if (count==0)
  124.     initscr();
  125.  
  126.   w = newwin(lines, cols, begin_y, begin_x);
  127.   if (w == 0)
  128.   {
  129.     (*lib_error_handler)("CursesWindow", "Cannot construct window");
  130.   }
  131.  
  132.   alloced = 1;
  133.   subwins = par = sib = 0;
  134.   count++;
  135. }
  136.  
  137. CursesWindow::CursesWindow(WINDOW* &window)
  138. {
  139.   if (count==0)
  140.     initscr();
  141.  
  142.   w = window;
  143.   alloced = FALSE;
  144.   subwins = 0;
  145.   count++;
  146. }
  147.  
  148. CursesWindow::CursesWindow(CursesWindow& win, int l, int c, 
  149.                            int by, int bx, char absrel = 'a')
  150. {
  151.  
  152.   if (absrel == 'r') // relative origin
  153.   {
  154.     by += win.begy();
  155.     bx += win.begx();
  156.   }
  157.  
  158.   // Even though we treat subwindows as a tree, the standard curses
  159.   // library needs the `subwin' call to link to the root in
  160.   // order to correctly perform refreshes, etc.
  161.  
  162.   CursesWindow* root = &win;
  163.   while (root->par != 0) root = root->par;
  164.  
  165.   w = subwin(root->w, l, c, by, bx);
  166.   if (w == 0)
  167.   {
  168.     (*lib_error_handler)("CursesWindow", "Cannot construct subwindow");
  169.   }
  170.  
  171.   par = &win;
  172.   sib = win.subwins;
  173.   win.subwins = this;
  174.   subwins = 0;
  175.   alloced = 1;
  176.   count++;
  177. }
  178.  
  179.  
  180. void CursesWindow::kill_subwindows()
  181. {
  182.   for (CursesWindow* p = subwins; p != 0; p = p->sib)
  183.   {
  184.     p->kill_subwindows();
  185.     if (p->alloced)
  186.     {
  187.       if (p->w != 0)
  188.         ::delwin(p->w);
  189.       p->alloced = 0;
  190.     }
  191.     p->w = 0; // cause a run-time error if anyone attempts to use...
  192.   }
  193. }
  194.     
  195. CursesWindow::~CursesWindow() 
  196. {
  197.   kill_subwindows();
  198.  
  199.   if (par != 0)   // Snip us from the parent's list of subwindows.
  200.   {
  201.     CursesWindow * win = par->subwins;
  202.     CursesWindow * trail = 0;
  203.     for (;;)
  204.     {
  205.       if (win == 0)
  206.         break;
  207.       else if (win == this)
  208.       {
  209.         if (trail != 0)
  210.           trail->sib = win->sib;
  211.         else
  212.           par->subwins = win->sib;
  213.         break;
  214.       }
  215.       else
  216.       {
  217.         trail = win;
  218.         win = win->sib;
  219.       }
  220.     }
  221.   }
  222.  
  223.   if (alloced && w != 0) 
  224.     delwin(w);
  225.  
  226.   --count;
  227.   if (count == 0) 
  228.     endwin();
  229.   else if (count < 0) // cannot happen!
  230.   {
  231.     (*lib_error_handler)("CursesWindow", "Too many windows destroyed");
  232.   }
  233. }
  234.  
  235.