/////// File CursesWindow.C /////// #include #include #include "CursesWindow.h" // static objects for standard windows CursesWindow std_win(stdscr); CursesWindow cur_win(curscr); // C++ interface to C-based curses library CursesWindow::CursesWindow (int ls, int cs, int begin_y, int begin_x) { if (count==0) initscr(); w = newwin(ls, cs, begin_y, begin_x); if (w == 0) { cerr << "CursesWindow: Cannot construct window"; return; } alloced = 1; subwins = par = sib = 0; count++; } CursesWindow::CursesWindow(WINDOW* &window) { if (count==0) initscr(); w = window; alloced = 0; subwins = par = sib = 0; count++; } CursesWindow::CursesWindow (CursesWindow* win, int l, int c, int by, int bx, char absrel) { if (absrel == 'r') // relative origin { by += win->Begy(); bx += win->Begx(); } // Even though we treat subwindows as a tree, the standard curses // library needs the `subwin' call to link to the root in // order to correctly perform refreshes, etc. CursesWindow* root = win; while (root->par != 0) root = root->par; w = subwin(root->w, l, c, by, bx); if (w == 0) { cerr << "CursesWindow : Cannot construct subwindow"; /* set error member later */ return; } par = win; sib = win->subwins; win->subwins = this; subwins = 0; alloced = 1; count++; } void CursesWindow::kill_subwindows() { for (CursesWindow* p = subwins; p != 0; p = p->sib) { p->kill_subwindows(); if (p->alloced) { if (p->w != 0) ::delwin(p->w); p->alloced = 0; } p->w = 0; // cause a run-time error if anyone attempts to use... } } CursesWindow::~CursesWindow() { kill_subwindows(); if (par != 0) // Snip us from the parent's list of subwindows. { CursesWindow * win = par->subwins; CursesWindow * trail = 0; for (;;) { if (win == 0) break; else if (win == this) { if (trail != 0) trail->sib = win->sib; else par->subwins = win->sib; break; } else { trail = win; win = win->sib; } } } if (alloced && w != 0) delwin(w); --count; if (count == 0) endwin(); else if (count < 0) // cannot happen! { cerr << "CursesWindow: Too many windows destroyed"; return; } }