2016-09-30 15:29:00 +02:00
|
|
|
/**
|
|
|
|
* @file lv_ta.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
|
|
|
#include "lv_conf.h"
|
|
|
|
#if USE_LV_TA != 0
|
|
|
|
|
|
|
|
#include "lv_ta.h"
|
2016-12-15 10:31:30 +01:00
|
|
|
#include "lvgl/lv_misc/anim.h"
|
2016-10-04 09:45:39 +02:00
|
|
|
#include "../lv_draw/lv_draw.h"
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2017-01-12 09:49:13 +01:00
|
|
|
/*Test configuration*/
|
|
|
|
#ifndef LV_TA_MAX_LENGTH
|
|
|
|
#define LV_TA_MAX_LENGTH 256
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LV_TA_CUR_BLINK_TIME
|
|
|
|
#define LV_TA_CUR_BLINK_TIME 400 /*ms*/
|
|
|
|
#endif
|
|
|
|
|
2017-01-02 15:00:21 +01:00
|
|
|
#define LV_TA_DEF_WIDTH (120 * LV_DOWNSCALE)
|
|
|
|
#define LV_TA_DEF_HEIGHT (80 * LV_DOWNSCALE)
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
2016-10-07 11:15:46 +02:00
|
|
|
static bool lv_ta_design(lv_obj_t * ta, const area_t * mask, lv_design_mode_t mode);
|
2016-10-07 17:33:35 +02:00
|
|
|
static bool lv_ta_scrling_design(lv_obj_t * scrling, const area_t * mask, lv_design_mode_t mode);
|
2016-12-15 10:31:30 +01:00
|
|
|
static void lv_ta_hide_cursor(lv_obj_t * ta, uint8_t hide);
|
2016-10-07 11:15:46 +02:00
|
|
|
static void lv_ta_save_valid_cursor_x(lv_obj_t * ta);
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
|
|
|
lv_design_f_t ancestor_design_f;
|
2017-01-01 22:56:09 +01:00
|
|
|
lv_design_f_t scrl_design_f;
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/*-----------------
|
|
|
|
* Create function
|
|
|
|
*-----------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a text area objects
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param par pointer to an object, it will be the parent of the new text area
|
|
|
|
* @param copy pointer to a text area object, if not NULL then the new object will be copied from it
|
2016-09-30 15:29:00 +02:00
|
|
|
* @return pointer to the created text area
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_t * lv_ta_create(lv_obj_t * par, lv_obj_t * copy)
|
2016-09-30 15:29:00 +02:00
|
|
|
{
|
|
|
|
/*Create the ancestor object*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_t * new_ta = lv_page_create(par, copy);
|
|
|
|
dm_assert(new_ta);
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/*Allocate the object type specific extended data*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_alloc_ext(new_ta, sizeof(lv_ta_ext_t));
|
|
|
|
dm_assert(ext);
|
2017-01-02 14:10:32 +01:00
|
|
|
ext->cur_hide = 0;
|
|
|
|
ext->cursor_pos = 0;
|
|
|
|
ext->cursor_valid_x = 0;
|
|
|
|
ext->label = NULL;
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2017-01-02 14:10:32 +01:00
|
|
|
if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_f(new_ta);
|
|
|
|
if(scrl_design_f == NULL) scrl_design_f = lv_obj_get_design_f(ext->page.scrl);
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_set_signal_f(new_ta, lv_ta_signal);
|
|
|
|
lv_obj_set_design_f(new_ta, lv_ta_design);
|
2016-09-30 15:29:00 +02:00
|
|
|
|
|
|
|
/*Init the new text area object*/
|
2016-10-07 11:15:46 +02:00
|
|
|
if(copy == NULL) {
|
|
|
|
ext->label = lv_label_create(new_ta, NULL);
|
2016-12-15 10:31:30 +01:00
|
|
|
|
2017-01-01 22:56:09 +01:00
|
|
|
lv_obj_set_design_f(ext->page.scrl, lv_ta_scrling_design);
|
2016-10-20 16:35:03 +02:00
|
|
|
lv_label_set_long_mode(ext->label, LV_LABEL_LONG_BREAK);
|
2016-12-15 10:31:30 +01:00
|
|
|
lv_label_set_text(ext->label, "Text area");
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_page_glue_obj(ext->label, true);
|
2016-12-18 22:07:03 +01:00
|
|
|
lv_obj_set_click(ext->label, false);
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_obj_set_style(new_ta, lv_obj_get_style(ext->page.scrl));
|
2017-01-02 15:00:21 +01:00
|
|
|
lv_obj_set_size(new_ta, LV_TA_DEF_WIDTH, LV_TA_DEF_HEIGHT);
|
2016-09-30 15:29:00 +02:00
|
|
|
}
|
|
|
|
/*Copy an existing object*/
|
|
|
|
else {
|
2017-01-01 22:56:09 +01:00
|
|
|
lv_obj_set_design_f(ext->page.scrl, lv_ta_scrling_design);
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * copy_ext = lv_obj_get_ext(copy);
|
|
|
|
ext->label = lv_label_create(new_ta, copy_ext->label);
|
|
|
|
lv_page_glue_obj(ext->label, true);
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2017-01-08 13:06:41 +01:00
|
|
|
/*Refresh the style with new signal function*/
|
|
|
|
lv_obj_refr_style(new_ta);
|
2016-09-30 15:29:00 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:31:30 +01:00
|
|
|
/*Create a cursor blinker animation*/
|
|
|
|
anim_t a;
|
|
|
|
a.var = new_ta;
|
|
|
|
a.fp = (anim_fp_t)lv_ta_hide_cursor;
|
|
|
|
a.time = LV_TA_CUR_BLINK_TIME;
|
|
|
|
a.act_time = 0;
|
|
|
|
a.end_cb = NULL;
|
2016-12-18 22:07:03 +01:00
|
|
|
a.start = 0;
|
|
|
|
a.end= 1;
|
2016-12-15 10:31:30 +01:00
|
|
|
a.repeat = 1;
|
|
|
|
a.repeat_pause = 0;
|
|
|
|
a.playback = 1;
|
|
|
|
a.playback_pause = 0;
|
|
|
|
a.path = anim_get_path(ANIM_PATH_STEP);
|
|
|
|
anim_create(&a);
|
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
return new_ta;
|
2016-09-30 15:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal function of the text area
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-09-30 15:29:00 +02:00
|
|
|
* @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
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
bool lv_ta_signal(lv_obj_t * ta, lv_signal_t sign, void * param)
|
2016-09-30 15:29:00 +02:00
|
|
|
{
|
|
|
|
bool valid;
|
|
|
|
|
|
|
|
/* Include the ancient signal function */
|
2016-10-07 11:15:46 +02:00
|
|
|
valid = lv_page_signal(ta, sign, param);
|
2016-09-30 15:29:00 +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_ta_ext_t * ext = lv_obj_get_ext(ta);
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_style_t * style = lv_obj_get_style(ta);
|
2016-09-30 15:29:00 +02:00
|
|
|
switch(sign) {
|
|
|
|
case LV_SIGNAL_CLEANUP:
|
2016-12-15 10:31:30 +01:00
|
|
|
/* Nothing to clean up.
|
|
|
|
* (The created label will be deleted automatically) */
|
2016-10-04 09:45:39 +02:00
|
|
|
break;
|
|
|
|
case LV_SIGNAL_STYLE_CHG:
|
2017-01-06 13:30:57 +01:00
|
|
|
if(ext->label) {
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_obj_set_style(ext->label, lv_obj_get_style(ext->page.scrl));
|
2017-01-06 13:30:57 +01:00
|
|
|
lv_obj_set_width(ext->label, lv_obj_get_width(ta) - 2 *
|
2017-04-13 10:20:35 +02:00
|
|
|
(style->hpad + style->hpad));
|
2017-01-06 13:30:57 +01:00
|
|
|
lv_label_set_text(ext->label, NULL);
|
|
|
|
}
|
2016-10-04 09:45:39 +02:00
|
|
|
break;
|
|
|
|
/*Set the label width according to the text area width*/
|
|
|
|
case LV_SIGNAL_CORD_CHG:
|
2017-01-06 13:30:57 +01:00
|
|
|
if(ext->label != NULL) {
|
|
|
|
lv_obj_set_width(ext->label, lv_obj_get_width(ta) - 2 *
|
2017-04-13 10:20:35 +02:00
|
|
|
(style->hpad + style->hpad));
|
2017-01-06 13:30:57 +01:00
|
|
|
lv_label_set_text(ext->label, NULL);
|
|
|
|
}
|
2016-09-30 15:29:00 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Setter functions
|
|
|
|
*====================*/
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Insert a character to the current cursor position
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @param c a character
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
void lv_ta_add_char(lv_obj_t * ta, char c)
|
2016-09-30 15:29:00 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
const char * label_txt = lv_label_get_text(ext->label);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2017-01-02 15:00:21 +01:00
|
|
|
/*Test the new length: txt length + 1 (closing'\0') + 1 (c character)*/
|
|
|
|
if((strlen(label_txt) + 2) > LV_TA_MAX_LENGTH) return;
|
|
|
|
char buf[LV_TA_MAX_LENGTH];
|
|
|
|
|
|
|
|
/*Insert the character*/
|
2016-10-07 11:15:46 +02:00
|
|
|
memcpy(buf, label_txt, ext->cursor_pos);
|
|
|
|
buf[ext->cursor_pos] = c;
|
|
|
|
memcpy(buf+ext->cursor_pos+1, label_txt+ext->cursor_pos, strlen(label_txt) - ext->cursor_pos + 1);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Refresh the label*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_label_set_text(ext->label, buf);
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Move the cursor after the new character*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_set_cursor_pos(ta, lv_ta_get_cursor_pos(ta) + 1);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/*It is a valid x step so save it*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_save_valid_cursor_x(ta);
|
2016-10-01 00:25:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Insert a text to the current cursor position
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @param txt a '\0' terminated string to insert
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
void lv_ta_add_text(lv_obj_t * ta, const char * txt)
|
2016-10-01 00:25:10 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
const char * label_txt = lv_label_get_text(ext->label);
|
2017-01-02 15:00:21 +01:00
|
|
|
uint16_t label_len = strlen(label_txt);
|
|
|
|
uint16_t txt_len = strlen(txt);
|
|
|
|
|
|
|
|
/*Test the new length (+ 1 for the closing '\0')*/
|
|
|
|
if((label_len + txt_len + 1) > LV_TA_MAX_LENGTH) return;
|
|
|
|
|
|
|
|
/*Insert the text*/
|
|
|
|
char buf[LV_TA_MAX_LENGTH];
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
memcpy(buf, label_txt, ext->cursor_pos);
|
2017-01-02 15:00:21 +01:00
|
|
|
memcpy(buf + ext->cursor_pos, txt, txt_len);
|
|
|
|
memcpy(buf + ext->cursor_pos + txt_len, label_txt+ext->cursor_pos, label_len - ext->cursor_pos + 1);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Refresh the label*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_label_set_text(ext->label, buf);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Move the cursor after the new text*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_set_cursor_pos(ta, lv_ta_get_cursor_pos(ta) + txt_len);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/*It is a valid x step so save it*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_save_valid_cursor_x(ta);
|
2016-10-01 00:25:10 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:31:30 +01:00
|
|
|
/**
|
2016-12-18 22:07:03 +01:00
|
|
|
* Set the text of a text area
|
2016-12-15 10:31:30 +01:00
|
|
|
* @param ta pointer to a text area
|
|
|
|
* @param txt pointer to the text
|
|
|
|
*/
|
|
|
|
void lv_ta_set_text(lv_obj_t * ta, const char * txt)
|
|
|
|
{
|
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
|
|
|
lv_label_set_text(ext->label, txt);
|
|
|
|
lv_ta_set_cursor_pos(ta, LV_TA_CUR_LAST);
|
|
|
|
|
|
|
|
/*It is a valid x step so save it*/
|
|
|
|
lv_ta_save_valid_cursor_x(ta);
|
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Delete a the left character from the current cursor position
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
void lv_ta_del(lv_obj_t * ta)
|
2016-10-01 00:25:10 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
|
|
|
uint16_t cur_pos = ext->cursor_pos;
|
2016-10-01 00:25:10 +02:00
|
|
|
|
|
|
|
if(cur_pos == 0) return;
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Delete a character*/
|
2016-10-01 00:25:10 +02:00
|
|
|
char buf[LV_TA_MAX_LENGTH];
|
2016-10-07 11:15:46 +02:00
|
|
|
const char * label_txt = lv_label_get_text(ext->label);
|
2016-10-01 00:25:10 +02:00
|
|
|
uint16_t label_len = strlen(label_txt);
|
|
|
|
memcpy(buf, label_txt, cur_pos - 1);
|
|
|
|
memcpy(buf+cur_pos - 1, label_txt + cur_pos, label_len - cur_pos + 1);
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Refresh the label*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_label_set_text(ext->label, buf);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Move the cursor to the place of the deleted character*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_set_cursor_pos(ta, lv_ta_get_cursor_pos(ta) - 1);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/*It is a valid x step so save it*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_save_valid_cursor_x(ta);
|
2016-10-01 00:25:10 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:31:30 +01:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Set the cursor position
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param obj pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @param pos the new cursor position in character index
|
2016-12-15 10:31:30 +01:00
|
|
|
* < 0 : index from the end of the text
|
|
|
|
* LV_TA_CUR_LAST: go after the last character
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2016-12-15 10:31:30 +01:00
|
|
|
void lv_ta_set_cursor_pos(lv_obj_t * ta, int16_t pos)
|
2016-10-01 00:25:10 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_obj_t * scrl = lv_page_get_scrl(ta);
|
|
|
|
lv_style_t * style_scrl = lv_obj_get_style(scrl);
|
2016-10-07 11:15:46 +02:00
|
|
|
uint16_t txt_len = strlen(lv_label_get_text(ext->label));
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2016-12-15 10:31:30 +01:00
|
|
|
if(pos < 0) pos = txt_len + pos;
|
|
|
|
|
|
|
|
if(pos > txt_len || pos == LV_TA_CUR_LAST) pos = txt_len;
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
ext->cursor_pos = pos;
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/*Position the label to make the cursor visible*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_t * label_par = lv_obj_get_parent(ext->label);
|
2016-10-04 09:45:39 +02:00
|
|
|
point_t cur_pos;
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_style_t * style = lv_obj_get_style(ta);
|
|
|
|
const font_t * font_p = style->font;
|
2016-12-30 21:44:41 +01:00
|
|
|
area_t label_cords;
|
|
|
|
area_t ta_cords;
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_label_get_letter_pos(ext->label, pos, &cur_pos);
|
2016-12-30 21:44:41 +01:00
|
|
|
lv_obj_get_cords(ta, &ta_cords);
|
|
|
|
lv_obj_get_cords(ext->label, &label_cords);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/*Check the top*/
|
2016-10-07 11:15:46 +02:00
|
|
|
if(lv_obj_get_y(label_par) + cur_pos.y < 0) {
|
|
|
|
lv_obj_set_y(label_par, - cur_pos.y);
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Check the bottom*/
|
2017-03-20 07:07:05 +01:00
|
|
|
cord_t font_h = font_get_height(font_p) >> LV_FONT_ANTIALIAS;
|
2017-04-13 10:20:35 +02:00
|
|
|
if(label_cords.y1 + cur_pos.y + font_h + style_scrl->vpad > ta_cords.y2) {
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_set_y(label_par, -(cur_pos.y - lv_obj_get_height(ta) +
|
2017-04-13 10:20:35 +02:00
|
|
|
font_h + 2 * style_scrl->vpad));
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_inv(ta);
|
2016-09-30 15:29:00 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the cursor one character right
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
void lv_ta_cursor_right(lv_obj_t * ta)
|
2016-10-04 09:45:39 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
uint16_t cp = lv_ta_get_cursor_pos(ta);
|
2016-10-04 09:45:39 +02:00
|
|
|
cp++;
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_set_cursor_pos(ta, cp);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/*It is a valid x step so save it*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_save_valid_cursor_x(ta);
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the cursor one character left
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
void lv_ta_cursor_left(lv_obj_t * ta)
|
2016-10-04 09:45:39 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
uint16_t cp = lv_ta_get_cursor_pos(ta);
|
2016-10-04 09:45:39 +02:00
|
|
|
if(cp > 0) {
|
|
|
|
cp--;
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_set_cursor_pos(ta, cp);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/*It is a valid x step so save it*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_save_valid_cursor_x(ta);
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the cursor one line down
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
void lv_ta_cursor_down(lv_obj_t * ta)
|
2016-10-01 00:25:10 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
2016-10-01 00:25:10 +02:00
|
|
|
point_t pos;
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Get the position of the current letter*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_label_get_letter_pos(ext->label, lv_ta_get_cursor_pos(ta), &pos);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Increment the y with one line and keep the valid x*/
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_style_t * label_style = lv_obj_get_style(ext->label);
|
2017-04-10 11:33:38 +02:00
|
|
|
const font_t * font_p = label_style->font;
|
2017-03-20 07:07:05 +01:00
|
|
|
cord_t font_h = font_get_height(font_p) >> LV_FONT_ANTIALIAS;
|
|
|
|
pos.y += font_h + label_style->line_space + 1;
|
2016-10-07 11:15:46 +02:00
|
|
|
pos.x = ext->cursor_valid_x;
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Do not go below he last line*/
|
2016-10-07 11:15:46 +02:00
|
|
|
if(pos.y < lv_obj_get_height(ext->label)) {
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Get the letter index on the new cursor position and set it*/
|
2016-10-07 11:15:46 +02:00
|
|
|
uint16_t new_cur_pos = lv_label_get_letter_on(ext->label, &pos);
|
|
|
|
lv_ta_set_cursor_pos(ta, new_cur_pos);
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
2016-10-01 00:25:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Move the cursor one line up
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
void lv_ta_cursor_up(lv_obj_t * ta)
|
2016-10-01 00:25:10 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
2016-10-01 00:25:10 +02:00
|
|
|
point_t pos;
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Get the position of the current letter*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_label_get_letter_pos(ext->label, lv_ta_get_cursor_pos(ta), &pos);
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Decrement the y with one line and keep the valid x*/
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_style_t * label_style = lv_obj_get_style(ext->label);
|
2017-04-10 11:33:38 +02:00
|
|
|
const font_t * font = label_style->font;
|
2017-03-20 07:07:05 +01:00
|
|
|
cord_t font_h = font_get_height(font) >> LV_FONT_ANTIALIAS;
|
|
|
|
pos.y -= font_h + label_style->line_space - 1;
|
2016-10-07 11:15:46 +02:00
|
|
|
pos.x = ext->cursor_valid_x;
|
2016-10-01 00:25:10 +02:00
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/*Get the letter index on the new cursor position and set it*/
|
2016-10-07 11:15:46 +02:00
|
|
|
uint16_t new_cur_pos = lv_label_get_letter_on(ext->label, &pos);
|
|
|
|
lv_ta_set_cursor_pos(ta, new_cur_pos);
|
2016-10-01 00:25:10 +02:00
|
|
|
}
|
2017-04-13 10:20:35 +02:00
|
|
|
|
2016-09-30 15:29:00 +02:00
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Get the text of the i the text area
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta obj pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @return pointer to the text
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
const char * lv_ta_get_txt(lv_obj_t * ta)
|
2016-10-04 09:45:39 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
|
|
|
return lv_label_get_text(ext->label);
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current cursor position in character index
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
* @return the cursor position
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
uint16_t lv_ta_get_cursor_pos(lv_obj_t * ta)
|
2016-10-04 09:45:39 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
|
|
|
return ext->cursor_pos;
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 15:29:00 +02:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the drawing related tasks of the text areas
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to an object
|
2016-09-30 15:29:00 +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)
|
2016-10-04 09:45:39 +02:00
|
|
|
* LV_DESIGN_DRAW_MAIN: draw the object (always return 'true')
|
|
|
|
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
|
2016-09-30 15:29:00 +02:00
|
|
|
* @param return true/false, depends on 'mode'
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
static bool lv_ta_design(lv_obj_t * ta, const area_t * masp, lv_design_mode_t mode)
|
2016-09-30 15:29:00 +02:00
|
|
|
{
|
|
|
|
if(mode == LV_DESIGN_COVER_CHK) {
|
|
|
|
/*Return false if the object is not covers the mask_p area*/
|
2016-10-07 11:15:46 +02:00
|
|
|
return ancestor_design_f(ta, masp, mode);
|
2016-10-04 09:45:39 +02:00
|
|
|
} else if(mode == LV_DESIGN_DRAW_MAIN) {
|
|
|
|
/*Draw the object*/
|
2016-10-07 11:15:46 +02:00
|
|
|
ancestor_design_f(ta, masp, mode);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
} else if(mode == LV_DESIGN_DRAW_POST) {
|
2016-10-07 11:15:46 +02:00
|
|
|
ancestor_design_f(ta, masp, mode);
|
2016-09-30 15:29:00 +02:00
|
|
|
}
|
2016-10-04 09:45:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-30 15:29:00 +02:00
|
|
|
|
2016-10-04 15:29:52 +02:00
|
|
|
/**
|
2016-10-07 17:33:35 +02:00
|
|
|
* An extended scrolling design of the page. Calls the normal design function and it draws a cursor.
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param label pointer to a text area object
|
|
|
|
* @param mask the object will be drawn only in this area
|
2016-10-04 15:29:52 +02:00
|
|
|
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
|
|
|
|
* (return 'true' if yes)
|
|
|
|
* LV_DESIGN_DRAW_MAIN: draw the object (always return 'true')
|
|
|
|
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
|
|
|
|
* @return return true/false, depends on 'mode'
|
|
|
|
*/
|
2016-10-07 17:33:35 +02:00
|
|
|
static bool lv_ta_scrling_design(lv_obj_t * scrling, const area_t * mask, lv_design_mode_t mode)
|
2016-10-04 09:45:39 +02:00
|
|
|
{
|
|
|
|
if(mode == LV_DESIGN_COVER_CHK) {
|
|
|
|
/*Return false if the object is not covers the mask_p area*/
|
2017-01-01 22:56:09 +01:00
|
|
|
return scrl_design_f(scrling, mask, mode);
|
2016-10-04 09:45:39 +02:00
|
|
|
} else if(mode == LV_DESIGN_DRAW_MAIN) {
|
|
|
|
/*Draw the object*/
|
2017-01-01 22:56:09 +01:00
|
|
|
scrl_design_f(scrling, mask, mode);
|
2016-10-04 09:45:39 +02:00
|
|
|
} else if(mode == LV_DESIGN_DRAW_POST) {
|
2017-01-01 22:56:09 +01:00
|
|
|
scrl_design_f(scrling, mask, mode);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
|
|
|
/*Draw the cursor too*/
|
2016-10-07 17:33:35 +02:00
|
|
|
lv_obj_t * ta = lv_obj_get_parent(scrling);
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ta_ext = lv_obj_get_ext(ta);
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_style_t * ta_style = lv_obj_get_style(ta);
|
2016-10-07 11:15:46 +02:00
|
|
|
|
2017-04-13 10:20:35 +02:00
|
|
|
if(ta_ext->cursor_show != 0 && ta_ext->cur_hide == 0) {
|
2016-10-07 11:15:46 +02:00
|
|
|
uint16_t cur_pos = lv_ta_get_cursor_pos(ta);
|
|
|
|
point_t letter_pos;
|
2016-10-07 17:33:35 +02:00
|
|
|
lv_label_get_letter_pos(ta_ext->label, cur_pos, &letter_pos);
|
2016-10-07 11:15:46 +02:00
|
|
|
|
|
|
|
area_t cur_area;
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_style_t * labels_p = lv_obj_get_style(ta_ext->label);
|
2017-04-11 10:50:57 +02:00
|
|
|
cur_area.x1 = letter_pos.x + ta_ext->label->cords.x1;
|
2016-10-07 17:33:35 +02:00
|
|
|
cur_area.y1 = letter_pos.y + ta_ext->label->cords.y1;
|
2017-04-11 10:50:57 +02:00
|
|
|
cur_area.x2 = letter_pos.x + ta_ext->label->cords.x1 + LV_DOWNSCALE ;
|
2017-04-10 11:33:38 +02:00
|
|
|
cur_area.y2 = letter_pos.y + ta_ext->label->cords.y1 + (font_get_height(labels_p->font) >> LV_FONT_ANTIALIAS);
|
2016-10-07 11:15:46 +02:00
|
|
|
|
2017-04-13 10:20:35 +02:00
|
|
|
lv_style_t cur_rects;
|
|
|
|
lv_style_get(LV_STYLE_PLAIN, &cur_rects);
|
2017-04-10 11:33:38 +02:00
|
|
|
cur_rects.radius = 0;
|
2016-10-07 11:15:46 +02:00
|
|
|
cur_rects.bwidth = 0;
|
2017-04-13 10:20:35 +02:00
|
|
|
cur_rects.mcolor = ta_style->ccolor;
|
|
|
|
cur_rects.gcolor = ta_style->ccolor;
|
2017-04-10 11:33:38 +02:00
|
|
|
lv_draw_rect(&cur_area, mask, &cur_rects);
|
2016-10-07 11:15:46 +02:00
|
|
|
}
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-15 10:31:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the cursor visibility to make a blinking cursor
|
|
|
|
* @param ta pointer to a text area
|
|
|
|
* @param hide 1: hide the cursor, 0: draw it
|
|
|
|
*/
|
|
|
|
static void lv_ta_hide_cursor(lv_obj_t * ta, uint8_t hide)
|
|
|
|
{
|
|
|
|
lv_ta_ext_t * ta_ext = lv_obj_get_ext(ta);
|
2016-12-18 22:07:03 +01:00
|
|
|
if(hide != ta_ext->cur_hide) {
|
|
|
|
ta_ext->cur_hide = hide == 0 ? 0 : 1;
|
|
|
|
lv_obj_inv(ta);
|
|
|
|
}
|
2016-12-15 10:31:30 +01:00
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
/**
|
|
|
|
* Save the cursor x position as valid. It is important when jumping up/down to a shorter line
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param ta pointer to a text area object
|
2016-10-04 09:45:39 +02:00
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
static void lv_ta_save_valid_cursor_x(lv_obj_t * ta)
|
2016-10-04 09:45:39 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_ta_ext_t * ext = lv_obj_get_ext(ta);
|
2016-10-04 09:45:39 +02:00
|
|
|
point_t cur_pos;
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_label_get_letter_pos(ext->label, ext->cursor_pos, &cur_pos);
|
|
|
|
ext->cursor_valid_x = cur_pos.x;
|
2016-09-30 15:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|