hide <ncursesw/ncurses.h> behind ncrswrap.*
isolate ct_dims implementation to dims.*
This commit is contained in:
parent
1dd5dd79c8
commit
b04f0b4aa3
8 changed files with 142 additions and 113 deletions
|
|
@ -24,7 +24,7 @@ struct ct_node *init_window_node(WINDOW *const win) {
|
|||
|
||||
static struct ct_node *
|
||||
auto_window_node(const struct ct_dims *const dims) {
|
||||
WINDOW *win = newwin(dims->height, dims->width, dims->y, dims->x);
|
||||
WINDOW *win = new_window(dims->x, dims->y, dims->width, dims->height);
|
||||
return init_window_node(win);
|
||||
}
|
||||
|
||||
|
|
@ -46,7 +46,7 @@ static struct ct_node *init_abstract_node(struct ct_node *const node0,
|
|||
void destroy_node(struct ct_node *const node) {
|
||||
if (node->type == NODE_WINDOW) {
|
||||
/* Window Node */
|
||||
delwin(node->win);
|
||||
destroy_window(node->win);
|
||||
goto end;
|
||||
}
|
||||
/* Abstract Node */
|
||||
|
|
@ -59,99 +59,20 @@ end:
|
|||
free(node);
|
||||
}
|
||||
|
||||
static inline struct ct_dims *__alloc_dims(int x, int y, int width,
|
||||
int height) {
|
||||
struct ct_dims *dims;
|
||||
|
||||
dims = (struct ct_dims *)malloc(sizeof(struct ct_dims));
|
||||
*dims = (struct ct_dims){
|
||||
.x = x,
|
||||
.y = y,
|
||||
.width = width,
|
||||
.height = height,
|
||||
};
|
||||
return dims;
|
||||
}
|
||||
|
||||
static inline struct ct_dims *
|
||||
__dup_dims(const struct ct_dims *const dims) {
|
||||
struct ct_dims *dup;
|
||||
dup = (struct ct_dims *)malloc(sizeof(struct ct_dims));
|
||||
memcpy(dup, dims, sizeof(struct ct_dims));
|
||||
|
||||
return dup;
|
||||
}
|
||||
|
||||
static inline struct ct_dims *termdims(void) {
|
||||
struct ct_dims *dims = __alloc_dims(0, 0, 0, 0);
|
||||
termsize(dims->width, dims->height);
|
||||
|
||||
return dims;
|
||||
}
|
||||
|
||||
static inline void nodesize(const struct ct_node *const node, int *const width,
|
||||
int *const height) {
|
||||
if (node->type == NODE_WINDOW) {
|
||||
/* Window Node */
|
||||
getmaxyx(node->win, *height, *width);
|
||||
} else {
|
||||
/* Abstract Node */
|
||||
assert(node->type == NODE_ABSTRACT);
|
||||
*width = node->dims->width;
|
||||
*height = node->dims->height;
|
||||
}
|
||||
}
|
||||
|
||||
static inline struct ct_dims *nodedims(const struct ct_node *const node) {
|
||||
struct ct_dims *dims;
|
||||
if (node->type == NODE_WINDOW) {
|
||||
/* Window Node */
|
||||
dims = (struct ct_dims *)malloc(sizeof(struct ct_dims));
|
||||
getbegyx(node->win, dims->y, dims->x);
|
||||
getmaxyx(node->win, dims->height, dims->width);
|
||||
dims = windims(node->win);
|
||||
} else {
|
||||
/* Abstract Node */
|
||||
assert(node->type == NODE_ABSTRACT);
|
||||
dims = __dup_dims(node->dims);
|
||||
dims = dup_dims(node->dims);
|
||||
}
|
||||
|
||||
return dims;
|
||||
}
|
||||
|
||||
/* Calculate the dimensions for nodes resulting from a bifurcation.
|
||||
* Returns 0 on success, and 1 on failure if any width/height are 0 characters.
|
||||
* WARNING: This function does not guarantee the x,y positions returned
|
||||
* WARNING: are valid screen coordinates.
|
||||
*/
|
||||
static int bifurcate_dims(const struct ct_dims *const parent_dims,
|
||||
const enum ct_axis axis, const float ratio,
|
||||
struct ct_dims **const dims0,
|
||||
struct ct_dims **const dims1) {
|
||||
assert(0 < ratio && ratio < 1);
|
||||
struct ct_dims *_dims0, *_dims1;
|
||||
|
||||
_dims0 = __dup_dims(parent_dims);
|
||||
_dims1 = __dup_dims(parent_dims);
|
||||
|
||||
if (axis == AXIS_X) {
|
||||
_dims0->width *= ratio;
|
||||
_dims1->width -= _dims0->width;
|
||||
_dims1->x += _dims0->width;
|
||||
} else {
|
||||
_dims0->height *= ratio;
|
||||
_dims1->height -= _dims0->height;
|
||||
_dims1->y += _dims0->height;
|
||||
}
|
||||
|
||||
if (!_dims0->width || !_dims0->height || !_dims1->width || !_dims1->height)
|
||||
return 1;
|
||||
|
||||
// propagate bifurcated dimensions
|
||||
*dims0 = _dims0;
|
||||
*dims1 = _dims1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* NOTE: resize_node calls wnoutrefresh(3x), which expects
|
||||
* NOTE: a call doupdate(3x) call afterwards to flush ncurses
|
||||
* NOTE: virtual screen to the physical screen.
|
||||
|
|
@ -185,7 +106,7 @@ void resize_node(struct ct_node *const node,
|
|||
* axis - controls which direction the subdivision occurs
|
||||
* invert_axis - invert index of the original node in the new abstract node
|
||||
*/
|
||||
void bifurcate_window_node(struct ct_node **const node,
|
||||
static void __bifurcate_window_node(struct ct_node **const node,
|
||||
const enum ct_axis axis, const int invert_axis,
|
||||
const float ratio) {
|
||||
assert((*node)->type == NODE_WINDOW);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue