Use block-based alert API

This commit is contained in:
Allan Odgaard
2012-12-31 03:59:50 +01:00
parent b84992bdb3
commit 9e332438e7
4 changed files with 40 additions and 62 deletions

View File

@@ -1,5 +1,6 @@
#import "DocumentOpenHelper.h"
#import "EncodingView.h"
#import <OakAppKit/OakAppKit.h>
#import <OakFoundation/NSString Additions.h>
#import <ns/ns.h>
#import <text/parse.h>
@@ -36,7 +37,10 @@ namespace
NSAlert* alert = [NSAlert alertWithMessageText:@"Unknown Encoding" defaultButton:@"Continue" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"This file is not UTF-8 nor does it have any encoding information stored."];
EncodingViewController* controller = [[EncodingViewController alloc] initWithFirst:content->begin() last:content->end()];
[alert setAccessoryView:controller.view];
[alert beginSheetModalForWindow:_window modalDelegate:_self didEndSelector:@selector(selectEncodingSheetDidEnd:returnCode:contextInfo:) contextInfo:new info_t(controller, context)];
OakShowAlertForWindow(alert, _window, ^(NSInteger returnCode){
if(returnCode == NSAlertDefaultReturn)
context->set_charset(text::split(to_s(controller.currentEncoding), " ")[0]);
});
[[alert window] recalculateKeyViewLoop];
}
@@ -93,13 +97,6 @@ namespace
}
}
- (void)selectEncodingSheetDidEnd:(NSAlert*)alert returnCode:(NSInteger)returnCode contextInfo:(info_t*)info
{
if(returnCode == NSAlertDefaultReturn)
info->context->set_charset(text::split(to_s(info->controller.currentEncoding), " ")[0]);
delete info;
}
- (void)fileTypeDialog:(FileTypeDialog*)fileTypeDialog didSelectFileType:(NSString*)aFileType contextInfo:(void*)info
{
if(aFileType)

View File

@@ -2,6 +2,7 @@
#import "DocumentCommand.h"
#import <OakFoundation/NSString Additions.h>
#import <OakAppKit/NSAlert Additions.h>
#import <OakAppKit/OakAppKit.h>
#import <OakAppKit/OakSavePanel.h>
#import <OakAppKit/OakEncodingPopUpButton.h>
#import <ns/ns.h>
@@ -55,7 +56,12 @@ namespace
// TODO “unlock file” checkbox (presently implied)
NSAlert* alert = [NSAlert tmAlertWithMessageText:[NSString stringWithCxxString:text::format("The file “%s” is locked.", _document->display_name().c_str())] informativeText:@"Do you want to overwrite it anyway?" buttons:@"Overwrite", @"Cancel", nil];
[alert beginSheetModalForWindow:_window modalDelegate:_self didEndSelector:@selector(makeWritableSheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
OakShowAlertForWindow(alert, _window, ^(NSInteger returnCode){
if(returnCode == NSAlertFirstButtonReturn)
context->set_make_writable(true);
else _self.userAbort = YES;
[_self setContext:file::save_context_ptr()];
});
}
void obtain_authorization (std::string const& path, io::bytes_ptr content, osx::authorization_t auth, file::save_context_ptr context)
@@ -73,10 +79,15 @@ namespace
if(charset != kCharsetNoEncoding)
{
// TODO transliteration / BOM check box
NSAlert* alert = [NSAlert tmAlertWithMessageText:[NSString stringWithCxxString:text::format("Unable to save document using “%s” as encoding.", charset.c_str())] informativeText:@"Please choose another encoding:" buttons:@"Retry", @"Cancel", nil];
[alert setAccessoryView:[OakEncodingPopUpButton new]];
[alert beginSheetModalForWindow:_window modalDelegate:_self didEndSelector:@selector(encodingSheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
OakEncodingPopUpButton* encodingPopUp = [OakEncodingPopUpButton new];
[alert setAccessoryView:encodingPopUp];
OakShowAlertForWindow(alert, _window, ^(NSInteger returnCode){
if(returnCode == NSAlertFirstButtonReturn)
context->set_charset(to_s(encodingPopUp.encoding));
else _self.userAbort = YES;
[_self setContext:file::save_context_ptr()];
});
[[alert window] recalculateKeyViewLoop];
}
else
@@ -206,27 +217,4 @@ namespace
}
context.reset();
}
- (void)makeWritableSheetDidEnd:(NSAlert*)alert returnCode:(NSInteger)returnCode contextInfo:(void*)info
{
D(DBF_DocumentController_SaveHelper, bug("%s\n", BSTR(returnCode == NSAlertDefaultReturn)););
file::save_context_ptr ctxt = context;
[self setContext:file::save_context_ptr()];
if(returnCode == NSAlertFirstButtonReturn)
ctxt->set_make_writable(true);
else userAbort = YES;
}
- (void)encodingSheetDidEnd:(NSAlert*)alert returnCode:(NSInteger)returnCode contextInfo:(void*)info
{
D(DBF_DocumentController_SaveHelper, bug("\n"););
file::save_context_ptr ctxt = context;
[self setContext:file::save_context_ptr()];
userAbort = returnCode != NSAlertFirstButtonReturn;
if(!userAbort)
{
OakEncodingPopUpButton* popUp = (OakEncodingPopUpButton*)[alert accessoryView];
ctxt->set_charset(to_s(popUp.encoding));
}
}
@end