From d04c2463201fb00c96a28e7d4915ad5cf0275db1 Mon Sep 17 00:00:00 2001 From: Gabor Date: Fri, 30 Sep 2016 15:29:00 +0200 Subject: [PATCH] Text area beginned --- lv_conf_temp.h | 2 + lv_objx/lv_label.c | 50 +++++++++++ lv_objx/lv_label.h | 1 + lv_objx/lv_ta.c | 210 +++++++++++++++++++++++++++++++++++++++++++++ lv_objx/lv_ta.h | 72 ++++++++++++++++ lvgl.h | 1 + 6 files changed, 336 insertions(+) create mode 100644 lv_objx/lv_ta.c create mode 100644 lv_objx/lv_ta.h diff --git a/lv_conf_temp.h b/lv_conf_temp.h index 0d2258d0e..61c32ed1f 100644 --- a/lv_conf_temp.h +++ b/lv_conf_temp.h @@ -95,6 +95,8 @@ #define USE_LV_BTNM 1 +#define USE_LV_TA 1 + /*================== * LV APP SETTINGS * =================*/ diff --git a/lv_objx/lv_label.c b/lv_objx/lv_label.c index 569bddc8f..1d2db8623 100644 --- a/lv_objx/lv_label.c +++ b/lv_objx/lv_label.c @@ -212,6 +212,7 @@ void lv_label_set_text(lv_obj_t * obj_dp, const char * text) /** * Set the fix width attribute + * If enabled the text will be automatically broken to fit to the size * @param obj_dp pointer to a label object * @param fixw true: enable fix width for the label */ @@ -248,6 +249,55 @@ bool lv_label_get_fixw(lv_obj_t * obj_dp) return ext_p->fixw == 0 ? false: true; } +/** + * Get the relative x and y coordinates of a letter + * @param obj_dp pointer to a label object + * @param index index of the letter (0 ... text length) + * @param pos_p store the result here + */ +void lv_label_get_letter_pos(lv_obj_t * obj_dp, uint16_t index, point_t * pos_p) +{ + const char * text = lv_label_get_text(obj_dp); + lv_label_ext_t * ext_p = lv_obj_get_ext(obj_dp); + uint32_t line_start = 0; + uint32_t new_line_start = 0; + cord_t max_length = lv_obj_get_width(obj_dp); + lv_labels_t * labels_p = lv_obj_get_style(obj_dp); + const font_t * font_p = font_get(labels_p->font); + uint8_t letter_height = font_get_height(font_p); + cord_t y = 0; + + /*If the fix width is not enabled the set the max length to very big */ + if(ext_p->fixw == 0) { + max_length = LV_CORD_MAX; + } + + /*Search the line of the index letter */; + while (text[line_start] != '\0') + { + new_line_start += txt_get_next_line(&text[line_start], font_p, labels_p->letter_space, max_length); + if(index < new_line_start) break; /*Lines of index letter begins at 'line_start'*/ + + y += letter_height + labels_p->line_space; + line_start = new_line_start; + } + + /*Calculate the x coordinate*/ + cord_t x = 0; + /*TODO handle 'mid'*/ + //if(labels_p->mid == 0) + { + uint32_t i; + for(i = line_start; i < index; i++) { + x += font_get_width(font_p, text[i]) + labels_p->line_space; + } + } + + pos_p->x = x + obj_dp->cords.x1; + pos_p->y = y + obj_dp->cords.y1; + +} + /** * Return with a pointer to a built-in style and/or copy it to a variable * @param style a style name from lv_labels_builtin_t enum diff --git a/lv_objx/lv_label.h b/lv_objx/lv_label.h index 1a76e4039..d8d497d3c 100644 --- a/lv_objx/lv_label.h +++ b/lv_objx/lv_label.h @@ -59,6 +59,7 @@ void lv_label_set_text(lv_obj_t* obj_dp, const char * text); void lv_label_set_fixw(lv_obj_t * obj_dp, bool fixw); const char * lv_label_get_text(lv_obj_t* obj_dp); bool lv_label_get_fixw(lv_obj_t * obj_dp); +void lv_label_get_letter_pos(lv_obj_t * obj_dp, uint16_t index, point_t * pos_p); lv_labels_t * lv_labels_get(lv_labels_builtin_t style, lv_labels_t * copy_p); diff --git a/lv_objx/lv_ta.c b/lv_objx/lv_ta.c new file mode 100644 index 000000000..88ee6b1ee --- /dev/null +++ b/lv_objx/lv_ta.c @@ -0,0 +1,210 @@ +/** + * @file lv_ta.c + * + */ + + +/********************* + * INCLUDES + *********************/ +#include "lv_conf.h" +#if USE_LV_TA != 0 + +#include "lv_ta.h" + +/********************* + * DEFINES + *********************/ +#define LV_TA_MAX_LENGTH 512 + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ +static bool lv_ta_design(lv_obj_t* obj_dp, const area_t * mask_p, lv_design_mode_t mode); + +/********************** + * STATIC VARIABLES + **********************/ +static lv_tas_t lv_tas_def = +{ /*Create a default style*/ }; +lv_design_f_t ancestor_design_f; + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +/*----------------- + * Create function + *-----------------*/ + +/** + * Create a text area objects + * @param par_dp pointer to an object, it will be the parent of the new text area + * @param copy_dp pointer to a text area object, if not NULL then the new object will be copied from it + * @return pointer to the created text area + */ +lv_obj_t* lv_ta_create(lv_obj_t* par_dp, lv_obj_t * copy_dp) +{ + /*Create the ancestor object*/ + lv_obj_t* new_obj_dp = lv_page_create(par_dp, copy_dp); + dm_assert(new_obj_dp); + + /*Allocate the object type specific extended data*/ + lv_ta_ext_t * ext_dp = lv_obj_alloc_ext(new_obj_dp, sizeof(lv_ta_ext_t)); + dm_assert(ext_dp); + + if(ancestor_design_f == NULL) { + ancestor_design_f = lv_obj_get_design_f(new_obj_dp); + } + + lv_obj_set_signal_f(new_obj_dp, lv_ta_signal); + lv_obj_set_design_f(new_obj_dp, lv_ta_design); + + /*Init the new text area object*/ + if(copy_dp == NULL) { + ext_dp->label_dp = lv_label_create(new_obj_dp, NULL); + lv_label_set_text(ext_dp->label_dp, "Abc def xyz\n\n\n\n\n\n\n\n\n\n\n\nghi\n\n\n\n\n\n\n\n\n\n123\n\n\n\n\n\n\n\n\n\n456\n\n\n\n\n\n789"); + lv_obj_set_style(ext_dp->label_dp, lv_labels_get(LV_LABELS_TXT, NULL)); + } + /*Copy an existing object*/ + else { + + } + + return new_obj_dp; +} + +/** + * Signal function of the text area + * @param obj_dp pointer to a text area object + * @param sign a signal type from lv_signal_t enum + * @param param pointer to a signal specific variable + * @return true: the object is still valid (not deleted), false: the object become invalid + */ +bool lv_ta_signal(lv_obj_t* obj_dp, lv_signal_t sign, void * param) +{ + bool valid; + + /* Include the ancient signal function */ + valid = lv_page_signal(obj_dp, sign, param); + + /* The object can be deleted so check its validity and then + * make the object specific signal handling */ + if(valid != false) { + switch(sign) { + case LV_SIGNAL_CLEANUP: + /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/ + break; + default: + break; + } + } + + return valid; +} + +/*===================== + * Setter functions + *====================*/ + +void lv_ta_set_cursor_pos(lv_obj_t * obj_dp, uint16_t pos) +{ + + lv_ta_ext_t * ta_dp = lv_obj_get_ext(obj_dp); + ta_dp->cursor_pos = pos; + + lv_obj_inv(obj_dp); +} + +/*===================== + * Getter functions + *====================*/ + +/** + * Return with a pointer to a built-in style and/or copy it to a variable + * @param style a style name from lv_tas_builtin_t enum + * @param copy_p copy the style to this variable. (NULL if unused) + * @return pointer to an lv_tas_t style + */ +lv_tas_t * lv_tas_get(lv_tas_builtin_t style, lv_tas_t * copy_p) +{ + lv_tas_t *style_p; + + switch(style) { + case LV_TAS_DEF: + style_p = &lv_tas_def; + break; + default: + style_p = &lv_tas_def; + } + + if(copy_p != NULL) { + if(style_p != NULL) memcpy(copy_p, style_p, sizeof(lv_tas_t)); + else memcpy(copy_p, &lv_tas_def, sizeof(lv_tas_t)); + } + + return style_p; +} + +uint16_t lv_ta_get_cursor_pos(lv_obj_t * obj_dp) +{ + lv_ta_ext_t * ta_dp = lv_obj_get_ext(obj_dp); + return ta_dp->cursor_pos; +} +/********************** + * STATIC FUNCTIONS + **********************/ + + +/** + * Handle the drawing related tasks of the text areas + * @param obj_dp pointer to an object + * @param mask the object will be drawn only in this area + * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area + * (return 'true' if yes) + * LV_DESIGN_DRAW: draw the object (always return 'true') + * @param return true/false, depends on 'mode' + */ +static bool lv_ta_design(lv_obj_t* obj_dp, const area_t * mask_p, lv_design_mode_t mode) +{ + if(mode == LV_DESIGN_COVER_CHK) { + /*Return false if the object is not covers the mask_p area*/ + return ancestor_design_f(obj_dp, mask_p, mode); + } + + /*Draw the object*/ + ancestor_design_f(obj_dp, mask_p, mode); + + lv_ta_ext_t * ta_dp = lv_obj_get_ext(obj_dp); + uint16_t cur_pos = lv_ta_get_cursor_pos(obj_dp); + point_t letter_pos; + lv_label_get_letter_pos(ta_dp->label_dp, cur_pos, &letter_pos); + + area_t cur_area; + lv_labels_t * label_p = lv_obj_get_style(ta_dp->label_dp); + cur_area.x1 = letter_pos.x; + cur_area.y1 = letter_pos.y; + cur_area.x2 = letter_pos.x + 4; + cur_area.y2 = letter_pos.y + font_get_height(font_get(label_p->font)); + + lv_rects_t rects; + lv_rects_get(LV_RECTS_DEF, &rects); + rects.round = 0; + rects.bwidth = 0; + rects.objs.color = COLOR_RED; + rects.gcolor = COLOR_RED; + lv_draw_rect(&cur_area, mask_p, &rects, OPA_COVER); + + return true; +} + + +#endif diff --git a/lv_objx/lv_ta.h b/lv_objx/lv_ta.h new file mode 100644 index 000000000..245c1953a --- /dev/null +++ b/lv_objx/lv_ta.h @@ -0,0 +1,72 @@ +/** + * @file lv_ta.h + * + */ + +#ifndef LV_TA_H +#define LV_TA_H + +/********************* + * INCLUDES + *********************/ +#include "lv_conf.h" +#if USE_LV_TA != 0 + +#include "../lv_obj/lv_obj.h" +#include "lv_page.h" +#include "lv_label.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/*Style of text area*/ +typedef struct +{ + lv_pages_t pages; /*Style of ancestor*/ + /*New style element for this type */ + lv_labels_t labels; + uint8_t cursor_show :1; +}lv_tas_t; + +/*Built-in styles of text area*/ +typedef enum +{ + LV_TAS_DEF, +}lv_tas_builtin_t; + +/*Data of text area*/ +typedef struct +{ + lv_page_ext_t page; /*Ext. of ancestor*/ + /*New data for this type */ + lv_obj_t * label_dp; + uint16_t cursor_pos; +}lv_ta_ext_t; + +/********************** + * GLOBAL PROTOTYPES + **********************/ +lv_obj_t* lv_ta_create(lv_obj_t* par_dp, lv_obj_t * copy_dp); +bool lv_ta_signal(lv_obj_t* obj_dp, lv_signal_t sign, void * param); +lv_tas_t * lv_tas_get(lv_tas_builtin_t style, lv_tas_t * copy_p); + +void lv_ta_add_char(lv_obj_t * obj_dp, char c); +void lv_ta_set_cursor_pos(lv_obj_t * obj_dp, uint16_t pos); + + +const char * lv_ta_get_txt(lv_obj_t obj_dp); +uint16_t lv_ta_get_cursor_pos(lv_obj_t * obj_dp); + + +/********************** + * MACROS + **********************/ + +#endif + +#endif diff --git a/lvgl.h b/lvgl.h index 93fc411ac..716fa8743 100644 --- a/lvgl.h +++ b/lvgl.h @@ -22,6 +22,7 @@ #include "lv_objx/lv_pb.h" #include "lv_objx/lv_led.h" #include "lv_objx/lv_btnm.h" +#include "lv_objx/lv_ta.h" /********************* * DEFINES