1 /*
2  * Copyright (c) Tony Bybell 2012.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  */
9 
10 #include "cocoa_misc.h"
11 
12 #ifdef WAVE_COCOA_GTK
13 
14 #include "alert_sheet.m"
15 #include <sys/stat.h>
16 
17 /*************************/
18 /* menu.c                */
19 /*************************/
20 
gtk_open_external_file(const char * fpath)21 void gtk_open_external_file(const char *fpath)
22 {
23 NSString *nspath = [NSString stringWithUTF8String:fpath];
24 [[NSWorkspace sharedWorkspace] openFile:nspath];
25 }
26 
27 
28 /*************************/
29 /* file.c                */
30 /*************************/
31 
gtk_open_file_req_bridge(const char * title,const char * fpath,const char * pattn)32 static char *gtk_open_file_req_bridge(const char *title, const char *fpath, const char *pattn)
33 {
34         NSOpenPanel * zOpenPanel = [NSOpenPanel openPanel];
35 	[zOpenPanel setCanChooseDirectories:NO];
36 	[zOpenPanel setCanChooseFiles:YES];
37 	[zOpenPanel setAllowsMultipleSelection:NO];
38 	[zOpenPanel setTreatsFilePackagesAsDirectories:YES];
39 
40 	NSString *nstitle = [NSString stringWithUTF8String:title];
41 	[zOpenPanel setTitle:nstitle];
42 
43         NSArray * zAryOfExtensions = nil;
44 
45 	if(pattn)
46 		{
47 		const char *d = strchr(pattn, '.');
48 		if(d)
49 			{
50 			const char *pattn2 = d+1;
51 			if(!strcasecmp(pattn2, "sav") || !strcasecmp(pattn2, "gtkw"))
52 				{
53 			        zAryOfExtensions = [NSArray arrayWithObjects:@"gtkw", @"sav", nil];
54 				}
55 				else
56 				{
57 				NSString *s = [NSString stringWithUTF8String:pattn2];
58 			        zAryOfExtensions = [NSArray arrayWithObjects:s,nil];
59 				}
60 			}
61 		}
62 
63 	NSString *p_path = nil;
64 	NSString *p_file = nil;
65 
66 	if(fpath)
67 		{
68 		char *s_temp = realpath(fpath,NULL);
69 
70 		if(s_temp)
71 			{
72 			struct stat sbuf;
73 			if(!stat(s_temp,&sbuf))
74 				{
75 				if(S_ISDIR(sbuf.st_mode))
76 					{
77 					p_path = [NSString stringWithUTF8String:s_temp];
78 					NSURL *URL = [NSURL URLWithString:p_path];
79 					[zOpenPanel setDirectoryURL:URL];
80 					}
81 					else
82 					{
83 					p_path = [[NSString stringWithUTF8String:s_temp] stringByDeletingLastPathComponent];
84 					p_file = [[NSString stringWithUTF8String:s_temp] lastPathComponent];
85 					NSURL *URL = [NSURL URLWithString:p_path];
86 					[zOpenPanel setDirectoryURL:URL];
87 					}
88 				}
89 
90 			free(s_temp);
91 			}
92 		}
93 
94         NSInteger zIntResult = [zOpenPanel runModalForDirectory:p_path
95 		file:p_file
96 		types:zAryOfExtensions];
97 
98         if (zIntResult == NSFileHandlingPanelCancelButton) {
99                 return(NULL);
100         }
101 
102         NSURL *zUrl = [zOpenPanel URL];
103 	NSString *us = [zUrl absoluteString];
104 	const char *cst = [us UTF8String];
105 	return(g_filename_from_uri(cst, NULL, NULL));
106 }
107 
gtk_save_file_req_bridge(const char * title,const char * fpath,const char * pattn)108 static char *gtk_save_file_req_bridge(const char *title, const char *fpath, const char *pattn)
109 {
110         NSSavePanel * zSavePanel = [NSSavePanel savePanel];
111 	[zSavePanel setAllowsOtherFileTypes:YES];
112 	[zSavePanel setTreatsFilePackagesAsDirectories:YES];
113 	[zSavePanel setExtensionHidden:NO];
114 
115 	NSString *nstitle = [NSString stringWithUTF8String:title];
116 	[zSavePanel setTitle:nstitle];
117 
118         NSArray * zAryOfExtensions = nil;
119 
120 	if(pattn)
121 		{
122 		const char *d = strchr(pattn, '.');
123 		if(d)
124 			{
125 			const char *pattn2 = d+1;
126 			if(!strcasecmp(pattn2, "sav") || !strcasecmp(pattn2, "gtkw"))
127 				{
128 			        zAryOfExtensions = [NSArray arrayWithObjects:@"gtkw", @"sav", nil];
129 				}
130 				else
131 				{
132 				NSString *s = [NSString stringWithUTF8String:pattn2];
133 			        zAryOfExtensions = [NSArray arrayWithObjects:s,nil];
134 				}
135 			}
136 		}
137 
138         [zSavePanel setAllowedFileTypes:zAryOfExtensions];
139 
140 	NSString *p_path = nil;
141 	NSString *p_file = nil;
142 
143 	if(fpath)
144 		{
145 		char *s_temp = realpath(fpath,NULL);
146 
147 		if(s_temp)
148 			{
149 			struct stat sbuf;
150 			if(!stat(s_temp,&sbuf))
151 				{
152 				if(S_ISDIR(sbuf.st_mode))
153 					{
154 					p_path = [NSString stringWithUTF8String:s_temp];
155 					NSURL *URL = [NSURL URLWithString:p_path];
156 					[zSavePanel setDirectoryURL:URL];
157 					}
158 					else
159 					{
160 					p_path = [[NSString stringWithUTF8String:s_temp] stringByDeletingLastPathComponent];
161 					p_file = [[NSString stringWithUTF8String:s_temp] lastPathComponent];
162 					NSURL *URL = [NSURL URLWithString:p_path];
163 					[zSavePanel setDirectoryURL:URL];
164 					[zSavePanel setNameFieldStringValue:p_file];
165 					}
166 				}
167 
168 			free(s_temp);
169 			}
170 		}
171 
172 
173         NSInteger zIntResult = [zSavePanel runModal];
174 
175         if (zIntResult == NSFileHandlingPanelCancelButton) {
176                 return(NULL);
177         }
178 
179         NSURL *zUrl = [zSavePanel URL];
180 	NSString *us = [zUrl absoluteString];
181 	const char *cst = [us UTF8String];
182 	return(g_filename_from_uri(cst, NULL, NULL));
183 }
184 
185 
gtk_file_req_bridge(const char * title,const char * fpath,const char * pattn,int is_writemode)186 char *gtk_file_req_bridge(const char *title, const char *fpath, const char *pattn, int is_writemode)
187 {
188 char *rc;
189 
190 if(is_writemode)
191 	{
192 	rc = gtk_save_file_req_bridge(title, fpath, pattn);
193 	}
194 	else
195 	{
196 	rc = gtk_open_file_req_bridge(title, fpath, pattn);
197 	}
198 
199 return(rc);
200 }
201 
202 
203 /*************************/
204 /* simplereq.c / entry.c */
205 /*************************/
206 
gtk_simplereqbox_req_bridge_2(char * title,char * default_text,char * oktext,char * canceltext,int is_alert,int is_entry,char * default_in_text_entry,char ** out_text_entry,int width)207 static int gtk_simplereqbox_req_bridge_2(char *title, char *default_text, char *oktext, char *canceltext,
208 	int is_alert, int is_entry, char *default_in_text_entry, char **out_text_entry, int width)
209 {
210 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
211 int rc = 0;
212 
213 if(oktext)
214 	{
215 	[alert addButtonWithTitle: [NSString stringWithUTF8String:oktext]];
216 
217 	if(canceltext)
218 		{
219 		[alert addButtonWithTitle: [NSString stringWithUTF8String:canceltext]];
220 		}
221 	}
222 
223 if(title)
224 	{
225 	[alert setMessageText: [NSString stringWithUTF8String:title]];
226 	}
227 
228 if(default_text)
229 	{
230 	[alert setInformativeText: [NSString stringWithUTF8String:default_text]];
231 	}
232 
233 if(is_alert)
234 	{
235 	[alert setAlertStyle:NSCriticalAlertStyle];
236 	}
237 	else
238 	{
239 	[alert setAlertStyle:NSInformationalAlertStyle];
240 	}
241 
242 NSTextField *input = nil;
243 if(is_entry && default_in_text_entry && out_text_entry)
244 	{
245 	input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, width, 24)];
246 	[input setSelectable:YES];
247 	[input setEditable:YES];
248 	[input setImportsGraphics:NO];
249 	[[alert window] makeFirstResponder:input];
250 	[input setStringValue:[NSString stringWithUTF8String:default_in_text_entry]];
251 	[input selectText:input];
252 	[alert setAccessoryView:input];
253 	}
254 
255 NSInteger zIntResult = [alert runModalSheet];
256 if(zIntResult == NSAlertFirstButtonReturn)
257 	{
258 	rc = 1;
259 	if(is_entry && default_in_text_entry && out_text_entry)
260 		{
261 		[input validateEditing];
262 		*out_text_entry = strdup([[input stringValue] UTF8String]);
263 		}
264 	}
265 else
266 if(zIntResult == NSAlertSecondButtonReturn)
267 	{
268 	rc = 2;
269 	}
270 
271 return(rc);
272 }
273 
274 
gtk_simplereqbox_req_bridge(char * title,char * default_text,char * oktext,char * canceltext,int is_alert)275 int gtk_simplereqbox_req_bridge(char *title, char *default_text, char *oktext, char *canceltext, int is_alert)
276 {
277 return(gtk_simplereqbox_req_bridge_2(title, default_text, oktext, canceltext, is_alert, 0, NULL, 0, 0));
278 }
279 
280 
entrybox_req_bridge(char * title,int width,char * dflt_text,char * comment,int maxch,char ** out_text_entry)281 int entrybox_req_bridge(char *title, int width, char *dflt_text, char *comment, int maxch, char **out_text_entry)
282 {
283 int rc = gtk_simplereqbox_req_bridge_2(title, comment, "OK", "Cancel",
284 	0, 1, dflt_text, out_text_entry, width);
285 
286 if((rc == 1)&&(*out_text_entry))
287 	{
288 	int len = strlen(*out_text_entry);
289 	if(len > maxch)
290 		{
291 		char *s2 = calloc(1, maxch+1);
292 		memcpy(s2, *out_text_entry, maxch);
293 		free(*out_text_entry);
294 		*out_text_entry = s2;
295 		}
296 	}
297 
298 return(rc);
299 }
300 
301 
302 #else
303 
cocoa_misc_dummy_compilation_unit(void)304 char *cocoa_misc_dummy_compilation_unit(void)
305 {
306 return(NULL); /* dummy compilation unit */
307 }
308 
309 #endif
310