From b7ac144cd2d242791938b51569effb7a1378a332 Mon Sep 17 00:00:00 2001 From: noodle Date: Mon, 10 Jul 2023 15:40:08 +0300 Subject: Add files --- richwin.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 richwin.c (limited to 'richwin.c') diff --git a/richwin.c b/richwin.c new file mode 100644 index 0000000..07af571 --- /dev/null +++ b/richwin.c @@ -0,0 +1,85 @@ +#include // for malloc() and free() +#include // for memcpy() +#include + +#include +#include "fun_menu.h" +#include "richwin.h" + +/* richwin_new */ +struct RichWin * +richwin_new(int lines, int cols, int begy, int begx, + struct WinBorders *winborders) +{ + assert(lines >= 0); + assert(cols >= 0); + assert(begy >= 0); + assert(begx >= 0); + // create a temporary struct for initializing const members of 'richwin' + struct RichWin richwin_init = { + .lines = lines, .cols = cols, + .begy = begy, .begx = begx, + }; + + // ask curses for a window + richwin_init.win = newwin(lines, cols, begy, begx); + if (richwin_init.win == NULL) { + richwin_error_code = ERROR_WINDOW_CREATION; + return NULL; + } + // allocate window borders struct if user asked for it + if (winborders != NULL) { + richwin_init.winborders = malloc(sizeof *richwin_init.winborders); + if (richwin_init.winborders == NULL) { + richwin_error_code = ERROR_MEMORY_ALLOCATION; + return NULL; + } + *richwin_init.winborders = *winborders; + } + + // allocate rich window struct + struct RichWin *richwin = malloc(sizeof *richwin); + if (richwin == NULL) { + richwin_error_code = ERROR_MEMORY_ALLOCATION; + return NULL; + } + // initialize 'richwin' from 'richwin_init' + memcpy(richwin, &richwin_init, sizeof *richwin); + return richwin; +} + +/* richwin_new_centered */ +struct RichWin * +richwin_new_centered(int lines, int cols, struct WinBorders *winborders) +{ + assert(lines <= LINES); + assert(cols <= COLS); + // no need for asserting because this is a convenience wrapper + return richwin_new(lines, cols, (LINES-lines)/2, (COLS-cols)/2, + winborders); +} + +/* richwin_border */ +int +richwin_border(struct RichWin *richwin) +{ + assert(richwin != NULL); + assert(richwin->winborders != NULL); + struct WinBorders *winborders = richwin->winborders; + return wborder(richwin->win, + winborders->ls, winborders->rs, winborders->ts, winborders->bs, + winborders->tl, winborders->tr, winborders->bl, winborders->br); +} + +/* richwin_del */ +int +richwin_del(struct RichWin *richwin) +{ + assert(richwin != NULL); + if (delwin(richwin->win) == ERR) { + return ERR; + } + free(richwin->winborders); + free(richwin); + return OK; +} -- cgit v1.2.3