1#include "compat.h"
2#include "osxbits.h"
3#import <Foundation/Foundation.h>
4#import <AppKit/AppKit.h>
5
6#ifndef MAC_OS_X_VERSION_10_5
7# define NSImageScaleNone NSScaleNone
8#endif
9
10#ifndef MAC_OS_X_VERSION_10_12
11# define NSEventModifierFlagOption NSAlternateKeyMask
12# define NSEventModifierFlagCommand NSCommandKeyMask
13# define NSEventMaskAny NSAnyEventMask
14# define NSWindowStyleMaskTitled NSTitledWindowMask
15# define NSWindowStyleMaskClosable NSClosableWindowMask
16# define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
17# define NSWindowStyleMaskResizable NSResizableWindowMask
18# define NSAlertStyleInformational NSInformationalAlertStyle
19# define NSControlSizeSmall NSSmallControlSize
20#endif
21
22#ifndef MAC_OS_VERSION_10_3
23# define MAC_OS_VERSION_10_3 1030
24#endif
25
26id nsapp;
27
28void osx_preopen(void)
29{
30    // fix for "ld: absolute address to symbol _NSApp in a different linkage unit not supported"
31    // (OS X 10.6) when building for PPC
32    nsapp = [NSApplication sharedApplication];
33}
34
35void osx_postopen(void)
36{
37    [nsapp finishLaunching];
38}
39
40int osx_msgbox(const char *name, const char *msg)
41{
42	NSString *mmsg = [[NSString alloc] initWithUTF8String:msg];
43
44#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
45	NSAlert *alert = [[NSAlert alloc] init];
46	[alert addButtonWithTitle: @"OK"];
47	[alert setInformativeText: mmsg];
48	[alert setAlertStyle: NSAlertStyleInformational];
49
50	[alert runModal];
51
52	[alert release];
53
54#else
55	NSRunAlertPanel(nil, mmsg, @"OK", nil, nil);
56#endif
57
58	[mmsg release];
59
60	UNREFERENCED_PARAMETER(name);
61
62	return 0;
63}
64
65int osx_ynbox(const char *name, const char *msg)
66{
67	NSString *mmsg = [[NSString alloc] initWithUTF8String:msg];
68	int r;
69
70#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
71	NSAlert *alert = [[NSAlert alloc] init];
72
73	[alert addButtonWithTitle:@"Yes"];
74	[alert addButtonWithTitle:@"No"];
75	[alert setInformativeText: mmsg];
76	[alert setAlertStyle: NSAlertStyleInformational];
77
78	r = ([alert runModal] == NSAlertFirstButtonReturn);
79
80	[alert release];
81#else
82	r = (NSRunAlertPanel(nil, mmsg, @"Yes", @"No", nil) == NSAlertDefaultReturn);
83#endif
84
85	[mmsg release];
86
87	UNREFERENCED_PARAMETER(name);
88
89	return r;
90}
91
92char *osx_gethomedir(void)
93{
94    NSString *path = NSHomeDirectory();
95    const char *Cpath = [path UTF8String];
96    char *returnpath = NULL;
97
98    if (Cpath)
99        returnpath = Bstrdup(Cpath);
100
101    [path release];
102
103    return returnpath;
104}
105
106char *osx_getsupportdir(int32_t local)
107{
108    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, local ? NSUserDomainMask : NSLocalDomainMask, YES);
109    char *returnpath = NULL;
110
111    if ([paths count] > 0)
112    {
113        const char *Cpath = [[paths objectAtIndex:0] UTF8String];
114
115        if (Cpath)
116            returnpath = Bstrdup(Cpath);
117    }
118
119    [paths release];
120
121    return returnpath;
122}
123
124char *osx_getappdir(void)
125{
126    CFBundleRef mainBundle;
127    CFURLRef resUrl, fullUrl;
128	CFStringRef str;
129    const char *s;
130    char *dir = NULL;
131
132    mainBundle = CFBundleGetMainBundle();
133    if (!mainBundle) {
134        return NULL;
135    }
136
137    resUrl = CFBundleCopyResourcesDirectoryURL(mainBundle);
138    CFRelease(mainBundle);
139    if (!resUrl) {
140        return NULL;
141    }
142    fullUrl = CFURLCopyAbsoluteURL(resUrl);
143    if (fullUrl) {
144        CFRelease(resUrl);
145        resUrl = fullUrl;
146    }
147
148	str = CFURLCopyFileSystemPath(resUrl, kCFURLPOSIXPathStyle);
149    CFRelease(resUrl);
150    if (!str) {
151        return NULL;
152    }
153
154    s = CFStringGetCStringPtr(str, CFStringGetSystemEncoding());
155    if (s) {
156        dir = strdup(s);
157    }
158    CFRelease(str);
159
160    return dir;
161}
162
163char *osx_getapplicationsdir(int32_t local)
164{
165    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSAllApplicationsDirectory, local ? NSUserDomainMask : NSLocalDomainMask, YES);
166    char *returnpath = NULL;
167
168    if ([paths count] > 0)
169    {
170        const char *Cpath = [[paths objectAtIndex:0] UTF8String];
171
172        if (Cpath)
173            returnpath = Bstrdup(Cpath);
174    }
175
176    [paths release];
177
178    return returnpath;
179}
180
181char *osx_getdocumentsdir(int32_t local)
182{
183    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, local ? NSUserDomainMask : NSLocalDomainMask, YES);
184    char *returnpath = NULL;
185
186    if ([paths count] > 0)
187    {
188        const char *Cpath = [[paths objectAtIndex:0] UTF8String];
189
190        if (Cpath)
191            returnpath = Bstrdup(Cpath);
192    }
193
194    [paths release];
195
196    return returnpath;
197}
198