home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscTableScroll / MiscTableCell.M < prev    next >
Encoding:
Text File  |  1996-02-11  |  30.6 KB  |  1,066 lines

  1. //=============================================================================
  2. //
  3. //        Copyright (C) 1995, 1996 by Paul S. McCarthy and Eric Sunshine.
  4. //                Written by Paul S. McCarthy and Eric Sunshine.
  5. //                            All Rights Reserved.
  6. //
  7. //        This notice may not be removed from this source code.
  8. //
  9. //        This object is included in the MiscKit by permission from the authors
  10. //        and its use is governed by the MiscKit license, found in the file
  11. //        "License.rtf" in the MiscKit distribution.    Please refer to that file
  12. //        for a list of all applicable permissions and restrictions.
  13. //        
  14. //=============================================================================
  15. //-----------------------------------------------------------------------------
  16. // MiscTableCell.M
  17. //
  18. //        Default cell class used by MiscTableScroll to display text.
  19. //
  20. // NOTE *SET-OWNER-VALUE*
  21. //        We must implement these methods so that MiscTableScroll will call 
  22. //        these methods instead of the explicit instance -setXxx methods.     
  23. //        However, we don't need to do any work.    We will ask the owner for 
  24. //        it's value whenever we need it.     
  25. //
  26. // FIXME: optional-allocation stuff does not address alignment requirements.
  27. // FIXME: automate most of the optional-allocation stuff.
  28. //-----------------------------------------------------------------------------
  29. //-----------------------------------------------------------------------------
  30. // $Id: MiscTableCell.M,v 1.7 96/01/13 23:41:21 zarnuk Exp $
  31. // $Log:        MiscTableCell.M,v $
  32. // Revision 1.7     96/01/13  23:41:21     zarnuk
  33. // Tag is no longer optional.
  34. // 
  35. // Revision 1.6     95/10/14  16:48:53     zarnuk
  36. // Now calls -setUseOwnerFont:YES in -initTextCell:
  37. // 
  38. // Revision 1.5     95/10/03  05:01:54     sunshine
  39. // Implemented -copyFromZone: instead of -copy.     Now all allocations are made
  40. // from [self zone] instead of default-malloc-zone.
  41. // 
  42. // Revision 1.4     95/10/01  15:07:07     sunshine
  43. // Added highlightTextColor and highlightBackgroundColor and corresponding
  44. // manipulation methods and 'gray' methods.     Fixed a bunch of bugs.     For
  45. // instance the -init methods were unconditionally setting the font and color
  46. // (and allocated memory for them) and clearing the useOwner... stuff.
  47. // Simplified -*pos methods somewhat by removing copy/paste code.  Now each
  48. // one calls its predecessor.  Made default background color LTGRAY once again
  49. // because Paul hatez it and it's much more consistent with the rest of the
  50. // AppKit (every scrolling list in practically every application has a LTGRAY
  51. // background).
  52. // 
  53. // Revision 1.3     95/09/29  16:01:02     zarnuk
  54. // lazy-allocation for tags, colors.
  55. // useOwner options for font, colors.
  56. //-----------------------------------------------------------------------------
  57. #import <misckit/MiscTableCell.h>
  58.  
  59. extern "Objective-C" {
  60. #import <appkit/Font.h>
  61. #import <appkit/Text.h>
  62. #import <dpsclient/wraps.h>
  63. }
  64.  
  65. extern "C" {
  66. #import <string.h>
  67. }
  68.  
  69. #define MISC_TC_VERSION_0        0
  70. #define MISC_TC_VERSION_1        1
  71. #define MISC_TC_VERSION            MISC_TC_VERSION_1
  72.  
  73. @implementation MiscTableCell
  74.  
  75. //-----------------------------------------------------------------------------
  76. // + initialize
  77. //-----------------------------------------------------------------------------
  78. + initialize
  79.     {
  80.     if (self == [MiscTableCell class])
  81.         {
  82.         [self setVersion: MISC_TC_VERSION];
  83.         }
  84.     return self;
  85.     }
  86.  
  87.  
  88. //-----------------------------------------------------------------------------
  89. // + defaultFont
  90. //-----------------------------------------------------------------------------
  91. + defaultFont
  92.     {
  93.     return [Font userFontOfSize:12.0 matrix:NX_FLIPPEDMATRIX];
  94.     }
  95.  
  96.  
  97. //-----------------------------------------------------------------------------
  98. // initTextCell:
  99. //        NOTE *1*  -[Cell initTextCell:] invokes -setStringValue: which
  100. //        invokes -setFont:.    This means that this cell will get an explicit
  101. //        -setFont call before we finish initialization, and we need to 
  102. //        restore the useOwnerFont setting.
  103. //-----------------------------------------------------------------------------
  104. - initTextCell: (char const*) s
  105.     {
  106.     [super initTextCell:s];                                // NOTE *1*
  107.     [self setBordered:NO];
  108.     [self setWrap:NO];
  109.     [self setAlignment:NX_LEFTALIGNED];
  110.     [self setUseOwnerFont:YES];                            // NOTE *1*
  111.     [self setOwnerFont: [[self class] defaultFont]];
  112.     return self;
  113.     }
  114.  
  115.  
  116. //-----------------------------------------------------------------------------
  117. // initIconCell:
  118. //-----------------------------------------------------------------------------
  119. - initIconCell: (char const*) s
  120.     {
  121.     [super initIconCell:s];
  122.     [self setBordered:NO];
  123.     return self;
  124.     }
  125.  
  126.  
  127. //-----------------------------------------------------------------------------
  128. // tag
  129. //-----------------------------------------------------------------------------
  130. - (int) tag
  131.     {
  132.     return tag;
  133.     }
  134.  
  135.  
  136. //-----------------------------------------------------------------------------
  137. // setTag:
  138. //-----------------------------------------------------------------------------
  139. - setTag:(int)x
  140.     {
  141.     tag = x;
  142.     return self;
  143.     }
  144.  
  145.  
  146. //-----------------------------------------------------------------------------
  147. // image
  148. //-----------------------------------------------------------------------------
  149. - image
  150.     {
  151.     return ([self type] == NX_ICONCELL ? support : 0);
  152.     }
  153.  
  154.  
  155. //-----------------------------------------------------------------------------
  156. // setImage:
  157. //-----------------------------------------------------------------------------
  158. - setImage: image
  159.     {
  160.     if ([self type] == NX_ICONCELL)
  161.         {
  162.         char const* name = [image name];
  163.         if (name)
  164.             {
  165.             [self setIcon: name];
  166.             }
  167.         else
  168.             {
  169.             support = image;
  170.             if (contents != 0 && cFlags1.freeText)
  171.                 NXZoneFree( [self zone], contents );
  172.             contents = 0;
  173.             cFlags1.freeText = NO;
  174.             }
  175.         }
  176.     return self;
  177.     }
  178.  
  179.  
  180. //-----------------------------------------------------------------------------
  181. // owner
  182. //-----------------------------------------------------------------------------
  183. - owner
  184.     {
  185.     return owner;
  186.     }
  187.  
  188.  
  189. //-----------------------------------------------------------------------------
  190. // setOwner:
  191. //-----------------------------------------------------------------------------
  192. - setOwner:obj
  193.     {
  194.     if (obj != owner)
  195.         {
  196.         owner = obj;
  197.         if (owner != 0)
  198.             {
  199.             if ([owner respondsTo:@selector(font)])
  200.                 [self setOwnerFont:[owner font]];
  201.             if ([owner respondsTo:@selector(textColor)])
  202.                 [self setOwnerTextColor:[owner textColor]];
  203.             if ([owner respondsTo:@selector(backgroundColor)])
  204.                 [self setOwnerBackgroundColor:[owner backgroundColor]];
  205.             if ([owner respondsTo:@selector(highlightTextColor)])
  206.                 [self setOwnerTextColor:[owner highlightTextColor]];
  207.             if ([owner respondsTo:@selector(highlightBackgroundColor)])
  208.                 [self setOwnerBackgroundColor:[owner highlightBackgroundColor]];
  209.             }
  210.         }
  211.     return self;
  212.     }
  213.  
  214.  
  215. //-----------------------------------------------------------------------------
  216. // font
  217. //-----------------------------------------------------------------------------
  218. - font
  219.     {
  220.     return [super font];
  221.     }
  222.  
  223.  
  224. //-----------------------------------------------------------------------------
  225. // setFont:
  226. //-----------------------------------------------------------------------------
  227. - setFont:fontObj
  228.     {
  229.     [self setUseOwnerFont:NO];
  230.     [super setFont:fontObj];
  231.     return self;
  232.     }
  233.  
  234.  
  235. //-----------------------------------------------------------------------------
  236. // tc1Flags
  237. //-----------------------------------------------------------------------------
  238. - (unsigned int) tc1Flags
  239.     {
  240.     return tc1_flags;
  241.     }
  242.  
  243.  
  244. //-----------------------------------------------------------------------------
  245. // tc1DataSize
  246. //-----------------------------------------------------------------------------
  247. - (unsigned int) tc1DataSize
  248.     {
  249.     unsigned int size = 0;
  250.     if (tc1_flags & MISC_TC1_SELF_TEXT_COLOR)
  251.         size += [self tc1TextColorLen];
  252.     if (tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR)
  253.         size += [self tc1BackgroundColorLen];
  254.     if (tc1_flags & MISC_TC1_SELF_TEXT_COLOR_H)
  255.         size += [self tc1HighlightTextColorLen];
  256.     if (tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR_H)
  257.         size += [self tc1HighlightBackgroundColorLen];
  258.     return size;
  259.     }
  260.  
  261.  
  262. //-----------------------------------------------------------------------------
  263. // tc1TextColorPos
  264. //-----------------------------------------------------------------------------
  265. - (unsigned int) tc1TextColorPos
  266.     {
  267.     return 0;
  268.     }
  269.  
  270.  
  271. //-----------------------------------------------------------------------------
  272. // tc1BackgroundColorPos
  273. //-----------------------------------------------------------------------------
  274. - (unsigned int) tc1BackgroundColorPos
  275.     {
  276.     unsigned int pos = [self tc1TextColorPos];
  277.     if (tc1_flags & MISC_TC1_SELF_TEXT_COLOR)
  278.         pos += [self tc1TextColorLen];
  279.     return pos;
  280.     }
  281.  
  282.  
  283. //-----------------------------------------------------------------------------
  284. // tc1HighlightTextColorPos
  285. //-----------------------------------------------------------------------------
  286. - (unsigned int) tc1HighlightTextColorPos
  287.     {
  288.     unsigned int pos = [self tc1BackgroundColorPos];
  289.     if (tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR)
  290.         pos += [self tc1BackgroundColorLen];
  291.     return pos;
  292.     }
  293.  
  294.  
  295. //-----------------------------------------------------------------------------
  296. // tc1HighlightBackgroundColorPos
  297. //-----------------------------------------------------------------------------
  298. - (unsigned int) tc1HighlightBackgroundColorPos
  299.     {
  300.     unsigned int pos = [self tc1HighlightTextColorPos];
  301.     if (tc1_flags & MISC_TC1_SELF_TEXT_COLOR_H)
  302.         pos += [self tc1HighlightTextColorLen];
  303.     return pos;
  304.     }
  305.  
  306.  
  307. //-----------------------------------------------------------------------------
  308. // tc1TextColorLen
  309. //-----------------------------------------------------------------------------
  310. - (unsigned int) tc1TextColorLen
  311.     {
  312.     return sizeof(NXColor);
  313.     }
  314.  
  315.  
  316. //-----------------------------------------------------------------------------
  317. // tc1BackgroundColorLen
  318. //-----------------------------------------------------------------------------
  319. - (unsigned int) tc1BackgroundColorLen
  320.     {
  321.     return sizeof(NXColor);
  322.     }
  323.  
  324.  
  325. //-----------------------------------------------------------------------------
  326. // tc1HighlightTextColorLen
  327. //-----------------------------------------------------------------------------
  328. - (unsigned int) tc1HighlightTextColorLen
  329.     {
  330.     return sizeof(NXColor);
  331.     }
  332.  
  333.  
  334. //-----------------------------------------------------------------------------
  335. // tc1HighlightBackgroundColorLen
  336. //-----------------------------------------------------------------------------
  337. - (unsigned int) tc1HighlightBackgroundColorLen
  338.     {
  339.     return sizeof(NXColor);
  340.     }
  341.  
  342.  
  343. //-----------------------------------------------------------------------------
  344. // tc1TextColorPtr
  345. //-----------------------------------------------------------------------------
  346. - (NXColor*) tc1TextColorPtr
  347.     {
  348.     return (NXColor*) ((char*)tc1_data + [self tc1TextColorPos]);
  349.     }
  350.  
  351.  
  352. //-----------------------------------------------------------------------------
  353. // tc1BackgroundColorPtr
  354. //-----------------------------------------------------------------------------
  355. - (NXColor*) tc1BackgroundColorPtr
  356.     {
  357.     return (NXColor*) ((char*)tc1_data + [self tc1BackgroundColorPos]);
  358.     }
  359.  
  360.  
  361. //-----------------------------------------------------------------------------
  362. // tc1HighlightTextColorPtr
  363. //-----------------------------------------------------------------------------
  364. - (NXColor*) tc1HighlightTextColorPtr
  365.     {
  366.     return (NXColor*) ((char*)tc1_data + [self tc1HighlightTextColorPos]);
  367.     }
  368.  
  369.  
  370. //-----------------------------------------------------------------------------
  371. // tc1HighlightBackgroundColorPtr
  372. //-----------------------------------------------------------------------------
  373. - (NXColor*) tc1HighlightBackgroundColorPtr
  374.     {
  375.     return (NXColor*) ((char*)tc1_data + [self tc1HighlightBackgroundColorPos]);
  376.     }
  377.  
  378.  
  379. //-----------------------------------------------------------------------------
  380. // tc1InsertData:len:pos:
  381. //-----------------------------------------------------------------------------
  382. - (void*) tc1InsertData:(void const*)data
  383.         pos:(unsigned int)pos
  384.         len:(unsigned int)len
  385.     {
  386.     if (len > 0)
  387.         {
  388.         unsigned int const old_size = [self tc1DataSize];
  389.         unsigned int const new_size = old_size + len;
  390.         if (old_size == 0)
  391.             tc1_data = NXZoneMalloc( [self zone], new_size );
  392.         else
  393.             {
  394.             tc1_data = NXZoneRealloc( [self zone], tc1_data, new_size );
  395.             if (pos < old_size)
  396.                 memmove( (char*) tc1_data + pos + len,            // Destination
  397.                         (char*) tc1_data + pos,                    // Source
  398.                         old_size - pos );                        // # bytes.
  399.             }
  400.         if (data != 0)
  401.             memcpy( (char*) tc1_data + pos, data, len );
  402.         else
  403.             memset( (char*) tc1_data + pos, 0, len );
  404.         return (void*)((char*) tc1_data + pos );
  405.         }
  406.     return 0;
  407.     }
  408.  
  409.  
  410. //-----------------------------------------------------------------------------
  411. // tc1DeleteDataPos:len:
  412. //-----------------------------------------------------------------------------
  413. - (void) tc1DeleteDataPos:(unsigned int)pos len:(unsigned int)len
  414.     {
  415.     if (len > 0)
  416.         {
  417.         unsigned int const old_size = [self tc1DataSize];
  418.         unsigned int const new_size = old_size - len;
  419.         if (new_size == 0)
  420.             {
  421.             NXZoneFree( [self zone], tc1_data );
  422.             tc1_data = 0;
  423.             }
  424.         else
  425.             {
  426.             if (pos < new_size)
  427.                 memmove( (char*) tc1_data + pos,                // Destination
  428.                         (char*) tc1_data + pos + len,            // Source
  429.                         new_size - pos );                        // # bytes
  430.             tc1_data = NXZoneRealloc( [self zone], tc1_data, new_size );
  431.             }
  432.         }
  433.     }
  434.  
  435.  
  436. //-----------------------------------------------------------------------------
  437. // useOwnerFont
  438. //-----------------------------------------------------------------------------
  439. - (BOOL) useOwnerFont
  440.     {
  441.     return ((tc1_flags & MISC_TC1_SELF_FONT) == 0);
  442.     }
  443.  
  444.  
  445. //-----------------------------------------------------------------------------
  446. // useOwnerTextColor
  447. //-----------------------------------------------------------------------------
  448. - (BOOL) useOwnerTextColor
  449.     {
  450.     return ((tc1_flags & MISC_TC1_SELF_TEXT_COLOR) == 0);
  451.     }
  452.  
  453.  
  454. //-----------------------------------------------------------------------------
  455. // useOwnerBackgroundColor
  456. //-----------------------------------------------------------------------------
  457. - (BOOL) useOwnerBackgroundColor
  458.     {
  459.     return ((tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR) == 0);
  460.     }
  461.  
  462.  
  463. //-----------------------------------------------------------------------------
  464. // useOwnerHighlightTextColor
  465. //-----------------------------------------------------------------------------
  466. - (BOOL) useOwnerHighlightTextColor
  467.     {
  468.     return ((tc1_flags & MISC_TC1_SELF_TEXT_COLOR_H) == 0);
  469.     }
  470.  
  471.  
  472. //-----------------------------------------------------------------------------
  473. // useOwnerHighlightBackgroundColor
  474. //-----------------------------------------------------------------------------
  475. - (BOOL) useOwnerHighlightBackgroundColor
  476.     {
  477.     return ((tc1_flags & MISC_TC1_SELF_BACKGROUND_COLOR_H) == 0);
  478.     }
  479.  
  480.  
  481. //-----------------------------------------------------------------------------
  482. // setUseOwnerFont:
  483. //-----------------------------------------------------------------------------
  484. - setUseOwnerFont: (BOOL) flag
  485.     {
  486.     if ([self useOwnerFont] != flag)
  487.         {
  488.         if (flag)
  489.             {
  490.             tc1_flags &= ~(MISC_TC1_SELF_FONT);
  491.             if (owner != 0 && [owner respondsTo:@selector(font)])
  492.                 [self setOwnerFont:[owner font]];
  493.             }
  494.         else // (!flag)
  495.             {
  496.             tc1_flags |= MISC_TC1_SELF_FONT;
  497.             }
  498.         }
  499.     return self;
  500.     }
  501.  
  502.  
  503. //-----------------------------------------------------------------------------
  504. // setUseOwnerTextColor:
  505. //-----------------------------------------------------------------------------
  506. - setUseOwnerTextColor: (BOOL) flag
  507.     {
  508.     if ([self useOwnerTextColor] != flag)
  509.         {
  510.         unsigned int const pos = [self tc1TextColorPos];
  511.         unsigned int const len = [self tc1TextColorLen];
  512.         if (flag)
  513.             {
  514.             [self tc1DeleteDataPos:pos len:len];
  515.             tc1_flags &= ~(MISC_TC1_SELF_TEXT_COLOR);
  516.             }
  517.         else // (!flag)
  518.             {
  519.             NXColor color = [[self class] defaultTextColor];
  520.             [self tc1InsertData:&color pos:pos len:len];
  521.             tc1_flags |= MISC_TC1_SELF_TEXT_COLOR;
  522.             }
  523.         }
  524.     return self;
  525.     }
  526.  
  527.  
  528. //-----------------------------------------------------------------------------
  529. // setUseOwnerBackgroundColor:
  530. //-----------------------------------------------------------------------------
  531. - setUseOwnerBackgroundColor: (BOOL) flag
  532.     {
  533.     if ([self useOwnerBackgroundColor] != flag)
  534.         {
  535.         unsigned int const pos = [self tc1BackgroundColorPos];
  536.         unsigned int const len = [self tc1BackgroundColorLen];
  537.         if (flag)
  538.             {
  539.             [self tc1DeleteDataPos:pos len:len];
  540.             tc1_flags &= ~(MISC_TC1_SELF_BACKGROUND_COLOR);
  541.             }
  542.         else // (!flag)
  543.             {
  544.             NXColor color = [[self class] defaultBackgroundColor];
  545.             [self tc1InsertData:&color pos:pos len:len];
  546.             tc1_flags |= MISC_TC1_SELF_BACKGROUND_COLOR;
  547.             }
  548.         }
  549.     return self;
  550.     }
  551.  
  552.  
  553. //-----------------------------------------------------------------------------
  554. // setUseOwnerHighlightTextColor:
  555. //-----------------------------------------------------------------------------
  556. - setUseOwnerHighlightTextColor: (BOOL) flag
  557.     {
  558.     if ([self useOwnerHighlightTextColor] != flag)
  559.         {
  560.         unsigned int const pos = [self tc1HighlightTextColorPos];
  561.         unsigned int const len = [self tc1HighlightTextColorLen];
  562.         if (flag)
  563.             {
  564.             [self tc1DeleteDataPos:pos len:len];
  565.             tc1_flags &= ~(MISC_TC1_SELF_TEXT_COLOR_H);
  566.             }
  567.         else // (!flag)
  568.             {
  569.             NXColor color = [[self class] defaultHighlightTextColor];
  570.             [self tc1InsertData:&color pos:pos len:len];
  571.             tc1_flags |= MISC_TC1_SELF_TEXT_COLOR_H;
  572.             }
  573.         }
  574.     return self;
  575.     }
  576.  
  577.  
  578. //-----------------------------------------------------------------------------
  579. // setUseOwnerHighlightBackgroundColor:
  580. //-----------------------------------------------------------------------------
  581. - setUseOwnerHighlightBackgroundColor: (BOOL) flag
  582.     {
  583.     if ([self useOwnerHighlightBackgroundColor] != flag)
  584.         {
  585.         unsigned int const pos = [self tc1HighlightBackgroundColorPos];
  586.         unsigned int const len = [self tc1HighlightBackgroundColorLen];
  587.         if (flag)
  588.             {
  589.             [self tc1DeleteDataPos:pos len:len];
  590.             tc1_flags &= ~(MISC_TC1_SELF_BACKGROUND_COLOR_H);
  591.             }
  592.         else // (!flag)
  593.             {
  594.             NXColor color = [[self class] defaultHighlightBackgroundColor];
  595.             [self tc1InsertData:&color pos:pos len:len];
  596.             tc1_flags |= MISC_TC1_SELF_BACKGROUND_COLOR_H;
  597.             }
  598.         }
  599.     return self;
  600.     }
  601.  
  602.  
  603. //-----------------------------------------------------------------------------
  604. // setOwnerFont:
  605. //-----------------------------------------------------------------------------
  606. - setOwnerFont:fontObj
  607.     {
  608.     if ([self useOwnerFont])
  609.         [super setFont:fontObj];
  610.     return self;
  611.     }
  612.  
  613.  
  614. //-----------------------------------------------------------------------------
  615. // setOwner values...    NOTE *SET-OWNER-VALUE*
  616. //-----------------------------------------------------------------------------
  617.  
  618. - setOwnerTextColor: (NXColor) pcolor                    { return self; }
  619. - setOwnerBackgroundColor: (NXColor) pcolor                { return self; }
  620. - setOwnerHighlightTextColor: (NXColor) pcolor            { return self; }
  621. - setOwnerHighlightBackgroundColor: (NXColor) pcolor    { return self; }
  622.  
  623.  
  624. //-----------------------------------------------------------------------------
  625. // textGray
  626. //-----------------------------------------------------------------------------
  627. - (float)textGray
  628.     {
  629.     float gray;
  630.     NXConvertColorToGray( [self textColor], &gray );
  631.     return gray;
  632.     }
  633.  
  634.  
  635. //-----------------------------------------------------------------------------
  636. // setTextGray:
  637. //-----------------------------------------------------------------------------
  638. - setTextGray:(float)value
  639.     {
  640.     return [self setTextColor: NXConvertGrayToColor( value )];
  641.     }
  642.  
  643.  
  644. //-----------------------------------------------------------------------------
  645. // backgroundGray
  646. //-----------------------------------------------------------------------------
  647. - (float)backgroundGray
  648.     {
  649.     float gray;
  650.     NXConvertColorToGray( [self backgroundColor], &gray );
  651.     return gray;
  652.     }
  653.  
  654.  
  655. //-----------------------------------------------------------------------------
  656. // setBackgroundGray:
  657. //-----------------------------------------------------------------------------
  658. - setBackgroundGray:(float)value
  659.     {
  660.     return [self setBackgroundColor: NXConvertGrayToColor( value ) ];
  661.     }
  662.  
  663.  
  664. //-----------------------------------------------------------------------------
  665. // highlightTextGray
  666. //-----------------------------------------------------------------------------
  667. - (float)highlightTextGray
  668.     {
  669.     float gray;
  670.     NXConvertColorToGray( [self highlightTextColor], &gray );
  671.     return gray;
  672.     }
  673.  
  674.  
  675. //-----------------------------------------------------------------------------
  676. // setHighlightTextGray:
  677. //-----------------------------------------------------------------------------
  678. - setHighlightTextGray:(float)value
  679.     {
  680.     return [self setHighlightTextColor: NXConvertGrayToColor( value )];
  681.     }
  682.  
  683.  
  684. //-----------------------------------------------------------------------------
  685. // highlightBackgroundGray
  686. //-----------------------------------------------------------------------------
  687. - (float)highlightBackgroundGray
  688.     {
  689.     float gray;
  690.     NXConvertColorToGray( [self highlightBackgroundColor], &gray );
  691.     return gray;
  692.     }
  693.  
  694.  
  695. //-----------------------------------------------------------------------------
  696. // setHighlightBackgroundGray:
  697. //-----------------------------------------------------------------------------
  698. - setHighlightBackgroundGray:(float)value
  699.     {
  700.     return [self setHighlightBackgroundColor: NXConvertGrayToColor( value ) ];
  701.     }
  702.  
  703.  
  704. //-----------------------------------------------------------------------------
  705. // +defaultBackgroundColor
  706. //-----------------------------------------------------------------------------
  707. + (NXColor) defaultBackgroundColor
  708.     {
  709.     return NX_COLORLTGRAY;
  710.     }
  711.  
  712.  
  713. //-----------------------------------------------------------------------------
  714. // backgroundColor
  715. //-----------------------------------------------------------------------------
  716. - (NXColor) backgroundColor
  717.     {
  718.     if ([self useOwnerBackgroundColor])
  719.         {
  720.         if (owner != 0 && [owner respondsTo:@selector(backgroundColor)])
  721.             return [owner backgroundColor];
  722.         return [[self class] defaultBackgroundColor];
  723.         }
  724.     NXColor const* const p = [self tc1BackgroundColorPtr];
  725.     return *p;
  726.     }
  727.  
  728.  
  729. //-----------------------------------------------------------------------------
  730. // setBackgroundColor
  731. //-----------------------------------------------------------------------------
  732. - setBackgroundColor: (NXColor) c
  733.     {
  734.     NXColor* p;
  735.     [self setUseOwnerBackgroundColor:NO];
  736.     p = [self tc1BackgroundColorPtr];
  737.     *p = c;
  738.     return self;
  739.     }
  740.  
  741.  
  742. //-----------------------------------------------------------------------------
  743. // +defaultHighlightBackgroundColor
  744. //-----------------------------------------------------------------------------
  745. + (NXColor) defaultHighlightBackgroundColor
  746.     {
  747.     return NX_COLORWHITE;
  748.     }
  749.  
  750.  
  751. //-----------------------------------------------------------------------------
  752. // highlightBackgroundColor
  753. //-----------------------------------------------------------------------------
  754. - (NXColor) highlightBackgroundColor
  755.     {
  756.     if ([self useOwnerHighlightBackgroundColor])
  757.         {
  758.         if (owner && [owner respondsTo:@selector(highlightBackgroundColor)])
  759.             return [owner highlightBackgroundColor];
  760.         return [[self class] defaultHighlightBackgroundColor];
  761.         }
  762.     NXColor const* const p = [self tc1HighlightBackgroundColorPtr];
  763.     return *p;
  764.     }
  765.  
  766.  
  767. //-----------------------------------------------------------------------------
  768. // setHighlightBackgroundColor
  769. //-----------------------------------------------------------------------------
  770. - setHighlightBackgroundColor: (NXColor) c
  771.     {
  772.     NXColor* p;
  773.     [self setUseOwnerHighlightBackgroundColor:NO];
  774.     p = [self tc1HighlightBackgroundColorPtr];
  775.     *p = c;
  776.     return self;
  777.     }
  778.  
  779.  
  780. //-----------------------------------------------------------------------------
  781. // +defaultTextColor
  782. //-----------------------------------------------------------------------------
  783. + (NXColor) defaultTextColor
  784.     {
  785.     return NX_COLORBLACK;
  786.     }
  787.  
  788.  
  789. //-----------------------------------------------------------------------------
  790. // textColor
  791. //-----------------------------------------------------------------------------
  792. - (NXColor) textColor
  793.     {
  794.     if ([self useOwnerTextColor])
  795.         {
  796.         if (owner != 0 && [owner respondsTo:@selector(textColor)])
  797.             return [owner textColor];
  798.         return [[self class] defaultTextColor];
  799.         }
  800.     NXColor const* const p = [self tc1TextColorPtr];
  801.     return *p;
  802.     }
  803.  
  804.  
  805. //-----------------------------------------------------------------------------
  806. // setTextColor
  807. //-----------------------------------------------------------------------------
  808. - setTextColor: (NXColor) c
  809.     {
  810.     NXColor* p;
  811.     [self setUseOwnerTextColor:NO];
  812.     p = [self tc1TextColorPtr];
  813.     *p = c;
  814.     return self;
  815.     }
  816.  
  817.  
  818. //-----------------------------------------------------------------------------
  819. // +defaultHighlightTextColor
  820. //-----------------------------------------------------------------------------
  821. + (NXColor) defaultHighlightTextColor
  822.     {
  823.     return NX_COLORBLACK;
  824.     }
  825.  
  826.  
  827. //-----------------------------------------------------------------------------
  828. // highlightTextColor
  829. //-----------------------------------------------------------------------------
  830. - (NXColor) highlightTextColor
  831.     {
  832.     if ([self useOwnerHighlightTextColor])
  833.         {
  834.         if (owner != 0 && [owner respondsTo:@selector(highlightTextColor)])
  835.             return [owner highlightTextColor];
  836.         return [[self class] defaultHighlightTextColor];
  837.         }
  838.     NXColor const* const p = [self tc1HighlightTextColorPtr];
  839.     return *p;
  840.     }
  841.  
  842.  
  843. //-----------------------------------------------------------------------------
  844. // setHighlightTextColor
  845. //-----------------------------------------------------------------------------
  846. - setHighlightTextColor: (NXColor) c
  847.     {
  848.     NXColor* p;
  849.     [self setUseOwnerHighlightTextColor:NO];
  850.     p = [self tc1HighlightTextColorPtr];
  851.     *p = c;
  852.     return self;
  853.     }
  854.  
  855.  
  856. //-----------------------------------------------------------------------------
  857. // bgColor
  858. //-----------------------------------------------------------------------------
  859. - (NXColor) bgColor
  860.     {
  861.     // FIXME: Use [self isHighlighted] when we figure out how to support
  862.     // this for lazy mode.
  863.     return cFlags1.state ?
  864.                 [self highlightBackgroundColor] : [self backgroundColor];
  865.     }
  866.  
  867.  
  868. //-----------------------------------------------------------------------------
  869. // fgColor
  870. //-----------------------------------------------------------------------------
  871. - (NXColor) fgColor
  872.     {
  873.     // FIXME: Use [self isHighlighted] when we figure out how to support
  874.     // this for lazy mode.
  875.     return cFlags1.state ? [self highlightTextColor] : [self textColor];
  876.     }
  877.  
  878.  
  879. //-----------------------------------------------------------------------------
  880. // calcCellSize:inRect:
  881. //-----------------------------------------------------------------------------
  882. - calcCellSize: (NXSize*) theSize inRect: (NXRect const*) aRect
  883.     {
  884.     [super calcCellSize: theSize inRect: aRect];
  885.     theSize->width += 1;
  886.     theSize->height += 1;
  887.     return self;
  888.     }
  889.  
  890.  
  891. //-----------------------------------------------------------------------------
  892. // setTextAttributes:
  893. //-----------------------------------------------------------------------------
  894. - setTextAttributes:textObject
  895.     {
  896.     [super setTextAttributes: textObject];
  897.     [textObject setTextColor: [self fgColor]];
  898.     [textObject setBackgroundColor: [self bgColor]];
  899.     return textObject;
  900.     }
  901.  
  902.  
  903. //-----------------------------------------------------------------------------
  904. // drawInside:inView:
  905. //-----------------------------------------------------------------------------
  906. - drawInside:(NXRect const*)cellFrame inView:controlView
  907.     {
  908.     NXRect rect;
  909.  
  910.     NXSetColor( [self bgColor] );
  911.     NXSetRect( &rect, NX_X(cellFrame), NX_Y(cellFrame),
  912.                    NX_WIDTH(cellFrame) - 1, NX_HEIGHT(cellFrame) - 1 );
  913.     NXRectFill( &rect );
  914.     [super drawInside:&rect inView:controlView];
  915.     return self;
  916.     }
  917.  
  918.  
  919. //-----------------------------------------------------------------------------
  920. // drawSelf:inView:
  921. //-----------------------------------------------------------------------------
  922. - drawSelf:(NXRect const*)cellFrame inView:aView
  923.     {
  924.     float const MEDGRAY = 0.5;
  925.     NXRect r[2];
  926.  
  927.     PSsetgray( MEDGRAY );
  928.     NXSetRect( &(r[0]), NX_MAXX(cellFrame) - 1, NX_Y(cellFrame),
  929.                         1, NX_HEIGHT(cellFrame) );
  930.     NXSetRect( &(r[1]), NX_X(cellFrame), NX_MAXY(cellFrame) - 1,
  931.                         NX_WIDTH(cellFrame), 1 );
  932.     NXRectFillList( r, 2 );
  933.  
  934.     return [self drawInside:cellFrame inView:aView];
  935.     }
  936.  
  937.  
  938. //-----------------------------------------------------------------------------
  939. // tc1DestroyData
  940. //-----------------------------------------------------------------------------
  941. - (void) tc1DestroyData
  942.     {
  943.     tc1_flags = 0;
  944.     }
  945.  
  946.  
  947. //-----------------------------------------------------------------------------
  948. // tc1FreeData
  949. //-----------------------------------------------------------------------------
  950. - (void) tc1FreeData
  951.     {
  952.     if (tc1_data != 0)
  953.         {
  954.         NXZoneFree( [self zone], tc1_data );
  955.         tc1_data = 0;
  956.         }
  957.     }
  958.  
  959.  
  960. //-----------------------------------------------------------------------------
  961. // free
  962. //-----------------------------------------------------------------------------
  963. - free
  964.     {
  965.     [self tc1DestroyData];
  966.     [self tc1FreeData];
  967.     return [super free];
  968.     }
  969.  
  970.  
  971. //-----------------------------------------------------------------------------
  972. // copyFromZone:
  973. //-----------------------------------------------------------------------------
  974. - copyFromZone: (NXZone*) zone;
  975.     {
  976.     MiscTableCell* the_clone = [super copyFromZone:zone];
  977.     if (tc1_data != 0)
  978.         {
  979.         unsigned int const size = [self tc1DataSize];
  980.         void* p = NXZoneMalloc( [self zone], size );
  981.         memcpy( p, tc1_data, size );
  982.         the_clone->tc1_data = p;
  983.         }
  984.     return the_clone;
  985.     }
  986.  
  987.  
  988. //-----------------------------------------------------------------------------
  989. // read:
  990. //-----------------------------------------------------------------------------
  991. - read: (NXTypedStream*) stream
  992.     {
  993.     [super read:stream];
  994.  
  995.     [self tc1DestroyData];
  996.     [self tc1FreeData];
  997.  
  998.     unsigned int x;
  999.     int ver = NXTypedStreamClassVersion( stream, [[MiscTableCell class] name] );
  1000.  
  1001.     owner = NXReadObject( stream );
  1002.  
  1003.     if (ver == MISC_TC_VERSION_0)
  1004.         {
  1005.         NXReadType( stream, @encode(unsigned int), &x );
  1006.         if (x & MISC_TC1_HAS_TAG)
  1007.             NXReadType( stream, @encode(int), &tag );
  1008.         else
  1009.             tag = 0;
  1010.         }
  1011.     else if (ver == MISC_TC_VERSION_1)
  1012.         {
  1013.         NXReadType( stream, @encode(int), &tag );
  1014.         NXReadType( stream, @encode(unsigned int), &x );
  1015.         }
  1016.     else
  1017.         {
  1018.         [self error:"Cannot read: unknown version."];
  1019.         }
  1020.  
  1021.     if (x & MISC_TC1_SELF_TEXT_COLOR)
  1022.         {
  1023.         NXColor c = NXReadColor( stream );
  1024.         [self setTextColor:c];
  1025.         }
  1026.     if (x & MISC_TC1_SELF_BACKGROUND_COLOR)
  1027.         {
  1028.         NXColor c = NXReadColor( stream );
  1029.         [self setBackgroundColor:c];
  1030.         }
  1031.     if (x & MISC_TC1_SELF_TEXT_COLOR_H)
  1032.         {
  1033.         NXColor c = NXReadColor( stream );
  1034.         [self setHighlightTextColor:c];
  1035.         }
  1036.     if (x & MISC_TC1_SELF_BACKGROUND_COLOR_H)
  1037.         {
  1038.         NXColor c = NXReadColor( stream );
  1039.         [self setHighlightBackgroundColor:c];
  1040.         }
  1041.     return self;
  1042.     }
  1043.  
  1044.  
  1045. //-----------------------------------------------------------------------------
  1046. // write:
  1047. //-----------------------------------------------------------------------------
  1048. - write: (NXTypedStream*) stream
  1049.     {
  1050.     [super write:stream];
  1051.     NXWriteObjectReference( stream, owner );
  1052.     NXWriteType( stream, @encode(int), &tag );
  1053.     NXWriteType( stream, @encode(unsigned int), &tc1_flags );
  1054.     if (![self useOwnerTextColor])
  1055.         NXWriteColor( stream, [self textColor] );
  1056.     if (![self useOwnerBackgroundColor])
  1057.         NXWriteColor( stream, [self backgroundColor] );
  1058.     if (![self useOwnerHighlightTextColor])
  1059.         NXWriteColor( stream, [self highlightTextColor] );
  1060.     if (![self useOwnerHighlightBackgroundColor])
  1061.         NXWriteColor( stream, [self highlightBackgroundColor] );
  1062.     return self;
  1063.     }
  1064.  
  1065. @end
  1066.