home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / YellowBox / Kits / MiscTableScroll-138.1 / Palettes / MiscTableScroll / Framework / MiscBorderCell.M < prev    next >
Encoding:
Text File  |  1998-03-31  |  5.0 KB  |  158 lines

  1. //=============================================================================
  2. //
  3. //  Copyright (C) 1995,1996,1997,1998 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. // MiscBorderCell.M
  17. //
  18. //    Cell class used by MiscBorderView to manage column headings and row 
  19. //    labels for MiscTableScroll.  
  20. //
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // $Id: MiscBorderCell.M,v 1.7 98/03/29 23:38:17 sunshine Exp $
  24. // $Log:    MiscBorderCell.M,v $
  25. //  Revision 1.7  98/03/29  23:38:17  sunshine
  26. //  v138.1: Now derived from NSTableHeaderCell rather than NSTextFieldCell
  27. //  so that it knows how to correctly draw and color itself on all platforms
  28. //  rather than only being able to draw itself in a NextStep-like fashion.
  29. //  
  30. //  Revision 1.6  97/03/23  05:45:38  sunshine
  31. //  v125.4: Worked around OPENSTEP 4.1 bug where -setupFieldEditorAttributes:
  32. //  never gets called, so text was not drawing white.  Worked around problem by
  33. //  subclassing from NSTextFieldCell rather than NSCell.  This way color can
  34. //  be set with -setTextColor:.
  35. //-----------------------------------------------------------------------------
  36. #import "MiscBorderCell.h"
  37. extern "Objective-C" {
  38. #import <AppKit/NSFont.h>
  39. #import <AppKit/NSImage.h>
  40. }
  41.  
  42. int const RESIZE_WIDTH = 4; // Resize zone to right of "toggle" image.
  43.  
  44. @implementation MiscBorderCell
  45.  
  46. //-----------------------------------------------------------------------------
  47. // -setToggleImage:
  48. //-----------------------------------------------------------------------------
  49. - (void)setToggleImage:(NSImage*)p
  50.     {
  51.     if (p != toggleImage)
  52.     {
  53.     [toggleImage release];
  54.     toggleImage = [p retain];
  55.     }
  56.     }
  57.  
  58.  
  59. //-----------------------------------------------------------------------------
  60. // -initBorderCell
  61. //-----------------------------------------------------------------------------
  62. - (void)initBorderCell
  63.     {
  64.     toggleImage = 0;
  65.     [self setBordered:YES];
  66.     [self setWraps:NO];
  67.     [self setAlignment:NSCenterTextAlignment];
  68.     }
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // -initTextCell:
  73. //-----------------------------------------------------------------------------
  74. - (id)initTextCell:(NSString*)s
  75.     {
  76.     [super initTextCell:s];
  77.     [self initBorderCell];
  78.     return self;
  79.     }
  80.  
  81.  
  82. //-----------------------------------------------------------------------------
  83. // -initImageCell:
  84. //-----------------------------------------------------------------------------
  85. - (id)initImageCell:(NSImage*)p
  86.     {
  87.     [super initImageCell:p];
  88.     [self initBorderCell];
  89.     return self;
  90.     }
  91.  
  92.  
  93. //-----------------------------------------------------------------------------
  94. // -cellSizeForBounds:
  95. //-----------------------------------------------------------------------------
  96. - (NSSize)cellSizeForBounds:(NSRect)aRect
  97.     {
  98.     NSSize s = [super cellSizeForBounds:aRect];
  99.     if (toggleImage != 0)
  100.     s.width += [toggleImage size].width;
  101.     return s;
  102.     }
  103.  
  104.  
  105. //-----------------------------------------------------------------------------
  106. // -drawInteriorWithFrame:inView: -- Assumes the view is flipped.
  107. //-----------------------------------------------------------------------------
  108. - (void)drawInteriorWithFrame:(NSRect)r inView:(NSView*)v
  109.     {
  110.     if (toggleImage != 0)
  111.     {
  112.     NSSize sz = [toggleImage size];
  113.     NSPoint pt = { NSMaxX(r) - RESIZE_WIDTH, NSMaxY(r) };
  114.  
  115.     float const max_height = floor( NSHeight(r) );
  116.     float const max_width = floor( NSWidth(r) - RESIZE_WIDTH );
  117.  
  118.     BOOL const too_high = sz.height > max_height;
  119.     BOOL const too_wide = sz.width > max_width;
  120.     if (too_high || too_wide)
  121.         {
  122.         NSRect q = { {0,0}, {sz.width, sz.height} };
  123.         if (too_high)
  124.         {
  125.         q.size.height = max_height;
  126.         q.origin.y = floor( (sz.height - max_height) / 2 );
  127.         }
  128.         else
  129.         {
  130.         // Center image vertically.
  131.         pt.y = floor( pt.y - ((max_height - sz.height) / 2) );
  132.         }
  133.         if (too_wide)
  134.         {
  135.         q.size.width = max_width;
  136.         q.origin.x = floor( (sz.width - max_width) / 2 );
  137.         }
  138.         sz = q.size;
  139.         pt.x -= sz.width;
  140.         [toggleImage compositeToPoint:pt fromRect:q
  141.                 operation:NSCompositeSourceOver];
  142.         }
  143.     else
  144.         {
  145.         // Center image vertically.
  146.         pt.y = floor( pt.y - ((max_height - sz.height) / 2) );
  147.         pt.x -= sz.width;
  148.         [toggleImage compositeToPoint:pt operation:NSCompositeSourceOver];
  149.         }
  150.  
  151.     r.size.width -= (sz.width + RESIZE_WIDTH);
  152.     }
  153.  
  154.     [super drawInteriorWithFrame:r inView:v];
  155.     }
  156.  
  157. @end
  158.