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

  1. /*------------------------------------------------------------*/
  2. /* filename -       tbutton.cpp                               */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*          TButton 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_TButton
  19. #define Uses_TDrawBuffer
  20. #define Uses_TEvent
  21. #define Uses_TRect
  22. #define Uses_TGroup
  23. #define Uses_opstream
  24. #define Uses_ipstream
  25. #include <tv.h>
  26.  
  27. #if !defined( __CTYPE_H )
  28. #include <ctype.h>
  29. #endif  // __CTYPE_H
  30.  
  31. #if !defined( __STRING_H )
  32. #include <String.h>
  33. #endif  // __STRING_H
  34.  
  35. #if !defined( __DOS_H )
  36. #include <Dos.h>
  37. #endif  // __DOS_H
  38.  
  39. const
  40.  
  41.     cmGrabDefault    = 61,
  42.     cmReleaseDefault = 62;
  43.  
  44. #define cpButton "\x0A\x0B\x0C\x0D\x0E\x0E\x0E\x0F"
  45.  
  46. TButton::TButton( const TRect& bounds,
  47.                   const char *aTitle,
  48.                   ushort aCommand,
  49.                   ushort aFlags) :
  50.     TView( bounds ),
  51.     flags( aFlags ),
  52.     amDefault( Boolean( (aFlags & bfDefault) != 0 ) ),
  53.     title( newStr( aTitle ) ),
  54.     command( aCommand )
  55. {
  56.     options |= ofSelectable | ofFirstClick | ofPreProcess | ofPostProcess;
  57.     eventMask |= evBroadcast;
  58.     if( !commandEnabled(aCommand) )
  59.         state |= sfDisabled;
  60. }
  61.  
  62. TButton::~TButton()
  63. {
  64.     delete (char *)title;
  65. }
  66.  
  67. void TButton::draw()
  68. {
  69.     drawState(False);
  70. }
  71.  
  72. void TButton::drawTitle( TDrawBuffer &b,
  73.                          int s,
  74.                          int i,
  75.                          ushort cButton,
  76.                          Boolean down
  77.                        )
  78. {
  79.     int l, scOff;
  80.     if( (flags & bfLeftJust) != 0 )
  81.         l = 1;
  82.     else
  83.         {
  84.         l = (s - cstrlen(title) - 1)/2;
  85.         if( l < 1 )
  86.             l = 1;
  87.         }
  88.     b.moveCStr( i+l, title, cButton );
  89.  
  90.     if( showMarkers == True && !down )
  91.         {
  92.         if( (state & sfSelected) != 0 )
  93.             scOff = 0;
  94.         else if( amDefault )
  95.             scOff = 2;
  96.         else
  97.             scOff = 4;
  98.         b.putChar( 0, specialChars[scOff] );
  99.         b.putChar( s, specialChars[scOff+1] );
  100.         }
  101. }
  102.  
  103. void TButton::drawState(Boolean down)
  104. {
  105.     ushort cButton, cShadow;
  106.     char   ch;
  107.     int    i;
  108.     TDrawBuffer b;
  109.  
  110.     if( (state & sfDisabled) != 0 )
  111.         cButton = getColor(0x0404);
  112.     else
  113.         {
  114.         cButton = getColor(0x0501);
  115.         if( (state & sfActive) != 0 )
  116.             if( (state & sfSelected) != 0 )
  117.                 cButton = getColor(0x0703);
  118.             else if( amDefault )
  119.                 cButton = getColor(0x0602);
  120.         }
  121.     cShadow = getColor(8);
  122.     int s = size.x-1;
  123.     int T = size.y / 2 - 1;
  124.  
  125.     for( int y = 0; y <= size.y-2; y++ )
  126.         {
  127.         b.moveChar( 0, ' ', cButton, size.x );
  128.         b.putAttribute( 0, cShadow );
  129.         if( down )
  130.             {
  131.             b.putAttribute( 1, cShadow );
  132.             ch =  ' ';
  133.             i = 2;
  134.             }
  135.         else
  136.             {
  137.             b.putAttribute( s, cShadow );
  138.             if( showMarkers == True )
  139.                 ch = ' ';
  140.             else
  141.                 {
  142.                 if( y == 0 )
  143.                     b.putChar( s, shadows[0] );
  144.                 else
  145.                     b.putChar( s, shadows[1] );
  146.                 ch = shadows[2];
  147.                 }
  148.             i =  1;
  149.             }
  150.  
  151.         if( y == T && title != 0 )
  152.             drawTitle( b, s, i, cButton, down );
  153.  
  154.         if( showMarkers && !down )
  155.             {
  156.             b.putChar( 1, markers[0] );
  157.             b.putChar( s-1, markers[1] );
  158.             }
  159.         writeLine( 0, y, size.x, 1, b );
  160.         }
  161.     b.moveChar( 0, ' ', cShadow, 2 );
  162.     b.moveChar( 2, ch, cShadow, s-1 );
  163.     writeLine( 0, size.y-1, size.x, 1, b );
  164. }
  165.  
  166. TPalette& TButton::getPalette() const
  167. {
  168.     static TPalette palette( cpButton, sizeof( cpButton )-1 );
  169.     return palette;
  170. }
  171.  
  172. void TButton::handleEvent( TEvent& event )
  173. {
  174.     TPoint mouse;
  175.     TRect clickRect;
  176.  
  177.     clickRect = getExtent();
  178.     clickRect.a.x++;
  179.     clickRect.b.x--;
  180.     clickRect.b.y--;
  181.  
  182.     if( event.what == evMouseDown )
  183.         {
  184.         mouse = makeLocal( event.mouse.where );
  185.         if( !clickRect.contains(mouse) )
  186.             clearEvent( event );
  187.         }
  188.     TView::handleEvent(event);
  189.  
  190.     switch( event.what )
  191.         {
  192.         case evMouseDown:
  193.             clickRect.b.x++;
  194.             Boolean down = False;
  195.             do  {
  196.                 mouse = makeLocal( event.mouse.where );
  197.                 if( down != clickRect.contains( mouse ) )
  198.                     {
  199.                     down = Boolean( !down );
  200.                     drawState( down );
  201.                     }
  202.                 } while( mouseEvent( event, evMouseMove ) );
  203.             if( down )
  204.                 {
  205.                 press();
  206.                 drawState( False );
  207.                 }
  208.             clearEvent( event );
  209.             break;
  210.  
  211.         case evKeyDown:
  212.             char c = hotKey( title );
  213.             if( event.keyDown.keyCode == getAltCode(c) ||
  214.                 ( owner->phase == phPostProcess &&
  215.                   c != 0 &&
  216.                   toupper(event.keyDown.charScan.charCode) == c
  217.                 ) ||
  218.                 ( (state & sfFocused) != 0 &&
  219.                   event.keyDown.charScan.charCode == ' '
  220.                 )
  221.               )
  222.                 {
  223.                 press();
  224.                 clearEvent( event );
  225.                 }
  226.             break;
  227.  
  228.         case evBroadcast:
  229.             switch( event.message.command )
  230.                 {
  231.                 case cmDefault:
  232.                     if( amDefault )
  233.                         {
  234.                         press();
  235.                         clearEvent(event);
  236.                         }
  237.                     break;
  238.  
  239.                 case cmGrabDefault:
  240.                 case cmReleaseDefault:
  241.                     if( (flags & bfDefault) != 0 )
  242.                         {
  243.                         amDefault = Boolean(event.message.command == cmReleaseDefault);
  244.                         drawView();
  245.                         }
  246.                     break;
  247.  
  248.                 case cmCommandSetChanged:
  249.                     setState(sfDisabled,Boolean(!commandEnabled(command)));
  250.                     drawView();
  251.                     break;
  252.                 }
  253.         break;
  254.         }
  255. }
  256.  
  257. void TButton::makeDefault( Boolean enable )
  258. {
  259.     if( (flags & bfDefault) == 0 )
  260.         {
  261.         message( owner,
  262.                  evBroadcast,
  263.                  (enable == True) ? cmGrabDefault : cmReleaseDefault,
  264.                  this
  265.                );
  266.         amDefault = enable;
  267.         drawView();
  268.         }
  269. }
  270.  
  271. void TButton::setState( ushort aState, Boolean enable )
  272. {
  273.     TView::setState(aState, enable);
  274.     if( aState & (sfSelected | sfActive) )
  275.         drawView();
  276.     if( (aState & sfFocused) != 0 )
  277.         makeDefault( enable );
  278. }
  279.  
  280. void TButton::press()
  281. {
  282.     message( owner, evBroadcast, cmRecordHistory, 0 );
  283.     if( (flags & bfBroadcast) != 0 )
  284.         message( owner, evBroadcast, command, this );
  285.     else
  286.         {
  287.         TEvent e;
  288.         e.what = evCommand;
  289.         e.message.command = command;
  290.         e.message.infoPtr = this;
  291.         putEvent( e );
  292.         }
  293. }
  294.  
  295. void TButton::write( opstream& os )
  296. {
  297.     TView::write( os );
  298.     os.writeString( title );
  299.     os << command << flags << (int)amDefault;
  300. }
  301.  
  302. void *TButton::read( ipstream& is )
  303. {
  304.     TView::read( is );
  305.     title = is.readString();
  306.     int temp;
  307.     is >> command >> flags >> temp;
  308.     amDefault = Boolean(temp);
  309.     if( TButton::commandEnabled( command ) )
  310.         state &= ~sfDisabled;
  311.     else
  312.         state |= sfDisabled;
  313.     return this;
  314. }
  315.  
  316. TStreamable *TButton::build()
  317. {
  318.     return new TButton( streamableInit );
  319. }
  320.