forked from AndrewBelt/osdialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osdialog_mac.m
203 lines (157 loc) · 5.53 KB
/
osdialog_mac.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <AppKit/AppKit.h>
#include <Availability.h>
#include "osdialog.h"
int osdialog_message(osdialog_message_level level, osdialog_message_buttons buttons, const char* message) {
@autoreleasepool {
NSWindow* keyWindow = [[NSApplication sharedApplication] keyWindow];
NSAlert* alert = [[NSAlert alloc] init];
switch (level) {
default:
#ifdef __MAC_10_12
case OSDIALOG_INFO: [alert setAlertStyle:NSAlertStyleInformational]; break;
case OSDIALOG_WARNING: [alert setAlertStyle:NSAlertStyleWarning]; break;
case OSDIALOG_ERROR: [alert setAlertStyle:NSAlertStyleCritical]; break;
#else
case OSDIALOG_INFO: [alert setAlertStyle:NSInformationalAlertStyle]; break;
case OSDIALOG_WARNING: [alert setAlertStyle:NSWarningAlertStyle]; break;
case OSDIALOG_ERROR: [alert setAlertStyle:NSCriticalAlertStyle]; break;
#endif
}
switch (buttons) {
default:
case OSDIALOG_OK:
[alert addButtonWithTitle:@"OK"];
break;
case OSDIALOG_OK_CANCEL:
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
break;
case OSDIALOG_YES_NO:
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
break;
}
NSString* messageString = [NSString stringWithUTF8String:message];
[alert setMessageText:messageString];
// Non-bold text
// [alert setInformativeText:messageString];
NSInteger button = [alert runModal];
[keyWindow makeKeyAndOrderFront:nil];
return (button == NSAlertFirstButtonReturn);
} // @autoreleasepool
}
char* osdialog_prompt(osdialog_message_level level, const char* message, const char* text) {
@autoreleasepool {
NSWindow* keyWindow = [[NSApplication sharedApplication] keyWindow];
NSAlert* alert = [[NSAlert alloc] init];
switch (level) {
default:
#ifdef __MAC_10_12
case OSDIALOG_INFO: [alert setAlertStyle:NSAlertStyleInformational]; break;
case OSDIALOG_WARNING: [alert setAlertStyle:NSAlertStyleWarning]; break;
case OSDIALOG_ERROR: [alert setAlertStyle:NSAlertStyleCritical]; break;
#else
case OSDIALOG_INFO: [alert setAlertStyle:NSInformationalAlertStyle]; break;
case OSDIALOG_WARNING: [alert setAlertStyle:NSWarningAlertStyle]; break;
case OSDIALOG_ERROR: [alert setAlertStyle:NSCriticalAlertStyle]; break;
#endif
}
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
NSString* messageString = [NSString stringWithUTF8String:message];
[alert setMessageText:messageString];
NSTextField* input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
[alert setAccessoryView:input];
if (text) {
NSString* path_str = [NSString stringWithUTF8String:text];
[input setStringValue:path_str];
}
NSInteger button = [alert runModal];
char* result = NULL;
if (button == NSAlertFirstButtonReturn) {
[input validateEditing];
NSString* result_str = [input stringValue];
result = osdialog_strndup([result_str UTF8String], [result_str length]);
}
[keyWindow makeKeyAndOrderFront:nil];
return result;
} // @autoreleasepool
}
char* osdialog_file(osdialog_file_action action, const char* path, const char* filename, osdialog_filters* filters) {
@autoreleasepool {
NSWindow* keyWindow = [[NSApplication sharedApplication] keyWindow];
NSSavePanel* panel;
// NSOpenPanel is a subclass of NSSavePanel. Not defined for OSDIALOG_SAVE.
NSOpenPanel* open_panel;
if (action == OSDIALOG_OPEN || action == OSDIALOG_OPEN_DIR) {
panel = open_panel = [NSOpenPanel openPanel];
}
else {
panel = [NSSavePanel savePanel];
}
// Bring dialog to front
// https://stackoverflow.com/a/2402069
// Thanks Dave!
[panel setLevel:CGShieldingWindowLevel()];
if (filters) {
NSMutableArray* fileTypes = [[NSMutableArray alloc] init];
for (; filters; filters = filters->next) {
for (osdialog_filter_patterns* patterns = filters->patterns; patterns; patterns = patterns->next) {
NSString* fileType = [NSString stringWithUTF8String:patterns->pattern];
[fileTypes addObject:fileType];
}
}
[panel setAllowedFileTypes:fileTypes];
}
if (action == OSDIALOG_OPEN || action == OSDIALOG_OPEN_DIR) {
[open_panel setAllowsMultipleSelection:NO];
}
if (action == OSDIALOG_OPEN) {
[open_panel setCanChooseDirectories:NO];
[open_panel setCanChooseFiles:YES];
}
if (action == OSDIALOG_OPEN_DIR) {
[open_panel setCanCreateDirectories:YES];
[open_panel setCanChooseDirectories:YES];
[open_panel setCanChooseFiles:NO];
}
if (path) {
NSString* path_str = [NSString stringWithUTF8String:path];
NSURL* path_url = [NSURL fileURLWithPath:path_str];
[panel setDirectoryURL:path_url];
}
if (filename) {
NSString* filenameString = [NSString stringWithUTF8String:filename];
[panel setNameFieldStringValue:filenameString];
}
char* result = NULL;
NSModalResponse response = [panel runModal];
#ifdef __MAC_10_9
#define OK NSModalResponseOK
#else
#define OK NSOKButton
#endif
if (response == OK) {
NSURL* result_url = [panel URL];
NSString* result_str = [result_url path];
result = osdialog_strndup([result_str UTF8String], [result_str length]);
}
[keyWindow makeKeyAndOrderFront:nil];
return result;
} // @autoreleasepool
}
int osdialog_color_picker(osdialog_color* color, int opacity) {
assert(0);
@autoreleasepool {
// TODO I have no idea what I'm doing here
NSColorPanel* panel = [NSColorPanel sharedColorPanel];
// [panel setDelegate:self];
[panel isVisible];
// if (opacity)
// [panel setShowAlpha:YES];
// else
// [panel setShowAlpha:NO];
// [panel makeKeyAndOrderFront:self];
return 0;
} // @autoreleasepool
}