mirror of
https://github.com/lvgl/lvgl.git
synced 2025-01-14 06:42:58 +08:00
331 lines
9.9 KiB
C
331 lines
9.9 KiB
C
|
|
/**
|
|
* @file lv_kb.c
|
|
*
|
|
*/
|
|
|
|
/*********************
|
|
* INCLUDES
|
|
*********************/
|
|
#include "lv_conf.h"
|
|
#if USE_LV_KB != 0
|
|
|
|
#include "lv_kb.h"
|
|
#include "lv_ta.h"
|
|
|
|
/*********************
|
|
* DEFINES
|
|
*********************/
|
|
|
|
/**********************
|
|
* TYPEDEFS
|
|
**********************/
|
|
|
|
/**********************
|
|
* STATIC PROTOTYPES
|
|
**********************/
|
|
static lv_res_t lv_kb_signal(lv_obj_t * kb, lv_signal_t sign, void * param);
|
|
static lv_res_t lv_app_kb_action(lv_obj_t * kb, const char * txt);
|
|
|
|
/**********************
|
|
* STATIC VARIABLES
|
|
**********************/
|
|
static lv_signal_func_t ancestor_signal;
|
|
|
|
static const char * kb_map_lc[] = {
|
|
"\2051#", "\204q", "\204w", "\204e", "\204r", "\204t", "\204y", "\204u", "\204i", "\204o", "\204p", "\207Del", "\n",
|
|
"\206ABC", "\203a", "\203s", "\203d", "\203f", "\203g", "\203h", "\203j", "\203k", "\203l", "\207Enter", "\n",
|
|
"_", "-", "z", "x", "c", "v", "b", "n", "m", ".", ",", ":", "\n",
|
|
"\202"SYMBOL_CLOSE, "\202"SYMBOL_LEFT, "\206 ", "\202"SYMBOL_RIGHT, "\202"SYMBOL_OK, ""
|
|
};
|
|
|
|
static const char * kb_map_uc[] = {
|
|
"\2051#", "\204Q", "\204W", "\204E", "\204R", "\204T", "\204Y", "\204U", "\204I", "\204O", "\204P", "\207Del", "\n",
|
|
"\206abc", "\203A", "\203S", "\203D", "\203F", "\203G", "\203H", "\203J", "\203K", "\203L", "\207Enter", "\n",
|
|
"_", "-", "Z", "X", "C", "V", "B", "N", "M", ".", ",", ":", "\n",
|
|
"\202"SYMBOL_CLOSE, "\202"SYMBOL_LEFT, "\206 ", "\202"SYMBOL_RIGHT, "\202"SYMBOL_OK, ""
|
|
};
|
|
|
|
static const char * kb_map_spec[] = {
|
|
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\202Del", "\n",
|
|
"\202abc", "+", "-", "/", "*", "=", "%", "!", "?", "#", "<", ">", "\n",
|
|
"\\", "@", "$", "(", ")", "{", "}", "[", "]", ";", "\"", "'", "\n",
|
|
"\202"SYMBOL_CLOSE, "\202"SYMBOL_LEFT, "\206 ", "\202"SYMBOL_RIGHT, "\202"SYMBOL_OK, ""
|
|
};
|
|
|
|
static const char * kb_map_num[] = {
|
|
"1", "2", "3", "\202"SYMBOL_CLOSE,"\n",
|
|
"4", "5", "6", "\202"SYMBOL_OK, "\n",
|
|
"7", "8", "9", "\202Del", "\n",
|
|
"+/-", "0", ".", SYMBOL_LEFT, SYMBOL_RIGHT, ""
|
|
};
|
|
/**********************
|
|
* MACROS
|
|
**********************/
|
|
|
|
/**********************
|
|
* GLOBAL FUNCTIONS
|
|
**********************/
|
|
|
|
/**
|
|
* Create a keyboard objects
|
|
* @param par pointer to an object, it will be the parent of the new keyboard
|
|
* @param copy pointer to a keyboard object, if not NULL then the new object will be copied from it
|
|
* @return pointer to the created keyboard
|
|
*/
|
|
lv_obj_t * lv_kb_create(lv_obj_t * par, lv_obj_t * copy)
|
|
{
|
|
/*Create the ancestor of keyboard*/
|
|
lv_obj_t * new_kb = lv_btnm_create(par, copy);
|
|
dm_assert(new_kb);
|
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_kb);
|
|
|
|
/*Allocate the keyboard type specific extended data*/
|
|
lv_kb_ext_t * ext = lv_obj_allocate_ext_attr(new_kb, sizeof(lv_kb_ext_t));
|
|
dm_assert(ext);
|
|
|
|
/*Initialize the allocated 'ext' */
|
|
|
|
ext->ta = NULL;
|
|
ext->mode = LV_KB_MODE_TEXT;
|
|
ext->cur_mng = 0;
|
|
ext->close_action = NULL;
|
|
ext->ok_action = NULL;
|
|
|
|
/*The signal and design functions are not copied so set them here*/
|
|
lv_obj_set_signal_func(new_kb, lv_kb_signal);
|
|
|
|
/*Init the new keyboard keyboard*/
|
|
if(copy == NULL) {
|
|
lv_obj_set_size(new_kb, LV_HOR_RES, LV_VER_RES / 2);
|
|
lv_obj_align(new_kb, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
|
|
lv_btnm_set_action(new_kb, lv_app_kb_action);
|
|
lv_btnm_set_map(new_kb, kb_map_lc);
|
|
}
|
|
/*Copy an existing keyboard*/
|
|
else {
|
|
lv_kb_ext_t * copy_ext = lv_obj_get_ext_attr(copy);ext->ta = NULL;
|
|
ext->ta = copy_ext->ta;
|
|
ext->mode = copy_ext->mode;
|
|
ext->cur_mng = copy_ext->cur_mng;
|
|
ext->close_action = copy_ext->close_action;
|
|
ext->ok_action = copy_ext->ok_action;
|
|
|
|
/*Refresh the style with new signal function*/
|
|
lv_obj_refresh_style(new_kb);
|
|
}
|
|
|
|
return new_kb;
|
|
}
|
|
|
|
/*=====================
|
|
* Setter functions
|
|
*====================*/
|
|
|
|
/**
|
|
* Assign a Text Area to the Keyboard. The pressed characters will be put there.
|
|
* @param kb pointer to a Keyboard object
|
|
* @param ta pointer to a Text Area object to write there
|
|
*/
|
|
void lv_kb_set_ta(lv_obj_t * kb, lv_obj_t * ta)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
|
|
if(ext->ta != NULL && ext->cur_mng != 0) lv_ta_set_cursor_show(ext->ta, false);
|
|
ext->ta = ta;
|
|
if(ext->cur_mng != 0) lv_ta_set_cursor_show(ext->ta, true);
|
|
}
|
|
|
|
/**
|
|
* Set a new a mode (text or number map)
|
|
* @param kb pointer to a Keyboard object
|
|
* @param mode the mode from 'lv_kb_mode_t'
|
|
*/
|
|
void lv_kb_set_mode(lv_obj_t * kb, lv_kb_mode_t mode)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
ext->mode = mode;
|
|
if(mode == LV_KB_MODE_TEXT) lv_btnm_set_map(kb, kb_map_lc);
|
|
else if(mode == LV_KB_MODE_NUM) lv_btnm_set_map(kb, kb_map_num);
|
|
}
|
|
|
|
|
|
/**
|
|
* Automatically hide or show the cursor of Text Area
|
|
* @param kb pointer to a Keyboard object
|
|
* @param en true: show cursor on the current text area, false: hide cursor
|
|
*/
|
|
void lv_kb_set_cursor_manage(lv_obj_t * kb, bool en)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
ext->cur_mng = en == false? 0 : 1;
|
|
|
|
if(ext->ta != NULL) lv_ta_set_cursor_show(ext->ta, true);
|
|
}
|
|
|
|
/**
|
|
* Set call back to call when the "Ok" button is pressed
|
|
* @param kb pointer to Keyboard object
|
|
* @param action a callback with 'lv_action_t' type
|
|
*/
|
|
void lv_kb_set_ok_action(lv_obj_t * kb, lv_action_t action)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
ext->ok_action = action;
|
|
}
|
|
|
|
/**
|
|
* Set call back to call when the "Hide" button is pressed
|
|
* @param kb pointer to Keyboard object
|
|
* @param action a callback with 'lv_action_t' type
|
|
*/
|
|
void lv_kb_set_close_action(lv_obj_t * kb, lv_action_t action)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
ext->close_action = action;
|
|
}
|
|
|
|
|
|
/*=====================
|
|
* Getter functions
|
|
*====================*/
|
|
|
|
/**
|
|
* Assign a Text Area to the Keyboard. The pressed characters will be put there.
|
|
* @param kb pointer to a Keyboard object
|
|
* @return pointer to the assigned Text Area object
|
|
*/
|
|
lv_obj_t * lv_kb_get_ta(lv_obj_t * kb)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
return ext->ta;
|
|
}
|
|
|
|
/**
|
|
* Set a new a mode (text or number map)
|
|
* @param kb pointer to a Keyboard object
|
|
* @return the current mode from 'lv_kb_mode_t'
|
|
*/
|
|
lv_kb_mode_t lv_kb_get_mode(lv_obj_t * kb)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
return ext->mode;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the current cursor manage mode.
|
|
* @param kb pointer to a Keyboard object
|
|
* @return true: show cursor on the current text area, false: hide cursor
|
|
*/
|
|
bool lv_kb_get_cursor_manage(lv_obj_t * kb, bool en)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
return ext->cur_mng == 0 ? false : true;
|
|
}
|
|
|
|
/**
|
|
* Get the callback to call when the "Ok" button is pressed
|
|
* @param kb pointer to Keyboard object
|
|
* @return the ok callback
|
|
*/
|
|
lv_action_t lv_kb_get_ok_action(lv_obj_t * kb, lv_action_t action)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
return ext->ok_action;
|
|
}
|
|
|
|
/**
|
|
* Get the callback to call when the "Hide" button is pressed
|
|
* @param kb pointer to Keyboard object
|
|
* @return the close callback
|
|
*/
|
|
lv_action_t lv_kb_get_close_action(lv_obj_t * kb, lv_action_t action)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
return ext->close_action;
|
|
}
|
|
|
|
|
|
/**********************
|
|
* STATIC FUNCTIONS
|
|
**********************/
|
|
|
|
/**
|
|
* Signal function of the keyboard
|
|
* @param kb pointer to a keyboard object
|
|
* @param sign a signal type from lv_signal_t enum
|
|
* @param param pointer to a signal specific variable
|
|
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
|
|
*/
|
|
static lv_res_t lv_kb_signal(lv_obj_t * kb, lv_signal_t sign, void * param)
|
|
{
|
|
lv_res_t res;
|
|
|
|
/* Include the ancient signal function */
|
|
res = ancestor_signal(kb, sign, param);
|
|
if(res != LV_RES_OK) return res;
|
|
|
|
if(sign == LV_SIGNAL_CLEANUP) {
|
|
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
/**
|
|
* Called when a button of 'kb_btnm' is released
|
|
* @param btnm pointer to 'kb_btnm'
|
|
* @param i the index of the released button from the current btnm map
|
|
* @return LV_ACTION_RES_INV if the btnm is deleted else LV_ACTION_RES_OK
|
|
*/
|
|
static lv_res_t lv_app_kb_action(lv_obj_t * kb, const char * txt)
|
|
{
|
|
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
|
|
|
|
/*Do the corresponding action according to the text of the button*/
|
|
if(strcmp(txt, "abc") == 0) lv_btnm_set_map(kb, kb_map_lc);
|
|
else if(strcmp(txt, "ABC") == 0) lv_btnm_set_map(kb, kb_map_uc);
|
|
else if(strcmp(txt, "1#") == 0) lv_btnm_set_map(kb, kb_map_spec);
|
|
else if(strcmp(txt, SYMBOL_CLOSE) == 0) {
|
|
if(ext->close_action) ext->close_action(kb);
|
|
else lv_obj_del(kb);
|
|
return LV_RES_INV;
|
|
} else if(strcmp(txt, SYMBOL_OK) == 0) {
|
|
if(ext->ok_action) ext->ok_action(kb);
|
|
else lv_obj_del(kb);
|
|
return LV_RES_INV;
|
|
}
|
|
|
|
if(ext->ta == NULL) return LV_RES_OK;
|
|
|
|
if(strcmp(txt, "Enter") == 0)lv_ta_add_char(ext->ta, '\n');
|
|
else if(strcmp(txt, SYMBOL_LEFT) == 0) lv_ta_cursor_left(ext->ta);
|
|
else if(strcmp(txt, SYMBOL_RIGHT) == 0) lv_ta_cursor_right(ext->ta);
|
|
else if(strcmp(txt, "Del") == 0) lv_ta_del(ext->ta);
|
|
else if(strcmp(txt, "+/-") == 0) {
|
|
uint16_t cur = lv_ta_get_cursor_pos(ext->ta);
|
|
const char * ta_txt = lv_ta_get_text(ext->ta);
|
|
if(ta_txt[0] == '-') {
|
|
lv_ta_set_cursor_pos(ext->ta, 1);
|
|
lv_ta_del(ext->ta);
|
|
lv_ta_add_char(ext->ta, '+');
|
|
lv_ta_set_cursor_pos(ext->ta, cur);
|
|
} else if(ta_txt[0] == '+') {
|
|
lv_ta_set_cursor_pos(ext->ta, 1);
|
|
lv_ta_del(ext->ta);
|
|
lv_ta_add_char(ext->ta, '-');
|
|
lv_ta_set_cursor_pos(ext->ta, cur);
|
|
} else {
|
|
lv_ta_set_cursor_pos(ext->ta, 0);
|
|
lv_ta_add_char(ext->ta, '-');
|
|
lv_ta_set_cursor_pos(ext->ta, cur + 1);
|
|
}
|
|
} else {
|
|
lv_ta_add_text(ext->ta, txt);
|
|
}
|
|
return LV_RES_OK;
|
|
}
|
|
|
|
#endif
|