home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 9.ddi / TVSRC.ZIP / TSTATUSL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  7.9 KB  |  315 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       tstatusl.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TStatusLine member functions              */
  6. /*------------------------------------------------------------*/
  7.  
  8. /*------------------------------------------------------------*/
  9. /*                                                            */
  10. /*    Turbo Vision -  Version 1.0                             */
  11. /*                                                            */
  12. /*                                                            */
  13. /*    Copyright (c) 1991 by Borland International             */
  14. /*    All Rights Reserved.                                    */
  15. /*                                                            */
  16. /*------------------------------------------------------------*/
  17.  
  18. #define Uses_TStatusLine
  19. #define Uses_TStatusItem
  20. #define Uses_TStatusDef
  21. #define Uses_TDrawBuffer
  22. #define Uses_TEvent
  23. #define Uses_opstream
  24. #define Uses_ipstream
  25. #include <tv.h>
  26.  
  27. #if !defined( __STRING_H )
  28. #include <String.h>
  29. #endif  // __STRING_H
  30.  
  31. #define cpStatusLine "\x02\x03\x04\x05\x06\x07"
  32.  
  33. TStatusLine::TStatusLine( const TRect& bounds, TStatusDef& aDefs ) :
  34.     TView( bounds ),
  35.     defs( &aDefs )
  36. {
  37.     options |= ofPreProcess;
  38.     eventMask |= evBroadcast;
  39.     growMode = gfGrowLoY | gfGrowHiX | gfGrowHiY;
  40.     findItems();
  41. }
  42.  
  43. void TStatusLine::disposeItems( TStatusItem *item )
  44. {
  45.     while( item != 0 )
  46.         {
  47.         TStatusItem *T = item;
  48.         item = item->next;
  49.         delete T;
  50.         }
  51. }
  52.  
  53. TStatusLine::~TStatusLine(void)
  54. {
  55.     while( defs != 0 )
  56.         {
  57.         TStatusDef *T = defs;
  58.         defs = defs->next;
  59.         disposeItems( T->items );
  60.         delete T;
  61.         }
  62. }
  63.  
  64. void TStatusLine::draw()
  65. {
  66.     drawSelect( 0 );
  67. }
  68.  
  69. void TStatusLine::drawSelect( TStatusItem *selected )
  70. {
  71.     TDrawBuffer b;
  72.     ushort color;
  73.     char hintBuf[256];
  74.  
  75.     ushort cNormal = getColor(0x0301);
  76.     ushort cSelect = getColor(0x0604);
  77.     ushort cNormDisabled = getColor(0x0202);
  78.     ushort cSelDisabled = getColor(0x0505);
  79.     b.moveChar( 0, ' ', cNormal, size.x );
  80.     TStatusItem *T =  items;
  81.     ushort i = 0;
  82.  
  83.     while( T != 0 )
  84.         {
  85.         if( T->text != 0 )
  86.             {
  87.             ushort l = cstrlen( T->text );
  88.             if( i + l < size.x )
  89.                 {
  90.                 if( commandEnabled( T->command) )
  91.                     if( T == selected )
  92.                         color = cSelect;
  93.                     else
  94.                         color = cNormal;
  95.                 else
  96.                     if( T == selected )
  97.                         color = cSelDisabled;
  98.                     else
  99.                         color = cNormDisabled;
  100.  
  101.                 b.moveChar( i, ' ', color, 1 );
  102.                 b.moveCStr( i+1, T->text, color );
  103.                 b.moveChar( i+l+1, ' ', color, 1 );
  104.                 }
  105.             i += l+2;
  106.             }
  107.         T = T->next;
  108.         }
  109.     if( i < size.x - 2 )
  110.         {
  111.         strcpy( hintBuf, hint( helpCtx ) );
  112.         if( *hintBuf != EOS )
  113.             {
  114.             b.moveStr( i, hintSeparator, cNormal );
  115.             i += 2;
  116.             if( strlen(hintBuf) + i > size.x )
  117.                 hintBuf[size.x-i] = EOS;
  118.             b.moveStr( i, hintBuf, cNormal );
  119.             i += strlen(hintBuf);
  120.             }
  121.         }
  122.     writeLine( 0, 0, size.x, 1, b );
  123. }
  124.  
  125. void TStatusLine::findItems()
  126. {
  127.     TStatusDef *p = defs;
  128.     while( p != 0 && ( helpCtx < p->min || helpCtx > p->max ) )
  129.         p = p->next;
  130.     items = ( p == 0 ) ? 0 : p->items;
  131. }
  132.  
  133. TPalette& TStatusLine::getPalette() const
  134. {
  135.     static TPalette palette( cpStatusLine, sizeof( cpStatusLine )-1 );
  136.     return palette;
  137. }
  138.  
  139. TStatusItem *TStatusLine::itemMouseIsIn( TPoint mouse )
  140. {
  141.     if( mouse.y !=  0 )
  142.         return 0;
  143.  
  144.     ushort i;
  145.     TStatusItem *T;
  146.  
  147.     for( i = 0, T = items; T != 0; T = T->next)
  148.         {
  149.         if( T->text != 0 )
  150.             {
  151.             ushort k = i + cstrlen(T->text) + 2;
  152.             if( mouse.x >= i && mouse. x < k )
  153.                 return T;
  154.             i = k;
  155.             }
  156.         }
  157.     return 0;
  158. }
  159.  
  160. void TStatusLine::handleEvent( TEvent& event )
  161. {
  162.     TView::handleEvent(event);
  163.  
  164.     switch (event.what)
  165.         {
  166.         case  evMouseDown:
  167.             {
  168.             TStatusItem *T = 0;
  169.  
  170.             do  {
  171.                 TPoint mouse = makeLocal( event.mouse.where );
  172.                 if( T != itemMouseIsIn(mouse) )
  173.                     drawSelect( T = itemMouseIsIn(mouse) );
  174.                 } while( mouseEvent( event, evMouseMove ) );
  175.  
  176.             if( T != 0 && commandEnabled(T->command) )
  177.                 {
  178.                 event.what = evCommand;
  179.                 event.message.command = T->command;
  180.                 event.message.infoPtr = 0;
  181.                 putEvent(event);
  182.                 }
  183.             clearEvent(event);
  184.             drawView();
  185.             break;
  186.             }
  187.         case evKeyDown:
  188.             {
  189.             for( TStatusItem *T = items; T != 0; T = T->next )
  190.                 {
  191.                 if( event.keyDown.keyCode ==  T->keyCode && 
  192.                     commandEnabled(T->command))
  193.                     {
  194.                     event.what = evCommand;
  195.                     event.message.command = T->command;
  196.                     event.message.infoPtr = 0;
  197.                     return;
  198.                     }
  199.             }
  200.             break;
  201.             }
  202.         case evBroadcast:
  203.             if( event.message.command == cmCommandSetChanged )
  204.                 drawView();
  205.             break;
  206.         }
  207. }
  208.  
  209. const char* TStatusLine::hint( ushort )
  210. {
  211.     return "";
  212. }
  213.  
  214. void TStatusLine::update()
  215. {
  216.     TView *p = TopView();
  217.     ushort h = ( p != 0 ) ? p->getHelpCtx() : hcNoContext;
  218.     if( helpCtx != h )
  219.         {
  220.         helpCtx = h;
  221.         findItems();
  222.         drawView();
  223.         }
  224. }
  225.  
  226. void TStatusLine::writeItems( opstream& os, TStatusItem *ts )
  227. {
  228.     int count = 0;
  229.     for( TStatusItem *t = ts; t != 0; t = t->next )
  230.         count++;
  231.     os << count;
  232.     for( ; ts != 0; ts = ts->next )
  233.         {
  234.         os.writeString( ts->text );
  235.         os << ts->keyCode << ts->command;
  236.         }
  237. }
  238.  
  239. void TStatusLine::writeDefs( opstream& os, TStatusDef *td )
  240. {
  241.     int count = 0;
  242.     for( TStatusDef *t = td; t != 0; t = t->next )
  243.         count++;
  244.     os << count;
  245.     for( ; td != 0; td = td->next )
  246.         {
  247.         os << td->min << td->max;
  248.         writeItems( os, td->items );
  249.         }
  250. }
  251.  
  252. void TStatusLine::write( opstream& os )
  253. {
  254.     TView::write( os );
  255.     writeDefs( os, defs );
  256. }
  257.  
  258. TStatusItem *TStatusLine::readItems( ipstream& is )
  259. {
  260.     TStatusItem *cur = 0;
  261.     TStatusItem *first;
  262.     TStatusItem **last = &first;
  263.     int count;
  264.     is >> count;
  265.     while( count-- > 0 )
  266.         {
  267.         const char *t = is.readString();
  268.         int key, cmd;
  269.         is >> key >> cmd;
  270.         cur = new TStatusItem( t, key, cmd );
  271.         *last = cur;
  272.         last = &(cur->next);
  273.         }
  274.     *last = 0;
  275.     return first;
  276. }
  277.  
  278. TStatusDef *TStatusLine::readDefs( ipstream& is )
  279. {
  280.     TStatusDef *cur = 0;
  281.     TStatusDef *first;
  282.     TStatusDef **last = &first;
  283.     int count;
  284.     is >> count;
  285.     while( count-- > 0 )
  286.         {
  287.         int min, max;
  288.         is >> min >> max;
  289.         cur = new TStatusDef( min, max, readItems( is ) );
  290.         *last = cur;
  291.         last = &(cur->next);
  292.         }
  293.     *last = 0;
  294.     return first;
  295. }
  296.  
  297. void *TStatusLine::read( ipstream& is )
  298. {   
  299.     TView::read( is );
  300.     defs = readDefs( is );
  301.     findItems();
  302.     return this;
  303. }
  304.  
  305. TStreamable *TStatusLine::build()
  306. {
  307.     return new TStatusLine( streamableInit );
  308. }
  309.  
  310. TStatusLine::TStatusLine( StreamableInit ) : TView( streamableInit )
  311. {
  312. }
  313.  
  314.  
  315.