home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / Palettes / ConnectDemo / MColCell.m < prev    next >
Encoding:
Text File  |  1993-01-19  |  3.5 KB  |  211 lines

  1.  
  2. #import "MColCell.h"
  3.  
  4. typedef    struct    _Column
  5. {
  6.     unsigned short    offset;
  7.     BOOL            copy;
  8.     const char        *value;
  9. } Column;
  10. #define    Column_desc        "SC*"
  11.  
  12. @implementation MColCell
  13.  
  14. - init
  15. {
  16.     if (![super init])
  17.         return nil;
  18.     [self initMColCell];
  19.  
  20.     return self;
  21. }
  22.  
  23. - initTextCell:(const char *)string
  24. {
  25.     Column    col;
  26.  
  27.     if (![super initTextCell:string])
  28.         return nil;
  29.     [self initMColCell];
  30.     col.offset = 0;
  31.     col.copy = YES;
  32.     if (string)
  33.         col.value = NXCopyStringBuffer(string);
  34.     else
  35.         col.value = NULL;
  36.     [stringValues addElement:&col];
  37.  
  38.     return self;
  39. }
  40.  
  41. - initIconCell:(const char *)string
  42. {
  43.     if (![super initIconCell:string])
  44.         return nil;
  45.     [self initMColCell];
  46.  
  47.     return self;
  48. }
  49.  
  50. - initMColCell
  51. {
  52.     stringValues = [[Storage alloc] initCount:0 elementSize:sizeof(Column)
  53.         description:Column_desc];
  54.     return self;
  55. }
  56.  
  57. - free
  58. {
  59.     int        count = [stringValues count];
  60.     int        ind;
  61.     Column    *col;
  62.  
  63.     for (ind=0; ind<count; ind++)
  64.     {
  65.         col = [stringValues elementAt:ind];
  66.         if (col->copy)
  67.             free((char *)col->value);
  68.     }
  69.     [stringValues free];
  70.  
  71.     return [super free];
  72. }
  73.  
  74. - setStringValue:(const char *)string at:(int)col copy:(BOOL)copy
  75. {
  76.     int        count = [stringValues count];
  77.     Column    *column;
  78.  
  79.     if (col>=count || col<0)
  80.         return nil;
  81.     column = [stringValues elementAt:col];
  82.     if (column->copy)
  83.         free((char *)column->value);
  84.     if (copy && string!=NULL)
  85.         column->value = NXCopyStringBuffer(string);
  86.     else
  87.         column->value = string;
  88.     column->copy = copy;
  89.         col = count;
  90.  
  91.     return self;
  92. }
  93.  
  94. - (const char *)stringValueAt:(int)col
  95. {
  96.     int        count = [stringValues count];
  97.     Column    *column;
  98.  
  99.     if (col>=count || col<0)
  100.         return NULL;
  101.     column = [stringValues elementAt:col];
  102.     return column->value;
  103. }
  104.  
  105. - insertColAt:(int)col offset:(int)offset
  106. {
  107.     int        count = [stringValues count];
  108.     Column    column;
  109.  
  110.     if (col > count)
  111.         col = count;
  112.     else if (col < 0)
  113.         col = 0;
  114.     column.offset = offset;
  115.     column.copy = NO;
  116.     column.value = NULL;
  117.     [stringValues insertElement:&column at:col];
  118.  
  119.     return self;
  120. }
  121.  
  122. - addColWithOffset:(int)offset
  123. {
  124.     return [self insertColAt:[stringValues count] offset:offset];
  125. }
  126.  
  127. - removeColAt:(int)col
  128. {
  129.     int        count = [stringValues count];
  130.     Column    *column;
  131.  
  132.     if (col>=count || col<0)
  133.         return nil;
  134.     column = [stringValues elementAt:col];
  135.     if (column->copy)
  136.         free((char *)column->value);
  137.     [stringValues removeElementAt:col];
  138.  
  139.     return self;
  140. }
  141.  
  142. - (int)colCount
  143. {
  144.     return [stringValues count];
  145. }
  146.  
  147. - drawInside:(const NXRect *)cellFrame inView:controlView
  148. {
  149.     NXCoord        baseX,baseY;
  150.     Column        *column;
  151.     int            count = [stringValues count];
  152.     int            ind;
  153.  
  154.     baseX = NX_X(cellFrame) + 4;
  155.     baseY = NX_Y(cellFrame) + 12;
  156.  
  157.     PSsetgray((cFlags1.state || cFlags1.highlighted) ? NX_WHITE : NX_LTGRAY);
  158.     NXRectFill(cellFrame);
  159.     PSsetgray(NX_BLACK);
  160.     [[support screenFont] set];
  161.  
  162.     for (ind=0; ind<count; ind++)
  163.     {
  164.         column = [stringValues elementAt:ind];
  165.         if (column->value != NULL)
  166.         {
  167.             PSmoveto(baseX+column->offset,baseY);
  168.             PSshow(column->value);
  169.         }
  170.     }
  171.  
  172.     return self;
  173. }
  174.  
  175. - copyFromZone:(NXZone *)zone
  176. {
  177.     MColCell    *copy;
  178.     int            count;
  179.     int            ind;
  180.     Column        *column;
  181.  
  182.     copy = [super copyFromZone:zone];
  183.     copy->stringValues = [stringValues copyFromZone:zone];
  184.  
  185.     count = [copy->stringValues count];
  186.     for (ind=0; ind<count; ind++)
  187.     {
  188.         column = [copy->stringValues elementAt:ind];
  189.         if (column->copy && column->value)
  190.             column->value = NXCopyStringBuffer(column->value);
  191.     }
  192.  
  193.     return copy;
  194. }
  195.  
  196. - read:(NXTypedStream *)stream
  197. {
  198.     [super read:stream];
  199.     stringValues = NXReadObject(stream);
  200.     return self;
  201. }
  202.  
  203. - write:(NXTypedStream *)stream
  204. {
  205.     [super write:stream];
  206.     NXWriteObject(stream,stringValues);
  207.     return self;
  208. }
  209.  
  210. @end
  211.