ugfx/src/gwin/gwin_label.c

177 lines
5.2 KiB
C
Raw Normal View History

2013-07-01 17:53:58 +00:00
/*
* 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:
*
2013-07-21 20:20:37 +00:00
* http://ugfx.org/license.html
2013-07-01 17:53:58 +00:00
*/
/**
* @file src/gwin/gwin_label.c
* @brief GWIN label widget header file
2013-07-01 17:53:58 +00:00
*/
#include "gfx.h"
#if GFX_USE_GWIN && GWIN_NEED_LABEL
#include "gwin_class.h"
2013-07-01 17:53:58 +00:00
2013-10-24 02:13:07 +00:00
// macros to assist in data type conversions
#define gh2obj ((GLabelObject *)gh)
2014-04-28 21:20:51 +00:00
#define gw2obj ((GLabelObject *)gw)
2013-10-24 02:13:07 +00:00
// flags for the GLabelObject
#define GLABEL_FLG_WAUTO (GWIN_FIRST_CONTROL_FLAG << 0)
#define GLABEL_FLG_HAUTO (GWIN_FIRST_CONTROL_FLAG << 1)
#define GLABEL_FLG_BORDER (GWIN_FIRST_CONTROL_FLAG << 2)
// simple: single line with no wrapping
2013-07-07 09:40:37 +00:00
static coord_t getwidth(const char *text, font_t font, coord_t maxwidth) {
(void) maxwidth;
2013-10-24 02:13:07 +00:00
2013-07-07 09:40:37 +00:00
return gdispGetStringWidth(text, font)+2; // Allow one pixel of padding on each side
}
2013-10-24 02:13:07 +00:00
// simple: single line with no wrapping
2013-07-07 09:40:37 +00:00
static coord_t getheight(const char *text, font_t font, coord_t maxwidth) {
(void) text;
(void) maxwidth;
return gdispGetFontMetric(font, fontHeight);
}
2014-05-01 06:45:26 +00:00
static void gwinLabelDefaultDraw(GWidgetObject *gw, void *param);
2013-07-01 17:53:58 +00:00
static const gwidgetVMT labelVMT = {
{
"Label", // The class name
2013-07-07 09:40:37 +00:00
sizeof(GLabelObject), // The object size
_gwidgetDestroy, // The destroy routine
_gwidgetRedraw, // The redraw routine
0, // The after-clear routine
},
gwinLabelDefaultDraw, // 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_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
2013-07-02 06:29:38 +00:00
};
GHandle gwinGLabelCreate(GDisplay *g, GLabelObject *widget, GWidgetInit *pInit) {
uint16_t flags = 0;
// auto assign width
if (pInit->g.width <= 0) {
2013-07-17 15:49:21 +00:00
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)))
2013-07-02 06:29:38 +00:00
return 0;
2014-04-28 21:20:51 +00:00
#if GWIN_LABEL_ATTRIBUTE
widget->tab = 0;
widget->attr = 0;
#endif
2013-10-24 02:31:31 +00:00
widget->w.g.flags |= flags;
gwinSetVisible(&widget->w.g, pInit->g.show);
2013-10-24 02:13:07 +00:00
2013-07-02 06:29:38 +00:00
return (GHandle)widget;
2013-07-01 17:53:58 +00:00
}
2013-10-24 02:13:07 +00:00
void gwinLabelSetBorder(GHandle gh, bool_t 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;
}
2014-04-28 21:20:51 +00:00
#if GWIN_LABEL_ATTRIBUTE
void gwinLabelSetAttribute(GHandle gh, coord_t tab, const char* attr) {
2014-04-28 21:20:51 +00:00
// is it a valid handle?
if (gh->vmt != (gwinVMT *)&labelVMT)
return;
gh2obj->tab = tab;
gh2obj->attr = attr;
gwinRedraw(gh);
}
#endif // GWIN_LABEL_ATTRIBUTE
2014-05-01 06:45:26 +00:00
static void gwinLabelDefaultDraw(GWidgetObject *gw, void *param) {
coord_t w, h;
2014-05-09 11:36:14 +00:00
color_t c;
2014-05-01 06:45:26 +00:00
(void) param;
// 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;
2014-05-01 06:45:26 +00:00
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;
2014-05-01 06:45:26 +00:00
}
#if GWIN_LABEL_ATTRIBUTE
2014-05-09 11:36:14 +00:00
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, justifyLeft);
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, justifyLeft);
} else
gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, w, h, gw->text, gw->g.font, c, gw->pstyle->background, justifyLeft);
2014-05-01 06:45:26 +00:00
#else
2014-05-09 11:36:14 +00:00
gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, w, h, gw->text, gw->g.font, c, gw->pstyle->background, justifyLeft);
2014-05-01 06:45:26 +00:00
#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);
2014-05-01 06:45:26 +00:00
}
2013-07-01 17:53:58 +00:00
#endif // GFX_USE_GWIN && GFX_NEED_LABEL