clay/src/clay-layout.c

78 lines
2.4 KiB
C

#include <malloc.h>
#include <stdarg.h>
#include <assert.h>
#include "clay-layout.h"
#include "clay-context.h"
#include "clay-property.h"
clay clay_create_layout(clay_ctx ctx, struct layout_class class) {
clay layout = malloc(sizeof(*layout));
assert(layout != NULL);
layout->properties = clay_propset_create();
layout->class = class;
layout->ctx = ctx;
if (layout->class.init != NULL) {
layout->class.init(layout);
}
clay_ctx_register_layout(ctx, layout);
return layout;
}
void clay_destroy_layout(clay layout) {
clay_ctx_unregister_layout(layout->ctx, layout);
clay_cleanup_layout(layout);
free(layout);
}
void clay_cleanup_layout(clay layout) {
if (layout->class.cleanup != NULL) {
layout->class.cleanup(layout);
}
clay_propset_destroy(layout->properties);
}
clay clay_clone(clay layout) {
clay cloned = malloc(sizeof(*cloned));
cloned->class = layout->class;
cloned->ctx = layout->ctx;
cloned->properties = clay_propset_clone(layout->properties);
return cloned;
}
clay_property_value clay_get_prop(clay layout, const char *prop) {
clay_ctx ctx = layout->ctx;
clay_property_desc desc = clay_ctx_get_property_desc(ctx, prop);
assert(desc != NULL);
assert(desc->type != CLAY_PROPERTY_NOT_SET);
return clay_property_get_by_tag(layout->properties, desc->tag);
}
void clay_set(clay layout, const char *prop, ...) {
// TODO: use a macro so that a property can have values of different types
// the property registry should not keep the type then
// also the property registry can have some kind of alias mechanism for translating strings to enums or something
clay_ctx ctx = layout->ctx;
clay_property_desc desc = clay_ctx_get_property_desc(ctx, prop);
assert(desc != NULL);
assert(desc->type != CLAY_PROPERTY_NOT_SET);
struct clay_property_value value;
value.type = desc->type;
va_list args;
va_start(args, prop);
switch (desc->type) {
case CLAY_PROPERTY_NOT_SET:
break;
case CLAY_PROPERTY_INT:
value.int_val = va_arg(args, int);
break;
case CLAY_PROPERTY_FLOAT:
value.float_val = va_arg(args, double);
break;
case CLAY_PROPERTY_POINTER:
value.pointer_val = va_arg(args, void*);
break;
}
va_end(args);
clay_property_set_by_tag(layout->properties, desc->tag, value);
}