1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/lv_objx/lv_label.c

536 lines
16 KiB
C
Raw Normal View History

2016-06-08 07:25:08 +02:00
/**
* @file lv_rect.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_conf.h"
2016-06-08 07:25:08 +02:00
#if USE_LV_LABEL != 0
#include "misc/others/color.h"
#include "misc/math/math_base.h"
2016-06-08 07:25:08 +02:00
#include "lv_label.h"
#include "../lv_obj/lv_obj.h"
#include "../lv_misc/text.h"
2016-10-20 16:35:03 +02:00
#include "../lv_misc/anim.h"
2016-06-08 07:25:08 +02:00
#include "../lv_draw/lv_draw.h"
/*********************
* DEFINES
*********************/
#define LV_LABEL_DOT_NUM 3
#define LV_LABEL_DOT_END_INV 0xFFFF
2016-06-08 07:25:08 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2016-10-07 11:15:46 +02:00
static bool lv_label_design(lv_obj_t * label, const area_t * mask, lv_design_mode_t mode);
static void lv_labels_init(void);
2016-06-08 07:25:08 +02:00
/**********************
* STATIC VARIABLES
**********************/
static lv_labels_t lv_labels_def;
static lv_labels_t lv_labels_btn;
static lv_labels_t lv_labels_title;
static lv_labels_t lv_labels_txt;
2016-06-08 07:25:08 +02:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a label objects
2016-10-07 11:15:46 +02:00
* @param par pointer to an object, it will be the parent of the new label
* @param copy pointer to a button object, if not NULL then the new object will be copied from it
2016-06-08 07:25:08 +02:00
* @return pointer to the created button
*/
2016-10-07 11:15:46 +02:00
lv_obj_t * lv_label_create(lv_obj_t * par, lv_obj_t * copy)
2016-06-08 07:25:08 +02:00
{
/*Create a basic object*/
2016-10-07 11:15:46 +02:00
lv_obj_t * new_label = lv_obj_create(par, copy);
dm_assert(new_label);
2016-06-08 07:25:08 +02:00
/*Extend the basic object to a label object*/
2016-10-07 11:15:46 +02:00
lv_obj_alloc_ext(new_label, sizeof(lv_label_ext_t));
2016-06-08 07:25:08 +02:00
lv_label_ext_t * ext = lv_obj_get_ext(new_label);
ext->txt = NULL;
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
lv_obj_set_design_f(new_label, lv_label_design);
lv_obj_set_signal_f(new_label, lv_label_signal);
2016-06-08 07:25:08 +02:00
/*Init the new label*/
2016-10-07 11:15:46 +02:00
if(copy == NULL) {
ext->dot_end = LV_LABEL_DOT_END_INV;
2016-10-07 11:15:46 +02:00
lv_obj_set_opa(new_label, OPA_COVER);
lv_obj_set_click(new_label, false);
lv_obj_set_style(new_label, lv_labels_get(LV_LABELS_DEF, NULL));
lv_label_set_long_mode(new_label, LV_LABEL_LONG_EXPAND);
2016-10-07 11:15:46 +02:00
lv_label_set_text(new_label, "Text");
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
/*Copy 'copy' if not NULL*/
2016-06-08 07:25:08 +02:00
else {
lv_label_set_long_mode(new_label, lv_label_get_long_mode(copy));
2016-10-07 11:15:46 +02:00
lv_label_set_text(new_label, lv_label_get_text(copy));
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
return new_label;
2016-06-08 07:25:08 +02:00
}
/**
* Signal function of the label
2016-10-07 11:15:46 +02:00
* @param label pointer to a label object
2016-06-08 07:25:08 +02:00
* @param sign a signal type from lv_signal_t enum
* @param param pointer to a signal specific variable
*/
2016-10-07 11:15:46 +02:00
bool lv_label_signal(lv_obj_t * label, lv_signal_t sign, void * param)
2016-06-08 07:25:08 +02:00
{
bool valid;
/* Include the ancient signal function */
2016-10-07 11:15:46 +02:00
valid = lv_obj_signal(label, sign, param);
2016-06-08 07:25:08 +02:00
/* The object can be deleted so check its validity and then
* make the object specific signal handling */
if(valid != false) {
2016-10-07 11:15:46 +02:00
lv_label_ext_t * label_p = lv_obj_get_ext(label);
2016-06-08 07:25:08 +02:00
/*No signal handling*/
switch(sign) {
case LV_SIGNAL_CLEANUP:
2016-10-07 11:15:46 +02:00
dm_free(label_p->txt);
label_p->txt = NULL;
2016-06-08 07:25:08 +02:00
break;
case LV_SIGNAL_STYLE_CHG:
2016-10-07 11:15:46 +02:00
lv_label_set_text(label, NULL);
2016-06-08 07:25:08 +02:00
break;
2016-06-08 07:25:08 +02:00
default:
break;
}
}
return valid;
}
/*=====================
* Setter functions
*====================*/
/**
* Set a new text for a label
2016-10-07 11:15:46 +02:00
* @param label pointer to a label object
* @param text '\0' terminated character string. If NULL then refresh with the current text.
2016-06-08 07:25:08 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_label_set_text(lv_obj_t * label, const char * text)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_obj_inv(label);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
lv_label_ext_t * ext = lv_obj_get_ext(label);
2016-06-08 07:25:08 +02:00
/*If trying to set its own text then use NULL
* because NULL means text refresh*/
2016-10-07 11:15:46 +02:00
if(text == ext->txt) text = NULL;
/*Allocate space for the new text*/
if(text != NULL) {
uint32_t len = strlen(text) + 1;
2016-10-07 11:15:46 +02:00
if(ext->txt != NULL) {
dm_free(ext->txt);
}
2016-10-07 11:15:46 +02:00
ext->txt = dm_alloc(len);
strcpy(ext->txt, text);
}
/*Do not allocate just use the current text*/
else {
2016-10-07 11:15:46 +02:00
text = ext->txt;
2016-06-08 07:25:08 +02:00
}
/*If 'text" still NULL then nothing to do: return*/
if(text == NULL) return;
2016-06-08 07:25:08 +02:00
cord_t max_w = lv_obj_get_width(label);
lv_labels_t * style = lv_obj_get_style(label);
const font_t * font = font_get(style->font);
2016-06-08 07:25:08 +02:00
ext->dot_end = LV_LABEL_DOT_END_INV; /*Initialize the dot end index*/
/*If the width will be expanded set the max length to very big */
if(ext->long_mode == LV_LABEL_LONG_EXPAND || ext->long_mode == LV_LABEL_LONG_SCROLL) {
max_w = LV_CORD_MAX;
2016-06-08 07:25:08 +02:00
}
/*Calc. the height and longest line*/
point_t size;
txt_get_size(&size, ext->txt, font, style->letter_space, style->line_space, max_w);
2016-06-08 07:25:08 +02:00
/*Refresh the full size in expand mode*/
if(ext->long_mode == LV_LABEL_LONG_EXPAND || ext->long_mode == LV_LABEL_LONG_SCROLL) {
lv_obj_set_size(label, size.x, size.y);
/*Start scrolling if the label is greater then its parent*/
if(ext->long_mode == LV_LABEL_LONG_SCROLL) {
2016-10-20 16:35:03 +02:00
lv_obj_t * parent = lv_obj_get_parent(label);
/*Delete the potential previous scroller animations*/
anim_del(label, (anim_fp_t) lv_obj_set_x);
anim_del(label, (anim_fp_t) lv_obj_set_y);
anim_t anim;
anim.var = label;
anim.repeat = 1;
anim.playback = 1;
anim.start = font_get_width(font, ' ');
anim.act_time = 0;
anim.end_cb = NULL;
anim.path = anim_get_path(ANIM_PATH_LIN);
anim.time = 3000;
anim.playback_pause = LV_LABEL_SCROLL_PLAYBACK_PAUSE;
anim.repeat_pause = LV_LABEL_SCROLL_REPEAT_PAUSE;
bool hor_anim = false;
if(lv_obj_get_width(label) > lv_obj_get_width(parent)) {
anim.end = lv_obj_get_width(parent) - lv_obj_get_width(label) - font_get_width(font, ' ');
anim.fp = (anim_fp_t) lv_obj_set_x;
anim.time = anim_speed_to_time(LV_LABEL_SCROLL_SPEED, anim.start, anim.end);
anim_create(&anim);
hor_anim = true;
}
if(lv_obj_get_height(label) > lv_obj_get_height(parent)) {
anim.end = lv_obj_get_height(parent) - lv_obj_get_height(label) - font_get_height(font);
anim.fp = (anim_fp_t)lv_obj_set_y;
/*Different animation speed if horizontal animation is created too*/
if(hor_anim == false) {
anim.time = anim_speed_to_time(LV_LABEL_SCROLL_SPEED, anim.start, anim.end);
} else {
anim.time = anim_speed_to_time(LV_LABEL_SCROLL_SPEED_VER, anim.start, anim.end);
}
anim_create(&anim);
}
}
}
/*In break mode only the height can change*/
else if (ext->long_mode == LV_LABEL_LONG_BREAK) {
lv_obj_set_height(label, size.y);
2016-06-08 07:25:08 +02:00
}
/*Replace the last 'LV_LABEL_DOT_NUM' characters with dots
* and save these characters*/
else if(ext->long_mode == LV_LABEL_LONG_DOTS) {
point_t point;
point.x = lv_obj_get_width(label) - 1;
point.y = lv_obj_get_height(label) - 1;
uint16_t index = lv_label_get_letter_on(label, &point);
if(index < strlen(text) - 1) {
2016-10-20 16:35:03 +02:00
/* Change the last 'LV_LABEL_DOT_NUM' to dots
* (if there are at least 'LV_LABEL_DOT_NUM' characters*/
if(index > LV_LABEL_DOT_NUM) {
uint8_t i;
for(i = 0; i < LV_LABEL_DOT_NUM; i++) {
ext->dot_tmp[i] = ext->txt[index - LV_LABEL_DOT_NUM + i];
ext->txt[index - LV_LABEL_DOT_NUM + i] = '.';
}
/*The last character is '\0'*/
ext->dot_tmp[i] = ext->txt[index];
ext->txt[index] = '\0';
}
/*Else with short text change all characters to dots*/
else {
uint8_t i;
for(i = 0; i < LV_LABEL_DOT_NUM; i++) {
ext->txt[i] = '.';
}
ext->txt[i] = '\0';
}
/*Save the dot end index*/
ext->dot_end = index;
}
}
2016-06-15 09:38:20 +02:00
2016-10-07 11:15:46 +02:00
lv_obj_inv(label);
2016-06-08 07:25:08 +02:00
}
/**
* Set the behavior of the label with longer text then the object size
2016-10-07 11:15:46 +02:00
* @param label pointer to a label object
* @param long_mode the new mode from 'lv_label_long_mode' enum.
*/
void lv_label_set_long_mode(lv_obj_t * label, lv_label_long_mode_t long_mode)
{
2016-10-07 11:15:46 +02:00
lv_label_ext_t * ext = lv_obj_get_ext(label);
/*When changing from dot mode reload the characters replaced by dots*/
if(ext->long_mode == LV_LABEL_LONG_DOTS &&
ext->dot_end != LV_LABEL_DOT_END_INV) {
uint8_t i;
for(i = 0; i < LV_LABEL_DOT_NUM + 1; i++) {
ext->txt[ext->dot_end - LV_LABEL_DOT_NUM + i] = ext->dot_tmp[i];
}
}
2016-10-20 16:35:03 +02:00
/*Delete the scroller animations*/
if(ext->long_mode == LV_LABEL_LONG_SCROLL) {
anim_del(label, (anim_fp_t) lv_obj_set_x);
anim_del(label, (anim_fp_t) lv_obj_set_y);
}
ext->long_mode = long_mode;
lv_label_set_text(label, NULL);
}
2016-06-08 07:25:08 +02:00
/*=====================
* Getter functions
*====================*/
/**
* Get the text of a label
2016-10-07 11:15:46 +02:00
* @param label pointer to a label object
2016-06-08 07:25:08 +02:00
* @return the text of the label
*/
2016-10-07 11:15:46 +02:00
const char * lv_label_get_text(lv_obj_t * label)
2016-06-08 07:25:08 +02:00
{
2016-10-07 11:15:46 +02:00
lv_label_ext_t * ext = lv_obj_get_ext(label);
2016-06-08 07:25:08 +02:00
2016-10-07 11:15:46 +02:00
return ext->txt;
}
/**
* Get the fix width attribute of a label
2016-10-07 11:15:46 +02:00
* @param label pointer to a label object
* @return true: fix width is enabled
*/
lv_label_long_mode_t lv_label_get_long_mode(lv_obj_t * label)
{
2016-10-07 11:15:46 +02:00
lv_label_ext_t * ext = lv_obj_get_ext(label);
return ext->long_mode;
2016-06-08 07:25:08 +02:00
}
2016-09-30 15:29:00 +02:00
/**
* Get the relative x and y coordinates of a letter
2016-10-07 11:15:46 +02:00
* @param label pointer to a label object
2016-09-30 15:29:00 +02:00
* @param index index of the letter (0 ... text length)
2016-10-07 11:15:46 +02:00
* @param pos store the result here (E.g. index = 0 gives 0;0 coordinates)
2016-09-30 15:29:00 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_label_get_letter_pos(lv_obj_t * label, uint16_t index, point_t * pos)
2016-09-30 15:29:00 +02:00
{
2016-10-07 11:15:46 +02:00
const char * text = lv_label_get_text(label);
lv_label_ext_t * ext = lv_obj_get_ext(label);
2016-09-30 15:29:00 +02:00
uint32_t line_start = 0;
uint32_t new_line_start = 0;
2016-12-20 15:02:23 +01:00
cord_t max_w = lv_obj_get_width(label);
2016-10-07 11:15:46 +02:00
lv_labels_t * labels = lv_obj_get_style(label);
const font_t * font = font_get(labels->font);
uint8_t letter_height = font_get_height(font);
2016-09-30 15:29:00 +02:00
cord_t y = 0;
/*If the width will be expanded the set the max length to very big */
if(ext->long_mode == LV_LABEL_LONG_EXPAND || ext->long_mode == LV_LABEL_LONG_SCROLL) {
2016-12-20 15:02:23 +01:00
max_w = LV_CORD_MAX;
2016-09-30 15:29:00 +02:00
}
/*Search the line of the index letter */;
while (text[new_line_start] != '\0') {
2016-12-20 15:02:23 +01:00
new_line_start += txt_get_next_line(&text[line_start], font, labels->letter_space, max_w);
if(index < new_line_start || text[new_line_start] == '\0') break; /*The line of 'index' letter begins at 'line_start'*/
2016-09-30 15:29:00 +02:00
2016-10-07 11:15:46 +02:00
y += letter_height + labels->line_space;
2016-09-30 15:29:00 +02:00
line_start = new_line_start;
}
2016-12-20 15:02:23 +01:00
if((text[index - 1] == '\n' || text[index - 1] == '\r') && text[index] == '\0') {
y += letter_height + labels->line_space;
line_start = index;
}
2016-09-30 15:29:00 +02:00
/*Calculate the x coordinate*/
cord_t x = 0;
2016-10-01 00:25:10 +02:00
uint32_t i;
for(i = line_start; i < index; i++) {
2016-10-07 11:15:46 +02:00
x += font_get_width(font, text[i]) + labels->letter_space;
2016-10-01 00:25:10 +02:00
}
2016-10-07 11:15:46 +02:00
if(labels->mid != 0) {
2016-10-01 00:25:10 +02:00
cord_t line_w;
line_w = txt_get_width(&text[line_start], new_line_start - line_start,
2016-10-07 11:15:46 +02:00
font, labels->letter_space);
x += lv_obj_get_width(label) / 2 - line_w / 2;
2016-10-01 00:25:10 +02:00
}
2016-10-07 11:15:46 +02:00
pos->x = x;
pos->y = y;
2016-10-01 00:25:10 +02:00
}
/**
* Get the index of letter on a relative point of a label
2016-10-07 11:15:46 +02:00
* @param label pointer to label object
* @param pos pointer to point with coordinates on a the label
* @return the index of the letter on the 'pos_p' point (E.g. on 0;0 is the 0. letter)
*/
2016-10-07 11:15:46 +02:00
uint16_t lv_label_get_letter_on(lv_obj_t * label, point_t * pos)
2016-10-01 00:25:10 +02:00
{
2016-10-07 11:15:46 +02:00
const char * text = lv_label_get_text(label);
lv_label_ext_t * ext = lv_obj_get_ext(label);
2016-10-01 00:25:10 +02:00
uint32_t line_start = 0;
uint32_t new_line_start = 0;
2016-12-20 15:02:23 +01:00
cord_t max_w = lv_obj_get_width(label);
lv_labels_t * style = lv_obj_get_style(label);
const font_t * font = font_get(style->font);
2016-10-07 11:15:46 +02:00
uint8_t letter_height = font_get_height(font);
2016-10-01 00:25:10 +02:00
cord_t y = 0;
/*If the width will be expanded set the max length to very big */
if(ext->long_mode == LV_LABEL_LONG_EXPAND || ext->long_mode == LV_LABEL_LONG_SCROLL) {
2016-12-20 15:02:23 +01:00
max_w = LV_CORD_MAX;
2016-10-01 00:25:10 +02:00
}
/*Search the line of the index letter */;
while (text[line_start] != '\0') {
2016-12-20 15:02:23 +01:00
new_line_start += txt_get_next_line(&text[line_start], font, style->letter_space, max_w);
if(pos->y <= y + letter_height + style->line_space) break; /*The line is found ('line_start')*/
y += letter_height + style->line_space;
2016-10-01 00:25:10 +02:00
line_start = new_line_start;
2016-09-30 15:29:00 +02:00
}
2016-10-01 00:25:10 +02:00
/*Calculate the x coordinate*/
cord_t x = 0;
2016-12-20 15:02:23 +01:00
if(style->mid != 0) {
2016-10-01 00:25:10 +02:00
cord_t line_w;
line_w = txt_get_width(&text[line_start], new_line_start - line_start,
2016-12-20 15:02:23 +01:00
font, style->letter_space);
2016-10-07 11:15:46 +02:00
x += lv_obj_get_width(label) / 2 - line_w / 2;
2016-10-01 00:25:10 +02:00
}
uint16_t i;
2016-10-01 00:25:10 +02:00
for(i = line_start; i < new_line_start-1; i++) {
2016-12-20 15:02:23 +01:00
x += font_get_width(font, text[i]) + style->letter_space;
2016-10-07 11:15:46 +02:00
if(pos->x < x) break;
2016-10-01 00:25:10 +02:00
}
2016-09-30 15:29:00 +02:00
2016-10-01 00:25:10 +02:00
return i;
2016-09-30 15:29:00 +02:00
}
2016-06-08 07:25:08 +02:00
/**
* 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
2016-10-07 11:15:46 +02:00
* @param copy copy the style to this variable. (NULL if unused)
2016-06-08 07:25:08 +02:00
* @return pointer to an lv_labels_t style
*/
2016-10-07 11:15:46 +02:00
lv_labels_t * lv_labels_get(lv_labels_builtin_t style, lv_labels_t * copy)
2016-06-08 07:25:08 +02:00
{
static bool style_inited = false;
/*Make the style initialization if it is not done yet*/
if(style_inited == false) {
lv_labels_init();
style_inited = true;
}
2016-06-08 07:25:08 +02:00
lv_labels_t * style_p;
switch(style) {
case LV_LABELS_DEF:
style_p = &lv_labels_def;
break;
case LV_LABELS_BTN:
style_p = &lv_labels_btn;
break;
case LV_LABELS_TXT:
style_p = &lv_labels_txt;
break;
case LV_LABELS_TITLE:
style_p = &lv_labels_title;
break;
default:
style_p = &lv_labels_def;
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
if(copy != NULL) {
if(style_p != NULL) memcpy(copy, style_p, sizeof(lv_labels_t));
else memcpy(copy, &lv_labels_def, sizeof(lv_labels_t));
2016-06-08 07:25:08 +02:00
}
return style_p;
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the labels
2016-10-07 11:15:46 +02:00
* @param label pointer to a label object
2016-06-08 07:25:08 +02:00
* @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')
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
2016-06-08 07:25:08 +02:00
* @param return true/false, depends on 'mode'
*/
2016-10-07 11:15:46 +02:00
static bool lv_label_design(lv_obj_t * label, const area_t * mask, lv_design_mode_t mode)
2016-06-08 07:25:08 +02:00
{
/* A label never covers an area */
if(mode == LV_DESIGN_COVER_CHK) return false;
else if(mode == LV_DESIGN_DRAW_MAIN) {
/*TEST: draw a background for the label*/
2016-10-20 16:35:03 +02:00
//lv_vfill(&label->cords, mask, COLOR_LIME, OPA_COVER);
area_t cords;
2016-10-07 11:15:46 +02:00
lv_obj_get_cords(label, &cords);
opa_t opa = lv_obj_get_opa(label);
lv_label_ext_t * ext = lv_obj_get_ext(label);
lv_draw_label(&cords, mask, lv_obj_get_style(label), opa, ext->txt);
}
2016-06-08 07:25:08 +02:00
return true;
}
/**
* Initialize the label styles
*/
static void lv_labels_init(void)
{
/*Default style*/
lv_labels_def.font = LV_FONT_DEFAULT;
lv_labels_def.objs.color = COLOR_MAKE(0x10, 0x18, 0x20);
lv_labels_def.letter_space = 2 * LV_DOWNSCALE;
lv_labels_def.line_space = 2 * LV_DOWNSCALE;
lv_labels_def.mid = 0;
memcpy(&lv_labels_btn, &lv_labels_def, sizeof(lv_labels_t));
lv_labels_btn.objs.color = COLOR_MAKE(0xd0, 0xe0, 0xf0);
lv_labels_btn.mid = 1;
memcpy(&lv_labels_title, &lv_labels_def, sizeof(lv_labels_t));
lv_labels_title.objs.color = COLOR_MAKE(0x10, 0x20, 0x30);
lv_labels_title.letter_space = 4 * LV_DOWNSCALE;
lv_labels_title.line_space = 4 * LV_DOWNSCALE;
lv_labels_title.mid = 0;
memcpy(&lv_labels_txt, &lv_labels_def, sizeof(lv_labels_t));
lv_labels_txt.objs.color = COLOR_MAKE(0x16, 0x23, 0x34);
lv_labels_txt.letter_space = 0 * LV_DOWNSCALE;
lv_labels_txt.line_space = 1 * LV_DOWNSCALE;
lv_labels_txt.mid = 0;
}
2016-06-08 07:25:08 +02:00
#endif