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

632 lines
18 KiB
C
Raw Normal View History

2016-06-08 07:25:08 +02:00
/**
* @file lv_cont.c
2016-06-08 07:25:08 +02:00
*
*/
/*********************
* INCLUDES
*********************/
2016-10-07 11:15:46 +02:00
#include "lv_conf.h"
2017-04-24 16:16:36 +02:00
#if USE_LV_CONT != 0
2016-06-22 21:52:39 +02:00
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
2017-04-21 09:15:39 +02:00
#include "lv_cont.h"
2016-06-08 07:25:08 +02:00
#include "../lv_draw/lv_draw.h"
#include "../lv_draw/lv_draw_vbasic.h"
2017-04-21 09:15:39 +02:00
#include "misc/gfx/area.h"
2016-10-07 11:15:46 +02:00
2017-04-21 09:15:39 +02:00
#include "misc/gfx/color.h"
2016-06-15 09:38:20 +02:00
#include "misc/math/math_base.h"
2016-06-08 07:25:08 +02:00
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
#if 0
2017-04-13 16:12:03 +02:00
static bool lv_cont_design(lv_obj_t * cont, const area_t * mask, lv_design_mode_t mode);
#endif
2017-04-13 16:12:03 +02:00
static void lv_cont_refr_layout(lv_obj_t * cont);
static void lv_cont_layout_col(lv_obj_t * cont);
static void lv_cont_layout_row(lv_obj_t * cont);
static void lv_cont_layout_center(lv_obj_t * cont);
static void lv_cont_layout_pretty(lv_obj_t * cont);
static void lv_cont_layout_grid(lv_obj_t * cont);
static void lv_cont_refr_autofit(lv_obj_t * cont);
2016-06-08 07:25:08 +02:00
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/*-----------------
* Create function
*-----------------*/
/**
2017-04-13 16:12:03 +02:00
* Create a container objects
* @param par pointer to an object, it will be the parent of the new container
* @param copy pointer to a container object, if not NULL then the new object will be copied from it
* @return pointer to the created container
2016-06-08 07:25:08 +02:00
*/
2017-04-13 16:12:03 +02:00
lv_obj_t * lv_cont_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_rect = lv_obj_create(par, copy);
dm_assert(new_rect);
2017-04-13 16:12:03 +02:00
lv_obj_alloc_ext(new_rect, sizeof(lv_cont_ext_t));
lv_cont_ext_t * ext = lv_obj_get_ext(new_rect);
dm_assert(ext);
2017-04-21 09:15:39 +02:00
ext->hfit_en = 0;
ext->vfit_en = 0;
2017-04-13 16:12:03 +02:00
ext->layout = LV_CONT_LAYOUT_OFF;
2017-04-13 16:12:03 +02:00
lv_obj_set_signal_f(new_rect, lv_cont_signal);
2016-06-11 13:41:35 +02:00
2017-04-13 16:12:03 +02:00
/*Init the new container*/
2016-10-07 11:15:46 +02:00
if(copy == NULL) {
lv_obj_set_style(new_rect, lv_style_get(LV_STYLE_PLAIN, NULL));
2016-10-07 11:15:46 +02:00
}
/*Copy an existing object*/
else {
2017-04-13 16:12:03 +02:00
lv_cont_ext_t * copy_ext = lv_obj_get_ext(copy);
2017-04-21 09:15:39 +02:00
ext->hfit_en = copy_ext->hfit_en;
ext->vfit_en = copy_ext->vfit_en;
ext->layout = copy_ext->layout;
/*Refresh the style with new signal function*/
lv_obj_refr_style(new_rect);
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
return new_rect;
2016-06-08 07:25:08 +02:00
}
2016-10-07 11:15:46 +02:00
2016-06-08 07:25:08 +02:00
/**
2017-04-13 16:12:03 +02:00
* Signal function of the container
* @param cont pointer to a container 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
*/
2017-04-13 16:12:03 +02:00
bool lv_cont_signal(lv_obj_t * cont, lv_signal_t sign, void * param)
2016-06-08 07:25:08 +02:00
{
bool valid;
/* Include the ancient signal function */
2017-04-13 16:12:03 +02:00
valid = lv_obj_signal(cont, 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) {
switch(sign) {
2016-06-15 09:38:20 +02:00
case LV_SIGNAL_STYLE_CHG: /*Recalculate the padding if the style changed*/
2017-04-13 16:12:03 +02:00
lv_cont_refr_layout(cont);
lv_cont_refr_autofit(cont);
break;
2016-06-15 09:38:20 +02:00
case LV_SIGNAL_CHILD_CHG:
2017-04-13 16:12:03 +02:00
lv_cont_refr_layout(cont);
lv_cont_refr_autofit(cont);
2016-06-22 20:39:07 +02:00
break;
case LV_SIGNAL_CORD_CHG:
2017-04-13 16:12:03 +02:00
if(lv_obj_get_width(cont) != area_get_width(param) ||
lv_obj_get_height(cont) != area_get_height(param)) {
lv_cont_refr_layout(cont);
lv_cont_refr_autofit(cont);
2016-06-15 09:38:20 +02:00
}
2016-06-22 20:39:07 +02:00
break;
2016-06-08 07:25:08 +02:00
default:
break;
}
}
return valid;
}
2016-06-15 09:38:20 +02:00
/*=====================
* Setter functions
*====================*/
2016-10-07 11:15:46 +02:00
/**
2017-04-13 16:12:03 +02:00
* Set the layout on a container
* @param cont pointer to a container object
* @param layout a layout from 'lv_cont_layout_t'
2016-10-07 11:15:46 +02:00
*/
2017-04-13 16:12:03 +02:00
void lv_cont_set_layout(lv_obj_t * cont, lv_cont_layout_t layout)
2016-06-22 20:39:07 +02:00
{
2017-04-13 16:12:03 +02:00
lv_cont_ext_t * ext = lv_obj_get_ext(cont);
2016-10-07 11:15:46 +02:00
ext->layout = layout;
2016-06-22 20:39:07 +02:00
2016-10-07 11:15:46 +02:00
/*Send a signal to refresh the layout*/
2017-04-13 16:12:03 +02:00
cont->signal_f(cont, LV_SIGNAL_CHILD_CHG, NULL);
2016-06-22 20:39:07 +02:00
}
2016-06-15 09:38:20 +02:00
/**
2016-10-07 11:15:46 +02:00
* Enable the horizontal or vertical fit.
2017-04-13 16:12:03 +02:00
* The container size will be set to involve the children horizontally or vertically.
* @param cont pointer to a container object
2016-06-15 09:38:20 +02:00
* @param hor_en true: enable the horizontal padding
* @param ver_en true: enable the vertical padding
*/
2017-04-13 16:12:03 +02:00
void lv_cont_set_fit(lv_obj_t * cont, bool hor_en, bool ver_en)
2016-06-15 09:38:20 +02:00
{
2017-04-13 16:12:03 +02:00
lv_obj_inv(cont);
lv_cont_ext_t * ext = lv_obj_get_ext(cont);
2017-04-21 09:15:39 +02:00
ext->hfit_en = hor_en == false ? 0 : 1;
ext->vfit_en = ver_en == false ? 0 : 1;
2016-06-15 09:38:20 +02:00
2016-10-07 11:15:46 +02:00
/*Send a signal to set a new size*/
2017-04-13 16:12:03 +02:00
cont->signal_f(cont, LV_SIGNAL_CORD_CHG, cont);
2016-06-15 09:38:20 +02:00
}
/*=====================
* Getter functions
*====================*/
2016-10-07 11:15:46 +02:00
/**
2017-04-13 16:12:03 +02:00
* Get the layout of a container
* @param cont pointer to container object
* @return the layout from 'lv_cont_layout_t'
2016-10-07 11:15:46 +02:00
*/
2017-04-13 16:12:03 +02:00
lv_cont_layout_t lv_cont_get_layout(lv_obj_t * cont)
2016-06-22 20:39:07 +02:00
{
2017-04-13 16:12:03 +02:00
lv_cont_ext_t * ext = lv_obj_get_ext(cont);
2016-10-07 11:15:46 +02:00
return ext->layout;
2016-06-22 20:39:07 +02:00
}
2016-06-15 09:38:20 +02:00
/**
2017-04-13 16:12:03 +02:00
* Get horizontal fit enable attribute of a container
* @param cont pointer to a container object
2016-06-15 09:38:20 +02:00
* @return true: horizontal padding is enabled
*/
2017-04-13 16:12:03 +02:00
bool lv_cont_get_hfit(lv_obj_t * cont)
2016-06-15 09:38:20 +02:00
{
2017-04-13 16:12:03 +02:00
lv_cont_ext_t * ext = lv_obj_get_ext(cont);
2017-04-21 09:15:39 +02:00
return ext->hfit_en == 0 ? false : true;
2016-06-15 09:38:20 +02:00
}
/**
2017-04-13 16:12:03 +02:00
* Get vertical fit enable attribute of a container
* @param cont pointer to a container object
2016-06-15 09:38:20 +02:00
* @return true: vertical padding is enabled
*/
2017-04-13 16:12:03 +02:00
bool lv_cont_get_vfit(lv_obj_t * cont)
2016-06-15 09:38:20 +02:00
{
2017-04-13 16:12:03 +02:00
lv_cont_ext_t * ext = lv_obj_get_ext(cont);
2017-04-21 09:15:39 +02:00
return ext->vfit_en == 0 ? false : true;
2016-06-15 09:38:20 +02:00
}
2016-06-08 07:25:08 +02:00
/**********************
* STATIC FUNCTIONS
**********************/
#if 0
2016-06-08 07:25:08 +02:00
/**
2017-04-13 16:12:03 +02:00
* Handle the drawing related tasks of the containers
* @param cont pointer to an 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'
*/
2017-04-13 16:12:03 +02:00
static bool lv_cont_design(lv_obj_t * cont, const area_t * mask, lv_design_mode_t mode)
2016-06-08 07:25:08 +02:00
{
if(mode == LV_DESIGN_COVER_CHK) {
return false;
} else if(mode == LV_DESIGN_DRAW_MAIN) {
2016-06-08 07:25:08 +02:00
} else if(mode == LV_DESIGN_DRAW_POST) {
}
2016-06-08 07:25:08 +02:00
return true;
}
#endif
2016-06-08 07:25:08 +02:00
2016-06-22 20:39:07 +02:00
/**
2017-04-13 16:12:03 +02:00
* Refresh the layout of a container
* @param cont pointer to an object which layout should be refreshed
2016-06-22 20:39:07 +02:00
*/
2017-04-13 16:12:03 +02:00
static void lv_cont_refr_layout(lv_obj_t * cont)
2016-06-22 20:39:07 +02:00
{
2017-04-13 16:12:03 +02:00
lv_cont_layout_t type = lv_cont_get_layout(cont);
2016-06-22 20:39:07 +02:00
2016-12-17 10:50:28 +01:00
/*'rect' has to be at least 1 child*/
2017-04-13 16:12:03 +02:00
if(lv_obj_get_child(cont, NULL) == NULL) return;
if(type == LV_CONT_LAYOUT_OFF) return;
if(type == LV_CONT_LAYOUT_CENTER) {
lv_cont_layout_center(cont);
} else if(type == LV_CONT_LAYOUT_COL_L || type == LV_CONT_LAYOUT_COL_M || type == LV_CONT_LAYOUT_COL_R) {
lv_cont_layout_col(cont);
} else if(type == LV_CONT_LAYOUT_ROW_T || type == LV_CONT_LAYOUT_ROW_M || type == LV_CONT_LAYOUT_ROW_B) {
lv_cont_layout_row(cont);
} else if(type == LV_CONT_LAYOUT_PRETTY) {
lv_cont_layout_pretty(cont);
} else if(type == LV_CONT_LAYOUT_GRID) {
lv_cont_layout_grid(cont);
2016-06-22 20:39:07 +02:00
}
}
/**
* Handle column type layouts
2017-04-13 16:12:03 +02:00
* @param cont pointer to an object which layout should be handled
2016-06-22 20:39:07 +02:00
*/
2017-04-13 16:12:03 +02:00
static void lv_cont_layout_col(lv_obj_t * cont)
2016-06-22 20:39:07 +02:00
{
2017-04-13 16:12:03 +02:00
lv_cont_layout_t type = lv_cont_get_layout(cont);
2016-06-22 20:39:07 +02:00
lv_obj_t * child;
/*Adjust margin and get the alignment type*/
lv_align_t align;
2017-04-13 16:12:03 +02:00
lv_style_t * style = lv_obj_get_style(cont);
2016-06-22 20:39:07 +02:00
cord_t hpad_corr;
2016-10-07 11:15:46 +02:00
2016-06-22 20:39:07 +02:00
switch(type) {
2017-04-13 16:12:03 +02:00
case LV_CONT_LAYOUT_COL_L:
2017-01-06 13:37:25 +01:00
hpad_corr = style->hpad;
2016-06-22 20:39:07 +02:00
align = LV_ALIGN_IN_TOP_LEFT;
break;
2017-04-13 16:12:03 +02:00
case LV_CONT_LAYOUT_COL_M:
2016-06-22 20:39:07 +02:00
hpad_corr = 0;
align = LV_ALIGN_IN_TOP_MID;
break;
2017-04-13 16:12:03 +02:00
case LV_CONT_LAYOUT_COL_R:
2016-10-07 11:15:46 +02:00
hpad_corr = -style->hpad;
2016-06-22 20:39:07 +02:00
align = LV_ALIGN_IN_TOP_RIGHT;
break;
default:
2016-12-21 15:34:01 +01:00
hpad_corr = 0;
2016-06-22 20:39:07 +02:00
align = LV_ALIGN_IN_TOP_LEFT;
break;
}
/* Disable child change action because the children will be moved a lot
* an unnecessary child change signals could be sent*/
2017-04-13 16:12:03 +02:00
lv_obj_set_protect(cont, LV_PROTECT_CHILD_CHG);
2016-06-22 20:39:07 +02:00
/* Align the children */
2016-10-07 11:15:46 +02:00
cord_t last_cord = style->vpad;
2017-04-13 16:12:03 +02:00
LL_READ_BACK(cont->child_ll, child) {
if(lv_obj_get_hidden(child) != false ||
lv_obj_is_protected(child, LV_PROTECT_POS) != false) continue;
2016-07-12 01:16:27 +02:00
2017-04-13 16:12:03 +02:00
lv_obj_align(child, cont, align, hpad_corr , last_cord);
2016-10-07 11:15:46 +02:00
last_cord += lv_obj_get_height(child) + style->opad;
2016-06-22 20:39:07 +02:00
}
2017-04-13 16:12:03 +02:00
lv_obj_clr_protect(cont, LV_PROTECT_CHILD_CHG);
2016-06-22 20:39:07 +02:00
}
/**
* Handle row type layouts
2017-04-13 16:12:03 +02:00
* @param cont pointer to an object which layout should be handled
2016-06-22 20:39:07 +02:00
*/
2017-04-13 16:12:03 +02:00
static void lv_cont_layout_row(lv_obj_t * cont)
2016-06-22 20:39:07 +02:00
{
2017-04-13 16:12:03 +02:00
lv_cont_layout_t type = lv_cont_get_layout(cont);
2016-06-22 20:39:07 +02:00
lv_obj_t * child;
/*Adjust margin and get the alignment type*/
lv_align_t align;
2017-04-13 16:12:03 +02:00
lv_style_t * style = lv_obj_get_style(cont);
2016-10-07 11:15:46 +02:00
cord_t vpad_corr = style->vpad;
2016-06-22 20:39:07 +02:00
switch(type) {
2017-04-13 16:12:03 +02:00
case LV_CONT_LAYOUT_ROW_T:
2016-10-07 11:15:46 +02:00
vpad_corr = style->vpad;
2016-06-22 20:39:07 +02:00
align = LV_ALIGN_IN_TOP_LEFT;
break;
2017-04-13 16:12:03 +02:00
case LV_CONT_LAYOUT_ROW_M:
2016-06-22 20:39:07 +02:00
vpad_corr = 0;
align = LV_ALIGN_IN_LEFT_MID;
break;
2017-04-13 16:12:03 +02:00
case LV_CONT_LAYOUT_ROW_B:
2016-10-07 11:15:46 +02:00
vpad_corr = -style->vpad;
2016-06-22 20:39:07 +02:00
align = LV_ALIGN_IN_BOTTOM_LEFT;
break;
default:
vpad_corr = 0;
align = LV_ALIGN_IN_TOP_LEFT;
break;
}
/* Disable child change action because the children will be moved a lot
* an unnecessary child change signals could be sent*/
2017-04-13 16:12:03 +02:00
lv_obj_set_protect(cont, LV_PROTECT_CHILD_CHG);
2016-06-22 20:39:07 +02:00
/* Align the children */
2016-10-07 11:15:46 +02:00
cord_t last_cord = style->hpad;
2017-04-13 16:12:03 +02:00
LL_READ_BACK(cont->child_ll, child) {
if(lv_obj_get_hidden(child) != false ||
lv_obj_is_protected(child, LV_PROTECT_POS) != false) continue;
2016-07-12 01:16:27 +02:00
2017-04-13 16:12:03 +02:00
lv_obj_align(child, cont, align, last_cord, vpad_corr);
2016-10-07 11:15:46 +02:00
last_cord += lv_obj_get_width(child) + style->opad;
2016-06-22 20:39:07 +02:00
}
2017-04-13 16:12:03 +02:00
lv_obj_clr_protect(cont, LV_PROTECT_CHILD_CHG);
2016-06-22 20:39:07 +02:00
}
2016-09-27 13:43:01 +02:00
/**
* Handle the center layout
2017-04-13 16:12:03 +02:00
* @param cont pointer to an object which layout should be handled
2016-09-27 13:43:01 +02:00
*/
2017-04-13 16:12:03 +02:00
static void lv_cont_layout_center(lv_obj_t * cont)
2016-06-22 20:39:07 +02:00
{
lv_obj_t * child;
2017-04-13 16:12:03 +02:00
lv_style_t * style = lv_obj_get_style(cont);
2016-06-22 20:39:07 +02:00
uint32_t obj_num = 0;
cord_t h_tot = 0;
2017-04-13 16:12:03 +02:00
LL_READ(cont->child_ll, child) {
2017-07-09 15:32:49 +02:00
if(lv_obj_get_hidden(child) != false ||
lv_obj_is_protected(child, LV_PROTECT_POS) != false) continue;
2016-10-07 11:15:46 +02:00
h_tot += lv_obj_get_height(child) + style->opad;
2016-06-22 20:39:07 +02:00
obj_num ++;
}
if(obj_num == 0) return;
2016-10-07 11:15:46 +02:00
h_tot -= style->opad;
2016-06-22 20:39:07 +02:00
/* Disable child change action because the children will be moved a lot
* an unnecessary child change signals could be sent*/
2017-04-13 16:12:03 +02:00
lv_obj_set_protect(cont, LV_PROTECT_CHILD_CHG);
2016-06-22 20:39:07 +02:00
/* Align the children */
cord_t last_cord = - (h_tot / 2);
2017-04-13 16:12:03 +02:00
LL_READ_BACK(cont->child_ll, child) {
if(lv_obj_get_hidden(child) != false ||
lv_obj_is_protected(child, LV_PROTECT_POS) != false) continue;
2016-07-12 01:16:27 +02:00
2017-04-13 16:12:03 +02:00
lv_obj_align(child, cont, LV_ALIGN_CENTER, 0, last_cord + lv_obj_get_height(child) / 2);
2016-10-07 11:15:46 +02:00
last_cord += lv_obj_get_height(child) + style->opad;
2016-06-22 20:39:07 +02:00
}
2017-04-13 16:12:03 +02:00
lv_obj_clr_protect(cont, LV_PROTECT_CHILD_CHG);
2016-06-22 20:39:07 +02:00
}
2016-09-27 13:43:01 +02:00
/**
2016-12-17 10:50:28 +01:00
* Handle the pretty layout. Put as many object as possible in row
2016-09-27 13:43:01 +02:00
* then begin a new row
2017-04-13 16:12:03 +02:00
* @param cont pointer to an object which layout should be handled
2016-09-27 13:43:01 +02:00
*/
2017-04-13 16:12:03 +02:00
static void lv_cont_layout_pretty(lv_obj_t * cont)
2016-09-27 13:43:01 +02:00
{
lv_obj_t * child_rs; /* Row starter child */
lv_obj_t * child_rc; /* Row closer child */
lv_obj_t * child_tmp; /* Temporary child */
2017-04-13 16:12:03 +02:00
lv_style_t * style = lv_obj_get_style(cont);
cord_t w_obj = lv_obj_get_width(cont);
2016-10-07 11:15:46 +02:00
cord_t act_y = style->vpad;
2016-09-27 13:43:01 +02:00
/* Disable child change action because the children will be moved a lot
* an unnecessary child change signals could be sent*/
2017-04-13 16:12:03 +02:00
child_rs = ll_get_tail(&cont->child_ll); /*Set the row starter child*/
2016-09-27 13:43:01 +02:00
if(child_rs == NULL) return; /*Return if no child*/
2017-04-13 16:12:03 +02:00
lv_obj_set_protect(cont, LV_PROTECT_CHILD_CHG);
2016-09-27 13:43:01 +02:00
child_rc = child_rs; /*Initially the the row starter and closer is the same*/
while(child_rs != NULL) {
cord_t h_row = 0;
2017-05-01 16:47:27 +02:00
cord_t w_row = style->hpad * 2; /*The width is at least the left+right hpad*/
2016-09-27 13:43:01 +02:00
uint32_t obj_num = 0;
/*Find the row closer object and collect some data*/
do {
if(lv_obj_get_hidden(child_rc) == false &&
lv_obj_is_protected(child_rc, LV_PROTECT_POS) == false) {
/*If this object is already not fit then break*/
if(w_row + lv_obj_get_width(child_rc) > w_obj) {
/*Step back one child because the last already not fit, so the previous is the closer*/
if(child_rc != NULL && obj_num != 0 ) {
child_rc = ll_get_next(&cont->child_ll, child_rc);
}
break;
}
2016-10-07 11:15:46 +02:00
w_row += lv_obj_get_width(child_rc) + style->opad; /*Add the object width + opad*/
h_row = MATH_MAX(h_row, lv_obj_get_height(child_rc)); /*Search the highest object*/
2016-09-27 13:43:01 +02:00
obj_num ++;
if(lv_obj_is_protected(child_rc, LV_PROTECT_FOLLOW)) break; /*If can not be followed by an other object then break here*/
2016-09-27 13:43:01 +02:00
}
2017-04-13 16:12:03 +02:00
child_rc = ll_get_prev(&cont->child_ll, child_rc); /*Load the next object*/
2016-09-27 13:43:01 +02:00
if(obj_num == 0) child_rs = child_rc; /*If the first object was hidden (or too long) then set the next as first */
}while(child_rc != NULL);
/*If the object is too long then align it to the middle*/
if(obj_num == 0) {
if(child_rc != NULL) {
2017-04-13 16:12:03 +02:00
lv_obj_align(child_rc, cont, LV_ALIGN_IN_TOP_MID, 0, act_y);
h_row = lv_obj_get_height(child_rc); /*Not set previously because of the early break*/
2016-09-27 13:43:01 +02:00
}
}
/*If there is only one object in the row then align it to the middle*/
2016-09-27 13:43:01 +02:00
else if (obj_num == 1) {
2017-04-13 16:12:03 +02:00
lv_obj_align(child_rs, cont, LV_ALIGN_IN_TOP_MID, 0, act_y);
2016-09-27 13:43:01 +02:00
}
/*If are two object in the row then align them proportionally*/
else if (obj_num == 2) {
lv_obj_t * obj1 = child_rs;
lv_obj_t * obj2 = ll_get_prev(&cont->child_ll, child_rs);
w_row = lv_obj_get_width(obj1) + lv_obj_get_width(obj2);
cord_t pad = (w_obj - w_row) / 3;
lv_obj_align(obj1, cont, LV_ALIGN_IN_TOP_LEFT, pad, act_y);
lv_obj_align(obj2, cont, LV_ALIGN_IN_TOP_RIGHT, -pad, act_y);
}
2016-09-27 13:43:01 +02:00
/* Align the children (from child_rs to child_rc)*/
else {
2016-10-07 11:15:46 +02:00
w_row -= style->opad * obj_num;
2016-09-27 13:43:01 +02:00
cord_t new_opad = (w_obj - w_row) / (obj_num - 1);
2016-10-07 11:15:46 +02:00
cord_t act_x = style->hpad; /*x init*/
2016-09-27 13:43:01 +02:00
child_tmp = child_rs;
2017-05-01 16:47:27 +02:00
while(child_tmp != NULL) {
if(lv_obj_get_hidden(child_tmp) == false &&
lv_obj_is_protected(child_tmp, LV_PROTECT_POS) == false) {
2017-04-13 16:12:03 +02:00
lv_obj_align(child_tmp, cont, LV_ALIGN_IN_TOP_LEFT, act_x, act_y);
2016-09-27 13:43:01 +02:00
act_x += lv_obj_get_width(child_tmp) + new_opad;
}
2017-05-01 16:47:27 +02:00
if(child_tmp == child_rc) break;
2017-04-13 16:12:03 +02:00
child_tmp = ll_get_prev(&cont->child_ll, child_tmp);
2017-05-01 16:47:27 +02:00
}
2016-09-27 13:43:01 +02:00
}
if(child_rc == NULL) break;
2016-10-07 11:15:46 +02:00
act_y += style->opad + h_row; /*y increment*/
2017-04-13 16:12:03 +02:00
child_rs = ll_get_prev(&cont->child_ll, child_rc); /*Go to the next object*/
2016-09-27 13:43:01 +02:00
child_rc = child_rs;
}
2017-04-13 16:12:03 +02:00
lv_obj_clr_protect(cont, LV_PROTECT_CHILD_CHG);
2016-09-27 13:43:01 +02:00
}
2016-12-17 10:50:28 +01:00
/**
* Handle the grid layout. Align same-sized objects in a grid
2017-04-13 16:12:03 +02:00
* @param cont pointer to an object which layout should be handled
2016-12-17 10:50:28 +01:00
*/
2017-04-13 16:12:03 +02:00
static void lv_cont_layout_grid(lv_obj_t * cont)
2016-12-17 10:50:28 +01:00
{
lv_obj_t * child;
2017-04-13 16:12:03 +02:00
lv_style_t * style = lv_obj_get_style(cont);
cord_t w_tot = lv_obj_get_width(cont);
cord_t w_obj = lv_obj_get_width(lv_obj_get_child(cont, NULL));
cord_t h_obj = lv_obj_get_height(lv_obj_get_child(cont, NULL));
2016-12-17 10:50:28 +01:00
uint16_t obj_row = (w_tot - (2 * style->hpad)) / (w_obj + style->opad); /*Obj. num. in a row*/
cord_t x_ofs;
if(obj_row > 1) {
x_ofs = w_obj + (w_tot - (2 * style->hpad) - (obj_row * w_obj)) / (obj_row - 1);
} else {
x_ofs = w_tot / 2 - w_obj / 2;
}
2016-12-17 10:50:28 +01:00
cord_t y_ofs = h_obj + style->opad;
/* Disable child change action because the children will be moved a lot
* an unnecessary child change signals could be sent*/
2017-04-13 16:12:03 +02:00
lv_obj_set_protect(cont, LV_PROTECT_CHILD_CHG);
2016-12-17 10:50:28 +01:00
/* Align the children */
cord_t act_x = style->hpad;
cord_t act_y = style->vpad;
uint16_t obj_cnt = 0;
2017-04-13 16:12:03 +02:00
LL_READ_BACK(cont->child_ll, child) {
if(lv_obj_get_hidden(child) != false ||
lv_obj_is_protected(child, LV_PROTECT_POS) != false) continue;
2016-12-17 10:50:28 +01:00
if(obj_row > 1) {
lv_obj_set_pos(child, act_x, act_y);
act_x += x_ofs;
} else {
lv_obj_set_pos(child, x_ofs, act_y);
}
2016-12-17 10:50:28 +01:00
obj_cnt ++;
if(obj_cnt >= obj_row) {
obj_cnt = 0;
act_x = style->hpad;
act_y += y_ofs;
}
}
2017-04-13 16:12:03 +02:00
lv_obj_clr_protect(cont, LV_PROTECT_CHILD_CHG);
2016-12-17 10:50:28 +01:00
}
2016-09-27 13:43:01 +02:00
/**
* Handle auto fit. Set the size of the object to involve all children.
2017-04-13 16:12:03 +02:00
* @param cont pointer to an object which size will be modified
2016-09-27 13:43:01 +02:00
*/
2017-04-13 16:12:03 +02:00
static void lv_cont_refr_autofit(lv_obj_t * cont)
2016-06-22 20:39:07 +02:00
{
2017-04-13 16:12:03 +02:00
lv_cont_ext_t * ext = lv_obj_get_ext(cont);
2016-06-22 20:39:07 +02:00
2017-04-21 09:15:39 +02:00
if(ext->hfit_en == 0 &&
ext->vfit_en == 0) {
2016-06-22 20:39:07 +02:00
return;
}
area_t new_cords;
2016-06-22 20:39:07 +02:00
area_t ori;
2017-04-13 16:12:03 +02:00
lv_style_t * style = lv_obj_get_style(cont);
2016-06-22 20:39:07 +02:00
lv_obj_t * i;
2016-10-07 11:15:46 +02:00
cord_t hpad = style->hpad;
cord_t vpad = style->vpad;
2016-06-22 20:39:07 +02:00
/*Search the side coordinates of the children*/
2017-04-13 16:12:03 +02:00
lv_obj_get_cords(cont, &ori);
lv_obj_get_cords(cont, &new_cords);
2016-06-22 20:39:07 +02:00
2017-04-21 09:15:39 +02:00
new_cords.x1 = CORD_MAX;
new_cords.y1 = CORD_MAX;
new_cords.x2 = CORD_MIN;
new_cords.y2 = CORD_MIN;
2016-06-22 20:39:07 +02:00
2017-04-13 16:12:03 +02:00
LL_READ(cont->child_ll, i) {
2016-07-12 01:16:27 +02:00
if(lv_obj_get_hidden(i) != false) continue;
new_cords.x1 = MATH_MIN(new_cords.x1, i->cords.x1);
new_cords.y1 = MATH_MIN(new_cords.y1, i->cords.y1);
new_cords.x2 = MATH_MAX(new_cords.x2, i->cords.x2);
new_cords.y2 = MATH_MAX(new_cords.y2, i->cords.y2);
2016-06-22 20:39:07 +02:00
}
/*If the value is not the init value then the page has >=1 child.*/
2017-04-21 09:15:39 +02:00
if(new_cords.x1 != CORD_MAX) {
if(ext->hfit_en != 0) {
new_cords.x1 -= hpad;
new_cords.x2 += hpad;
2016-06-22 20:39:07 +02:00
} else {
2017-04-13 16:12:03 +02:00
new_cords.x1 = cont->cords.x1;
new_cords.x2 = cont->cords.x2;
2016-06-22 20:39:07 +02:00
}
2017-04-21 09:15:39 +02:00
if(ext->vfit_en != 0) {
new_cords.y1 -= vpad;
new_cords.y2 += vpad;
2016-06-22 20:39:07 +02:00
} else {
2017-04-13 16:12:03 +02:00
new_cords.y1 = cont->cords.y1;
new_cords.y2 = cont->cords.y2;
2016-06-22 20:39:07 +02:00
}
/*Do nothing if the coordinates are not changed*/
2017-04-13 16:12:03 +02:00
if(cont->cords.x1 != new_cords.x1 ||
cont->cords.y1 != new_cords.y1 ||
cont->cords.x2 != new_cords.x2 ||
cont->cords.y2 != new_cords.y2) {
2016-06-22 20:39:07 +02:00
2017-04-13 16:12:03 +02:00
lv_obj_inv(cont);
area_cpy(&cont->cords, &new_cords);
lv_obj_inv(cont);
2016-06-22 20:39:07 +02:00
/*Notify the object about its new coordinates*/
2017-04-13 16:12:03 +02:00
cont->signal_f(cont, LV_SIGNAL_CORD_CHG, &ori);
2016-06-22 20:39:07 +02:00
/*Inform the parent about the new coordinates*/
2017-04-13 16:12:03 +02:00
lv_obj_t * par = lv_obj_get_parent(cont);
par->signal_f(par, LV_SIGNAL_CHILD_CHG, cont);
}
2016-06-22 20:39:07 +02:00
}
}
2016-06-08 07:25:08 +02:00
#endif