diff options
Diffstat (limited to 'src/gwin/gwin.c')
-rw-r--r-- | src/gwin/gwin.c | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/src/gwin/gwin.c b/src/gwin/gwin.c index 88be0c88..923567ef 100644 --- a/src/gwin/gwin.c +++ b/src/gwin/gwin.c @@ -180,18 +180,20 @@ GHandle gwinGWindowCreate(GDisplay *g, GWindowObject *pgw, const GWindowInit *pI } void gwinDestroy(GHandle gh) { + if (!gh) { + // should log a runtime error here + return; + } + #if GWIN_NEED_HIERARCHY - // fix hierarchy structure - if (gh->parent->child == gh) { - // we are the first child - gh->parent->child = gh->sibling; - } else { - // find our predecessor - GHandle tmp = gh->parent->child; - while (tmp->sibling != gh) - tmp = tmp->sibling; - tmp->sibling = gh->sibling; - } + GHandle tmp; + + // recursively destroy our children first + for(tmp = gh->child; tmp; tmp = tmp->sibling) + gwinDestroy(tmp); + + // remove myself from the hierarchy + gwinRemoveChild(gh); #endif // Make the window invisible @@ -332,6 +334,32 @@ void gwinRedraw(GHandle gh) { gwinRedraw(parent); } + void gwinRemoveChild(GHandle gh) { + if(!gh || !gh->parent) { + // without a parent, removing is impossible + // should log a runtime error here + return; + } + + if (gh->parent->child == gh) { + // we are the first child, update parent + gh->parent->child = gh->sibling; + } else { + // otherwise find our predecessor + GHandle tmp = gh->parent->child; + while (tmp && tmp->sibling != gh) + tmp = tmp->sibling; + + if(!tmp) { + // our parent's children list is corrupted + // should log a runtime error here + return; + } + + tmp->sibling = gh->sibling; + } + } + GHandle gwinGetFirstChild(GHandle gh) { return gh->child; } |