summaryrefslogtreecommitdiff
path: root/richwin.c
blob: 07af571872ae54a6f7e38300a1b3bc98ddc3b23d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
}