-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnuklear_console_file.h
More file actions
785 lines (690 loc) · 25.9 KB
/
nuklear_console_file.h
File metadata and controls
785 lines (690 loc) · 25.9 KB
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
#ifndef NK_CONSOLE_FILE_H__
#define NK_CONSOLE_FILE_H__
#ifndef NK_CONSOLE_FILE_PATH_MAX
#ifdef PATH_MAX
#define NK_CONSOLE_FILE_PATH_MAX PATH_MAX
#else
#define NK_CONSOLE_FILE_PATH_MAX 4096
#endif
#endif // NK_CONSOLE_FILE_PATH_MAX
/**
* A single file or directory entry stored for the file widget's list view.
*/
typedef struct nk_console_file_entry {
char* label; /** The basename of the file or directory. */
nk_bool is_directory; /** True if this entry is a directory. */
} nk_console_file_entry;
/**
* Custom data for the file widget.
*/
typedef struct nk_console_file_data {
nk_console_button_data button; /** Inherited from Button */
char* file_path_buffer; /** The string buffer for the chosen file path. */
int file_path_buffer_size; /** The size of the buffer. */
char directory[NK_CONSOLE_FILE_PATH_MAX]; /** When selecting a file, this is the current directory. */
void* file_user_data; /** Custom user data for the file system. */
nk_bool select_directory; /** Flag indicating if we are selecting a directory. */
nk_bool use_list_view; /** When true, uses a list view for file selection. Defaults to buttons. */
char dir_label_buf[NK_CONSOLE_FILE_PATH_MAX + 2]; /** Scratch buffer for appending "/" to directory labels in the list view. */
nk_console_file_entry* entries; /** cvector of file/directory entries for the list view. */
} nk_console_file_data;
#if defined(__cplusplus)
extern "C" {
#endif
/**
* Creates a file widget that allows the user to select a file.
*
* @param parent The parent widget.
* @param label The label for the file widget. For example: "Select a file".
* @param file_path_buffer The buffer to store the file path.
* @param file_path_buffer_size The size of the buffer.
*
* @return The new file widget.
*/
NK_API nk_console* nk_console_file(nk_console* parent, const char* label, char* file_path_buffer, int file_path_buffer_size);
/**
* Creates a directory widget that allows the user to select a directory.
*
* @param parent The parent widget.
* @param label The label for the file widget. For example: "Select a directory".
* @param dir_buffer The buffer to store the directory path.
* @param dir_buffer_size The size of the buffer.
*
* @return The new file widget.
*/
NK_API nk_console* nk_console_dir(nk_console* parent, const char* label, char* dir_buffer, int dir_buffer_size);
/**
* Render callback to display the file widget.
*/
NK_API struct nk_rect nk_console_file_render(nk_console* widget);
/**
* Sets any custom user data specifically for the file widget.
*
* This can be helpful for storing additional file system data for the file widget.
*
* @param file The file widget.
* @param user_data The custom user data.
*/
NK_API void nk_console_file_set_file_user_data(nk_console* file, void* user_data);
/**
* Gets the custom user data specifically for the file widget.
*
* @param file The file widget.
*
* @return The custom user data.
*/
NK_API void* nk_console_file_get_file_user_data(nk_console* file);
/**
* Add an individual file or directory entry to the given file widget's list view.
*
* This should be called from the file system callbacks. See `nuklear_console_file_system.h` for examples.
*
* @param parent The file widget.
* @param path The path to the file or directory.
* @param is_directory True if the path is a directory. False otherwise.
*
* @return Non-NULL if the entry was successfully added, NULL otherwise.
*
* @see nk_console_file_add_files_tinydir()
* @see nk_console_file_add_files_raylib()
*/
NK_API nk_console* nk_console_file_add_entry(nk_console* parent, const char* path, nk_bool is_directory);
/**
* Sets whether the file/directory widget uses a list view or buttons for file selection.
*
* @param file The file widget.
* @param use_list_view True to use a list view, false to use buttons (the default).
*/
NK_API void nk_console_file_set_list_view(nk_console* file, nk_bool use_list_view);
/**
* Gets whether the file/directory widget is using a list view for file selection.
*
* @param file The file widget.
*
* @return True if using a list view, false if using buttons (the default).
*/
NK_API nk_bool nk_console_file_get_list_view(nk_console* file);
/**
* Refreshes the file widget with the contents with its given directory.
*
* @internal
*/
NK_API void nk_console_file_refresh(nk_console* widget, void* user_data);
#if defined(__cplusplus)
}
#endif
#endif // NK_CONSOLE_FILE_H__
#if defined(NK_CONSOLE_IMPLEMENTATION) && !defined(NK_CONSOLE_HEADER_ONLY)
#ifndef NK_CONSOLE_FILE_IMPLEMENTATION_ONCE
#define NK_CONSOLE_FILE_IMPLEMENTATION_ONCE
#include "nuklear_console_file_system.h"
#if defined(__cplusplus)
extern "C" {
#endif
/**
* Gets the base name of a file path.
*
* @internal
*/
static const char* nk_console_file_basename(const char* path) {
if (path == NULL) {
return NULL;
}
int len = nk_strlen(path);
for (int i = len - 1; i > 0; i--) {
if (path[i] == '\\' || path[i] == '/') {
path = path + i + 1;
break;
}
}
return path;
}
NK_API struct nk_rect nk_console_file_render(nk_console* console) {
if (console == NULL || console->data == NULL) {
return nk_rect(0, 0, 0, 0);
}
nk_console_file_data* data = (nk_console_file_data*)console->data;
nk_console_layout_widget(console);
// Display the label
if (console->label != NULL && console->label[0] != '\0') {
if (!nk_console_is_active_widget(console)) {
nk_widget_disable_begin(console->ctx);
}
if (console->label_length > 0) {
nk_text(console->ctx, console->label, console->label_length, NK_TEXT_LEFT);
}
else {
nk_label(console->ctx, console->label, NK_TEXT_LEFT);
}
if (!nk_console_is_active_widget(console)) {
nk_widget_disable_end(console->ctx);
}
}
// Display the mocked button
int swap_columns = console->columns;
const char* swap_label = console->label;
console->columns = 0;
if (data->file_path_buffer != NULL && data->file_path_buffer[0] != '\0') {
console->label = nk_console_file_basename(data->file_path_buffer);
}
else {
console->label = data->select_directory ? "[Select Directory]" : "[Select a File]";
}
struct nk_rect widget_bounds = nk_console_button_render(console);
console->columns = swap_columns;
console->label = swap_label;
return widget_bounds;
}
/**
* Gets the file widget from a child button.
*
* @internal
*/
static nk_console* nk_console_file_button_get_file_widget(nk_console* button) {
if (button == NULL) {
return NULL;
}
if (button->type == NK_CONSOLE_FILE) {
return button;
}
while (button->parent != NULL) {
button = button->parent;
if (button->type == NK_CONSOLE_FILE) {
return button;
}
}
return NULL;
}
/**
* Free all file entries stored in the file data.
*
* @internal
*/
static void nk_console_file_entries_clear(nk_console_file_data* data) {
if (data == NULL) {
return;
}
for (size_t i = 0; i < cvector_size(data->entries); i++) {
if (data->entries[i].label != NULL) {
nk_console_mfree(nk_handle_id(0), data->entries[i].label);
data->entries[i].label = NULL;
}
}
cvector_clear(data->entries);
}
/**
* Event handler: Destroy the file widget.
*
* @internal
*/
static void nk_console_file_event_destroy(nk_console* file, void* user_data) {
NK_UNUSED(user_data);
if (file == NULL || file->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
// Clear all the file entries.
nk_console_file_entries_clear(data);
cvector_free(data->entries);
data->entries = NULL;
}
#ifdef NK_CONSOLE_FILE_ADD_FILES
/**
* get_label callback for the file list view. Appends "/" to directory entries.
*
* @internal
*/
static const char* nk_console_file_list_view_get_label(struct nk_console* list_view, nk_uint index) {
nk_console* file = nk_console_file_button_get_file_widget(list_view);
if (file == NULL || file->data == NULL) {
return NULL;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
if (cvector_empty(data->entries)) {
return "(Empty directory)";
}
if (index >= cvector_size(data->entries)) {
return NULL;
}
nk_console_file_entry* entry = &data->entries[index];
if (entry->is_directory) {
nk_size len = (nk_size)nk_strlen(entry->label);
if (len + 2 > sizeof(data->dir_label_buf)) {
return entry->label;
}
NK_MEMCPY(data->dir_label_buf, entry->label, len);
data->dir_label_buf[len] = '/';
data->dir_label_buf[len + 1] = '\0';
return data->dir_label_buf;
}
return entry->label;
}
#endif
/**
* Appends a path component to data->directory, inserting the platform separator.
* Returns nk_false and shows an error if the result would overflow the buffer.
*
* @internal
*/
static nk_bool nk_console_file_append_to_directory(nk_console_file_data* data, nk_console* file, const char* label) {
int len = nk_strlen(data->directory);
int label_len = nk_strlen(label);
int dir_len_after_slash = (len == 1 && data->directory[0] == '.') ? 0 : (len > 0 ? len + 1 : 0);
if (dir_len_after_slash + label_len + 1 > NK_CONSOLE_FILE_PATH_MAX) {
NK_ASSERT(0); // Path too long
nk_console_show_message(file, "Error: File path is too long.");
return nk_false;
}
if (len == 1 && data->directory[0] == '.') {
len = 0;
data->directory[0] = '\0';
}
else if (len > 0) {
// TODO: file: Make sure this is cross-platform.
#if defined(_WIN32) || defined(WIN32)
data->directory[len] = '\\';
#else
data->directory[len] = '/';
#endif
data->directory[len + 1] = '\0';
len++;
}
// TODO: file: Resolve the path properly, so the paths don't recurse. For example: folder/../folder
NK_MEMCPY(data->directory + len, label, (nk_size)(label_len + 1));
return nk_true;
}
#ifdef NK_CONSOLE_FILE_ADD_FILES
/**
* Click handler for the file list view. Navigates into directories or selects files.
*
* @internal
*/
static void nk_console_file_list_view_onclick(nk_console* list_view, void* user_data) {
NK_UNUSED(user_data);
nk_console* file = nk_console_file_button_get_file_widget(list_view);
if (file == NULL || file->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
nk_console_list_view_data* lv_data = (nk_console_list_view_data*)list_view->data;
nk_uint selected = lv_data->selected;
if (selected >= cvector_size(data->entries)) {
return;
}
nk_console_file_entry* entry = &data->entries[selected];
if (!nk_console_file_append_to_directory(data, file, entry->label)) {
return;
}
if (entry->is_directory) {
// Navigate into the directory.
nk_console_set_active_parent(file);
nk_console_add_event(file, NK_CONSOLE_EVENT_POST_RENDER_ONCE, &nk_console_file_refresh);
}
else {
// Copy the path to the file buffer.
int desired_length = nk_strlen(data->directory);
if (desired_length >= data->file_path_buffer_size) {
NK_ASSERT(0); // File path is too long
nk_console_show_message(file, "Error: File path is too long.");
}
else {
NK_MEMCPY(data->file_path_buffer, data->directory, (nk_size)desired_length);
data->file_path_buffer[desired_length] = '\0';
nk_console_trigger_event(file, NK_CONSOLE_EVENT_CHANGED);
}
// Exit the file browser.
nk_console_navigate_back(file);
}
}
#endif
/**
* Click handler for the "select this directory" button in directory-selection mode.
*
* @internal
*/
static void nk_console_file_select_dir_onclick(nk_console* button, void* user_data) {
NK_UNUSED(user_data);
nk_console* file = nk_console_file_button_get_file_widget(button);
if (file == NULL || file->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
int desired_length = nk_strlen(data->directory);
if (desired_length >= data->file_path_buffer_size) {
NK_ASSERT(0); // Directory path is too long
nk_console_show_message(file, "Error: Directory path is too long.");
}
else {
NK_MEMCPY(data->file_path_buffer, data->directory, (nk_size)desired_length);
data->file_path_buffer[desired_length] = '\0';
nk_console_trigger_event(file, NK_CONSOLE_EVENT_CHANGED);
}
// Exit the file browser.
nk_console_navigate_back(file);
}
/**
* Event hanlder for when the user clicks on an individual file entry.
*
* @internal
*/
static void nk_console_file_entry_onclick(nk_console* button, void* user_data) {
NK_UNUSED(user_data);
if (button == NULL || button->label == NULL) {
return;
}
nk_console* file = nk_console_file_button_get_file_widget(button);
if (file == NULL || file->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
if (!nk_console_file_append_to_directory(data, file, button->label)) {
return;
}
// Navigate into the directory (or back to parent via "..").
nk_console_set_active_parent(file);
nk_console_add_event(file, NK_CONSOLE_EVENT_POST_RENDER_ONCE, &nk_console_file_refresh);
}
#ifdef NK_CONSOLE_FILE_ADD_FILES
/**
* Click handler for file entry buttons (non-directory) in button view mode.
*
* @internal
*/
static void nk_console_file_button_file_onclick(nk_console* button, void* user_data) {
NK_UNUSED(user_data);
if (button == NULL || button->label == NULL) {
return;
}
nk_console* file = nk_console_file_button_get_file_widget(button);
if (file == NULL || file->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
if (!nk_console_file_append_to_directory(data, file, button->label)) {
return;
}
int desired_length = nk_strlen(data->directory);
if (desired_length >= data->file_path_buffer_size) {
NK_ASSERT(0);
nk_console_show_message(file, "Error: File path is too long.");
}
else {
NK_MEMCPY(data->file_path_buffer, data->directory, (nk_size)desired_length);
data->file_path_buffer[desired_length] = '\0';
nk_console_trigger_event(file, NK_CONSOLE_EVENT_CHANGED);
}
nk_console_navigate_back(file);
}
#endif
NK_API nk_console* nk_console_file_add_entry(nk_console* parent, const char* path, nk_bool is_directory) {
if (parent == NULL || path == NULL || path[0] == '\0') {
return NULL;
}
nk_console* file = nk_console_file_button_get_file_widget(parent);
if (file == NULL || file->data == NULL) {
return NULL;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
// Are we only selecting directories?
if (is_directory == nk_false && data->select_directory == nk_true) {
return NULL;
}
int len = nk_strlen(path);
// Ignore the current directory.
if (len == 1 && path[0] == '.') {
return NULL;
}
else if (len == 2 && path[0] == '.' && path[1] == '.') {
// Ignore the parent directory.
return NULL;
}
// Copy the basename as the entry label.
const char* basename = nk_console_file_basename(path);
nk_size basename_len = (nk_size)nk_strlen(basename);
char* label = (char*)NK_CONSOLE_MALLOC(nk_handle_id(0), NULL, basename_len + 1);
if (label == NULL) {
return NULL;
}
NK_MEMCPY(label, basename, basename_len);
label[basename_len] = '\0';
nk_console_file_entry entry;
entry.label = label;
entry.is_directory = is_directory;
cvector_push_back(data->entries, entry);
// Return the file widget as a non-NULL success indicator.
return file;
}
/**
* Gets the length of the directory string of the given file path.
*
* @internal
*/
static int nk_console_file_get_directory_len(const char* file_path) {
if (file_path == NULL) {
return 0;
}
int len = nk_strlen(file_path);
for (int i = len - 1; i > 0; i--) {
if (file_path[i] == '\\' || file_path[i] == '/') {
return i;
}
}
return 0;
}
/**
* Fills the list view with the files in the current directory.
*/
NK_API void nk_console_file_refresh(nk_console* widget, void* user_data) {
NK_UNUSED(user_data);
widget = nk_console_file_button_get_file_widget(widget);
if (widget == NULL || widget->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)widget->data;
// Clear existing entries.
nk_console_file_entries_clear(data);
// Build the static children (cancel, directory label, parent dir button) on the first call only.
// children[1]'s label points directly to data->directory, so it auto-updates on subsequent calls.
if (widget->children == NULL || cvector_empty(widget->children)) {
// Add the back/cancel button
nk_console* cancelButton = nk_console_button_onclick(widget, "Cancel", &nk_console_button_back);
nk_console_button_set_symbol(cancelButton, NK_SYMBOL_X);
// Show the active directory.
if (!data->select_directory) {
// Active directory label
nk_console* activeLabel = nk_console_label(widget, data->directory);
activeLabel->alignment = NK_TEXT_CENTERED;
}
else {
// Add a button to select the current directory.
nk_console* button = nk_console_button(widget, data->directory);
nk_console_button_set_symbol(button, NK_SYMBOL_CIRCLE_SOLID);
nk_console_set_tooltip(button, "Use this directory");
nk_console_add_event(button, NK_CONSOLE_EVENT_CLICKED, &nk_console_file_select_dir_onclick);
}
// Add the parent directory button
nk_console* parent_directory_button = nk_console_button_onclick(widget, "..", &nk_console_file_entry_onclick);
nk_console_button_set_symbol(parent_directory_button, NK_SYMBOL_TRIANGLE_LEFT);
nk_console_set_tooltip(parent_directory_button, "Navigate to the parent directory");
}
// Focus the parent directory button (always children[2]).
NK_ASSERT(cvector_size(widget->children) > 2 && widget->children[2] != NULL);
nk_console_set_active_widget(widget->children[2]);
#ifdef NK_CONSOLE_FILE_ADD_FILES
// Populate the entries array via the file system callback.
NK_CONSOLE_FILE_ADD_FILES(widget, data->directory);
if (data->use_list_view) {
// Show at least 1 item so the get_label callback can display "[Empty]".
nk_uint display_count = cvector_empty(data->entries) ? 1 : (nk_uint)cvector_size(data->entries);
if (cvector_size(widget->children) >= 4 && widget->children[3]->type == NK_CONSOLE_LIST_VIEW) {
// Update the existing list view in place.
nk_console_list_view_set_item_count(widget->children[3], display_count);
}
else {
// Remove any old button-mode children before creating the list view.
while (cvector_size(widget->children) > 3) {
size_t idx = cvector_size(widget->children) - 1;
nk_console* child = widget->children[idx];
cvector_erase(widget->children, idx);
nk_console_free(child);
}
nk_console* list_view = nk_console_list_view(widget, "file_entries", 10, display_count, &nk_console_file_list_view_get_label);
nk_console_add_event(list_view, NK_CONSOLE_EVENT_CLICKED, &nk_console_file_list_view_onclick);
}
}
else {
// Button mode: remove old entry children (list view or previous button entries).
while (cvector_size(widget->children) > 3) {
size_t idx = cvector_size(widget->children) - 1;
nk_console* child = widget->children[idx];
cvector_erase(widget->children, idx);
nk_console_free(child);
}
if (cvector_empty(data->entries)) {
nk_console* empty_label = nk_console_label(widget, "[Empty Directory]");
empty_label->alignment = NK_TEXT_CENTERED;
empty_label->disabled = nk_true;
}
else {
for (size_t i = 0; i < cvector_size(data->entries); i++) {
nk_console_file_entry* entry = &data->entries[i];
if (entry->is_directory) {
nk_console* btn = nk_console_button_onclick(widget, entry->label, &nk_console_file_entry_onclick);
nk_console_button_set_symbol(btn, NK_SYMBOL_TRIANGLE_RIGHT);
}
else {
nk_console_button_onclick(widget, entry->label, &nk_console_file_button_file_onclick);
}
}
}
// Focus the first entry button if available.
if (cvector_size(widget->children) > 3) {
nk_console_set_active_widget(widget->children[3]);
}
}
#else
// NK_CONSOLE_FILE_ADD_FILES is undefined, so back out.
nk_console_show_message(widget, "Error: File system not available.");
// Go back to the parent widget, and disable the widget.
if (widget->parent != NULL) {
widget->disabled = nk_true;
nk_console_set_active_parent(widget->parent);
}
#endif
}
/**
* Event handler to clear out unneeded data for the file widget.
*
* @see nk_console_file_event_back
* @internal
*/
static void nk_console_file_event_back_post_render(nk_console* file, void* user_data) {
// Remove all the children, since we don't need them.
nk_console_free_children(file);
// Clear out all the file entries too.
nk_console_file_event_destroy(file, user_data);
}
/**
* Event handler to clear out all unneeded data when not using the widget.
* @internal
*/
static void nk_console_file_event_back(nk_console* file, void* user_data) {
NK_UNUSED(user_data);
// Clear it out at post-render to avoid segfaults.
nk_console_add_event(file, NK_CONSOLE_EVENT_POST_RENDER_ONCE, &nk_console_file_event_back_post_render);
}
/**
* Event handler: Called when the make file button is clicked.
*
* Will build out the sub-elements to select a file.
*
* @internal
*/
static void nk_console_file_event_clicked(nk_console* button, void* user_data) {
NK_UNUSED(user_data);
if (button == NULL || button->data == NULL) {
return;
}
nk_console* file = nk_console_file_button_get_file_widget(button);
if (file == NULL || file->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
int directory_len = nk_console_file_get_directory_len(data->file_path_buffer);
NK_MEMCPY(data->directory, data->file_path_buffer, (nk_size)directory_len);
data->directory[directory_len] = '\0';
if (nk_strlen(data->directory) == 0) {
// TODO: file: Make get current working directory function.
data->directory[0] = '.';
data->directory[1] = '\0';
}
// Set the active parent to the file widget, and refresh it after rendering everything else.
nk_console_set_active_parent(file);
nk_console_add_event(file, NK_CONSOLE_EVENT_POST_RENDER_ONCE, &nk_console_file_refresh);
}
NK_API void nk_console_file_set_list_view(nk_console* file, nk_bool use_list_view) {
file = nk_console_file_button_get_file_widget(file);
if (file == NULL || file->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
data->use_list_view = use_list_view;
}
NK_API nk_bool nk_console_file_get_list_view(nk_console* file) {
file = nk_console_file_button_get_file_widget(file);
if (file == NULL || file->data == NULL) {
return nk_false;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
return data->use_list_view;
}
NK_API void nk_console_file_set_file_user_data(nk_console* file, void* user_data) {
file = nk_console_file_button_get_file_widget(file);
if (file == NULL || file->data == NULL) {
return;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
data->file_user_data = user_data;
}
NK_API void* nk_console_file_get_file_user_data(nk_console* file) {
file = nk_console_file_button_get_file_widget(file);
if (file == NULL || file->data == NULL) {
return NULL;
}
nk_console_file_data* data = (nk_console_file_data*)file->data;
return data->file_user_data;
}
NK_API nk_console* nk_console_file(nk_console* parent, const char* label, char* file_path_buffer, int file_path_buffer_size) {
if (parent == NULL || file_path_buffer == NULL || file_path_buffer_size <= 0) {
return NULL;
}
// Create the widget data.
nk_console_file_data* data = (nk_console_file_data*)NK_CONSOLE_MALLOC(nk_handle_id(0), NULL, sizeof(nk_console_file_data));
nk_zero(data, sizeof(nk_console_file_data));
data->file_path_buffer = file_path_buffer;
data->file_path_buffer_size = file_path_buffer_size;
nk_console* widget = nk_console_label(parent, label);
widget->type = NK_CONSOLE_FILE;
widget->columns = label == NULL ? 1 : 2;
widget->render = nk_console_file_render;
widget->selectable = nk_true;
widget->data = data;
nk_console_add_event(widget, NK_CONSOLE_EVENT_CLICKED, &nk_console_file_event_clicked);
nk_console_add_event(widget, NK_CONSOLE_EVENT_BACK, &nk_console_file_event_back);
nk_console_add_event(widget, NK_CONSOLE_EVENT_DESTROYED, &nk_console_file_event_destroy);
return widget;
}
NK_API nk_console* nk_console_dir(nk_console* parent, const char* label, char* dir_buffer, int dir_buffer_size) {
nk_console* widget = nk_console_file(parent, label, dir_buffer, dir_buffer_size);
if (widget == NULL) {
return NULL;
}
nk_console_file_data* data = (nk_console_file_data*)widget->data;
data->select_directory = nk_true;
return widget;
}
#if defined(__cplusplus)
}
#endif
#endif // NK_CONSOLE_FILE_IMPLEMENTATION_ONCE
#endif // NK_CONSOLE_IMPLEMENTATION