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

392 lines
12 KiB
C
Raw Normal View History

2016-06-22 17:24:02 +02:00
/**
* @file lv_list.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_conf.h"
#if USE_LV_LIST != 0
#include "lv_list.h"
2017-04-13 16:12:03 +02:00
#include <lvgl/lv_objx/lv_cont.h>
2016-07-12 01:16:27 +02:00
#include "misc/math/math_base.h"
2016-06-22 17:24:02 +02:00
/*********************
* DEFINES
*********************/
2017-04-13 16:12:03 +02:00
#define LV_LIST_LAYOUT_DEF LV_CONT_LAYOUT_COL_M
2016-06-22 17:24:02 +02:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2016-07-12 15:05:17 +02:00
#if 0
2016-10-07 11:15:46 +02:00
static bool lv_list_design(lv_obj_t * list, const area_t * mask, lv_design_mode_t mode);
2016-07-12 15:05:17 +02:00
#endif
2016-06-22 17:24:02 +02:00
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/*-----------------
* Create function
*-----------------*/
/**
* Create a list objects
2016-10-07 11:15:46 +02:00
* @param par pointer to an object, it will be the parent of the new list
* @param copy pointer to a list object, if not NULL then the new object will be copied from it
2016-06-22 17:24:02 +02:00
* @return pointer to the created list
*/
2016-10-07 11:15:46 +02:00
lv_obj_t * lv_list_create(lv_obj_t * par, lv_obj_t * copy)
2016-06-22 17:24:02 +02:00
{
/*Create the ancestor basic object*/
2016-10-07 11:15:46 +02:00
lv_obj_t * new_list = lv_page_create(par, copy);
dm_assert(new_list);
lv_list_ext_t * ext = lv_obj_alloc_ext(new_list, sizeof(lv_list_ext_t));
dm_assert(ext);
2017-04-21 09:15:39 +02:00
ext->sb_out = 0;
ext->style_img = NULL;
ext->styles_btn[LV_BTN_STATE_REL] = lv_style_get(LV_STYLE_BTN_REL, NULL);
ext->styles_btn[LV_BTN_STATE_PR] = lv_style_get(LV_STYLE_BTN_PR, NULL);
ext->styles_btn[LV_BTN_STATE_TREL] = lv_style_get(LV_STYLE_BTN_TREL, NULL);
ext->styles_btn[LV_BTN_STATE_PR] = lv_style_get(LV_STYLE_BTN_TPR, NULL);
ext->styles_btn[LV_BTN_STATE_INA] = lv_style_get(LV_STYLE_BTN_INA, NULL);
2016-10-07 11:15:46 +02:00
lv_obj_set_signal_f(new_list, lv_list_signal);
2016-06-22 17:24:02 +02:00
/*Init the new list object*/
2016-10-07 11:15:46 +02:00
if(copy == NULL) {
2017-04-21 09:15:39 +02:00
lv_obj_set_size(new_list, 2 * LV_DPI, 3 * LV_DPI);
2017-04-13 16:12:03 +02:00
lv_cont_set_layout(ext->page.scrl, LV_LIST_LAYOUT_DEF);
lv_obj_set_style(new_list, lv_style_get(LV_STYLE_TRANSP_TIGHT, NULL));
lv_obj_set_style(lv_page_get_scrl(new_list), lv_style_get(LV_STYLE_PRETTY, NULL));
lv_page_set_sb_mode(new_list, LV_PAGE_SB_MODE_AUTO);
} else {
2017-04-21 09:15:39 +02:00
lv_list_ext_t * copy_ext = lv_obj_get_ext(copy);
lv_list_set_styles_btn(new_list, copy_ext->styles_btn[LV_BTN_STATE_REL],
copy_ext->styles_btn[LV_BTN_STATE_PR],
copy_ext->styles_btn[LV_BTN_STATE_TREL],
copy_ext->styles_btn[LV_BTN_STATE_TPR],
copy_ext->styles_btn[LV_BTN_STATE_INA]);
lv_list_set_style_img(new_list, copy_ext->style_img);
lv_list_set_sb_out(new_list, copy_ext->sb_out);
/*Refresh the style with new signal function*/
lv_obj_refr_style(new_list);
2016-07-12 01:16:27 +02:00
}
2016-06-22 17:24:02 +02:00
2016-10-07 11:15:46 +02:00
return new_list;
2016-06-22 17:24:02 +02:00
}
/**
* Signal function of the list
2016-10-07 11:15:46 +02:00
* @param list pointer to a list object
2016-06-22 17:24:02 +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_list_signal(lv_obj_t * list, lv_signal_t sign, void * param)
2016-06-22 17:24:02 +02:00
{
bool valid;
/* Include the ancient signal function */
2016-10-07 11:15:46 +02:00
valid = lv_page_signal(list, sign, param);
2016-06-22 17:24:02 +02:00
return valid;
}
2016-07-12 01:16:27 +02:00
/**
* Add a list element to the list
2016-10-07 11:15:46 +02:00
* @param list pointer to list object
2016-07-12 01:16:27 +02:00
* @param img_fn file name of an image before the text (NULL if unused)
* @param txt text of the list element (NULL if unused)
* @param rel_action pointer to release action function (like with lv_btn)
* @return pointer to the new list element which can be customized (a button)
*/
lv_obj_t * lv_list_add(lv_obj_t * list, const char * img_fn, const char * txt, lv_action_t rel_action)
2016-06-22 17:24:02 +02:00
{
lv_style_t * style = lv_obj_get_style(list);
lv_list_ext_t * ext = lv_obj_get_ext(list);
2016-07-12 01:16:27 +02:00
/*Create a list element with the image an the text*/
2016-06-22 17:24:02 +02:00
lv_obj_t * liste;
2016-10-07 11:15:46 +02:00
liste = lv_btn_create(list, NULL);
2017-04-21 09:15:39 +02:00
lv_btn_set_styles(liste, ext->styles_btn[LV_BTN_STATE_REL], ext->styles_btn[LV_BTN_STATE_PR],
ext->styles_btn[LV_BTN_STATE_TREL], ext->styles_btn[LV_BTN_STATE_TPR],
ext->styles_btn[LV_BTN_STATE_INA]);
2016-07-12 01:16:27 +02:00
lv_btn_set_rel_action(liste, rel_action);
2016-06-22 17:24:02 +02:00
lv_page_glue_obj(liste, true);
2017-04-13 16:12:03 +02:00
lv_cont_set_layout(liste, LV_CONT_LAYOUT_ROW_M);
lv_cont_set_fit(liste, false, true);
2016-06-22 20:39:07 +02:00
/*Make the size adjustment*/
cord_t w = lv_obj_get_width(list);
lv_style_t * style_scrl = lv_obj_get_style(lv_page_get_scrl(list));
cord_t hpad_tot = style->hpad + style_scrl->hpad;
w -= hpad_tot * 2;
/*Make place for the scrollbar if hpad_tot is too small*/
if(ext->sb_out != 0) {
if(hpad_tot < ext->page.sb_width) w -= ext->page.sb_width - hpad_tot;
}
lv_obj_set_width(liste, w);
if(img_fn != NULL && img_fn[0] != '\0') {
2016-07-11 16:16:53 +02:00
lv_obj_t * img = lv_img_create(liste, NULL);
lv_img_set_file(img, img_fn);
2017-04-21 09:15:39 +02:00
lv_obj_set_style(img, ext->style_img);
2016-07-12 01:16:27 +02:00
lv_obj_set_click(img, false);
}
if(txt != NULL) {
lv_obj_t * label = lv_label_create(liste, NULL);
lv_label_set_text(label, txt);
2017-04-21 09:15:39 +02:00
lv_obj_set_style(label, ext->styles_btn[LV_BTN_STATE_REL]);
2016-07-12 01:16:27 +02:00
lv_obj_set_click(label, false);
2017-06-20 15:14:54 +02:00
lv_obj_set_width(label, liste->cords.x2 - label->cords.x1);
2017-06-26 14:22:56 +02:00
lv_label_set_long_mode(label, LV_LABEL_LONG_ROLL);
2016-06-22 20:39:07 +02:00
}
2016-07-12 01:16:27 +02:00
return liste;
2016-06-22 17:24:02 +02:00
}
2016-07-12 01:16:27 +02:00
/**
* Move the list elements up by one
2016-10-07 11:15:46 +02:00
* @param list pointer a to list object
2016-07-12 01:16:27 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_list_up(lv_obj_t * list)
2016-07-12 01:16:27 +02:00
{
/*Search the first list element which 'y' coordinate is below the parent
* and position the list to show this element on the bottom*/
2016-10-07 11:15:46 +02:00
lv_obj_t * h = lv_obj_get_parent(list);
2016-07-12 01:16:27 +02:00
lv_obj_t * e;
lv_obj_t * e_prev = NULL;
2016-10-07 11:15:46 +02:00
e = lv_obj_get_child(list, NULL);
2016-07-12 01:16:27 +02:00
while(e != NULL) {
if(e->cords.y2 <= h->cords.y2) {
if(e_prev != NULL)
2016-10-07 11:15:46 +02:00
lv_obj_set_y(list, lv_obj_get_height(h) -
2016-07-12 01:16:27 +02:00
(lv_obj_get_y(e_prev) + lv_obj_get_height(e_prev)));
break;
}
e_prev = e;
2016-10-07 11:15:46 +02:00
e = lv_obj_get_child(list, e);
2016-07-12 01:16:27 +02:00
}
}
/**
* Move the list elements down by one
2016-10-07 11:15:46 +02:00
* @param list pointer to a list object
2016-07-12 01:16:27 +02:00
*/
2016-10-07 11:15:46 +02:00
void lv_list_down(lv_obj_t * list)
2016-07-12 01:16:27 +02:00
{
/*Search the first list element which 'y' coordinate is above the parent
* and position the list to show this element on the top*/
2016-10-07 11:15:46 +02:00
lv_obj_t * h = lv_obj_get_parent(list);
2016-07-12 01:16:27 +02:00
lv_obj_t * e;
2016-10-07 11:15:46 +02:00
e = lv_obj_get_child(list, NULL);
2016-07-12 01:16:27 +02:00
while(e != NULL) {
if(e->cords.y1 < h->cords.y1) {
2016-10-07 11:15:46 +02:00
lv_obj_set_y(list, -lv_obj_get_y(e));
2016-07-12 01:16:27 +02:00
break;
}
2016-10-07 11:15:46 +02:00
e = lv_obj_get_child(list, e);
2016-07-12 01:16:27 +02:00
}
}
2016-06-22 17:24:02 +02:00
/*=====================
* Setter functions
*====================*/
2017-04-21 09:15:39 +02:00
/**
* Enable/Disable to scrollbar outside attribute
* @param list pointer to list object
* @param out true: reduce the buttons width therefore scroll bar will be out of the buttons,
* false: keep button size and place scroll bar on the buttons
*/
void lv_list_set_sb_out(lv_obj_t * list, bool out)
{
lv_list_ext_t * ext = lv_obj_get_ext(list);
ext->sb_out = out == false ? 0 : 1;
}
/**
* Enable or disable the text rolling on a list element
* @param liste pinter to list element
* @param en true: enable text scrolling, false: disable text scrolling
*/
void lv_list_set_element_text_roll(lv_obj_t * liste, bool en)
{
/*The last child is the label*/
lv_obj_t * label = lv_obj_get_child(liste, NULL);
if(en == false) {
lv_label_set_long_mode(label, LV_LABEL_LONG_DOTS);
} else {
lv_obj_set_width(label, liste->cords.x2 - label->cords.x1);
lv_label_set_long_mode(label, LV_LABEL_LONG_ROLL);
}
}
/**
* Set styles of the list elements of a list in each state
* @param list pointer to list object
* @param rel pointer to a style for releases state
* @param pr pointer to a style for pressed state
* @param trel pointer to a style for toggled releases state
* @param tpr pointer to a style for toggled pressed state
* @param ina pointer to a style for inactive state
*/
2017-04-21 09:15:39 +02:00
void lv_list_set_styles_btn(lv_obj_t * list, lv_style_t * rel, lv_style_t * pr,
lv_style_t * trel, lv_style_t * tpr,
lv_style_t * ina)
{
lv_list_ext_t * ext = lv_obj_get_ext(list);
2017-04-21 09:15:39 +02:00
ext->styles_btn[LV_BTN_STATE_REL] = rel;
ext->styles_btn[LV_BTN_STATE_PR] = pr;
ext->styles_btn[LV_BTN_STATE_TREL] = trel;
ext->styles_btn[LV_BTN_STATE_TPR] = tpr;
ext->styles_btn[LV_BTN_STATE_INA] = ina;
lv_obj_t * scrl = lv_page_get_scrl(list);
lv_obj_t * liste = lv_obj_get_child(scrl, NULL);
while(liste != NULL)
{
lv_btn_set_styles(liste, rel, pr, trel, tpr, ina);
liste = lv_obj_get_child(scrl, liste);
}
2017-04-21 09:15:39 +02:00
}
2017-04-21 09:15:39 +02:00
/**
* Set the styles of the list element image (typically to set symbol font)
* @param list pointer to list object
* @param style pointer to the new style of the button images
*/
void lv_list_set_style_img(lv_obj_t * list, lv_style_t * style)
{
lv_list_ext_t * ext = lv_obj_get_ext(list);
ext->style_img = style;
lv_obj_t * scrl = lv_page_get_scrl(list);
lv_obj_t * liste = lv_obj_get_child(scrl, NULL);
lv_obj_t * img;
while(liste != NULL)
{
img = lv_obj_get_child(liste, NULL); /*Now img = the label*/
img = lv_obj_get_child(liste, img); /*Now img = the image (if ULL then no image) */
if(img != NULL) lv_obj_set_style(img, style);
liste = lv_obj_get_child(scrl, liste);
}
}
2016-06-22 17:24:02 +02:00
/*=====================
* Getter functions
*====================*/
/**
* Get the text of a list element
* @param liste pointer to list element
* @return pointer to the text
*/
2017-04-28 16:12:35 +02:00
const char * lv_list_get_element_text(lv_obj_t * liste)
{
/*The last child is the label*/
lv_obj_t * label = lv_obj_get_child(liste, NULL);
return lv_label_get_text(label);
}
2017-04-21 09:15:39 +02:00
/**
* Get the scroll bar outside attribute
* @param list pointer to list object
* @param en true: scroll bar outside the buttons, false: scroll bar inside
*/
bool lv_list_get_sb_out(lv_obj_t * list, bool en)
{
lv_list_ext_t * ext = lv_obj_get_ext(list);
return ext->sb_out == 0 ? false : true;
}
/**
* Get the style of the list elements in a given state
2017-04-21 09:15:39 +02:00
* @param list pointer to a list object
* @param state a state from 'lv_btn_state_t' in which style should be get
* @return pointer to the style in the given state
*/
lv_style_t * lv_list_get_style_liste(lv_obj_t * list, lv_btn_state_t state)
{
lv_list_ext_t * ext = lv_obj_get_ext(list);
2017-04-21 09:15:39 +02:00
if(ext->styles_btn[state] == NULL) return lv_obj_get_style(list);
2017-04-21 09:15:39 +02:00
return ext->styles_btn[state];
}
2017-04-21 09:15:39 +02:00
/**
* Get the style of the list elements images
* @param list pointer to a list object
* @return pointer to the image style
*/
lv_style_t * lv_list_get_style_img(lv_obj_t * list, lv_btn_state_t state)
{
lv_list_ext_t * ext = lv_obj_get_ext(list);
if(ext->style_img == NULL) return lv_list_get_style_liste(list, LV_BTN_STATE_REL);
return ext->style_img;
}
2016-06-22 17:24:02 +02:00
/**********************
* STATIC FUNCTIONS
**********************/
2016-07-12 15:05:17 +02:00
#if 0 /*A new design function is not necessary*/
2016-06-22 17:24:02 +02:00
/**
* Handle the drawing related tasks of the lists
2016-10-07 11:15:46 +02:00
* @param list pointer to an object
2016-06-22 17:24:02 +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-22 17:24:02 +02:00
* @param return true/false, depends on 'mode'
*/
2016-10-07 11:15:46 +02:00
static bool lv_list_design(lv_obj_t * list, const area_t * mask, lv_design_mode_t mode)
2016-06-22 17:24:02 +02:00
{
if(mode == LV_DESIGN_COVER_CHK) {
/*Return false if the object is not covers the mask_p area*/
return false;
}
/*Draw the object*/
return true;
}
2016-07-12 15:05:17 +02:00
#endif
2016-06-22 17:24:02 +02:00
#endif