aboutsummaryrefslogtreecommitdiffstats
path: root/src/gwin/gwin_label.c
blob: a067953a6c124599ebb5b2e27d2bc91bd703c9e1 (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
/*
 * 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/gwin_label.c
 * @brief	GWIN label widget header file
 */

#include "../../gfx.h"

#if GFX_USE_GWIN && GWIN_NEED_LABEL

#include "gwin_class.h"

// macros to assist in data type conversions
#define gh2obj					((GLabelObject *)gh)
#define gw2obj					((GLabelObject *)gw)

// simple: single line with no wrapping
static gCoord getwidth(const char *text, font_t font, gCoord maxwidth) {
	(void) maxwidth;

	return gdispGetStringWidth(text, font)+2;		// Allow one pixel of padding on each side
}

// simple: single line with no wrapping
static gCoord getheight(const char *text, font_t font, gCoord maxwidth) {
	(void) text;
	(void) maxwidth;

	return gdispGetFontMetric(font, fontHeight);
}

static const gwidgetVMT labelVMT = {
	{
		"Label",					// The class name
		sizeof(GLabelObject),		// The object size
		_gwidgetDestroy,			// The destroy routine
		_gwidgetRedraw, 			// The redraw routine
		0,							// The after-clear routine
	},
	gwinLabelDrawJustifiedLeft,		// default drawing routine
	#if GINPUT_NEED_MOUSE
		{
			0,						// Process mose down events (NOT USED)
			0,						// Process mouse up events (NOT USED)
			0,						// Process mouse move events (NOT USED)
		},
	#endif
	#if GINPUT_NEED_KEYBOARD || GWIN_NEED_KEYBOARD
		{
			0						// Process keyboard key down events
		},
	#endif
	#if GINPUT_NEED_TOGGLE
		{
			0,						// No toggle role
			0,						// Assign Toggles (NOT USED)
			0,						// Get Toggles (NOT USED)
			0,						// Process toggle off event (NOT USED)
			0,						// Process toggle on event (NOT USED)
		},
	#endif
	#if GINPUT_NEED_DIAL
		{
			0,						// No dial roles
			0,						// Assign Dials (NOT USED)
			0, 						// Get Dials (NOT USED)
			0,						// Procees dial move events (NOT USED)
		},
	#endif
};

GHandle gwinGLabelCreate(GDisplay *g, GLabelObject *widget, GWidgetInit *pInit) {
	uint16_t flags = 0;

	// auto assign width
	if (pInit->g.width <= 0) {

		flags |= GLABEL_FLG_WAUTO;
		pInit->g.width = getwidth(pInit->text, gwinGetDefaultFont(), gdispGGetWidth(g) - pInit->g.x);
	}
 
	// auto assign height
	if (pInit->g.height <= 0) {
		flags |= GLABEL_FLG_HAUTO;
		pInit->g.height = getheight(pInit->text, gwinGetDefaultFont(), gdispGGetWidth(g) - pInit->g.x);
	}

	if (!(widget = (GLabelObject *)_gwidgetCreate(g, &widget->w, pInit, &labelVMT)))
		return 0;

	#if GWIN_LABEL_ATTRIBUTE
		widget->tab = 0;
		widget->attr = 0;
	#endif

	widget->w.g.flags |= flags;	
	gwinSetVisible(&widget->w.g, pInit->g.show);

	return (GHandle)widget;
}

void gwinLabelSetBorder(GHandle gh, gBool border) {
	// is it a valid handle?
	if (gh->vmt != (gwinVMT *)&labelVMT)
		return;
	
	if (border)
		gh2obj->w.g.flags |= GLABEL_FLG_BORDER;
	else
		gh2obj->w.g.flags &=~ GLABEL_FLG_BORDER;
}

#if GWIN_LABEL_ATTRIBUTE
	void gwinLabelSetAttribute(GHandle gh, gCoord tab, const char* attr) {
		// is it a valid handle?
		if (gh->vmt != (gwinVMT *)&labelVMT)
			return;
	
		gh2obj->tab = tab;
		gh2obj->attr = attr;

		gwinRedraw(gh); 
	}
#endif // GWIN_LABEL_ATTRIBUTE

static void gwinLabelDraw(GWidgetObject *gw, justify_t justify) {
	gCoord				w, h;
	gColor				c;

	// is it a valid handle?
	if (gw->g.vmt != (gwinVMT *)&labelVMT)
		return;

	w = (gw->g.flags & GLABEL_FLG_WAUTO) ? getwidth(gw->text, gw->g.font, gdispGGetWidth(gw->g.display) - gw->g.x) : gw->g.width;
	h = (gw->g.flags & GLABEL_FLG_HAUTO) ? getheight(gw->text, gw->g.font, gdispGGetWidth(gw->g.display) - gw->g.x) : gw->g.height;
	c = (gw->g.flags & GWIN_FLG_SYSENABLED) ? gw->pstyle->enabled.text : gw->pstyle->disabled.text;

	if (gw->g.width != w || gw->g.height != h) {
		/* Only allow the widget to be resize if it will grow larger.
		 * Growing smaller is problematic because it requires a temporary change in visibility.
		 * This is a work-around for a crashing bug caused by calling gwinResize() in the draw function
		 * (dubious) as it may try to reclaim the drawing lock.
		 */
		if (gw->g.width < w || gw->g.height < h) {
			gwinResize(&gw->g, (w > gw->g.width ? w : gw->g.width), (h > gw->g.height ? h : gw->g.height));
			return;
		}
		w = gw->g.width;
		h = gw->g.height;
	}

	#if GWIN_LABEL_ATTRIBUTE
		if (gw2obj->attr) {
			gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw2obj->tab, h, gw2obj->attr, gw->g.font, c, gw->pstyle->background, justify);
			gdispGFillStringBox(gw->g.display, gw->g.x + gw2obj->tab, gw->g.y, w-gw2obj->tab, h, gw->text, gw->g.font, c, gw->pstyle->background, justify);
		} else
			gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, w, h, gw->text, gw->g.font, c, gw->pstyle->background, justify);
	#else
		gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, w, h, gw->text, gw->g.font, c, gw->pstyle->background, justify);
	#endif

	// render the border (if any)
	if (gw->g.flags & GLABEL_FLG_BORDER)
		gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, w, h, (gw->g.flags & GWIN_FLG_SYSENABLED) ? gw->pstyle->enabled.edge : gw->pstyle->disabled.edge);
}

void gwinLabelDrawJustifiedLeft(GWidgetObject *gw, void *param) {
	(void)param;
	
	gwinLabelDraw(gw, justifyLeft);
}

void gwinLabelDrawJustifiedRight(GWidgetObject *gw, void *param) {
	(void)param;
	
	gwinLabelDraw(gw, justifyRight);
}

void gwinLabelDrawJustifiedCenter(GWidgetObject *gw, void *param) {
	(void)param;
	
	gwinLabelDraw(gw, justifyCenter);
}

#undef gh2obj
#undef gw2obj
#endif // GFX_USE_GWIN && GFX_NEED_LABEL