home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / ClassEditor.0.4 / Source / MiscSources.subproj / Text_MiscExtensions.m < prev    next >
Encoding:
Text File  |  1995-06-07  |  4.0 KB  |  234 lines

  1. /* Text_MiscExtensions.m                 
  2.  *
  3.  * This category makes dealing with the Text object easier. 
  4.  * See the docu for details.
  5.  *
  6.  * For interface-info see the header file. The comments in this file mostly
  7.  * cover only the real implementation details.
  8.  *
  9.  * Written by:         Thomas Engel
  10.  * Created:            30.01.1995 (Copyleft)
  11.  * Last modified:     30.01.1995
  12.  */
  13.  
  14. // The following define is a fake. Looks like NeXT...but it is a cheatmode
  15. // action.
  16. // We don't have a RTFD paste type. But I really need one !
  17.  
  18. #define NXRTFDPboardType NXUniqueString("NeXT RTFD pasteboard type")
  19.  
  20.  
  21. #import "Text_MiscExtensions.h"
  22. #import <misckit/MiscTextExtensions.h>
  23.  
  24. @implementation Text(MoreMiscExtensions)
  25.  
  26. - copyTo:aPasteboard
  27. {
  28.     NXAtom textTypes[5];
  29.  
  30.     if( [self isMonoFont] )
  31.     {
  32.         textTypes[0] = NXAsciiPboardType;
  33.         textTypes[1] = NULL;
  34.     }
  35.     // In the other case we will cheat. The RTFD type was NEVER defined by
  36.     // NeXT. But we will use it...it works. (see the define)
  37.  
  38.     else
  39.     {
  40.         textTypes[0] = NXRTFDPboardType;
  41.         textTypes[1] = NXTIFFPboardType;
  42.         textTypes[2] = NXRTFPboardType;
  43.         textTypes[3] = NXAsciiPboardType;
  44.         textTypes[4] = NULL;
  45.     }
  46.     [self writeSelectionToPasteboard:aPasteboard types:textTypes];
  47.     return self;
  48. }
  49.  
  50. - cutTo:aPasteboard
  51. {
  52.     [self copyTo:aPasteboard];
  53.     [self delete:self];
  54.     return self;
  55. }
  56.  
  57. - pasteFrom:aPasteboard
  58. {
  59.     NXSelPt    start;
  60.     NXSelPt    end;
  61.  
  62.     if( [self hasInvalidSelection] )
  63.         [self setSelToEnd];
  64.     [self readSelectionFromPasteboard:aPasteboard];
  65.  
  66.     // Now set the cursor to the last char of the current paste.
  67.     
  68.     [self getSel:&start :&end];
  69.     [self setSel:end.cp :end.cp];
  70.     
  71.     return self;
  72. }
  73.  
  74. - (BOOL)hasEmptySelection
  75. {
  76.     NXSelPt    start;
  77.     NXSelPt    end;
  78.  
  79.     [self getSel:&start :&end];
  80.     
  81.     if( start.cp == end.cp || 
  82.         start.cp < 0 )
  83.         return YES;
  84.     
  85.     return NO;
  86. }
  87.  
  88. - (BOOL)hasInvalidSelection
  89. {
  90.     NXSelPt    start;
  91.     NXSelPt    end;
  92.  
  93.     [self getSel:&start :&end];
  94.     
  95.     if( start.cp < 0 ||
  96.         end.cp < 0 )
  97.         return YES;
  98.     
  99.     return NO;
  100. }
  101.  
  102. - setSelToStart
  103. {
  104.     return [self setSel:0 :0];
  105. }
  106.  
  107. - setSelToEnd
  108. {
  109.     return [self setSel:[self textLength] :[self textLength]];
  110. }
  111.  
  112. - selectTrueLine:(int)line;
  113. {
  114.     int    start;
  115.     int    stop;
  116.     
  117.     // To be
  118.     
  119.     if( line < 1 )
  120.     {
  121.         [self setSelToStart];
  122.         return nil;
  123.     }
  124.     
  125.     start = [self positionFromLine:line];
  126.     stop = [self positionFromLine:line+1];
  127.     
  128.     if( stop < start )
  129.         stop = [self textLength];
  130.         
  131.     [self setSel: start :stop];
  132.     return self;
  133. }
  134.  
  135. - findPerviousWord
  136. {
  137.     return self;
  138. }
  139.  
  140. - findNextWord
  141. {
  142.     return self;
  143. }
  144.  
  145. - (BOOL)findText:(const char *)string ignoreCase:(BOOL)ignoreCaseflag backwards:(BOOL)backwardsflag wrap:(BOOL)wrapflag font:aSharedFont
  146. {
  147.     BOOL        gotIt;
  148.     NXSelPt    fromPos;
  149.     NXSelPt    toPos;
  150.     int        firstHit;
  151.  
  152.     firstHit = -2;
  153.     gotIt = NO;
  154.  
  155.     while( !gotIt )
  156.     {
  157.         gotIt = [self findText:string ignoreCase:ignoreCaseflag
  158.                      backwards:NO wrap:NO];
  159.     
  160.         // Now we should get the position of the found seleection.
  161.         // we have to remember the first place where we have found something to
  162.         // trace down inf-loops when we are allowed to do wraps!
  163.  
  164.         [self getSel:&fromPos :&toPos];
  165.  
  166.         if( !gotIt ||
  167.             fromPos.cp == firstHit ) return NO;
  168.         
  169.         firstHit = fromPos.cp;
  170.  
  171.         // Now it seems like we have found the right string but
  172.         // still this does not have to be the right font
  173.  
  174.         if( [self fontAtPosition:fromPos.cp] != aSharedFont ) gotIt = NO;
  175.     }
  176.     return YES;
  177. }
  178.  
  179. - substringFromLine:(int)line
  180. {
  181.     int    from;
  182.     int    length;
  183.  
  184.     from = [self positionFromLine:line];
  185.     length = [self positionFromLine:line+1] - from;
  186.  
  187.     return [self getSubstringStringStart:from length:length];
  188. }
  189.  
  190. // Working with Text runs and their information
  191.  
  192. - (NXRun *)textRunForPosition:(int)position
  193. {
  194.     BOOL     found;
  195.     int    i;
  196.     int    currentOffset;
  197.     
  198.     currentOffset = 0;
  199.     found = NO;
  200.  
  201.     for( i=0; i<theRuns->chunk.used; i++ )
  202.     {
  203.         if( currentOffset >= position )
  204.         {
  205.             found = YES;
  206.             break;
  207.         }
  208.         currentOffset += theRuns->runs[i].chars;
  209.     }
  210.  
  211.     return &(theRuns->runs[i]);
  212. }
  213.  
  214. - fontsInsideSelection
  215. {
  216.     return self;
  217. }
  218.  
  219. - fontAtPosition:(int)position
  220. {
  221.     NXRun *aRun;
  222.  
  223.     aRun = [self textRunForPosition:position];
  224.     return aRun->font;
  225. }
  226.  
  227. @end
  228.  
  229. /*
  230.  * History: 30.01.95 Buh
  231.  *            
  232.  *
  233.  * Bugs: - ...
  234.  */