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

  1. /*------------------------------------------------------------*/
  2. /* filename -       tcluster.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TCluster 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_TCluster
  20. #define Uses_TDrawBuffer
  21. #define Uses_TEvent
  22. #define Uses_TPoint
  23. #define Uses_TSItem
  24. #define Uses_TStringCollection
  25. #define Uses_TGroup
  26. #define Uses_opstream
  27. #define Uses_ipstream
  28. #include <tv.h>
  29.  
  30. #if !defined( __CTYPE_H )
  31. #include <ctype.h>
  32. #endif  // __CTYPE_H
  33.  
  34. #if !defined( __STRING_H )
  35. #include <String.h>
  36. #endif  // __STRING_H
  37.  
  38. #if !defined( __DOS_H )
  39. #include <Dos.h>
  40. #endif  // __DOS_H
  41.  
  42. #define cpCluster "\x10\x11\x12\x12"
  43.  
  44. TCluster::TCluster( const TRect& bounds, TSItem *aStrings ) :
  45.     TView(bounds),
  46.     value( 0 ),
  47.     sel( 0 )
  48. {
  49.     options |= ofSelectable | ofFirstClick | ofPreProcess | ofPostProcess;
  50.     short i = 0;
  51.     TSItem *p;
  52.     for( p = aStrings; p != 0; p = p->next )
  53.         i++;
  54.  
  55.     strings = new TStringCollection( i, 0 );
  56.  
  57.     while( aStrings != 0 )
  58.         {
  59.         p = aStrings;
  60.         strings->atInsert( strings->getCount(), newStr(aStrings->value) );
  61.         aStrings = aStrings->next;
  62.         delete p;
  63.         }
  64.  
  65.     setCursor( 2, 0 );
  66.     showCursor();
  67. }
  68.  
  69. TCluster::~TCluster()
  70. {
  71.     destroy( (TCollection *)strings );
  72. }
  73.  
  74. ushort  TCluster::dataSize()
  75. {
  76.     return sizeof(value);
  77. }
  78.  
  79. void TCluster::drawBox( const char *icon, char marker)
  80. {
  81.     TDrawBuffer b;
  82.     ushort color;
  83.  
  84.     ushort cNorm = getColor( 0x0301 );
  85.     ushort cSel = getColor( 0x0402 );
  86.     for( int i = 0; i <= size.y; i++ )
  87.         {
  88.         for( int j = 0; j <= (strings->getCount()-1)/size.y + 1; j++ )
  89.             {
  90.             int cur = j * size.y + i;
  91.             if( cur < strings->getCount() )
  92.                 {
  93.                 int col = column( cur );
  94.                 if( (cur == sel) && (state & sfSelected) != 0 )
  95.                     color = cSel;
  96.                 else
  97.                     color = cNorm;
  98.                 b.moveChar( col, ' ', color, size.x - col );
  99.                 b.moveCStr( col, icon, color );
  100.                 if( mark(cur) )
  101.                     b.putChar( col+2, marker );
  102.                 b.moveCStr( col+5, (char *)(strings->at(cur)), color );
  103.                 if( showMarkers && (state & sfSelected) != 0 && cur == sel )
  104.                     {
  105.                     b.putChar( col, specialChars[0] );
  106.                     b.putChar( column(cur+size.y)-1, specialChars[1] );
  107.                     }
  108.                 }
  109.             }
  110.         writeBuf( 0, i, size.x, 1, b );
  111.         }
  112.     setCursor( column(sel)+2, row(sel) );
  113. }
  114.  
  115. void TCluster::getData(void * rec)
  116. {
  117.     *(ushort*)rec = value;
  118.     drawView();
  119. }
  120.  
  121. ushort TCluster::getHelpCtx()
  122. {
  123.     if( helpCtx == hcNoContext )
  124.         return hcNoContext;
  125.     else
  126.         return helpCtx + sel;
  127. }
  128.  
  129. TPalette& TCluster::getPalette() const
  130. {
  131.     static TPalette palette( cpCluster, sizeof( cpCluster )-1 );
  132.     return palette;
  133. }
  134.  
  135. void TCluster::handleEvent( TEvent& event )
  136. {
  137.     TView::handleEvent(event);
  138.     if( event.what == evMouseDown )
  139.         {
  140.         TPoint mouse = makeLocal( event.mouse.where );
  141.         int i = findSel(mouse);
  142.         if( i != -1 )
  143.             sel = i;
  144.         drawView();
  145.         do  {
  146.             mouse = makeLocal( event.mouse.where );
  147.             if( findSel(mouse) == sel )
  148.                 showCursor();
  149.             else
  150.                 hideCursor();
  151.             } while( mouseEvent(event,evMouseMove) );
  152.         showCursor();
  153.         mouse = makeLocal( event.mouse.where );
  154.         if( findSel(mouse) == sel )
  155.             {
  156.             press(sel);
  157.             drawView();
  158.             }
  159.         clearEvent(event);
  160.         }
  161.     else if( event.what == evKeyDown )
  162.         switch (ctrlToArrow(event.keyDown.keyCode))
  163.             {
  164.             case kbUp:
  165.                 if( (state & sfFocused) != 0 )
  166.                     {
  167.                     if( --sel < 0 )
  168.                         sel = strings->getCount()-1;
  169.                     movedTo(sel);
  170.                     drawView();
  171.                     clearEvent(event);
  172.                     }
  173.                 break;
  174.  
  175.             case kbDown:
  176.                 if( (state & sfFocused) != 0 )
  177.                     {
  178.                     if( ++sel >= strings->getCount() )
  179.                         sel = 0;
  180.                     movedTo(sel);
  181.                     drawView();
  182.                     clearEvent(event);
  183.                     }
  184.                 break;
  185.             case kbRight:
  186.                 if( (state & sfFocused) != 0 )
  187.                     {
  188.                     sel += size.y;
  189.                     if( sel >= strings->getCount() )
  190.                         {
  191.                         sel = (sel +  1) % size.y;
  192.                         if( sel >= strings->getCount() )
  193.                             sel =  0;
  194.                         }
  195.                     movedTo(sel);
  196.                     drawView();
  197.                     clearEvent(event);
  198.                     }
  199.                 break;
  200.             case kbLeft:
  201.                 if( (state & sfFocused) != 0 )
  202.                     {
  203.                     if( sel > 0 )
  204.                         {
  205.                         sel -= size.y;
  206.                         if( sel < 0 )
  207.                             {
  208.                             sel = ((strings->getCount()+size.y-1) /size.y)*size.y + sel - 1;
  209.                             if( sel >= strings->getCount() )
  210.                                 sel = strings->getCount()-1;
  211.                             }
  212.                         }
  213.                     else
  214.                         sel = strings->getCount()-1;
  215.                     movedTo(sel);
  216.                     drawView();
  217.                     clearEvent(event);
  218.                     }
  219.                 break;
  220.             default:
  221.                 for( int i = 0; i < strings->getCount(); i++ )
  222.                     {
  223.                     char c = hotKey( (char *)(strings->at(i)) );
  224.                     if( getAltCode(c) == event.keyDown.keyCode ||
  225.                         ( ( owner->phase == phPostProcess ||
  226.                             (state & sfFocused) != 0
  227.                           ) &&
  228.                           c != 0 &&
  229.                           toupper(event.keyDown.charScan.charCode) == c
  230.                         )
  231.                       )
  232.                         {
  233.                         select();
  234.                         sel =  i;
  235.                         movedTo(sel);
  236.                         press(sel);
  237.                         drawView();
  238.                         clearEvent(event);
  239.                         return;
  240.                         }
  241.                     }
  242.                 if( event.keyDown.charScan.charCode == ' ' &&
  243.                     (state & sfFocused) != 0
  244.                   )
  245.                     {
  246.                     press(sel);
  247.                     drawView();
  248.                     clearEvent(event);
  249.                     }
  250.             }
  251. }
  252.  
  253. void TCluster::setData(void * rec)
  254. {
  255.     value =  *(ushort *)rec;
  256.     drawView();
  257. }
  258.  
  259. void TCluster::setState( ushort aState, Boolean enable )
  260. {
  261.     TView::setState( aState, enable );
  262.     if( aState == sfSelected )
  263.         drawView();
  264. }
  265.  
  266. Boolean TCluster::mark( int )
  267. {
  268.     return False;
  269. }
  270.  
  271. void TCluster::movedTo( int )
  272. {
  273. }
  274.  
  275. void TCluster::press( int )
  276. {
  277. }
  278.  
  279. int TCluster::column( int item )
  280. {
  281.     if( item < size.y )
  282.         return 0;
  283.     else
  284.         {
  285.         int width = 0;
  286.         int col = -6;
  287.         int l = 0;
  288.         for( int i = 0; i <= item; i++ )
  289.             {
  290.             if( i % size.y == 0 )
  291.                 {
  292.                 col += width + 6;
  293.                 width = 0;
  294.                 }
  295.  
  296.             if( i < strings->getCount() )
  297.                 l = cstrlen( (char *)(strings->at(i)) );
  298.             if( l > width )
  299.                 width = l;
  300.             }
  301.         return col;
  302.         }
  303. }
  304.  
  305. int TCluster::findSel( TPoint p )
  306. {
  307.     TRect r = getExtent();
  308.     if( !r.contains(p) )
  309.         return -1;
  310.     else
  311.         {
  312.         int i = 0;
  313.         while( p.x >= column( i + size.y ) )
  314.             i += size.y;
  315.         int s = i + p.y;
  316.         if( s >= strings->getCount() )
  317.             return -1;
  318.         else
  319.             return s;
  320.         }
  321. }
  322.  
  323. int TCluster::row( int item )
  324. {
  325.     return item % size.y;
  326. }
  327.  
  328. void TCluster::write( opstream& os )
  329. {
  330.     TView::write( os );
  331.     os << value << sel << strings;
  332. }
  333.  
  334. void *TCluster::read( ipstream& is )
  335. {
  336.     TView::read( is );
  337.     is >> value >> sel >> strings;
  338.     setCursor( 2, 0 );
  339.     showCursor();
  340.     return this;
  341. }
  342.  
  343. TStreamable *TCluster::build()
  344. {
  345.     return new TCluster( streamableInit );
  346. }
  347.  
  348. TCluster::TCluster( StreamableInit ) : TView( streamableInit )
  349. {
  350. }
  351.  
  352.  
  353.