summarylogtreecommitdiffstats
path: root/sdl2-dialog.patch
blob: 3b4bf9a83510bcc42573a594338883eca95eedb3 (plain)
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
--- ClassiCube-1.3.7.orig/src/Window_SDL2.c	2024-09-21 02:44:10.000000000 +0300
+++ ClassiCube-1.3.7/src/Window_SDL2.c	2024-09-21 12:51:04.849585840 +0300
@@ -7,6 +7,7 @@
 #include "Funcs.h"
 #include "Bitmap.h"
 #include "Errors.h"
+#include "Utils.h"
 #include <SDL2/SDL.h>
 
 static SDL_Window* win_handle;
@@ -347,6 +348,36 @@
 	SDL_ShowSimpleMessageBox(0, title, msg, win_handle);
 }
 
+#ifdef CC_BUILD_LINUX
+static cc_result OpenSaveFileDialog(const char* args, FileDialogCallback callback, const char* defaultExt) {
+	cc_string path; char pathBuffer[1024];
+	char result[4096] = { 0 };
+	int len, i;
+	/* TODO this doesn't detect when Zenity doesn't exist */
+	FILE* fp = popen(args, "r");
+	if (!fp) return 0;
+
+	/* result from zenity is normally just one string */
+	while (fgets(result, sizeof(result), fp)) { }
+	pclose(fp);
+
+	len = String_Length(result);
+	if (!len) return 0;
+
+	String_InitArray(path, pathBuffer);
+	String_AppendUtf8(&path, result, len);
+
+	/* Add default file extension if necessary */
+	if (defaultExt) {
+		cc_string file = path;
+		Utils_UNSAFE_GetFilename(&file);
+		if (String_IndexOf(&file, '.') == -1) String_AppendConst(&path, defaultExt);
+	}
+	callback(&path);
+	return 0;
+}
+#endif
+
 cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) {
 #if defined CC_BUILD_OS2
 	FILEDLG fileDialog;
@@ -367,6 +398,29 @@
 	}
 	
 	return 0;
+#elif defined CC_BUILD_LINUX
+	const char* const* filters = args->filters;
+	cc_string path; char pathBuffer[1024];
+	int i;
+
+	String_InitArray_NT(path, pathBuffer);
+	String_Format1(&path, "zenity --file-selection --file-filter='%c (", args->description);
+
+	for (i = 0; filters[i]; i++)
+	{
+		if (i) String_Append(&path, ',');
+		String_Format1(&path, "*%c", filters[i]);
+	}
+	String_AppendConst(&path, ") |");
+
+	for (i = 0; filters[i]; i++)
+	{
+		String_Format1(&path, " *%c", filters[i]);
+	}
+	String_AppendConst(&path, "'");
+
+	path.buffer[path.length] = '\0';
+	return OpenSaveFileDialog(path.buffer, args->Callback, NULL);
 #else
 	return ERR_NOT_SUPPORTED;
 #endif
@@ -392,6 +446,27 @@
 	}
 	
 	return 0;
+#elif defined CC_BUILD_LINUX
+	const char* const* titles   = args->titles;
+	const char* const* fileExts = args->filters;
+	cc_string path; char pathBuffer[1024];
+	int i;
+
+	String_InitArray_NT(path, pathBuffer);
+	String_AppendConst(&path, "zenity --file-selection");
+	for (i = 0; fileExts[i]; i++)
+	{
+		String_Format3(&path, " --file-filter='%c (*%c) | *%c'", titles[i], fileExts[i], fileExts[i]);
+	}
+	String_AppendConst(&path, " --save --confirm-overwrite");
+
+	/* TODO: Utf8 encode filename */
+	if (args->defaultName.length) {
+		String_Format1(&path, " --filename='%s'", &args->defaultName);
+	}
+
+	path.buffer[path.length] = '\0';
+	return OpenSaveFileDialog(path.buffer, args->Callback, fileExts[0]);
 #else
 	return ERR_NOT_SUPPORTED;
 #endif