Skip to content

Commit

Permalink
LibGUI: Add GWidget::for_each_child_widget(callback).
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed May 27, 2019
1 parent 906fde8 commit 723ba91
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
13 changes: 6 additions & 7 deletions LibGUI/GRadioButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ void GRadioButton::for_each_in_group(Callback callback)
{
if (!parent())
return;
for (auto& object : parent()->children()) {
if (!object->is_widget())
continue;
if (!static_cast<GWidget*>(object)->is_radio_button())
continue;
callback(*static_cast<GRadioButton*>(object));
}
for_each_child_widget([&] (auto& child) {
if (!child.is_radio_button())
return IterationDecision::Continue;
return callback(static_cast<GRadioButton&>(child));
});
}

void GRadioButton::click()
Expand All @@ -71,6 +69,7 @@ void GRadioButton::click()
for_each_in_group([this] (auto& button) {
if (&button != this)
button.set_checked(false);
return IterationDecision::Continue;
});
set_checked(true);
}
16 changes: 7 additions & 9 deletions LibGUI/GSplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ void GSplitter::mousedown_event(GMouseEvent& event)
GWidget* first_resizee { nullptr };
GWidget* second_resizee { nullptr };
int fudge = layout()->spacing();
for (auto* child : children()) {
if (!child->is_widget())
continue;
auto& child_widget = *static_cast<GWidget*>(child);
int child_start = m_orientation == Orientation::Horizontal ? child_widget.relative_rect().left() : child_widget.relative_rect().top();
int child_end = m_orientation == Orientation::Horizontal ? child_widget.relative_rect().right() : child_widget.relative_rect().bottom();
for_each_child_widget([&] (auto& child) {
int child_start = m_orientation == Orientation::Horizontal ? child.relative_rect().left() : child.relative_rect().top();
int child_end = m_orientation == Orientation::Horizontal ? child.relative_rect().right() : child.relative_rect().bottom();
if (x_or_y > child_end && (x_or_y - fudge) <= child_end)
first_resizee = &child_widget;
first_resizee = &child;
if (x_or_y < child_start && (x_or_y + fudge) >= child_start)
second_resizee = &child_widget;
}
second_resizee = &child;
return IterationDecision::Continue;
});
ASSERT(first_resizee && second_resizee);
m_first_resizee = first_resizee->make_weak_ptr();
m_second_resizee = second_resizee->make_weak_ptr();
Expand Down
10 changes: 4 additions & 6 deletions LibGUI/GTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ void GTabWidget::child_event(CChildEvent& event)
} else if (event.type() == GEvent::ChildRemoved) {
if (m_active_widget == &child) {
GWidget* new_active_widget = nullptr;
for (auto* new_child : children()) {
if (new_child->is_widget()) {
new_active_widget = static_cast<GWidget*>(new_child);
break;
}
}
for_each_child_widget([&] (auto& new_child) {
new_active_widget = &new_child;
return IterationDecision::Abort;
});
set_active_widget(new_active_widget);
}
}
Expand Down
16 changes: 8 additions & 8 deletions LibGUI/GWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ void GWidget::handle_paint_event(GPaintEvent& event)
#endif
}
paint_event(event);
for (auto* ch : children()) {
auto* child = (GWidget*)ch;
if (!child->is_visible())
continue;
if (child->relative_rect().intersects(event.rect())) {
GPaintEvent local_event(event.rect().intersected(child->relative_rect()).translated(-child->relative_position()));
child->event(local_event);
for_each_child_widget([&] (auto& child) {
if (!child.is_visible())
return IterationDecision::Continue;
if (child.relative_rect().intersects(event.rect())) {
GPaintEvent local_event(event.rect().intersected(child.relative_rect()).translated(-child.relative_position()));
child.event(local_event);
}
}
return IterationDecision::Continue;
});
second_paint_event(event);
}

Expand Down
10 changes: 10 additions & 0 deletions LibGUI/GWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ class GWidget : public CObject {
void register_local_shortcut_action(Badge<GAction>, GAction&);
void unregister_local_shortcut_action(Badge<GAction>, GAction&);

template<typename Callback>
void for_each_child_widget(Callback callback)
{
for_each_child([&] (auto& child) {
if (child.is_widget())
return callback(static_cast<GWidget&>(child));
return IterationDecision::Continue;
});
}

virtual bool is_radio_button() const { return false; }

private:
Expand Down
18 changes: 8 additions & 10 deletions LibGUI/GWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,14 @@ Vector<GWidget*> GWindow::focusable_widgets() const
Function<void(GWidget&)> collect_focusable_widgets = [&] (GWidget& widget) {
if (widget.accepts_focus())
collected_widgets.append(&widget);
for (auto& child : widget.children()) {
if (!child->is_widget())
continue;
auto& child_widget = *static_cast<GWidget*>(child);
if (!child_widget.is_visible())
continue;
if (!child_widget.is_enabled())
continue;
collect_focusable_widgets(child_widget);
}
widget.for_each_child_widget([&] (auto& child) {
if (!child.is_visible())
return IterationDecision::Continue;
if (!child.is_enabled())
return IterationDecision::Continue;
collect_focusable_widgets(child);
return IterationDecision::Continue;
});
};

collect_focusable_widgets(*m_main_widget);
Expand Down

0 comments on commit 723ba91

Please sign in to comment.