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
|
/*
* 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_window {
struct mftk_widget *root;
struct mftk_widget *first;
struct mftk_widget *focus;
};
struct mftk_window *
mftk_window_new(struct mftk_widget *root)
{
struct mftk_window *w;
w = calloc(1, sizeof(*w));
if (w == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't create window: %s",
strerror(errno));
return NULL;
}
w->root = root;
mftk_widget_layout(w->root);
mftk_widget_index(w->root, w);
w->focus = w->first;
w->focus->focused = SDL_TRUE;
return w;
}
void
mftk_window_index(struct mftk_window *win, struct mftk_widget *wid)
{
if (win->first == NULL) {
win->first = wid;
wid->prev = wid;
wid->next = wid;
return;
}
/*
* Head: win->first
* Tail: win->first->prev
*/
wid->prev = win->first->prev;
wid->next = win->first;
win->first->prev->next = wid;
win->first->prev = wid;
}
int
mftk_window_event(struct mftk_window *w, SDL_Event *e)
{
switch (e->type) {
case SDL_KEYDOWN:
switch (e->key.keysym.sym) {
case SDLK_TAB:
w->focus->focused = SDL_FALSE;
if (e->key.keysym.mod & KMOD_SHIFT) {
w->focus = w->focus->prev;
} else {
w->focus = w->focus->next;
}
w->focus->focused = SDL_TRUE;
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONUP:
return mftk_widget_event(w->root, e, 0, 0);
default:
break;
}
return 0;
}
int
mftk_window_render(struct mftk_window *w, SDL_Renderer *renderer)
{
return mftk_widget_render(w->root, renderer, 0, 0);
}
void
mftk_window_destroy(struct mftk_window **w_p)
{
struct mftk_window *w;
if (w_p == NULL || *w_p == NULL) {
return;
}
w = *w_p;
mftk_widget_destroy(&w->root);
free(w);
w = NULL;
}
|