From c0962bf16ec495315bfc460f1b86323be4daceae Mon Sep 17 00:00:00 2001 From: Martin Fuhrer Date: Wed, 25 Jul 2007 05:39:18 +0000 Subject: [PATCH] Print configuration is now saved between Tux Paint sessions on Mac OS X. --- macosx/SDLMain.h | 1 + macosx/SDLMain.m | 13 +++++- src/macosx_print.h | 5 +-- src/macosx_print.m | 110 +++++++++++++++++++++++++++++++-------------- src/tuxpaint.c | 6 +++ 5 files changed, 98 insertions(+), 37 deletions(-) diff --git a/macosx/SDLMain.h b/macosx/SDLMain.h index 1b7b3c815..7b2a67a2a 100755 --- a/macosx/SDLMain.h +++ b/macosx/SDLMain.h @@ -26,6 +26,7 @@ - (IBAction)onQuit:(id)sender; - (void) sendSDLControlKeystroke:(int)key; +- (void) sendSDLControlShiftKeystroke:(int)key; - (void) setupBridge; @end diff --git a/macosx/SDLMain.m b/macosx/SDLMain.m index 358ac4425..8354fe202 100644 --- a/macosx/SDLMain.m +++ b/macosx/SDLMain.m @@ -183,7 +183,7 @@ static NSString *getApplicationName(void) - (IBAction) onPageSetup:(id)sender { - DisplayPageSetup(); + [self sendSDLControlShiftKeystroke:SDLK_p]; } - (IBAction) onUndo:(id)sender @@ -204,6 +204,8 @@ static NSString *getApplicationName(void) - (IBAction) onQuit:(id)sender { + [[NSUserDefaults standardUserDefaults] synchronize]; + /* Post a SDL_QUIT event */ SDL_Event event; event.type = SDL_QUIT; @@ -219,6 +221,15 @@ static NSString *getApplicationName(void) SDL_PushEvent(&event); } +- (void) sendSDLControlShiftKeystroke:(int)key +{ + SDL_Event event; + event.type = SDL_KEYDOWN; + event.key.keysym.sym = key; + event.key.keysym.mod = KMOD_CTRL | KMOD_SHIFT; + SDL_PushEvent(&event); +} + /* Set the working directory to the .app's parent directory */ - (void) setupWorkingDirectory:(BOOL)shouldChdir { diff --git a/src/macosx_print.h b/src/macosx_print.h index 29c3fabc6..d7ee4e9a8 100644 --- a/src/macosx_print.h +++ b/src/macosx_print.h @@ -24,12 +24,11 @@ #include "SDL.h" -const char *SurfacePrint(SDL_Surface * surface, int showDialog); +const char *SurfacePrint(SDL_Surface *surface, int showDialog); +int DisplayPageSetup(const SDL_Surface *surface); #ifdef OBJECTIVEC -BOOL DisplayPageSetup(); - @interface PrintSheetController : NSObject { bool displayPrintSetupSheet; diff --git a/src/macosx_print.m b/src/macosx_print.m index 4de35a3af..079011c38 100644 --- a/src/macosx_print.m +++ b/src/macosx_print.m @@ -28,6 +28,7 @@ #import extern WrapperData macosx; +NSData* printData = nil; // this object presents the image to the printing layer @interface ImageView : NSView @@ -179,33 +180,8 @@ static NSImage* CreateImage( SDL_Surface *surface ) return image; } -BOOL DisplayPageSetup() +void DefaultPrintSettings( const SDL_Surface *surface, NSPrintInfo *printInfo ) { - NSPageLayout* pageLayout; - NSPrintInfo* printInfo; - ModalDelegate* delegate; - BOOL result; - - macosx.cocoaKeystrokes = 1; - printInfo = [ NSPrintInfo sharedPrintInfo ]; - delegate = [ [ [ ModalDelegate alloc ] init ] autorelease ]; - pageLayout = [ NSPageLayout pageLayout ]; - [ pageLayout beginSheetWithPrintInfo:printInfo - modalForWindow:[ NSApp mainWindow ] - delegate:delegate - didEndSelector:@selector(pageLayoutEnded:returnCode:contextInfo:) - contextInfo:nil ]; - - result = [ delegate wait ]; - macosx.cocoaKeystrokes = 0; - - return result; -} - -void DefaultPrintSettings( SDL_Surface *surface ) -{ - NSPrintInfo* printInfo = [ NSPrintInfo sharedPrintInfo ]; - if( surface->w > surface->h ) [ printInfo setOrientation:NSLandscapeOrientation ]; else @@ -217,6 +193,78 @@ void DefaultPrintSettings( SDL_Surface *surface ) [ printInfo setHorizontalPagination:NSFitPagination ]; } +NSPrintInfo* LoadPrintInfo( const SDL_Surface *surface ) +{ + NSUserDefaults* standardUserDefaults; + NSPrintInfo* printInfo; + NSData* printData = nil; + static BOOL firstTime = YES; + + standardUserDefaults = [ NSUserDefaults standardUserDefaults ]; + + if( standardUserDefaults ) + { + printData = [ standardUserDefaults dataForKey:@"PrintInfo" ]; + } + + if( printData ) + { + printInfo = (NSPrintInfo*)[ NSUnarchiver unarchiveObjectWithData:printData ]; + } + else + { + printInfo = [ NSPrintInfo sharedPrintInfo ]; + if( firstTime == YES ) + { + DefaultPrintSettings( surface, printInfo ); + firstTime = NO; + } + } + + return printInfo; +} + +void SavePrintInfo( NSPrintInfo* printInfo ) +{ + NSUserDefaults* standardUserDefaults; + NSData* printData = nil; + + printData = [ NSArchiver archivedDataWithRootObject:printInfo ]; + standardUserDefaults = [ NSUserDefaults standardUserDefaults ]; + + if( standardUserDefaults ) + { + [ standardUserDefaults setObject:printData forKey:@"PrintInfo" ]; + } +} + +int DisplayPageSetup( const SDL_Surface * surface ) +{ + NSPageLayout* pageLayout; + NSPrintInfo* printInfo; + ModalDelegate* delegate; + BOOL result; + + macosx.cocoaKeystrokes = 1; + + printInfo = LoadPrintInfo( surface ); + + delegate = [ [ [ ModalDelegate alloc ] init ] autorelease ]; + pageLayout = [ NSPageLayout pageLayout ]; + [ pageLayout beginSheetWithPrintInfo:printInfo + modalForWindow:[ NSApp mainWindow ] + delegate:delegate + didEndSelector:@selector(pageLayoutEnded:returnCode:contextInfo:) + contextInfo:nil ]; + + result = [ delegate wait ]; + SavePrintInfo( printInfo ); + + macosx.cocoaKeystrokes = 0; + + return (int)( result ); +} + const char* SurfacePrint( SDL_Surface *surface, int showDialog ) { NSImage* image; @@ -225,7 +273,6 @@ const char* SurfacePrint( SDL_Surface *surface, int showDialog ) NSPrintOperation* printOperation; NSPrintInfo* printInfo; ModalDelegate* delegate; - static BOOL firstTime = YES; BOOL ok = YES; const char* error = NULL; @@ -234,13 +281,8 @@ const char* SurfacePrint( SDL_Surface *surface, int showDialog ) if( image == nil ) return "Could not create image"; - if( firstTime == YES ) { - DefaultPrintSettings( surface ); - firstTime = NO; - } - // create print control objects - printInfo = [ NSPrintInfo sharedPrintInfo ]; + printInfo = LoadPrintInfo( surface ); NSRect pageRect = [ printInfo imageablePageBounds ]; NSSize pageSize = pageRect.size; @@ -285,6 +327,8 @@ const char* SurfacePrint( SDL_Surface *surface, int showDialog ) error = "Canceled or error when printing"; macosx.cocoaKeystrokes = 0; + + SavePrintInfo( printInfo ); [ image release ]; return error; diff --git a/src/tuxpaint.c b/src/tuxpaint.c index 5ffa5334f..5c48e0d06 100644 --- a/src/tuxpaint.c +++ b/src/tuxpaint.c @@ -2051,6 +2051,12 @@ static void mainloop(void) draw_toolbar(); update_screen_rect(&r_tools); } +#ifdef __APPLE__ + else if (key == SDLK_p && (mod & KMOD_CTRL) && (mod & KMOD_SHIFT) && !noshortcuts) { + /* Ctrl-Shft-P - Page Setup */ + DisplayPageSetup(canvas); + } +#endif else if (key == SDLK_p && (mod & KMOD_CTRL) && !noshortcuts) { /* Ctrl-P - Print */