acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
lv_msgbox.c (6330B)
1 /** 2 * @file lv_msgbox.c 3 * 4 */ 5 6 /********************* 7 * INCLUDES 8 *********************/ 9 #include "lv_msgbox.h" 10 #if LV_USE_MSGBOX 11 12 #include "../../../misc/lv_assert.h" 13 14 /********************* 15 * DEFINES 16 *********************/ 17 #define LV_MSGBOX_FLAG_AUTO_PARENT LV_OBJ_FLAG_WIDGET_1 /*Mark that the parent was automatically created*/ 18 #define MY_CLASS &lv_msgbox_class 19 20 /********************** 21 * TYPEDEFS 22 **********************/ 23 24 /********************** 25 * STATIC PROTOTYPES 26 **********************/ 27 static void msgbox_close_click_event_cb(lv_event_t * e); 28 29 /********************** 30 * STATIC VARIABLES 31 **********************/ 32 const lv_obj_class_t lv_msgbox_class = { 33 .base_class = &lv_obj_class, 34 .width_def = LV_DPI_DEF * 2, 35 .height_def = LV_SIZE_CONTENT, 36 .instance_size = sizeof(lv_msgbox_t) 37 }; 38 39 const lv_obj_class_t lv_msgbox_content_class = { 40 .base_class = &lv_obj_class, 41 .width_def = LV_PCT(100), 42 .height_def = LV_SIZE_CONTENT, 43 .instance_size = sizeof(lv_obj_t) 44 }; 45 46 const lv_obj_class_t lv_msgbox_backdrop_class = { 47 .base_class = &lv_obj_class, 48 .width_def = LV_PCT(100), 49 .height_def = LV_PCT(100), 50 .instance_size = sizeof(lv_obj_t) 51 }; 52 53 /********************** 54 * MACROS 55 **********************/ 56 57 /********************** 58 * GLOBAL FUNCTIONS 59 **********************/ 60 61 lv_obj_t * lv_msgbox_create(lv_obj_t * parent, const char * title, const char * txt, const char * btn_txts[], 62 bool add_close_btn) 63 { 64 LV_LOG_INFO("begin"); 65 bool auto_parent = false; 66 if(parent == NULL) { 67 auto_parent = true; 68 parent = lv_obj_class_create_obj(&lv_msgbox_backdrop_class, lv_layer_top()); 69 LV_ASSERT_MALLOC(parent); 70 lv_obj_class_init_obj(parent); 71 lv_obj_clear_flag(parent, LV_OBJ_FLAG_IGNORE_LAYOUT); 72 lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100)); 73 } 74 75 lv_obj_t * obj = lv_obj_class_create_obj(&lv_msgbox_class, parent); 76 LV_ASSERT_MALLOC(obj); 77 if(obj == NULL) return NULL; 78 lv_obj_class_init_obj(obj); 79 lv_msgbox_t * mbox = (lv_msgbox_t *)obj; 80 81 if(auto_parent) lv_obj_add_flag(obj, LV_MSGBOX_FLAG_AUTO_PARENT); 82 83 lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW_WRAP); 84 85 bool has_title = title && strlen(title) > 0; 86 87 /*When a close button is required, we need the empty label as spacer to push the button to the right*/ 88 if(add_close_btn || has_title) { 89 mbox->title = lv_label_create(obj); 90 lv_label_set_text(mbox->title, has_title ? title : ""); 91 lv_label_set_long_mode(mbox->title, LV_LABEL_LONG_SCROLL_CIRCULAR); 92 if(add_close_btn) lv_obj_set_flex_grow(mbox->title, 1); 93 else lv_obj_set_width(mbox->title, LV_PCT(100)); 94 } 95 96 if(add_close_btn) { 97 mbox->close_btn = lv_btn_create(obj); 98 lv_obj_set_ext_click_area(mbox->close_btn, LV_DPX(10)); 99 lv_obj_add_event_cb(mbox->close_btn, msgbox_close_click_event_cb, LV_EVENT_CLICKED, NULL); 100 lv_obj_t * label = lv_label_create(mbox->close_btn); 101 lv_label_set_text(label, LV_SYMBOL_CLOSE); 102 const lv_font_t * font = lv_obj_get_style_text_font(mbox->close_btn, LV_PART_MAIN); 103 lv_coord_t close_btn_size = lv_font_get_line_height(font) + LV_DPX(10); 104 lv_obj_set_size(mbox->close_btn, close_btn_size, close_btn_size); 105 lv_obj_align(label, LV_ALIGN_CENTER, 0, 0); 106 } 107 108 mbox->content = lv_obj_class_create_obj(&lv_msgbox_content_class, obj); 109 110 bool has_txt = txt && strlen(txt) > 0; 111 if(has_txt) { 112 mbox->text = lv_label_create(mbox->content); 113 lv_label_set_text(mbox->text, txt); 114 lv_label_set_long_mode(mbox->text, LV_LABEL_LONG_WRAP); 115 lv_obj_set_width(mbox->text, lv_pct(100)); 116 } 117 118 if(btn_txts) { 119 mbox->btns = lv_btnmatrix_create(obj); 120 lv_btnmatrix_set_map(mbox->btns, btn_txts); 121 lv_btnmatrix_set_btn_ctrl_all(mbox->btns, LV_BTNMATRIX_CTRL_CLICK_TRIG | LV_BTNMATRIX_CTRL_NO_REPEAT); 122 123 uint32_t btn_cnt = 0; 124 while(btn_txts[btn_cnt] && btn_txts[btn_cnt][0] != '\0') { 125 btn_cnt++; 126 } 127 128 const lv_font_t * font = lv_obj_get_style_text_font(mbox->btns, LV_PART_ITEMS); 129 lv_coord_t btn_h = lv_font_get_line_height(font) + LV_DPI_DEF / 10; 130 lv_obj_set_size(mbox->btns, btn_cnt * (2 * LV_DPI_DEF / 3), btn_h); 131 lv_obj_set_style_max_width(mbox->btns, lv_pct(100), 0); 132 lv_obj_add_flag(mbox->btns, LV_OBJ_FLAG_EVENT_BUBBLE); /*To see the event directly on the message box*/ 133 } 134 135 return obj; 136 } 137 138 139 lv_obj_t * lv_msgbox_get_title(lv_obj_t * obj) 140 { 141 LV_ASSERT_OBJ(obj, MY_CLASS); 142 lv_msgbox_t * mbox = (lv_msgbox_t *)obj; 143 return mbox->title; 144 } 145 146 lv_obj_t * lv_msgbox_get_close_btn(lv_obj_t * obj) 147 { 148 LV_ASSERT_OBJ(obj, MY_CLASS); 149 lv_msgbox_t * mbox = (lv_msgbox_t *)obj; 150 return mbox->close_btn; 151 } 152 153 lv_obj_t * lv_msgbox_get_text(lv_obj_t * obj) 154 { 155 LV_ASSERT_OBJ(obj, MY_CLASS); 156 lv_msgbox_t * mbox = (lv_msgbox_t *)obj; 157 return mbox->text; 158 } 159 160 lv_obj_t * lv_msgbox_get_content(lv_obj_t * obj) 161 { 162 LV_ASSERT_OBJ(obj, MY_CLASS); 163 lv_msgbox_t * mbox = (lv_msgbox_t *)obj; 164 return mbox->content; 165 } 166 167 lv_obj_t * lv_msgbox_get_btns(lv_obj_t * obj) 168 { 169 LV_ASSERT_OBJ(obj, MY_CLASS); 170 lv_msgbox_t * mbox = (lv_msgbox_t *)obj; 171 return mbox->btns; 172 } 173 174 uint16_t lv_msgbox_get_active_btn(lv_obj_t * mbox) 175 { 176 lv_obj_t * btnm = lv_msgbox_get_btns(mbox); 177 return lv_btnmatrix_get_selected_btn(btnm); 178 } 179 180 const char * lv_msgbox_get_active_btn_text(lv_obj_t * mbox) 181 { 182 lv_obj_t * btnm = lv_msgbox_get_btns(mbox); 183 return lv_btnmatrix_get_btn_text(btnm, lv_btnmatrix_get_selected_btn(btnm)); 184 } 185 186 void lv_msgbox_close(lv_obj_t * mbox) 187 { 188 if(lv_obj_has_flag(mbox, LV_MSGBOX_FLAG_AUTO_PARENT)) lv_obj_del(lv_obj_get_parent(mbox)); 189 else lv_obj_del(mbox); 190 } 191 192 void lv_msgbox_close_async(lv_obj_t * dialog) 193 { 194 if(lv_obj_has_flag(dialog, LV_MSGBOX_FLAG_AUTO_PARENT)) lv_obj_del_async(lv_obj_get_parent(dialog)); 195 else lv_obj_del_async(dialog); 196 } 197 198 /********************** 199 * STATIC FUNCTIONS 200 **********************/ 201 202 static void msgbox_close_click_event_cb(lv_event_t * e) 203 { 204 lv_obj_t * btn = lv_event_get_target(e); 205 lv_obj_t * mbox = lv_obj_get_parent(btn); 206 lv_msgbox_close(mbox); 207 } 208 209 #endif /*LV_USE_MSGBOX*/