# HG changeset patch # User Markus Bröker # Date 1493914737 -7200 # Node ID c9cf6f811b8664856f5bcce0c4a6f1e9fa389ffd # Parent c350b40cbd5e755af3b45cf2a8d2fe104d5eb381 Automatisches Öffnen des letzten Dokuments diff --git a/PDFViewer/appdelegate/AppDelegate.h b/PDFViewer/appdelegate/AppDelegate.h --- a/PDFViewer/appdelegate/AppDelegate.h +++ b/PDFViewer/appdelegate/AppDelegate.h @@ -9,9 +9,7 @@ #import #import "ViewController.h" -@interface AppDelegate : NSObject { - NSWindow *window; - ViewController *controller; -} - +@interface AppDelegate : NSObject +@property (weak) NSWindow* window; +@property (weak) ViewController* controller; @end diff --git a/PDFViewer/appdelegate/AppDelegate.m b/PDFViewer/appdelegate/AppDelegate.m --- a/PDFViewer/appdelegate/AppDelegate.m +++ b/PDFViewer/appdelegate/AppDelegate.m @@ -9,8 +9,25 @@ #import "AppDelegate.h" @implementation AppDelegate +@synthesize controller = _controller; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newDocumentLoaded:) name:@"newDocumentLoaded" object:nil]; +} + +- (void)newDocumentLoaded:(NSNotification*) notification { + NSString *lastUrl = [notification object]; + + NSLog(@"Setze die letzte URL auf %@", lastUrl); + + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + [defaults setObject:lastUrl forKey:@"lastURL"]; + [defaults synchronize]; +} + +- (void)applicationWillFinishLaunching:(NSNotification *)notification { + _window = [[[NSApplication sharedApplication] windows] objectAtIndex:0]; + _controller = (ViewController*) [_window contentViewController]; } - (void)applicationWillTerminate:(NSNotification *)aNotification { @@ -22,33 +39,21 @@ } - (BOOL) application:(NSApplication *)sender openFile:(NSString *)filename { - window = [[[NSApplication sharedApplication] windows] objectAtIndex:0]; - controller = (ViewController*) [window contentViewController]; - NSURL *url = [[NSURL alloc] initFileURLWithPath:filename]; - BOOL status = [[controller pdfViewer] loadUrl: url]; + BOOL status = [[_controller pdfViewer] loadUrl: url]; if (!status) { - [[controller pdfViewer] speak:(NSMutableString*)@"Das Dokument konnte nicht geladen werden"]; + [[_controller pdfViewer] speak:(NSMutableString*)@"Das Dokument konnte nicht geladen werden"]; } else { - [[controller pdfViewer] startSpeaking:nil]; + [[_controller pdfViewer] startSpeaking:nil]; } return status; } - (void) application:(NSApplication *)sender openFiles:(NSArray *)filenames { - window = [[[NSApplication sharedApplication] windows] objectAtIndex:0]; - controller = (ViewController*) [window contentViewController]; - - NSString *filename = [filenames objectAtIndex:0]; - NSURL *url = [[NSURL alloc] initFileURLWithPath:filename]; - BOOL status = [[controller pdfViewer] loadUrl: url]; - - if (!status) { - [[controller pdfViewer] speak:(NSMutableString*)@"Das Dokument konnte nicht geladen werden"]; - } else { - [[controller pdfViewer] startSpeaking:nil]; + for (id filename in filenames) { + [self application:sender openFile:filename]; } } diff --git a/PDFViewer/pdfviewer/PDFViewer.m b/PDFViewer/pdfviewer/PDFViewer.m --- a/PDFViewer/pdfviewer/PDFViewer.m +++ b/PDFViewer/pdfviewer/PDFViewer.m @@ -22,20 +22,36 @@ NSString *salutation = [self getSalutation]; + if (nOps == 0) { + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSURL *url = [NSURL URLWithString:[defaults objectForKey:@"lastURL"]]; + + [self loadUrl:url]; + nOps = [[self document] pageCount]; + } + if (nOps > 0) { [strings appendString:[NSString stringWithFormat:@"%@, das Dokument hat %lu Seiten", salutation, nOps]]; - } else { - [strings appendString:[NSString stringWithFormat:@"%@, laden Sie ein Dokument mit MAC-O", salutation]]; - } - for (int pages = 0; pages < nOps; pages++) { - PDFPage *page = [[self document] pageAtIndex:pages]; - [strings appendString:[self replaceString:[page string]]]; + for (int pages = 0; pages < nOps; pages++) { + PDFPage *page = [[self document] pageAtIndex:pages]; + [strings appendString:[self replaceString:[page string]]]; + } } return strings; } +- (IBAction)print:(id)sender { + [super print:sender]; +} + +- (IBAction)runPageLayout:(id)sender { + NSPageLayout *pageLayout = [NSPageLayout pageLayout]; + + [pageLayout runModal]; +} + /* Datei-Menü: Öffnen-Dialog */ - (IBAction)openDocument:(id)sender { NSArray *fileTypes = [NSPDFImageRep imageTypes]; @@ -132,6 +148,10 @@ [self silence]; [self setDocument:doc]; + if (doc != NULL) { + [[NSNotificationCenter defaultCenter] postNotificationName:@"newDocumentLoaded" object:url.absoluteString]; + } + return (doc != NULL); }