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

  1. // scroller.cpp RHS 10/15/91
  2.  
  3. #include"scroller.h"
  4.  
  5. Scroller::Scroller(char *title, DWORD position, BYTE colors, 
  6.         int num, int size)  
  7.         : Window(title, position, colors)
  8.     {
  9.     bufsize = size;
  10.     startBuf = curBuf = 0;
  11.     endBuf = numbuffers-1;
  12.     buffer_changed = FALSE;
  13.     }
  14.  
  15. Scroller::Scroller(char *title, DWORD position, BYTE colors)
  16.         : Window(title, position, colors)
  17.     {
  18.     numbuffers = bufsize = 0;
  19.     startBuf = endBuf = curBuf = 0;
  20.     buffer_changed = FALSE;
  21.     }
  22.  
  23. int Scroller::Init(int num, int size)
  24.     {
  25.     bufsize = size;
  26.     SetColors();
  27.     endBuf = numbuffers-1;
  28.     return numbuffers;
  29.     }
  30.  
  31. void Scroller::Open(void)
  32.     {
  33.     Window::Open();
  34.     startBuf = curBuf = 0;
  35. //    endBuf = MaxBufs();
  36.     Paint();
  37.     }
  38.  
  39. void Scroller::Paint(void)
  40.     {
  41.     int i;
  42.     for(i = startBuf; (i < endBuf+1) && (i-startBuf < clientheight); i++)
  43.         DispBuffer(i-startBuf,i);
  44.                                             // display current
  45.     DispBuffer(curBuf-startBuf,curBuf,SCROLLER_CURRENT);
  46.     buffer_changed = FALSE;
  47.     }
  48.  
  49. void Scroller::Home(void)
  50.     {
  51.     BufMgt(SCROLL_HOME);
  52.  
  53.     if(startBuf == 0 && !buffer_changed)
  54.         {
  55.         if(curBuf == 0)
  56.             return;
  57.         DispBuffer(curBuf,curBuf);          // no longer current
  58.         curBuf = 0;
  59.                                             // top one now current
  60.         DispBuffer(curBuf,curBuf,SCROLLER_CURRENT);
  61.         return;
  62.         }
  63.     curBuf = startBuf = 0;
  64.     endBuf = Lines()-1;
  65.     Paint();
  66.     }
  67.  
  68. void Scroller::End(void)
  69.     {
  70.     BufMgt(SCROLL_END);
  71. //    if(endBuf == MaxBufs())
  72. //        return;
  73.  
  74. //    if(endBuf == (numbuffers-1) && !buffer_changed)
  75.       if((endBuf == MaxBufs()) && !buffer_changed)
  76.         {
  77.         DispBuffer(curBuf-startBuf,curBuf); // no longer current
  78.         curBuf = endBuf;
  79.                                             // top one now current
  80.         DispBuffer(curBuf-startBuf,curBuf,SCROLLER_CURRENT);
  81.         return;
  82.         }
  83.  
  84. //    curBuf = endBuf = numbuffers-1;
  85.     curBuf = endBuf = MaxBufs();
  86.     startBuf = endBuf-(int)(Lines()-1);
  87.     Paint();
  88.     }
  89.  
  90. void Scroller::PgUp(void)
  91.     {
  92.     BufMgt(SCROLL_PGUP);
  93.  
  94.     if(startBuf == 0 && !buffer_changed)
  95.         {
  96.         Home();
  97.         return;
  98.         }
  99.  
  100.     curBuf -= startBuf;
  101.     endBuf -= startBuf;
  102.  
  103.     if((startBuf -= (int)Lines()) < 0)
  104.         startBuf = 0;
  105.  
  106.     curBuf += startBuf;
  107.     endBuf += startBuf;
  108.     Paint();
  109.     }
  110.  
  111. void Scroller::PgDn(void)
  112.     {
  113.     BufMgt(SCROLL_PGDN);
  114.  
  115.     if(!buffer_changed)
  116.         if((endBuf == MaxBufs()) || (endBuf+Lines() > MaxBufs()))
  117.             {
  118.             End();
  119.             return;
  120.             }
  121.  
  122.     curBuf = endBuf-curBuf;
  123.     startBuf = endBuf-startBuf;
  124.  
  125.     if((endBuf += Lines()) >= numbuffers)
  126.         endBuf = numbuffers-1;
  127.     curBuf = endBuf-curBuf;
  128.     startBuf = endBuf-startBuf;
  129.  
  130.     Paint();
  131.     }
  132.  
  133. // Down and Up are dedicated to 'Mobimientos Del Alma' by Earl Klugh
  134. void Scroller::Down(void)
  135.     {
  136.     BufMgt(SCROLL_DN);
  137.     if(curBuf == endBuf)                    // if on last displayed buffer
  138.         {
  139.         if(endBuf == numbuffers-1)          // if no more buffers, out
  140.             return;
  141.         if(endBuf == MaxBufs())
  142.             return;
  143.                                             // no longer current
  144.         DispBuffer(curBuf-startBuf,curBuf);
  145.         curBuf++;                           // move to next line
  146.         startBuf++;
  147.         endBuf++;
  148.         Scroll(WIN_SCROLLUP,1);             // scroll window up
  149.                                             // new buffer now current
  150.         DispBuffer(Lines()-1,curBuf,SCROLLER_CURRENT);
  151.         return;
  152.         }
  153.                                             // else not on bottom line
  154.     DispBuffer(curBuf-startBuf,curBuf);     // this buffer no longer current
  155.     curBuf++;                               // next buffer
  156.                                             // and make it current
  157.     DispBuffer(curBuf-startBuf,curBuf,SCROLLER_CURRENT);
  158.     if(buffer_changed)
  159.         Paint();
  160.     }
  161.  
  162. void Scroller::Up(void)
  163.     {
  164.     BufMgt(SCROLL_UP);
  165.  
  166.     if(!curBuf)                             // if on first buffer
  167.         return;                             // fuh-gi-da-bou-dit!
  168.  
  169.     if(curBuf == startBuf)                  // else on first line
  170.         {
  171.         DispBuffer(0,curBuf); // no longer current line
  172.         curBuf--;                           // m'on back!
  173.         startBuf--;
  174.         endBuf--;
  175.         Scroll(WIN_SCROLLDN,1);             // scroll window down
  176.         DispBuffer(0,curBuf,SCROLLER_CURRENT);  // this line current
  177.         return;
  178.         }
  179.     DispBuffer(curBuf-startBuf,curBuf);     // else not on first line
  180.                                             // no longer current line
  181.     curBuf--;                               // back up
  182.                                             // this line current
  183.     DispBuffer(curBuf-startBuf,curBuf,SCROLLER_CURRENT);
  184.     if(buffer_changed)
  185.         Paint();
  186.     }
  187.  
  188. void Scroller::DispBuffer(int line, int bufnum, BYTE attribute)
  189.     {
  190.     BYTE attrib = GetAttribute(bufnum);    // retrieve attribute byte
  191.     switch(attribute)                           // check this operation:
  192.         {
  193.         case SCROLLER_NORMAL:                   // turn off CURRENT bit
  194.             attrib &= 0xfe;
  195.             break;
  196.         case SCROLLER_CURRENT:                  // turn on CURRENT bit
  197.             attrib |= 0x01;
  198.             break;
  199.         default:
  200.             attrib = attribute;
  201.         }
  202.     SetAttribute(bufnum,attrib);         // store attribute byte
  203.     BYTE color = Colors(attrib);            // get color value
  204. //    strcpy(workbuffer,GetBuffer(bufnum));
  205.     char buffer[80];
  206.     strcpy(buffer,GetBuffer(bufnum));
  207.     AtSay(line,0,buffer,color);
  208.     }
  209.  
  210. BYTE Scroller::Select(void)
  211.     {
  212.     BYTE attrib = GetAttribute(curBuf);
  213.  
  214.     if(attrib & SCROLLER_DELETED)
  215.         return attrib;
  216.     if(attrib & SCROLLER_SELECTED_NORMAL)       // if selected
  217.         attrib &= 0xfd;                         // de-select
  218.     else                                        // if not selected
  219.         attrib |= SCROLLER_SELECTED_NORMAL;     // select it
  220.     SetAttribute(curBuf,attrib);
  221.     DispBuffer(curBuf-startBuf,curBuf,attrib);
  222.     return attrib;
  223.     }
  224.  
  225. void Scroller::SetColors(BYTE cur, BYTE sel_cur, 
  226.     BYTE sel_norm, BYTE norm, BYTE sel_del, BYTE del_cur)
  227.     {
  228.     ScrColors[SCROLLER_NORMAL] = norm;
  229.     ScrColors[SCROLLER_CURRENT] = cur;
  230.     ScrColors[SCROLLER_SELECTED_NORMAL] = sel_norm;
  231.     ScrColors[SCROLLER_SELECTED_CURRENT] = sel_cur;
  232.     ScrColors[SCROLLER_DELETED] = sel_del;
  233.     ScrColors[SCROLLER_DELETED_CURRENT] = del_cur;
  234.     }
  235.  
  236. //#define MAIN
  237. #if defined(MAIN)
  238. #include<stdio.h>
  239. #include"stdapp.h"
  240.  
  241. class TestScroller : public StandardApplication
  242.     {
  243. public:
  244.     TestScroller(void);
  245.     };
  246.  
  247. TestScroller::TestScroller(void)
  248.     {
  249.     Scroller scroller("Test Scroller!",MakePosition(6,20,
  250.         (screen.Lines() > 25 ? 42 : 18), 26));
  251.  
  252.     scroller.Init(100,50);
  253.     int i;
  254.     char buf[50];
  255.  
  256.     for(i = 0; i < 100; i++)
  257.         {
  258.         sprintf(buf,"This is buffer number %02d!",i);
  259.         scroller.Change(i,buf,0,i*1L);
  260.         }
  261. #ifdef JUNK
  262.     for(i = 0; i < 100; i++)
  263.         printf("%s  Attribute=%02d  Recno=%02ld\n",
  264.             scroller[i]->buffer,
  265.             scroller[i]->attribute,
  266.             scroller[i]->recno);
  267. #endif
  268.  
  269.     scroller.Open();
  270.     while(TRUE)
  271.         {
  272.         switch(keyboard.Get())
  273.             {
  274.             case UP:
  275.                 scroller.Up();
  276.                 break;
  277.             case DOWN:
  278.                 scroller.Down();
  279.                 break;
  280.             case HOME:
  281.                 scroller.Home();
  282.                 break;
  283.             case END:
  284.                 scroller.End();
  285.                 break;
  286.             case PGDN:
  287.                 scroller.PgDn();
  288.                 break;
  289.             case PGUP:
  290.                 scroller.PgUp();
  291.                 break;
  292.             case SPACE:
  293.                 scroller.Select();
  294.                 break;
  295.             case ESC:
  296.                 return;
  297.             }
  298.         }
  299.     }
  300.  
  301. void main(void)
  302.     {
  303.     TestScroller testscroller;
  304.     }
  305.  
  306. #endif
  307.  
  308.  
  309.