Debian Codegraph @

src/cdogsed/file_dialog.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2022 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "file_dialog.h"
29
30 #include <tinydir/tinydir.h>
31
32 #include <cdogs/log.h>
33
34 #include "nk_window.h"
35
36 #define WIDTH 400
37 #define HEIGHT 400
38 #define ROW_HEIGHT_SMALL 18
39 #define ROW_HEIGHT 35
40 #define FILE_LIST_HEIGHT (HEIGHT - ROW_HEIGHT_SMALL - 2 * ROW_HEIGHT)
41
42 typedef struct
43 {
44 struct nk_context *ctx;
45 CArray files; // of char[CDOGS_FILENAME_MAX]
46 int selected;
47 char pathOrig[CDOGS_PATH_MAX];
48 char filename[CDOGS_FILENAME_MAX];
49 char dir[CDOGS_PATH_MAX];
50 bool mustExist;
51 bool result;
52 } FileData;
53
54 static void OpenDir(SDL_Window *win, FileData *data, const char *path);
55 static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data);
56
57 bool TryFileDialog(
58 char *out, EventHandlers *handlers, const char *dir, const char *filename,
59 const char *title, const bool mustExist)
60 {
61 NKWindowConfig cfg;
62 memset(&cfg, 0, sizeof cfg);
63 cfg.Title = title;
64 cfg.Size = svec2i(WIDTH, HEIGHT);
65 color_t bg = {41, 26, 26, 255};
66 cfg.BG = bg;
67 cfg.Handlers = handlers;
68 cfg.Draw = DrawDialog;
69
70 NKWindowInit(&cfg);
71
72 FileData data;
73 memset(&data, 0, sizeof data);
74 data.ctx = cfg.ctx;
75 strcpy(data.filename, filename);
76 data.mustExist = mustExist;
77 OpenDir(NULL, &data, dir);
78 cfg.DrawData = &data;
79 char buf[CDOGS_PATH_MAX];
80 sprintf(buf, "%s/%s", dir, filename);
81 RealPath(buf, data.pathOrig);
82
83 NKWindow(cfg);
84
85 if (data.result)
86 {
87 sprintf(out, "%s/%s", data.dir, data.filename);
88 }
89 return data.result;
90 }
91
92 static void OpenDir(SDL_Window *win, FileData *data, const char *path)
93 {
94 // Fill FileData with the file listings of a dir
95 tinydir_dir dir;
96
97 if (tinydir_open_sorted(&dir, path) == -1)
98 {
99 LOG(LM_EDIT, LL_ERROR, "Cannot load dir %s", path);
100 if (win)
101 {
102 char msgbuf[CDOGS_PATH_MAX];
103 sprintf(msgbuf, "Cannot open %s", path);
104 SDL_ShowSimpleMessageBox(
105 SDL_MESSAGEBOX_ERROR, "Open Dir Error", msgbuf, win);
106 }
107 return;
108 }
109
110 RealPath(path, data->dir);
111
112 CArrayTerminate(&data->files);
113 CArrayInit(&data->files, CDOGS_FILENAME_MAX);
114 char buf[CDOGS_FILENAME_MAX];
115 if (strcmp(path, "/") != 0)
116 {
117 CArrayPushBack(&data->files, "../");
118 }
119 for (int i = 0; i < (int)dir.n_files; i++)
120 {
121 tinydir_file file;
122 tinydir_readfile_n(&dir, &file, i);
123 // Ignore campaigns that start with a ~; these are autosaved
124 if (file.name[0] == '~' || file.name[0] == '.')
125 {
126 continue;
127 }
128 if ((file.is_dir && Stricmp(file.extension, "cdogscpn") == 0) ||
129 (file.is_reg && Stricmp(file.extension, "cpn") == 0))
130 {
131 strcpy(buf, file.name);
132 CArrayPushBack(&data->files, buf);
133 }
134 else if (file.is_dir)
135 {
136 sprintf(buf, "%s/", file.name);
137 CArrayPushBack(&data->files, buf);
138 }
139 }
140
141 tinydir_close(&dir);
142 }
143
144 bool TryOpenDir(
145 char *out, EventHandlers *handlers, const char *dir, const char *filename)
146 {
147 return TryFileDialog(out, handlers, dir, filename, "Open File", true);
148 }
149 bool TrySaveFile(
150 char *out, EventHandlers *handlers, const char *dir, const char *filename)
151 {
152 return TryFileDialog(out, handlers, dir, filename, "Save File", false);
153 }
154
155 static void OnSelectChange(FileData *fData, const int selected);
156 static bool OnSelect(SDL_Window *win, FileData *fData);
157 static bool DrawDialog(SDL_Window *win, struct nk_context *ctx, void *data)
158 {
159 FileData *fData = data;
160 char buf[CDOGS_PATH_MAX];
161
162 bool done = false;
163 float y = 0;
164 if (nk_begin(
165 ctx, "Dir", nk_rect(0, y, WIDTH, ROW_HEIGHT_SMALL),
166 NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
167 {
168 const float colRatios[] = {0.1f, 0.9f};
169 nk_layout_row(ctx, NK_DYNAMIC, ROW_HEIGHT, 2, colRatios);
170 nk_label(ctx, "Dir:", NK_TEXT_ALIGN_LEFT);
171 if (strlen(fData->dir) > 44)
172 {
173 sprintf(buf, "... %s", strrchr(fData->dir, '/'));
174 nk_label(ctx, buf, NK_TEXT_ALIGN_LEFT);
175 }
176 else
177 {
178 nk_label(ctx, fData->dir, NK_TEXT_ALIGN_LEFT);
179 }
180 nk_end(ctx);
181 }
182 y += ROW_HEIGHT_SMALL;
183
184 if (nk_begin(
185 ctx, "Picker", nk_rect(0, y, WIDTH, FILE_LIST_HEIGHT),
186 NK_WINDOW_BORDER))
187 {
188 nk_layout_row_dynamic(ctx, ROW_HEIGHT_SMALL, 1);
189
190 CA_FOREACH(const char, filename, fData->files)
191 const bool selected = fData->selected == _ca_index;
192 nk_select_label(
193 fData->ctx, filename, NK_TEXT_ALIGN_CENTERED | NK_TEXT_ALIGN_LEFT,
194 selected);
195 if (nk_widget_is_mouse_clicked(ctx, NK_BUTTON_LEFT))
196 {
197 OnSelectChange(fData, _ca_index);
198 done = OnSelect(win, fData);
199 }
200 else if (nk_widget_is_hovered(ctx))
201 {
202 OnSelectChange(fData, _ca_index);
203 }
204 CA_FOREACH_END()
205
206 if (nk_input_is_key_pressed(&ctx->input, NK_KEY_DOWN))
207 {
208 OnSelectChange(
209 fData,
210 CLAMP_OPPOSITE(
211 fData->selected + 1, 0, (int)fData->files.size - 1));
212 }
213 else if (nk_input_is_key_pressed(&ctx->input, NK_KEY_UP))
214 {
215 OnSelectChange(
216 fData,
217 CLAMP_OPPOSITE(
218 fData->selected - 1, 0, (int)fData->files.size - 1));
219 }
220 else if (
221 nk_input_is_key_pressed(&ctx->input, NK_KEY_ENTER) &&
222 fData->selected >= 0)
223 {
224 done = OnSelect(win, fData);
225 }
226
227 nk_end(ctx);
228 }
229 y += FILE_LIST_HEIGHT;
230
231 if (nk_begin(
232 ctx, "Filename", nk_rect(0, y, WIDTH, ROW_HEIGHT),
233 NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
234 {
235 nk_layout_row_dynamic(ctx, ROW_HEIGHT - 10, 1);
236 DrawTextbox(
237 ctx, fData->filename, CDOGS_FILENAME_MAX, NULL, NK_EDIT_FIELD);
238 nk_end(ctx);
239 }
240 y += ROW_HEIGHT;
241
242 if (nk_begin(
243 ctx, "Controls", nk_rect(0, y, WIDTH, ROW_HEIGHT),
244 NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
245 {
246 nk_layout_row_dynamic(ctx, ROW_HEIGHT - 10, 2);
247 if (nk_button_label(ctx, "OK") && fData->selected >= 0)
248 {
249 done = OnSelect(win, fData);
250 }
251 if (nk_button_label(ctx, "Cancel"))
252 {
253 fData->result = false;
254 done = true;
255 }
256 nk_end(ctx);
257 }
258
259 return !done;
260 }
261 static void OnSelectChange(FileData *fData, const int selected)
262 {
263 if (fData->selected == selected || selected < 0 ||
264 selected >= (int)fData->files.size)
265 return;
266
267 const char *filename = CArrayGet(&fData->files, selected);
268 strcpy(fData->filename, filename);
269 fData->selected = selected;
270 }
271 static bool OnSelect(SDL_Window *win, FileData *fData)
272 {
273 char buf[CDOGS_PATH_MAX];
274 sprintf(buf, "%s/%s", fData->dir, fData->filename);
275 char buf2[CDOGS_PATH_MAX];
276 RealPath(buf, buf2);
277 if (fData->filename[strlen(fData->filename) - 1] == '/')
278 {
279 fData->selected = 0;
280 OpenDir(win, fData, buf2);
281 fData->filename[0] = '\0';
282 return false;
283 }
284 else
285 {
286 if (strcmp(buf2, fData->pathOrig) != 0 && !fData->mustExist)
287 {
288 // Overwriting file; confirm whether to overwrite
289 const SDL_MessageBoxButtonData buttons[] = {
290 {SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 1, "Cancel"},
291 {SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "OK"},
292 };
293 const SDL_MessageBoxData messageboxdata = {
294 SDL_MESSAGEBOX_INFORMATION,
295 win,
296 "Overwrite File", // title
297 "File already exists. Do you want to overwrite?",
298 SDL_arraysize(buttons),
299 buttons,
300 NULL,
301 };
302 int buttonid;
303 if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0)
304 {
305 LOG(LM_EDIT, LL_ERROR, "error displaying message box");
306 }
307 if (buttonid != 0)
308 {
309 return false;
310 }
311 }
312 fData->result = true;
313 return true;
314 }
315 }