LVGL 库提交
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
/**
|
||||
* @file lv_msgbox.c
|
||||
*
|
||||
*/
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#include "lv_msgbox_private.h"
|
||||
#include "../../core/lv_obj_private.h"
|
||||
#include "../../core/lv_obj_class_private.h"
|
||||
#if LV_USE_MSGBOX
|
||||
|
||||
#include "../label/lv_label.h"
|
||||
#include "../button/lv_button.h"
|
||||
#include "../image/lv_image.h"
|
||||
#include "../../misc/lv_assert.h"
|
||||
#include "../../display/lv_display.h"
|
||||
#include "../../layouts/flex/lv_flex.h"
|
||||
#include "../../stdlib/lv_string.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
#define LV_MSGBOX_FLAG_AUTO_PARENT LV_OBJ_FLAG_WIDGET_1 /*Mark that the parent was automatically created*/
|
||||
#define MY_CLASS (&lv_msgbox_class)
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* STATIC PROTOTYPES
|
||||
**********************/
|
||||
static void msgbox_close_click_event_cb(lv_event_t * e);
|
||||
static void msgbox_size_changed_event_cb(lv_event_t * e);
|
||||
|
||||
/**********************
|
||||
* STATIC VARIABLES
|
||||
**********************/
|
||||
const lv_obj_class_t lv_msgbox_class = {
|
||||
.base_class = &lv_obj_class,
|
||||
.width_def = LV_DPI_DEF * 2,
|
||||
.height_def = LV_SIZE_CONTENT,
|
||||
.instance_size = sizeof(lv_msgbox_t),
|
||||
.name = "lv_msgbox",
|
||||
};
|
||||
|
||||
const lv_obj_class_t lv_msgbox_header_class = {
|
||||
.base_class = &lv_obj_class,
|
||||
.width_def = LV_PCT(100),
|
||||
.height_def = LV_DPI_DEF / 3,
|
||||
.instance_size = sizeof(lv_obj_t),
|
||||
.name = "lv_msgbox_header",
|
||||
};
|
||||
|
||||
const lv_obj_class_t lv_msgbox_content_class = {
|
||||
.base_class = &lv_obj_class,
|
||||
.width_def = LV_PCT(100),
|
||||
.height_def = LV_SIZE_CONTENT,
|
||||
.instance_size = sizeof(lv_obj_t),
|
||||
.name = "lv_msgbox_content",
|
||||
};
|
||||
|
||||
const lv_obj_class_t lv_msgbox_footer_class = {
|
||||
.base_class = &lv_obj_class,
|
||||
.width_def = LV_PCT(100),
|
||||
.height_def = LV_DPI_DEF / 3,
|
||||
.instance_size = sizeof(lv_obj_t),
|
||||
.name = "lv_msgbox_footer",
|
||||
};
|
||||
|
||||
const lv_obj_class_t lv_msgbox_footer_button_class = {
|
||||
.base_class = &lv_obj_class,
|
||||
.width_def = LV_SIZE_CONTENT,
|
||||
.height_def = LV_PCT(100),
|
||||
.instance_size = sizeof(lv_obj_t),
|
||||
.group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
|
||||
.name = "lv_msgbox_footer_button",
|
||||
};
|
||||
|
||||
const lv_obj_class_t lv_msgbox_header_button_class = {
|
||||
.base_class = &lv_obj_class,
|
||||
.width_def = LV_DPI_DEF / 3,
|
||||
.height_def = LV_PCT(100),
|
||||
.instance_size = sizeof(lv_obj_t),
|
||||
.group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
|
||||
.name = "lv_msgbox_header_button",
|
||||
};
|
||||
|
||||
const lv_obj_class_t lv_msgbox_backdrop_class = {
|
||||
.base_class = &lv_obj_class,
|
||||
.width_def = LV_PCT(100),
|
||||
.height_def = LV_PCT(100),
|
||||
.instance_size = sizeof(lv_obj_t),
|
||||
.name = "lv_msgbox_backdrop",
|
||||
};
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL FUNCTIONS
|
||||
**********************/
|
||||
|
||||
lv_obj_t * lv_msgbox_create(lv_obj_t * parent)
|
||||
{
|
||||
LV_LOG_INFO("begin");
|
||||
bool auto_parent = false;
|
||||
if(parent == NULL) {
|
||||
auto_parent = true;
|
||||
parent = lv_obj_class_create_obj(&lv_msgbox_backdrop_class, lv_layer_top());
|
||||
LV_ASSERT_MALLOC(parent);
|
||||
lv_obj_class_init_obj(parent);
|
||||
lv_obj_remove_flag(parent, LV_OBJ_FLAG_IGNORE_LAYOUT);
|
||||
lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100));
|
||||
}
|
||||
|
||||
lv_obj_t * obj = lv_obj_class_create_obj(&lv_msgbox_class, parent);
|
||||
LV_ASSERT_MALLOC(obj);
|
||||
if(obj == NULL) return NULL;
|
||||
lv_obj_class_init_obj(obj);
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_COLUMN);
|
||||
|
||||
if(auto_parent) lv_obj_add_flag(obj, LV_MSGBOX_FLAG_AUTO_PARENT);
|
||||
|
||||
mbox->content = lv_obj_class_create_obj(&lv_msgbox_content_class, obj);
|
||||
LV_ASSERT_MALLOC(obj);
|
||||
if(mbox->content == NULL) return NULL;
|
||||
lv_obj_class_init_obj(mbox->content);
|
||||
lv_obj_set_flex_flow(mbox->content, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_add_event_cb(obj, msgbox_size_changed_event_cb, LV_EVENT_SIZE_CHANGED, 0);
|
||||
|
||||
lv_obj_center(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_add_title(lv_obj_t * obj, const char * title)
|
||||
{
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
if(mbox->header == NULL) {
|
||||
mbox->header = lv_obj_class_create_obj(&lv_msgbox_header_class, obj);
|
||||
LV_ASSERT_MALLOC(obj);
|
||||
if(mbox->header == NULL) return NULL;
|
||||
lv_obj_class_init_obj(mbox->header);
|
||||
|
||||
lv_obj_set_size(mbox->header, lv_pct(100), lv_display_get_dpi(lv_obj_get_display(obj)) / 3);
|
||||
lv_obj_set_flex_flow(mbox->header, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_set_flex_align(mbox->header, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
|
||||
lv_obj_remove_flag(mbox->header, LV_OBJ_FLAG_SCROLLABLE);
|
||||
lv_obj_move_to_index(mbox->header, 0);
|
||||
}
|
||||
|
||||
if(mbox->title == NULL) {
|
||||
mbox->title = lv_label_create(mbox->header);
|
||||
lv_obj_set_flex_grow(mbox->title, 1);
|
||||
}
|
||||
|
||||
lv_label_set_text(mbox->title, title);
|
||||
|
||||
return mbox->title;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_add_header_button(lv_obj_t * obj, const void * icon)
|
||||
{
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
if(mbox->header == NULL) {
|
||||
lv_msgbox_add_title(obj, ""); /*Just to push the buttons to the right*/
|
||||
}
|
||||
|
||||
lv_obj_t * btn = lv_obj_class_create_obj(&lv_msgbox_header_button_class, mbox->header);
|
||||
LV_ASSERT_MALLOC(obj);
|
||||
if(btn == NULL) return NULL;
|
||||
lv_obj_class_init_obj(btn);
|
||||
lv_obj_remove_flag(btn, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
if(icon) {
|
||||
lv_obj_t * img = lv_image_create(btn);
|
||||
lv_image_set_src(img, icon);
|
||||
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
|
||||
}
|
||||
|
||||
return btn;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_add_text(lv_obj_t * obj, const char * text)
|
||||
{
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
|
||||
lv_obj_t * label = lv_label_create(mbox->content);
|
||||
lv_label_set_text(label, text);
|
||||
lv_obj_set_width(label, lv_pct(100));
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_add_footer_button(lv_obj_t * obj, const char * text)
|
||||
{
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
if(mbox->footer == NULL) {
|
||||
mbox->footer = lv_obj_class_create_obj(&lv_msgbox_footer_class, obj);
|
||||
LV_ASSERT_MALLOC(obj);
|
||||
if(mbox->footer == NULL) return NULL;
|
||||
lv_obj_class_init_obj(mbox->footer);
|
||||
|
||||
lv_obj_set_flex_flow(mbox->footer, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_set_flex_align(mbox->footer, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
|
||||
lv_obj_remove_flag(mbox->footer, LV_OBJ_FLAG_SCROLLABLE);
|
||||
}
|
||||
|
||||
lv_obj_t * btn = lv_obj_class_create_obj(&lv_msgbox_footer_button_class, mbox->footer);
|
||||
LV_ASSERT_MALLOC(obj);
|
||||
if(btn == NULL) return NULL;
|
||||
lv_obj_class_init_obj(btn);
|
||||
lv_obj_remove_flag(btn, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
if(text) {
|
||||
lv_obj_t * label = lv_label_create(btn);
|
||||
lv_label_set_text(label, text);
|
||||
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
|
||||
}
|
||||
|
||||
return btn;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_add_close_button(lv_obj_t * obj)
|
||||
{
|
||||
lv_obj_t * btn = lv_msgbox_add_header_button(obj, LV_SYMBOL_CLOSE);
|
||||
lv_obj_add_event_cb(btn, msgbox_close_click_event_cb, LV_EVENT_CLICKED, NULL);
|
||||
return btn;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_get_header(lv_obj_t * obj)
|
||||
{
|
||||
LV_ASSERT_OBJ(obj, MY_CLASS);
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
return mbox->header;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_get_footer(lv_obj_t * obj)
|
||||
{
|
||||
LV_ASSERT_OBJ(obj, MY_CLASS);
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
return mbox->footer;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_get_content(lv_obj_t * obj)
|
||||
{
|
||||
LV_ASSERT_OBJ(obj, MY_CLASS);
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
return mbox->content;
|
||||
}
|
||||
|
||||
lv_obj_t * lv_msgbox_get_title(lv_obj_t * obj)
|
||||
{
|
||||
LV_ASSERT_OBJ(obj, MY_CLASS);
|
||||
lv_msgbox_t * mbox = (lv_msgbox_t *)obj;
|
||||
return mbox->title;
|
||||
}
|
||||
|
||||
void lv_msgbox_close(lv_obj_t * obj)
|
||||
{
|
||||
if(lv_obj_has_flag(obj, LV_MSGBOX_FLAG_AUTO_PARENT)) lv_obj_delete(lv_obj_get_parent(obj));
|
||||
else lv_obj_delete(obj);
|
||||
}
|
||||
|
||||
void lv_msgbox_close_async(lv_obj_t * obj)
|
||||
{
|
||||
if(lv_obj_has_flag(obj, LV_MSGBOX_FLAG_AUTO_PARENT)) lv_obj_delete_async(lv_obj_get_parent(obj));
|
||||
else lv_obj_delete_async(obj);
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
static void msgbox_close_click_event_cb(lv_event_t * e)
|
||||
{
|
||||
lv_obj_t * btn = lv_event_get_current_target(e);
|
||||
lv_obj_t * mbox = lv_obj_get_parent(lv_obj_get_parent(btn));
|
||||
lv_msgbox_close(mbox);
|
||||
}
|
||||
|
||||
static void msgbox_size_changed_event_cb(lv_event_t * e)
|
||||
{
|
||||
lv_obj_t * mbox = lv_event_get_target(e);
|
||||
lv_obj_t * content = lv_msgbox_get_content(mbox);
|
||||
bool is_msgbox_height_size_content = (lv_obj_get_style_height(mbox, 0) == LV_SIZE_CONTENT);
|
||||
lv_obj_set_flex_grow(content, !is_msgbox_height_size_content);
|
||||
}
|
||||
|
||||
#endif /*LV_USE_MSGBOX*/
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* @file lv_msgbox.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_MSGBOX_H
|
||||
#define LV_MSGBOX_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#include "../../core/lv_obj.h"
|
||||
|
||||
#if LV_USE_MSGBOX
|
||||
|
||||
/*Testing of dependencies*/
|
||||
#if LV_USE_BUTTONMATRIX == 0
|
||||
#error "lv_mbox: lv_buttonmatrix is required. Enable it in lv_conf.h (LV_USE_BUTTONMATRIX 1) "
|
||||
#endif
|
||||
|
||||
#if LV_USE_LABEL == 0
|
||||
#error "lv_mbox: lv_label is required. Enable it in lv_conf.h (LV_USE_LABEL 1) "
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_class;
|
||||
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_header_class;
|
||||
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_content_class;
|
||||
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_footer_class;
|
||||
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_header_button_class;
|
||||
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_footer_button_class;
|
||||
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_msgbox_backdrop_class;
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Create an empty message box
|
||||
* @param parent the parent or NULL to create a modal msgbox
|
||||
* @return the created message box
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_create(lv_obj_t * parent);
|
||||
|
||||
/**
|
||||
* Add title to the message box. It also creates a header for the title.
|
||||
* @param obj pointer to a message box
|
||||
* @param title the text of the tile
|
||||
* @return the created title label
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_add_title(lv_obj_t * obj, const char * title);
|
||||
|
||||
/**
|
||||
* Add a button to the header of to the message box. It also creates a header.
|
||||
* @param obj pointer to a message box
|
||||
* @param icon the icon of the button
|
||||
* @return the created button
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_add_header_button(lv_obj_t * obj, const void * icon);
|
||||
|
||||
/**
|
||||
* Add a text to the content area of message box. Multiple texts will be created below each other.
|
||||
* @param obj pointer to a message box
|
||||
* @param text text to add
|
||||
* @return the created button
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_add_text(lv_obj_t * obj, const char * text);
|
||||
|
||||
/**
|
||||
* Add a button to the footer of to the message box. It also creates a footer.
|
||||
* @param obj pointer to a message box
|
||||
* @param text the text of the button
|
||||
* @return the created button
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_add_footer_button(lv_obj_t * obj, const char * text);
|
||||
|
||||
/**
|
||||
* Add a close button to the message box. It also creates a header.
|
||||
* @param obj pointer to a message box
|
||||
* @return the created close button
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_add_close_button(lv_obj_t * obj);
|
||||
|
||||
/**
|
||||
* Get the header widget
|
||||
* @param obj pointer to a message box
|
||||
* @return the header, or NULL if not exists
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_get_header(lv_obj_t * obj);
|
||||
|
||||
/**
|
||||
* Get the footer widget
|
||||
* @param obj pointer to a message box
|
||||
* @return the footer, or NULL if not exists
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_get_footer(lv_obj_t * obj);
|
||||
|
||||
/**
|
||||
* Get the content widget
|
||||
* @param obj pointer to a message box
|
||||
* @return the content
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_get_content(lv_obj_t * obj);
|
||||
|
||||
/**
|
||||
* Get the title label
|
||||
* @param obj pointer to a message box
|
||||
* @return the title, or NULL if it does not exist
|
||||
*/
|
||||
lv_obj_t * lv_msgbox_get_title(lv_obj_t * obj);
|
||||
|
||||
/**
|
||||
* Close a message box
|
||||
* @param mbox pointer to a message box
|
||||
*/
|
||||
void lv_msgbox_close(lv_obj_t * mbox);
|
||||
|
||||
/**
|
||||
* Close a message box in the next call of the message box
|
||||
* @param mbox pointer to a message box
|
||||
*/
|
||||
void lv_msgbox_close_async(lv_obj_t * mbox);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#endif /*LV_USE_MSGBOX*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /*LV_MSGBOX_H*/
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* @file lv_msgbox_private.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_MSGBOX_PRIVATE_H
|
||||
#define LV_MSGBOX_PRIVATE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
#include "lv_msgbox.h"
|
||||
|
||||
#if LV_USE_MSGBOX
|
||||
#include "../../core/lv_obj_private.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
struct _lv_msgbox_t {
|
||||
lv_obj_t obj;
|
||||
lv_obj_t * header;
|
||||
lv_obj_t * content;
|
||||
lv_obj_t * footer;
|
||||
lv_obj_t * title;
|
||||
};
|
||||
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#endif /* LV_USE_MSGBOX */
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /*LV_MSGBOX_PRIVATE_H*/
|
||||
Reference in New Issue
Block a user