ARC: Update HTMLOutputWindow framework

This commit is contained in:
Allan Odgaard
2012-09-24 18:32:33 +02:00
parent 45d38ff6c8
commit 962bf5149b
3 changed files with 41 additions and 48 deletions

View File

@@ -1,29 +1,38 @@
#import "HTMLOutputWindow.h"
#import <OakAppKit/OakWindowFrameHelper.h>
#import <command/runner.h>
#import <oak/debug.h>
OAK_DEBUG_VAR(HTMLOutputWindow);
static std::multimap<oak::uuid_t, HTMLOutputWindowController*> Windows;
@interface HTMLOutputWindowController ()
@property (nonatomic, retain) NSWindow* window;
@property (nonatomic, retain) OakHTMLOutputView* htmlOutputView;
@property (nonatomic, readonly) BOOL running;
@end
@implementation HTMLOutputWindowController
{
command::runner_ptr runner;
}
- (id)initWithRunner:(command::runner_ptr const&)aRunner
{
if(self = [[super init] retain])
if(self = [super init])
{
window = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 100, 100) styleMask:(NSTitledWindowMask|NSClosableWindowMask|NSResizableWindowMask|NSMiniaturizableWindowMask) backing:NSBackingStoreBuffered defer:NO];
htmlOutputView = [[OakHTMLOutputView alloc] init];
self.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 100, 100) styleMask:(NSTitledWindowMask|NSClosableWindowMask|NSResizableWindowMask|NSMiniaturizableWindowMask) backing:NSBackingStoreBuffered defer:NO];
self.htmlOutputView = [[OakHTMLOutputView alloc] init];
[window setFrameAutosaveName:@"Command Output (HTML)"];
[window bind:@"title" toObject:htmlOutputView.webView withKeyPath:@"mainFrameTitle" options:nil];
[window bind:@"documentEdited" toObject:htmlOutputView withKeyPath:@"runningCommand" options:nil];
[window setContentView:htmlOutputView];
[window setDelegate:self];
[self.window setFrameAutosaveName:@"Command Output (HTML)"];
[self.window bind:@"title" toObject:self.htmlOutputView.webView withKeyPath:@"mainFrameTitle" options:nil];
[self.window bind:@"documentEdited" toObject:self.htmlOutputView withKeyPath:@"runningCommand" options:nil];
[self.window setContentView:self.htmlOutputView];
[self.window setDelegate:self];
[self.window setReleasedWhenClosed:NO];
[OakWindowFrameHelper windowFrameHelperWithWindow:window];
[OakWindowFrameHelper windowFrameHelperWithWindow:self.window];
[self setCommandRunner:aRunner];
}
@@ -34,8 +43,9 @@ static std::multimap<oak::uuid_t, HTMLOutputWindowController*> Windows;
{
foreach(it, Windows.lower_bound(aRunner->uuid()), Windows.upper_bound(aRunner->uuid()))
{
if(![it->second running])
return [it->second setCommandRunner:aRunner], it->second;
HTMLOutputWindowController* controller = it->second;
if(![controller running])
return [controller setCommandRunner:aRunner], controller;
}
for(NSWindow* window in [NSApp orderedWindows])
@@ -45,23 +55,7 @@ static std::multimap<oak::uuid_t, HTMLOutputWindowController*> Windows;
return [delegate setCommandRunner:aRunner], delegate;
}
return [[[self alloc] initWithRunner:aRunner] autorelease];
}
- (void)dealloc
{
D(DBF_HTMLOutputWindow, bug("\n"););
foreach(it, Windows.lower_bound(runner->uuid()), Windows.upper_bound(runner->uuid()))
{
if(it->second == self)
{
Windows.erase(it);
break;
}
}
[htmlOutputView release];
[super dealloc];
return [[self alloc] initWithRunner:aRunner];
}
- (BOOL)setCommandRunner:(command::runner_ptr const&)aRunner
@@ -72,10 +66,10 @@ static std::multimap<oak::uuid_t, HTMLOutputWindowController*> Windows;
runner = aRunner;
Windows.insert(std::make_pair(runner->uuid(), self));
[htmlOutputView setEnvironment:runner->environment()];
[htmlOutputView loadRequest:URLRequestForCommandRunner(runner) autoScrolls:runner->auto_scroll_output()];
[self.htmlOutputView setEnvironment:runner->environment()];
[self.htmlOutputView loadRequest:URLRequestForCommandRunner(runner) autoScrolls:runner->auto_scroll_output()];
[window makeKeyAndOrderFront:nil];
[self.window makeKeyAndOrderFront:nil];
return YES;
}
@@ -87,9 +81,14 @@ static std::multimap<oak::uuid_t, HTMLOutputWindowController*> Windows;
- (void)tearDown
{
[window setDelegate:nil];
window = nil; // window will releaseOnClose
[self release];
foreach(it, Windows.lower_bound(runner->uuid()), Windows.upper_bound(runner->uuid()))
{
if(it->second == self)
{
Windows.erase(it);
break;
}
}
}
- (void)closeWarningDidEnd:(NSAlert*)alert returnCode:(NSInteger)returnCode contextInfo:(void*)stack
@@ -97,21 +96,20 @@ static std::multimap<oak::uuid_t, HTMLOutputWindowController*> Windows;
D(DBF_HTMLOutputWindow, bug("close %s\n", BSTR(returnCode == NSAlertDefaultReturn)););
if(returnCode == NSAlertDefaultReturn) /* "Stop" */
{
[window close];
oak::kill_process_group_in_background(runner->process_id());
[self.window close];
[self tearDown];
}
[alert release];
}
- (BOOL)windowShouldClose:(id)aWindow
{
D(DBF_HTMLOutputWindow, bug("\n"););
if(!runner->running())
return [self tearDown], YES;
NSAlert* alert = [[NSAlert alertWithMessageText:@"Stop task before closing?" defaultButton:@"Stop Task" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"The job that the task is performing will not be completed."] retain];
[alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(closeWarningDidEnd:returnCode:contextInfo:) contextInfo:NULL];
NSAlert* alert = [NSAlert alertWithMessageText:@"Stop task before closing?" defaultButton:@"Stop Task" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"The job that the task is performing will not be completed."];
[alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:@selector(closeWarningDidEnd:returnCode:contextInfo:) contextInfo:NULL];
return NO;
}
@end