Skip to content

Commit 826695e

Browse files
committed
feat(gui): add Widget_Each()
1 parent ae5afea commit 826695e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

include/LCUI/gui/widget_tree.h

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ LCUI_API LCUI_Widget Widget_GetNext(LCUI_Widget w);
5757
/** 获取一个子部件 */
5858
LCUI_API LCUI_Widget Widget_GetChild(LCUI_Widget w, size_t index);
5959

60+
/** Traverse the child widget tree */
61+
LCUI_API size_t Widget_Each(LCUI_Widget w,
62+
void (*callback)(LCUI_Widget, void *), void *arg);
63+
6064
/** 获取当前点命中的最上层可见部件 */
6165
LCUI_API LCUI_Widget Widget_At(LCUI_Widget widget, int x, int y);
6266

src/gui/widget_tree.c

+24
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,30 @@ void Widget_PrintTree(LCUI_Widget w)
282282
_LCUIWidget_PrintTree(w, 0, " ");
283283
}
284284

285+
size_t Widget_Each(LCUI_Widget w, void (*callback)(LCUI_Widget, void *),
286+
void *arg)
287+
{
288+
size_t count = 0;
289+
290+
LCUI_Widget next;
291+
LCUI_Widget child = LinkedList_Get(&w->children, 0);
292+
293+
while (child && child != w) {
294+
while (child->children.length > 0) {
295+
child = LinkedList_Get(&child->children, 0);
296+
}
297+
callback(child, arg);
298+
++count;
299+
next = Widget_GetNext(child);
300+
while (!next && child->parent != w) {
301+
child = child->parent;
302+
next = Widget_GetNext(child);
303+
}
304+
child = next;
305+
}
306+
return count;
307+
}
308+
285309
LCUI_Widget Widget_At(LCUI_Widget widget, int ix, int iy)
286310
{
287311
float x, y;

0 commit comments

Comments
 (0)