summaryrefslogtreecommitdiffstats
path: root/src/tk/box.c
blob: fd6c0714d3322fe46e93816da526ffde106a3448 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (C) 2021  P. J. McDermott
 *
 * This file is part of Maze Fight
 *
 * Maze Fight is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Maze Fight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Maze Fight.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <SDL.h>
#include "errno.h"
#include <stdlib.h>
#include <string.h>
#include "../tk.h"
#include "style.h"
#include "widget.h"

struct mftk_box {
	struct mftk_widget  parent;
	int                 container_w;
	int                 container_h;
	int                 padding;
	int                 border;
	struct mftk_widget *child;
	int                 child_x;
	int                 child_y;
};

static void
_mftk_box_layout(struct mftk_widget *w)
{
	struct mftk_box *b = (struct mftk_box *) w;

	mftk_widget_layout(b->child);
	if (w->w == 0) {
		w->w = b->child->w + b->padding * 2 + b->border * 2;
	}
	if (w->h == 0) {
		w->h = b->child->h + b->padding * 2 + b->border * 2;
	}
	b->child_x = (b->container_w - w->w) / 2 + b->padding + b->border;
	b->child_y = (b->container_h - w->h) / 2 + b->padding + b->border;
}

static void
_mftk_box_index(struct mftk_widget *w, struct mftk_window *win)
{
	struct mftk_box *b = (struct mftk_box *) w;

	mftk_widget_index(b->child, win);
}

static int
_mftk_box_mouse_event(struct mftk_widget *w, SDL_Event *e, int x, int y)
{
	struct mftk_box *b = (struct mftk_box *) w;
	SDL_Point        p;
	SDL_Rect         rect;

	switch (e->type) {
		case SDL_MOUSEBUTTONUP:
			p.x = e->button.x;
			p.y = e->button.y;
			rect.x = x + b->child_x;
			rect.y = y + b->child_y;
			rect.w = b->child->w;
			rect.h = b->child->h;
			if (SDL_PointInRect(&p, &rect) == SDL_TRUE) {
				return mftk_widget_mouse_event(b->child, e,
						rect.x, rect.y);
			}
			break;
		default:
			break;
	}

	return 0;
}

static int
_mftk_box_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y)
{
	struct mftk_box *b = (struct mftk_box *) w;
	SDL_Rect         outer;
	SDL_Rect         inner;
	SDL_Rect         edge;

	outer.x = x + b->child_x - b->padding - b->border;
	outer.y = y + b->child_y - b->padding - b->border;
	outer.w = w->w;
	outer.h = w->h;
	inner.x = x + b->child_x - b->padding;
	inner.y = y + b->child_y - b->padding;
	inner.w = w->w - b->border * 2;
	inner.h = w->h - b->border * 2;

	/* Border */
	if (SDL_SetRenderDrawColor(renderer,
				MFTK_COLOR_FBDR_R, MFTK_COLOR_FBDR_G,
				MFTK_COLOR_FBDR_B, MFTK_COLOR_FBDR_A) < 0) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't render widget: %s",
				SDL_GetError());
		return -1;
	}
#define _mftk_edge(X, Y, W, H) \
	edge.x = X, edge.y = Y, edge.w = W, edge.h = H; \
	if (SDL_RenderFillRect(renderer, &edge) < 0) { \
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
				"Couldn't render widget: %s", \
				SDL_GetError()); \
		return -1; \
	}
	_mftk_edge(outer.x          , outer.y          , outer.w  , b->border);
	_mftk_edge(outer.x          , inner.y + inner.h, outer.w  , b->border);
	_mftk_edge(outer.x          , outer.y          , b->border, outer.h  );
	_mftk_edge(inner.x + inner.w, outer.y          , b->border, outer.h  );
#undef _mftk_edge

	/* Background */
	if (SDL_SetRenderDrawColor(renderer,
				MFTK_COLOR_FORM_R, MFTK_COLOR_FORM_G,
				MFTK_COLOR_FORM_B, MFTK_COLOR_FORM_A) < 0 ||
			SDL_RenderFillRect(renderer, &inner) < 0) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't render widget: %s",
				SDL_GetError());
		return -1;
	}

	/* Child widget */
	if (mftk_widget_render(b->child, renderer,
				x + b->child_x, y + b->child_y) < 0) {
		return -1;
	}

	return 0;
}

static void
_mftk_box_destroy(struct mftk_widget *w)
{
	struct mftk_box *b = (struct mftk_box *) w;

	mftk_widget_destroy(&b->child);
}

struct mftk_widget *
mftk_box_new(int container_w, int container_h, int own_w, int own_h,
		int padding, int border, struct mftk_widget *child)
{
	struct mftk_widget *w;
	struct mftk_box    *b;

	mftk_widget_init_container(w, b, box);

	b->container_w = container_w;
	b->container_h = container_h;
	w->w           = own_w;
	w->h           = own_h;
	b->padding     = padding;
	b->border      = border;
	b->child       = child;

	child->container = w;

	return w;
}