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_checkbox.c (8509B)

      1 /**
      2  * @file lv_cb.c
      3  *
      4  */
      5 
      6 /*********************
      7  *      INCLUDES
      8  *********************/
      9 #include "lv_checkbox.h"
     10 #if LV_USE_CHECKBOX != 0
     11 
     12 #include "../misc/lv_assert.h"
     13 #include "../misc/lv_txt_ap.h"
     14 #include "../core/lv_group.h"
     15 #include "../draw/lv_draw.h"
     16 
     17 /*********************
     18  *      DEFINES
     19  *********************/
     20 #define MY_CLASS &lv_checkbox_class
     21 
     22 /**********************
     23  *      TYPEDEFS
     24  **********************/
     25 
     26 /**********************
     27  *  STATIC PROTOTYPES
     28  **********************/
     29 static void lv_checkbox_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
     30 static void lv_checkbox_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
     31 static void lv_checkbox_event(const lv_obj_class_t * class_p, lv_event_t * e);
     32 static void lv_checkbox_draw(lv_event_t * e);
     33 
     34 /**********************
     35  *  STATIC VARIABLES
     36  **********************/
     37 const lv_obj_class_t lv_checkbox_class = {
     38     .constructor_cb = lv_checkbox_constructor,
     39     .destructor_cb = lv_checkbox_destructor,
     40     .event_cb = lv_checkbox_event,
     41     .width_def = LV_SIZE_CONTENT,
     42     .height_def = LV_SIZE_CONTENT,
     43     .group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
     44     .instance_size = sizeof(lv_checkbox_t),
     45     .base_class = &lv_obj_class
     46 };
     47 
     48 /**********************
     49  *      MACROS
     50  **********************/
     51 
     52 /**********************
     53  *   GLOBAL FUNCTIONS
     54  **********************/
     55 
     56 lv_obj_t * lv_checkbox_create(lv_obj_t * parent)
     57 {
     58     LV_LOG_INFO("begin");
     59     lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
     60     lv_obj_class_init_obj(obj);
     61     return obj;
     62 }
     63 
     64 /*=====================
     65  * Setter functions
     66  *====================*/
     67 
     68 void lv_checkbox_set_text(lv_obj_t * obj, const char * txt)
     69 {
     70     lv_checkbox_t * cb = (lv_checkbox_t *)obj;
     71 #if LV_USE_ARABIC_PERSIAN_CHARS
     72     size_t len = _lv_txt_ap_calc_bytes_cnt(txt);
     73 #else
     74     size_t len = strlen(txt);
     75 #endif
     76 
     77     if(!cb->static_txt) cb->txt = lv_mem_realloc(cb->txt, len + 1);
     78     else  cb->txt = lv_mem_alloc(len + 1);
     79 #if LV_USE_ARABIC_PERSIAN_CHARS
     80     _lv_txt_ap_proc(txt, cb->txt);
     81 #else
     82     strcpy(cb->txt, txt);
     83 #endif
     84 
     85     cb->static_txt = 0;
     86 
     87     lv_obj_refresh_self_size(obj);
     88     lv_obj_invalidate(obj);
     89 }
     90 
     91 void lv_checkbox_set_text_static(lv_obj_t * obj, const char * txt)
     92 {
     93     lv_checkbox_t * cb = (lv_checkbox_t *)obj;
     94 
     95     if(!cb->static_txt) lv_mem_free(cb->txt);
     96 
     97     cb->txt = (char *)txt;
     98     cb->static_txt = 1;
     99 
    100     lv_obj_refresh_self_size(obj);
    101     lv_obj_invalidate(obj);
    102 }
    103 
    104 /*=====================
    105  * Getter functions
    106  *====================*/
    107 
    108 const char * lv_checkbox_get_text(const lv_obj_t * obj)
    109 {
    110     lv_checkbox_t * cb = (lv_checkbox_t *)obj;
    111     return cb->txt;
    112 }
    113 
    114 /**********************
    115  *   STATIC FUNCTIONS
    116  **********************/
    117 
    118 static void lv_checkbox_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
    119 {
    120     LV_UNUSED(class_p);
    121     LV_TRACE_OBJ_CREATE("begin");
    122 
    123     lv_checkbox_t * cb = (lv_checkbox_t *)obj;
    124 
    125     cb->txt = "Check box";
    126     cb->static_txt = 1;
    127     lv_obj_add_flag(obj, LV_OBJ_FLAG_CLICKABLE);
    128     lv_obj_add_flag(obj, LV_OBJ_FLAG_CHECKABLE);
    129     lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
    130 
    131     LV_TRACE_OBJ_CREATE("finished");
    132 }
    133 
    134 static void lv_checkbox_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
    135 {
    136     LV_UNUSED(class_p);
    137     LV_TRACE_OBJ_CREATE("begin");
    138 
    139     lv_checkbox_t * cb = (lv_checkbox_t *)obj;
    140     if(!cb->static_txt) {
    141         lv_mem_free(cb->txt);
    142         cb->txt = NULL;
    143     }
    144     LV_TRACE_OBJ_CREATE("finished");
    145 }
    146 
    147 static void lv_checkbox_event(const lv_obj_class_t * class_p, lv_event_t * e)
    148 {
    149     LV_UNUSED(class_p);
    150 
    151     lv_res_t res;
    152     /*Call the ancestor's event handler*/
    153     res = lv_obj_event_base(MY_CLASS, e);
    154     if(res != LV_RES_OK) return;
    155 
    156     lv_event_code_t code = lv_event_get_code(e);
    157     lv_obj_t * obj = lv_event_get_target(e);
    158 
    159     if(code == LV_EVENT_GET_SELF_SIZE) {
    160         lv_point_t * p = lv_event_get_param(e);
    161         lv_checkbox_t * cb = (lv_checkbox_t *)obj;
    162 
    163         const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
    164         lv_coord_t font_h = lv_font_get_line_height(font);
    165         lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
    166         lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_MAIN);
    167 
    168         lv_point_t txt_size;
    169         lv_txt_get_size(&txt_size, cb->txt, font, letter_space, line_space, LV_COORD_MAX, LV_TEXT_FLAG_NONE);
    170 
    171         lv_coord_t bg_colp = lv_obj_get_style_pad_column(obj, LV_PART_MAIN);
    172         lv_coord_t marker_leftp = lv_obj_get_style_pad_left(obj, LV_PART_INDICATOR);
    173         lv_coord_t marker_rightp = lv_obj_get_style_pad_right(obj, LV_PART_INDICATOR);
    174         lv_coord_t marker_topp = lv_obj_get_style_pad_top(obj, LV_PART_INDICATOR);
    175         lv_coord_t marker_bottomp = lv_obj_get_style_pad_bottom(obj, LV_PART_INDICATOR);
    176         lv_point_t marker_size;
    177         marker_size.x = font_h + marker_leftp + marker_rightp;
    178         marker_size.y = font_h + marker_topp + marker_bottomp;
    179 
    180         p->x = marker_size.x + txt_size.x + bg_colp;
    181         p->y = LV_MAX(marker_size.y, txt_size.y);
    182     }
    183     else if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
    184         lv_coord_t * s = lv_event_get_param(e);
    185         lv_coord_t m = lv_obj_calculate_ext_draw_size(obj, LV_PART_INDICATOR);
    186         *s = LV_MAX(*s, m);
    187     }
    188     else if(code == LV_EVENT_DRAW_MAIN) {
    189         lv_checkbox_draw(e);
    190     }
    191 }
    192 
    193 static void lv_checkbox_draw(lv_event_t * e)
    194 {
    195     lv_obj_t * obj = lv_event_get_target(e);
    196     lv_checkbox_t * cb = (lv_checkbox_t *)obj;
    197 
    198     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
    199     const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
    200     lv_coord_t font_h = lv_font_get_line_height(font);
    201 
    202     lv_coord_t bg_border = lv_obj_get_style_border_width(obj, LV_PART_MAIN);
    203     lv_coord_t bg_topp = lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + bg_border;
    204     lv_coord_t bg_leftp = lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + bg_border;
    205     lv_coord_t bg_colp = lv_obj_get_style_pad_column(obj, LV_PART_MAIN);
    206 
    207     lv_coord_t marker_leftp = lv_obj_get_style_pad_left(obj, LV_PART_INDICATOR);
    208     lv_coord_t marker_rightp = lv_obj_get_style_pad_right(obj, LV_PART_INDICATOR);
    209     lv_coord_t marker_topp = lv_obj_get_style_pad_top(obj, LV_PART_INDICATOR);
    210     lv_coord_t marker_bottomp = lv_obj_get_style_pad_bottom(obj, LV_PART_INDICATOR);
    211 
    212     lv_coord_t transf_w = lv_obj_get_style_transform_width(obj, LV_PART_INDICATOR);
    213     lv_coord_t transf_h = lv_obj_get_style_transform_height(obj, LV_PART_INDICATOR);
    214 
    215     lv_draw_rect_dsc_t indic_dsc;
    216     lv_draw_rect_dsc_init(&indic_dsc);
    217     lv_obj_init_draw_rect_dsc(obj, LV_PART_INDICATOR, &indic_dsc);
    218     lv_area_t marker_area;
    219     marker_area.x1 = obj->coords.x1 + bg_leftp;
    220     marker_area.x2 = marker_area.x1 + font_h + marker_leftp + marker_rightp - 1;
    221     marker_area.y1 = obj->coords.y1 + bg_topp;
    222     marker_area.y2 = marker_area.y1 + font_h + marker_topp + marker_bottomp - 1;
    223 
    224     lv_area_t marker_area_transf;
    225     lv_area_copy(&marker_area_transf, &marker_area);
    226     marker_area_transf.x1 -= transf_w;
    227     marker_area_transf.x2 += transf_w;
    228     marker_area_transf.y1 -= transf_h;
    229     marker_area_transf.y2 += transf_h;
    230 
    231     lv_obj_draw_part_dsc_t part_draw_dsc;
    232     lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
    233     part_draw_dsc.rect_dsc = &indic_dsc;
    234     part_draw_dsc.class_p = MY_CLASS;
    235     part_draw_dsc.type = LV_CHECKBOX_DRAW_PART_BOX;
    236     part_draw_dsc.draw_area = &marker_area_transf;
    237     part_draw_dsc.part = LV_PART_INDICATOR;
    238 
    239     lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
    240     lv_draw_rect(draw_ctx, &indic_dsc, &marker_area_transf);
    241     lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
    242 
    243     lv_coord_t line_space = lv_obj_get_style_text_line_space(obj, LV_PART_MAIN);
    244     lv_coord_t letter_space = lv_obj_get_style_text_letter_space(obj, LV_PART_MAIN);
    245 
    246     lv_point_t txt_size;
    247     lv_txt_get_size(&txt_size, cb->txt, font, letter_space, line_space, LV_COORD_MAX, LV_TEXT_FLAG_NONE);
    248 
    249     lv_draw_label_dsc_t txt_dsc;
    250     lv_draw_label_dsc_init(&txt_dsc);
    251     lv_obj_init_draw_label_dsc(obj, LV_PART_MAIN, &txt_dsc);
    252 
    253     lv_coord_t y_ofs = (lv_area_get_height(&marker_area) - font_h) / 2;
    254     lv_area_t txt_area;
    255     txt_area.x1 = marker_area.x2 + bg_colp;
    256     txt_area.x2 = txt_area.x1 + txt_size.x;
    257     txt_area.y1 = obj->coords.y1 + bg_topp + y_ofs;
    258     txt_area.y2 = txt_area.y1 + txt_size.y;
    259 
    260     lv_draw_label(draw_ctx, &txt_dsc, &txt_area, cb->txt, NULL);
    261 }
    262 #endif