Text & number edit use the same custom widget

This commit is contained in:
Raphael Coeffic
2022-06-11 15:13:11 +02:00
committed by Raphael Coeffic
parent 574d008b67
commit aca9bf420a
8 changed files with 170 additions and 28 deletions
+1
View File
@@ -38,6 +38,7 @@ set(LIBOPENUI_SRC
# LVGL widgets
widgets/window_base.c
widgets/simple_btn.c
widgets/field_edit.c
)
if(SOFTWARE_KEYBOARD)
+2 -22
View File
@@ -18,6 +18,7 @@
*/
#include "numberedit.h"
#include "widgets/field_edit.h"
#if defined(SOFTWARE_KEYBOARD)
#include "keyboard_number.h"
@@ -40,34 +41,13 @@ static void numberedit_cb(lv_event_t* e)
}
}
// create a new class for number edit so we can set the sytles differently if needed.
const lv_obj_class_t lv_numberedit_class = {
.base_class = &lv_obj_class,
.constructor_cb = lv_textarea_class.constructor_cb,
.destructor_cb = lv_textarea_class.destructor_cb,
.user_data = nullptr,
.event_cb = lv_textarea_class.event_cb,
.width_def = LV_DPI_DEF / 2,
.height_def = LV_SIZE_CONTENT,
.editable = LV_OBJ_CLASS_EDITABLE_TRUE,
.group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
.instance_size = sizeof(lv_textarea_t)
};
lv_obj_t *lv_numberedit_create(lv_obj_t *parent)
{
lv_obj_t * obj = lv_obj_class_create_obj(&lv_numberedit_class, parent);
lv_obj_class_init_obj(obj);
return obj;
}
NumberEdit::NumberEdit(Window* parent, const rect_t& rect, int vmin, int vmax,
std::function<int()> getValue,
std::function<void(int)> setValue,
WindowFlags windowFlags, LcdFlags textFlags) :
BaseNumberEdit(parent, rect, vmin, vmax, std::move(getValue),
std::move(setValue), windowFlags, textFlags,
lv_numberedit_create)
field_edit_create)
{
// properties
lv_obj_set_scrollbar_mode(lvobj, LV_SCROLLBAR_MODE_OFF);
+5 -2
View File
@@ -19,7 +19,10 @@
#include "textedit.h"
#include "font.h"
#include "libopenui_config.h"
#include "widgets/field_edit.h"
// #include "libopenui_config.h"
#if !defined(STR_EDIT)
#define STR_EDIT "Edit"
@@ -45,7 +48,7 @@ TextEdit::TextEdit(Window *parent, const rect_t &rect, char *value,
uint8_t length, LcdFlags windowFlags,
const char *_extra_chars) :
FormField(parent, rect, windowFlags, 0,
lv_textarea_create),
field_edit_create),
value(value),
length(length)
{
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (C) OpenTX
*
* Source:
* https://github.com/opentx/libopenui
*
* This file is a part of libopenui library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "field_edit.h"
#define MY_CLASS &field_edit_class
static void field_edit_event(const lv_obj_class_t* class_p, lv_event_t* e)
{
/* Call the ancestor's event handler */
lv_obj_event_base(MY_CLASS, e);
}
const lv_obj_class_t field_edit_class = {
.base_class = &lv_textarea_class,
.event_cb = field_edit_event,
.width_def = LV_DPI_DEF / 2,
.height_def = LV_SIZE_CONTENT,
.editable = LV_OBJ_CLASS_EDITABLE_TRUE,
.group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
.instance_size = sizeof(lv_textarea_t)
};
lv_obj_t* field_edit_create(lv_obj_t* parent)
{
lv_obj_t* obj = lv_obj_class_create_obj(MY_CLASS, parent);
lv_obj_class_init_obj(obj);
return obj;
}
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright (C) OpenTX
*
* Source:
* https://github.com/opentx/libopenui
*
* This file is a part of libopenui library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef FIELD_EDIT_H
#define FIELD_EDIT_H
#include <lvgl/lvgl.h>
#ifdef __cplusplus
extern "C" {
#endif
extern const lv_obj_class_t field_edit_class;
lv_obj_t* field_edit_create(lv_obj_t* parent);
#ifdef __cplusplus
};
#endif
#endif
@@ -0,0 +1,41 @@
/*
* Copyright (C) OpenTX
*
* Source:
* https://github.com/opentx/libopenui
*
* This file is a part of libopenui library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "field_edit.h"
#define MY_CLASS &field_edit_class
const lv_obj_class_t field_edit_class = {
.base_class = &lv_obj_class,
.constructor_cb = lv_textarea_class.constructor_cb,
.destructor_cb = lv_textarea_class.destructor_cb,
.event_cb = lv_textarea_class.event_cb,
.width_def = LV_DPI_DEF / 2,
.height_def = LV_SIZE_CONTENT,
.editable = LV_OBJ_CLASS_EDITABLE_TRUE,
.group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
.instance_size = sizeof(lv_textarea_t)
};
lv_obj_t* field_edit_create(lv_obj_t* parent)
{
lv_obj_t* obj = lv_obj_class_create_obj(MY_CLASS, parent);
lv_obj_class_init_obj(obj);
return obj;
}
@@ -0,0 +1,36 @@
/*
* Copyright (C) OpenTX
*
* Source:
* https://github.com/opentx/libopenui
*
* This file is a part of libopenui library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef FIELD_EDIT_H
#define FIELD_EDIT_H
#include <lvgl/lvgl.h>
#ifdef __cplusplus
extern "C" {
#endif
extern const lv_obj_class_t field_edit_class;
lv_obj_t* field_edit_create(lv_obj_t* parent);
#ifdef __cplusplus
};
#endif
#endif
+4 -4
View File
@@ -117,12 +117,12 @@ extern "C" void window_event_cb(lv_event_t * e)
lv_point_t* p = (lv_point_t*)lv_event_get_param(e);
lv_coord_t scroll_bottom = lv_obj_get_scroll_bottom(target);
// TRACE("SCROLL[x=%d;y=%d;top=%d;bottom=%d]", p->x, p->y,
// scroll_y, scroll_bottom);
TRACE("SCROLL[x=%d;y=%d;top=%d;bottom=%d]", p->x, p->y,
scroll_y, scroll_bottom);
if (scroll_y <= lv_dpx(45) && p->y > 0) {
if (scroll_y <= 45 && p->y > 0) {
lv_obj_scroll_by(target, 0, scroll_y, LV_ANIM_OFF);
} else if (scroll_bottom <= lv_dpx(16) && p->y < 0) {
} else if (scroll_bottom <= 16 && p->y < 0) {
lv_obj_scroll_by(target, 0, -scroll_bottom, LV_ANIM_OFF);
}
}