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

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: 
1.1.1.9 ! root      115:        if ([openPanel respondsToSelector:@selector(setDirectoryURL:)])
        !           116:         {      if (defoDir!=nil)  [openPanel setDirectoryURL:[NSURL fileURLWithPath:defoDir isDirectory:YES]] ;        // A partir de 10.6
        !           117:                if (defoFile!=nil) [openPanel setNameFieldStringValue:defoFile] ;
        !           118:                btOk = [openPanel runModal] == NSOKButton ;                                         // Ok ?
        !           119:         }
        !           120:        else
        !           121:                btOk = [openPanel runModalForDirectory:defoDir file:defoFile] == NSOKButton     ;       // avant 10.6
1.1.1.8   root      122: 
                    123:        if (btOk)
                    124:         {      lesURLs = [openPanel URLs] ;
                    125:                if ((lesURLs != nil) && ([lesURLs count] != 0))
                    126:                                return [[lesURLs objectAtIndex:0] path] ;
                    127:         } ;
                    128:        return @"" ;
                    129: }
                    130: 
                    131: // Save file
                    132: //
                    133: - (NSString *)hsavefile:(BOOL)creatDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types
                    134: {
                    135:        return [self hsavefile:creatDir defoDir:defoDir defoFile:defoFile types:types titre:nil] ;
                    136: }
                    137: 
                    138: - (NSString *)hsavefile:(BOOL)creatDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types titre:(NSString *)titre
                    139: {
                    140:        NSSavePanel *savPanel ;
                    141:        NSURL       *lURL ;
                    142:        BOOL        btOk ;
                    143: 
                    144:        savPanel = [NSSavePanel savePanel];
                    145:        [savPanel setCanCreateDirectories:creatDir];
                    146:        if (types != nil)
                    147:         {      [savPanel setAllowedFileTypes:types] ;
                    148:                [savPanel setAllowsOtherFileTypes:YES] ; } ;
                    149:        if (titre != nil)  [savPanel setTitle:titre] ;
                    150: 
1.1.1.9 ! root      151:        if ([savPanel respondsToSelector:@selector(setDirectoryURL:)])
        !           152:         {      if (defoDir!=nil)  [savPanel setDirectoryURL:[NSURL fileURLWithPath:defoDir isDirectory:YES]] ; // A partir de 10.6
        !           153:                if (defoFile!=nil) [savPanel setNameFieldStringValue:defoFile] ;
        !           154:                btOk = [savPanel runModal] == NSOKButton ;                                                                              // Ok?
        !           155:         }
        !           156:        else
        !           157:                btOk = [savPanel runModalForDirectory:defoDir file:defoFile] == NSOKButton ;    // Ok ? deprecated en 10.6
1.1.1.8   root      158: 
                    159:        if (btOk)
                    160:         {      lURL = [savPanel URL] ;
                    161:                if (lURL != nil)
                    162:                        return [lURL path] ;
                    163:         } ;
                    164:        return @"" ;
                    165: }
                    166: 
                    167: // Returne localized path
                    168: //
                    169: - (NSString *)localpath:(NSString *)thepath :(NSFileManager *)afilemanager
                    170: {
                    171:        NSString        *thend ;
                    172:        NSArray         *thelist ;
                    173: 
                    174:        if (thepath == nil) return @"" ;
                    175:        if ([thepath length] == 0) return @"" ;
                    176:        if (![afilemanager fileExistsAtPath:thepath])
                    177:         {      thend = [thepath lastPathComponent] ;
                    178:                return [[self localpath:[thepath stringByDeletingLastPathComponent] :afilemanager] stringByAppendingPathComponent:thend] ;
                    179:         } ;
                    180:        thelist = [afilemanager  componentsToDisplayForPath:thepath] ;                          // convert in matrix
                    181:        if ( [thelist count] != 0)
                    182:                return [NSString pathWithComponents:thelist] ;                          // return localized path
                    183:        else
                    184:                return thepath ;
                    185: }
                    186: 
                    187: - (NSString *)localpath:(NSString *)thepath                                                                            // return a full localized path
                    188: {
                    189:        NSFileManager *afilemanager = [NSFileManager defaultManager] ;              // call "default manager"
                    190:        return [self localpath:thepath :afilemanager] ;
                    191: }
                    192: 
                    193: //  return a localized path related to user home directoryr   ~/
                    194: //
                    195: - (NSString *)pathUser:(NSString *)thepath
                    196: {
                    197:        NSString *here ;
                    198:        NSString *apath ;
                    199: 
                    200:        apath = [self localpath:thepath] ;
                    201:        if ([apath length] == 0) return @"" ;
                    202:        here = [self localpath:[@"~/" stringByExpandingTildeInPath]] ;
                    203:        if (([apath rangeOfString:here].location) != NSNotFound)
                    204:                return [NSString stringWithFormat:@"~%@", [apath substringFromIndex:[here length]]] ;
                    205:        return apath;
                    206: }
                    207: 
1.1.1.9 ! root      208: // NSAlert available 10.3 to 10.9
        !           209: //
        !           210: - (NSInteger)myAlerte:(NSUInteger)style Txt:(NSString *)Txt firstB:(NSString *)firstB alternateB:(NSString *)alternateB
        !           211:                                                                                otherB:(NSString *)otherB informativeTxt:(NSString *)informativeT
        !           212: {
        !           213:        NSAlert         *lalerte ;
        !           214:        NSInteger       ret ;
1.1.1.8   root      215: 
1.1.1.9 ! root      216:        lalerte = [[NSAlert alloc] init] ;
        !           217:        [lalerte setAlertStyle:style] ;
        !           218:        if (Txt == nil)
        !           219:                [lalerte setMessageText:@"Hatari"] ;
        !           220:        else
        !           221:                [lalerte setMessageText:Txt] ;
        !           222:        [lalerte addButtonWithTitle:firstB] ;
        !           223:        if (alternateB != nil)  [lalerte addButtonWithTitle:alternateB] ;
        !           224:        if (otherB != nil)              [lalerte addButtonWithTitle:otherB] ;
        !           225:        if (informativeT!= nil) [lalerte setInformativeText:informativeT] ;
        !           226:        ret = [lalerte runModal] ;
        !           227:        [lalerte release] ;
        !           228:        switch (ret) {
        !           229:        case NSAlertFirstButtonReturn : return NSAlertDefaultReturn ;
        !           230:                                                                        break ;
        !           231:        case NSAlertSecondButtonReturn: return NSAlertAlternateReturn ;
        !           232:                                                                        break ;
        !           233:        default :                                               return NSAlertOtherReturn ;
        !           234:        } ;
        !           235: }
1.1.1.8   root      236: 
1.1.1.9 ! root      237: @end

unix.superglobalmegacorp.com

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