Skip to content

Commit f12e00b

Browse files
committed
fix(builder): widget should be initialized before appending
1 parent 56491dd commit f12e00b

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

include/LCUI/gui/builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* ***************************************************************************
22
* builder.h -- The GUI build module, parse UI config code and build UI.
33
*
4-
* Copyright (c) 2018, Liu chao <lc-soft@live.cn> All rights reserved.
4+
* Copyright (c) 2018-2019, Liu chao <lc-soft@live.cn> All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
77
* modification, are permitted provided that the following conditions are met:

src/gui/builder.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* ***************************************************************************
22
* builder.c -- the GUI build module, parse UI config code and build UI.
33
*
4-
* Copyright (c) 2018, Liu chao <lc-soft@live.cn> All rights reserved.
4+
* Copyright (c) 2018-2019, Liu chao <lc-soft@live.cn> All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
77
* modification, are permitted provided that the following conditions are met:
@@ -175,7 +175,7 @@ static int ParseUI(XMLParserContext ctx, xmlNodePtr node)
175175
static int ParseWidget(XMLParserContext ctx, xmlNodePtr node)
176176
{
177177
xmlAttrPtr prop;
178-
char *prop_val = NULL, *prop_name;
178+
char *prop_val = NULL, *prop_name, *type = NULL;
179179
LCUI_Widget w = NULL, parent = ctx->widget;
180180

181181
if (ctx->parent_parser && ctx->parent_parser->id != ID_UI &&
@@ -184,13 +184,6 @@ static int ParseWidget(XMLParserContext ctx, xmlNodePtr node)
184184
}
185185
switch (node->type) {
186186
case XML_ELEMENT_NODE:
187-
w = LCUIWidget_New(NULL);
188-
if (!w) {
189-
return PB_ERROR;
190-
}
191-
DEBUG_MSG("create widget: %p\n", w);
192-
Widget_Append(parent, w);
193-
ctx->widget = w;
194187
break;
195188
case XML_TEXT_NODE:
196189
if (!parent->proto || !parent->proto->settext) {
@@ -203,36 +196,43 @@ static int ParseWidget(XMLParserContext ctx, xmlNodePtr node)
203196
default: return PB_ERROR;
204197
}
205198
for (prop = node->properties; prop; prop = prop->next) {
199+
prop_val = (char *)xmlGetProp(node, prop->name);
200+
if (PropNameIs(prop, "type")) {
201+
type = prop_val;
202+
break;
203+
}
206204
if (prop_val) {
207205
xmlFree(prop_val);
208206
}
207+
}
208+
prop_val = NULL;
209+
w = LCUIWidget_New(type);
210+
if (type) {
211+
xmlFree(type);
212+
}
213+
if (!w) {
214+
return PB_ERROR;
215+
}
216+
DEBUG_MSG("create widget: %s\n", w->type);
217+
Widget_Append(parent, w);
218+
ctx->widget = w;
219+
for (prop = node->properties; prop; prop = prop->next) {
209220
prop_val = (char*)xmlGetProp(node, prop->name);
210-
if (PropNameIs(prop, "type")) {
211-
DEBUG_MSG("widget: %p, set type: %s\n", w, prop_val);
212-
w->proto = LCUIWidget_GetPrototype(prop_val);
213-
if (w->proto && w->proto->init) {
214-
w->proto->init(w);
215-
w->type = w->proto->name;
216-
} else {
217-
w->type = strdup2(prop_val);
218-
}
219-
continue;
220-
} else if (PropNameIs(prop, "id")) {
221+
if (PropNameIs(prop, "id")) {
221222
DEBUG_MSG("widget: %p, set id: %s\n", w, prop_val);
222223
Widget_SetId(w, prop_val);
223-
continue;
224224
} else if (PropNameIs(prop, "class")) {
225225
DEBUG_MSG("widget: %p, add class: %s\n", w, prop_val);
226226
Widget_AddClass(w, prop_val);
227-
continue;
227+
} else {
228+
prop_name = malloc(strsize((const char*)prop->name));
229+
strtolower(prop_name, (const char*)prop->name);
230+
Widget_SetAttribute(w, prop_name, prop_val);
231+
free(prop_name);
232+
}
233+
if (prop_val) {
234+
xmlFree(prop_val);
228235
}
229-
prop_name = malloc(strsize((const char*)prop->name));
230-
strtolower(prop_name, (const char*)prop->name);
231-
Widget_SetAttribute(w, prop_name, prop_val);
232-
free(prop_name);
233-
}
234-
if (prop_val) {
235-
xmlFree(prop_val);
236236
}
237237
return PB_ENTER;
238238
}

0 commit comments

Comments
 (0)