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

  1. /*------------------------------------------------------------*/
  2. /* filename -       tview.cpp                                 */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TView 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_TView
  20. #define Uses_TCommandSet
  21. #define Uses_TPoint
  22. #define Uses_TGroup
  23. #define Uses_TRect
  24. #define Uses_TEvent
  25. #define Uses_opstream
  26. #define Uses_ipstream
  27. #include <tv.h>
  28.  
  29. #if !defined( __DOS_H )
  30. #include <Dos.h>
  31. #endif  // __DOS_H
  32.  
  33. #if !defined( __LIMITS_H )
  34. #include <Limits.h>
  35. #endif  // __LIMITS_H
  36.  
  37. TPoint shadowSize = {2,1};
  38. uchar shadowAttr = 0x08;
  39. Boolean near TView::showMarkers = False;
  40. uchar near TView::errorAttr = 0xCF;
  41. Boolean near TView::commandSetChanged = False;
  42.  
  43. extern TView *TheTopView;
  44.  
  45. static TCommandSet initCommands()
  46. {
  47.     TCommandSet temp;
  48.     for( int i = 0; i < 256; i++ )
  49.         temp.enableCmd( i );
  50.     temp.disableCmd( cmZoom );
  51.     temp.disableCmd( cmClose );
  52.     temp.disableCmd( cmResize );
  53.     temp.disableCmd( cmNext );
  54.     temp.disableCmd( cmPrev );
  55.     return temp;
  56. }
  57.  
  58. TCommandSet near TView::curCommandSet = initCommands();
  59.  
  60. TView::TView( const TRect& bounds) :
  61.     owner( 0 ), next( 0 ), options( 0 ), state( sfVisible ),
  62.     growMode( 0 ), dragMode( dmLimitLoY ), helpCtx( hcNoContext ),
  63.     eventMask( evMouseDown | evKeyDown | evCommand )
  64. {
  65.     setBounds( bounds);
  66.     cursor.x = cursor.y = 0;
  67. }
  68.  
  69. TView::~TView()
  70. {
  71. }
  72.  
  73. void TView::blockCursor()
  74. {
  75.      setState(sfCursorIns, True);
  76. }
  77.  
  78. #define grow(i) (( (growMode & gfGrowRel)) ? \
  79.                 (i = (i * s + ((s - d) >> 1)) / (s - d)) : (i += d))
  80.  
  81. inline int range( int val, int min, int max )
  82. {
  83.     if( val < min )
  84.         return min;
  85.     else if( val > max )
  86.         return max;
  87.     else
  88.         return val;
  89. }
  90.                 
  91. void TView::calcBounds( TRect& bounds, TPoint delta )
  92. {
  93.     bounds = getBounds();
  94.  
  95.     short s = owner->size.x;
  96.     short d = delta.x;
  97.  
  98.     if( (growMode & gfGrowLoX) != 0 )
  99.         grow(bounds.a.x);
  100.  
  101.     if( (growMode & gfGrowHiX) != 0 )
  102.         grow(bounds.b.x);
  103.  
  104.     s = owner->size.y;
  105.     d = delta.y;
  106.  
  107.     if( (growMode & gfGrowLoY) != 0 )
  108.         grow(bounds.a.y);
  109.  
  110.     if( (growMode & gfGrowHiY) != 0 )
  111.         grow(bounds.b.y);
  112.  
  113.     TPoint minLim, maxLim;
  114.     sizeLimits( minLim, maxLim );
  115.     bounds.b.x = bounds.a.x + range( bounds.b.x-bounds.a.x, minLim.x, maxLim.x );
  116.     bounds.b.y = bounds.a.y + range( bounds.b.y-bounds.a.y, minLim.y, maxLim.y );
  117. }
  118.  
  119. void TView::changeBounds( const TRect& bounds )
  120. {
  121.     setBounds(bounds);
  122.     drawView();
  123. }
  124.  
  125. void TView::clearEvent( TEvent& event )
  126. {
  127.     event.what = evNothing;
  128.     event.message.infoPtr = this;
  129. }
  130.  
  131. Boolean TView::commandEnabled( ushort command )
  132. {
  133.     return Boolean((command > 255) || curCommandSet.has(command));
  134. }
  135.  
  136. ushort TView::dataSize()
  137. {
  138.     return 0;
  139. }
  140.  
  141. void TView::disableCommands( TCommandSet& commands )
  142. {
  143.     commandSetChanged = Boolean( commandSetChanged ||
  144.                                 !(curCommandSet & commands).isEmpty());
  145.     curCommandSet.disableCmd(commands);
  146. }
  147.  
  148. void TView::disableCommand( ushort command )
  149. {
  150.     commandSetChanged = Boolean( commandSetChanged ||
  151.                                  curCommandSet.has(command) );
  152.     curCommandSet.disableCmd(command);
  153. }
  154.  
  155. void TView::moveGrow( TPoint p,
  156.                       TPoint s,
  157.                       TRect& limits,
  158.                       TPoint minSize,
  159.                       TPoint maxSize,
  160.                       uchar mode
  161.                     )
  162. {
  163.     TRect   r;
  164.     s.x = min(max(s.x, minSize.x), maxSize.x);
  165.     s.y = min(max(s.y, minSize.y), maxSize.y);
  166.     p.x = min(max(p.x, limits.a.x - s.x+1), limits.b.x-1);
  167.     p.y = min(max(p.y, limits.a.y - s.y+1), limits.b.y-1);
  168.  
  169.     if( (mode & dmLimitLoX) != 0 )
  170.         p.x = max(p.x, limits.a.x);
  171.     if( (mode & dmLimitLoY) != 0 )
  172.         p.y = max(p.y, limits.a.y);
  173.     if( (mode & dmLimitHiX) != 0 )
  174.         p.x = min(p.x, limits.b.x-s.x);
  175.     if( (mode & dmLimitHiY) != 0 )
  176.         p.y = min(p.y, limits.b.y-s.y);
  177.     r = TRect(p.x, p.y, p.x +  s.x, p.y +  s.y);
  178.     locate(r);
  179. }
  180.  
  181. void TView::change( uchar mode, TPoint delta, TPoint& p, TPoint& s )
  182. {
  183. # ifdef PROTECT
  184.     unsigned char *shiftState = (unsigned char *) MK_FP(biosSeg,0x17);
  185. # else
  186.     unsigned char *shiftState = (unsigned char *) MK_FP(0x40,0x17);
  187. # endif
  188.  
  189.     if( (mode & dmDragMove) != 0 && (*shiftState & 3) == 0 )
  190.         p += delta;
  191.     else if( (mode & dmDragGrow) != 0 && (*shiftState & 3) != 0 )
  192.         s += delta;
  193. }
  194.  
  195. void TView::dragView( TEvent& event,
  196.                       uchar mode,
  197.                       TRect& limits,
  198.                       TPoint minSize,
  199.                       TPoint maxSize
  200.                     )
  201. {
  202.     TRect saveBounds;
  203.  
  204.     TPoint p, s;
  205.     setState( sfDragging, True );
  206.  
  207.     if( event.what == evMouseDown )
  208.         {
  209.         if( (mode & dmDragMove) != 0 )
  210.             {
  211.             p = origin - event.mouse.where;
  212.             do  {
  213.                 event.mouse.where += p;
  214.                 moveGrow( event.mouse.where,
  215.                           size,
  216.                           limits,
  217.                           minSize,
  218.                           maxSize,
  219.                           mode
  220.                         );
  221.                 } while( mouseEvent(event,evMouseMove) );
  222.             }
  223.         else
  224.             {
  225.             p = size - event.mouse.where;
  226.             do  {
  227.                 event.mouse.where += p;
  228.                 moveGrow( origin,
  229.                           event.mouse.where,
  230.                           limits,
  231.                           minSize,
  232.                           maxSize,
  233.                           mode
  234.                         );
  235.                 } while( mouseEvent(event,evMouseMove) );
  236.             }
  237.         }
  238.     else
  239.         {
  240.         static TPoint 
  241.             goLeft      =   {-1, 0}, 
  242.             goRight     =   { 1, 0}, 
  243.             goUp        =   { 0,-1}, 
  244.             goDown      =   { 0, 1}, 
  245.             goCtrlLeft  =   {-8, 0}, 
  246.             goCtrlRight =   { 8, 0};
  247.             
  248.         saveBounds = getBounds();
  249.         do  {
  250.             p = origin;
  251.             s = size;
  252.             keyEvent(event);
  253.             switch (event.keyDown.keyCode & 0xFF00)
  254.                 {
  255.                 case kbLeft:
  256.                     change(mode,goLeft,p,s);
  257.                     break;
  258.                 case kbRight:
  259.                     change(mode,goRight,p,s);
  260.                     break;
  261.                 case kbUp:
  262.                     change(mode,goUp,p,s);
  263.                     break;
  264.                 case kbDown:
  265.                     change(mode,goDown,p,s);
  266.                     break;
  267.                 case kbCtrlLeft:
  268.                     change(mode,goCtrlLeft,p,s);
  269.                     break;
  270.                 case kbCtrlRight:
  271.                     change(mode,goCtrlRight,p,s);
  272.                     break;
  273.                 case kbHome:
  274.                     p.x = limits.a.x;
  275.                     break;
  276.                 case kbEnd:
  277.                     p.x = limits.b.x - s.x;
  278.                     break;
  279.                 case kbPgUp:
  280.                     p.y = limits.a.y;
  281.                     break;
  282.                 case kbPgDn:
  283.                     p.y = limits.b.y - s.y;
  284.                     break;
  285.                 }
  286.             moveGrow( p, s, limits, minSize, maxSize, mode );
  287.             } while( event.keyDown.keyCode != kbEsc &&
  288.                      event.keyDown.keyCode != kbEnter
  289.                    );
  290.         if( event.keyDown.keyCode == kbEsc )
  291.             locate(saveBounds);
  292.         }
  293.     setState(sfDragging, False);
  294. }
  295.  
  296. void TView::draw()
  297. {
  298.     TDrawBuffer  b;
  299.  
  300.     b.moveChar( 0, ' ', getColor(1), size.x );
  301.     writeLine( 0, 0, size.x, size.y, b );
  302. }
  303.  
  304. void TView::drawCursor()
  305. {
  306.     if( (state & sfFocused) != 0 )
  307.         resetCursor();
  308. }
  309.  
  310. void TView::drawHide( TView* lastView )
  311. {
  312.     drawCursor();
  313.     drawUnderView(Boolean(state & sfShadow), lastView);
  314. }
  315.  
  316. void TView::drawShow( TView* lastView )
  317. {
  318.     drawView();
  319.     if( (state & sfShadow) != 0 )
  320.         drawUnderView( True, lastView );
  321. }
  322.  
  323. void TView::drawUnderRect( TRect& r, TView* lastView )
  324. {
  325.     owner->clip.intersect(r);
  326.     owner->drawSubViews(nextView(), lastView);
  327.     owner->clip = owner->getExtent();
  328. }
  329.  
  330. void TView::drawUnderView( Boolean doShadow, TView* lastView )
  331. {
  332.     TRect r = getBounds();
  333.     if( doShadow != False )
  334.         r.b += shadowSize;
  335.     drawUnderRect( r, lastView );
  336. }
  337.  
  338. void TView::drawView()
  339. {
  340.     if (exposed())
  341.         {
  342.         draw();
  343.         drawCursor();
  344.         }
  345. }
  346.  
  347. void TView::enableCommands( TCommandSet& commands )
  348. {
  349.     commandSetChanged = Boolean( commandSetChanged ||
  350.                                 ((curCommandSet&commands) != commands) );
  351.     curCommandSet += commands;
  352. }
  353.  
  354. void TView::enableCommand( ushort command )
  355. {
  356.     commandSetChanged = Boolean( commandSetChanged ||
  357.                                  !curCommandSet.has( command ) );
  358.     curCommandSet += command;
  359. }
  360.  
  361. void TView::endModal( ushort command )
  362. {
  363.     if( TopView() != 0 )
  364.         TopView()->endModal(command);
  365. }
  366.  
  367. Boolean  TView::eventAvail()
  368. {
  369.     TEvent event;
  370.     getEvent(event);
  371.     if( event.what != evNothing )
  372.         putEvent(event);
  373.     return Boolean( event.what != evNothing );
  374. }
  375.  
  376. TRect TView::getBounds()
  377. {
  378.     return TRect( origin, origin+size );
  379. }
  380.  
  381. ushort  TView::execute()
  382. {
  383.     return cmCancel;
  384. }
  385.  
  386. TRect TView::getClipRect()
  387. {
  388.     TRect clip = getBounds();
  389.     if( owner != 0 )
  390.         clip.intersect(owner->clip);
  391.     clip.move(-origin.x, -origin.y);
  392.     return clip;
  393. }
  394.  
  395. ushort TView::getColor( ushort color )
  396. {
  397.     ushort colorPair = color >> 8;
  398.  
  399.     if( colorPair != 0 )
  400.         colorPair = mapColor(colorPair) << 8;
  401.  
  402.     colorPair |= mapColor( uchar(color) );
  403.  
  404.     return colorPair;
  405. }
  406.  
  407. void TView::getCommands( TCommandSet& commands )
  408. {
  409.     commands = curCommandSet;
  410. }
  411.  
  412. void TView::getData( void * )
  413. {
  414. }
  415.  
  416. void TView::getEvent( TEvent& event )
  417. {
  418.     if( owner != 0 )
  419.         owner->getEvent(event);
  420. }
  421.  
  422. TRect TView::getExtent()
  423. {
  424.     return TRect( 0, 0, size.x, size.y );
  425. }
  426.  
  427. ushort TView::getHelpCtx()
  428. {
  429.     if( (state & sfDragging) != 0 )
  430.         return hcDragging;
  431.     return helpCtx;
  432. }
  433.  
  434. TPalette& TView::getPalette() const
  435. {
  436.     static char ch = 0;
  437.     static TPalette palette( &ch, 0 );
  438.     return palette;
  439. }
  440.  
  441. Boolean TView::getState( ushort aState )
  442. {
  443.     return Boolean( (state & aState) == aState );
  444. }
  445.  
  446. void TView::growTo( short x, short y )
  447. {
  448.     TRect r = TRect(origin.x, origin.y, origin.x + x, origin.y + y);
  449.     locate(r);
  450. }
  451.  
  452. void TView::handleEvent(TEvent& event)
  453. {
  454.     if( event.what == evMouseDown )
  455.         {
  456.         if(!(state & (sfSelected | sfDisabled)) && (options & ofSelectable) )
  457.             {
  458.             select();
  459.             if( !(options & ofFirstClick) )
  460.                 clearEvent(event);
  461.             }
  462.         }
  463. }
  464.  
  465. void TView::hide()
  466. {
  467.     if( (state & sfVisible) != 0 )
  468.         setState( sfVisible, False );
  469. }
  470.  
  471. void TView::hideCursor()
  472. {
  473.     setState( sfCursorVis, False );
  474. }
  475.  
  476. void TView::keyEvent( TEvent& event )
  477. {
  478.     do {
  479.        getEvent(event);
  480.         } while( event.what != evKeyDown );
  481. }
  482.  
  483. #define range(Val, Min, Max)    (((Val < Min) ? Min : ((Val > Max) ? Max : Val)))
  484.  
  485. void TView::locate( TRect& bounds )
  486. {
  487.     TPoint   min, max;
  488.     sizeLimits(min, max);
  489.     bounds.b.x = bounds.a.x + range(bounds.b.x - bounds.a.x, min.x, max.x);
  490.     bounds.b.y = bounds.a.y + range(bounds.b.y - bounds.a.y, min.y, max.y);
  491.     TRect r = getBounds();
  492.     if( bounds != r )
  493.         {
  494.         changeBounds( bounds );
  495.         if( owner != 0 && (state & sfVisible) != 0 )
  496.             {
  497.             if( (state & sfShadow) != 0 )
  498.                 {
  499.                 r.Union(bounds);
  500.                 r.b += shadowSize;
  501.                 }
  502.             drawUnderRect( r, 0 );
  503.             }
  504.         }
  505. }
  506.  
  507. void TView::makeFirst()
  508. {
  509.     putInFrontOf(owner->first());
  510. }
  511.  
  512. TPoint TView::makeGlobal( TPoint source )
  513. {
  514.     TPoint temp = source + origin;
  515.     TView *cur = this;
  516.     while( cur->owner != 0 )
  517.         {
  518.         cur = cur->owner;
  519.         temp += cur->origin;
  520.         }
  521.     return temp;
  522. }
  523.  
  524. TPoint TView::makeLocal( TPoint source )
  525. {
  526.     TPoint temp = source - origin;
  527.     TView* cur = this;
  528.     while( cur->owner != 0 )
  529.         {
  530.         cur = cur->owner;
  531.         temp -= cur->origin;
  532.         }
  533.     return temp;
  534. }
  535.  
  536. Boolean TView::mouseEvent(TEvent& event, ushort mask)
  537. {
  538.     do {
  539.        getEvent(event);
  540.         } while( !(event.what & (mask | evMouseUp)) );
  541.  
  542.     return Boolean(event.what != evMouseUp);
  543. }
  544.  
  545. Boolean TView::mouseInView(TPoint mouse)
  546. {
  547.      mouse = makeLocal( mouse );
  548.      TRect r = getExtent();
  549.      return r.contains(mouse);
  550. }
  551.  
  552. void TView::moveTo( short x, short y )
  553. {
  554.      TRect r( x, y, x+size.x, y+size.y );
  555.      locate(r);
  556. }
  557.  
  558. TView *TView::nextView()
  559. {
  560.     if( this == owner->last )
  561.         return 0;
  562.     else
  563.         return next;
  564. }
  565.  
  566. void TView::normalCursor()
  567. {
  568.     setState(sfCursorIns, False);
  569. }
  570.  
  571. TView *TView::prev()
  572. {
  573.     TView* res = this;
  574.     while( res->next != this )
  575.         res = res->next;
  576.     return res;
  577. }
  578.  
  579. TView *TView::prevView()
  580. {
  581.     if( this == owner->first() )
  582.         return 0;
  583.     else
  584.         return prev();
  585. }
  586.  
  587. void TView::putEvent( TEvent& event )
  588. {
  589.     if( owner != 0 )
  590.         owner->putEvent(event);
  591. }
  592.  
  593. void TView::putInFrontOf( TView *Target )
  594. {
  595.     TView *p, *lastView;
  596.  
  597.     if( owner != 0 && Target != this && Target != nextView() &&
  598.          ( Target == 0 || Target->owner == owner)
  599.       )
  600.         if( (state & sfVisible) == 0 )
  601.             {
  602.             owner->removeView(this);
  603.             owner->insertView(this, Target);
  604.             }
  605.         else
  606.             {
  607.             lastView = nextView();
  608.             p = Target;
  609.             while( p != 0 && p != this )
  610.                 p = p->nextView();
  611.             if( p == 0 )
  612.                 lastView = Target;
  613.             state &= ~sfVisible;
  614.             if( lastView == Target )
  615.                 drawHide(lastView);
  616.             owner->removeView(this);
  617.             owner->insertView(this, Target);
  618.             state |= sfVisible;
  619.             if( lastView != Target )
  620.                 drawShow(lastView);
  621.             if( (options & ofSelectable) != 0 )
  622.                 owner->resetCurrent();
  623.             }
  624. }
  625.  
  626. void TView::select()
  627. {
  628.     if( (options & ofTopSelect) != 0 )
  629.         makeFirst();
  630.     else if( owner != 0 )
  631.         owner->setCurrent( this, normalSelect );
  632. }
  633.  
  634. void TView::setBounds( const TRect& bounds )
  635. {            
  636.     origin = bounds.a;
  637.     size = bounds.b - bounds.a;
  638. }
  639.  
  640. void TView::setCommands( TCommandSet& commands )
  641. {
  642.     commandSetChanged = Boolean( commandSetChanged ||
  643.                                 (curCommandSet != commands ));
  644.     curCommandSet = commands;
  645. }
  646.  
  647. void TView::setCursor( short x, short y )
  648. {
  649.     cursor.x = x;
  650.     cursor.y = y;
  651.     drawCursor();
  652. }
  653.  
  654. void TView::setData( void * )
  655. {
  656. }
  657.  
  658. void TView::setState( ushort aState, Boolean enable )
  659. {
  660.     if( enable == True )
  661.         state |= aState;
  662.     else
  663.         state &= ~aState;
  664.  
  665.     if( owner == 0 )
  666.         return;
  667.  
  668.     switch( aState )
  669.         {
  670.         case  sfVisible:
  671.             if( (owner->state & sfExposed) != 0 )
  672.                 setState( sfExposed, enable );
  673.             if( enable == True )
  674.                 drawShow( 0 );
  675.             else
  676.                 drawHide( 0 );
  677.             if( (options & ofSelectable) != 0 )
  678.                 owner->resetCurrent();
  679.             break;
  680.         case  sfCursorVis:
  681.         case  sfCursorIns:
  682.             drawCursor();
  683.             break;
  684.         case  sfShadow:
  685.             drawUnderView( True, 0 );
  686.             break;
  687.         case  sfFocused:
  688.             resetCursor();
  689.             message( owner,
  690.                      evBroadcast,
  691.                      (enable == True) ? cmReceivedFocus : cmReleasedFocus,
  692.                      this
  693.                    );
  694.             break;
  695.         }
  696. }
  697.  
  698. void TView::show()
  699. {
  700.     if( (state & sfVisible) == 0 )
  701.         setState(sfVisible, True);
  702. }
  703.  
  704. void TView::showCursor()
  705. {
  706.     setState( sfCursorVis, True );
  707. }
  708.  
  709. void TView::sizeLimits( TPoint& min, TPoint& max )
  710. {
  711.     min.x = min.y = 0;
  712.     if( owner != 0 )
  713.         max = owner->size;
  714.     else
  715.         max.x = max.y = INT_MAX;
  716. }
  717.  
  718. TView* TView::TopView()
  719. {
  720.     if( TheTopView != 0 )
  721.         return TheTopView;
  722.     else
  723.         {
  724.         TView* p = this;
  725.         while( p != 0 && !(p->state & sfModal) )
  726.             p = p->owner;
  727.         return p;
  728.         }
  729. }
  730.  
  731. Boolean TView::valid( ushort )
  732. {
  733.     return True;
  734. }
  735.  
  736. Boolean TView::containsMouse( TEvent& event )
  737. {
  738.     return Boolean( (state & sfVisible) != 0 &&
  739.                     mouseInView( event.mouse.where )
  740.                   );
  741. }
  742.  
  743. void TView::shutDown()
  744. {
  745.     hide();
  746.     if( owner != 0 )
  747.         owner->remove( this );
  748.     TObject::shutDown();
  749. }
  750.  
  751. void TView::write( opstream& os )
  752. {
  753.     ushort saveState =
  754.         state & ~( sfActive | sfSelected | sfFocused | sfExposed );
  755.  
  756.     os << origin << size << cursor
  757.        << growMode << dragMode << helpCtx
  758.        << saveState << options << eventMask;
  759. }
  760.  
  761. void *TView::read( ipstream& is )
  762. {
  763.     is >> origin >> size >> cursor
  764.        >> growMode >> dragMode >> helpCtx
  765.        >> state >> options >> eventMask;
  766.     owner = 0;
  767.     next = 0;
  768.     return this;
  769. }
  770.  
  771. TStreamable *TView::build()
  772. {
  773.     return new TView( streamableInit );
  774. }
  775.  
  776. TView::TView( StreamableInit )
  777. {
  778. }
  779.