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];
29 [[HBOutputRedirect stdoutRedirect] addListener:self];
35 * Stops redirection of stderr and releases resources.
39 [[HBOutputRedirect stderrRedirect] removeListener:self];
40 [[HBOutputRedirect stdoutRedirect] removeListener:self];
41 [outputTextStorage release];
42 [outputPanel release];
47 * Loads output panel from OutputPanel.nib and shows it.
49 - (IBAction)showOutputPanel:(id)sender
53 BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
54 NSAssert(loadSucceeded, @"Could not open nib file");
56 [outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
57 [[textView layoutManager] replaceTextStorage:outputTextStorage];
60 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
61 [outputPanel orderFront:nil];
63 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
67 * Displays text received from HBOutputRedirect in the text view.
69 - (void)stderrRedirect:(NSString *)text
71 NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
72 [outputTextStorage appendAttributedString:attributedString];
73 [attributedString release];
75 if ([outputTextStorage length] > TextStorageUpperSizeLimit)
76 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
78 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
80 - (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }
83 * Clears the output window.
85 - (IBAction)clearOutput:(id)sender
87 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
91 * Copies all text in the output window to pasteboard.
93 - (IBAction)copyAllOutputToPasteboard:(id)sender
95 NSPasteboard *pboard = [NSPasteboard generalPasteboard];
96 [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
97 [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
100 - (void)windowWillClose:(NSNotification *)aNotification
102 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];