#include "args.h"
#include "../ui/common/args.h"
#define DEFAULT_ARG_FUNCS(var, prefix) \
static ArgDefaultFuncs var = { \
.fill = (argfunc_set_bool)prefix##_args_set_fill, \
.hexpand = (argfunc_set_bool)prefix##_args_set_hexpand, \
.vexpand = (argfunc_set_bool)prefix##_args_set_vexpand, \
.hfill = (argfunc_set_bool)prefix##_args_set_hfill, \
.vfill = (argfunc_set_bool)prefix##_args_set_vfill, \
.override_defaults = (argfunc_set_bool)prefix##_args_set_override_defaults, \
.margin = (argfunc_set_int)prefix##_args_set_margin, \
.margin_left = (argfunc_set_int)prefix##_args_set_margin_left, \
.margin_right = (argfunc_set_int)prefix##_args_set_margin_right, \
.margin_top = (argfunc_set_int)prefix##_args_set_margin_top, \
.margin_bottom = (argfunc_set_int)prefix##_args_set_margin_bottom, \
.colspan = (argfunc_set_int)prefix##_args_set_colspan, \
.rowspan = (argfunc_set_int)prefix##_args_set_rowspan, \
.name = (argfunc_set_str)prefix##_args_set_name, \
.style_class = (argfunc_set_str)prefix##_args_set_style_class \
}
DEFAULT_ARG_FUNCS(container_args, ui_container);
DEFAULT_ARG_FUNCS(button_args, ui_button);
static void init_common_args(
const CxJsonValue *value,
void *args, ArgDefaultFuncs *funcs) {
CxJsonValue *val;
val = cxJsonObjGet(value,
"fill");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
funcs->fill(args,
TRUE);
}
val = cxJsonObjGet(value,
"hexpand");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
funcs->hexpand(args,
TRUE);
}
val = cxJsonObjGet(value,
"vexpand");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
funcs->vexpand(args,
TRUE);
}
val = cxJsonObjGet(value,
"hfill");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
funcs->hfill(args,
TRUE);
}
val = cxJsonObjGet(value,
"vfill");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
funcs->vfill(args,
TRUE);
}
val = cxJsonObjGet(value,
"override_defaults");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
funcs->override_defaults(args,
TRUE);
}
val = cxJsonObjGet(value,
"margin");
if(val && val->type ==
CX_JSON_INTEGER) {
funcs->margin(args, (
int)val->value.integer);
}
val = cxJsonObjGet(value,
"margin_left");
if(val && val->type ==
CX_JSON_INTEGER) {
funcs->margin_left(args, (
int)val->value.integer);
}
val = cxJsonObjGet(value,
"margin_right");
if(val && val->type ==
CX_JSON_INTEGER) {
funcs->margin_right(args, (
int)val->value.integer);
}
val = cxJsonObjGet(value,
"margin_top");
if(val && val->type ==
CX_JSON_INTEGER) {
funcs->margin_top(args, (
int)val->value.integer);
}
val = cxJsonObjGet(value,
"margin_bottom");
if(val && val->type ==
CX_JSON_INTEGER) {
funcs->margin_bottom(args, (
int)val->value.integer);
}
val = cxJsonObjGet(value,
"colspan");
if(val && val->type ==
CX_JSON_INTEGER) {
funcs->colspan(args, (
int)val->value.integer);
}
val = cxJsonObjGet(value,
"rowspan");
if(val && val->type ==
CX_JSON_INTEGER) {
funcs->rowspan(args, (
int)val->value.integer);
}
val = cxJsonObjGet(value,
"name");
if(val && val->type ==
CX_JSON_STRING) {
funcs->name(args, val->value.string.ptr);
}
val = cxJsonObjGet(value,
"style_class");
if(val && val->type ==
CX_JSON_STRING) {
funcs->name(args, val->value.string.ptr);
}
}
void init_states(
const CxJsonValue *value,
void *args, argfunc_set_intarray setarray) {
CxJsonValue *val = cxJsonObjGet(value,
"states");
if(!val || val->type !=
CX_JSON_ARRAY) {
return;
}
int len = (
int)val->value.array.array_size;
int *states = calloc(len,
sizeof(
int));
for(
int i=
0;i<len;i++) {
CxJsonValue *s = val->value.array.array[i];
if(s->type ==
CX_JSON_INTEGER) {
states[i] = (
int)s->value.integer;
}
}
setarray(args, states, len);
}
UiContainerArgs* json2container_args(
const CxJsonValue *value) {
UiContainerArgs *args = ui_container_args_new();
if(value->type !=
CX_JSON_OBJECT) {
return args;
}
init_common_args(value, args, &container_args);
CxJsonValue *val = cxJsonObjGet(value,
"spacing");
if(val && val->type ==
CX_JSON_INTEGER) {
args->spacing = (
int)val->value.integer;
}
val = cxJsonObjGet(value,
"columnspacing");
if(val && val->type ==
CX_JSON_INTEGER) {
args->columnspacing = (
int)val->value.integer;
}
val = cxJsonObjGet(value,
"rowspacing");
if(val && val->type ==
CX_JSON_INTEGER) {
args->rowspacing = (
int)val->value.integer;
}
val = cxJsonObjGet(value,
"def_hfill");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
args->def_hfill =
TRUE;
}
val = cxJsonObjGet(value,
"def_vfill");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
args->def_vfill =
TRUE;
}
val = cxJsonObjGet(value,
"def_hexpand");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
args->def_hexpand =
TRUE;
}
val = cxJsonObjGet(value,
"def_vexpand");
if(val && val->type ==
CX_JSON_LITERAL && val->value.literal ==
CX_JSON_TRUE) {
args->def_vexpand =
TRUE;
}
return args;
}
UiButtonArgs* json2button_args(
const CxJsonValue *value) {
UiButtonArgs *args = ui_button_args_new();
if(value->type !=
CX_JSON_OBJECT) {
return args;
}
init_common_args(value, args, &button_args);
init_states(value, args, (argfunc_set_intarray)ui_button_args_set_states);
CxJsonValue *val = cxJsonObjGet(value,
"label");
if(val && val->type ==
CX_JSON_STRING) {
ui_button_args_set_label(args, val->value.string.ptr);
}
val = cxJsonObjGet(value,
"icon");
if(val && val->type ==
CX_JSON_STRING) {
ui_button_args_set_icon(args, val->value.string.ptr);
}
val = cxJsonObjGet(value,
"tooltip");
if(val && val->type ==
CX_JSON_STRING) {
ui_button_args_set_tooltip(args, val->value.string.ptr);
}
val = cxJsonObjGet(value,
"labeltype");
if(val && val->type ==
CX_JSON_INTEGER) {
ui_button_args_set_labeltype(args, (
int)val->value.integer);
}
return args;
}
UiToggleArgs* json2toggle_args(
const CxJsonValue *value) {
UiToggleArgs *args = ui_toggle_args_new();
if(value->type !=
CX_JSON_OBJECT) {
return args;
}
init_common_args(value, args, &button_args);
init_states(value, args, (argfunc_set_intarray)ui_button_args_set_states);
CxJsonValue *val = cxJsonObjGet(value,
"label");
if(val && val->type ==
CX_JSON_STRING) {
ui_toggle_args_set_label(args, val->value.string.ptr);
}
val = cxJsonObjGet(value,
"icon");
if(val && val->type ==
CX_JSON_STRING) {
ui_toggle_args_set_icon(args, val->value.string.ptr);
}
val = cxJsonObjGet(value,
"tooltip");
if(val && val->type ==
CX_JSON_STRING) {
ui_toggle_args_set_tooltip(args, val->value.string.ptr);
}
val = cxJsonObjGet(value,
"labeltype");
if(val && val->type ==
CX_JSON_INTEGER) {
ui_toggle_args_set_labeltype(args, (
int)val->value.integer);
}
val = cxJsonObjGet(value,
"enable_group");
if(val && val->type ==
CX_JSON_INTEGER) {
ui_toggle_args_set_enablestate(args, (
int)val->value.integer);
}
return args;
}