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"
|
2016-06-22 20:39:07 +02:00
|
|
|
#include "lv_rect.h"
|
2016-07-12 01:16:27 +02:00
|
|
|
#include "misc/math/math_base.h"
|
2016-06-22 17:24:02 +02:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2016-06-22 21:52:39 +02:00
|
|
|
#define LV_LIST_LAYOUT_DEF LV_RECT_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-10-04 15:19:07 +02:00
|
|
|
static void lv_lists_init(void);
|
2016-06-22 17:24:02 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
2016-10-04 15:19:07 +02:00
|
|
|
static lv_lists_t lv_lists_def;
|
|
|
|
static lv_lists_t lv_lists_tight;
|
2016-06-22 17:24:02 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* 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));
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_set_signal_f(new_list, lv_list_signal);
|
2016-10-04 09:45:39 +02:00
|
|
|
|
2016-06-22 17:24:02 +02:00
|
|
|
/*Init the new list object*/
|
2016-10-07 11:15:46 +02:00
|
|
|
if(copy == NULL) {
|
|
|
|
ext ->fit = LV_LIST_FIT_LONGEST;
|
|
|
|
lv_obj_set_size_us(new_list, 100, 150);
|
|
|
|
lv_obj_set_style(new_list, lv_lists_get(LV_LISTS_DEF, NULL));
|
|
|
|
lv_rect_set_layout(LV_EA(new_list, lv_list_ext_t)->page_ext.scrolling, LV_LIST_LAYOUT_DEF);
|
2016-10-04 09:45:39 +02:00
|
|
|
} else {
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_list_ext_t * copy_ext = lv_obj_get_ext(copy);
|
|
|
|
ext ->fit = copy_ext->fit;
|
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
|
|
|
|
|
|
|
/* The object can be deleted so check its validity and then
|
|
|
|
* make the object specific signal handling */
|
|
|
|
if(valid != false) {
|
|
|
|
switch(sign) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_t * lv_list_add(lv_obj_t * list, const char * img_fn, const char * txt, bool (*rel_action)(lv_obj_t *, lv_dispi_t *))
|
2016-06-22 17:24:02 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_lists_t * lists = 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);
|
|
|
|
lv_obj_set_style(liste, &lists->liste_btn);
|
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);
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_rect_set_layout(liste, lists->liste_layout);
|
2016-07-12 01:16:27 +02:00
|
|
|
lv_rect_set_fit(liste, true, true); /*hor. fit might be disabled later*/
|
2016-06-22 20:39:07 +02:00
|
|
|
|
2016-07-11 16:16:53 +02:00
|
|
|
if(img_fn != NULL) {
|
|
|
|
lv_obj_t * img = lv_img_create(liste, NULL);
|
|
|
|
lv_img_set_file(img, img_fn);
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_set_style(img, &lists->liste_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);
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_obj_set_style(label,&lists->liste_label);
|
2016-07-12 01:16:27 +02:00
|
|
|
lv_obj_set_click(label, false);
|
2016-06-22 20:39:07 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 13:43:01 +02:00
|
|
|
/*Make the size adjustment*/
|
2016-10-07 11:15:46 +02:00
|
|
|
if(ext->fit == LV_LIST_FIT_HOLDER) {
|
2016-07-12 01:16:27 +02:00
|
|
|
/*Now the width will be adjusted*/
|
|
|
|
lv_rect_set_fit(liste, false, true);
|
2016-10-07 11:15:46 +02:00
|
|
|
cord_t w = lv_obj_get_width(list);
|
|
|
|
w -= lists->bg_page.bg_rects.hpad * 2;
|
2016-07-12 01:16:27 +02:00
|
|
|
lv_obj_set_width(liste, w);
|
2016-10-07 11:15:46 +02:00
|
|
|
} else if(ext->fit == LV_LIST_FIT_LONGEST) {
|
2016-09-27 13:43:01 +02:00
|
|
|
/*In this case the width will be adjusted*/
|
2016-07-12 01:16:27 +02:00
|
|
|
lv_rect_set_fit(liste, false, true);
|
|
|
|
|
|
|
|
lv_obj_t * e;
|
|
|
|
cord_t w = 0;
|
|
|
|
/*Get the longest list element*/
|
2016-10-04 09:45:39 +02:00
|
|
|
lv_obj_t * e_par = lv_obj_get_parent(liste); /*The page changes the parent so get it*/
|
|
|
|
e = lv_obj_get_child(e_par, NULL);
|
2016-07-12 01:16:27 +02:00
|
|
|
while(e != NULL) {
|
|
|
|
w = max(w, lv_obj_get_width(e));
|
2016-10-04 09:45:39 +02:00
|
|
|
e = lv_obj_get_child(e_par, e);
|
2016-07-12 01:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Set all list element to the longest width*/
|
2016-10-04 09:45:39 +02:00
|
|
|
e = lv_obj_get_child(e_par, NULL);
|
2016-07-12 01:16:27 +02:00
|
|
|
while(e != NULL) {
|
|
|
|
if(lv_obj_get_width(e) != w) {
|
|
|
|
lv_obj_set_width(e, w);
|
|
|
|
}
|
2016-10-04 09:45:39 +02:00
|
|
|
e = lv_obj_get_child(e_par, e);
|
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
|
|
|
|
*====================*/
|
|
|
|
|
2016-07-12 01:16:27 +02:00
|
|
|
/**
|
|
|
|
* Set the list element fitting of a list
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param list pointer to a list object
|
2016-07-12 01:16:27 +02:00
|
|
|
* @param fit type of fitting (from lv_list_fit_t)
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
void lv_list_set_fit(lv_obj_t * list, lv_list_fit_t fit)
|
2016-07-12 01:16:27 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_list_ext_t * ext = lv_obj_get_ext(list);
|
2016-07-12 01:16:27 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
ext->fit = fit;
|
2016-07-12 01:16:27 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 17:24:02 +02:00
|
|
|
|
|
|
|
/*=====================
|
|
|
|
* Getter functions
|
|
|
|
*====================*/
|
|
|
|
|
2016-07-12 01:16:27 +02:00
|
|
|
/**
|
|
|
|
* Get the fit type of a list
|
2016-10-07 11:15:46 +02:00
|
|
|
* @param list pointer to list object
|
2016-07-12 01:16:27 +02:00
|
|
|
* @return the fit (from lv_list_fit_t)
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_list_fit_t lv_list_get_fit(lv_obj_t * list)
|
2016-07-12 01:16:27 +02:00
|
|
|
{
|
2016-10-07 11:15:46 +02:00
|
|
|
return LV_EA(list, lv_list_ext_t)->fit;
|
2016-07-12 01:16:27 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +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_lists_builtin_t enum
|
|
|
|
* @param copy_p copy the style to this variable. (NULL if unused)
|
|
|
|
* @return pointer to an lv_lists_t style
|
|
|
|
*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_lists_t * lv_lists_get(lv_lists_builtin_t style, lv_lists_t * list)
|
2016-10-04 09:45:39 +02:00
|
|
|
{
|
2016-10-04 15:19:07 +02:00
|
|
|
static bool style_inited = false;
|
|
|
|
|
|
|
|
/*Make the style initialization if it is not done yet*/
|
|
|
|
if(style_inited == false) {
|
|
|
|
lv_lists_init();
|
|
|
|
style_inited = true;
|
|
|
|
}
|
|
|
|
|
2016-10-04 09:45:39 +02:00
|
|
|
lv_lists_t *style_p;
|
|
|
|
|
|
|
|
switch(style) {
|
|
|
|
case LV_LISTS_DEF:
|
|
|
|
case LV_LISTS_GAP:
|
|
|
|
style_p = &lv_lists_def;
|
|
|
|
break;
|
|
|
|
case LV_LISTS_TIGHT:
|
|
|
|
style_p = &lv_lists_tight;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
style_p = &lv_lists_def;
|
|
|
|
}
|
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
if(list != NULL) {
|
|
|
|
if(style_p != NULL) memcpy(list, style_p, sizeof(lv_lists_t));
|
|
|
|
else memcpy(list, &lv_lists_def, sizeof(lv_lists_t));
|
2016-10-04 09:45:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return style_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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')
|
2016-10-04 09:45:39 +02:00
|
|
|
* 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
|
|
|
|
2016-10-04 15:19:07 +02:00
|
|
|
/**
|
|
|
|
* Initialize the list styles
|
|
|
|
*/
|
|
|
|
static void lv_lists_init(void)
|
|
|
|
{
|
|
|
|
/*Default style*/
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_pages_get(LV_PAGES_TRANSP, &lv_lists_def.bg_page);
|
|
|
|
lv_lists_def.bg_page.bg_rects.vpad = 0 * LV_STYLE_MULT;
|
|
|
|
lv_lists_def.bg_page.bg_rects.hpad = 0 * LV_STYLE_MULT;
|
|
|
|
lv_lists_def.bg_page.bg_rects.opad = 0 * LV_STYLE_MULT;
|
2016-10-04 15:19:07 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_lists_def.bg_page.scrable_rects.vpad = 10 * LV_STYLE_MULT;
|
|
|
|
lv_lists_def.bg_page.scrable_rects.hpad = 10 * LV_STYLE_MULT;
|
|
|
|
lv_lists_def.bg_page.scrable_rects.opad = 5 * LV_STYLE_MULT;
|
2016-10-04 15:19:07 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_btns_get(LV_BTNS_DEF, &lv_lists_def.liste_btn); /*List element button style*/
|
2016-10-04 15:19:07 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_labels_get(LV_LABELS_BTN, &lv_lists_def.liste_label); /*List element label style*/
|
|
|
|
lv_lists_def.liste_label.mid = 0;
|
2016-10-04 15:19:07 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_imgs_get(LV_IMGS_DEF, &lv_lists_def.liste_img); /*Lit element image style*/
|
2016-06-22 17:24:02 +02:00
|
|
|
|
2016-10-04 15:19:07 +02:00
|
|
|
lv_lists_def.liste_layout = LV_RECT_LAYOUT_ROW_M;
|
|
|
|
|
|
|
|
memcpy(&lv_lists_tight, &lv_lists_def, sizeof(lv_lists_t));
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_lists_tight.bg_page.bg_rects.vpad = 0 * LV_STYLE_MULT;
|
|
|
|
lv_lists_tight.bg_page.bg_rects.hpad = 0 * LV_STYLE_MULT;
|
|
|
|
lv_lists_tight.bg_page.bg_rects.opad = 0 * LV_STYLE_MULT;
|
2016-10-04 15:19:07 +02:00
|
|
|
|
2016-10-07 11:15:46 +02:00
|
|
|
lv_lists_tight.bg_page.scrable_rects.vpad = 0 * LV_STYLE_MULT;
|
|
|
|
lv_lists_tight.bg_page.scrable_rects.hpad = 0 * LV_STYLE_MULT;
|
|
|
|
lv_lists_tight.bg_page.scrable_rects.opad = 0 * LV_STYLE_MULT;
|
2016-10-04 15:19:07 +02:00
|
|
|
|
|
|
|
}
|
2016-06-22 17:24:02 +02:00
|
|
|
#endif
|