5 * Implementation of class HBOutputPanelController.
8 #import "HBOutputPanelController.h"
9 #import "HBOutputRedirect.h"
11 /// Maximum amount of characters that can be shown in the view.
12 #define TextStorageUpperSizeLimit 20000
14 /// When old output is removed, this is the amount of characters that will be
15 /// left in outputTextStorage.
16 #define TextStorageLowerSizeLimit 15000
18 @implementation HBOutputPanelController
21 * Initializes the object, creates outputTextStorage and starts redirection of stderr.
25 if (self = [super init])
27 outputTextStorage = [[NSTextStorage alloc] init];
28 [[HBOutputRedirect stderrRedirect] addListener:self];
34 * Stops redirection of stderr and releases resources.
38 [[HBOutputRedirect stderrRedirect] removeListener:self];
39 [outputTextStorage release];
40 [outputPanel release];
45 * Loads output panel from OutputPanel.nib and shwos it.
47 - (IBAction)showOutputPanel:(id)sender
51 BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
52 NSAssert(loadSucceeded, @"Could not open nib file");
54 [outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
55 [[textView layoutManager] replaceTextStorage:outputTextStorage];
58 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
59 [outputPanel orderFront:nil];
63 * Displays text received from HBOutputRedirect in the text view.
65 - (void)stderrRedirect:(NSString *)text
67 NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
68 [outputTextStorage appendAttributedString:attributedString];
69 [attributedString release];
71 if ([outputTextStorage length] > TextStorageUpperSizeLimit)
72 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
74 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
78 * Clears the output window.
80 - (IBAction)clearOutput:(id)sender
82 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
86 * Copies all text in the output window to pasteboard.
88 - (IBAction)copyAllOutputToPasteboard:(id)sender
90 NSPasteboard *pboard = [NSPasteboard generalPasteboard];
91 [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
92 [pboard setString:[outputTextStorage string] forType:NSStringPboardType];