GuiLite/widgets/edit.cpp

196 lines
5.2 KiB
C++
Raw Normal View History

2019-08-08 12:53:31 +08:00
#include "../core_include/api.h"
#include "../core_include/rect.h"
#include "../core_include/cmd_target.h"
#include "../core_include/wnd.h"
#include "../core_include/resource.h"
#include "../core_include/word.h"
#include "../core_include/surface.h"
#include "../core_include/theme.h"
#include "../widgets_include/button.h"
#include "../widgets_include/label.h"
2019-06-14 14:00:23 +08:00
#include "../widgets_include/keyboard.h"
#include "../widgets_include/edit.h"
#include "../widgets_include/keyboard.h"
2017-12-06 21:43:47 +08:00
#include <string.h>
2019-06-14 14:00:23 +08:00
#define IDD_KEY_BOARD 0x1
2017-12-06 21:43:47 +08:00
2018-12-02 22:39:43 +08:00
GL_BEGIN_MESSAGE_MAP(c_edit)
2019-06-14 14:00:23 +08:00
ON_KEYBORAD_UPDATE(IDD_KEY_BOARD, c_edit::on_key_board_click)
2018-12-02 22:39:43 +08:00
GL_END_MESSAGE_MAP()
2017-12-06 21:43:47 +08:00
static c_keyboard s_keyboard;
void c_edit::pre_create_wnd()
{
2019-07-03 16:45:58 +08:00
m_attr = (WND_ATTRIBUTION)(ATTR_VISIBLE | ATTR_FOCUS);
2019-06-14 14:00:23 +08:00
m_kb_style = STYLE_ALL_BOARD;
m_font_type = c_theme::get_font(FONT_DEFAULT);
m_font_color = c_theme::get_color(COLOR_WND_FONT);
2017-12-06 21:43:47 +08:00
memset(m_str_input, 0, sizeof(m_str_input));
memset(m_str, 0, sizeof(m_str));
2019-05-24 10:20:40 +08:00
set_text(c_wnd::m_str);
2017-12-06 21:43:47 +08:00
}
void c_edit::set_text(const char* str)
{
2019-07-03 16:45:58 +08:00
if (str != 0 && strlen(str) < sizeof(m_str))
2017-12-06 21:43:47 +08:00
{
strcpy(m_str, str);
}
}
bool c_edit::on_touch(int x, int y, TOUCH_ACTION action)
{
(action == TOUCH_DOWN) ? on_touch_down(x, y) : on_touch_up(x, y);
return true;
}
2017-12-06 21:43:47 +08:00
void c_edit::on_touch_down(int x, int y)
{
c_rect kb_rect_relate_2_edit_parent;
s_keyboard.get_wnd_rect(kb_rect_relate_2_edit_parent);
kb_rect_relate_2_edit_parent.m_left += m_wnd_rect.m_left;
kb_rect_relate_2_edit_parent.m_right += m_wnd_rect.m_left;
kb_rect_relate_2_edit_parent.m_top += m_wnd_rect.m_top;
kb_rect_relate_2_edit_parent.m_bottom += m_wnd_rect.m_top;
if (m_wnd_rect.PtInRect(x, y))
{//click edit box
if (STATUS_NORMAL == m_status)
{
m_parent->set_child_focus(this);
2017-12-06 21:43:47 +08:00
}
}
else if (kb_rect_relate_2_edit_parent.PtInRect(x,y))
{//click key board
c_wnd::on_touch(x, y, TOUCH_DOWN);
2017-12-06 21:43:47 +08:00
}
else
{
if (STATUS_PUSHED == m_status)
{
2018-12-28 15:56:36 +08:00
m_status = STATUS_FOCUSED;
2017-12-06 21:43:47 +08:00
on_paint();
}
}
}
void c_edit::on_touch_up(int x, int y)
{
if (STATUS_FOCUSED == m_status)
{
2018-12-28 15:56:36 +08:00
m_status = STATUS_PUSHED;
2017-12-06 21:43:47 +08:00
on_paint();
}
else if (STATUS_PUSHED == m_status)
{
if (m_wnd_rect.PtInRect(x,y))
{//click edit box
2018-12-28 15:56:36 +08:00
m_status = STATUS_FOCUSED;
2017-12-06 21:43:47 +08:00
on_paint();
}
else
{
c_wnd::on_touch(x, y, TOUCH_UP);
2017-12-06 21:43:47 +08:00
}
}
}
void c_edit::on_focus()
{
2018-12-28 15:56:36 +08:00
m_status = STATUS_FOCUSED;
2017-12-06 21:43:47 +08:00
on_paint();
}
void c_edit::on_kill_focus()
{
2018-12-28 15:56:36 +08:00
m_status = STATUS_NORMAL;
2017-12-06 21:43:47 +08:00
on_paint();
}
void c_edit::on_paint()
{
c_rect rect;
get_screen_rect(rect);
c_rect empty_rect;
empty_rect.Empty();
switch(m_status)
{
2018-10-04 14:30:29 +08:00
case STATUS_NORMAL:
2017-12-06 21:43:47 +08:00
if (m_z_order > m_parent->get_z_order())
{
s_keyboard.disconnect();
m_surface->set_frame_layer_visible_rect(empty_rect, s_keyboard.get_z_order());
2017-12-06 21:43:47 +08:00
m_z_order = m_parent->get_z_order();
2019-07-03 16:45:58 +08:00
m_attr = (WND_ATTRIBUTION)(ATTR_VISIBLE | ATTR_FOCUS);
2017-12-06 21:43:47 +08:00
}
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_NORMAL), m_z_order);
2019-07-03 16:45:58 +08:00
c_word::draw_string_in_rect(m_surface, m_parent->get_z_order(), m_str, rect, m_font_type, m_font_color, c_theme::get_color(COLOR_WND_NORMAL), ALIGN_HCENTER | ALIGN_VCENTER);
2017-12-06 21:43:47 +08:00
break;
2018-10-04 14:30:29 +08:00
case STATUS_FOCUSED:
2017-12-06 21:43:47 +08:00
if (m_z_order > m_parent->get_z_order())
{
s_keyboard.disconnect();
m_surface->set_frame_layer_visible_rect(empty_rect, s_keyboard.get_z_order());
2017-12-06 21:43:47 +08:00
m_z_order = m_parent->get_z_order();
2019-07-03 16:45:58 +08:00
m_attr = (WND_ATTRIBUTION)(ATTR_VISIBLE | ATTR_FOCUS);
2017-12-06 21:43:47 +08:00
}
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_FOCUS), m_z_order);
2019-07-03 16:45:58 +08:00
c_word::draw_string_in_rect(m_surface, m_parent->get_z_order(), m_str, rect, m_font_type, m_font_color, c_theme::get_color(COLOR_WND_FOCUS), ALIGN_HCENTER | ALIGN_VCENTER);
2018-10-04 14:30:29 +08:00
break;
case STATUS_PUSHED:
if (m_z_order == m_parent->get_z_order())
{
m_z_order++;
2019-07-03 16:45:58 +08:00
m_attr = (WND_ATTRIBUTION)(ATTR_VISIBLE | ATTR_FOCUS | ATTR_MODAL);
2018-10-04 14:30:29 +08:00
show_keyboard();
}
m_surface->fill_rect(rect.m_left, rect.m_top, rect.m_right, rect.m_bottom, c_theme::get_color(COLOR_WND_PUSHED), m_parent->get_z_order());
m_surface->draw_rect(rect.m_left, rect.m_top, rect.m_right, rect.m_bottom, c_theme::get_color(COLOR_WND_BORDER), m_parent->get_z_order(), 2);
2019-07-03 16:45:58 +08:00
strlen(m_str_input) ? c_word::draw_string_in_rect(m_surface, m_parent->get_z_order(), m_str_input, rect, m_font_type, m_font_color, c_theme::get_color(COLOR_WND_PUSHED), ALIGN_HCENTER | ALIGN_VCENTER) :
c_word::draw_string_in_rect(m_surface, m_parent->get_z_order(), m_str, rect, m_font_type, m_font_color, c_theme::get_color(COLOR_WND_PUSHED), ALIGN_HCENTER | ALIGN_VCENTER);
2017-12-06 21:43:47 +08:00
break;
default:
2019-07-03 16:45:58 +08:00
ASSERT(false);
2017-12-06 21:43:47 +08:00
}
}
void c_edit::show_keyboard()
{
2019-06-14 14:00:23 +08:00
s_keyboard.connect(this, IDD_KEY_BOARD, m_kb_style);
2017-12-06 21:43:47 +08:00
c_rect kb_rect;
s_keyboard.get_screen_rect(kb_rect);
m_surface->set_frame_layer_visible_rect(kb_rect, s_keyboard.get_z_order());
2017-12-06 21:43:47 +08:00
s_keyboard.show_window();
}
void c_edit::on_key_board_click(unsigned int ctrl_id, long param)
{
switch (param)
{
case CLICK_CHAR:
strcpy(m_str_input, s_keyboard.get_str());
on_paint();
break;
case CLICK_ENTER:
if (strlen(m_str_input))
{
memcpy(m_str, m_str_input, sizeof(m_str_input));
}
2018-12-28 15:56:36 +08:00
m_status = STATUS_FOCUSED;
2017-12-06 21:43:47 +08:00
on_paint();
break;
case CLICK_ESC:
memset(m_str_input, 0, sizeof(m_str_input));
2018-12-28 15:56:36 +08:00
m_status = STATUS_FOCUSED;
2017-12-06 21:43:47 +08:00
on_paint();
break;
default:
2019-07-03 16:45:58 +08:00
ASSERT(false);
2017-12-06 21:43:47 +08:00
break;
}
}