|
|
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.10! root 10: 2013, 2016 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.10! root 30: GuiOsx_Pause(true);
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: {
1.1.1.10! root 42: NSWindow *windowAboutToClose = notification.object ;
1.1 root 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: /*-----------------------------------------------------------------------*/
1.1.1.10! root 55: /*Helper function to write the contents of a path as an NSString to a string*/
! 56: /*----------------------------------------------------------------------*/
1.1 root 57: void GuiOsx_ExportPathString(NSString* path, char* szTarget, size_t cchTarget)
58: {
59: NSCAssert((szTarget), @"Target buffer must not be null.");
60: NSCAssert((cchTarget > 0), @"Target buffer size must be greater than zero.");
61:
1.1.1.8 root 62: // Copy the string getCString:maxLength:encoding:
63: [path getCString:szTarget maxLength:cchTarget-1 encoding:NSASCIIStringEncoding] ;
1.1 root 64: }
65:
1.1.1.10! root 66: /*----------------------------------------------------------------------*/
! 67: /*Pauses emulation */
! 68: /* Added the visualize option for 2.0.0 */
! 69: /*----------------------------------------------------------------------*/
! 70: void GuiOsx_Pause(bool visualize)
1.1 root 71: {
72: // Pause emulation
1.1.1.10! root 73: Main_PauseEmulation(visualize);
1.1 root 74: }
75:
76: /*-----------------------------------------------------------------------*/
1.1.1.10! root 77: /*Switches back to emulation mode */
! 78: /*----------------------------------------------------------------------*/
1.1.1.4 root 79: void GuiOsx_Resume(void)
1.1 root 80: {
81: // Resume emulation
82: Main_UnPauseEmulation();
83: }
1.1.1.8 root 84:
1.1.1.10! root 85: /*----------------------------------------------------------------------*/
1.1.1.8 root 86: // Add global services. 6 methods
1.1.1.10! root 87: /*----------------------------------------------------------------------*/
1.1.1.8 root 88:
89: @implementation NSApplication (service)
90:
91: // Open file or directory
92: //
93: - (NSString *)hopenfile:(BOOL)chooseDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types
94: {
95: return [self hopenfile:chooseDir defoDir:defoDir defoFile:defoFile types:types titre:nil] ;
96: }
97:
1.1.1.10! root 98: /*----------------------------------------------------------------------*/
1.1.1.8 root 99: - (NSString *)hopenfile:(BOOL)chooseDir defoDir:(NSString *)defoDir defoFile:(NSString *)defoFile types:(NSArray *)types titre:(NSString *)titre
100: {
101: NSOpenPanel *openPanel ;
102: NSArray *lesURLs = nil ;
103: BOOL btOk ;
104:
105: openPanel = [NSOpenPanel openPanel];
1.1.1.10! root 106: openPanel.canChooseDirectories = chooseDir ;
! 107: openPanel.canChooseFiles = !chooseDir;
! 108: openPanel.allowsMultipleSelection = NO ;
1.1.1.8 root 109: if (types != nil)
1.1.1.10! root 110: { openPanel.allowedFileTypes = types ;
! 111: openPanel.allowsOtherFileTypes = YES ; } ;
! 112: if (titre != nil) openPanel.title = titre ;
! 113:
! 114: // if ([openPanel respondsToSelector:@selector(setDirectoryURL:)])
! 115: { if (defoDir!=nil) openPanel.directoryURL = [NSURL fileURLWithPath:defoDir isDirectory:YES] ; // A partir de 10.6
! 116: if (defoFile!=nil) openPanel.nameFieldStringValue = defoFile ;
! 117: btOk = [openPanel runModal] == NSModalResponseOK ; // Ok ?
1.1.1.9 root 118: }
1.1.1.10! root 119: // else
! 120: // btOk = [openPanel runModalForDirectory:defoDir file:defoFile] == NSModalResponseOK; // avant 10.6
1.1.1.8 root 121:
122: if (btOk)
1.1.1.10! root 123: { lesURLs = openPanel.URLs ;
! 124: if ((lesURLs != nil) && (lesURLs.count != 0))
1.1.1.8 root 125: return [[lesURLs objectAtIndex:0] path] ;
126: } ;
127: return @"" ;
128: }
129:
1.1.1.10! root 130: /*----------------------------------------------------------------------*/
1.1.1.8 root 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: }
1.1.1.10! root 137: /*----------------------------------------------------------------------*/
1.1.1.8 root 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];
1.1.1.10! root 145: savPanel.canCreateDirectories = creatDir ;
1.1.1.8 root 146: if (types != nil)
1.1.1.10! root 147: { savPanel.allowedFileTypes = types ;
! 148: savPanel.allowsOtherFileTypes = YES ; } ;
! 149: if (titre != nil) savPanel.title = titre ;
! 150:
! 151: // if ([savPanel respondsToSelector:@selector(setDirectoryURL:)])
! 152: { if (defoDir!=nil) savPanel.directoryURL = [NSURL fileURLWithPath:defoDir isDirectory:YES] ; // A partir de 10.6
! 153: if (defoFile!=nil) savPanel.nameFieldStringValue = defoFile ;
! 154: btOk = [savPanel runModal] == NSModalResponseOK; // Ok?
1.1.1.9 root 155: }
1.1.1.10! root 156: // else
! 157: // btOk = [savPanel runModalForDirectory:defoDir file:defoFile] == NSOKButton ; // Ok ? deprecated en 10.6
1.1.1.8 root 158:
159: if (btOk)
1.1.1.10! root 160: { lURL = savPanel.URL ;
1.1.1.8 root 161: if (lURL != nil)
1.1.1.10! root 162: return lURL.path ;
1.1.1.8 root 163: } ;
164: return @"" ;
165: }
1.1.1.10! root 166: /*----------------------------------------------------------------------*/
1.1.1.8 root 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 @"" ;
1.1.1.10! root 175: if (thepath.length == 0) return @"" ;
1.1.1.8 root 176: if (![afilemanager fileExistsAtPath:thepath])
1.1.1.10! root 177: { thend = thepath.lastPathComponent ;
! 178: return [[self localpath:thepath.stringByDeletingLastPathComponent :afilemanager] stringByAppendingPathComponent:thend] ;
1.1.1.8 root 179: } ;
180: thelist = [afilemanager componentsToDisplayForPath:thepath] ; // convert in matrix
1.1.1.10! root 181: if ( thelist.count != 0)
1.1.1.8 root 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: }
1.1.1.10! root 192: /*----------------------------------------------------------------------*/
1.1.1.8 root 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] ;
1.1.1.10! root 201: if (apath.length == 0) return @"" ;
! 202: here = [self localpath: @"~/".stringByExpandingTildeInPath ] ;
1.1.1.8 root 203: if (([apath rangeOfString:here].location) != NSNotFound)
1.1.1.10! root 204: return [NSString stringWithFormat:@"~%@", [apath substringFromIndex:here.length ]] ;
1.1.1.8 root 205: return apath;
206: }
1.1.1.10! root 207: /*----------------------------------------------------------------------*/
! 208: // NSAlert available 10.3 to 10.9 ..... 10.11
1.1.1.9 root 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] ;
1.1.1.10! root 217: lalerte.alertStyle = style ;
! 218: lalerte.messageText = Txt == nil ? @"Hatari" : Txt ;
1.1.1.9 root 219: [lalerte addButtonWithTitle:firstB] ;
220: if (alternateB != nil) [lalerte addButtonWithTitle:alternateB] ;
221: if (otherB != nil) [lalerte addButtonWithTitle:otherB] ;
222: if (informativeT!= nil) [lalerte setInformativeText:informativeT] ;
223: ret = [lalerte runModal] ;
224: [lalerte release] ;
225: switch (ret) {
1.1.1.10! root 226: case NSAlertFirstButtonReturn :
! 227: return NSAlertFirstButtonReturn;
! 228: case NSAlertSecondButtonReturn:
! 229: return NSAlertSecondButtonReturn;
! 230: default:
! 231: return NSAlertFirstButtonReturn;
1.1.1.9 root 232: } ;
233: }
1.1.1.8 root 234:
1.1.1.9 root 235: @end
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.