From 9976dd05a2c5342d62313da1dc3950c521aa2d22 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 05 Aug 2021 02:13:13 -0400 Subject: tk: Add internal grid constructor --- (limited to 'src/tk/grid.c') diff --git a/src/tk/grid.c b/src/tk/grid.c index ca6b180..8b6b99a 100644 --- a/src/tk/grid.c +++ b/src/tk/grid.c @@ -156,24 +156,14 @@ _mftk_grid_destroy(struct mftk_widget *w) } struct mftk_widget * -mftk_grid_new(int rows, int cols, int row_spacing, int col_spacing, ...) +mftk_grid_new_a(int rows, int cols, int row_spacing, int col_spacing, + struct mftk_widget **children) { struct mftk_widget *w; struct mftk_grid *g; - va_list ap; - int r; - int c; mftk_widget_init(w, g, grid); - g->children = calloc(rows * cols, sizeof(*g->children)); - if (g->children == NULL) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Couldn't create widget: %s", - strerror(errno)); - free(g); - return NULL; - } g->children_x = calloc(cols, sizeof(*g->children_x)); if (g->children_x == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, @@ -198,15 +188,40 @@ mftk_grid_new(int rows, int cols, int row_spacing, int col_spacing, ...) g->cols = cols; g->row_spacing = row_spacing; g->col_spacing = col_spacing; + g->children = children; + + return w; +} + +struct mftk_widget * +mftk_grid_new(int rows, int cols, int row_spacing, int col_spacing, ...) +{ + struct mftk_widget **children; + struct mftk_widget *w; + va_list ap; + int r; + int c; + + children = calloc(rows * cols, sizeof(*children)); + if (children == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't create widget: %s", + strerror(errno)); + return NULL; + } va_start(ap, col_spacing); - for (r = 0; r < g->rows; ++r) { - for (c = 0; c < g->cols; ++c) { - g->children[r * g->cols + c] = va_arg(ap, - struct mftk_widget *); + for (r = 0; r < rows; ++r) { + for (c = 0; c < cols; ++c) { + children[r*cols + c] = va_arg(ap, struct mftk_widget *); } } va_end(ap); + w = mftk_grid_new_a(rows, cols, row_spacing, col_spacing, children); + if (w == NULL) { + free(children); + } + return w; } -- cgit v0.9.1