home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Programming / GDBbundle-1.0-MIS / src / TextEdit / LineAndCharacterRange.m < prev    next >
Encoding:
Text File  |  1997-04-20  |  3.3 KB  |  130 lines

  1. /* LineAndCharacterRange.m created by ovidiu on Sat 29-Mar-1997 */
  2.  
  3. #import <AppKit/AppKit.h>
  4. #import "Document.h"
  5. #import "LineAndCharacterRange.h"
  6.  
  7. @implementation LineAndCharacterRange
  8.  
  9. // Class variables
  10. static id sharedInstance = nil;
  11.  
  12. + sharedInstance
  13. {
  14.     if (!sharedInstance) {
  15.         sharedInstance = [self new];
  16.         if (![NSBundle loadNibNamed:@"LineRange" owner:sharedInstance]) {
  17.             NSLog(@"Failed to load LineRange.nib");
  18.             [sharedInstance release];
  19.             sharedInstance = nil;
  20.             return nil;
  21.         }
  22.     }
  23.  
  24.     return sharedInstance;
  25. }
  26.  
  27. - init
  28. {
  29.     [super init];
  30.  
  31.     if (!sharedInstance)
  32.         sharedInstance = self;
  33.     else
  34.         [self release];
  35.  
  36.     return sharedInstance;
  37. }
  38.  
  39. - (void)awakeFromNib
  40. {
  41.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  42.  
  43.     useLineRange = [defaults boolForKey:@"LineRange"];
  44.     autoupdate = [defaults boolForKey:@"AutoUpdate"];
  45.     [lineCharacterMatrix selectCellAtRow:!useLineRange column:0];
  46.     [autoUpdateButton setState:autoupdate];
  47.     [[rangeField window] setFrameAutosaveName:@"Line Range Window"];
  48. }
  49.  
  50. - (void)buttonsChanged:sender
  51. {
  52.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  53.  
  54.     [defaults setBool:![lineCharacterMatrix selectedRow] forKey:@"LineRange"];
  55.     [defaults setBool:[autoUpdateButton state] forKey:@"AutoUpdate"];
  56. }
  57.  
  58. - (void)select:sender
  59. {
  60.     Document* document = [Document currentDocument];
  61.     NSArray* selectionArray = [[rangeField stringValue]
  62.                                 componentsSeparatedByString:@":"];
  63.     int a = -1, b = -1, count = [selectionArray count];
  64.  
  65.     a = [[selectionArray objectAtIndex:0] intValue];
  66.     if (count == 2)
  67.         b = [[selectionArray objectAtIndex:1] intValue];
  68.  
  69.     if (b == -1)
  70.         b = a;
  71.     else if (a > b) {
  72.         int tmp = a;
  73.         a = b;
  74.         b = tmp;
  75.     }
  76.  
  77.     if (a < 0 && b < 0) {
  78.         [[lineCharacterMatrix window] orderOut:nil];
  79.         return;
  80.     }
  81.  
  82.     if (![lineCharacterMatrix selectedRow])
  83.         [document selectFromLine:a toEndLine:b];
  84.     else
  85.         [document selectFromChar:a toEndChar:b];
  86.  
  87.     [[lineCharacterMatrix window] orderOut:nil];
  88. }
  89.  
  90. - (void)updateInfo
  91. {
  92.     Document* document;
  93.  
  94.     document = [Document currentDocument];
  95.  
  96.     if (![lineCharacterMatrix selectedRow]) {
  97.         int startLine, endLine;
  98.         [document getStartLine:&startLine endLine:&endLine];
  99.         if (startLine != endLine)
  100.             [rangeField setStringValue:
  101.                 [NSString stringWithFormat:@"%d:%d", startLine, endLine]];
  102.         else
  103.             [rangeField setStringValue:
  104.                 [NSString stringWithFormat:@"%d", startLine]];
  105.     }
  106.     else {
  107.         NSRange range = [document selectionRange];
  108.  
  109.         if (range.length)
  110.             [rangeField setStringValue:
  111.                 [NSString stringWithFormat:@"%d:%d",
  112.                     range.location, range.location + range.length]];
  113.         else
  114.             [rangeField setStringValue:
  115.                 [NSString stringWithFormat:@"%d", range.location]];
  116.     }
  117. }
  118.  
  119. - (void)showPanel
  120. {
  121.     [self updateInfo];
  122.     [[rangeField window] makeFirstResponder:rangeField];
  123.     [[lineCharacterMatrix window] makeKeyAndOrderFront:nil];
  124. }
  125.  
  126. - (BOOL)useLineRange        { return useLineRange; }
  127. - (BOOL)autoupdate        { return autoupdate; }
  128.  
  129. @end
  130.