home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / TButton.cpp < prev    next >
C/C++ Source or Header  |  1999-05-26  |  8KB  |  321 lines

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