aboutsummaryrefslogtreecommitdiffstats
path: root/src/gwin/frame.c
blob: f86bd11b62f17bee3ed8919f0c6a47a0f4ed9c36 (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
/*
 * 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/frame.c
 * @brief   GWIN sub-system frame code.
 *
 * @defgroup Frame Frame
 * @ingroup GWIN
 *
 * @{
 */

#include "gfx.h"

#if GFX_USE_GWIN && GWIN_NEED_FRAME

/* Some values for the default render */
#define BORDER_X		5
#define BORDER_Y		30
#define BUTTON_X		20
#define BUTTON_Y		20

/* Some useful macros for data type conversions */
#define gh2obj			((GFrameObject *)gh)

/* Forware declarations */
void gwinFrameDraw_Std(GWidgetObject *gw, void *param);
static void _callbackBtn(void *param, GEvent *pe);

static void _frameDestroy(GHandle gh) {
	/* Deregister the button callback */
	geventRegisterCallback(&gh2obj->gl, NULL, NULL);
	geventDetachSource(&gh2obj->gl, NULL);

	/* call the gcontainer standard destroy routine */
	_gcontainerDestroy(gh);
}

#if 0 && GINPUT_NEED_MOUSE
	static void _mouseDown(GWidgetObject *gw, coord_t x, coord_t y) {
	
	}

	static void _mouseUp(GWidgetObject *gw, coord_t x, coord_t y) {

	}

	static void _mouseMove(GWidgetObject *gw, coord_t x, coord_t y) {
	
	}
#endif

static const gcontainerVMT frameVMT = {
	{
		{
			"Frame",					// The classname
			sizeof(GFrameObject),		// The object size
			_frameDestroy,				// The destroy routie
			_gcontainerRedraw,			// The redraw routine
			0,							// The after-clear routine
		},
		gwinFrameDraw_Std,				// The default drawing routine
		#if GINPUT_NEED_MOUSE
			{
				0,//_mouseDown,				// Process mouse down event
				0,//_mouseUp,				// Process mouse up events
				0,//_mouseMove,				// Process mouse move events
			},
		#endif
		#if GINPUT_NEED_TOGGLE
			{
				0,						// 1 toggle role
				0,						// Assign Toggles
				0,						// Get Toggles
				0,						// Process toggle off events
				0,						// Process toggle on events
			},
		#endif
		#if GINPUT_NEED_DIAL
			{
				0,						// 1 dial roles
				0,						// Assign Dials
				0,						// Get Dials
				0,						// Process dial move events
			},
		#endif
	},
	0,									// Adjust the relative position of a child (optional)
	0,									// Adjust the size of a child (optional)
	0,									// A child has been added (optional)
	0,									// A child has been deleted (optional)
};

GHandle gwinGFrameCreate(GDisplay *g, GFrameObject *fo, GWidgetInit *pInit, uint32_t flags) {
	if (!(fo = (GFrameObject *)_gcontainerCreate(g, &fo->gc, pInit, &frameVMT)))
		return 0;

	fo->btnClose = NULL;
	fo->btnMin = NULL;
	fo->btnMax = NULL;

	/* Buttons require a border */
	if ((flags & GWIN_FRAME_CLOSE_BTN || flags & GWIN_FRAME_MINMAX_BTN) && !(flags & GWIN_FRAME_BORDER))
		flags |= GWIN_FRAME_BORDER;

	/* apply flags */
	fo->gc.g.flags |= flags;

	/* create and initialize the listener if any button is present. */
	if ((flags & GWIN_FRAME_CLOSE_BTN) || (flags & GWIN_FRAME_MINMAX_BTN)) {
		geventListenerInit(&fo->gl);
		gwinAttachListener(&fo->gl);
		geventRegisterCallback(&fo->gl, _callbackBtn, (GHandle)fo);
	}

	/* create close button if necessary */
	if (flags & GWIN_FRAME_CLOSE_BTN) {
		GWidgetInit wi;

		gwinWidgetClearInit(&wi);
		wi.g.show = TRUE;
		wi.g.parent = &fo->gc.g;

		wi.g.x = fo->gc.g.width - BORDER_X - BUTTON_X;
		wi.g.y = (BORDER_Y - BUTTON_Y) / 2;
		wi.g.width = BUTTON_X;
		wi.g.height = BUTTON_Y;
		wi.text = "X";
		fo->btnClose = gwinButtonCreate(NULL, &wi);		
	}

	/* create minimize and maximize buttons if necessary */
	if (flags & GWIN_FRAME_MINMAX_BTN) {
		GWidgetInit wi;

		gwinWidgetClearInit(&wi);
		wi.g.show = TRUE;
		wi.g.parent = &fo->gc.g;

		wi.g.x = (flags & GWIN_FRAME_CLOSE_BTN) ? fo->gc.g.width - 2*BORDER_X - 2*BUTTON_X : fo->gc.g.width - BORDER_X - BUTTON_X;
		wi.g.y = (BORDER_Y - BUTTON_Y) / 2;
		wi.g.width = BUTTON_X;
		wi.g.height = BUTTON_Y;
		wi.text = "O";
		fo->btnMin = gwinButtonCreate(NULL, &wi);		

		wi.g.x = (flags & GWIN_FRAME_CLOSE_BTN) ? fo->gc.g.width - 3*BORDER_X - 3*BUTTON_X : fo->gc.g.width - BORDER_X - BUTTON_X;
		wi.g.y = (BORDER_Y - BUTTON_Y) / 2;
		wi.g.width = BUTTON_X;
		wi.g.height = BUTTON_Y;
		wi.text = "_";
		fo->btnMax = gwinButtonCreate(NULL, &wi);
	}

	gwinSetVisible(&fo->gc.g, pInit->g.show);

	return &fo->gc.g;
}

/* Process a button event */
static void _callbackBtn(void *param, GEvent *pe) {
	switch (pe->type) {
		case GEVENT_GWIN_BUTTON:
			if (((GEventGWinButton *)pe)->button == ((GFrameObject*)(GHandle)param)->btnClose)
				gwinDestroy((GHandle)param);

			else if (((GEventGWinButton *)pe)->button == ((GFrameObject*)(GHandle)param)->btnMin) {
				;/* ToDo */

			} else if (((GEventGWinButton *)pe)->button == ((GFrameObject*)(GHandle)param)->btnMax) {
				;/* ToDo */
			}

			break;

		default:
			break;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Default render routines                                                                       //
///////////////////////////////////////////////////////////////////////////////////////////////////

static const GColorSet* _getDrawColors(GWidgetObject *gw) {
	if (!(gw->g.flags & GWIN_FLG_SYSENABLED))
		return &gw->pstyle->disabled;
	//if ((gw->g.flags & GBUTTON_FLG_PRESSED))
	//	return &gw->pstyle->pressed;

	return &gw->pstyle->enabled;
}

void gwinFrameDraw_Std(GWidgetObject *gw, void *param) {
	const GColorSet		*pcol;
	color_t			border;
	color_t			background;
	(void)param;

	if (gw->g.vmt != (gwinVMT *)&frameVMT)
		return;

	pcol = _getDrawColors(gw);

	// do some magic to make the background lighter than the widgets. Fix this somewhen.
	border = HTML2COLOR(0x2698DE);
	background = HTML2COLOR(0xEEEEEE);

	// Render the actual frame (with border, if any)
	if (gw->g.flags & GWIN_FRAME_BORDER) {
		gdispGFillArea(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, border);
		gdispGFillArea(gw->g.display, gw->g.x + BORDER_X, gw->g.y + BORDER_Y, gw->g.width - 2*BORDER_X, gw->g.width - BORDER_Y - BORDER_X, background);
	} else {
		// This ensure that the actual frame content (it's children) render at the same spot, no mather whether the frame has a border or not
		gdispGFillArea(gw->g.display, gw->g.x + BORDER_X, gw->g.y + BORDER_Y, gw->g.width, gw->g.height, background);
	}

	// Render frame title - if any
	if (gw->text != NULL) {
		coord_t text_y;

		text_y = ((BORDER_Y - gdispGetFontMetric(gw->g.font, fontHeight))/2);

		gdispGDrawString(gw->g.display, gw->g.x + BORDER_X, gw->g.y + text_y, gw->text, gw->g.font, pcol->text);
	}
}

#endif  /* (GFX_USE_GWIN && GWIN_NEED_FRAME) || defined(__DOXYGEN__) */
/** @} */