1 /** 2 * @file lv_ddlist.h 3 * 4 */ 5 6 #ifndef LV_DROPDOWN_H 7 #define LV_DROPDOWN_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 /********************* 14 * INCLUDES 15 *********************/ 16 #include "../lv_conf_internal.h" 17 18 #if LV_USE_DROPDOWN != 0 19 20 /*Testing of dependencies*/ 21 #if LV_USE_PAGE == 0 22 #error "lv_ddlist: lv_page is required. Enable it in lv_conf.h (LV_USE_PAGE 1) " 23 #endif 24 25 #if LV_USE_LABEL == 0 26 #error "lv_ddlist: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1) " 27 #endif 28 29 #include "../lv_widgets/lv_page.h" 30 #include "../lv_widgets/lv_label.h" 31 32 /********************* 33 * DEFINES 34 *********************/ 35 #define LV_DROPDOWN_POS_LAST 0xFFFF 36 37 /********************** 38 * TYPEDEFS 39 **********************/ 40 41 enum { 42 LV_DROPDOWN_DIR_DOWN, 43 LV_DROPDOWN_DIR_UP, 44 LV_DROPDOWN_DIR_LEFT, 45 LV_DROPDOWN_DIR_RIGHT, 46 }; 47 48 typedef uint8_t lv_dropdown_dir_t; 49 50 /*Data of drop down list*/ 51 typedef struct { 52 /*New data for this type */ 53 lv_obj_t * page; /*The dropped down list*/ 54 const char * text; /*Text to display on the ddlist's button*/ 55 const char * symbol; /*Arrow or other icon when the drop-down list is closed*/ 56 char * options; 57 lv_style_list_t style_selected; /*Style of the selected option*/ 58 lv_style_list_t style_page; /*Style of the dropped down list*/ 59 lv_style_list_t style_scrlbar; /*Style of the scroll bar*/ 60 lv_coord_t max_height; /*Height of the ddlist when opened. (0: auto-size)*/ 61 uint16_t option_cnt; /*Number of options*/ 62 uint16_t sel_opt_id; /*Index of the currently selected option*/ 63 uint16_t sel_opt_id_orig; /*Store the original index on focus*/ 64 uint16_t pr_opt_id; /*Index of the currently pressed option*/ 65 lv_dropdown_dir_t dir : 2; 66 uint8_t show_selected : 1; 67 uint8_t static_txt : 1; 68 } lv_dropdown_ext_t; 69 70 enum { 71 LV_DROPDOWN_PART_MAIN = LV_OBJ_PART_MAIN, 72 LV_DROPDOWN_PART_LIST = _LV_OBJ_PART_REAL_LAST, 73 LV_DROPDOWN_PART_SCROLLBAR, 74 LV_DROPDOWN_PART_SELECTED, 75 }; 76 typedef uint8_t lv_dropdown_part_t; 77 78 /********************** 79 * GLOBAL PROTOTYPES 80 **********************/ 81 /** 82 * Create a drop down list objects 83 * @param par pointer to an object, it will be the parent of the new drop down list 84 * @param copy pointer to a drop down list object, if not NULL then the new object will be copied 85 * from it 86 * @return pointer to the created drop down list 87 */ 88 lv_obj_t * lv_dropdown_create(lv_obj_t * par, const lv_obj_t * copy); 89 90 /*===================== 91 * Setter functions 92 *====================*/ 93 94 /** 95 * Set text of the ddlist (Displayed on the button if `show_selected = false`) 96 * @param ddlist pointer to a drop down list object 97 * @param txt the text as a string (Only it's pointer is saved) 98 */ 99 void lv_dropdown_set_text(lv_obj_t * ddlist, const char * txt); 100 101 /** 102 * Clear any options in a drop down list. Static or dynamic. 103 * @param ddlist pointer to drop down list object 104 */ 105 void lv_dropdown_clear_options(lv_obj_t * ddlist); 106 107 /** 108 * Set the options in a drop down list from a string 109 * @param ddlist pointer to drop down list object 110 * @param options a string with '\n' separated options. E.g. "One\nTwo\nThree" 111 * The options string can be destroyed after calling this function 112 */ 113 void lv_dropdown_set_options(lv_obj_t * ddlist, const char * options); 114 115 /** 116 * Set the options in a drop down list from a string 117 * @param ddlist pointer to drop down list object 118 * @param options a static string with '\n' separated options. E.g. "One\nTwo\nThree" 119 */ 120 void lv_dropdown_set_options_static(lv_obj_t * ddlist, const char * options); 121 122 /** 123 * Add an options to a drop down list from a string. Only works for dynamic options. 124 * @param ddlist pointer to drop down list object 125 * @param option a string without '\n'. E.g. "Four" 126 * @param pos the insert position, indexed from 0, LV_DROPDOWN_POS_LAST = end of string 127 */ 128 void lv_dropdown_add_option(lv_obj_t * ddlist, const char * option, uint32_t pos); 129 130 /** 131 * Set the selected option 132 * @param ddlist pointer to drop down list object 133 * @param sel_opt id of the selected option (0 ... number of option - 1); 134 */ 135 void lv_dropdown_set_selected(lv_obj_t * ddlist, uint16_t sel_opt); 136 137 138 /** 139 * Set the direction of the a drop down list 140 * @param ddlist pointer to a drop down list object 141 * @param dir LV_DROPDOWN_DIR_LEF/RIGHT/TOP/BOTTOM 142 */ 143 void lv_dropdown_set_dir(lv_obj_t * ddlist, lv_dropdown_dir_t dir); 144 145 /** 146 * Set the maximal height for the drop down list 147 * @param ddlist pointer to a drop down list 148 * @param h the maximal height 149 */ 150 void lv_dropdown_set_max_height(lv_obj_t * ddlist, lv_coord_t h); 151 152 /** 153 * Set an arrow or other symbol to display when the drop-down list is closed. 154 * @param ddlist pointer to drop down list object 155 * @param symbol a text like `LV_SYMBOL_DOWN` or NULL to not draw icon 156 */ 157 void lv_dropdown_set_symbol(lv_obj_t * ddlist, const char * symbol); 158 159 /** 160 * Set whether the ddlist highlight the last selected option and display its text or not 161 * @param ddlist pointer to a drop down list object 162 * @param show true/false 163 */ 164 void lv_dropdown_set_show_selected(lv_obj_t * ddlist, bool show); 165 166 /*===================== 167 * Getter functions 168 *====================*/ 169 170 /** 171 * Get text of the ddlist (Displayed on the button if `show_selected = false`) 172 * @param ddlist pointer to a drop down list object 173 * @return the text string 174 */ 175 const char * lv_dropdown_get_text(lv_obj_t * ddlist); 176 177 /** 178 * Get the options of a drop down list 179 * @param ddlist pointer to drop down list object 180 * @return the options separated by '\n'-s (E.g. "Option1\nOption2\nOption3") 181 */ 182 const char * lv_dropdown_get_options(const lv_obj_t * ddlist); 183 184 /** 185 * Get the selected option 186 * @param ddlist pointer to drop down list object 187 * @return id of the selected option (0 ... number of option - 1); 188 */ 189 uint16_t lv_dropdown_get_selected(const lv_obj_t * ddlist); 190 191 /** 192 * Get the total number of options 193 * @param ddlist pointer to drop down list object 194 * @return the total number of options in the list 195 */ 196 uint16_t lv_dropdown_get_option_cnt(const lv_obj_t * ddlist); 197 198 /** 199 * Get the current selected option as a string 200 * @param ddlist pointer to ddlist object 201 * @param buf pointer to an array to store the string 202 * @param buf_size size of `buf` in bytes. 0: to ignore it. 203 */ 204 void lv_dropdown_get_selected_str(const lv_obj_t * ddlist, char * buf, uint32_t buf_size); 205 206 /** 207 * Get the fix height value. 208 * @param ddlist pointer to a drop down list object 209 * @return the height if the ddlist is opened (0: auto size) 210 */ 211 lv_coord_t lv_dropdown_get_max_height(const lv_obj_t * ddlist); 212 213 /** 214 * Get the symbol to draw when the drop-down list is closed 215 * @param ddlist pointer to drop down list object 216 * @return the symbol or NULL if not enabled 217 */ 218 const char * lv_dropdown_get_symbol(lv_obj_t * ddlist); 219 220 /** 221 * Get the symbol to draw when the drop-down list is closed 222 * @param ddlist pointer to drop down list object 223 * @return the symbol or NULL if not enabled 224 */ 225 lv_dropdown_dir_t lv_dropdown_get_dir(const lv_obj_t * ddlist); 226 227 /** 228 * Get whether the ddlist highlight the last selected option and display its text or not 229 * @param ddlist pointer to a drop down list object 230 * @return true/false 231 */ 232 bool lv_dropdown_get_show_selected(lv_obj_t * ddlist); 233 234 /*===================== 235 * Other functions 236 *====================*/ 237 238 /** 239 * Open the drop down list with or without animation 240 * @param ddlist pointer to drop down list object 241 */ 242 void lv_dropdown_open(lv_obj_t * ddlist); 243 244 /** 245 * Close (Collapse) the drop down list 246 * @param ddlist pointer to drop down list object 247 * @param anim_en LV_ANIM_ON: use animation; LV_ANOM_OFF: not use animations 248 */ 249 void lv_dropdown_close(lv_obj_t * ddlist); 250 251 /********************** 252 * MACROS 253 **********************/ 254 255 #endif /*LV_USE_DROPDOWN*/ 256 257 #ifdef __cplusplus 258 } /* extern "C" */ 259 #endif 260 261 #endif /*LV_DROPDOWN_H*/ 262