hide <ncursesw/ncurses.h> behind ncrswrap.*

isolate ct_dims implementation to dims.*
This commit is contained in:
Emile Clark-Boman 2025-09-13 12:44:58 +10:00
parent 1dd5dd79c8
commit b04f0b4aa3
8 changed files with 142 additions and 113 deletions

59
cursetree/dims.c Normal file
View file

@ -0,0 +1,59 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "dims.h"
struct ct_dims *new_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;
}
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;
}
/* 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.
*/
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;
}