1 
2 #include "../../lv_examples.h"
3 
4 #if LV_USE_TABLE && 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 
exch_table_item(lv_obj_t * tb,int16_t i,int16_t j)9 static void exch_table_item(lv_obj_t * tb, int16_t i, int16_t j)
10 {
11     const char * tmp;
12     tmp = lv_table_get_cell_value(tb, i, 0);
13     lv_table_set_cell_value(tb, 0, 2, tmp);
14     lv_table_set_cell_value(tb, i, 0, lv_table_get_cell_value(tb, j, 0));
15     lv_table_set_cell_value(tb, j, 0, lv_table_get_cell_value(tb, 0, 2));
16 
17     tmp = lv_table_get_cell_value(tb, i, 1);
18     lv_table_set_cell_value(tb, 0, 2, tmp);
19     lv_table_set_cell_value(tb, i, 1, lv_table_get_cell_value(tb, j, 1));
20     lv_table_set_cell_value(tb, j, 1, lv_table_get_cell_value(tb, 0, 2));
21 }
22 
23 /*Quick sort 3 way*/
sort_by_file_kind(lv_obj_t * tb,int16_t lo,int16_t hi)24 static void sort_by_file_kind(lv_obj_t * tb, int16_t lo, int16_t hi)
25 {
26     if(lo >= hi) return;
27 
28     int16_t lt = lo;
29     int16_t i = lo + 1;
30     int16_t gt = hi;
31     const char * v = lv_table_get_cell_value(tb, lo, 1);
32     while(i <= gt) {
33         if(strcmp(lv_table_get_cell_value(tb, i, 1), v) < 0)
34             exch_table_item(tb, lt++, i++);
35         else if(strcmp(lv_table_get_cell_value(tb, i, 1), v) > 0)
36             exch_table_item(tb, i, gt--);
37         else
38             i++;
39     }
40 
41     sort_by_file_kind(tb, lo, lt - 1);
42     sort_by_file_kind(tb, gt + 1, hi);
43 }
44 
file_explorer_event_handler(lv_event_t * e)45 static void file_explorer_event_handler(lv_event_t * e)
46 {
47     lv_event_code_t code = lv_event_get_code(e);
48     lv_obj_t * obj = lv_event_get_target(e);
49 
50     if(code == LV_EVENT_VALUE_CHANGED) {
51         const char * cur_path =  lv_file_explorer_get_current_path(obj);
52         const char * sel_fn = lv_file_explorer_get_selected_file_name(obj);
53 
54         LV_LOG_USER("%s%s", cur_path, sel_fn);
55     }
56     else if(code == LV_EVENT_READY) {
57         lv_obj_t * tb = lv_file_explorer_get_file_table(obj);
58         uint16_t sum = lv_table_get_row_count(tb);
59 
60         sort_by_file_kind(tb, 0, (sum - 1));
61     }
62 }
63 
lv_example_file_explorer_3(void)64 void lv_example_file_explorer_3(void)
65 {
66     lv_obj_t * file_explorer = lv_file_explorer_create(lv_screen_active());
67     /*Before custom sort, please set the default sorting to NONE. The default is NONE.*/
68     lv_file_explorer_set_sort(file_explorer, LV_EXPLORER_SORT_NONE);
69 
70 #if LV_USE_FS_WIN32
71     /* Note to Windows users:  the initial "C:" on these paths corresponds to
72      * the value of `LV_FS_WIN32_LETTER` in `lv_conf.h`, and should not be
73      * confused with the Windows/DOS drive letter.  It is an identifier that
74      * is used to enable LVGL to look up the appropriate driver from a list of
75      * registered file-system drivers.  `lv_fs_win32_init()` happens to use the
76      * identifier letter 'C' so "C:" is the driver-identifier-prefix used here.
77      * The "C:" following that is indeed the Windows/DOS drive letter and is
78      * part of the actual path that gets passed to the OS-level functions.
79      *
80      * See https://docs.lvgl.io/master/details/main-components/fs.html for details.
81      * File Explorer uses `lv_fs` internally, thus the required prefix in path strings.
82      */
83     lv_file_explorer_open_dir(file_explorer, "C:C:/");
84 #if LV_FILE_EXPLORER_QUICK_ACCESS
85     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "C:C:/Users/Public/Desktop");
86     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "C:C:/Users/Public/Videos");
87     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "C:C:/Users/Public/Pictures");
88     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "C:C:/Users/Public/Music");
89     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "C:C:/Users/Public/Documents");
90     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "C:C:/");
91 #endif
92 
93 #else
94     /* linux */
95     lv_file_explorer_open_dir(file_explorer, "A:/");
96 #if LV_FILE_EXPLORER_QUICK_ACCESS
97     char * envvar = "HOME";
98     char home_dir[LV_FS_MAX_PATH_LENGTH];
99     strcpy(home_dir, "A:");
100     /* get the user's home directory from the HOME environment variable*/
101     strcat(home_dir, getenv(envvar));
102     LV_LOG_USER("home_dir: %s\n", home_dir);
103     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, home_dir);
104     char video_dir[LV_FS_MAX_PATH_LENGTH];
105     strcpy(video_dir, home_dir);
106     strcat(video_dir, "/Videos");
107     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, video_dir);
108     char picture_dir[LV_FS_MAX_PATH_LENGTH];
109     strcpy(picture_dir, home_dir);
110     strcat(picture_dir, "/Pictures");
111     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, picture_dir);
112     char music_dir[LV_FS_MAX_PATH_LENGTH];
113     strcpy(music_dir, home_dir);
114     strcat(music_dir, "/Music");
115     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, music_dir);
116     char document_dir[LV_FS_MAX_PATH_LENGTH];
117     strcpy(document_dir, home_dir);
118     strcat(document_dir, "/Documents");
119     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, document_dir);
120 
121     lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "A:/");
122 #endif
123 #endif
124 
125     lv_obj_add_event_cb(file_explorer, file_explorer_event_handler, LV_EVENT_ALL, NULL);
126 }
127 
128 #endif
129