home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / TextSizingExample / Controller.m < prev    next >
Text File  |  1996-04-17  |  5KB  |  165 lines

  1. /*
  2.         Controller.m
  3.         TextSizingExample
  4.  
  5.         Author: Mike Ferris
  6.  
  7.     You may freely copy, distribute and reuse the code in this example.
  8.     NeXT disclaims any warranty of any kind, expressed or implied,
  9.     as to its fitness for any particular use.
  10. */
  11.  
  12. #import "Controller.h"
  13.  
  14. #import "Aspect.h"
  15. #import "VertScrollAspect.h"
  16. #import "BiScrollAspect.h"
  17. #import "FixedSizeAspect.h"
  18. #import "TwoColumnsAspect.h"
  19. #import "FieldAspect.h"
  20.  
  21.  
  22. @implementation Controller
  23.  
  24. - (id)init {
  25.     self = [super init];
  26.     if (self) {
  27.         _currentAspectIndex = -1;
  28.     }
  29.     return self;
  30. }
  31.  
  32. - (void)dealloc {
  33.     [self swapInAspectAtIndex:-1];
  34.     [_aspects release];
  35.     [_openPanelPath release];
  36.     [_textStorage release];
  37.     [[swapView window] autorelease];
  38.     [super dealloc];
  39. }
  40.  
  41. - (NSArray *)aspects {
  42.     if (!_aspects) {
  43.         _aspects = [[NSMutableArray allocWithZone:[self zone]] init];
  44.  
  45.         [_aspects addObject:[VertScrollAspect aspectWithController:self]];
  46.         [_aspects addObject:[BiScrollAspect aspectWithController:self]];
  47.         [_aspects addObject:[FixedSizeAspect aspectWithController:self]];
  48.         [_aspects addObject:[TwoColumnsAspect aspectWithController:self]];
  49.         [_aspects addObject:[FieldAspect aspectWithController:self]];
  50.     }
  51.     return _aspects;
  52. }
  53.  
  54. - (Aspect *)currentAspect {
  55.     if (_currentAspectIndex >= 0) {
  56.         return [[self aspects] objectAtIndex:_currentAspectIndex];
  57.     } else {
  58.         return nil;
  59.     }
  60. }
  61.  
  62. - (void)swapInAspectAtIndex:(int)newIndex {
  63.     if (newIndex != _currentAspectIndex) {
  64.         NSArray *aspects = [self aspects];
  65.         if (_currentAspectIndex >= 0) {
  66.             // Swap out current aspect.
  67.             [[[aspects objectAtIndex:_currentAspectIndex] aspectView] removeFromSuperview];
  68.         }
  69.         _currentAspectIndex = newIndex;
  70.         if (_currentAspectIndex >= 0) {
  71.             // Size and swap in new aspect.
  72.             NSSize frameSize = [swapView frame].size;
  73.             NSView *aspectView = [[aspects objectAtIndex:_currentAspectIndex] aspectView];
  74.             [aspectView setFrame:NSMakeRect(0.0, 0.0, frameSize.width, frameSize.height)];
  75.             [swapView addSubview:aspectView];
  76.             // Notify the aspect it was just installed.
  77.             [[aspects objectAtIndex:_currentAspectIndex] didSwapIn];
  78.         }
  79.     }
  80. }
  81.  
  82. - (void)awakeFromNib {
  83.     NSArray *aspects = [self aspects];
  84.     unsigned i, c = [aspects count];
  85.     
  86.     NSString *path;
  87.     NSData *rtfData = nil;
  88.  
  89.     // Create the NSTextStorage and load the default text file
  90.     path = [[NSBundle bundleForClass:[self class]] pathForResource:@"README" ofType:@"rtf"];
  91.     rtfData = [NSData dataWithContentsOfFile:path];
  92.     _textStorage = [[NSTextStorage allocWithZone:[self zone]] initWithRTF:rtfData documentAttributes:NULL];
  93.     
  94.     // Load the popup.
  95.     [aspectPopUpButton removeAllItems];
  96.     for (i=0; i<c; i++) {
  97.         [aspectPopUpButton addItemWithTitle:[[aspects objectAtIndex:i] aspectName]];
  98.     }
  99.  
  100.     // Set up an initial aspect to view
  101.     if (c > 0) {
  102.         [self swapInAspectAtIndex:0];
  103.     } else {
  104.         // No aspects!
  105.         [aspectPopUpButton addItemWithTitle:NSLocalizedString(@"No Aspect", @"Item title for aspect popup if no aspects exist.")];
  106.     }
  107. }
  108.  
  109. - (NSTextStorage *)textStorage {
  110.     return _textStorage;
  111. }
  112.  
  113. - (void)aspectPopUpAction:(id)sender {
  114.     int selectedIndex = [sender indexOfSelectedItem];
  115.     NSArray *aspects = [self aspects];
  116.     if ((selectedIndex >= 0) && (selectedIndex < [aspects count])) {
  117.         [self swapInAspectAtIndex:selectedIndex];
  118.     }
  119. }
  120.  
  121. - (void)loadDocumentAction:(id)sender {
  122.     NSOpenPanel *openPanel = [NSOpenPanel openPanel];
  123.     int runModalResult;
  124.  
  125.     // Configure the OpenPanel
  126.     [openPanel setTitle:NSLocalizedString(@"Load", @"Title for open panel for load document action.")];
  127.     [openPanel setTreatsFilePackagesAsDirectories:NO];
  128.     [openPanel setAllowsMultipleSelection:NO];
  129.     [openPanel setCanChooseDirectories:NO];
  130.     [openPanel setCanChooseFiles:YES];
  131.     [openPanel setDirectory:(_openPanelPath ? _openPanelPath : NSHomeDirectory())];
  132.  
  133.     // RUn the panel
  134.     runModalResult = [openPanel runModal];
  135.     if (runModalResult == NSOKButton) {
  136.         NSString *path = [openPanel filename];
  137.         NSString *extension = [path pathExtension];
  138.         NSAttributedString *attrString;
  139.  
  140.         if ([extension isEqualToString:@"rtfd"] || [extension isEqualToString:@"rtf"]) {
  141.             // Rich text.
  142.             attrString = [[NSAttributedString allocWithZone:[self zone]] initWithPath:path documentAttributes:NULL];
  143.         } else {
  144.             // Plain text.
  145.             attrString = [[NSAttributedString allocWithZone:[self zone]] initWithString:[NSString stringWithContentsOfFile:path]];
  146.         }
  147.  
  148.         [_textStorage replaceCharactersInRange:NSMakeRange(0, [_textStorage length]) withAttributedString:attrString];
  149.         [attrString release];
  150.     }
  151.     [_openPanelPath release];
  152.     _openPanelPath = [[openPanel directory] copyWithZone:[self zone]];
  153. }
  154.  
  155. - (void)infoAction:(id)sender {
  156.     if (!infoPanel) {
  157.         if (![NSBundle loadNibNamed:@"Info" owner:self]) {
  158.             NSLog(@"Failed to load Info.nib.");
  159.         }
  160.     }
  161.     [infoPanel makeKeyAndOrderFront:self];
  162. }
  163.  
  164. @end
  165.