summaryrefslogtreecommitdiffstats
path: root/src/tk/radio.c
blob: 79271ee3eac76bd5e8538986c0b2b49cba17d812 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * 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 "widget.h"

struct _mftk_radio_state {
	struct mftk_radio *r;
	int                state;
};

struct mftk_radio {
	struct mftk_widget         parent;
	struct mftk_widget       **children;
	struct mftk_widget        *grid;
	struct _mftk_radio_state  *states;
	int                        state;
	int                        options;
	int                      (*action)(void *, int);
	int                      (*submit)(void *);
	void                      *user_data;
};

static int
_mftk_radio_state_change(void *user_data, int state)
{
	struct _mftk_radio_state *rs = (struct _mftk_radio_state *) user_data;

	if (state == SDL_FALSE) {
		/* Radio buttons can't be deselected */
		mftk_check_set_state(rs->r->children[rs->r->state], SDL_TRUE);
		return 0;
	} else {
		/* Deselect previous check button */
		mftk_check_set_state(rs->r->children[rs->r->state], SDL_FALSE);
		/* Set and announce new state */
		rs->r->state = rs->state;
		if (rs->r->action == NULL) {
			return 0;
		}
		return rs->r->action(rs->r->user_data, rs->r->state);
	}
}

static void
_mftk_radio_layout(struct mftk_widget *w)
{
	struct mftk_radio *r = (struct mftk_radio *) w;

	mftk_widget_layout(r->grid);
	w->w = r->grid->w;
	w->h = r->grid->h;
}

static void
_mftk_radio_focus(struct mftk_widget *w __attribute__((__unused__)))
{
	/* Nothing to do */
}

static void
_mftk_radio_defocus(struct mftk_widget *w __attribute__((__unused__)))
{
	/* Nothing to do */
}

static int
_mftk_radio_key_event(struct mftk_widget *w, SDL_Event *e)
{
	struct mftk_radio *r = (struct mftk_radio *) w;

	switch (e->type) {
		case SDL_KEYDOWN:
			switch (e->key.keysym.sym) {
				case SDLK_UP:
					mftk_check_set_state(r->children[
							r->state], SDL_FALSE);
					--r->state;
					if (r->state < 0) {
						r->state = r->options - 1;
					}
					mftk_check_set_state(r->children[
							r->state], SDL_TRUE);
					if (r->action == NULL) {
						return 0;
					}
					return r->action(r->user_data,r->state);
				case SDLK_DOWN:
					mftk_check_set_state(r->children[
							r->state], SDL_FALSE);
					++r->state;
					if (r->state >= r->options) {
						r->state = 0;
					}
					mftk_check_set_state(r->children[
							r->state], SDL_TRUE);
					if (r->action == NULL) {
						return 0;
					}
					return r->action(r->user_data,r->state);
				case SDLK_RETURN:
					return r->submit(r->user_data);
				default:
					break;
			}
		default:
			break;
	}

	return 0;
}

static int
_mftk_radio_mouse_event(struct mftk_widget *w, SDL_Event *e, int x, int y)
{
	struct mftk_radio *r = (struct mftk_radio *) w;

	switch (e->type) {
		case SDL_MOUSEBUTTONUP:
			if (e->button.button == SDL_BUTTON_LEFT) {
				mftk_window_focus(w->window, w);
				return mftk_widget_mouse_event(r->grid, e,
						x, y);
			}
			break;
		default:
			break;
	}

	return 0;
}

static int
_mftk_radio_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y)
{
	struct mftk_radio *r = (struct mftk_radio *) w;

	return mftk_widget_render(r->grid, renderer, x, y);
}

static void
_mftk_radio_destroy(struct mftk_widget *w)
{
	struct mftk_radio *r = (struct mftk_radio *) w;

	mftk_widget_destroy(&r->grid);
	free(r->states);
}

struct mftk_widget *
mftk_radio_new(int butn_width, int butn_padding, SDL_Color *butn_color,
		SDL_Color *mark_color, int label_padding, int item_padding,
		TTF_Font *font, SDL_Color *text_color,
		int (*action)(void *, int), int (*submit)(void *),
		void *user_data, SDL_Renderer *renderer, int state, int options,
		...)
{
	struct mftk_widget *w;
	struct mftk_radio  *r;
	va_list             ap;
	int                 i;

	mftk_widget_init_focusable(w, r, radio);

	r->children = calloc(options, sizeof(*r->children));
	if (r->children == NULL) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't create widget: %s",
				strerror(errno));
		free(r);
		return NULL;
	}
	r->states = calloc(options, sizeof(*r->states));
	if (r->states == NULL) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't create widget: %s",
				strerror(errno));
		free(r->children);
		free(r);
		return NULL;
	}

	va_start(ap, options);
	for (i = 0; i < options; ++i) {
		r->children[i] = mftk_check_new(butn_width, butn_padding,
				butn_color, mark_color, (i == state),
				label_padding, font, va_arg(ap, const char *),
				text_color, &_mftk_radio_state_change, submit,
				&r->states[i], renderer);
		if (r->children[i] == NULL) {
			for (; i >= 0; --i) {
				mftk_widget_destroy(&r->children[i]);
			}
			free(r->children);
			free(r->states);
			free(r);
			va_end(ap);
			return NULL;
		}
		mftk_check_set_shape(r->children[i], MFTK_CHECK_SHAPE_CIR);
		mftk_check_set_steals_focus(r->children[i], SDL_FALSE);
		r->states[i].r     = r;
		r->states[i].state = i;
	}
	va_end(ap);

	r->grid = mftk_grid_new_a(options, 1, item_padding, 0, r->children,
			NULL);
	if (r->grid == NULL) {
		for (i = 0; i < options; ++i) {
			mftk_widget_destroy(&r->children[i]);
		}
		free(r->children);
		free(r->states);
		free(r);
	}

	r->state     = state;
	r->options   = options;
	r->action    = action;
	r->submit    = submit;
	r->user_data = user_data;

	return w;
}