home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_11 / 1011054b < prev    next >
Text File  |  1992-09-08  |  3KB  |  181 lines

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