dlg.m 5.77 KB
Newer Older
1
2
3
4
5
#import <Cocoa/Cocoa.h>
#include "dlg.h"
#include <string.h>
#include <sys/syslimits.h>

6
7
8
9
10
// Import UniformTypeIdentifiers for macOS 11+
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#endif

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
void* NSStr(void* buf, int len) {
	return (void*)[[NSString alloc] initWithBytes:buf length:len encoding:NSUTF8StringEncoding];
}

void checkActivationPolicy() {
	NSApplicationActivationPolicy policy = [NSApp activationPolicy];
	// prohibited NSApp will not show the panel at all.
	// It probably means that this is not run in a GUI app, that would set the policy on its own,
	// but in a terminal app - setting it to accessory will allow dialogs to show
	if (policy == NSApplicationActivationPolicyProhibited) {
		[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
	}
}

void NSRelease(void* obj) {
	[(NSObject*)obj release];
}

@interface AlertDlg : NSObject {
	AlertDlgParams* params;
	DlgResult result;
}
+ (AlertDlg*)init:(AlertDlgParams*)params;
- (DlgResult)run;
@end

DlgResult alertDlg(AlertDlgParams* params) {
	return [[AlertDlg init:params] run];
}

@implementation AlertDlg
+ (AlertDlg*)init:(AlertDlgParams*)params {
	AlertDlg* d = [AlertDlg alloc];
	d->params = params;
	return d;
}

- (DlgResult)run {
	if(![NSThread isMainThread]) {
		[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
		return self->result;
	}
	NSAlert* alert = [[NSAlert alloc] init];
	if(self->params->title != nil) {
		[[alert window] setTitle:[[NSString alloc] initWithUTF8String:self->params->title]];
	}
	[alert setMessageText:[[NSString alloc] initWithUTF8String:self->params->msg]];
	switch (self->params->style) {
	case MSG_YESNO:
		[alert addButtonWithTitle:@"Yes"];
		[alert addButtonWithTitle:@"No"];
		break;
	case MSG_ERROR:
		[alert setIcon:[NSImage imageNamed:NSImageNameCaution]];
		[alert addButtonWithTitle:@"OK"];
		break;
	case MSG_INFO:
		[alert setIcon:[NSImage imageNamed:NSImageNameInfo]];
		[alert addButtonWithTitle:@"OK"];
		break;
	}

	checkActivationPolicy();

	self->result = [alert runModal] == NSAlertFirstButtonReturn ? DLG_OK : DLG_CANCEL;
	return self->result;
}
@end

@interface FileDlg : NSObject {
	FileDlgParams* params;
	DlgResult result;
}
+ (FileDlg*)init:(FileDlgParams*)params;
- (DlgResult)run;
@end

DlgResult fileDlg(FileDlgParams* params) {
	return [[FileDlg init:params] run];
}

@implementation FileDlg
+ (FileDlg*)init:(FileDlgParams*)params {
	FileDlg* d = [FileDlg alloc];
	d->params = params;
	return d;
}

- (DlgResult)run {
	if(![NSThread isMainThread]) {
		[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
	} else if(self->params->mode == SAVEDLG) {
		self->result = [self save];
	} else {
		self->result = [self load];
	}
	return self->result;
}

- (NSInteger)runPanel:(NSSavePanel*)panel {
	[panel setFloatingPanel:YES];
	[panel setShowsHiddenFiles:self->params->showHidden ? YES : NO];
	[panel setCanCreateDirectories:YES];
	if(self->params->title != nil) {
		[panel setTitle:[[NSString alloc] initWithUTF8String:self->params->title]];
	}
117
	// Use modern allowedContentTypes API for better file type support (especially video files)
118
	if(self->params->numext > 0) {
119
120
121
122
123
124
125
126
127
128
129
		NSMutableArray *utTypes = [NSMutableArray arrayWithCapacity:self->params->numext];
		NSString** exts = (NSString**)self->params->exts;
		for(int i = 0; i < self->params->numext; i++) {
			UTType *type = [UTType typeWithFilenameExtension:exts[i]];
			if(type) {
				[utTypes addObject:type];
			}
		}
		if([utTypes count] > 0) {
			[panel setAllowedContentTypes:utTypes];
		}
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
	}
	if(self->params->relaxext) {
		[panel setAllowsOtherFileTypes:YES];
	}
	if(self->params->startDir) {
		[panel setDirectoryURL:[NSURL URLWithString:[[NSString alloc] initWithUTF8String:self->params->startDir]]];
	}
	if(self->params->filename != nil) {
		[panel setNameFieldStringValue:[[NSString alloc] initWithUTF8String:self->params->filename]];
	}

	checkActivationPolicy();

	return [panel runModal];
}

- (DlgResult)save {
	NSSavePanel* panel = [NSSavePanel savePanel];
	if(![self runPanel:panel]) {
		return DLG_CANCEL;
	} else if(![[panel URL] getFileSystemRepresentation:self->params->buf maxLength:self->params->nbuf]) {
		return DLG_URLFAIL;
	}
	return DLG_OK;
}

- (DlgResult)load {
	NSOpenPanel* panel = [NSOpenPanel openPanel];
	if(self->params->mode == DIRDLG) {
		[panel setCanChooseDirectories:YES];
		[panel setCanChooseFiles:NO];
	}
	
	if(self->params->allowMultiple) {
		[panel setAllowsMultipleSelection:YES];
	}
	
	if(![self runPanel:panel]) {
		return DLG_CANCEL;
	}
	
	NSArray* urls = [panel URLs];
172
173
174
175
176
	if([urls count] == 0) {
		return DLG_CANCEL;
	}
	
	if(self->params->allowMultiple) {
177
178
179
180
		// For multiple files, we need to return all paths separated by null bytes
		char* bufPtr = self->params->buf;
		int remainingBuf = self->params->nbuf;
		
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
		// Calculate total required buffer size first
		int totalSize = 0;
		for(NSURL* url in urls) {
			char tempBuf[PATH_MAX];
			if(![url getFileSystemRepresentation:tempBuf maxLength:PATH_MAX]) {
				return DLG_URLFAIL;
			}
			totalSize += strlen(tempBuf) + 1; // +1 for null terminator
		}
		totalSize += 1; // Final null terminator

		if(totalSize > self->params->nbuf) {
			// Not enough buffer space
			return DLG_URLFAIL;
		}

		// Now actually copy the paths (we know we have space)
		bufPtr = self->params->buf;
		for(NSURL* url in urls) {
			char tempBuf[PATH_MAX];
			[url getFileSystemRepresentation:tempBuf maxLength:PATH_MAX];
			int pathLen = strlen(tempBuf);
			strcpy(bufPtr, tempBuf);
			bufPtr += pathLen + 1;
		}
		*bufPtr = '\0'; // Final null terminator
	} else {
		// Single file/directory selection - write path to buffer
		NSURL* url = [urls firstObject];
		if(![url getFileSystemRepresentation:self->params->buf maxLength:self->params->nbuf]) {
			return DLG_URLFAIL;
		}
213
214
215
216
217
218
	}
	
	return DLG_OK;
}

@end