home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_01 / 1001131a < prev    next >
Text File  |  1991-11-19  |  4KB  |  172 lines

  1. #include "window.h"
  2. #include "vstream.h"  // console stream   header
  3.  
  4. // TC++ 1.0 didn't define _wscroll in conio.h
  5. #ifndef __BORLANDC__
  6. extern int _wscroll;
  7. #endif
  8.  
  9. // Initialize class variable. Note you can't do this in  the
  10. // Definition itself.
  11. win * win::topwin=NULL;
  12. win * win::lastwin=NULL;
  13.  
  14. win::win(int x0,int y0,int x1,int y1,unsigned int clr,int mar):
  15.        region(x0,y0,x1,y1,0)
  16.    {
  17.    if (!topwin)  // first window
  18.       {
  19.       textattr(7);  // reset screen
  20.       clrscr();
  21.       lastwin=this;
  22.       }
  23.    else
  24.       {
  25. // save  window contents   & cursor
  26.       topwin->reinit();
  27.       topwin->oldx=wherex();
  28.       topwin->oldy=wherey();
  29.       }
  30.    margin=mar;
  31.    color=clr;
  32.    prev=NULL;
  33.    if (topwin) topwin->prev=this;
  34.    next=topwin;
  35.    topwin=this;
  36.    window(x0,y0,x1,y1);
  37.    gotoxy(1,1);
  38.    textattr(clr);
  39.    clrscr();
  40.    }
  41.  
  42. void win::maketop(void)
  43.    {
  44.    win *gpw;
  45.  // return if already at top
  46.    if (this==topwin) return;
  47. // force top window to save
  48.    topwin->reinit();
  49.    topwin->oldx=wherex();
  50.    topwin->oldy=wherey();
  51. // patch link list
  52.    if (lastwin==this) lastwin=prev;
  53.    if (prev) prev->next=next;
  54.    if (next) next->prev=prev;
  55.    prev=NULL;
  56.    topwin->prev=this;
  57.    next=topwin;
  58.    topwin=this;
  59.    settop();
  60.  
  61. @cplus =    restore();  // Draw  our screen contents
  62.    }
  63.  
  64. void win::settop(void)
  65.    {
  66.    window(
  67.       topwin->left+topwin->margin,
  68.       topwin->top+topwin->margin,
  69.       topwin->right-topwin->margin,
  70.       topwin->bot-topwin->margin);
  71.    textattr(topwin->color);
  72.    gotoxy(topwin->oldx,topwin->oldy);
  73.    }
  74.  
  75.  
  76.  
  77. win::~win()
  78.    {
  79.    this->maketop();  // force us on top
  80. // just  in case  there is a margin
  81.    window(left,top,right,bot);
  82.    textattr(7);
  83.    clrscr();
  84.    destroy();
  85.    if (next) next->prev=NULL;
  86.    topwin=next;
  87.    if (!topwin)
  88.       {
  89.       window(1,1,80,25);
  90.       clrscr();
  91.       }
  92.    else
  93.       {
  94.       for (win *i=lastwin;i;i=i->prev)
  95.          {
  96.          i->restore();
  97.          if (i!=topwin) i->reinit();
  98.          }
  99.       settop();
  100.       }
  101.    }
  102.  
  103.  
  104. // boxwin methods
  105. boxwin::boxwin(int x0,int y0,int x1,int   y1,unsigned int   clr,int  boxt) :
  106.          win(x0-1,y0-1,x1+1,y1+1,clr,1)
  107.    {
  108.    draw_box(boxt,1,1,x1-x0+3,y1-y0+3);
  109.    window(x0,y0,x1,y1);
  110.    }
  111.  
  112. // General purpose box drawing function
  113. // Type  0: single line box
  114. // Type  1: double line box
  115. // Other types are easily added
  116. void draw_box(int type,int x0,int y0,int x1,int y1)
  117.    {
  118.    int oldscroll;  // old  value for _wscroll
  119.    int i;
  120.    int hline;
  121.  
  122. @cplus =    int vline;
  123.    int c1,c2,c3,c4;
  124.    int xlen;
  125.    int ylen;
  126.    if (type<0||type>1) return;  //  change value to   add more types
  127.    xlen=x1-x0;
  128.    ylen=y1-y0;
  129.    if (type==0)
  130.       {
  131. // Constants for a "normal" box
  132.       hline=196;
  133.       vline=179;
  134.       c1=218;
  135.       c2=191;
  136.       c3=192;
  137.       c4=217;
  138.       }
  139.    else if  (type==1)
  140.       {
  141.       hline=205;
  142.       vline=186;
  143.       c1=201;
  144.       c2=187;
  145.       c3=200;
  146.       c4=188;
  147.       }
  148.    oldscroll=_wscroll;
  149.    _wscroll=0;
  150.    gotoxy(x0+1,y0);
  151.    for (i=1;i<xlen;i++) putch(hline);
  152.    gotoxy(x0+1,y0+ylen);
  153.    for (i=1;i<xlen;i++) putch(hline);
  154.    gotoxy(x0,y0);
  155.    putch(c1);
  156.    gotoxy(x0+xlen,y0);
  157.    putch(c2);
  158.    gotoxy(x0,ylen+y0);
  159.    putch(c3);
  160.    gotoxy(xlen+x0,ylen+y0);
  161.    putch(c4);
  162.    for (i=y0;i<ylen;i++)
  163.       {
  164.       gotoxy(x0,i+1);
  165.       putch(vline);
  166.       gotoxy(xlen+x0,i+1);
  167.       putch(vline);
  168.       }
  169.    _wscroll=oldscroll;
  170.    }
  171.  
  172.