freertos_f407/User/system/sgl/widgets/checkbox/sgl_checkbox.c

191 lines
7.4 KiB
C

/* source/widgets/sgl_checkbox.c
*
* MIT License
*
* Copyright(c) 2023-present All contributors of SGL
* Document reference link: https://sgl-docs.readthedocs.io
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <sgl_core.h>
#include <sgl_draw.h>
#include <sgl_math.h>
#include <sgl_log.h>
#include <sgl_mm.h>
#include <sgl_theme.h>
#include <sgl_cfgfix.h>
#include <string.h>
#include "sgl_checkbox.h"
static const uint8_t unchecked_pixmap[] = {
/*  */
0x00,0x03,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x00,0x00,0x00,0x00,
0x05,0xef,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xea,0x00,0x00,0x00,
0x8e,0xfe,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xce,0xff,0xa0,0x00,0x00,
0xef,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xaf,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0xe0,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0xe0,0x00,0x00,
0xef,0xe3,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xcf,0xe0,0x00,0x00,
0x5e,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0x80,0x00,0x00,
0x03,0xcf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc5,0x00,0x00,0x00,
};
static const uint8_t checked_pixmap[] = {
/*  */
0x00,0x03,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x00,0x00,0x00,0x00,
0x05,0xef,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe3,0x00,0x30,0x00,
0x8e,0xfe,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x80,0x5e,0xfa,0x00,
0xef,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0xef,0xff,0xa0,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5e,0xff,0xff,0xe0,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0xef,0xff,0xff,0xe0,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x5e,0xff,0xff,0xfe,0x30,
0xee,0x50,0x03,0x55,0x00,0x00,0x00,0x05,0xef,0xff,0xff,0xe3,0x00,
0xee,0x50,0x3e,0xfe,0x80,0x00,0x00,0x5e,0xff,0xff,0xfe,0x30,0x00,
0xee,0x50,0xef,0xff,0xe8,0x00,0x05,0xef,0xff,0xff,0xe3,0x00,0x00,
0xee,0x58,0xef,0xff,0xfe,0x80,0x5e,0xff,0xff,0xfe,0x30,0x00,0x00,
0xee,0x50,0xcf,0xff,0xff,0xe8,0xef,0xff,0xff,0xe3,0x00,0x00,0x00,
0xee,0x50,0x0c,0xff,0xff,0xff,0xff,0xff,0xfe,0x30,0x85,0x00,0x00,
0xee,0x50,0x00,0xcf,0xff,0xff,0xff,0xff,0xe3,0x0c,0xe8,0x00,0x00,
0xee,0x50,0x00,0x0c,0xff,0xff,0xff,0xfe,0x30,0x0e,0xe8,0x00,0x00,
0xee,0x50,0x00,0x00,0xcf,0xff,0xff,0xe3,0x00,0x0e,0xe8,0x00,0x00,
0xee,0x50,0x00,0x00,0x0c,0xff,0xfe,0x30,0x00,0x0e,0xe8,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0xcf,0xe3,0x00,0x00,0x0e,0xe8,0x00,0x00,
0xee,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0xe8,0x00,0x00,
0xef,0xe3,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xcf,0xe3,0x00,0x00,
0x5e,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0x80,0x00,0x00,
0x03,0xcf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc5,0x00,0x00,0x00,
};
static sgl_icon_pixmap_t unchecked_icon = {
.bitmap = unchecked_pixmap,
.height = 22,
.width = 26,
};
static sgl_icon_pixmap_t checked_icon = {
.bitmap = checked_pixmap,
.height = 22,
.width = 26,
};
/**
* @brief checkbox construct callback
* @param surf surface pointer
* @param obj checkbox object
* @param evt event parameter
* @return none
*/
static void sgl_checkbox_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t *evt)
{
sgl_checkbox_t *checkbox = sgl_container_of(obj, sgl_checkbox_t, obj);
int text_x = 0, icon_y = 0;
sgl_pos_t align_pos;
SGL_ASSERT(checkbox->font != NULL);
if(evt->type == SGL_EVENT_DRAW_MAIN) {
if(checkbox->status) {
checkbox->icon = &checked_icon;
}
else {
checkbox->icon = &unchecked_icon;
}
if (checkbox->icon) {
text_x = checkbox->icon->width + 2;
}
align_pos = sgl_get_text_pos(&obj->coords, checkbox->font, checkbox->text, text_x, SGL_ALIGN_CENTER);
if (checkbox->icon) {
icon_y = ((obj->coords.y2 - obj->coords.y1) - (checkbox->icon->height)) / 2 + 1;
sgl_draw_icon(surf, &obj->area, align_pos.x, obj->coords.y1 + icon_y, checkbox->color, checkbox->alpha, checkbox->icon);
}
sgl_draw_string(surf, &obj->area, align_pos.x + text_x, align_pos.y, checkbox->text, checkbox->color, checkbox->alpha, checkbox->font);
}
else if(evt->type == SGL_EVENT_PRESSED) {
checkbox->status = !checkbox->status;
sgl_obj_set_dirty(obj);
}
else if(evt->type == SGL_EVENT_DRAW_INIT) {
if(obj->coords.y2 < obj->coords.y1) {
obj->coords.y2 = obj->coords.y1 + unchecked_icon.height;
}
if(obj->coords.x2 < obj->coords.x1) {
obj->coords.x2 = obj->coords.x1 + (checked_icon.width + sgl_font_get_string_width(checkbox->text, checkbox->font));
}
}
}
/**
* @brief create a checkbox object
* @param parent parent of the checkbox
* @return checkbox object
*/
sgl_obj_t* sgl_checkbox_create(sgl_obj_t* parent)
{
sgl_checkbox_t *checkbox = sgl_malloc(sizeof(sgl_checkbox_t));
if(checkbox == NULL) {
SGL_LOG_ERROR("sgl_checkbox_create: malloc failed");
return NULL;
}
/* set object all member to zero */
memset(checkbox, 0, sizeof(sgl_checkbox_t));
sgl_obj_t *obj = &checkbox->obj;
sgl_obj_init(&checkbox->obj, parent);
obj->construct_fn = sgl_checkbox_construct_cb;
obj->needinit = 1;
checkbox->status = false;
checkbox->alpha = SGL_ALPHA_MAX;
checkbox->color = SGL_THEME_TEXT_COLOR;
checkbox->icon = &unchecked_icon;
obj->coords.y2 = SGL_POS_INVALID;
obj->coords.x2 = SGL_POS_INVALID;
sgl_obj_set_clickable(obj);
checkbox->font = sgl_get_system_font();
checkbox->text = " ";
return obj;
}