summaryrefslogtreecommitdiffstats
path: root/macosx/HBOutputPanelController.m
blob: 54bf4916efb4da318e33b9779dba43448bffe41a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
 * @file
 * @date 18.5.2007
 *
 * Implementation of class HBOutputPanelController.
 */

#import "HBOutputPanelController.h"
#import "HBOutputRedirect.h"

/// Maximum amount of characters that can be shown in the view.
#define TextStorageUpperSizeLimit 20000

/// When old output is removed, this is the amount of characters that will be
/// left in outputTextStorage.
#define TextStorageLowerSizeLimit 15000

@implementation HBOutputPanelController

/**
 * Initializes the object, creates outputTextStorage and starts redirection of stderr.
 */
- (id)init
{
	if (self = [super init])
	{
		outputTextStorage = [[NSTextStorage alloc] init];
		[[HBOutputRedirect stderrRedirect] addListener:self];
		[[HBOutputRedirect stdoutRedirect] addListener:self];
	}
	return self;
}

/**
 * Stops redirection of stderr and releases resources.
 */
- (void)dealloc
{
	[[HBOutputRedirect stderrRedirect] removeListener:self];
	[[HBOutputRedirect stdoutRedirect] removeListener:self];	
	[outputTextStorage release];
	[outputPanel release];
	[super dealloc];
}

/**
 * Loads output panel from OutputPanel.nib and shows it.
 */
- (IBAction)showOutputPanel:(id)sender
{
	if (!outputPanel)
	{
		BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
		NSAssert(loadSucceeded, @"Could not open nib file");
		
		[outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
		[[textView layoutManager] replaceTextStorage:outputTextStorage];
	}
		
    [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
	[outputPanel orderFront:nil];

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
}

/**
 * Displays text received from HBOutputRedirect in the text view.
 */
- (void)stderrRedirect:(NSString *)text
{
	NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
	[outputTextStorage appendAttributedString:attributedString];
	[attributedString release];

	if ([outputTextStorage length] > TextStorageUpperSizeLimit)
		[outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];

    [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
}
- (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }

/**
 * Clears the output window.
 */
- (IBAction)clearOutput:(id)sender
{
	[outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
}

/**
 * Copies all text in the output window to pasteboard.
 */
- (IBAction)copyAllOutputToPasteboard:(id)sender
{
	NSPasteboard *pboard = [NSPasteboard generalPasteboard];
	[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
	[pboard setString:[outputTextStorage string] forType:NSStringPboardType];
}

- (void)windowWillClose:(NSNotification *)aNotification
{
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];
}


@end