summaryrefslogtreecommitdiff
path: root/platform/gl/gl-file.c
blob: 6fc34f5de5050ae61e41498e8ea43a1a275148db (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include "gl-app.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

#define ICON_PC 0x1f4bb
#define ICON_HOME 0x1f3e0
#define ICON_FOLDER 0x1f4c1
#define ICON_DOCUMENT 0x1f4c4
#define ICON_DISK 0x1f4be
#define ICON_PIN 0x1f4cc

#ifndef PATH_MAX
#define PATH_MAX 2048
#endif

struct entry
{
	int is_dir;
	char name[FILENAME_MAX];
};

static struct
{
	int (*filter)(const char *fn);
	struct input input_dir;
	struct input input_file;
	struct list list_dir;
	char curdir[PATH_MAX];
	int count;
	struct entry files[512];
	int selected;
} fc;

static int cmp_entry(const void *av, const void *bv)
{
	const struct entry *a = av;
	const struct entry *b = bv;
	/* "." first */
	if (a->name[0] == '.' && a->name[1] == 0) return -1;
	if (b->name[0] == '.' && b->name[1] == 0) return 1;
	/* ".." second */
	if (a->name[0] == '.' && a->name[1] == '.' && a->name[2] == 0) return -1;
	if (b->name[0] == '.' && b->name[1] == '.' && b->name[2] == 0) return 1;
	/* directories before files */
	if (a->is_dir && !b->is_dir) return -1;
	if (b->is_dir && !a->is_dir) return 1;
	/* then alphabetically */
	return strcmp(a->name, b->name);
}

#ifdef _WIN32

#include <strsafe.h>
#include <shlobj.h>

const char *realpath(const char *path, char buf[PATH_MAX])
{
	wchar_t wpath[PATH_MAX];
	wchar_t wbuf[PATH_MAX];
	int i;
	MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, PATH_MAX);
	GetFullPathNameW(wpath, PATH_MAX, wbuf, NULL);
	WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, PATH_MAX, NULL, NULL);
	for (i=0; buf[i]; ++i)
		if (buf[i] == '\\')
			buf[i] = '/';
	return buf;
}

static void load_dir(const char *path)
{
	WIN32_FIND_DATA ffd;
	HANDLE dir;
	wchar_t wpath[PATH_MAX];
	char buf[PATH_MAX];
	int i;

	realpath(path, fc.curdir);
	if (!fz_is_directory(ctx, path))
		return;

	ui_input_init(&fc.input_dir, fc.curdir);

	fc.selected = -1;
	fc.count = 0;

	MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, PATH_MAX);
	for (i=0; wpath[i]; ++i)
		if (wpath[i] == '/')
			wpath[i] = '\\';
	StringCchCat(wpath, PATH_MAX, TEXT("/*"));
	dir = FindFirstFileW(wpath, &ffd);
	if (dir)
	{
		do
		{
			WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, -1, buf, PATH_MAX, NULL, NULL);
			if (ffd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
				continue;
			fc.files[fc.count].is_dir = ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
			if (fc.files[fc.count].is_dir || !fc.filter || fc.filter(buf))
			{
				fz_strlcpy(fc.files[fc.count].name, buf, FILENAME_MAX);
				++fc.count;
			}
		}
		while (FindNextFile(dir, &ffd));
		FindClose(dir);
	}

	qsort(fc.files, fc.count, sizeof fc.files[0], cmp_entry);
}

static void list_drives(void)
{
	static struct list drive_list;
	DWORD drives;
	char dir[PATH_MAX], vis[PATH_MAX], buf[100];
	const char *user, *home;
	char personal[MAX_PATH], desktop[MAX_PATH];
	int i, n;

	drives = GetLogicalDrives();
	n = 5; /* curdir + home + desktop + documents + downloads */
	for (i=0; i < 26; ++i)
		if (drives & (1<<i))
			++n;

	ui_list_begin(&drive_list, n, 0, 10 * ui.lineheight + 4);

	user = getenv("USERNAME");
	home = getenv("USERPROFILE");
	if (user && home)
	{
		fz_snprintf(vis, sizeof vis, "%C %s", ICON_HOME, user);
		if (ui_list_item(&drive_list, "~", vis, 0))
			load_dir(home);
	}

	if (SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, desktop) == S_OK)
	{
		fz_snprintf(vis, sizeof vis, "%C Desktop", ICON_PC);
		if (ui_list_item(&drive_list, "~/Desktop", vis, 0))
			load_dir(desktop);
	}

	if (SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, 0, personal) == S_OK)
	{
		fz_snprintf(vis, sizeof vis, "%C Documents", ICON_FOLDER);
		if (ui_list_item(&drive_list, "~/Documents", vis, 0))
			load_dir(personal);
	}

	if (home)
	{
		fz_snprintf(vis, sizeof vis, "%C Downloads", ICON_FOLDER);
		fz_snprintf(dir, sizeof dir, "%s/Downloads", home);
		if (ui_list_item(&drive_list, "~/Downloads", vis, 0))
			load_dir(dir);
	}

	for (i = 0; i < 26; ++i)
	{
		if (drives & (1<<i))
		{
			fz_snprintf(dir, sizeof dir, "%c:\\", i+'A');
			if (!GetVolumeInformationA(dir, buf, sizeof buf, NULL, NULL, NULL, NULL, 0))
				buf[0] = 0;
			fz_snprintf(vis, sizeof vis, "%C %c: %s", ICON_DISK, i+'A', buf);
			if (ui_list_item(&drive_list, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+i, vis, 0))
			{
				load_dir(dir);
			}
		}
	}

	fz_snprintf(vis, sizeof vis, "%C .", ICON_PIN);
	if (ui_list_item(&drive_list, ".", vis, 0))
		load_dir(".");

	ui_list_end(&drive_list);
}

#else

#include <dirent.h>

static void load_dir(const char *path)
{
	char buf[PATH_MAX];
	DIR *dir;
	struct dirent *dp;

	realpath(path, fc.curdir);
	if (!fz_is_directory(ctx, fc.curdir))
		return;

	ui_input_init(&fc.input_dir, fc.curdir);

	fc.selected = -1;
	fc.count = 0;
	dir = opendir(fc.curdir);
	if (!dir)
	{
		fc.files[fc.count].is_dir = 1;
		fz_strlcpy(fc.files[fc.count].name, "..", FILENAME_MAX);
		++fc.count;
	}
	else
	{
		while ((dp = readdir(dir)) && fc.count < nelem(fc.files))
		{
			/* skip hidden files */
			if (dp->d_name[0] == '.' && strcmp(dp->d_name, ".") && strcmp(dp->d_name, ".."))
				continue;
			fz_snprintf(buf, sizeof buf, "%s/%s", fc.curdir, dp->d_name);
			fc.files[fc.count].is_dir = fz_is_directory(ctx, buf);
			if (fc.files[fc.count].is_dir || !fc.filter || fc.filter(buf))
			{
				fz_strlcpy(fc.files[fc.count].name, dp->d_name, FILENAME_MAX);
				++fc.count;
			}
		}
		closedir(dir);
	}

	qsort(fc.files, fc.count, sizeof fc.files[0], cmp_entry);
}

static const struct {
	int icon;
	const char *name;
} common_dirs[] = {
	{ ICON_HOME, "~" },
	{ ICON_PC, "~/Desktop" },
	{ ICON_FOLDER, "~/Documents" },
	{ ICON_FOLDER, "~/Downloads" },
	{ ICON_FOLDER, "/" },
	{ ICON_DISK, "/Volumes" },
	{ ICON_DISK, "/media" },
	{ ICON_DISK, "/mnt" },
	{ ICON_PIN, "." },
};

static int has_dir(const char *home, const char *user, int i, char dir[PATH_MAX], char vis[PATH_MAX])
{
	const char *subdir = common_dirs[i].name;
	int icon = common_dirs[i].icon;
	if (subdir[0] == '~')
	{
		if (!home)
			return 0;
		if (subdir[1] == '/')
		{
			fz_snprintf(dir, PATH_MAX, "%s/%s", home, subdir+2);
			fz_snprintf(vis, PATH_MAX, "%C %s", icon, subdir+2);
		}
		else
		{
			fz_snprintf(dir, PATH_MAX, "%s", home);
			fz_snprintf(vis, PATH_MAX, "%C %s", icon, user ? user : "~");
		}
	}
	else
	{
		fz_strlcpy(dir, subdir, PATH_MAX);
		fz_snprintf(vis, PATH_MAX, "%C %s", icon, subdir);
	}
	return fz_is_directory(ctx, dir);
}

static void list_drives(void)
{
	static struct list drive_list;
	char dir[PATH_MAX], vis[PATH_MAX];
	const char *home = getenv("HOME");
	const char *user = getenv("USER");
	int i;

	ui_list_begin(&drive_list, nelem(common_dirs), 0, nelem(common_dirs) * ui.lineheight + 4);

	for (i = 0; i < nelem(common_dirs); ++i)
		if (has_dir(home, user, i, dir, vis))
			if (ui_list_item(&drive_list, common_dirs[i].name, vis, 0))
				load_dir(dir);

	ui_list_end(&drive_list);
}

#endif

void ui_init_open_file(const char *dir, int (*filter)(const char *fn))
{
	fc.filter = filter;
	load_dir(".");
}

int ui_open_file(char filename[PATH_MAX])
{
	static int last_click_time = 0;
	static int last_click_sel = -1;
	int i, rv = 0;

	ui_panel_begin(0, 0, 4, 4, 1);
	{
		ui_layout(L, Y, NW, 0, 0);
		ui_panel_begin(150, 0, 0, 0, 0);
		{
			ui_layout(T, X, NW, 2, 2);
			list_drives();
			ui_layout(B, X, NW, 2, 2);
			if (ui_button("Cancel") || (!ui.focus && ui.key == KEY_ESCAPE))
			{
				filename[0] = 0;
				rv = 1;
			}
		}
		ui_panel_end();

		ui_layout(T, X, NW, 2, 2);
		ui_panel_begin(0, ui.gridsize, 0, 0, 0);
		{
			if (fc.selected >= 0)
			{
				ui_layout(R, NONE, CENTER, 0, 0);
				if (ui_button("Open") || (!ui.focus && ui.key == KEY_ENTER))
				{
					fz_snprintf(filename, PATH_MAX, "%s/%s", fc.curdir, fc.files[fc.selected].name);
					rv = 1;
				}
				ui_spacer();
			}
			ui_layout(ALL, X, CENTER, 0, 0);
			if (ui_input(&fc.input_dir, 0, 1) == UI_INPUT_ACCEPT)
				load_dir(fc.input_dir.text);
		}
		ui_panel_end();

		ui_layout(ALL, BOTH, NW, 2, 2);
		ui_list_begin(&fc.list_dir, fc.count, 0, 0);
		for (i = 0; i < fc.count; ++i)
		{
			const char *name = fc.files[i].name;
			char buf[PATH_MAX];
			if (fc.files[i].is_dir)
				fz_snprintf(buf, sizeof buf, "%C %s", ICON_FOLDER, name);
			else
				fz_snprintf(buf, sizeof buf, "%C %s", ICON_DOCUMENT, name);
			if (ui_list_item(&fc.list_dir, &fc.files[i], buf, i==fc.selected))
			{
				fc.selected = i;
				if (fc.files[i].is_dir)
				{
					fz_snprintf(buf, sizeof buf, "%s/%s", fc.curdir, name);
					load_dir(buf);
					ui.active = NULL;
					last_click_sel = -1;
				}
				else
				{
					int click_time = glutGet(GLUT_ELAPSED_TIME);
					if (i == last_click_sel && click_time < last_click_time + 250)
					{
						fz_snprintf(filename, PATH_MAX, "%s/%s", fc.curdir, name);
						rv = 1;
					}
					last_click_time = click_time;
					last_click_sel = i;
				}
			}
		}
		ui_list_end(&fc.list_dir);
	}
	ui_panel_end();

	return rv;
}

void ui_init_save_file(const char *path, int (*filter)(const char *fn))
{
	char dir[PATH_MAX], *p;
	fc.filter = filter;
	fz_strlcpy(dir, path, sizeof dir);
	for (p=dir; *p; ++p)
		if (*p == '\\') *p = '/';
	fz_cleanname(dir);
	p = strrchr(dir, '/');
	if (p)
	{
		*p = 0;
		load_dir(dir);
		ui_input_init(&fc.input_file, p+1);
	}
	else
	{
		load_dir(".");
		ui_input_init(&fc.input_file, dir);
	}
}

static void bump_file_version(int dir)
{
	char buf[PATH_MAX], *p, *n;
	char base[PATH_MAX], out[PATH_MAX];
	int x;
	fz_strlcpy(buf, fc.input_file.text, sizeof buf);
	p = strrchr(buf, '.');
	if (p)
	{
		n = p;
		while (n > buf && n[-1] >= '0' && n[-1] <= '9')
			--n;
		if (n != p)
			x = atoi(n) + dir;
		else
			x = dir;
		memcpy(base, buf, n-buf);
		base[n-buf] = 0;
		fz_snprintf(out, sizeof out, "%s%d%s", base, x, p);
		ui_input_init(&fc.input_file, out);
	}
}

int ui_save_file(char filename[PATH_MAX], void (*extra_panel)(void))
{
	int i, rv = 0;

	ui_panel_begin(0, 0, 4, 4, 1);
	{
		ui_layout(L, Y, NW, 0, 0);
		ui_panel_begin(150, 0, 0, 0, 0);
		{
			ui_layout(T, X, NW, 2, 2);
			list_drives();
			if (extra_panel)
			{
				ui_spacer();
				extra_panel();
			}
			ui_layout(B, X, NW, 2, 2);
			if (ui_button("Cancel") || (!ui.focus && ui.key == KEY_ESCAPE))
			{
				filename[0] = 0;
				rv = 1;
			}
		}
		ui_panel_end();

		ui_layout(T, X, NW, 2, 2);
		if (ui_input(&fc.input_dir, 0, 1) == UI_INPUT_ACCEPT)
			load_dir(fc.input_dir.text);

		ui_layout(T, X, NW, 2, 2);
		ui_panel_begin(0, ui.gridsize, 0, 0, 0);
		{
			ui_layout(R, NONE, CENTER, 0, 0);
			if (ui_button("Save"))
			{
				fz_snprintf(filename, PATH_MAX, "%s/%s", fc.curdir, fc.input_file.text);
				rv = 1;
			}
			ui_spacer();
			if (ui_button("\xe2\x9e\x95")) /* U+2795 HEAVY PLUS */
				bump_file_version(1);
			if (ui_button("\xe2\x9e\x96")) /* U+2796 HEAVY MINUS */
				bump_file_version(-1);
			ui_spacer();
			ui_layout(ALL, X, CENTER, 0, 0);
			ui_input(&fc.input_file, 0, 1);
		}
		ui_panel_end();

		ui_layout(ALL, BOTH, NW, 2, 2);
		ui_list_begin(&fc.list_dir, fc.count, 0, 0);
		for (i = 0; i < fc.count; ++i)
		{
			const char *name = fc.files[i].name;
			char buf[PATH_MAX];
			if (fc.files[i].is_dir)
				fz_snprintf(buf, sizeof buf, "\xf0\x9f\x93\x81 %s", name);
			else
				fz_snprintf(buf, sizeof buf, "\xf0\x9f\x93\x84 %s", name);
			if (ui_list_item(&fc.list_dir, &fc.files[i], buf, i==fc.selected))
			{
				fc.selected = i;
				if (fc.files[i].is_dir)
				{
					fz_snprintf(buf, sizeof buf, "%s/%s", fc.curdir, name);
					load_dir(buf);
					ui.active = NULL;
				}
				else
				{
					ui_input_init(&fc.input_file, name);
				}
			}
		}
		ui_list_end(&fc.list_dir);
	}
	ui_panel_end();

	return rv;
}