00001
00002
00003
00010 #include <ncurses.h>
00011 #include <cstdio>
00012 #include <streambuf>
00013 #include <ostream>
00014
00015 using namespace std;
00016
00025 class outbuf : public streambuf
00026 {
00027 protected:
00028 WINDOW *win;
00029
00030 public:
00031 outbuf(int nlines, int ncols, int begin_y, int begin_x)
00032 {
00033 win=newwin(nlines,ncols,begin_y,begin_x);
00034 #if 1
00035 box(win, 0 , 0);
00036
00037
00038 wrefresh(win);
00039 delwin(win);
00040 if (nlines==0) nlines=LINES-begin_y;
00041 if (ncols==0) ncols=COLS-begin_x;
00042 win=newwin(nlines-2,ncols-2,begin_y+1,begin_x+1);
00043 #endif
00044
00045 scrollok(win,true);
00046 }
00047
00048 ~outbuf()
00049 {
00050 delwin(win); win=NULL;
00051 }
00052
00053 int wgetch()
00054 {
00055 return ::wgetch(win);
00056 }
00057
00058 protected:
00059 virtual int overflow (int c)
00060 {
00061 waddch(win,c);
00062 wrefresh(win);
00063 return c;
00064 }
00065 };
00066
00067
00076 class Cwin : public ostream
00077 {
00078 protected:
00079 outbuf mywindow;
00080 public:
00081 Cwin(int nlines, int ncols, int begin_y, int begin_x)
00082 : ostream(&mywindow), mywindow(nlines,ncols,begin_y,begin_x)
00083 { }
00084
00085 int wgetch() { return mywindow.wgetch(); }
00086
00087 };
00088
00089
00090 static void exit_my_ncurses();
00091 static bool init_called = false;
00092
00093 static void init_my_ncurses()
00094 {
00095 if (init_called) exit(94);
00096 initscr();
00097 cbreak();
00098 static bool installed = false;
00099 if (!installed) atexit(exit_my_ncurses);
00100 installed=true;
00101 init_called=true;
00102 }
00103
00104 static void exit_my_ncurses()
00105 {
00106 if (init_called)
00107 {
00108 endwin();
00109 init_called=false;
00110 }
00111 }
00112
00113
00114
00115 #if 0
00116 int main()
00117 {
00118 initscr();
00119 cbreak();
00120 Cwin win1(5,40,10,12);
00121 Cwin win2(5,40,16,7);
00122
00123 for (int i=0; i< 100; ++i)
00124 {
00125 win1 << "Hallo! " << i << endl;
00126 win2 << i;
00127 sleep(1);
00128 }
00129
00130 sleep(10);
00131
00132 endwin();
00133 };
00134 #endif