summaryrefslogtreecommitdiff
path: root/richwin.c
diff options
context:
space:
mode:
Diffstat (limited to 'richwin.c')
-rw-r--r--richwin.c85
1 files changed, 85 insertions, 0 deletions
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 <stdlib.h> // for malloc() and free()
+#include <string.h> // for memcpy()
+#include <assert.h>
+
+#include <curses.h>
+#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;
+}