/* Copyright (C) 2002-2003, Jeffrey D. Argast. The authors make NO WARRANTY or representation, either express or implied, with respect to this software, its quality, accuracy, merchantability, or fitness for a particular purpose. This software is provided "AS IS", and you, its user, assume the entire risk as to its quality and accuracy. Permission is hereby granted to use, copy, modify, and distribute this software or portions thereof for any purpose, without fee, subject to these conditions: (1) If any part of the source code is distributed, then this statement must be included, with this copyright and no-warranty notice unaltered. (2) Permission for use of this software is granted only if the user accepts full responsibility for any undesirable consequences; the authors accept NO LIABILITY for damages of any kind. */ #import "ConsoleController.h" #import "MessageDefs.h" @implementation ConsoleController - (id) init { if ( self = [super init] ) { NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter]; [nCenter addObserver:self selector:@selector(handleOutputMessage:) name:kOutputMessage object:nil]; [nCenter addObserver:self selector:@selector(handleExecutingMessage:) name:kExecutingMessage object:nil]; [nCenter addObserver:self selector:@selector(handleInfoMessage:) name:kInfoMessage object:nil]; [nCenter addObserver:self selector:@selector(handleWarnMessage:) name:kWarnMessage object:nil]; [nCenter addObserver:self selector:@selector(handleErrorMessage:) name:kErrorMessage object:nil]; } return self; } - (void) awakeFromNib { // We don't want a context menu [fTextView setMenu:nil]; } - (void) dealloc { NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter]; [nCenter removeObserver:self]; [super dealloc]; } - (void) handleExecutingMessage: (NSNotification*) notification { [self sendToConsole:[notification object] withRed:0.0 withGreen:0.0 withBlue:0.0]; } - (void) handleOutputMessage: (NSNotification*) notification { [self sendToConsole:[notification object] withRed:0.0 withGreen:0.5 withBlue:0.0]; } - (void) handleInfoMessage: (NSNotification*) notification { [self sendToConsole:[notification object] withRed:0.0 withGreen:0.0 withBlue:1.0]; } - (void) handleWarnMessage: (NSNotification*) notification { [self sendToConsole:[notification object] withRed:0.8 withGreen:0.4 withBlue:0.0]; } - (void) handleErrorMessage: (NSNotification*) notification { [self sendToConsole:[notification object] withRed:1.0 withGreen:0.0 withBlue:0.0]; } - (void) sendToConsole: (NSString*) msg withRed: (float) red withGreen: (float) green withBlue: (float) blue { NSRange attrStrRange; NSRange endOfTextRange; NSColor* textColor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:1.0]; // We can't use the insertText method on NSTextView because // the view is not editable. The insertText method honors the // editable flag. Instead we have to use NSText methods to insert // the text // Since the text view is selectable, the current insertion point can be altered // by the user. But we always want to insert the text at the end of the window. // Hence we set the selection to be the end of the text. NSString* backingStoreString = [fTextView string]; endOfTextRange.location = [backingStoreString length]; endOfTextRange.length = 0; attrStrRange.location = endOfTextRange.location; attrStrRange.length = [msg length]; [fTextView replaceCharactersInRange:endOfTextRange withString:msg]; [fTextView setTextColor:textColor range:attrStrRange]; [fTextView scrollRangeToVisible:attrStrRange]; [fTextView display]; } @end