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

  1. /*
  2.  * TMenuBox.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_TRect
  13. #define Uses_TMenu
  14. #define Uses_TMenuItem
  15. #define Uses_TMenuBox
  16. #include <tvision/tv.h>
  17.  
  18. #include <string.h>
  19.  
  20. static TRect getRect( const TRect& bounds, TMenu *aMenu )
  21. {
  22.     short w =  10;
  23.     short h =  2;
  24.     if( aMenu != 0 )
  25.         {
  26.         for( TMenuItem *p = aMenu->items; p != 0; p = p->next )
  27.             {
  28.             if( p->name != 0 )
  29.                 {
  30.                 short l = cstrlen(p->name) + 6;
  31.                 if( p->command == 0 )
  32.                     l += 3;
  33.                 else
  34.                     if( p->param != 0 )
  35.                         l += cstrlen(p->param) + 2;
  36.                 w = max( l, w );
  37.                 }
  38.             h++;
  39.             }
  40.         }
  41.  
  42.     TRect r( bounds );
  43.  
  44.     if( r.a.x + w < r.b.x )
  45.         r.b.x = r.a.x + w;
  46.     else
  47.         r.a.x = r.b.x - w;
  48.  
  49.     if (r.a.y + h < r.b.y)
  50.         r.b.y = r.a.y + h;
  51.     else
  52.         r.a.y = r.b.y - h;
  53.  
  54.     return r;
  55. }
  56.  
  57. TMenuBox::TMenuBox( const TRect& bounds,
  58.                     TMenu *aMenu,
  59.                     TMenuView *aParentMenu) :
  60.     TMenuView( getRect( bounds, aMenu ), aMenu, aParentMenu )
  61. {
  62.     state |= sfShadow;
  63.     options |= ofPreProcess;
  64. }
  65.  
  66. static ushort cNormal, color;
  67.  
  68. void TMenuBox::frameLine( TDrawBuffer& b, short n )
  69. {
  70.     b.moveBuf( 0, &frameChars[n], cNormal, 2 );
  71.     b.moveChar( 2, frameChars[n+2], color, size.x - 4 );
  72.     b.moveBuf( size.x-2, &frameChars[n+3], cNormal, 2 );
  73. }
  74.  
  75. void TMenuBox::draw()
  76. {
  77.     TDrawBuffer    b;
  78.  
  79.     cNormal = getColor(0x0301);
  80.     ushort cSelect = getColor(0x0604);
  81.     ushort cNormDisabled = getColor(0x0202);
  82.     ushort cSelDisabled = getColor(0x0505);
  83.     short y = 0;
  84.     color =  cNormal;
  85.     frameLine( b, 0 );
  86.     writeBuf( 0, y++, size.x, 1, b );
  87.     if( current == NULL) current = menu->deflt;
  88.  
  89.     if( menu != 0 )
  90.         {
  91.         for( TMenuItem *p = menu->items; p != 0; p = p->next )
  92.             {
  93.             color = cNormal;
  94.             if( p->name == 0 )
  95.                 frameLine( b, 15 );
  96.             else
  97.                 {
  98.                 if( p->disabled )
  99.                     if( p ==  current )
  100.                         color = cSelDisabled;
  101.                     else
  102.                         color = cNormDisabled;
  103.                 else if( p == current )
  104.                     color = cSelect;
  105.                 frameLine( b, 10 );
  106.                 b.moveCStr( 3, p->name, color );
  107.                 if( p->command == 0 )
  108.                     b.putChar( size.x-4, 16 );
  109.                 else if( p->param != 0 )
  110.                     b.moveStr( size.x-3-strlen(p->param),
  111.                                p->param,
  112.                                color);
  113.                 }
  114.             writeBuf( 0, y++, size.x, 1, b );
  115.             }
  116.         }
  117.     color = cNormal;
  118.     frameLine( b, 5 );
  119. #ifndef __UNPATCHED
  120.     writeBuf( 0, y, size.x, 1, b );
  121. #else
  122.     writeBuf( 0, y++, size.x, 1, b );
  123. #endif
  124. }
  125.  
  126. TRect TMenuBox::getItemRect( TMenuItem *item )
  127. {
  128.     short  y = 1;
  129.     TMenuItem *p = menu->items;
  130.  
  131.     while( p != item )
  132.         {
  133.         y++;
  134.         p =  p->next;
  135.         }
  136.     return TRect( 2, y, size.x-2, y+1 );
  137. }
  138.  
  139. #if !defined(NO_STREAMABLE)
  140.  
  141. TStreamable *TMenuBox::build()
  142. {
  143.     return new TMenuBox( streamableInit );
  144. }
  145.  
  146. TMenuBox::TMenuBox( StreamableInit ) : TMenuView( streamableInit )
  147. {
  148. }
  149.  
  150. #endif
  151.