home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / TWindow.cpp < prev   
C/C++ Source or Header  |  1998-01-19  |  6KB  |  262 lines

  1. /*
  2.  * TWindow.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_TKeys
  13. #define Uses_TWindow
  14. #define Uses_TEvent
  15. #define Uses_TRect
  16. #define Uses_TFrame
  17. #define Uses_TCommandSet
  18. #define Uses_TScrollBar
  19. #define Uses_opstream
  20. #define Uses_ipstream
  21. #include <tvision/tv.h>
  22.  
  23. #include <string.h>
  24.  
  25. const TPoint minWinSize = {16, 6};
  26.  
  27. TWindowInit::TWindowInit( TFrame *(*cFrame)( TRect ) ) :
  28.     createFrame( cFrame )
  29. {
  30. }
  31.  
  32. TWindow::TWindow( const TRect& bounds,
  33.                   const char *aTitle,
  34.                   short aNumber
  35.                 ) :
  36.     TGroup( bounds ),
  37.     flags( wfMove | wfGrow | wfClose | wfZoom ),
  38.     zoomRect( getBounds() ),
  39.     number( aNumber ),
  40.     palette( wpBlueWindow ),
  41.     title( newStr( aTitle ) ),
  42.     TWindowInit( &TWindow::initFrame )
  43. {
  44.     state |= sfShadow;
  45.     options |= ofSelectable | ofTopSelect;
  46.     growMode = gfGrowAll | gfGrowRel;
  47.  
  48.     if( createFrame != 0 &&
  49.         (frame = createFrame( getExtent() )) != 0
  50.       )
  51.         insert( frame );
  52. }
  53.  
  54. TWindow::~TWindow()
  55. {
  56.     delete (char *)title;
  57. }
  58.  
  59. void TWindow::close()
  60. {
  61.     if( valid( cmClose ) )
  62.     {
  63.         frame = 0;  // so we don't try to use the frame after it's been deleted
  64.         destroy( this );
  65.     }
  66. }
  67.  
  68. void TWindow::shutDown()
  69. {
  70.     frame = 0;
  71.     TGroup::shutDown();
  72. }
  73.  
  74. TPalette& TWindow::getPalette() const
  75. {
  76.     static TPalette blue( cpBlueWindow, sizeof( cpBlueWindow )-1 );
  77.     static TPalette cyan( cpCyanWindow, sizeof( cpCyanWindow )-1 );
  78.     static TPalette gray( cpGrayWindow, sizeof( cpGrayWindow )-1 );
  79.     static TPalette *palettes[] =
  80.         {
  81.         &blue,
  82.         &cyan,
  83.         &gray
  84.         };
  85.     return *(palettes[palette]);
  86. }
  87.  
  88. const char *TWindow::getTitle( short )
  89. {
  90.     return title;
  91. }
  92.  
  93. void TWindow::handleEvent( TEvent& event )
  94. {
  95.  TRect  limits;
  96.  TPoint min, max;
  97.  
  98.     TGroup::handleEvent(event);
  99.     if( event.what== evCommand )
  100.         switch (event.message.command)
  101.             {
  102.             case  cmResize:
  103.                 if( (flags & (wfMove | wfGrow)) != 0 )
  104.                     {
  105.                     limits = owner->getExtent();
  106.                     sizeLimits(min, max);
  107.                     dragView( event, dragMode | (flags & (wfMove | wfGrow)),
  108.                               limits, min, max);
  109.                     clearEvent(event);
  110.                     }
  111.                 break;
  112.             case  cmClose:
  113.                 if( (flags & wfClose) != 0 &&
  114.                     ( event.message.infoPtr == 0 || event.message.infoPtr == this )
  115.                   )
  116.                     {
  117.             clearEvent(event);
  118.                     if( (state & sfModal) == 0 )
  119.                         close();
  120.                     else
  121.                         {
  122.                         event.what = evCommand;
  123.                         event.message.command = cmCancel;
  124.                         putEvent( event );
  125.                         clearEvent( event );
  126.                         }
  127.                     }
  128.                 break;
  129.             case  cmZoom:
  130.                 if( (flags & wfZoom) != 0 &&
  131.                     (event.message.infoPtr == 0 || event.message.infoPtr == this)
  132.                   )
  133.                     {
  134.                     zoom();
  135.                     clearEvent(event);
  136.                     }
  137.                 break;
  138.             }
  139.     else if( event.what == evKeyDown )
  140.             switch (event.keyDown.keyCode)
  141.                 {
  142.                 case  kbTab:
  143.                     focusNext(False);
  144.                     clearEvent(event);
  145.                     break;
  146.                 case  kbShiftTab:
  147.                     focusNext(True);
  148.                     clearEvent(event);
  149.                     break;
  150.                 }
  151.     else if( event.what == evBroadcast &&
  152.              event.message.command == cmSelectWindowNum &&
  153.         /*
  154.          * Some non-portable code changed.  See `TProgram.cc'.
  155.          */
  156.         (int)event.message.infoPtr == number &&
  157. //             event.message.infoInt == number &&
  158.              (options & ofSelectable) != 0
  159.            )
  160.             {
  161.             select();
  162.             clearEvent(event);
  163.             }
  164. }
  165.  
  166. TFrame *TWindow::initFrame( TRect r )
  167. {
  168.     return new TFrame(r);
  169. }
  170.  
  171. void TWindow::setState( ushort aState, Boolean enable )
  172. {
  173.     TCommandSet windowCommands;
  174.  
  175.     TGroup::setState(aState, enable);
  176.     if( (aState & sfSelected) != 0 )
  177.         {
  178.         setState(sfActive, enable);
  179.         if( frame != 0 )
  180.             frame->setState(sfActive,enable);
  181.         windowCommands += cmNext;
  182.         windowCommands += cmPrev;
  183.         if( (flags & (wfGrow | wfMove)) != 0 )
  184.             windowCommands += cmResize;
  185.         if( (flags & wfClose) != 0 )
  186.             windowCommands += cmClose;
  187.         if( (flags & wfZoom) != 0 )
  188.             windowCommands += cmZoom;
  189.         if( enable != False )
  190.             enableCommands(windowCommands);
  191.         else
  192.             disableCommands(windowCommands);
  193.         }
  194. }
  195.  
  196. TScrollBar *TWindow::standardScrollBar( ushort aOptions )
  197. {
  198.     TRect r = getExtent();
  199.     if( (aOptions & sbVertical) != 0 )
  200.         r = TRect( r.b.x-1, r.a.y+1, r.b.x, r.b.y-1 );
  201.     else
  202.         r = TRect( r.a.x+2, r.b.y-1, r.b.x-2, r.b.y );
  203.  
  204.     TScrollBar *s;
  205.     insert( s = new TScrollBar(r) );
  206.     if( (aOptions & sbHandleKeyboard) != 0 )
  207.         s->options |= ofPostProcess;
  208.     return s;
  209. }
  210.  
  211. void TWindow::sizeLimits( TPoint& min, TPoint& max )
  212. {
  213.     TView::sizeLimits(min, max);
  214.     min = minWinSize;
  215. }
  216.  
  217. void TWindow::zoom()
  218. {
  219.     TPoint minSize, maxSize;
  220.     sizeLimits( minSize, maxSize );
  221.     if( size != maxSize )
  222.         {
  223.         zoomRect = getBounds();
  224.         TRect r( 0, 0, maxSize.x, maxSize.y );
  225.         locate(r);
  226.         }
  227.     else
  228.         locate( zoomRect );
  229. }
  230.  
  231. #if !defined(NO_STREAMABLE)
  232.  
  233. void TWindow::write( opstream& os )
  234. {
  235.     TGroup::write( os );
  236.     os << flags << zoomRect << number << palette;
  237.     os << frame;
  238.     os.writeString( title );
  239. }
  240.  
  241. void *TWindow::read( ipstream& is )
  242. {
  243.     TGroup::read( is );
  244.     is >> flags >> zoomRect >> number >> palette;
  245.     is >> frame;
  246.     title = is.readString();
  247.     return this;
  248. }
  249.  
  250. TStreamable *TWindow::build()
  251. {
  252.     return new TWindow( streamableInit );
  253. }
  254.  
  255. TWindow::TWindow( StreamableInit ) :
  256.     TGroup( streamableInit ),
  257.     TWindowInit( 0 /*streamableInit*/ )
  258. {
  259. }
  260.  
  261. #endif
  262.