home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / src / colorsel.cpp < prev    next >
C/C++ Source or Header  |  1999-05-19  |  22KB  |  931 lines

  1. /*
  2.  * colorsel.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_TColorSelector
  14. #define Uses_TMonoSelector
  15. #define Uses_TColorDisplay
  16. #define Uses_TColorItem
  17. #define Uses_TColorItemList
  18. #define Uses_TColorGroup
  19. #define Uses_TColorGroupList
  20. #define Uses_TColorDialog
  21. #define Uses_TEvent
  22. #define Uses_TDrawBuffer
  23. #define Uses_TGroup
  24. #define Uses_TSItem
  25. #define Uses_TScrollBar
  26. #define Uses_TLabel
  27. #define Uses_TButton
  28. #define Uses_TRect
  29. #define Uses_opstream
  30. #define Uses_ipstream
  31. #include <tvision/tv.h>
  32.  
  33. #include <string.h>
  34.  
  35. static TColorIndex* colorIndexes = 0;
  36.  
  37. TColorItem::TColorItem( const char *nm, uchar idx, TColorItem *nxt )
  38. {
  39.     index = idx;
  40.     next = nxt;
  41.     name = newStr( nm );
  42. }
  43.  
  44. TColorItem::~TColorItem()
  45. {
  46.     delete (char *)name;
  47. }
  48.  
  49. TColorGroup::TColorGroup( const char *nm, TColorItem *itm, TColorGroup *nxt )
  50. {
  51.     items = itm;
  52.     next = nxt;
  53.     name = newStr( nm );
  54. }
  55.  
  56. TColorGroup::~TColorGroup()
  57. {
  58.     delete (char *)name;
  59. }
  60.  
  61. TColorItem& operator + ( TColorItem& i1, TColorItem& i2 )
  62. {
  63.     TColorItem *cur = &i1;
  64.     while( cur->next != 0 )
  65.         cur = cur->next;
  66.     cur->next = &i2;
  67.     return i1;
  68. }
  69.  
  70. TColorGroup& operator + ( TColorGroup& g, TColorItem& i )
  71. {
  72.     TColorGroup *grp = &g;
  73.     while( grp->next != 0 )
  74.         grp = grp->next;
  75.  
  76.     if( grp->items == 0 )
  77.         grp->items = &i;
  78.     else
  79.         {
  80.         TColorItem *cur = grp->items;
  81.         while( cur->next != 0 )
  82.             cur = cur->next;
  83.         cur->next = &i;
  84.         }
  85.     return g;
  86. }
  87.  
  88. TColorGroup& operator + ( TColorGroup& g1, TColorGroup& g2 )
  89. {
  90.     TColorGroup *cur = &g1;
  91.     while( cur->next != 0 )
  92.         cur = cur->next;
  93.     cur->next = &g2;
  94.     return g1;
  95. }
  96.  
  97.  
  98. TColorSelector::TColorSelector( const TRect& bounds, ColorSel aSelType ) :
  99.     TView( bounds )
  100. {
  101.     options |= ofSelectable | ofFirstClick | ofFramed;
  102.     eventMask |= evBroadcast;
  103.     selType = aSelType;
  104.     color = 0;
  105. }
  106.  
  107. void TColorSelector::draw()
  108. {
  109.     TDrawBuffer b;
  110.     b.moveChar( 0, ' ', 0x70, size.x );
  111.     for(int i = 0; i <= size.y; i++ )
  112.         {
  113.         if( i < 4 )
  114.             {
  115.             for( int j = 0; j < 4; j++ )
  116.                 {
  117.                 int c = i*4+j;
  118.                 b.moveChar( j*3, icon, c, 3 );
  119.                 if( c == color )
  120.                     {
  121.                     b.putChar( j*3+1, 8 );
  122.                     if( c == 0 )
  123.                         b.putAttribute( j*3+1, 0x70 );
  124.                     }
  125.                 }
  126.             }
  127.         writeLine( 0, i, size.x, 1, b );
  128.         }
  129. }
  130.  
  131. void TColorSelector::colorChanged()
  132. {
  133.     int msg;
  134.     if( selType == csForeground )
  135.         msg = cmColorForegroundChanged;
  136.     else
  137.         msg = cmColorBackgroundChanged;
  138.     message( owner, evBroadcast, msg, (void *)color );
  139. }
  140.  
  141. void TColorSelector::handleEvent( TEvent& event )
  142. {
  143.     const width = 4;
  144.  
  145.     TView::handleEvent( event );
  146.  
  147.     uchar oldColor = color;
  148.     int maxCol = (selType == csBackground) ? 7 : 15;
  149.     switch( event.what )
  150.         {
  151.  
  152.         case evMouseDown:
  153.             do  {
  154.                 if( mouseInView( event.mouse.where ) )
  155.                     {
  156.                     TPoint mouse = makeLocal( event.mouse.where );
  157.                     color = mouse.y*4 + mouse.x/3;
  158.                     }
  159.                 else
  160.                     color = oldColor;
  161.                 colorChanged();
  162.                 drawView();
  163.                 } while( mouseEvent( event, evMouseMove ) );
  164.             break;
  165.  
  166.         case evKeyDown:
  167.             switch( ctrlToArrow( event.keyDown.keyCode ) )
  168.                 {
  169.                 case kbLeft:
  170.                     if( color > 0 )
  171.                         color--;
  172.                     else
  173.                         color = maxCol;
  174.                     break;
  175.  
  176.                 case kbRight:
  177.                     if( color < maxCol )
  178.                         color++;
  179.                     else
  180.                         color = 0;
  181.                     break;
  182.  
  183.                 case kbUp:
  184.                     if( color > width-1 )
  185.                         color -= width;
  186.                     else if( color == 0 )
  187.                         color = maxCol;
  188.                     else
  189.                         color += maxCol - width;
  190.                     break;
  191.  
  192.                 case kbDown:
  193.                     if( color < maxCol - (width-1) )
  194.                         color += width;
  195.                     else if( color == maxCol )
  196.                         color = 0;
  197.                     else
  198.                         color -= maxCol - width;
  199.                     break;
  200.  
  201.                 default:
  202.                     return;
  203.                 }
  204.             break;
  205.  
  206.         case evBroadcast:
  207.             if( event.message.command == cmColorSet )
  208.                 {
  209.         /*
  210.          * SS: some non-portable code changed.
  211.          */
  212.                 if( selType == csBackground )
  213.                     color = (int)event.message.infoPtr >> 4;
  214. //                    color = event.message.infoByte >> 4;
  215.                 else
  216.                     color = (int)event.message.infoPtr & 0x0F;
  217. //                    color = event.message.infoByte & 0x0F;
  218.                 drawView();
  219.                 return ;
  220.                 }
  221.             else
  222.                 return;
  223.         default:
  224.             return ;
  225.         }
  226.     drawView();
  227.     colorChanged();
  228.     clearEvent( event );
  229. }
  230.  
  231. #if !defined(NO_STREAMABLE)
  232.  
  233. void TColorSelector::write( opstream& os )
  234. {
  235.     TView::write( os );
  236.     os << color << (int)selType;
  237. }
  238.  
  239. void *TColorSelector::read( ipstream& is )
  240. {
  241.     int temp;
  242.     TView::read( is );
  243.     is >> color >> temp;
  244.     selType = ColorSel(temp);
  245.     return this;
  246. }
  247.  
  248. TStreamable *TColorSelector::build()
  249. {
  250.     return new TColorSelector( streamableInit );
  251. }
  252.  
  253. TColorSelector::TColorSelector( StreamableInit ) : TView( streamableInit )
  254. {
  255. }
  256.  
  257. #endif
  258.  
  259. const uchar monoColors[] = { 0x07, 0x0F, 0x01, 0x70, 0x09 };
  260.  
  261. TMonoSelector::TMonoSelector( const TRect& bounds ) :
  262.     TCluster( bounds, new TSItem( normal,
  263.                       new TSItem( highlight,
  264.                       new TSItem( underline,
  265.                       new TSItem( inverse,  0 )))))
  266. {
  267.     eventMask |= evBroadcast;
  268. }
  269.  
  270. void TMonoSelector::draw()
  271. {
  272.     drawBox( button, 0x07 );
  273. }
  274.  
  275. void TMonoSelector::handleEvent( TEvent& event )
  276. {
  277.     TCluster::handleEvent( event );
  278.     if( event.what == evBroadcast && event.message.command == cmColorSet )
  279.         {
  280.     /*
  281.      * SS: some non-portable code changed.
  282.      */
  283.         value = (int)event.message.infoPtr;
  284. //        value = event.message.infoByte;
  285.         drawView();
  286.         }
  287. }
  288.  
  289. Boolean TMonoSelector::mark( int item )
  290. {
  291.     return Boolean(monoColors[item] == value);
  292. }
  293.  
  294. void TMonoSelector::newColor()
  295. {
  296.     message( owner, evBroadcast, cmColorForegroundChanged,
  297.         (void *)(value & 0x0F) );
  298.     message( owner, evBroadcast, cmColorBackgroundChanged,
  299.         (void *)((value >> 4) & 0x0F));
  300. }
  301.  
  302. void TMonoSelector::press( int item )
  303. {
  304.     value = monoColors[item];
  305.     newColor();
  306. }
  307.  
  308. void TMonoSelector::movedTo( int item )
  309. {
  310.     value = monoColors[item];
  311.     newColor();
  312. }
  313.  
  314. #if !defined(NO_STREAMABLE)
  315.  
  316. TStreamable *TMonoSelector::build()
  317. {
  318.     return new TMonoSelector( streamableInit );
  319. }
  320.  
  321. TMonoSelector::TMonoSelector( StreamableInit ) : TCluster( streamableInit )
  322. {
  323. }
  324.  
  325. #endif
  326.  
  327.  
  328. TColorDisplay::TColorDisplay( const TRect& bounds, const char *aText ) :
  329.     TView( bounds ),
  330.     text( newStr( aText ) ),
  331.     color( 0 )
  332. {
  333.   eventMask |= evBroadcast;
  334. }
  335.  
  336. TColorDisplay::~TColorDisplay()
  337. {
  338.     delete (char *)text;
  339. }
  340.  
  341. void TColorDisplay::draw()
  342. {
  343.     uchar c = *color;
  344.     if( c == 0 )
  345.         c = errorAttr;
  346.     const int len = strlen( text );
  347.     TDrawBuffer b;
  348.     for( int i = 0; i <= size.x/len; i++ )
  349.         b.moveStr( i*len, text, c );
  350.     writeLine( 0, 0, size.x, size.y, b );
  351. }
  352.  
  353. void TColorDisplay::handleEvent( TEvent& event )
  354. {
  355.     TView::handleEvent( event );
  356.     if( event.what == evBroadcast )
  357.         switch( event.message.command )
  358.             {
  359.             case cmColorBackgroundChanged:
  360.         /*
  361.          * SS: some non-portable code changed.
  362.          */
  363.                 *color = (*color & 0x0F) | (((int)event.message.infoPtr << 4) & 0xF0);
  364. //                *color = (*color & 0x0F) | ((event.message.infoByte << 4) & 0xF0);
  365.                 drawView();
  366.                 break;
  367.  
  368.             case cmColorForegroundChanged:
  369.                 *color = (*color & 0xF0) | ((int)event.message.infoPtr & 0x0F);
  370. //                *color = (*color & 0xF0) | (event.message.infoByte & 0x0F);
  371.                 drawView();
  372.             }
  373. }
  374.  
  375. void TColorDisplay::setColor( uchar *aColor )
  376. {
  377.     color = aColor;
  378.     message( owner, evBroadcast, cmColorSet, (void *)(*color) );
  379.     drawView();
  380. }
  381.  
  382. #if !defined(NO_STREAMABLE)
  383.  
  384. void TColorDisplay::write( opstream& os )
  385. {
  386.     TView::write( os );
  387.     os.writeString( text );
  388. }
  389.  
  390. void *TColorDisplay::read( ipstream& is )
  391. {
  392.     TView::read( is );
  393.     text = is.readString();
  394.     color = 0;
  395.     return this;
  396. }
  397.  
  398. TStreamable *TColorDisplay::build()
  399. {
  400.     return new TColorDisplay( streamableInit );
  401. }
  402.  
  403. TColorDisplay::TColorDisplay( StreamableInit ) : TView( streamableInit )
  404. {
  405. }
  406.  
  407. #endif
  408.  
  409. TColorGroupList::TColorGroupList( const TRect& bounds,
  410.                                   TScrollBar *aScrollBar,
  411.                                   TColorGroup *aGroups
  412.                                 ) :
  413.     TListViewer( bounds, 1, 0, aScrollBar ),
  414.     groups( aGroups )
  415. {
  416.     int i = 0;
  417.     while( aGroups != 0 )
  418.         {
  419.         aGroups = aGroups->next;
  420.         i++;
  421.         }
  422.     setRange(i);
  423. }
  424.  
  425. static void freeItems( TColorItem *curItem )
  426. {
  427.     while( curItem != 0 )
  428.         {
  429.         TColorItem *p = curItem;
  430.         curItem = curItem->next;
  431.         delete p;
  432.         }
  433. }
  434.  
  435. static void freeGroups( TColorGroup *curGroup )
  436. {
  437.     while( curGroup != 0 )
  438.         {
  439.         TColorGroup *p = curGroup;
  440.         freeItems( curGroup->items );
  441.         curGroup = curGroup->next;
  442.         delete p;
  443.         }
  444. }
  445.  
  446. TColorGroupList::~TColorGroupList()
  447. {
  448.     freeGroups( groups );
  449. }
  450.  
  451. void TColorGroupList::focusItem( short item )
  452. {
  453.     TListViewer::focusItem( item );
  454.     TColorGroup *curGroup = groups;
  455.     while( item-- > 0 )
  456.         curGroup = curGroup->next;
  457.     message( owner, evBroadcast, cmNewColorItem, curGroup);
  458. }
  459.  
  460. void TColorGroupList::getText( char *dest, short item, short maxChars )
  461. {
  462.     TColorGroup *curGroup = groups;
  463.     while( item-- > 0 )
  464.         curGroup = curGroup->next;
  465.     strncpy( dest, curGroup->name, maxChars );
  466.     dest[maxChars] = '\0';
  467. }
  468.  
  469. #if !defined(NO_STREAMABLE)
  470.  
  471. void TColorGroupList::writeItems( opstream& os, TColorItem *items )
  472. {
  473.     int count = 0;
  474.     TColorItem *cur;
  475.  
  476.     for( cur = items; cur != 0; cur = cur->next )
  477.         count++;
  478.  
  479.     os << count;
  480.  
  481.     for( cur = items; cur != 0; cur = cur->next )
  482.         {
  483.         os.writeString( cur->name );
  484.         os << cur->index;
  485.         }
  486. }
  487.  
  488.  
  489. void TColorGroupList::writeGroups( opstream& os, TColorGroup *groups )
  490. {
  491.     int count = 0;
  492.     TColorGroup *cur;
  493.  
  494.     for( cur = groups; cur != 0; cur = cur->next )
  495.         count++;
  496.  
  497.     os << count;
  498.  
  499.     for( cur = groups; cur != 0; cur = cur->next )
  500.         {
  501.         os.writeString( cur->name );
  502.         writeItems( os, cur->items );
  503.         }
  504. }
  505. #endif
  506.  
  507. void TColorGroupList::handleEvent(TEvent& ev)
  508. {
  509.     TListViewer::handleEvent(ev);
  510.     if ((ev.what == evBroadcast) &&
  511.         (ev.message.command == cmSaveColorIndex))
  512.     /*
  513.      * SS: some non-portable code changed.
  514.      */
  515.         setGroupIndex(focused, (int)ev.message.infoPtr);
  516. //        setGroupIndex(focused, ev.message.infoByte);
  517. }
  518.  
  519. void TColorGroupList::setGroupIndex(uchar groupNum, uchar itemNum)
  520. {
  521.     TColorGroup* g = getGroup(groupNum);
  522.     if (g)
  523.         g->index = itemNum;
  524. }
  525.  
  526. uchar TColorGroupList::getGroupIndex(uchar groupNum)
  527. {
  528.     TColorGroup* g = getGroup(groupNum);
  529.     if (g)
  530.         return g->index;
  531.     else
  532.  
  533.     /* SS: this makes g++ happy */
  534.  
  535.     return (uchar) NULL;
  536. //        return NULL;
  537. }
  538.  
  539. TColorGroup* TColorGroupList::getGroup(uchar groupNum)
  540. {
  541.     TColorGroup* g = groups;
  542.  
  543.     while (groupNum--)
  544.         g = g->next;
  545.  
  546.     return g;
  547. }
  548.  
  549. uchar TColorGroupList::getNumGroups()
  550. {
  551.     uchar n;
  552.     TColorGroup* g = groups;
  553.  
  554.     for (n=0; g; n++)
  555.         g = g->next;
  556.  
  557.     return n;
  558. }
  559.  
  560. #if !defined(NO_STREAMABLE)
  561.  
  562. void TColorGroupList::write( opstream& os )
  563. {
  564.     TListViewer::write( os );
  565.     writeGroups( os, groups );
  566. }
  567.  
  568. TColorItem *TColorGroupList::readItems( ipstream& is )
  569. {
  570.     int count;
  571.     is >> count;
  572.     TColorItem *items = 0;
  573.     TColorItem **cur = &items;
  574.     while( count-- > 0 )
  575.         {
  576.         char *nm = is.readString();
  577.         uchar index;
  578.         is >> index;
  579.         *cur = new TColorItem( nm, index );
  580.         delete nm;
  581.         cur = &((*cur)->next);
  582.         }
  583.     *cur = 0;
  584.     return items;
  585. }
  586.  
  587. TColorGroup *TColorGroupList::readGroups( ipstream& is )
  588. {
  589.     int count;
  590.     is >> count;
  591.     TColorGroup *groups = 0;
  592.     TColorGroup **cur = &groups;
  593.     while( count-- > 0 )
  594.         {
  595.         char *nm = is.readString();
  596.         TColorItem *grp = readItems( is );
  597.         *cur = new TColorGroup( nm, grp );
  598.         cur = &((*cur)->next);
  599.         delete nm;
  600.         }
  601.     *cur = 0;
  602.     return groups;
  603. }
  604.  
  605. void *TColorGroupList::read( ipstream& is )
  606. {
  607.     TListViewer::read( is );
  608.     groups = readGroups( is );
  609.     return this;
  610. }
  611.  
  612. TStreamable *TColorGroupList::build()
  613. {
  614.     return new TColorGroupList( streamableInit );
  615. }
  616.  
  617. TColorGroupList::TColorGroupList( StreamableInit ) :
  618.     TListViewer( streamableInit )
  619. {
  620. }
  621.  
  622. #endif
  623.  
  624. TColorItemList::TColorItemList( const TRect& bounds,
  625.                                 TScrollBar *aScrollBar,
  626.                                 TColorItem *aItems
  627.                               ) :
  628.     TListViewer( bounds, 1, 0, aScrollBar ),
  629.     items( aItems )
  630. {
  631.     eventMask |= evBroadcast;
  632.     int i = 0;
  633.     while( aItems != 0 )
  634.         {
  635.         aItems = aItems->next;
  636.         i++;
  637.         }
  638.     setRange( i );
  639. }
  640.  
  641. void TColorItemList::focusItem( short item )
  642. {
  643.     TListViewer::focusItem( item );
  644.     message(owner,evBroadcast, cmSaveColorIndex, (void*)item);
  645.  
  646.     TColorItem *curItem = items;
  647.     while( item-- > 0 )
  648.         curItem = curItem->next;
  649.     message( owner, evBroadcast, cmNewColorIndex, (void *)(curItem->index));
  650. }
  651.  
  652. void TColorItemList::getText( char *dest, short item, short maxChars )
  653. {
  654.     TColorItem *curItem = items;
  655.     while( item-- > 0 )
  656.         curItem = curItem->next;
  657.     strncpy( dest, curItem->name, maxChars );
  658.     dest[maxChars] = '\0';
  659. }
  660.  
  661. void TColorItemList::handleEvent( TEvent& event )
  662. {
  663.     TListViewer::handleEvent( event );
  664.     if( event.what == evBroadcast )
  665.         {
  666.         TColorGroup* g = (TColorGroup*) event.message.infoPtr;
  667.         TColorItem *curItem;
  668.         int i = 0;
  669.  
  670.         switch(event.message.command)
  671.             {
  672.             case cmNewColorItem:
  673.                 curItem = items = g->items;
  674.                 while( curItem != 0 )
  675.                     {
  676.                     curItem = curItem->next;
  677.                     i++;
  678.                     }
  679.                 setRange( i );
  680.                 focusItem( g->index);
  681.                 drawView();
  682.                 break;
  683.             default:
  684.                 break;
  685.             }
  686.         }
  687. }
  688.  
  689. #if !defined(NO_STREAMABLE)
  690.  
  691. TStreamable *TColorItemList::build()
  692. {
  693.     return new TColorItemList( streamableInit );
  694. }
  695.  
  696. TColorItemList::TColorItemList( StreamableInit ) :
  697.     TListViewer( streamableInit )
  698. {
  699. }
  700.  
  701. #endif
  702.  
  703. TColorDialog::TColorDialog( TPalette *aPalette, TColorGroup *aGroups ):
  704. //#ifndef 1 //__UNPATCHED
  705. //    TDialog( TRect( 0, 0, 79, 18 ), colors ),
  706. //#else
  707.     TDialog( TRect( 0, 0, 61, 18 ), colors ),
  708. //#endif
  709.     TWindowInit( &TColorDialog::initFrame )
  710. {
  711.     options |= ofCentered;
  712.     if( aPalette != 0 )
  713.         {
  714.         pal = new TPalette( "", 0 );
  715.         *pal = *aPalette;
  716.         }
  717.     else
  718.         pal = 0;
  719.  
  720. /*
  721. #ifndef 1 //__UNPATCHED
  722.  
  723.     TScrollBar *sb = new TScrollBar( TRect( 27, 3, 28, 14 ) );
  724.     insert( sb );
  725.  
  726.     groups = new TColorGroupList( TRect( 3, 3, 27, 14 ), sb, aGroups);
  727.     insert( groups );
  728.     insert( new TLabel( TRect( 3, 2, 10, 3 ), groupText, groups ) );
  729.  
  730.     sb = new TScrollBar( TRect( 59, 3, 60, 14 ) );
  731.     insert( sb );
  732.  
  733.     TView *p = new TColorItemList( TRect( 30, 3, 59, 14 ), sb, aGroups->items );
  734.     insert( p );
  735.     insert( new TLabel( TRect( 30, 2, 36, 3 ), itemText, p ) );
  736.  
  737.     forSel = new TColorSelector( TRect( 63, 3, 75, 7 ),
  738.                  TColorSelector::csForeground );
  739.     insert( forSel );
  740.     forLabel = new TLabel( TRect( 63, 2, 75, 3 ), forText, forSel );
  741.     insert( forLabel );
  742.  
  743.     bakSel = new TColorSelector( TRect( 63, 9, 75, 11 ),
  744.                  TColorSelector::csBackground );
  745.     insert( bakSel );
  746.     bakLabel = new TLabel( TRect( 63, 8, 75, 9 ), bakText, bakSel );
  747.     insert( bakLabel );
  748.  
  749.     display = new TColorDisplay( TRect( 62, 12, 76, 14 ), textText );
  750.     insert( display );
  751.  
  752.     monoSel = new TMonoSelector( TRect( 62, 3, 77, 7 ) );
  753.     monoSel->hide();
  754.     insert( monoSel );
  755.     monoLabel = new TLabel( TRect( 62, 2, 69, 3 ), colorText, monoSel );
  756.     monoLabel->hide();
  757.     insert( monoLabel );
  758.  
  759.     insert( new TButton( TRect( 51, 15, 61, 17 ), okText, cmOK, bfDefault ) );
  760.     insert( new TButton( TRect( 63, 15, 73, 17 ), cancelText, cmCancel, bfNormal ) );
  761.     selectNext( False );
  762. #else
  763. */
  764.     TScrollBar *sb = new TScrollBar( TRect( 18, 3, 19, 14 ) );
  765.     insert( sb );
  766.  
  767.     groups = new TColorGroupList( TRect( 3, 3, 18, 14 ), sb, aGroups);
  768.     insert( groups );
  769.     insert( new TLabel( TRect( 2, 2, 8, 3 ), groupText, groups ) );
  770.  
  771.     sb = new TScrollBar( TRect( 41, 3, 42, 14 ) );
  772.     insert( sb );
  773.  
  774.     TView *p = new TColorItemList( TRect( 21, 3, 41, 14 ), sb, aGroups->items );
  775.     insert( p );
  776.     insert( new TLabel( TRect( 20, 2, 25, 3 ), itemText, p ) );
  777.  
  778.     forSel = new TColorSelector( TRect( 45, 3, 57, 7 ),
  779.                                  TColorSelector::csForeground );
  780.     insert( forSel );
  781.     forLabel = new TLabel( TRect( 45, 2, 57, 3 ), forText, forSel );
  782.     insert( forLabel );
  783.  
  784.     bakSel = new TColorSelector( TRect( 45, 9, 57, 11 ),
  785.                                  TColorSelector::csBackground );
  786.     insert( bakSel );
  787.     bakLabel = new TLabel( TRect( 45, 8, 57, 9 ), bakText, bakSel );
  788.     insert( bakLabel );
  789.  
  790.     display = new TColorDisplay( TRect( 44, 12, 58, 14 ), textText );
  791.     insert( display );
  792.  
  793.     monoSel = new TMonoSelector( TRect( 44, 3, 59, 7 ) );
  794.     monoSel->hide();
  795.     insert( monoSel );
  796.     monoLabel = new TLabel( TRect( 43, 2, 49, 3 ), colorText, monoSel );
  797.     monoLabel->hide();
  798.     insert( monoLabel );
  799.  
  800.     insert( new TButton( TRect( 36, 15, 46, 17 ), okText, cmOK, bfDefault ) );
  801.     insert( new TButton( TRect( 48, 15, 58, 17 ), cancelText, cmCancel, bfNormal ) );
  802.     selectNext( False );
  803. //#endif
  804.  
  805.     if( pal != 0 )
  806.         setData( pal );
  807. }
  808.  
  809. TColorDialog::~TColorDialog()
  810. {
  811.     delete pal;
  812. }
  813.  
  814. void TColorDialog::handleEvent( TEvent& event )
  815. {
  816.     if( event.what==evBroadcast && event.message.command==cmNewColorItem )
  817.         groupIndex = groups->focused;
  818.     TDialog::handleEvent( event );
  819.     if( event.what==evBroadcast && event.message.command==cmNewColorIndex )
  820.     /*
  821.      * SS: some non-portable code changed.
  822.      */
  823.         display->setColor( (uchar *)&pal->data[(int)event.message.infoPtr] );
  824. //        display->setColor( (uchar *)&pal->data[event.message.infoByte] );
  825. }
  826.  
  827. ushort TColorDialog::dataSize()
  828. {
  829.     return *pal->data + 1;
  830. }
  831.  
  832. void TColorDialog::getData( void *rec )
  833. {
  834.     getIndexes(colorIndexes);
  835.     *(TPalette *) rec = *pal;
  836. }
  837.  
  838. void TColorDialog::setData( void *rec)
  839. {
  840.     if( !pal )
  841.         pal = new TPalette("", 0);
  842.     *pal = *(TPalette *) rec;
  843.  
  844.     setIndexes(colorIndexes);
  845.     display->setColor((uchar *)&pal->data[groups->getGroupIndex(groupIndex)]);
  846.     groups->focusItem( groupIndex);
  847.     if( showMarkers )
  848.         {
  849.         forLabel->hide();
  850.         forSel->hide();
  851.         bakLabel->hide();
  852.         bakSel->hide();
  853.         monoLabel->show();
  854.         monoSel->show();
  855.         }
  856.     groups->select();
  857. }
  858.  
  859. void TColorDialog::setIndexes(TColorIndex*& colIdx)
  860. {
  861.     uchar numGroups, index;
  862.  
  863.     numGroups = groups->getNumGroups();
  864.     if (colIdx && (colIdx->colorSize != numGroups))
  865.     {
  866.         delete colIdx;
  867. #ifndef __UNPATCHED
  868.         colIdx = NULL;      // BUG FIX
  869. #else
  870.         colors = NULL;
  871. #endif
  872.     }
  873.     if (!colIdx)
  874.     {
  875.         colIdx = (TColorIndex*) new uchar[numGroups+2];
  876.         colIdx->groupIndex = 0;
  877.         memset(colIdx->colorIndex, 0, numGroups);
  878.         colIdx->colorSize = numGroups;
  879.     }
  880.     for (index = 0; index < numGroups; index++)
  881.         groups->setGroupIndex(index, colIdx->colorIndex[index]);
  882.  
  883.     groupIndex = colIdx->groupIndex;
  884. }
  885.  
  886. void TColorDialog::getIndexes(TColorIndex*& colIdx)
  887. {
  888.     uchar n = groups->getNumGroups();
  889.     if (!colIdx)
  890.     {
  891.         colIdx = (TColorIndex*) new uchar[n+2];
  892.         memset(colIdx->colorIndex, 0, n);
  893.         colIdx->colorSize = n;
  894.     }
  895.     colIdx->groupIndex = groupIndex;
  896.     for (uchar index=0; index < n; index++)
  897.         colIdx->colorIndex[index] = groups->getGroupIndex(index);
  898. }
  899.  
  900. #if !defined(NO_STREAMABLE)
  901.  
  902. void TColorDialog::write( opstream& os )
  903. {
  904.     TDialog::write( os );
  905.     os << display << groups << forLabel << forSel
  906.        << bakLabel << bakSel << monoLabel << monoSel;
  907. }
  908.  
  909. void *TColorDialog::read( ipstream& is )
  910. {
  911.     TDialog::read( is );
  912.     is >> display >> groups >> forLabel >> forSel
  913.        >> bakLabel >> bakSel >> monoLabel >> monoSel;
  914.     pal = 0;
  915.     return this;
  916. }
  917.  
  918. TStreamable *TColorDialog::build()
  919. {
  920.     return new TColorDialog( streamableInit );
  921. }
  922.  
  923. TColorDialog::TColorDialog( StreamableInit ) :
  924.     TDialog( streamableInit ),
  925.     TWindowInit( 0 /*streamableInit*/ )
  926. {
  927. }
  928.  
  929.  
  930. #endif
  931.