mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:28:11 +00:00
LibGUI: Add a spinbox widget.
This is essentially a combo widget containing a single-line GTextEditor and two buttons for increment and decrement. The GTextEditor::on_change callback is hooked to prevent non-numeric input but it's not entirely perfect since that callback is asynchronous. This will work until we have some more sophisticated input validation mechanism though.
This commit is contained in:
parent
4ba5e472be
commit
4c0f586f2b
4 changed files with 115 additions and 14 deletions
71
LibGUI/GSpinBox.cpp
Normal file
71
LibGUI/GSpinBox.cpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <LibGUI/GSpinBox.h>
|
||||
#include <LibGUI/GButton.h>
|
||||
#include <LibGUI/GTextEditor.h>
|
||||
|
||||
GSpinBox::GSpinBox(GWidget* parent)
|
||||
: GWidget(parent)
|
||||
{
|
||||
m_editor = new GTextEditor(GTextEditor::Type::SingleLine, this);
|
||||
m_editor->on_change = [this] {
|
||||
bool ok;
|
||||
int value = m_editor->text().to_uint(ok);
|
||||
if (ok)
|
||||
set_value(value);
|
||||
else
|
||||
m_editor->set_text(String::format("%d", m_value));
|
||||
};
|
||||
m_increment_button = new GButton(this);
|
||||
m_increment_button->set_caption("+");
|
||||
m_increment_button->on_click = [this] (GButton&) { set_value(m_value + 1); };
|
||||
m_decrement_button = new GButton(this);
|
||||
m_decrement_button->set_caption("-");
|
||||
m_decrement_button->on_click = [this] (GButton&) { set_value(m_value - 1); };
|
||||
}
|
||||
|
||||
GSpinBox::~GSpinBox()
|
||||
{
|
||||
}
|
||||
|
||||
void GSpinBox::set_value(int value)
|
||||
{
|
||||
if (value < m_min)
|
||||
value = m_min;
|
||||
if (value > m_max)
|
||||
value = m_max;
|
||||
if (m_value == value)
|
||||
return;
|
||||
m_value = value;
|
||||
m_editor->set_text(String::format("%d", value));
|
||||
update();
|
||||
if (on_change)
|
||||
on_change(value);
|
||||
}
|
||||
|
||||
void GSpinBox::set_range(int min, int max)
|
||||
{
|
||||
ASSERT(min <= max);
|
||||
if (m_min == min && m_max == max)
|
||||
return;
|
||||
|
||||
m_min = min;
|
||||
m_max = max;
|
||||
|
||||
int old_value = m_value;
|
||||
if (m_value < m_min)
|
||||
m_value = m_min;
|
||||
if (m_value > m_max)
|
||||
m_value = m_max;
|
||||
if (on_change && m_value != old_value)
|
||||
on_change(m_value);
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void GSpinBox::resize_event(GResizeEvent& event)
|
||||
{
|
||||
int button_height = event.size().height() / 2;
|
||||
int button_width = 16;
|
||||
m_increment_button->set_relative_rect(width() - button_width, 0, button_width, button_height);
|
||||
m_decrement_button->set_relative_rect(width() - button_width, button_height, button_width, button_height);
|
||||
m_editor->set_relative_rect(0, 0, width() - button_width, height());
|
||||
}
|
31
LibGUI/GSpinBox.h
Normal file
31
LibGUI/GSpinBox.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibGUI/GWidget.h>
|
||||
|
||||
class GButton;
|
||||
class GTextEditor;
|
||||
|
||||
class GSpinBox : public GWidget {
|
||||
public:
|
||||
GSpinBox(GWidget* parent = nullptr);
|
||||
virtual ~GSpinBox() override;
|
||||
|
||||
int value() const { return m_value; }
|
||||
void set_value(int);
|
||||
|
||||
void set_range(int min, int max);
|
||||
|
||||
Function<void(int value)> on_change;
|
||||
|
||||
protected:
|
||||
virtual void resize_event(GResizeEvent&) override;
|
||||
|
||||
private:
|
||||
GTextEditor* m_editor { nullptr };
|
||||
GButton* m_increment_button { nullptr };
|
||||
GButton* m_decrement_button { nullptr };
|
||||
|
||||
int m_min { 0 };
|
||||
int m_max { 100 };
|
||||
int m_value { 0 };
|
||||
};
|
|
@ -63,6 +63,7 @@ LIBGUI_OBJS = \
|
|||
GHttpRequest.o \
|
||||
GHttpResponse.o \
|
||||
GHttpJob.o \
|
||||
GSpinBox.o \
|
||||
GWindow.o
|
||||
|
||||
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue