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

  1. /*------------------------------------------------------------*/
  2. /* filename -       colorsel.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  Member function(s) of following classes   */
  6. /*                      TColorSelector                        */
  7. /*                      TMonoSelector                         */
  8. /*                      TColorDisplay                         */
  9. /*                      TColorItem                            */
  10. /*                      TColorItemList                        */
  11. /*                      TColorGroup                           */
  12. /*                      TColorGroupList                       */
  13. /*                      TColorDialog                          */
  14. /*------------------------------------------------------------*/
  15.  
  16. /*------------------------------------------------------------*/
  17. /*                                                            */
  18. /*    Turbo Vision -  Version 1.0                             */
  19. /*                                                            */
  20. /*                                                            */
  21. /*    Copyright (c) 1991 by Borland International             */
  22. /*    All Rights Reserved.                                    */
  23. /*                                                            */
  24. /*------------------------------------------------------------*/
  25.  
  26. #define Uses_TKeys
  27. #define Uses_TColorSelector
  28. #define Uses_TMonoSelector
  29. #define Uses_TColorDisplay
  30. #define Uses_TColorItem
  31. #define Uses_TColorItemList
  32. #define Uses_TColorGroup
  33. #define Uses_TColorGroupList
  34. #define Uses_TColorDialog
  35. #define Uses_TEvent
  36. #define Uses_TDrawBuffer
  37. #define Uses_TGroup
  38. #define Uses_TSItem
  39. #define Uses_TScrollBar
  40. #define Uses_TLabel
  41. #define Uses_TButton
  42. #define Uses_TRect
  43. #define Uses_opstream
  44. #define Uses_ipstream
  45. #include <tv.h>
  46.  
  47. #if !defined( __STRING_H )
  48. #include <STring.h>
  49. #endif  // __STRING_H
  50.  
  51. TColorItem::TColorItem( const char *nm, uchar idx, TColorItem *nxt )
  52. {
  53.     index = idx;
  54.     next = nxt;
  55.     name = newStr( nm );
  56. }
  57.  
  58. TColorItem::~TColorItem()
  59. {
  60.     delete (char *)name;
  61. }
  62.  
  63. TColorGroup::TColorGroup( const char *nm, TColorItem *itm, TColorGroup *nxt )
  64. {
  65.     items = itm;
  66.     next = nxt;
  67.     name = newStr( nm );
  68. }
  69.  
  70. TColorGroup::~TColorGroup()
  71.     delete (char *)name;
  72. }
  73.  
  74. TColorItem& operator + ( TColorItem& i1, TColorItem& i2 )
  75. {
  76.     TColorItem *cur = &i1;
  77.     while( cur->next != 0 )
  78.         cur = cur->next;
  79.     cur->next = &i2;
  80.     return i1;
  81. }
  82.  
  83. TColorGroup& operator + ( TColorGroup& g, TColorItem& i )
  84. {
  85.     TColorGroup *grp = &g;
  86.     while( grp->next != 0 )
  87.         grp = grp->next;
  88.  
  89.     if( grp->items == 0 )
  90.         grp->items = &i;
  91.     else
  92.         {
  93.         TColorItem *cur = grp->items;
  94.         while( cur->next != 0 )
  95.             cur = cur->next;
  96.         cur->next = &i;
  97.         }
  98.     return g;
  99. }
  100.  
  101. TColorGroup& operator + ( TColorGroup& g1, TColorGroup& g2 )
  102. {
  103.     TColorGroup *cur = &g1;
  104.     while( cur->next != 0 )
  105.         cur = cur->next;
  106.     cur->next = &g2;
  107.     return g1;
  108. }
  109.  
  110.  
  111. TColorSelector::TColorSelector( const TRect& bounds, ColorSel aSelType ) :
  112.     TView( bounds )
  113. {
  114.     options |= ofSelectable | ofFirstClick | ofFramed;
  115.     eventMask |= evBroadcast;
  116.     selType = aSelType;
  117.     color = 0;
  118. }
  119.  
  120. void TColorSelector::draw()
  121. {
  122.     TDrawBuffer b;
  123.     b.moveChar( 0, ' ', 0x70, size.x );
  124.     for(int i = 0; i <= size.y; i++ )
  125.         {
  126.         if( i < 4 )
  127.             {
  128.             for( int j = 0; j < 4; j++ )
  129.                 {
  130.                 int c = i*4+j;
  131.                 b.moveChar( j*3, icon, c, 3 );
  132.                 if( c == color )
  133.                     {
  134.                     b.putChar( j*3+1, 8 );
  135.                     if( c == 0 )
  136.                         b.putAttribute( j*3+1, 0x70 );
  137.                     }
  138.                 }
  139.             }
  140.         writeLine( 0, i, size.x, 1, b );
  141.         }
  142. }
  143.  
  144. void TColorSelector::colorChanged()
  145. {
  146.     int msg;
  147.     if( selType == csForeground )
  148.         msg = cmColorForegroundChanged;
  149.     else
  150.         msg = cmColorBackgroundChanged;
  151.     message( owner, evBroadcast, msg, (void *)color );
  152. }
  153.  
  154. void TColorSelector::handleEvent( TEvent& event )
  155. {
  156.     const width = 4;
  157.  
  158.     TView::handleEvent( event );
  159.  
  160.     switch( event.what )
  161.         {
  162.  
  163.         case evMouseDown:
  164.             uchar oldColor = color;
  165.             do  {
  166.                 if( mouseInView( event.mouse.where ) )
  167.                     {
  168.                     TPoint mouse = makeLocal( event.mouse.where );
  169.                     color = mouse.y*4 + mouse.x/3;
  170.                     }
  171.                 else
  172.                     color = oldColor;
  173.                 colorChanged();
  174.                 drawView();
  175.                 } while( mouseEvent( event, evMouseMove ) );
  176.             break;
  177.  
  178.         case evKeyDown:
  179.             int maxCol = (selType == csBackground) ? 7 : 15;
  180.  
  181.             switch( ctrlToArrow( event.keyDown.keyCode ) )
  182.                 {
  183.                 case kbLeft:
  184.                     if( color > 0 )
  185.                         color--;
  186.                     else
  187.                         color = maxCol;
  188.                     break;
  189.  
  190.                 case kbRight:
  191.                     if( color < maxCol )
  192.                         color++;
  193.                     else
  194.                         color = 0;
  195.                     break;
  196.  
  197.                 case kbUp:
  198.                     if( color > width-1 )
  199.                         color -= width;
  200.                     else if( color == 0 )
  201.                         color = maxCol;
  202.                     else
  203.                         color += maxCol - width;
  204.                     break;
  205.  
  206.                 case kbDown:
  207.                     if( color < maxCol - (width-1) )
  208.                         color += width;
  209.                     else if( color == maxCol )
  210.                         color = 0;
  211.                     else
  212.                         color -= maxCol - width;
  213.                     break;
  214.  
  215.                 default:
  216.                     return;
  217.                 }
  218.             break;
  219.  
  220.         case evBroadcast:
  221.             if( event.message.command == cmColorSet )
  222.                 {
  223.                 if( selType == csBackground )
  224.                     color = event.message.infoByte >> 4;
  225.                 else
  226.                     color = event.message.infoByte & 0x0F;
  227.                 drawView();
  228.                 return ;
  229.                 }
  230.             else
  231.                 return;
  232.         default:
  233.             return ;
  234.         }
  235.     drawView();
  236.     colorChanged();
  237.     clearEvent( event );
  238. }
  239.  
  240. void TColorSelector::write( opstream& os )
  241. {
  242.     TView::write( os );
  243.     os << color << (int)selType;
  244. }
  245.  
  246. void *TColorSelector::read( ipstream& is )
  247. {
  248.     int temp;
  249.     TView::read( is );
  250.     is >> color >> temp;
  251.     selType = ColorSel(temp);
  252.     return this;
  253. }
  254.  
  255. TStreamable *TColorSelector::build()
  256. {
  257.     return new TColorSelector( streamableInit );
  258. }
  259.  
  260. TColorSelector::TColorSelector( StreamableInit ) : TView( streamableInit )
  261. {
  262. }
  263.  
  264. const uchar monoColors[] = { 0x07, 0x0F, 0x01, 0x70, 0x09 };
  265.  
  266. TMonoSelector::TMonoSelector( const TRect& bounds ) :
  267.     TCluster( bounds, new TSItem( normal,
  268.                       new TSItem( highlight,
  269.                       new TSItem( underline,
  270.                       new TSItem( inverse,  0 )))))
  271. {
  272.     eventMask |= evBroadcast;
  273. }
  274.  
  275. void TMonoSelector::draw()
  276. {
  277.     drawBox( button, 0x07 );
  278. }
  279.  
  280. void TMonoSelector::handleEvent( TEvent& event )
  281. {
  282.     TCluster::handleEvent( event );
  283.     if( event.what == evBroadcast && event.message.command == cmColorSet )
  284.         {
  285.         value = event.message.infoByte;
  286.         drawView();
  287.         }
  288. }
  289.  
  290. Boolean TMonoSelector::mark( int item )
  291. {
  292.     return Boolean(monoColors[item] == value);
  293. }
  294.  
  295. void TMonoSelector::newColor()
  296. {
  297.     message( owner, evBroadcast, cmColorForegroundChanged,
  298.         (void *)(value & 0x0F) );
  299.     message( owner, evBroadcast, cmColorBackgroundChanged,
  300.         (void *)((value >> 4) & 0x0F));
  301. }
  302.  
  303. void TMonoSelector::press( int item )
  304. {
  305.     value = monoColors[item];
  306.     newColor();
  307. }
  308.  
  309. void TMonoSelector::movedTo( int item )
  310. {
  311.     value = monoColors[item];
  312.     newColor();
  313. }
  314.  
  315. TStreamable *TMonoSelector::build()
  316. {
  317.     return new TMonoSelector( streamableInit );
  318. }
  319.  
  320. TMonoSelector::TMonoSelector( StreamableInit ) : TCluster( streamableInit )
  321. {
  322. }
  323.  
  324. TColorDisplay::TColorDisplay( const TRect& bounds, const char *aText ) :
  325.     TView( bounds ),
  326.     text( newStr( aText ) ),
  327.     color( 0 )
  328. {
  329.   eventMask |= evBroadcast;
  330. }
  331.  
  332. TColorDisplay::~TColorDisplay()
  333. {
  334.     delete (char *)text;
  335. }
  336.  
  337. void TColorDisplay::draw()
  338. {
  339.     uchar c = *color;
  340.     if( c == 0 )
  341.         c = errorAttr;
  342.     const int len = strlen( text );
  343.     TDrawBuffer b;
  344.     for( int i = 0; i <= size.x/len; i++ )
  345.         b.moveStr( i*len, text, c );
  346.     writeLine( 0, 0, size.x, size.y, b );
  347. }
  348.  
  349. void TColorDisplay::handleEvent( TEvent& event )
  350. {
  351.     TView::handleEvent( event );
  352.     if( event.what == evBroadcast )
  353.         switch( event.message.command )
  354.             {
  355.             case cmColorBackgroundChanged:
  356.                 *color = (*color & 0x0F) | ((event.message.infoByte << 4) & 0xF0);
  357.                 drawView();
  358.                 break;
  359.  
  360.             case cmColorForegroundChanged:
  361.                 *color = (*color & 0xF0) | (event.message.infoByte & 0x0F);
  362.                 drawView();
  363.             }
  364. }
  365.  
  366. void TColorDisplay::setColor( uchar *aColor )
  367. {
  368.     color = aColor;
  369.     message( owner, evBroadcast, cmColorSet, (void *)(*color) );
  370.     drawView();
  371. }
  372.  
  373. void TColorDisplay::write( opstream& os )
  374. {
  375.     TView::write( os );
  376.     os.writeString( text );
  377. }
  378.  
  379. void *TColorDisplay::read( ipstream& is )
  380. {
  381.     TView::read( is );
  382.     text = is.readString();
  383.     color = 0;
  384.     return this;
  385. }
  386.  
  387. TStreamable *TColorDisplay::build()
  388. {
  389.     return new TColorDisplay( streamableInit );
  390. }
  391.  
  392. TColorDisplay::TColorDisplay( StreamableInit ) : TView( streamableInit )
  393. {
  394. }
  395.  
  396. TColorGroupList::TColorGroupList( const TRect& bounds,
  397.                                   TScrollBar *aScrollBar,
  398.                                   TColorGroup *aGroups
  399.                                 ) :
  400.     TListViewer( bounds, 1, 0, aScrollBar ),
  401.     groups( aGroups )
  402. {
  403.     int i = 0;
  404.     while( aGroups != 0 )
  405.         {
  406.         aGroups = aGroups->next;
  407.         i++;
  408.         }
  409.     setRange(i);
  410. }
  411.  
  412. static void freeItems( TColorItem *curItem )
  413. {
  414.     while( curItem != 0 )
  415.         {
  416.         TColorItem *p = curItem;
  417.         curItem = curItem->next;
  418.         delete p;
  419.         }
  420. }
  421.  
  422. static void freeGroups( TColorGroup *curGroup )
  423. {
  424.     while( curGroup != 0 )
  425.         {
  426.         TColorGroup *p = curGroup;
  427.         freeItems( curGroup->items );
  428.         curGroup = curGroup->next;
  429.         delete p;
  430.         }
  431. }
  432.  
  433. TColorGroupList::~TColorGroupList()
  434. {
  435.     freeGroups( groups );
  436. }
  437.  
  438. void TColorGroupList::focusItem( short item )
  439. {
  440.     TListViewer::focusItem( item );
  441.     TColorGroup *curGroup = groups;
  442.     while( item-- > 0 )
  443.         curGroup = curGroup->next;
  444.     message( owner, evBroadcast, cmNewColorItem, curGroup->items);
  445. }
  446.  
  447. void TColorGroupList::getText( char *dest, short item, short maxChars )
  448. {
  449.     TColorGroup *curGroup = groups;
  450.     while( item-- > 0 )
  451.         curGroup = curGroup->next;
  452.     strncpy( dest, curGroup->name, maxChars );
  453.     dest[maxChars] = '\0';
  454. }
  455.  
  456. void TColorGroupList::writeItems( opstream& os, TColorItem *items )
  457. {
  458.     int count = 0;
  459.     TColorItem *cur;
  460.  
  461.     for( cur = items; cur != 0; cur = cur->next )
  462.         count++;
  463.  
  464.     os << count;
  465.  
  466.     for( cur = items; cur != 0; cur = cur->next )
  467.         {
  468.         os.writeString( cur->name );
  469.         os << cur->index;
  470.         }
  471. }
  472.  
  473. void TColorGroupList::writeGroups( opstream& os, TColorGroup *groups )
  474. {
  475.     int count = 0;
  476.     TColorGroup *cur;
  477.  
  478.     for( cur = groups; cur != 0; cur = cur->next )
  479.         count++;
  480.  
  481.     os << count;
  482.  
  483.     for( cur = groups; cur != 0; cur = cur->next )
  484.         {
  485.         os.writeString( cur->name );
  486.         writeItems( os, cur->items );
  487.         }
  488. }
  489.                                     
  490. void TColorGroupList::write( opstream& os )
  491. {
  492.     TListViewer::write( os );
  493.     writeGroups( os, groups );
  494. }
  495.  
  496. TColorItem *TColorGroupList::readItems( ipstream& is )
  497. {
  498.     int count;
  499.     is >> count;
  500.     TColorItem *items = 0;
  501.     TColorItem **cur = &items;
  502.     while( count-- > 0 )
  503.         {
  504.         const char *nm = is.readString();
  505.         uchar index;
  506.         is >> index;
  507.         *cur = new TColorItem( nm, index );
  508.         cur = &((*cur)->next);
  509.         }
  510.     *cur = 0;
  511.     return items;
  512. }
  513.  
  514. TColorGroup *TColorGroupList::readGroups( ipstream& is )
  515. {
  516.     int count;
  517.     is >> count;
  518.     TColorGroup *groups = 0;
  519.     TColorGroup **cur = &groups;
  520.     while( count-- > 0 )
  521.         {
  522.         const char *nm = is.readString();
  523.         TColorItem *grp = readItems( is );
  524.         *cur = new TColorGroup( nm, grp );
  525.         cur = &((*cur)->next);
  526.         }
  527.     *cur = 0;
  528.     return groups;
  529. }
  530.  
  531. void *TColorGroupList::read( ipstream& is )
  532. {
  533.     TListViewer::read( is );
  534.     groups = readGroups( is );
  535.     return this;
  536. }
  537.  
  538. TStreamable *TColorGroupList::build()
  539. {
  540.     return new TColorGroupList( streamableInit );
  541. }
  542.  
  543. TColorGroupList::TColorGroupList( StreamableInit ) :
  544.     TListViewer( streamableInit )
  545. {
  546. }
  547.  
  548. TColorItemList::TColorItemList( const TRect& bounds,
  549.                                 TScrollBar *aScrollBar,
  550.                                 TColorItem *aItems
  551.                               ) :
  552.     TListViewer( bounds, 1, 0, aScrollBar ),
  553.     items( aItems )
  554. {
  555.     eventMask |= evBroadcast;
  556.     int i = 0;
  557.     while( aItems != 0 )
  558.         {
  559.         aItems = aItems->next;
  560.         i++;
  561.         }
  562.     setRange( i );
  563. }
  564.  
  565. void TColorItemList::focusItem( short item )
  566. {
  567.     TListViewer::focusItem( item );
  568.     TColorItem *curItem = items;
  569.     while( item-- > 0 )
  570.         curItem = curItem->next;
  571.     message( owner, evBroadcast, cmNewColorIndex, (void *)(curItem->index));
  572. }
  573.  
  574. void TColorItemList::getText( char *dest, short item, short maxChars )
  575. {
  576.     TColorItem *curItem = items;
  577.     while( item-- > 0 )
  578.         curItem = curItem->next;
  579.     strncpy( dest, curItem->name, maxChars );
  580.     dest[maxChars] = '\0';
  581. }
  582.  
  583. void TColorItemList::handleEvent( TEvent& event )
  584. {
  585.     TListViewer::handleEvent( event );
  586.     if( event.what == evBroadcast && event.message.command == cmNewColorItem )
  587.         {
  588.         items = (TColorItem *)event.message.infoPtr;
  589.         TColorItem *curItem = items;
  590.         int i = 0;
  591.         while( curItem != 0 )
  592.             {
  593.             curItem = curItem->next;
  594.             i++;
  595.             }
  596.         setRange( i );
  597.         focusItem( 0 );
  598.         drawView();
  599.         }
  600. }
  601.  
  602. TStreamable *TColorItemList::build()
  603. {
  604.     return new TColorItemList( streamableInit );
  605. }
  606.  
  607. TColorItemList::TColorItemList( StreamableInit ) :
  608.     TListViewer( streamableInit )
  609. {
  610. }
  611.  
  612. TColorDialog::TColorDialog( TPalette *aPalette, TColorGroup *aGroups ):
  613.     TDialog( TRect( 0, 0, 61, 18 ), colors ),
  614.     TWindowInit( &TColorDialog::initFrame )
  615. {
  616.     options |= ofCentered;
  617.     pal = aPalette;
  618.  
  619.     TScrollBar *sb = new TScrollBar( TRect( 18, 3, 19, 14 ) );
  620.     insert( sb );
  621.  
  622.     groups = new TColorGroupList( TRect( 3, 3, 18, 14 ), sb, aGroups);
  623.     insert( groups );
  624.     insert( new TLabel( TRect( 2, 2, 8, 3 ), groupText, groups ) );
  625.  
  626.     sb = new TScrollBar( TRect( 41, 3, 42, 14 ) );
  627.     insert( sb );
  628.  
  629.     TView *p = new TColorItemList( TRect( 21, 3, 41, 14 ), sb, aGroups->items );
  630.     insert( p );
  631.     insert( new TLabel( TRect( 20, 2, 25, 3 ), itemText, p ) );
  632.  
  633.     forSel = new TColorSelector( TRect( 45, 3, 57, 7 ),
  634.                                  TColorSelector::csForeground );
  635.     insert( forSel );
  636.     forLabel = new TLabel( TRect( 45, 2, 57, 3 ), forText, forSel );
  637.     insert( forLabel );
  638.  
  639.     bakSel = new TColorSelector( TRect( 45, 9, 57, 11 ),
  640.                                  TColorSelector::csBackground );
  641.     insert( bakSel );
  642.     bakLabel = new TLabel( TRect( 45, 8, 57, 9 ), bakText, bakSel );
  643.     insert( bakLabel );
  644.  
  645.     display = new TColorDisplay( TRect( 44, 12, 58, 14 ), textText );
  646.     insert( display );
  647.  
  648.     monoSel = new TMonoSelector( TRect( 44, 3, 59, 8 ) );
  649.     monoSel->hide();
  650.     insert( monoSel );
  651.     monoLabel = new TLabel( TRect( 43, 2, 49, 3 ), colorText, monoSel );
  652.     monoLabel->hide();
  653.     insert( monoLabel );
  654.  
  655.     if( aGroups != 0 && aGroups->items != 0 )
  656.         display->setColor( (uchar *)&pal->data[ aGroups->items->index ] );
  657.  
  658.     insert( new TButton( TRect( 36, 15, 46, 17 ), okText, cmOK, bfDefault ) );
  659.     insert( new TButton( TRect( 48, 15, 58, 17 ),
  660.                          cancelText,
  661.                          cmCancel,
  662.                          bfNormal ) );
  663.     selectNext( False );
  664. }
  665.  
  666. TColorDialog::~TColorDialog()
  667. }  
  668.  
  669. void TColorDialog::handleEvent( TEvent& event )
  670. {
  671.     TDialog::handleEvent( event );
  672.     if( event.what==evBroadcast && event.message.command==cmNewColorIndex )
  673.         display->setColor( (uchar *)&pal->data[event.message.infoByte] );
  674. }
  675.  
  676. ushort TColorDialog::dataSize()
  677. {
  678.     return *pal->data + 1;
  679. }
  680.  
  681. void TColorDialog::getData( void *rec )
  682. {
  683.     memcpy( rec, pal->data, *pal->data+1 );
  684. }
  685.  
  686. void TColorDialog::setData( void *rec)
  687. {
  688.     TPalette *p = (TPalette *)rec;
  689.  
  690.     memcpy( pal->data, p->data, *p->data+1 );
  691.     display->setColor( (uchar *)&pal->data[1] );
  692.     groups->focusItem( 0 );
  693.     if( showMarkers )
  694.         {
  695.         forLabel->hide();
  696.         forSel->hide();
  697.         bakLabel->hide();
  698.         bakSel->hide();
  699.         monoLabel->show();
  700.         monoSel->show();
  701.         }
  702.     groups->select();
  703. }
  704.  
  705. void TColorDialog::write( opstream& os )
  706. {
  707.     TDialog::write( os );
  708.     os << display << groups << forLabel << forSel
  709.        << bakLabel << bakSel << monoLabel << monoSel;
  710. }
  711.  
  712. void *TColorDialog::read( ipstream& is )
  713. {
  714.     TDialog::read( is );
  715.     is >> display >> groups >> forLabel >> forSel
  716.        >> bakLabel >> bakSel >> monoLabel >> monoSel;
  717.     pal = 0;
  718.     return this;
  719. }
  720.  
  721. TStreamable *TColorDialog::build()
  722. {
  723.     return new TColorDialog( streamableInit );
  724. }
  725.  
  726. TColorDialog::TColorDialog( StreamableInit ) :
  727.     TDialog( streamableInit ),
  728.     TWindowInit( streamableInit )
  729. {
  730. }
  731.  
  732.