diff --git a/cli/main.c b/cli/main.c index f78dee2..cd79176 100644 --- a/cli/main.c +++ b/cli/main.c @@ -10,15 +10,34 @@ int main(int argc, char **argv) { struct ct_tree *tree; ct_init(&tree); - init_pair(1, COLOR_CYAN, COLOR_CYAN); - init_pair(2, COLOR_RED, COLOR_RED); + init_pair(1, COLOR_BLACK, COLOR_CYAN); + init_pair(2, COLOR_BLACK, COLOR_RED); - // wbkgd(tree->root->win, COLOR_PAIR(1)); - // wrefresh(tree->root->win); - - int i = 1; - while (1) { + // wbkgd(tree->root->surface->win, COLOR_PAIR(1)); + // wrefresh(tree->root->surface->win); + + struct ct_node *const child1 = new_node(bounds_none(), tree->root); + struct ct_node *const child2 = new_node(bounds_none(), tree->root); + append_child_node(tree->root, child1); + append_child_node(tree->root, child2); + + wbkgd(child1->surface->win, COLOR_PAIR(1)); + wbkgd(child2->surface->win, COLOR_PAIR(2)); + + // wrefresh(child1->surface->win); + + mvwprintw(child2->surface->win, 0, 0, "1@(%d,%d) %dx%d\n", + child1->surface->dims->x, child1->surface->dims->y, + child1->surface->dims->width, child1->surface->dims->height); + mvwprintw(child2->surface->win, 1, 0, "2@(%d,%d) %dx%d\n", + child2->surface->dims->x, child2->surface->dims->y, + child2->surface->dims->width, child2->surface->dims->height); + wrefresh(child2->surface->win); + + + while (1) { ct_update(tree); + napms(100); } destroy_tree(tree); diff --git a/cursetree/cursetree.c b/cursetree/cursetree.c index 90b1476..5c79652 100644 --- a/cursetree/cursetree.c +++ b/cursetree/cursetree.c @@ -1,6 +1,7 @@ #include #include "ncrswrap.h" +#include "node.h" #include "tree.h" #include "_ncurses.h" @@ -12,6 +13,19 @@ int ct_init(struct ct_tree **const tree) { return EXIT_SUCCESS; } +/* Recursively search the tree for update requests. + */ +static void __update_rec(struct ct_node *const node) { + if (node->flags & NFLAG_RESIZE) { + /* TODO: the child has requested a resizing, but resize_node() + * TODO: wastes CPU time resizing itself! + */ + resize_node(node, dup_dims(node->surface->dims)); + } + for (int i=0; i < node->cindex; i++) { + __update_rec(node->child[i]); + } +} void ct_update(struct ct_tree *const tree) { const int key = wgetch(curscr); @@ -32,9 +46,11 @@ void ct_update(struct ct_tree *const tree) { doupdate(); break; default: - wclear(tree->root->surface->win); + // wclear(tree->root->surface->win); mvwprintw(tree->root->surface->win, 0, 0, " \r%d\n", key); wrefresh(tree->root->surface->win); break; } + + __update_rec(tree->root); } diff --git a/cursetree/dims.c b/cursetree/dims.c index e4073a2..f0bb0ef 100644 --- a/cursetree/dims.c +++ b/cursetree/dims.c @@ -4,6 +4,7 @@ #include #include "dims.h" +#include "node.h" #include "util.h" struct ct_dims *new_dims(const int x, const int y, const int width, const int height) { diff --git a/cursetree/dims.h b/cursetree/dims.h index 25e6ccc..86417ba 100644 --- a/cursetree/dims.h +++ b/cursetree/dims.h @@ -3,7 +3,7 @@ #define __BOUND_UNLIMITED (-1) #define __BOUND_ABS_MIN (1) -#define __BOUND_ABS_MAX (INT_MAX) +#define __BOUND_ABS_MAX (INT_MAX / CINDEX_MAX - 1) #define __BOUND_REL_MIN ((float)0) #define __BOUND_REL_MAX ((float)1) diff --git a/cursetree/ncrswrap.h b/cursetree/ncrswrap.h index c6845e0..f21ffbf 100644 --- a/cursetree/ncrswrap.h +++ b/cursetree/ncrswrap.h @@ -29,15 +29,6 @@ WINDOW *new_window(const int x, const int y, const int width, const int height); WINDOW *new_window_fs(void); void destroy_window(WINDOW *); -// #define winpos(win, x, y) (x = winposx(win), y = winposy(win)) -// #define winsize(win, width, height) \ -// (width = winwidth(win), height = winheight(win)) -// int winposx(WINDOW *const); -// int winposy(WINDOW *const); -// int winwidth(WINDOW *const); -// int winheight(WINDOW *const); -// struct ct_dims *windims(WINDOW *win); - int resizemv_window(WINDOW *const win, const int x, const int y, const int width, const int height); diff --git a/cursetree/node.c b/cursetree/node.c index 4c5a300..3f1abaf 100644 --- a/cursetree/node.c +++ b/cursetree/node.c @@ -46,7 +46,7 @@ struct ct_node *new_node(struct ct_bounds *const bounds, * WARNING: new_node doesn't set the NFLAG_RESIZE request flag * WARNING: that should be done by a function calling new_node */ - return __node(parent->surface->dims, bounds, parent); + return __node(dup_dims(parent->surface->dims), bounds, parent); } /* WARNING: Do NOT use __destroy_node() to destroy a node's children! @@ -76,8 +76,9 @@ struct ct_spdims { /* */ -int resize_node(struct ct_node *const node, struct ct_dims *const dims) { +int resize_node(struct ct_node *const node, struct ct_dims *dims) { resize_surface(node->surface, dims); + dims = node->surface->dims; if (node->surface->bounds->wmin > dims->width || node->surface->bounds->hmin > dims->height) { @@ -101,36 +102,43 @@ int resize_node(struct ct_node *const node, struct ct_dims *const dims) { } int axm_size, axm_free; - size_t bounds_axm_min_offset, bounds_axm_max_offset; - size_t dims_axm_pos_offset, dims_axm_size_offset; + /* DEBUG: WARNING :DEBUG */ + // size_t bounds_axm_min_offset, bounds_axm_max_offset; + // size_t dims_axm_pos_offset, dims_axm_size_offset; if (node->axis == AXIS_X) { axm_free = dims->width; axm_size = dims->width; - dims_axm_pos_offset = offsetof(struct ct_dims, x); - dims_axm_size_offset = offsetof(struct ct_dims, width); - bounds_axm_min_offset = offsetof(struct ct_bounds, wmin); - bounds_axm_max_offset = offsetof(struct ct_bounds, wmax); + /* DEBUG: WARNING :DEBUG */ + // dims_axm_pos_offset = offsetof(struct ct_dims, x); + // dims_axm_size_offset = offsetof(struct ct_dims, width); + // bounds_axm_min_offset = offsetof(struct ct_bounds, wmin); + // bounds_axm_max_offset = offsetof(struct ct_bounds, wmax); } else { assert(node->axis == AXIS_Y); axm_free = dims->height; axm_size = dims->height; - - dims_axm_pos_offset = offsetof(struct ct_dims, y); - dims_axm_size_offset = offsetof(struct ct_dims, height); - bounds_axm_min_offset = offsetof(struct ct_bounds, hmin); - bounds_axm_max_offset = offsetof(struct ct_bounds, hmax); + + /* DEBUG: WARNING :DEBUG */ + // dims_axm_pos_offset = offsetof(struct ct_dims, y); + // dims_axm_size_offset = offsetof(struct ct_dims, height); + // bounds_axm_min_offset = offsetof(struct ct_bounds, hmin); + // bounds_axm_max_offset = offsetof(struct ct_bounds, hmax); } struct ct_spdims cspdims[node->cindex]; cindex parts_n = node->cindex; memset(cspdims, 0, sizeof(struct ct_spdims) * node->cindex); for (int i = 0; i < node->cindex; i++) { - cspdims[i].min = - *(int *)(node->child[i]->surface->bounds + bounds_axm_min_offset); - cspdims[i].max = - *(int *)(node->child[i]->surface->bounds + bounds_axm_max_offset); + /* DEBUG: WARNING :DEBUG */ + // cspdims[i].min = + // *(int *)(node->child[i]->surface->bounds + bounds_axm_min_offset); + // cspdims[i].max = + // *(int *)(node->child[i]->surface->bounds + bounds_axm_max_offset); + cspdims[i].min = node->child[i]->surface->bounds->wmin; + cspdims[i].max = node->child[i]->surface->bounds->wmax; + if (node->child[i]->surface->bounds->type == BOUND_RELATIVE) { cspdims[i].min *= axm_size; cspdims[i].max *= axm_size; @@ -165,9 +173,13 @@ int resize_node(struct ct_node *const node, struct ct_dims *const dims) { // *(int*)(cdims + dims_axo_pos_offset) = *(int*)(dims + dims_axo_pos_offset); // *(int*)(cdims + dims_axo_size_offset) = axo_size; for (int i = 0; i < node->cindex; i++) { - *(int *)(cdims + dims_axm_size_offset) = cspdims[i].axm_size; + /* DEBUG: WARNING :DEBUG */ + // *(int *)(cdims + dims_axm_size_offset) = cspdims[i].axm_size; + cdims->width = cspdims[i].axm_size; resize_node(node->child[i], dup_dims(cdims)); - *(int *)(cdims + dims_axm_pos_offset) += cspdims[i].axm_size; + /* DEBUG: WARNING :DEBUG */ + // *(int *)(cdims + dims_axm_pos_offset) += cspdims[i].axm_size; + cdims->x += cspdims[i].axm_size; } free(cdims); @@ -200,13 +212,13 @@ static int __set_cbounds(struct ct_node *const parent, parent->cbounds.wmin_rel = p_wmin_rel; parent->cbounds.hmin_rel = p_hmin_rel; - assert(parent->cbounds.wmin_rel > __BOUND_REL_MIN); - assert(parent->cbounds.hmin_rel > __BOUND_REL_MIN); + assert(parent->cbounds.wmin_rel >= __BOUND_REL_MIN); + assert(parent->cbounds.hmin_rel >= __BOUND_REL_MIN); } else { parent->cbounds.wmin_abs += c_wmin; parent->cbounds.hmin_abs += c_hmin; - assert(parent->cbounds.wmin_abs > __BOUND_ABS_MIN); - assert(parent->cbounds.hmin_abs > __BOUND_ABS_MIN); + assert(parent->cbounds.wmin_abs >= __BOUND_ABS_MIN); + assert(parent->cbounds.hmin_abs >= __BOUND_ABS_MIN); } return 0; } @@ -317,8 +329,7 @@ static int __index_as_child(const struct ct_node *const node, return 2; } -/* - * If preserve_bounds is set then upon collapse the new node maintains the +/* If preserve_bounds is set then upon collapse the new node maintains the * original node's bounds struct. Otherwise the bounds of the child collapsed * onto are used. */ diff --git a/cursetree/node.h b/cursetree/node.h index f0341f6..b62438b 100644 --- a/cursetree/node.h +++ b/cursetree/node.h @@ -11,7 +11,7 @@ typedef struct _win_st WINDOW; #define NODE_INIT_CHILDREN 4 #define NODE_CHILDREN_GROWTH 1.5 -#define CINDEX_MAX UCHAR_MAX +#define CINDEX_MAX (UCHAR_MAX) #define NFLAG_EMPTY (0) #define NFLAG_RESIZE (1 << 0) @@ -47,9 +47,17 @@ struct ct_node *__node(struct ct_dims *const dims, struct ct_node *const parent); struct ct_node *new_node(struct ct_bounds *const bounds, struct ct_node *const parent); - void __destroy_node(struct ct_node *const node); -int resize_node(struct ct_node *const node, struct ct_dims *const new_dims); + +int resize_node(struct ct_node *const node, struct ct_dims *dims); + +int insert_child_node(struct ct_node *const parent, struct ct_node *const child, + const cindex i); +int append_child_node(struct ct_node *const parent, + struct ct_node *const child); +struct ct_node *remove_child_node(struct ct_node *const parent, + const cindex i); +int destroy_child_node(struct ct_node *const parent, const cindex i); void collapse_node(struct ct_node **const node, const int i, const bool preserve_bounds);