-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
2647 lines (2249 loc) · 98.6 KB
/
Copy pathapp.cpp
File metadata and controls
2647 lines (2249 loc) · 98.6 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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define CREATE_STRING(var, size, format, ...) char var[size]; \
snprintf(var, size, \
format, __VA_ARGS__);
bool win32_file_exists (char *path){
DWORD attrib = GetFileAttributes(path);
return (attrib != INVALID_FILE_ATTRIBUTES && !(attrib & FILE_ATTRIBUTE_DIRECTORY));
}
// @analyze: use fuzzy finder library?
bool win32_find_file (char *buffer, size_t buffer_size, char *folder_path, char *filename)
{
bool result = false;
// @note: depth 0 is folder_path
constexpr int max_depth = 4;
int last_depth = -1;
int depth = 0;
// @note: dir is a stack of currently and previously used dirs
constexpr int dir_name_size = PATH_MAX_SIZE;
char dir[max_depth][dir_name_size];
while (true) {
char full_dir_path[MAX_PATH];
strcpy_s(full_dir_path, folder_path);
for (int i = 0; i < depth - 1; i++) {
strcat_s(full_dir_path, dir[i]);
}
// @note: when we first open a folder, check
// if the file we're looking for exists
if (depth > last_depth) {
char file_path[MAX_PATH];
strcpy_s(file_path, full_dir_path);
if (depth > 0) strcat_s(file_path, dir[depth - 1]);
strcat_s(file_path, filename);
if (win32_file_exists(file_path)) {
strcpy_s(buffer, buffer_size, file_path);
result = true;
break;
}
}
// @note: prepare to find folder
if (depth == max_depth) {
last_depth = depth;
depth--;
} else if (depth > 0) {
strcat_s(full_dir_path, dir[depth - 1]);
}
strcat_s(full_dir_path, MAX_PATH, "*");
bool found_next_dir = false;
WIN32_FIND_DATAA file_data;
HANDLE file = FindFirstFile(full_dir_path, &file_data);
if (file != INVALID_HANDLE_VALUE) {
defer { FindClose(file); };
bool found_prev_dir = false;
do {
if (!(file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue;
if (strcmp(file_data.cFileName, ".") == 0 ||
strcmp(file_data.cFileName, "..") == 0 ||
stricmp(file_data.cFileName, "$RECYCLE.BIN") == 0 ||
stricmp(file_data.cFileName, "System Volume Information") == 0) continue;
// @note: sadly, skip file name bigger than we can handle :(
if (strlen(file_data.cFileName) > dir_name_size) continue;
// @note: find the previous folder we were in, then find
// the next folder to open
if (depth < last_depth && !found_prev_dir) {
char temp[dir_name_size];
char *ctx;
strcpy_s(temp, dir[depth]);
strtok_s(temp, "\\", &ctx);
if (strcmp(temp, file_data.cFileName) == 0) {
found_prev_dir = true;
}
continue;
}
strcpy_s(dir[depth], file_data.cFileName);
strcat_s(dir[depth], dir_name_size, "\\");
last_depth = depth;
depth++;
found_next_dir = true;
break;
} while(FindNextFile(file, &file_data));
}
// @note: go up a dir
if (!found_next_dir) {
last_depth = depth;
depth--;
}
if (depth < 0) {
break;
}
}
return result;
}
bool open_file_dialog (char path[MAX_PATH])
{
OPENFILENAMEA ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = GetActiveWindow();
ofn.lpstrFile = path;
ofn.nMaxFile = MAX_PATH;
// ofn.lpstrFilter = "Images (*.png, *.jpg)\0*.png\0";
ofn.lpstrFilter = NULL;
ofn.Flags = OFN_FILEMUSTEXIST;
return GetOpenFileName(&ofn) == TRUE;
}
ImTextureID get_file_tex (App_State *state, char *extension)
{
ImTextureID id = state->tex_file.id;
if (strcmp(extension, "exe") == 0) {
id = state->tex_file_exe.id;
} else if (strcmp(extension, "txt") == 0) {
id = state->light_mode ? state->tex_file_txt_dark.id : state->tex_file_txt.id;
} else if (strcmp(extension, "mp3") == 0) {
id = state->tex_file_mp3.id;
} else if (strcmp(extension, "mp4") == 0) {
id = state->tex_file_mp4.id;
} else if (strcmp(extension, "png") == 0 ||
strcmp(extension, "jpg") == 0 ||
strcmp(extension, "jpeg") == 0) {
id = state->tex_file_image.id;
} else if (strcmp(extension, "zip") == 0) {
id = state->tex_file_zip.id;
} else if (strcmp(extension, "docx") == 0) {
id = state->tex_file_word.id;
}
return id;
}
void text_centered (char *text, ImVec2 p, ImVec2 s, bool clip = true)
{
ImDrawList *draw_list = ImGui::GetWindowDrawList();
ImGuiContext& g = *GImGui;
ImGuiStyle& style = g.Style;
char *text_end = text + strlen(text);
const char *cursor = text;
const char *word_wrap_eol = NULL;
ImVec4 clipping = ImVec4(p.x,
p.y,
p.x + s.x,
p.y + s.y);
// if (clip) {
// draw_list->PushClipRect(ImVec2(p.x, p.y), ImVec2(p.x + s.x, p.y + 10), true);
// }
int line = 0;
while (cursor < text_end) {
if (line > 5) break;
word_wrap_eol = g.Font->ImFont::CalcWordWrapPositionA(1,
cursor,
text_end,
s.x);
if (*cursor == ' ') {
cursor++;
}
if (word_wrap_eol == cursor) {
word_wrap_eol++;
}
float offset = (s.x - ImGui::CalcTextSize(cursor, word_wrap_eol, true, s.x).x) / 2;
draw_list->AddText(g.Font,
g.FontSize,
ImVec2(p.x + offset, p.y + line * g.FontSize),
ImGui::GetColorU32(ImGuiCol_Text),
cursor,
word_wrap_eol,
s.x,
clip ? &clipping : NULL);
cursor = word_wrap_eol;
line++;
}
// if (clip) {
// ImGui::PopClipRect();
// }
}
Texture get_texture(char *path)
{
Texture tex = {};
int channels = 0;
uchar *image = stbi_load(path, &tex.width, &tex.height, &channels, 0);
if (!image) {
printf("File doesn't exists: %s\n", path);
assert(false);
}
int type = GL_RGBA;
if (channels == 3) {
type = GL_RGB;
} else if (channels != 4) {
return tex;
}
GLuint id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, type, tex.width, tex.height, 0, type, GL_UNSIGNED_BYTE, image);
stbi_image_free(image);
tex.id = (ImTextureID)(intptr_t)id;
return tex;
}
void change_wallpaper (App_State *state)
{
}
Texture get_app_texture (App_State *state, char *path)
{
char temp[PATH_MAX_SIZE];
snprintf(temp, PATH_MAX_SIZE, "%s\\data\\%s", state->exe_path, path);
return get_texture(temp);
}
// @note: "C:\\\\test\\\\\" -> "C:\test\"
void remove_multiple_slashes (char *path)
{
char *cursor = path;
char *next_cursor = path;
bool slash = false;
while (*next_cursor) {
if (!slash) {
if (*next_cursor == '\\') {
slash = true;
} else {
*(cursor++) = *next_cursor;
}
} else {
if (*next_cursor != '\\') {
*(cursor++) = '\\';
*(cursor++) = *next_cursor;
slash = false;
}
}
next_cursor++;
}
if (slash) {
*(cursor++) = '\\';
}
*(cursor++) = '\0';
}
bool file_button (ImTextureID id, char *text, ImVec2 image_size)
{
bool result = false;
ImVec2 p = ImGui::GetCursorScreenPos();
ImDrawList *draw_list = ImGui::GetWindowDrawList();
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
ImVec2 padding = style.FramePadding;
ImVec2 text_size = ImGui::CalcTextSize(text, NULL, true, image_size.x - padding.x * 2);
ImVec2 button_size = ImVec2(image_size.x, image_size.y + (text_size.y < 30 ? text_size.y : 30));
result = ImGui::InvisibleButton(text, button_size + padding * 2);
bool hover = ImGui::IsItemHovered() &&
(!ImGui::IsAnyItemActive() || ImGui::IsItemActive());
bool click = ImGui::IsItemClicked(0);
ImU32 col = ImGui::GetColorU32((hover && click) ? ImGuiCol_ButtonActive :
hover ? ImGuiCol_ButtonHovered :
ImGuiCol_Button);
if (hover) {
ImVec2 min = draw_list->GetClipRectMin();
ImVec2 max = draw_list->GetClipRectMax();
// @note: the size and location of this window doesn't matter,
// we just need its draw_list
ImGui::SetNextWindowFocus();
ImGui::SetNextWindowSize(ImVec2(10, 10));
ImGui::SetNextWindowBgAlpha(0);
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoFocusOnAppearing;
ImGui::Begin("Folder Name Hover", NULL, flags);
draw_list = ImGui::GetWindowDrawList();
draw_list->PushClipRect(min, max);
// @note: if the bottom of the hover is clipped, offset it
float hover_bottom = p.y + image_size.y + text_size.y;
if (hover_bottom > max.y) {
p.y -= hover_bottom - max.y + padding.y * 2;
}
}
draw_list->AddRectFilled(p,
ImVec2(p.x + button_size.x + padding.x * 2,
p.y + (hover ? image_size.y + text_size.y : button_size.y) + padding.y * 2),
col,
ImClamp((float)ImMin(padding.x, padding.y), 0.0f, g.Style.FrameRounding));
float offset = padding.x;
draw_list->AddImage(id,
p + padding + ImVec2(offset, 0),
p + image_size - padding + ImVec2(offset, 0));
ImVec4 clipping = ImVec4(p.x,
p.y,
p.x + button_size.x,
p.y + button_size.y + g.FontSize / 2);
char *cursor = text;
while (*cursor) {
if (*cursor == '#' && *(cursor + 1) == '#') {
break;
}
cursor++;
}
CREATE_STRING(temp_text, PATH_MAX_SIZE,
"%.*s", (int) (cursor - text), text);
text_centered(temp_text, ImVec2(p.x + padding.x, p.y + image_size.y), ImVec2(button_size.x, button_size.y - image_size.y + padding.y), !hover);
// draw_list->AddText(g.Font,
// g.FontSize,
// ImVec2(p.x + padding.x, p.y + image_size.y),
// ImGui::GetColorU32(ImGuiCol_Text),
// temp_text,
// NULL,
// button_size.x - padding.x,
// hover ? NULL : &clipping);
if (hover) {
draw_list->PopClipRect();
ImGui::End();
}
return result;
}
// @note:
// ---------------padding.y----------------
// padding.x|image|padding.x|text|padding.x
// ---------------padding.y----------------
bool button_with_icon (ImTextureID icon_id, char *text, ImVec2 size = ImVec2(0, 0))
{
bool result = false;
ImVec2 p = ImGui::GetCursorScreenPos();
ImDrawList *draw_list = ImGui::GetWindowDrawList();
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
ImVec2 padding = style.FramePadding;
ImVec2 text_size = ImGui::CalcTextSize(text, NULL);
ImVec2 button_size = ImVec2(size.x == 0 ? text_size.x : size.x,
size.y == 0 ? ImGui::GetFrameHeight() - padding.y * 2 :
size.y);
button_size.x += button_size.y; // @note: icon size
result = ImGui::InvisibleButton(text,
button_size + padding * 2 + ImVec2(padding.x, 0));
bool hover = ImGui::IsItemHovered() &&
(!ImGui::IsAnyItemActive() || ImGui::IsItemActive());
bool click = ImGui::IsItemClicked(0);
ImU32 col = ImGui::GetColorU32((hover && click) ? ImGuiCol_ButtonActive :
hover ? ImGuiCol_ButtonHovered :
ImGuiCol_Button);
draw_list->AddRectFilled(p,
p + button_size + padding * 2 + ImVec2(padding.x, 0),
col,
g.Style.FrameRounding);
draw_list->AddImage(icon_id,
p + padding,
p + padding + ImVec2(button_size.y, button_size.y));
draw_list->AddText(ImVec2(p.x + button_size.y + padding.x * 2, p.y + padding.y),
ImGui::GetColorU32(ImGuiCol_Text),
text,
NULL);
return result;
}
// @todo: support unix's forward slash
char *get_last_slash (char *str, bool include_slash = true)
{
char *last_slash = str;
int offset = include_slash ? 1 : 0;
char *temp = str;
while (*temp) {
if (*temp == '\\') {
last_slash = temp + offset;
}
temp++;
}
return last_slash;
}
void change_dir (App_State *state)
{
// Back_History *back = &state->back;
}
void add_back (App_State *state)
{
Back_History *back = &state->tabs[state->current_tab].back;
assert(back->cursor < back->count);
back->count = back->cursor + 1;
int index = (back->start + back->count) % HISTORY_MAX_COUNT;
strcpy_s(back->history[index], state->tabs[state->current_tab].current_dir);
if (back->count < HISTORY_MAX_COUNT) {
back->cursor++;
back->count++;
} else {
back->start = (back->start + 1) % HISTORY_MAX_COUNT;
}
// printf("\"%s\" start: %d, cursor: %d, count: %d\n", back->history[index], back->start, back->cursor, back->count);
}
void add_tab (App_State *state, char *start_path = NULL)
{
if (state->tab_count >= TAB_MAX_COUNT) return;
int index = 0;
while (index < TAB_MAX_COUNT && state->tabs[index].id != 0) index++;
if (index == TAB_MAX_COUNT) assert(!"Couldn't find empty tab to fill!");
// state->tab_indices[state->tab_count] = index;
Tab *tab = &state->tabs[index];
if (start_path == NULL) start_path = state->start_path;
tab->id = state->tab_gen_id++;
strcpy_s(tab->current_dir, PATH_MAX_SIZE, start_path);
tab->back.start = 0;
tab->back.cursor = 0;
tab->back.count = 1;
strcpy_s(tab->back.history[0], start_path);
tab->selected_count = 0;
state->tab_count++;
}
void remove_tab (App_State *state, int index)
{
assert(index > -1 && index < TAB_MAX_COUNT);
state->tabs[index].id = 0;
// int temp = index;
// while (temp < state->tab_count - 1) {
// state->tab_indices[temp] = state->tab_indices[temp + 1];
// }
state->tab_count--;
}
void go_back (App_State *state)
{
Back_History *back = &state->tabs[state->current_tab].back;
if (back->cursor <= 0) return;
back->cursor--;
int index = (back->start + back->cursor) % HISTORY_MAX_COUNT;
strcpy_s(state->tabs[state->current_tab].current_dir, back->history[index]);
}
void go_forward (App_State *state)
{
Back_History *back = &state->tabs[state->current_tab].back;
if (back->cursor >= back->count - 1) return;
back->cursor++;
int index = (back->start + back->cursor) % HISTORY_MAX_COUNT;
strcpy_s(state->tabs[state->current_tab].current_dir, back->history[index]);
}
Tab *get_current_tab (App_State *state)
{
return &state->tabs[state->current_tab];
}
void open_file_or_folder (App_State *state, char *path, bool is_folder)
{
char *current_dir = get_current_tab(state)->current_dir;
if (is_folder) {
snprintf(current_dir, PATH_MAX_SIZE, "%s\\", path);
add_back(state);
} else {
ShellExecute(NULL, NULL, path, NULL, NULL, SW_SHOW);
}
}
// @todo: "\Handbrake" is illegal!!! must be "D:\Handbrake"
void path_bar (App_State *state)
{
Tab *current_tab = &state->tabs[state->current_tab];
char *current_dir = current_tab->current_dir;
ImVec2 image_button_size = ImVec2(14, 14);
ImGui::PushStyleColor(ImGuiCol_Button, state->theme.path_button);
if (ImGui::ImageButton(state->light_mode ? state->tex_back_dark.id :
state->tex_back.id,
image_button_size) ||
(!ImGui::IsAnyItemActive() && // @note: just incase
ImGui::IsKeyPressed(ImGuiKey_Backspace)))
{
go_back(state);
}
ImGui::SameLine();
if (ImGui::ImageButton(state->light_mode ? state->tex_forward_dark.id :
state->tex_forward.id,
image_button_size)) {
go_forward(state);
}
ImGui::SameLine();
if (ImGui::ImageButton(state->light_mode ? state->tex_up_dark.id :
state->tex_up.id,
image_button_size)) {
char *first_folder = current_dir;
char *second_to_last_folder = current_dir;
char *temp = current_dir;
int found_backslash = 0;
while (*temp) {
if (*temp == '\\') {
if (*(temp + 1) == '\0') {
break;
} else {
second_to_last_folder = temp + 1;
found_backslash++;
}
}
temp++;
}
if (second_to_last_folder != current_dir) {
*second_to_last_folder = '\0';
} else if (found_backslash > 1) {
*first_folder = '\0';
}
add_back(state);
}
ImGui::PopStyleColor();
ImGui::SameLine();
float size = ImGui::GetContentRegionAvail().x - image_button_size.x * 2 - ImGui::GetStyle().FramePadding.x * 5 - ImGui::CalcTextSize("Search", NULL).x - ImGui::GetStyle().ItemSpacing.x * 2;
// float start_x = ImGui::GetItemRectMax().x + ImGui::GetStyle().ItemSpacing.x;
if (current_tab->editing) {
static char prev_path[PATH_MAX_SIZE];
bool init = false;
if (!ImGui::IsAnyItemActive() && !ImGui::IsMouseClicked(0)) {
ImGui::SetKeyboardFocusHere();
init = true;
strcpy_s(prev_path, current_tab->current_dir);
}
char temp[32];
snprintf(temp, 32, "##input%d", current_tab->id);
ImGui::SetNextItemWidth(size + image_button_size.x + ImGui::GetStyle().FramePadding.x * 2);
if (ImGui::InputText(temp,
current_dir,
PATH_MAX_SIZE,
ImGuiInputTextFlags_EnterReturnsTrue) ||
((ImGui::IsMouseClicked(0) || ImGui::IsMouseClicked(1)) &&
!ImGui::IsItemHovered()))
{
if (!state->path_invalid) {
remove_multiple_slashes(current_dir);
// @note: if the path is a valid folder (no trailing word after slash)
if (get_last_slash(current_dir)[0] == '\0') {
add_back(state);
}
} else {
strcpy_s(current_tab->current_dir, prev_path);
}
current_tab->editing = false;
}
if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
current_tab->editing = false;
}
if (init) {
// @note: clear selection when we first focus
ImGuiID id = ImGui::GetItemID();
if (ImGuiInputTextState *state = ImGui::GetInputTextState(id)) {
state->ClearSelection();
}
}
} else {
if (ImGui::BeginChild("Path Select",
ImVec2(size,
0),
false,
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_HorizontalScrollbar))
{
char temp_path[256];
strcpy_s(temp_path, current_dir);
bool start = true;
char *folder, *ctx;
folder = strtok_s(temp_path, "\\", &ctx);
int index = 0;
while (folder) {
if (start) {
ImGui::PushStyleColor(ImGuiCol_Button, state->theme.path_button);
start = false;
}
char *next_folder = strtok_s(NULL, "\\", &ctx);
if (!next_folder) {
ImGui::PopStyleColor();
}
ImGui::PushID(index);
if (ImGui::Button(folder)) {
char *backslash = folder;
while (*backslash) backslash++;
current_dir[backslash - temp_path + 1] = '\0';
// printf("%s\n", current_dir);
add_back(state);
}
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) {
current_tab->editing = true;
}
ImGui::PopID();
ImGui::SameLine(0, 0);
folder = next_folder;
if (folder) {
ImGui::Text("|");
ImGui::SameLine(0, 0);
}
index++;
}
static float last_width = ImGui::GetScrollMaxX();
if (last_width < ImGui::GetScrollMaxX()) {
ImGui::SetScrollHereX();
}
last_width = ImGui::GetScrollMaxX();
ImGui::EndChild();
}
ImGui::SameLine();
ImGui::PushStyleColor(ImGuiCol_Button, state->theme.path_button);
// Edit
if (ImGui::ImageButton(state->light_mode ? state->tex_edit_dark.id :
state->tex_edit.id,
image_button_size)) {
current_tab->editing = true;
}
ImGui::SameLine();
// Search
if (button_with_icon(state->light_mode ? state->tex_search_dark.id :
state->tex_search.id,
"Search",
ImVec2(0, image_button_size.y))) {
state->open_search = true;
}
ImGui::PopStyleColor();
}
}
int group_hash_func (char *str)
{
int result = 0;
int len = strlen(str);
for (int i = 0; i < len; i++) {
result += str[i] * 7 + result * 31;
}
return result % GROUP_MAX_COUNT;
}
void set_theme_dark (App_State *state)
{
Theme *theme = &state->theme;
ImGuiStyle &style = ImGui::GetStyle();
style.Colors[ImGuiCol_Text] = ImVec4(1, 1, 1, 1);
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.15, 0.15, 0.15, 1);
style.Colors[ImGuiCol_PopupBg] = ImVec4(0.15, 0.15, 0.15, 1);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.15, 0.15, 0.15, 1);
style.Colors[ImGuiCol_Button] = ImVec4(0.25, 0.25, 0.25, 1);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.2, 0.2, 0.2, 1);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.2, 0.2, 0.2, 1);
style.Colors[ImGuiCol_Tab] = style.Colors[ImGuiCol_Button];
style.Colors[ImGuiCol_TabHovered] = style.Colors[ImGuiCol_ButtonHovered];
style.Colors[ImGuiCol_TabActive] = style.Colors[ImGuiCol_ButtonActive];
style.Colors[ImGuiCol_FrameBg] = style.Colors[ImGuiCol_Button];
style.Colors[ImGuiCol_FrameBgHovered] = style.Colors[ImGuiCol_ButtonHovered];
style.Colors[ImGuiCol_FrameBgActive] = style.Colors[ImGuiCol_ButtonActive];
theme->path_button = ImVec4(0, 0, 0, 0);
// theme->path_button_hover;
// theme->path_button_click;
theme->files_button = ImVec4(0, 0, 0, 0);
theme->files_button_hover = ImGui::GetStyle().Colors[ImGuiCol_Button];
// theme->files_button_hover.x *= 0.7;
// theme->files_button_hover.y *= 0.7;
// theme->files_button_hover.z *= 0.7;
// theme->files_button_hover.w = 1;
theme->files_button_click = ImGui::GetStyle().Colors[ImGuiCol_ButtonHovered];
theme->files_selected_button = theme->files_button_click;
theme->files_selected_button_hover = theme->files_button_click;
// theme->files_selected_button_click;
}
void set_theme_light (App_State *state)
{
Theme *theme = &state->theme;
ImGuiStyle &style = ImGui::GetStyle();
style.Colors[ImGuiCol_Text] = ImVec4(0, 0, 0, 1);
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.75, 0.75, 0.75, 1);
style.Colors[ImGuiCol_PopupBg] = ImVec4(0.75, 0.75, 0.75, 1);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.75, 0.75, 0.75, 1);
style.Colors[ImGuiCol_Button] = ImVec4(0.8, 0.8, 0.8, 1);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.85, 0.85, 0.85, 1);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.85, 0.85, 0.85, 1);
style.Colors[ImGuiCol_Tab] = style.Colors[ImGuiCol_Button];
style.Colors[ImGuiCol_TabHovered] = style.Colors[ImGuiCol_ButtonHovered];
style.Colors[ImGuiCol_TabActive] = style.Colors[ImGuiCol_ButtonActive];
style.Colors[ImGuiCol_FrameBg] = style.Colors[ImGuiCol_Button];
style.Colors[ImGuiCol_FrameBgHovered] = style.Colors[ImGuiCol_ButtonHovered];
style.Colors[ImGuiCol_FrameBgActive] = style.Colors[ImGuiCol_ButtonActive];
theme->path_button = ImVec4(0, 0, 0, 0);
// theme->path_button_hover;
// theme->path_button_click;
theme->files_button = ImVec4(0, 0, 0, 0);
theme->files_button_hover = ImGui::GetStyle().Colors[ImGuiCol_Button];
theme->files_button_click = ImGui::GetStyle().Colors[ImGuiCol_ButtonHovered];
theme->files_selected_button = theme->files_button_click;
theme->files_selected_button_hover = theme->files_button_click;
}
void add_favorite (App_State *state, char *path)
{
for (int i = 0; i < state->favorite_count; i++) {
if (strcmp(path, state->favorites[i]) == 0) {
return;
}
}
strcpy_s(state->favorites[state->favorite_count++], path);
}
void paste(App_State *state)
{
Tab *tab = get_current_tab(state);
// @todo: implementation
}
Group *get_group (App_State *state)
{
Group *result = NULL;
char *current_dir = state->tabs[state->current_tab].current_dir;
Group *group = &state->groups[group_hash_func(current_dir)];
if (strcmp(group->location, current_dir)) {
result = group;
} else {
Group *next = group->next;
for (; next; next = next->next) {
if (strcmp(next->location, current_dir)) {
result = next;
break;
}
}
}
return result;
}
void group_button (ImTextureID id, char *text, ImVec2 image_size, ImTextureID tex[4])
{
ImVec2 p = ImGui::GetCursorScreenPos();
ImDrawList *draw_list = ImGui::GetWindowDrawList();
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
ImVec2 padding = style.FramePadding;
ImVec2 text_size = ImGui::CalcTextSize(text, NULL, true, image_size.x - padding.x);
ImVec2 button_size = ImVec2(image_size.x, image_size.y + (text_size.y < 30 ? text_size.y : 30));
ImGui::InvisibleButton(text, button_size + padding * 4);
bool hover = ImGui::IsItemHovered() &&
(!ImGui::IsAnyItemActive() || ImGui::IsItemActive());
bool click = ImGui::IsItemClicked(0);
ImU32 col = ImGui::GetColorU32(ImGuiCol_PopupBg);
draw_list->AddRectFilled(p + padding,
ImVec2(p.x + button_size.x,
p.y + button_size.y) + padding,
col,
ImClamp((float)ImMin(padding.x, padding.y),
0.0f,
g.Style.FrameRounding));
ImVec2 small_image_size = image_size / ImVec2(2, 2);
float offset = padding.x;
for (int i = 0; tex[i] && i < 4; i++) {
float x = ((i % 2) * small_image_size.x);
float y = (i / 2) * small_image_size.y;
draw_list->AddImage(tex[i],
p + ImVec2(x, y) + padding + ImVec2(offset, 0),
p + ImVec2(x, y) + small_image_size - padding + ImVec2(offset, 0));
}
ImVec4 clipping = ImVec4(p.x,
p.y,
p.x + button_size.x,
p.y + button_size.y + g.FontSize / 2);
char *cursor = text;
while (*cursor) {
if (*cursor == '#' && *(cursor + 1) == '#') {
break;
}
cursor++;
}
CREATE_STRING(temp_text, PATH_MAX_SIZE, "%.*s", (int) (cursor - text), text);
text_centered(temp_text, ImVec2(p.x + padding.x * 2, p.y + image_size.y), ImVec2(button_size.x, button_size.y - image_size.y + padding.y), !hover);
// draw_list->AddText(g.Font,
// g.FontSize,
// ImVec2(p.x + padding.x, p.y + image_size.y),
// ImGui::GetColorU32(ImGuiCol_Text),
// temp_text,
// NULL,
// button_size.x - padding.x,
// hover ? NULL : &clipping);
}
#define FILENAME_MAX_COUNT 512
#define FILENAME_MAX_SIZE 128
// @analyze: cache this, and update every so often?
void files_and_folders (App_State *state)
{
Tab *current_tab = get_current_tab(state);
char *current_dir = current_tab->current_dir;
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
ImGui::PushStyleColor(ImGuiCol_Button, state->theme.files_button);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, state->theme.files_button_hover);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, state->theme.files_button_click);
char path[MAX_PATH];
snprintf(path, MAX_PATH, "%s*", current_dir);
// @todo: put these in platform specific *_app.cpp
WIN32_FIND_DATAA file_data;
HANDLE file_handle = FindFirstFile(path, &file_data);
if (file_handle == INVALID_HANDLE_VALUE ||
get_last_slash(path) == path ||
*path == '\\') {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1, 0, 0, 1));
ImGui::TextWrapped("Invalid Path!");
ImGui::PopStyleColor();
ImGui::TextWrapped("Might be because the path doesn't exist or Filex doesn't have the permission to access it.");
ImGui::PopStyleColor(3);
ImGui::PopStyleVar(2);
state->path_invalid = true;
return;
}
state->path_invalid = false;
defer { FindClose(file_handle); };
char folders[FILENAME_MAX_COUNT][FILENAME_MAX_SIZE];
uint folder_count = 0;
char files[FILENAME_MAX_COUNT][FILENAME_MAX_SIZE];
uint file_count = 0;
do {
if (strcmp(file_data.cFileName, ".") == 0 ||
strcmp(file_data.cFileName, "..") == 0 ||
strcmp(file_data.cFileName, "$RECYCLE.BIN") == 0 ||
strcmp(file_data.cFileName, "System Volume Information") == 0) continue;
if (strlen(file_data.cFileName) > FILENAME_MAX_SIZE) continue;
if (!state->show_hidden_files && file_data.cFileName[0] == '.') continue;
if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
assert(folder_count < FILENAME_MAX_COUNT);
strcpy_s(folders[folder_count++], file_data.cFileName);
} else {
assert(file_count < FILENAME_MAX_COUNT);
strcpy_s(files[file_count++], file_data.cFileName);
}
} while (FindNextFile(file_handle, &file_data));
assert(folder_count + file_count < SELECTED_MAX_COUNT);
// @note: reset ui when changing folder
static char prev_path[MAX_PATH];
if (strcmp(prev_path, current_dir) != 0) {
ImGui::SetScrollHereY(0);
current_tab->selected_count = 0;
strcpy_s(prev_path, current_dir);
}
ImGuiStyle &style = ImGui::GetStyle();
ImVec2 button_size = ImVec2(90, 90);
float window_visible = ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x;
bool is_selecting = false;
{
Group *start_group = &state->groups[group_hash_func(current_dir)];
Group *group = start_group;
static uint group_opened_id = 0;
if (group != NULL && group->id != 0) {
bool first = false;
ImVec2 p = ImVec2(0, 0);
bool open = false;
for (; group; group = group->next) {
if (strcmp(group->location, current_dir) != 0) continue;
ImTextureID tex[4] = {};
for (int i = 0; i < 4 && i < group->count; i++) {
char *text = group->folders[i];
char *extension = text;
{
char *temp = text;
while (*temp) {
if (*temp == '.') {
extension = temp + 1;
}
temp++;
}
}
if (text == extension) {
tex[i] = state->tex_folder.id;
} else {
tex[i] = get_file_tex(state, extension);
}
}