home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 04 / window.cpp < prev    next >
C/C++ Source or Header  |  1992-02-05  |  6KB  |  279 lines

  1. // window.cpp RHS 11/1/91
  2.  
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdarg.h>
  6. #include<malloc.h>
  7. #include"window.h"
  8.  
  9. const BYTE  WIN_ACTIVE_VERT     = 186;
  10. const BYTE  WIN_ACTIVE_HORZ     = 205;
  11. const BYTE  WIN_ACTIVE_ULEFT    = 201;
  12. const BYTE  WIN_ACTIVE_URIGHT   = 187;
  13. const BYTE  WIN_ACTIVE_LLEFT    = 200;
  14. const BYTE  WIN_ACTIVE_LRIGHT   = 188;
  15.  
  16. const BYTE  WIN_INACTIVE_VERT   = 179;
  17. const BYTE  WIN_INACTIVE_HORZ   = 196;
  18. const BYTE  WIN_INACTIVE_ULEFT  = 218;
  19. const BYTE  WIN_INACTIVE_URIGHT = 191;
  20. const BYTE  WIN_INACTIVE_LLEFT  = 192;
  21. const BYTE  WIN_INACTIVE_LRIGHT = 217;
  22.  
  23. BYTE WIN_DEFAULT_COLORS = (MakeBG(VID_BLUE)+VID_YELLOW);
  24.  
  25. void Window::SetDefaultColors(BYTE newcolors)
  26.     {   
  27.     WIN_DEFAULT_COLORS = newcolors; 
  28.     }
  29.  
  30.     // ****************************************** Class Window
  31. Window::Window(char *title, DWORD position,
  32.     BYTE attrib, BYTE fc, BYTE clr)
  33.     {
  34.     SetDefaults(title, position,
  35.         attrib,fc,clr);
  36.     clientwidth = width-2;
  37.     clientheight = height-3;
  38.     }
  39.  
  40. void Window::SetDefaults(char *ttl, DWORD position,
  41.         BYTE attrib, BYTE fc, BYTE clr)
  42.     {
  43.     if(title = (char far *)farmalloc(strlen(ttl)+1))
  44.         _fstrcpy(title,ttl);
  45.     attributes = attrib;
  46.     GetPosition(position,top,left,height,width);
  47.     fillchar = fc;
  48.     color = clr;
  49.  
  50.     curRow = curCol = 0;
  51.     isOpen = FALSE;
  52.     }
  53.  
  54. // ADD INITIALIZATION CODE HERE!
  55. void Window::Init(void)
  56.     {
  57.  
  58.     }
  59.  
  60.  
  61. void Window::Open(void)
  62.     {
  63.     int bottom = top+height;
  64.  
  65.     Screen::GotoXY(top,left);
  66.     ClearEntireWindow();
  67.     int i,j;
  68.     WORD far *temp = params.curpos;
  69.     WORD bar = ScreenChar(WIN_ACTIVE_HORZ);
  70.  
  71.     isOpen = TRUE;
  72.     for(i = top; i < bottom-1; )
  73.         {
  74.         if(i == top)
  75.             SetTitle();
  76.         else if(i == bottom-2)
  77.             {
  78.             *temp = ScreenChar(WIN_ACTIVE_LLEFT);
  79.             SetWords(&temp[1],bar,width-1);
  80.             temp[width-1] = ScreenChar(WIN_ACTIVE_LRIGHT);
  81.             }
  82.         else
  83.             temp[(width-1)] = *temp = ScreenChar(WIN_ACTIVE_VERT);
  84.         i++;
  85.         temp = Screen::GotoXY(i,left);
  86.         }
  87.  
  88.     clientwidth = width-2;
  89.     clientheight = height-3;
  90.     GotoXY(0,0);
  91.     }
  92.  
  93. void Window::GetTitle(char *buf)
  94.     {
  95.     _fstrcpy(buf,title);
  96.     }
  97.  
  98. void Window::SetTitle(char *newtitle)
  99.     {
  100.     if(newtitle)
  101.         {
  102.         if(title)
  103.             farfree(title);
  104.         if(title = (char far *)farmalloc(strlen(newtitle)+1))
  105.             _fstrcpy(title,newtitle);
  106.         }
  107.  
  108.     if(isOpen)
  109.         {
  110.         Screen::GotoXY(top,left);
  111.         WORD far *temp = params.curpos;
  112.         WORD bar = ScreenChar(WIN_ACTIVE_HORZ);
  113.  
  114.         *temp = ScreenChar(WIN_ACTIVE_ULEFT);
  115.         SetWords(&temp[1],bar,width-1);
  116.         temp[width-1] = ScreenChar(WIN_ACTIVE_URIGHT);
  117.  
  118.         int pos, len = _fstrlen(title);
  119.  
  120.         char buf[80];
  121.  
  122.         *buf = ' ';
  123.  
  124.         if(width < (_fstrlen(title)+4))
  125.             {
  126.             _fstrncpy(&buf[1],title,width-4);
  127.             strcpy(&buf[width-4]," ");
  128.             Screen::AtSay(top,left+1,buf);
  129.             }
  130.         else
  131.             {
  132.             _fstrcpy(&buf[1],title);
  133.             strcat(buf," ");
  134.             Screen::AtSay(top,left+(width-(len+2))/2,buf);
  135.             }
  136.         }
  137.     }
  138.  
  139. WORD far *Window::GotoXY(int row, int col)
  140.     {
  141. // ********************************** add range checking here
  142.     curRow = row;
  143.     curCol = col;
  144.     return Screen::GotoXY(row+top+1,col+left+1);
  145.     }
  146.  
  147. void Window::ClearEntireWindow(void)
  148.     {
  149.     Screen::Clear(top,left,top+height,left+width,color);
  150.     }
  151.  
  152. void Window::Clear(void)
  153.     {
  154.     Screen::Clear(top+1,left+1,top+clientheight+2,left+clientwidth+1,color);
  155.     }
  156.  
  157. void Window::Paint(void)
  158.     {
  159.     }
  160.  
  161. Window::~Window(void)
  162.     {
  163.     if(title)
  164.         farfree(title);
  165.     }
  166.  
  167. void cdecl Window::AtSay(int row, int col, char *fmt, ...)
  168.     {
  169.     if(!workbuffer)
  170.         return;
  171.     va_list argptr;
  172.  
  173.     va_start(argptr,fmt);
  174.     vsprintf(workbuffer,fmt,argptr);
  175.     va_end(arg_ptr);
  176.     AtSay(row,col,workbuffer,color);
  177.     }
  178.  
  179. void Window::AtSay(int row, int col, char *buffer, BYTE color)
  180.     {
  181.     GotoXY(row,col);
  182.     strcpy(workbuffer,buffer);
  183.     if(strlen(workbuffer) > clientwidth-col)
  184.         workbuffer[clientwidth-col] = '\0';
  185.     Write(workbuffer,color);
  186.     }
  187.  
  188. void Window::Scroll(int direction,int numlines)
  189.     {
  190.     _DL = left+clientwidth;
  191.     _DH = top+clientheight;
  192.     _CL = left+1;
  193.     _CH = top+1;
  194.     _BH = color;
  195.     _AH = (direction == WIN_SCROLLUP ? 6 : 7);
  196.     _AL = numlines;
  197.     asm int 10h
  198.     }
  199.  
  200.  
  201. void cdecl Window::Printf(char *fmt, ...)
  202.     {
  203.     char temp[DEFAULTLINELEN];
  204.  
  205.     va_list argptr;
  206.  
  207.     va_start(argptr,fmt);
  208.     vsprintf(temp,fmt,argptr);
  209.     va_end(arg_ptr);
  210.  
  211.     int len = strlen(temp);
  212.     char *q = temp, *p = strchr(temp,'\n');
  213.     if(!p)
  214.         {
  215.         Puts(temp);
  216.         return;
  217.         }
  218.  
  219.     while(TRUE)
  220.         {
  221.         if(p)
  222.             *p = '\0';
  223.         Puts(q);
  224.         if(!p)
  225.             break;
  226.         q = p+1;
  227.         if(q >= temp+len)
  228.             break;
  229.         p = strchr(q,'\n');
  230.         }
  231.     }
  232.  
  233. void Window::Puts(char *str)
  234.     {
  235.     if(curRow == clientheight-1)
  236.         Scroll(WIN_SCROLLUP,1);
  237.     AtSay(curRow,curCol,str,color);
  238.     if(curRow < clientheight-1)
  239.         curRow++;
  240.     curCol = 0;
  241.     }
  242.  
  243.     // ************************************* Class PopWin 
  244. PopWin::~PopWin(void)
  245.     {
  246.     if(isOpen)
  247.         Close();
  248.     farfree(saved_screen);
  249.     }
  250.  
  251. void PopWin::Close(void)
  252.     {
  253.     // restore screen
  254.     BYTE far *temp;
  255.     for(int i = 0; i < height-1; i++)
  256.         {
  257.         temp = (BYTE far *)Screen::GotoXY(top+i,left);
  258.         CpyWords(temp,&saved_screen[i*width*2],width);
  259.         }
  260.     isOpen = FALSE;
  261.     }
  262.  
  263. void PopWin::Open(void)
  264.     {
  265.     saved_screen = (BYTE far *)farmalloc((width+1)*(height+1)*2);
  266.  
  267.     if(!saved_screen)
  268.         return;
  269.  
  270.     BYTE far *temp;
  271.     for(int i = 0; i < height-1; i++)
  272.         {
  273.         temp = (BYTE far *)Screen::GotoXY(top+i,left);
  274.         CpyWords(&saved_screen[i*width*2],temp,width);
  275.         }
  276.  
  277.     Window::Open();
  278.     }
  279.