--- hatari/src/gui-osx/Shared.m 2019/04/09 08:47:11 1.1.1.3 +++ hatari/src/gui-osx/Shared.m 2019/04/09 08:55:20 1.1.1.9 @@ -1,18 +1,19 @@ /* - Hatari - CreateFloppyController.m + Hatari - Shared.m - This file is distributed under the GNU Public License, version 2 or at - your option any later version. Read the file gpl.txt for details. + This file is distributed under the GNU General Public License, version 2 + or at your option any later version. Read the file gpl.txt for details. Helper code used by the other Cocoa code files June 2006, Sébastien Molines - Created + 2013, M. SARO */ #import #import "Shared.h" #import "AlertHooks.h" -#include "main.h" +#import "main.h" @implementation ModalWrapper @@ -59,18 +60,15 @@ void GuiOsx_ExportPathString(NSString* p NSCAssert((szTarget), @"Target buffer must not be null."); NSCAssert((cchTarget > 0), @"Target buffer size must be greater than zero."); - // Copy the string - strncpy(szTarget, [[path stringByExpandingTildeInPath] cString], cchTarget); - - // Make sure it is null terminated (as strncpy does not null-terminate if the buffer is too small) - szTarget[cchTarget - 1] = 0; + // Copy the string getCString:maxLength:encoding: + [path getCString:szTarget maxLength:cchTarget-1 encoding:NSASCIIStringEncoding] ; } /*-----------------------------------------------------------------------*/ /* Pauses emulation */ -void GuiOsx_Pause() +void GuiOsx_Pause(void) { // Pause emulation Main_PauseEmulation(false); @@ -80,8 +78,160 @@ void GuiOsx_Pause() /* Switches back to emulation mode */ -void GuiOsx_Resume() +void GuiOsx_Resume(void) { // Resume emulation Main_UnPauseEmulation(); } + +//----------------------------------------------------------------------------------------------------------- +// Add global services. 6 methods + +@implementation NSApplication (service) + +// Open file or directory +// +- (NSString *)hopenfile:(BOOL)chooseDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types +{ + return [self hopenfile:chooseDir defoDir:defoDir defoFile:defoFile types:types titre:nil] ; +} + + +- (NSString *)hopenfile:(BOOL)chooseDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types titre:(NSString *)titre +{ +NSOpenPanel *openPanel ; + NSArray *lesURLs = nil ; + BOOL btOk ; + + openPanel = [NSOpenPanel openPanel]; + [openPanel setCanChooseDirectories: chooseDir]; + [openPanel setCanChooseFiles: !chooseDir]; + [openPanel setAllowsMultipleSelection:NO] ; + if (types != nil) + { [openPanel setAllowedFileTypes:types] ; + [openPanel setAllowsOtherFileTypes:YES] ; } ; + if (titre != nil) [openPanel setTitle:titre] ; + + if ([openPanel respondsToSelector:@selector(setDirectoryURL:)]) + { if (defoDir!=nil) [openPanel setDirectoryURL:[NSURL fileURLWithPath:defoDir isDirectory:YES]] ; // A partir de 10.6 + if (defoFile!=nil) [openPanel setNameFieldStringValue:defoFile] ; + btOk = [openPanel runModal] == NSOKButton ; // Ok ? + } + else + btOk = [openPanel runModalForDirectory:defoDir file:defoFile] == NSOKButton ; // avant 10.6 + + if (btOk) + { lesURLs = [openPanel URLs] ; + if ((lesURLs != nil) && ([lesURLs count] != 0)) + return [[lesURLs objectAtIndex:0] path] ; + } ; + return @"" ; +} + +// Save file +// +- (NSString *)hsavefile:(BOOL)creatDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types +{ + return [self hsavefile:creatDir defoDir:defoDir defoFile:defoFile types:types titre:nil] ; +} + +- (NSString *)hsavefile:(BOOL)creatDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types titre:(NSString *)titre +{ + NSSavePanel *savPanel ; + NSURL *lURL ; + BOOL btOk ; + + savPanel = [NSSavePanel savePanel]; + [savPanel setCanCreateDirectories:creatDir]; + if (types != nil) + { [savPanel setAllowedFileTypes:types] ; + [savPanel setAllowsOtherFileTypes:YES] ; } ; + if (titre != nil) [savPanel setTitle:titre] ; + + if ([savPanel respondsToSelector:@selector(setDirectoryURL:)]) + { if (defoDir!=nil) [savPanel setDirectoryURL:[NSURL fileURLWithPath:defoDir isDirectory:YES]] ; // A partir de 10.6 + if (defoFile!=nil) [savPanel setNameFieldStringValue:defoFile] ; + btOk = [savPanel runModal] == NSOKButton ; // Ok? + } + else + btOk = [savPanel runModalForDirectory:defoDir file:defoFile] == NSOKButton ; // Ok ? deprecated en 10.6 + + if (btOk) + { lURL = [savPanel URL] ; + if (lURL != nil) + return [lURL path] ; + } ; + return @"" ; +} + +// Returne localized path +// +- (NSString *)localpath:(NSString *)thepath :(NSFileManager *)afilemanager +{ + NSString *thend ; + NSArray *thelist ; + + if (thepath == nil) return @"" ; + if ([thepath length] == 0) return @"" ; + if (![afilemanager fileExistsAtPath:thepath]) + { thend = [thepath lastPathComponent] ; + return [[self localpath:[thepath stringByDeletingLastPathComponent] :afilemanager] stringByAppendingPathComponent:thend] ; + } ; + thelist = [afilemanager componentsToDisplayForPath:thepath] ; // convert in matrix + if ( [thelist count] != 0) + return [NSString pathWithComponents:thelist] ; // return localized path + else + return thepath ; +} + +- (NSString *)localpath:(NSString *)thepath // return a full localized path +{ + NSFileManager *afilemanager = [NSFileManager defaultManager] ; // call "default manager" + return [self localpath:thepath :afilemanager] ; +} + +// return a localized path related to user home directoryr ~/ +// +- (NSString *)pathUser:(NSString *)thepath +{ + NSString *here ; + NSString *apath ; + + apath = [self localpath:thepath] ; + if ([apath length] == 0) return @"" ; + here = [self localpath:[@"~/" stringByExpandingTildeInPath]] ; + if (([apath rangeOfString:here].location) != NSNotFound) + return [NSString stringWithFormat:@"~%@", [apath substringFromIndex:[here length]]] ; + return apath; +} + +// NSAlert available 10.3 to 10.9 +// +- (NSInteger)myAlerte:(NSUInteger)style Txt:(NSString *)Txt firstB:(NSString *)firstB alternateB:(NSString *)alternateB + otherB:(NSString *)otherB informativeTxt:(NSString *)informativeT +{ + NSAlert *lalerte ; + NSInteger ret ; + + lalerte = [[NSAlert alloc] init] ; + [lalerte setAlertStyle:style] ; + if (Txt == nil) + [lalerte setMessageText:@"Hatari"] ; + else + [lalerte setMessageText:Txt] ; + [lalerte addButtonWithTitle:firstB] ; + if (alternateB != nil) [lalerte addButtonWithTitle:alternateB] ; + if (otherB != nil) [lalerte addButtonWithTitle:otherB] ; + if (informativeT!= nil) [lalerte setInformativeText:informativeT] ; + ret = [lalerte runModal] ; + [lalerte release] ; + switch (ret) { + case NSAlertFirstButtonReturn : return NSAlertDefaultReturn ; + break ; + case NSAlertSecondButtonReturn: return NSAlertAlternateReturn ; + break ; + default : return NSAlertOtherReturn ; + } ; +} + +@end