1
2 #include "../../lv_examples.h"
3
4 #if LV_USE_TABLE && LV_USE_DROPDOWN && LV_USE_FILE_EXPLORER && (LV_USE_FS_STDIO || LV_USE_FS_POSIX || LV_USE_FS_WIN32 || LV_USE_FS_FATFS) && LV_BUILD_EXAMPLES
5
6 #include <stdlib.h>
7 #include <string.h>
8
file_explorer_event_handler(lv_event_t * e)9 static void file_explorer_event_handler(lv_event_t * e)
10 {
11 lv_event_code_t code = lv_event_get_code(e);
12 lv_obj_t * obj = lv_event_get_target(e);
13
14 if(code == LV_EVENT_VALUE_CHANGED) {
15 const char * cur_path = lv_file_explorer_get_current_path(obj);
16 const char * sel_fn = lv_file_explorer_get_selected_file_name(obj);
17
18 LV_LOG_USER("%s%s", cur_path, sel_fn);
19 }
20 }
21
22 #if LV_FILE_EXPLORER_QUICK_ACCESS
btn_event_handler(lv_event_t * e)23 static void btn_event_handler(lv_event_t * e)
24 {
25 lv_event_code_t code = lv_event_get_code(e);
26 lv_obj_t * btn = lv_event_get_target(e);
27 lv_obj_t * file_explorer = lv_event_get_user_data(e);
28
29 if(code == LV_EVENT_VALUE_CHANGED) {
30 if(lv_obj_has_state(btn, LV_STATE_CHECKED))
31 lv_obj_add_flag(file_explorer, LV_OBJ_FLAG_HIDDEN);
32 else
33 lv_obj_remove_flag(file_explorer, LV_OBJ_FLAG_HIDDEN);
34 }
35 }
36
dd_event_handler(lv_event_t * e)37 static void dd_event_handler(lv_event_t * e)
38 {
39 lv_event_code_t code = lv_event_get_code(e);
40 lv_obj_t * dd = lv_event_get_target(e);
41 lv_obj_t * fe_quick_access_obj = lv_event_get_user_data(e);
42
43 if(code == LV_EVENT_VALUE_CHANGED) {
44 char buf[32];
45 lv_dropdown_get_selected_str(dd, buf, sizeof(buf));
46 if(strcmp(buf, "NONE") == 0) {
47 lv_file_explorer_set_sort(fe_quick_access_obj, LV_EXPLORER_SORT_NONE);
48 }
49 else if(strcmp(buf, "KIND") == 0) {
50 lv_file_explorer_set_sort(fe_quick_access_obj, LV_EXPLORER_SORT_KIND);
51 }
52 }
53 }
54 #endif
55
lv_example_file_explorer_2(void)56 void lv_example_file_explorer_2(void)
57 {
58 lv_obj_t * file_explorer = lv_file_explorer_create(lv_screen_active());
59
60 #if LV_USE_FS_WIN32
61 /* Note to Windows users: the initial "C:" on these paths corresponds to
62 * the value of `LV_FS_WIN32_LETTER` in `lv_conf.h`, and should not be
63 * confused with the Windows/DOS drive letter. It is an identifier that
64 * is used to enable LVGL to look up the appropriate driver from a list of
65 * registered file-system drivers. `lv_fs_win32_init()` happens to use the
66 * identifier letter 'C' so "C:" is the driver-identifier-prefix used here.
67 * The "C:" following that is indeed the Windows/DOS drive letter and is
68 * part of the actual path that gets passed to the OS-level functions.
69 *
70 * See https://docs.lvgl.io/master/details/main-components/fs.html for details.
71 * File Explorer uses `lv_fs` internally, thus the required prefix in path strings.
72 */
73 lv_file_explorer_open_dir(file_explorer, "C:C:/");
74 #if LV_FILE_EXPLORER_QUICK_ACCESS
75 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "C:C:/Users/Public/Desktop");
76 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "C:C:/Users/Public/Videos");
77 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "C:C:/Users/Public/Pictures");
78 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "C:C:/Users/Public/Music");
79 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "C:C:/Users/Public/Documents");
80 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "C:C:/");
81 #endif
82
83 #else
84 /* linux */
85 lv_file_explorer_open_dir(file_explorer, "A:/");
86 #if LV_FILE_EXPLORER_QUICK_ACCESS
87 char * envvar = "HOME";
88 char home_dir[LV_FS_MAX_PATH_LENGTH];
89 strcpy(home_dir, "A:");
90 /* get the user's home directory from the HOME environment variable*/
91 strcat(home_dir, getenv(envvar));
92 LV_LOG_USER("home_dir: %s\n", home_dir);
93 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, home_dir);
94 char video_dir[LV_FS_MAX_PATH_LENGTH];
95 strcpy(video_dir, home_dir);
96 strcat(video_dir, "/Videos");
97 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, video_dir);
98 char picture_dir[LV_FS_MAX_PATH_LENGTH];
99 strcpy(picture_dir, home_dir);
100 strcat(picture_dir, "/Pictures");
101 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, picture_dir);
102 char music_dir[LV_FS_MAX_PATH_LENGTH];
103 strcpy(music_dir, home_dir);
104 strcat(music_dir, "/Music");
105 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, music_dir);
106 char document_dir[LV_FS_MAX_PATH_LENGTH];
107 strcpy(document_dir, home_dir);
108 strcat(document_dir, "/Documents");
109 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, document_dir);
110
111 lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "A:/");
112 #endif
113 #endif
114
115 lv_obj_add_event_cb(file_explorer, file_explorer_event_handler, LV_EVENT_ALL, NULL);
116
117 #if LV_FILE_EXPLORER_QUICK_ACCESS
118 /*Quick access status control button*/
119 lv_obj_t * fe_quick_access_obj = lv_file_explorer_get_quick_access_area(file_explorer);
120 lv_obj_t * fe_header_obj = lv_file_explorer_get_header(file_explorer);
121 lv_obj_t * btn = lv_button_create(fe_header_obj);
122 lv_obj_set_style_radius(btn, 2, 0);
123 lv_obj_set_style_pad_all(btn, 4, 0);
124 lv_obj_align(btn, LV_ALIGN_LEFT_MID, 0, 0);
125 lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
126
127 lv_obj_t * label = lv_label_create(btn);
128 lv_label_set_text(label, LV_SYMBOL_LIST);
129 lv_obj_center(label);
130
131 lv_obj_add_event_cb(btn, btn_event_handler, LV_EVENT_VALUE_CHANGED, fe_quick_access_obj);
132
133 /*Sort control*/
134 static const char * opts = "NONE\n"
135 "KIND";
136
137 lv_obj_t * dd = lv_dropdown_create(fe_header_obj);
138 lv_obj_set_style_radius(dd, 4, 0);
139 lv_obj_set_style_pad_all(dd, 0, 0);
140 lv_obj_set_size(dd, LV_PCT(30), LV_SIZE_CONTENT);
141 lv_dropdown_set_options_static(dd, opts);
142 lv_obj_align(dd, LV_ALIGN_RIGHT_MID, 0, 0);
143
144 lv_obj_add_event_cb(dd, dd_event_handler, LV_EVENT_VALUE_CHANGED, file_explorer);
145 #endif
146 }
147
148 #endif
149