OSDN Git Service

MacGui: internal code changes
[handbrake-jp/handbrake-jp-git.git] / macosx / HBOutputPanelController.m
1 /**
2  * @file
3  * @date 18.5.2007
4  *
5  * Implementation of class HBOutputPanelController.
6  */
7
8 #import "HBOutputPanelController.h"
9 #import "HBOutputRedirect.h"
10
11 /// Maximum amount of characters that can be shown in the view.
12 #define TextStorageUpperSizeLimit 20000
13
14 /// When old output is removed, this is the amount of characters that will be
15 /// left in outputTextStorage.
16 #define TextStorageLowerSizeLimit 15000
17
18 @implementation HBOutputPanelController
19
20 /**
21  * Initializes the object, creates outputTextStorage and starts redirection of stderr.
22  */
23 - (id)init
24 {
25         if (self = [super init])
26         {
27                 outputTextStorage = [[NSTextStorage alloc] init];
28                 [[HBOutputRedirect stderrRedirect] addListener:self];
29                 [[HBOutputRedirect stdoutRedirect] addListener:self];
30         }
31         return self;
32 }
33
34 /**
35  * Stops redirection of stderr and releases resources.
36  */
37 - (void)dealloc
38 {
39         [[HBOutputRedirect stderrRedirect] removeListener:self];
40         [[HBOutputRedirect stdoutRedirect] removeListener:self];        
41         [outputTextStorage release];
42         [outputPanel release];
43         [super dealloc];
44 }
45
46 /**
47  * Loads output panel from OutputPanel.nib and shows it.
48  */
49 - (IBAction)showOutputPanel:(id)sender
50 {
51         if (!outputPanel)
52         {
53                 BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
54                 NSAssert(loadSucceeded, @"Could not open nib file");
55                 
56                 [outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
57                 [[textView layoutManager] replaceTextStorage:outputTextStorage];
58                 [[textView enclosingScrollView] setLineScroll:10];
59                 [[textView enclosingScrollView] setPageScroll:20];
60         }
61                 
62     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
63         [outputPanel orderFront:nil];
64
65     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OutputPanelIsOpen"];
66 }
67
68 /**
69  * Displays text received from HBOutputRedirect in the text view.
70  */
71 - (void)stderrRedirect:(NSString *)text
72 {
73         NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
74         [outputTextStorage appendAttributedString:attributedString];
75         [attributedString release];
76
77         if ([outputTextStorage length] > TextStorageUpperSizeLimit)
78                 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
79
80     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
81 }
82 - (void)stdoutRedirect:(NSString *)text { [self stderrRedirect:text]; }
83
84 /**
85  * Clears the output window.
86  */
87 - (IBAction)clearOutput:(id)sender
88 {
89         [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
90 }
91
92 /**
93  * Copies all text in the output window to pasteboard.
94  */
95 - (IBAction)copyAllOutputToPasteboard:(id)sender
96 {
97         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
98         [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
99         [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
100 }
101
102 - (void)windowWillClose:(NSNotification *)aNotification
103 {
104     [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"OutputPanelIsOpen"];
105 }
106
107
108 @end