home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Starter / Source / AdjFontTextField.m < prev    next >
Encoding:
Text File  |  1994-09-27  |  2.6 KB  |  110 lines

  1. /* 
  2.     AdjFontTextField class, written by Robert Vasvari
  3.     
  4.     this class displays a string of any length by
  5.        first: cutting down the size of the font
  6.           until it reaches the specified minimum font
  7.        second: the text will be truncated until it fits
  8.            into the textfield
  9.  
  10.  
  11.     setup: [adjFontTextObject setFont:"SomeFontName"];
  12.            [adjFontTextObject setAttributes:minsize :maxsize
  13.             align:NX_CENTERED
  14.             lines:1];
  15.     the numbers minsize and maxsize are indices in the array 
  16.     containing the known font sizes: 8,9,10,12,14, etc...
  17.     So, 0 stands for point size 8, 1 for 9 etc...
  18.     
  19.     AdjFontTextField will always try to use the maximum font size.
  20.     AdjFontTextField will use the specified alignment, such
  21.     as NX_LEFTALIGNED, NX_CENTERED, etc.
  22.     
  23.     textGray defaults to NX_BLACK,
  24.     backgroundGraydefaults to NX_LTGRAY
  25. */
  26.  
  27. #import <stdio.h>
  28. #import <stdlib.h>
  29. #import "AdjFontTextField.h"
  30.  
  31. #define BUFSIZE 1024
  32. #define ELLIPSIS ('\274')
  33.  
  34. @implementation AdjFontTextField
  35.  
  36. - initFrame:(const NXRect *)f
  37. {
  38.     [super initFrame:f];
  39.     [self setSelectable:NO];
  40.     [self setBordered:NO];
  41.     [self setBackgroundGray:NX_LTGRAY];
  42.     [self setTextGray:NX_BLACK];
  43.     return self;
  44. }
  45.  
  46. - setFontString:(const char *)aFont
  47. {    fontString=aFont;
  48.     font=[self font];
  49.     return self;
  50. }
  51.  
  52. - setAttributes:(int)min :(int)max
  53.     align:(int)aMode
  54.     lines:(int)numLines
  55. {    minSize=min;
  56.     maxSize=max;
  57.     curSize=0;
  58.     [cell setAlignment:aMode];
  59.     lines=numLines;
  60.     return self;
  61. }
  62.  
  63. - setStringValue:(const char *)text
  64. {
  65. static float sizes[]={8.0,9.0,10.0,11.0,12.0,14.0,16.0,18.0,
  66.                       24.0,36.0,48.0,64.0};
  67. NXSize s;
  68. NXRect r=frame;
  69.     r.size.height=36.0;
  70.     [super setStringValue:text];
  71.  
  72.     if(curSize!=maxSize)
  73.       { font=[Font newFont:fontString
  74.                   size:sizes[maxSize]
  75.                   matrix:NX_FLIPPEDMATRIX];
  76.        [self setFont:font];
  77.        curSize=maxSize;
  78.       }
  79.     
  80.     [cell calcCellSize:&s inRect:&r];
  81.     
  82.     while(curSize>0 && (int)(s.height/sizes[curSize])>lines)
  83.      { curSize--;
  84.        font=[Font newFont:fontString
  85.                   size:sizes[curSize]
  86.                   matrix:NX_FLIPPEDMATRIX];
  87.        [self setFont:font];
  88.        [cell calcCellSize:&s inRect:&r];
  89.      }
  90.     
  91.     if(!curSize)
  92.          { /* the text is still too large let's cut it down */
  93.            char *p, shortNameBuf[BUFSIZE+1]="";
  94.            strcpy(shortNameBuf,text);
  95.            p=strchr(shortNameBuf,'\0');
  96.            while((int)(s.height/sizes[curSize])>lines)
  97.               { p--;
  98.                 *p=ELLIPSIS; p[1]='\0';
  99.                 [super setStringValue:shortNameBuf];
  100.                 [cell calcCellSize:&s inRect:&r];
  101.               }
  102.            [cell calcCellSize:&s inRect:&r];      
  103.          }
  104.     [self sizeTo:bounds.size.width :s.height];
  105.     return self;
  106. }
  107.  
  108.  
  109. @end
  110.