OSDN Git Service

MacGui: Debug Window to read libhb output like cli in inspector window
[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         }
30         return self;
31 }
32
33 /**
34  * Stops redirection of stderr and releases resources.
35  */
36 - (void)dealloc
37 {
38         [[HBOutputRedirect stderrRedirect] removeListener:self];        
39         [outputTextStorage release];
40         [outputPanel release];
41         [super dealloc];
42 }
43
44 /**
45  * Loads output panel from OutputPanel.nib and shwos it.
46  */
47 - (IBAction)showOutputPanel:(id)sender
48 {
49         if (!outputPanel)
50         {
51                 BOOL loadSucceeded = [NSBundle loadNibNamed:@"OutputPanel" owner:self] && outputPanel;
52                 NSAssert(loadSucceeded, @"Could not open nib file");
53                 
54                 [outputPanel setFrameAutosaveName:@"OutputPanelFrame"];
55                 [[textView layoutManager] replaceTextStorage:outputTextStorage];
56         }
57                 
58     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
59         [outputPanel orderFront:nil];
60 }
61
62 /**
63  * Displays text received from HBOutputRedirect in the text view.
64  */
65 - (void)stderrRedirect:(NSString *)text
66 {
67         NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
68         [outputTextStorage appendAttributedString:attributedString];
69         [attributedString release];
70
71         if ([outputTextStorage length] > TextStorageUpperSizeLimit)
72                 [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length] - TextStorageLowerSizeLimit)];
73
74     [textView scrollRangeToVisible:NSMakeRange([outputTextStorage length], 0)];
75 }
76
77 /**
78  * Clears the output window.
79  */
80 - (IBAction)clearOutput:(id)sender
81 {
82         [outputTextStorage deleteCharactersInRange:NSMakeRange(0, [outputTextStorage length])];
83 }
84
85 /**
86  * Copies all text in the output window to pasteboard.
87  */
88 - (IBAction)copyAllOutputToPasteboard:(id)sender
89 {
90         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
91         [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
92         [pboard setString:[outputTextStorage string] forType:NSStringPboardType];
93 }
94
95 @end