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];
58 [[textView enclosingScrollView] setLineScroll:10];
59 [[textView enclosingScrollView] setPageScroll:20];
62 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
63 [outputPanel orderFront:nil];
65 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
69 * Displays text received from HBOutputRedirect in the text view.
71 - (void)stderrRedirect:(NSString *)text
73 NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
74 [outputTextStorage appendAttributedString:attributedString];
75 [attributedString release];
77 if ([outputTextStorage length] > TextStorageUpperSizeLimit)
78 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
80 [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
82 - (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }
85 * Clears the output window.
87 - (IBAction)clearOutput:(id)sender
89 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
93 * Copies all text in the output window to pasteboard.
95 - (IBAction)copyAllOutputToPasteboard:(id)sender
97 NSPasteboard *pboard = [NSPasteboard generalPasteboard];
98 [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
99 [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
102 - (void)windowWillClose:(NSNotification *)aNotification
104 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];