GuiLite/workspace/widgets_include/spinbox.h

109 lines
2.9 KiB
C
Raw Normal View History

#pragma once
2017-12-06 21:43:47 +08:00
2020-01-22 11:03:29 +08:00
#include "../core_include/api.h"
#include "../core_include/wnd.h"
#include "../core_include/resource.h"
#include "../core_include/word.h"
#include "../core_include/display.h"
#include "../core_include/theme.h"
#include "../widgets_include/button.h"
2020-09-10 14:28:42 +08:00
#define ID_BT_ARROW_UP 0x1111
#define ID_BT_ARROW_DOWN 0x2222
2017-12-06 21:43:47 +08:00
class c_spin_box;
class c_spin_button : public c_button
{
friend class c_spin_box;
2020-01-22 11:03:29 +08:00
inline virtual void on_touch(int x, int y, TOUCH_ACTION action);
c_spin_box* m_spin_box;
};
2017-12-06 21:43:47 +08:00
class c_spin_box : public c_wnd
{
friend class c_spin_button;
2017-12-06 21:43:47 +08:00
public:
short get_value() { return m_value; }
void set_value(unsigned short value) { m_value = m_cur_value = value; }
void set_max_min(short max, short min) { m_max = max; m_min = min; }
void set_step(short step) { m_step = step; }
short get_min() { return m_min; }
short get_max() { return m_max; }
short get_step() { return m_step; }
void set_value_digit(short digit) { m_digit = digit; }
short get_value_digit() { return m_digit; }
2020-10-20 11:57:57 +08:00
void set_on_change(WND_CALLBACK on_change) { this->on_change = on_change; }
2017-12-06 21:43:47 +08:00
protected:
2020-01-22 11:03:29 +08:00
virtual void on_paint()
{
c_rect rect;
get_screen_rect(rect);
2020-07-02 13:39:26 +08:00
rect.m_right = rect.m_left + (rect.width() * 2 / 3);
2020-01-22 11:03:29 +08:00
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_NORMAL), m_z_order);
2021-03-26 11:32:11 +08:00
c_word::draw_value_in_rect(m_surface, m_parent->get_z_order(), m_cur_value, m_digit, rect, m_font, m_font_color, c_theme::get_color(COLOR_WND_NORMAL), ALIGN_HCENTER | ALIGN_VCENTER);
2020-01-22 11:03:29 +08:00
}
virtual void pre_create_wnd()
{
m_attr = (WND_ATTRIBUTION)(ATTR_VISIBLE);
2021-03-26 11:32:11 +08:00
m_font = c_theme::get_font(FONT_DEFAULT);
2020-01-22 11:03:29 +08:00
m_font_color = c_theme::get_color(COLOR_WND_FONT);
m_max = 6;
m_min = 1;
m_digit = 0;
m_step = 1;
//link arrow button position.
c_rect rect;
get_wnd_rect(rect);
2020-01-22 11:03:29 +08:00
m_bt_down.m_spin_box = m_bt_up.m_spin_box = this;
2020-07-02 13:39:26 +08:00
m_bt_up.connect(m_parent, ID_BT_ARROW_UP, "+", (rect.m_left + rect.width() * 2 / 3), rect.m_top, (rect.width() / 3), (rect.height() / 2));
m_bt_down.connect(m_parent, ID_BT_ARROW_DOWN, "-", (rect.m_left + rect.width() * 2 / 3), (rect.m_top + rect.height() / 2), (rect.width() / 3), (rect.height() / 2));
2020-01-22 11:03:29 +08:00
}
void on_arrow_up_bt_click()
{
if (m_cur_value + m_step > m_max)
{
return;
}
m_cur_value += m_step;
2020-10-20 11:57:57 +08:00
if(on_change)
{
(m_parent->*(on_change))(m_id, m_cur_value);
}
2020-01-22 11:03:29 +08:00
on_paint();
}
void on_arrow_down_bt_click()
{
if (m_cur_value - m_step < m_min)
{
return;
}
m_cur_value -= m_step;
2020-10-20 11:57:57 +08:00
if(on_change)
{
(m_parent->*(on_change))(m_id, m_cur_value);
}
2020-01-22 11:03:29 +08:00
on_paint();
}
2017-12-06 21:43:47 +08:00
short m_cur_value;
short m_value;
short m_step;
short m_max;
short m_min;
short m_digit;
c_spin_button m_bt_up;
c_spin_button m_bt_down;
2020-10-20 11:57:57 +08:00
WND_CALLBACK on_change;
2017-12-06 21:43:47 +08:00
};
2020-01-22 11:03:29 +08:00
inline void c_spin_button::on_touch(int x, int y, TOUCH_ACTION action)
{
if (action == TOUCH_UP)
{
(m_id == ID_BT_ARROW_UP) ? m_spin_box->on_arrow_up_bt_click() : m_spin_box->on_arrow_down_bt_click();
}
c_button::on_touch(x, y, action);
}