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

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