Annotation of hatari/src/gui-osx/Shared.m, revision 1.1.1.8

1.1       root        1: /*
1.1.1.6   root        2:   Hatari - Shared.m
1.1       root        3: 
1.1.1.7   root        4:   This file is distributed under the GNU General Public License, version 2
                      5:   or at your option any later version. Read the file gpl.txt for details.
1.1       root        6: 
                      7:   Helper code used by the other Cocoa code files
                      8: 
                      9:   June 2006, Sébastien Molines - Created
1.1.1.8 ! root       10:   2013, M. SARO
1.1       root       11: */
                     12: 
                     13: #import <Cocoa/Cocoa.h>
                     14: #import "Shared.h"
                     15: #import "AlertHooks.h"
1.1.1.8 ! root       16: #import "main.h"
1.1       root       17: 
                     18: @implementation ModalWrapper
                     19: 
                     20: // Runs an NSWindow modally
                     21: - (void)runModal:(NSWindow*)window
                     22: {
                     23:        // Grab the window
                     24:        modalWindow = window;
                     25: 
                     26:        // Set the window's delegate
                     27:        [window setDelegate:self];
                     28: 
                     29:        // Change emulation and UI state
1.1.1.3   root       30:        GuiOsx_Pause();
1.1       root       31:        
                     32:        // Run it as modal
                     33:        [NSApp runModalForWindow:window];
                     34: 
                     35:        // Restore emulation and UI state
1.1.1.3   root       36:        GuiOsx_Resume();
1.1       root       37: }
                     38: 
                     39: // On closure of the NSWindow, end the modal session
                     40: - (void) windowWillClose:(NSNotification *)notification
                     41: {
                     42:        NSWindow *windowAboutToClose = [notification object];
                     43:        
                     44:        // Is this our modal window?
                     45:        if (windowAboutToClose == modalWindow)
                     46:        {
                     47:                // Stop the modal loop
                     48:                [NSApp stopModal];
                     49:        }
                     50: }
                     51: 
                     52: @end
                     53: 
                     54: /*-----------------------------------------------------------------------*/
                     55: /*
                     56:   Helper function to write the contents of a path as an NSString to a string
                     57: */
                     58: void GuiOsx_ExportPathString(NSString* path, char* szTarget, size_t cchTarget)
                     59: {
                     60:        NSCAssert((szTarget), @"Target buffer must not be null.");
                     61:        NSCAssert((cchTarget > 0), @"Target buffer size must be greater than zero.");
                     62: 
1.1.1.8 ! root       63:        // Copy the string getCString:maxLength:encoding:
        !            64:        [path getCString:szTarget maxLength:cchTarget-1 encoding:NSASCIIStringEncoding] ;
1.1       root       65: }
                     66: 
                     67: /*-----------------------------------------------------------------------*/
                     68: /*
1.1.1.3   root       69:   Pauses emulation
1.1       root       70: */
1.1.1.4   root       71: void GuiOsx_Pause(void)
1.1       root       72: {
                     73:        // Pause emulation
1.1.1.3   root       74:        Main_PauseEmulation(false);
1.1       root       75: }
                     76: 
                     77: /*-----------------------------------------------------------------------*/
                     78: /*
1.1.1.3   root       79:   Switches back to emulation mode
1.1       root       80: */
1.1.1.4   root       81: void GuiOsx_Resume(void)
1.1       root       82: {
                     83:        // Resume emulation
                     84:        Main_UnPauseEmulation();
                     85: }
1.1.1.8 ! root       86: 
        !            87: //-----------------------------------------------------------------------------------------------------------
        !            88: // Add global services.  6 methods
        !            89: 
        !            90: @implementation NSApplication (service)
        !            91: 
        !            92: // Open file or directory
        !            93: //
        !            94: - (NSString *)hopenfile:(BOOL)chooseDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types
        !            95: {
        !            96:        return [self hopenfile:chooseDir defoDir:defoDir defoFile:defoFile types:types titre:nil] ;
        !            97: }
        !            98: 
        !            99: 
        !           100: - (NSString *)hopenfile:(BOOL)chooseDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types titre:(NSString *)titre
        !           101: {
        !           102: NSOpenPanel *openPanel ;
        !           103:        NSArray     *lesURLs = nil ;
        !           104:        BOOL        btOk ;
        !           105: 
        !           106:        openPanel = [NSOpenPanel openPanel];
        !           107:        [openPanel      setCanChooseDirectories: chooseDir];
        !           108:        [openPanel      setCanChooseFiles: !chooseDir];
        !           109:        [openPanel      setAllowsMultipleSelection:NO] ;
        !           110:        if (types != nil) 
        !           111:         {      [openPanel      setAllowedFileTypes:types] ;
        !           112:                [openPanel      setAllowsOtherFileTypes:YES] ;  } ;
        !           113:        if (titre != nil)  [openPanel setTitle:titre] ;
        !           114: 
        !           115: #if MAC_OS_X_VERSION_MAX_ALLOWED  > 1058
        !           116: 
        !           117:        if (defoDir!=nil)  [openPanel setDirectoryURL:[NSURL URLWithString:defoDir]] ;      //  10.6 & newer
        !           118:        if (defoFile!=nil) [openPanel setNameFieldStringValue:defoFile] ;
        !           119:        btOk = [openPanel runModal] == NSOKButton ;                                         // Ok ?
        !           120: #else                                                                                   // */
        !           121:        btOk = [openPanel runModalForDirectory:defoDir file:defoFile] == NSOKButton     ;       // 10.5
        !           122: #endif
        !           123: 
        !           124:        if (btOk)
        !           125:         {      lesURLs = [openPanel URLs] ;
        !           126:                if ((lesURLs != nil) && ([lesURLs count] != 0))
        !           127:                                return [[lesURLs objectAtIndex:0] path] ;
        !           128:         } ;
        !           129:        return @"" ;
        !           130: }
        !           131: 
        !           132: // Save file
        !           133: //
        !           134: - (NSString *)hsavefile:(BOOL)creatDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types
        !           135: {
        !           136:        return [self hsavefile:creatDir defoDir:defoDir defoFile:defoFile types:types titre:nil] ;
        !           137: }
        !           138: 
        !           139: - (NSString *)hsavefile:(BOOL)creatDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types titre:(NSString *)titre
        !           140: {
        !           141:        NSSavePanel *savPanel ;
        !           142:        NSURL       *lURL ;
        !           143:        BOOL        btOk ;
        !           144: 
        !           145:        savPanel = [NSSavePanel savePanel];
        !           146:        [savPanel setCanCreateDirectories:creatDir];
        !           147:        if (types != nil)
        !           148:         {      [savPanel setAllowedFileTypes:types] ;
        !           149:                [savPanel setAllowsOtherFileTypes:YES] ; } ;
        !           150:        if (titre != nil)  [savPanel setTitle:titre] ;
        !           151: 
        !           152: #if MAC_OS_X_VERSION_MAX_ALLOWED > 1058
        !           153: 
        !           154:        if (defoDir!=nil)  [savPanel setDirectoryURL:[NSURL URLWithString:defoDir]] ;   // A partir de 10.6
        !           155:        if (defoFile!=nil) [savPanel setNameFieldStringValue:defoFile] ;
        !           156:        btOk = [savPanel runModal] == NSOKButton ;                                                                              // Ok?
        !           157: 
        !           158: #else                                                                                                                                                          // */
        !           159:        btOk = [savPanel runModalForDirectory:defoDir file:defoFile] == NSOKButton ;    // Ok ? deprecated en 10.6
        !           160: #endif
        !           161:        if (btOk)
        !           162:         {      lURL = [savPanel URL] ;
        !           163:                if (lURL != nil)
        !           164:                        return [lURL path] ;
        !           165:         } ;
        !           166:        return @"" ;
        !           167: }
        !           168: 
        !           169: // Returne localized path
        !           170: //
        !           171: - (NSString *)localpath:(NSString *)thepath :(NSFileManager *)afilemanager
        !           172: {
        !           173:        NSString        *thend ;
        !           174:        NSArray         *thelist ;
        !           175: 
        !           176:        if (thepath == nil) return @"" ;
        !           177:        if ([thepath length] == 0) return @"" ;
        !           178:        if (![afilemanager fileExistsAtPath:thepath])
        !           179:         {      thend = [thepath lastPathComponent] ;
        !           180:                return [[self localpath:[thepath stringByDeletingLastPathComponent] :afilemanager] stringByAppendingPathComponent:thend] ;
        !           181:         } ;
        !           182:        thelist = [afilemanager  componentsToDisplayForPath:thepath] ;                          // convert in matrix
        !           183:        if ( [thelist count] != 0)
        !           184:                return [NSString pathWithComponents:thelist] ;                          // return localized path
        !           185:        else
        !           186:                return thepath ;
        !           187: }
        !           188: 
        !           189: - (NSString *)localpath:(NSString *)thepath                                                                            // return a full localized path
        !           190: {
        !           191:        NSFileManager *afilemanager = [NSFileManager defaultManager] ;              // call "default manager"
        !           192:        return [self localpath:thepath :afilemanager] ;
        !           193: }
        !           194: 
        !           195: //  return a localized path related to user home directoryr   ~/
        !           196: //
        !           197: - (NSString *)pathUser:(NSString *)thepath
        !           198: {
        !           199:        NSString *here ;
        !           200:        NSString *apath ;
        !           201: 
        !           202:        apath = [self localpath:thepath] ;
        !           203:        if ([apath length] == 0) return @"" ;
        !           204:        here = [self localpath:[@"~/" stringByExpandingTildeInPath]] ;
        !           205:        if (([apath rangeOfString:here].location) != NSNotFound)
        !           206:                return [NSString stringWithFormat:@"~%@", [apath substringFromIndex:[here length]]] ;
        !           207:        return apath;
        !           208: }
        !           209: 
        !           210: @end
        !           211: 
        !           212: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.