From ce895ac44faa1de5e10ee77b2331ff36e7201647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 27 Dec 2018 14:21:33 +0100 Subject: [PATCH] particle/empty: placeholder particle, renders nothing But may have margins. --- CMakeLists.txt | 1 + config.c | 20 +++++++++++++++++++- particles/empty.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ particles/empty.h | 4 ++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 particles/empty.c create mode 100644 particles/empty.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ce05345..4f96ceb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ add_executable(f00bar decorations/stack.c decorations/stack.h decorations/underline.c decorations/underline.h + particles/empty.c particles/empty.h particles/list.c particles/list.h particles/map.c particles/map.h particles/ramp.c particles/ramp.h diff --git a/config.c b/config.c index 0617bcf..3325d9e 100644 --- a/config.c +++ b/config.c @@ -13,6 +13,7 @@ #include "decorations/underline.h" #include "particle.h" +#include "particles/empty.h" #include "particles/list.h" #include "particles/map.h" #include "particles/ramp.h" @@ -164,6 +165,21 @@ deco_from_config(const struct yml_node *node) assert(false); } +static struct particle * +particle_empty_from_config(const struct yml_node *node, + const struct font *parent_font) +{ + const struct yml_node *left_margin = yml_get_value(node, "left_margin"); + const struct yml_node *right_margin = yml_get_value(node, "right_margin"); + + assert(left_margin == NULL || yml_is_scalar(left_margin)); + assert(right_margin == NULL || yml_is_scalar(right_margin)); + + return particle_empty_new( + left_margin != NULL ? yml_value_as_int(left_margin) : 0, + right_margin != NULL ? yml_value_as_int(right_margin) : 0); +} + static struct particle * particle_string_from_config(const struct yml_node *node, const struct font *parent_font) @@ -318,7 +334,9 @@ particle_from_config(const struct yml_node *node, const struct font *parent_font const char *type = yml_value_as_string(pair.key); struct particle *ret = NULL; - if (strcmp(type, "string") == 0) + if (strcmp(type, "empty") == 0) + ret = particle_empty_from_config(pair.value, parent_font); + else if (strcmp(type, "string") == 0) ret = particle_string_from_config(pair.value, parent_font); else if (strcmp(type, "list") == 0) ret = particle_list_from_config(pair.value, parent_font); diff --git a/particles/empty.c b/particles/empty.c new file mode 100644 index 0000000..92b55b9 --- /dev/null +++ b/particles/empty.c @@ -0,0 +1,45 @@ +#include "empty.h" + +#include + +static void +exposable_destroy(struct exposable *exposable) +{ + free(exposable); +} + +static int +begin_expose(struct exposable *exposable, cairo_t *cr) +{ + exposable->width = exposable->particle->left_margin + + exposable->particle->right_margin; + return exposable->width; +} + +static void +expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height) +{ + const struct deco *deco = exposable->particle->deco; + if (deco != NULL) + deco->expose(deco, cr, x, y, exposable->width, height); +} + +static struct exposable * +instantiate(const struct particle *particle, const struct tag_set *tags) +{ + struct exposable *exposable = malloc(sizeof(*exposable)); + exposable->particle = particle; + exposable->destroy = &exposable_destroy; + exposable->begin_expose = &begin_expose; + exposable->expose = &expose; + return exposable; +} + +struct particle * +particle_empty_new(int left_margin, int right_margin) +{ + struct particle *particle = particle_common_new(left_margin, right_margin); + particle->destroy = &particle_default_destroy; + particle->instantiate = &instantiate; + return particle; +} diff --git a/particles/empty.h b/particles/empty.h new file mode 100644 index 0000000..c17b034 --- /dev/null +++ b/particles/empty.h @@ -0,0 +1,4 @@ +#pragma once +#include "../particle.h" + +struct particle *particle_empty_new(int left_margin, int right_margin);