aboutsummaryrefslogtreecommitdiffstats
path: root/src/gwin/gcontainer.c
blob: 998433b4c623f8ba317e77cc410e3498cdfc8f9e (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
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */

/**
 * @file    src/gwin/gcontainer.c
 * @brief   GWIN sub-system container code
 */

#include "gfx.h"

#if GFX_USE_GWIN && GWIN_NEED_CONTAINERS

#include "src/gwin/class_gwin.h"

void _gcontainerInit(void)
{
}

void _gcontainerDeinit(void)
{
}

GHandle _gcontainerCreate(GDisplay *g, GContainerObject *pgc, const GWidgetInit *pInit, const gcontainerVMT *vmt) {
	if (!(pgc = (GContainerObject *)_gwidgetCreate(g, (GWidgetObject *)pgc, pInit, &vmt->gw)))
		return 0;

	pgc->g.flags |= GWIN_FLG_CONTAINER;

	return 	&pgc->g;
}

void _gcontainerDestroy(GHandle gh) {
	GHandle		child;

	while((child = gwinGetFirstChild(gh)))
		gwinDestroy(child);
	_gwidgetDestroy(gh);
}

void _gcontainerRedraw(GHandle gh) {
	GHandle		child;

	if (!(gh->flags & GWIN_FLG_SYSVISIBLE))
		return;

	((GWidgetObject *)gh)->fnDraw((GWidgetObject *)gh, ((GWidgetObject *)gh)->fnParam);

	for(child = gwinGetFirstChild(gh); child; child = gwinGetSibling(child))
		gwinRedraw(child);
}

void _gcontainerUpdate(GHandle gh) {
	GHandle		child;

	if (!(gh->flags & GWIN_FLG_SYSVISIBLE))
		return;

	#if GDISP_NEED_CLIP
		gdispGSetClip(gh->display, gh->x, gh->y, gh->width, gh->height);
	#endif
	((GWidgetObject *)gh)->fnDraw((GWidgetObject *)gh, ((GWidgetObject *)gh)->fnParam);

	for(child = gwinGetFirstChild(gh); child; child = gwinGetSibling(child))
		gwinRedraw(child);
}

void _gwinRecurse(GHandle gh, bool_t (*fn)(GHandle gh)) {
	if (fn(gh) && (gh->flags & GWIN_FLG_CONTAINER)) {
		// Apply to this windows children
		for(gh = gwinGetFirstChild(gh); gh; gh = gwinGetSibling(gh)) {
			// Only recurse when we have to. Otherwise apply it directly
			if ((gh->flags & GWIN_FLG_CONTAINER))
				_gwinRecurse(gh, fn);
			else
				fn(gh);
		}
	}
}

GHandle gwinGetFirstChild(GHandle gh) {
	GHandle		child;

	for(child = gwinGetNextWindow(0); child; child = gwinGetNextWindow(child))
		if (child->parent == gh)
			return child;
	return 0;
}

GHandle gwinGetSibling(GHandle gh) {
	GHandle		child;

	for(child = gwinGetNextWindow(gh), gh = gh->parent; child; child = gwinGetNextWindow(child))
		if (child->parent == gh)
			return child;
	return 0;
}

coord_t gwinGetInnerWidth(GHandle gh) {
	if (!(gh->flags & GWIN_FLG_CONTAINER))
		return 0;
	return gh->width - ((const gcontainerVMT *)gh->vmt)->LeftBorder(gh) - ((const gcontainerVMT *)gh->vmt)->RightBorder(gh);
}

coord_t gwinGetInnerHeight(GHandle gh) {
	if (!(gh->flags & GWIN_FLG_CONTAINER))
		return 0;
	return gh->height - ((const gcontainerVMT *)gh->vmt)->TopBorder(gh) - ((const gcontainerVMT *)gh->vmt)->BottomBorder(gh);
}

#endif /* GFX_USE_GWIN && GWIN_NEED_CONTAINERS */
/** @} */

/*-----------------------------------------------
 * The simplest container type - a container
 *-----------------------------------------------
 *
 * @defgroup Containers Containers
 * @ingroup GWIN
 *
 * @{
 */

#if GFX_USE_GWIN && GWIN_NEED_CONTAINER

#if GWIN_CONTAINER_BORDER != GWIN_FIRST_CONTROL_FLAG
	#error "GWIN Container: - Flag definitions don't match"
#endif

static coord_t BorderSize(GHandle gh)	{ return (gh->flags & GWIN_CONTAINER_BORDER) ? 2 : 0; }

static void DrawSimpleContainer(GWidgetObject *gw, void *param) {
	(void)	param;
	gdispGFillArea(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, gw->pstyle->background);
	if ((gw->g.flags & GWIN_CONTAINER_BORDER))
		gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, (gw->g.flags & GWIN_FLG_SYSENABLED) ? gw->pstyle->enabled.edge : gw->pstyle->disabled.edge);
}

// The container VMT table
static const gcontainerVMT containerVMT = {
	{
		{
			"Container",				// The classname
			sizeof(GContainerObject),	// The object size
			_gcontainerDestroy,			// The destroy routine
			_gcontainerRedraw,			// The redraw routine
			0,							// The after-clear routine
		},
		DrawSimpleContainer,			// The default drawing routine
		#if GINPUT_NEED_MOUSE
			{
				0, 0, 0,				// No mouse
			},
		#endif
		#if GINPUT_NEED_TOGGLE
			{
				0, 0, 0, 0, 0,			// No toggles
			},
		#endif
		#if GINPUT_NEED_DIAL
			{
				0, 0, 0, 0,				// No dials
			},
		#endif
	},
	BorderSize,							// The size of the left border (mandatory)
	BorderSize,							// The size of the top border (mandatory)
	BorderSize,							// The size of the right border (mandatory)
	BorderSize,							// The size of the bottom border (mandatory)
	0,									// A child has been added (optional)
	0,									// A child has been deleted (optional)
};

GHandle gwinGContainerCreate(GDisplay *g, GContainerObject *gc, const GWidgetInit *pInit, uint32_t flags) {
	if (!(gc = (GContainerObject *)_gcontainerCreate(g, gc, pInit, &containerVMT)))
		return 0;

	gc->g.flags |= flags;

	gwinSetVisible((GHandle)gc, pInit->g.show);
	return (GHandle)gc;
}

#endif