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

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