diff options
Diffstat (limited to 'src/layout.c')
| -rw-r--r-- | src/layout.c | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/layout.c b/src/layout.c index 126e2f5..c5969fb 100644 --- a/src/layout.c +++ b/src/layout.c @@ -6,11 +6,12 @@ #include "any_log.h" #include "layout.h" -layout_t *layout_create(block_t *block, layout_info_t info) +void layout_init(layout_t *layout, block_t *block, layout_info_t info) { assert(!block->hidden); - layout_t *layout = calloc(1, sizeof(layout_t)); + memset(layout, 0, sizeof(layout_t)); + layout->block = block; layout->x = info.x_offset; layout->y = 0; @@ -19,20 +20,20 @@ layout_t *layout_create(block_t *block, layout_info_t info) layout->height = info.height; if (block->type == BLOCK_GROUP) { + int x_offset = info.x_offset; layout->children = calloc(block->group.n_children, sizeof(layout_t)); - int x = info.x_offset; for (int i = 0; i < block->group.n_children; i++) { if (block->group.children[i].hidden) continue; - layout_t *child = layout_create(&block->group.children[i], info); - info.x_offset += child->width + block->group.spacing; - layout->children[layout->n_children++] = child; + layout_init(&layout->children[layout->n_children], &block->group.children[i], info); + info.x_offset += layout->children[layout->n_children].width + block->group.spacing; + layout->n_children++; } - layout_t *last = layout->children[layout->n_children - 1]; - layout->width = last->x + last->width - x; + layout_t *last = &layout->children[layout->n_children - 1]; + layout->width = last->x + last->width - x_offset; } else if (block->type == BLOCK_TEXT) { layout->pl = pango_layout_new(info.context); @@ -46,7 +47,11 @@ layout_t *layout_create(block_t *block, layout_info_t info) layout->width = info.height + (length != 1) * layout->text_width; } - return layout; + if (layout->block->max_width > 0 && layout->width > layout->block->max_width) + layout->width = layout->block->max_width; + + if (layout->block->min_width > 0 && layout->width < layout->block->min_width) + layout->width = layout->block->min_width; } void layout_render(layout_t *layout, cairo_t *cr) @@ -79,7 +84,7 @@ void layout_render(layout_t *layout, cairo_t *cr) if (layout->block->type == BLOCK_GROUP) { for (int i = 0; i < layout->n_children; i++) { cairo_push_group(cr); - layout_render(layout->children[i], cr); + layout_render(&layout->children[i], cr); cairo_pop_group_to_source(cr); cairo_paint(cr); } @@ -99,11 +104,11 @@ void layout_render(layout_t *layout, cairo_t *cr) void layout_free(layout_t *layout) { - for (int i = 0; i < layout->n_children; i++) - layout_free(layout->children[i]); - if (layout->pl != NULL) g_object_unref(layout->pl); - free(layout); + for (int i = 0; i < layout->n_children; i++) + layout_free(&layout->children[i]); + + free(layout->children); } |
