// Copyright GreenKeeper Software 2005 #import "LogController.h" #import "Preferences.h" // Responsible for connecting the Log model to the interface @implementation LogController - (id)init { [super init]; // Initialize the model log = [[Log alloc] init]; // Listen to see if the log has been updated [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLog:) name:@"logUpdated" object:nil]; return self; } - (void)awakeFromNib { // Grab the text storage from logView logStorage = [logView textStorage]; // Helps with "autoscrolloing" - Makes sure logView is scrolled all the way to the bottom to start with [[[logView enclosingScrollView] verticalScroller] setFloatValue:1.0]; // Sets the name which will be used to remember the location of the log window from launch to launch [logWindow setFrameUsingName:@"LogWindow"]; // Open the log window on launch if the preferences indicate to do so if([[Preferences prefs] boolForKey:@"displayLogOnAppLaunch"]) [self displayLog:self]; } - (void)dealloc { // Release all local variables [log release]; [logStorage release]; // Eliminated listeners [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } // Displays the log window - (IBAction)displayLog:(id)sender { [NSApp activateIgnoringOtherApps:YES]; [logWindow makeKeyAndOrderFront:self]; [self updateLog:nil]; } // Places standard output into the log window - (void)updateLog:(NSNotification *)notification { if(![logWindow isVisible]) return; // Set a flag to remember whether or not the logView was scrolled all the way to the bottom BOOL scrollerWasAtBottom = [[[logView enclosingScrollView] verticalScroller] floatValue] == 1.0; // Replace whatever is in the log with the log string sent by the model [logStorage replaceCharactersInRange:NSMakeRange(0, [logStorage length]) withString:[log currentLog]]; // Scroll to the end of the log if scroller was at the bottom when we started if(scrollerWasAtBottom) [logView scrollRangeToVisible:NSMakeRange([logStorage length], 0)]; } // The following two methods save the position and size of the log window - (void)windowDidMove:(NSNotification *)aNotification { [logWindow saveFrameUsingName:@"LogWindow"]; } - (void)windowDidResize:(NSNotification *)aNotification { [logWindow saveFrameUsingName:@"LogWindow"]; } @end