LibGUI: Add an "exclusive" property to GAbstractButtons.

This makes checkable buttons exclusive with other checkable buttons in the
same parent widget. Basically like radio buttons. This should probably be
used to implement GRadioButton once it's a bit more mature.
This commit is contained in:
Andreas Kling 2019-06-12 05:57:26 +02:00
parent cadc65a82d
commit d7756fc09f
Notes: sideshowbarker 2024-07-19 13:39:16 +09:00
4 changed files with 31 additions and 0 deletions

View File

@ -29,6 +29,20 @@ void GAbstractButton::set_checked(bool checked)
if (m_checked == checked)
return;
m_checked = checked;
if (is_exclusive() && checked) {
parent_widget()->for_each_child_of_type<GAbstractButton>([&] (auto& sibling) {
if (!sibling.is_exclusive() || !sibling.is_checkable() || !sibling.is_checked())
return IterationDecision::Continue;
sibling.m_checked = false;
sibling.update();
if (sibling.on_checked)
sibling.on_checked(false);
return IterationDecision::Continue;
});
m_checked = true;
}
update();
if (on_checked)
on_checked(checked);

View File

@ -14,6 +14,9 @@ public:
void set_text(const StringView&);
const String& text() const { return m_text; }
bool is_exclusive() const { return m_exclusive; }
void set_exclusive(bool b) { m_exclusive = b; }
bool is_checked() const { return m_checked; }
void set_checked(bool);
@ -42,9 +45,20 @@ protected:
void paint_text(GPainter&, const Rect&, const Font&, TextAlignment);
private:
virtual bool is_abstract_button() const final { return true; }
String m_text;
bool m_checked { false };
bool m_checkable { false };
bool m_hovered { false };
bool m_being_pressed { false };
bool m_exclusive { false };
};
template<>
inline bool is<GAbstractButton>(const CObject& object)
{
if (!is<GWidget>(object))
return false;
return to<GWidget>(object).is_abstract_button();
}

View File

@ -60,6 +60,8 @@ void GButton::click()
{
if (!is_enabled())
return;
if (is_checkable())
set_checked(!is_checked());
if (on_click)
on_click(*this);
}

View File

@ -198,6 +198,7 @@ public:
}
virtual bool is_radio_button() const { return false; }
virtual bool is_abstract_button() const { return false; }
private:
virtual bool is_widget() const final { return true; }