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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-08-20 07:42:53 +00:00
|
|
|
* @file src/gwin/gwin_label.c
|
2014-05-20 16:05:38 +00:00
|
|
|
* @brief GWIN label widget header file
|
2013-07-01 17:53:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gfx.h"
|
|
|
|
|
|
|
|
#if GFX_USE_GWIN && GWIN_NEED_LABEL
|
|
|
|
|
2014-08-20 07:42:53 +00:00
|
|
|
#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-07-02 17:26:48 +00:00
|
|
|
|
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) {
|
2013-07-03 14:20:32 +00:00
|
|
|
(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-07-03 14:20:32 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2013-07-03 14:20:32 +00:00
|
|
|
(void) maxwidth;
|
|
|
|
|
|
|
|
return gdispGetFontMetric(font, fontHeight);
|
|
|
|
}
|
|
|
|
|
2013-07-02 17:26:48 +00:00
|
|
|
static const gwidgetVMT labelVMT = {
|
|
|
|
{
|
2015-11-06 23:51:23 +00:00
|
|
|
"Label", // The class name
|
|
|
|
sizeof(GLabelObject), // The object size
|
|
|
|
_gwidgetDestroy, // The destroy routine
|
|
|
|
_gwidgetRedraw, // The redraw routine
|
|
|
|
0, // The after-clear routine
|
2013-07-02 17:26:48 +00:00
|
|
|
},
|
2015-11-06 23:51:23 +00:00
|
|
|
gwinLabelDrawJustifiedLeft, // default drawing routine
|
2013-07-05 15:45:24 +00:00
|
|
|
#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
|
2015-08-16 14:18:54 +00:00
|
|
|
#if GINPUT_NEED_KEYBOARD || GWIN_NEED_KEYBOARD
|
2015-08-12 15:32:38 +00:00
|
|
|
{
|
|
|
|
0 // Process keyboard key down events
|
|
|
|
},
|
|
|
|
#endif
|
2013-07-05 15:45:24 +00:00
|
|
|
#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
|
|
|
};
|
|
|
|
|
2015-11-06 23:39:08 +00:00
|
|
|
GHandle gwinGLabelCreate(GDisplay *g, GLabelObject *widget, GWidgetInit *pInit) {
|
2013-07-02 17:26:48 +00:00
|
|
|
uint16_t flags = 0;
|
|
|
|
|
|
|
|
// auto assign width
|
|
|
|
if (pInit->g.width <= 0) {
|
2013-07-17 15:49:21 +00:00
|
|
|
|
2013-07-02 17:26:48 +00:00
|
|
|
flags |= GLABEL_FLG_WAUTO;
|
2013-10-24 08:36:11 +00:00
|
|
|
pInit->g.width = getwidth(pInit->text, gwinGetDefaultFont(), gdispGGetWidth(g) - pInit->g.x);
|
2013-07-02 17:26:48 +00:00
|
|
|
}
|
2015-11-06 23:39:08 +00:00
|
|
|
|
2013-07-02 17:26:48 +00:00
|
|
|
// auto assign height
|
|
|
|
if (pInit->g.height <= 0) {
|
|
|
|
flags |= GLABEL_FLG_HAUTO;
|
2013-10-24 08:36:11 +00:00
|
|
|
pInit->g.height = getheight(pInit->text, gwinGetDefaultFont(), gdispGGetWidth(g) - pInit->g.x);
|
2013-07-02 17:26:48 +00:00
|
|
|
}
|
|
|
|
|
2013-10-24 08:36:11 +00:00
|
|
|
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;
|
2013-07-03 14:20:32 +00:00
|
|
|
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
|
2014-04-28 23:00:06 +00:00
|
|
|
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
|
|
|
|
|
2015-11-06 23:51:23 +00:00
|
|
|
static void gwinLabelDraw(GWidgetObject *gw, justify_t justify) {
|
2014-05-01 06:45:26 +00:00
|
|
|
coord_t w, h;
|
2014-05-09 11:36:14 +00:00
|
|
|
color_t c;
|
2014-05-01 06:45:26 +00:00
|
|
|
|
|
|
|
// 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;
|
2014-05-09 15:11:30 +00:00
|
|
|
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) {
|
2015-04-29 12:43:47 +00:00
|
|
|
/* 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) {
|
2015-11-06 23:51:23 +00:00
|
|
|
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);
|
2015-11-06 23:39:08 +00:00
|
|
|
} else
|
2015-11-06 23:51:23 +00:00
|
|
|
gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, w, h, gw->text, gw->g.font, c, gw->pstyle->background, justify);
|
2014-05-01 06:45:26 +00:00
|
|
|
#else
|
2015-11-06 23:51:23 +00:00
|
|
|
gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, w, h, gw->text, gw->g.font, c, gw->pstyle->background, justify);
|
2014-05-01 06:45:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// render the border (if any)
|
|
|
|
if (gw->g.flags & GLABEL_FLG_BORDER)
|
2014-05-09 15:11:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-06 23:51:23 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-06-08 04:14:40 +00:00
|
|
|
#undef gh2obj
|
|
|
|
#undef gw2obj
|
2013-07-01 17:53:58 +00:00
|
|
|
#endif // GFX_USE_GWIN && GFX_NEED_LABEL
|