dlg.m 5.18 KB
Newer Older
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
#import <Cocoa/Cocoa.h>
#include "dlg.h"
#include <string.h>
#include <sys/syslimits.h>

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]];
	}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
	if(self->params->numext > 0) {
		[panel setAllowedFileTypes:[NSArray arrayWithObjects:(NSString**)self->params->exts count:self->params->numext]];
	}
#pragma clang diagnostic pop
	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];
	if(self->params->allowMultiple && [urls count] >= 1) {
		// For multiple files, we need to return all paths separated by null bytes
		char* bufPtr = self->params->buf;
		int remainingBuf = self->params->nbuf;
		
  // 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
	}
	
	return DLG_OK;
}

@end