| 1 |
// |
| 2 |
// osx.m |
| 3 |
// Hivelytracker: OSX dialog and file reqester support |
| 4 |
// |
| 5 |
// Created by Christopher O'Neill on 15/10/2012. |
| 6 |
// |
| 7 |
|
| 8 |
#import <Cocoa/Cocoa.h> |
| 9 |
#import "NSFileManager+DirectoryLocations.h" |
| 10 |
|
| 11 |
typedef uint32_t uint32; |
| 12 |
typedef int32_t int32; |
| 13 |
typedef char TEXT; |
| 14 |
|
| 15 |
extern BOOL enableKeys; |
| 16 |
|
| 17 |
int32 gui_req( uint32 img, const TEXT *title, const TEXT *reqtxt, const TEXT *buttons ) |
| 18 |
{ |
| 19 |
int i; |
| 20 |
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
| 21 |
[alert setMessageText:[NSString stringWithCString:title encoding:NSASCIIStringEncoding]]; |
| 22 |
[alert setInformativeText:[NSString stringWithCString:reqtxt encoding:NSASCIIStringEncoding]]; |
| 23 |
NSMutableString *btnStr = [NSMutableString stringWithCString:buttons encoding:NSASCIIStringEncoding]; |
| 24 |
[btnStr replaceOccurrencesOfString:@"_" withString:@"" options:0 |
| 25 |
range:NSMakeRange(0, [btnStr length])]; |
| 26 |
NSArray *btnArr = [btnStr componentsSeparatedByString:@"|"]; |
| 27 |
for (btnStr in [[btnArr reverseObjectEnumerator] allObjects]) |
| 28 |
{ |
| 29 |
[alert addButtonWithTitle:btnStr]; |
| 30 |
} |
| 31 |
|
| 32 |
enableKeys = TRUE; |
| 33 |
i = [alert runModal] - 1000; |
| 34 |
enableKeys = FALSE; |
| 35 |
return i; |
| 36 |
} |
| 37 |
|
| 38 |
#define FR_HVLSAVE 0 |
| 39 |
#define FR_AHXSAVE 1 |
| 40 |
#define FR_INSSAVE 2 |
| 41 |
#define FR_MODLOAD 3 |
| 42 |
#define FR_INSLOAD 4 |
| 43 |
|
| 44 |
char *filerequester( const char *title, const char *path, const char *fname, int type ) |
| 45 |
{ |
| 46 |
NSArray *fileTypes; |
| 47 |
NSSavePanel *savePanel; |
| 48 |
NSOpenPanel *panel; |
| 49 |
switch (type) { |
| 50 |
case FR_HVLSAVE: |
| 51 |
fileTypes = [NSArray arrayWithObjects:@"hvl", @"HVL", nil]; |
| 52 |
break; |
| 53 |
case FR_AHXSAVE: |
| 54 |
fileTypes = [NSArray arrayWithObjects:@"ahx", @"AHX", nil]; |
| 55 |
break; |
| 56 |
case FR_INSSAVE: |
| 57 |
case FR_INSLOAD: |
| 58 |
fileTypes = [NSArray arrayWithObjects:@"ins", @"INS", nil]; |
| 59 |
break; |
| 60 |
case FR_MODLOAD: |
| 61 |
fileTypes = [NSArray arrayWithObjects:@"hvl", @"HVL", @"ahx", @"AHX", nil]; |
| 62 |
break; |
| 63 |
default: |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
if ((type == FR_INSLOAD) || (type == FR_MODLOAD)) { |
| 68 |
panel = [NSOpenPanel openPanel]; |
| 69 |
} else { |
| 70 |
savePanel = [NSSavePanel savePanel]; |
| 71 |
panel = (NSOpenPanel *)savePanel; |
| 72 |
} |
| 73 |
|
| 74 |
[panel setAllowedFileTypes:fileTypes]; |
| 75 |
[panel setAllowsOtherFileTypes:YES]; |
| 76 |
[panel setTitle:[NSString stringWithCString:title encoding:NSASCIIStringEncoding]]; |
| 77 |
[panel setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithCString:path encoding:NSASCIIStringEncoding] isDirectory:YES]]; |
| 78 |
[panel setNameFieldStringValue:[NSString stringWithCString:fname encoding:NSASCIIStringEncoding]]; |
| 79 |
|
| 80 |
enableKeys = TRUE; |
| 81 |
int i = [panel runModal]; |
| 82 |
enableKeys = FALSE; |
| 83 |
if (i == 1) { |
| 84 |
const char *cstr = [[[panel URL] path] UTF8String]; |
| 85 |
char *rval = (char *)malloc(strlen(cstr) + 1); |
| 86 |
strcpy(rval, cstr); |
| 87 |
return rval; |
| 88 |
}; |
| 89 |
|
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
BOOL directoryrequester( const char *title, char *path ) |
| 94 |
{ |
| 95 |
NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 96 |
[panel setCanChooseFiles:NO]; |
| 97 |
[panel setCanChooseDirectories:YES]; |
| 98 |
[panel setTitle: [NSString stringWithCString:title encoding:NSASCIIStringEncoding]]; |
| 99 |
[panel setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithCString:path encoding:NSASCIIStringEncoding] isDirectory:YES]]; |
| 100 |
enableKeys = FALSE; |
| 101 |
int i = [panel runModal]; |
| 102 |
enableKeys = FALSE; |
| 103 |
if (i == 1) { |
| 104 |
const char *cstr = [[[panel URL] path] UTF8String]; |
| 105 |
strncpy(path, cstr, 512); |
| 106 |
return TRUE; |
| 107 |
}; |
| 108 |
|
| 109 |
return FALSE; |
| 110 |
} |
| 111 |
|
| 112 |
char *osxGetPrefsPath() |
| 113 |
{ |
| 114 |
static char *path; |
| 115 |
|
| 116 |
if(!path) { |
| 117 |
NSString *strPath = [[NSFileManager defaultManager] applicationSupportDirectory]; |
| 118 |
strPath = [strPath stringByAppendingPathComponent:@"ht.prefs"]; |
| 119 |
path = malloc([strPath length] + 1); |
| 120 |
strcpy(path, [strPath UTF8String]); |
| 121 |
} |
| 122 |
return path; |
| 123 |
} |
| 124 |
|
| 125 |
char *osxGetResourcesPath(char *path, const char *pathAppend) |
| 126 |
{ |
| 127 |
NSBundle *bundle = [NSBundle mainBundle]; |
| 128 |
NSString *strPath = [bundle resourcePath]; |
| 129 |
strPath = [strPath stringByAppendingPathComponent:[NSString stringWithUTF8String:pathAppend]]; |
| 130 |
if(!path) |
| 131 |
path = malloc([strPath length] + 1); |
| 132 |
strcpy(path, [strPath UTF8String]); |
| 133 |
return path; |
| 134 |
} |