home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / src / TStatusLine.cpp < prev    next >
C/C++ Source or Header  |  1999-05-21  |  7KB  |  331 lines

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