version 2.8
gwin_label.c
Go to the documentation of this file.
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.org/license.html
6  */
7 
8 /**
9  * @file src/gwin/gwin_label.c
10  * @brief GWIN label widget header file
11  */
12 
13 #include "../../gfx.h"
14 
15 #if GFX_USE_GWIN && GWIN_NEED_LABEL
16 
17 #include "gwin_class.h"
18 
19 // macros to assist in data type conversions
20 #define gh2obj ((GLabelObject *)gh)
21 #define gw2obj ((GLabelObject *)gw)
22 
23 // simple: single line with no wrapping
24 static coord_t getwidth(const char *text, font_t font, coord_t maxwidth) {
25  (void) maxwidth;
26 
27  return gdispGetStringWidth(text, font)+2; // Allow one pixel of padding on each side
28 }
29 
30 // simple: single line with no wrapping
31 static coord_t getheight(const char *text, font_t font, coord_t maxwidth) {
32  (void) text;
33  (void) maxwidth;
34 
35  return gdispGetFontMetric(font, fontHeight);
36 }
37 
38 static const gwidgetVMT labelVMT = {
39  {
40  "Label", // The class name
41  sizeof(GLabelObject), // The object size
42  _gwidgetDestroy, // The destroy routine
43  _gwidgetRedraw, // The redraw routine
44  0, // The after-clear routine
45  },
46  gwinLabelDrawJustifiedLeft, // default drawing routine
48  {
49  0, // Process mose down events (NOT USED)
50  0, // Process mouse up events (NOT USED)
51  0, // Process mouse move events (NOT USED)
52  },
53  #endif
54  #if GINPUT_NEED_KEYBOARD || GWIN_NEED_KEYBOARD
55  {
56  0 // Process keyboard key down events
57  },
58  #endif
59  #if GINPUT_NEED_TOGGLE
60  {
61  0, // No toggle role
62  0, // Assign Toggles (NOT USED)
63  0, // Get Toggles (NOT USED)
64  0, // Process toggle off event (NOT USED)
65  0, // Process toggle on event (NOT USED)
66  },
67  #endif
68  #if GINPUT_NEED_DIAL
69  {
70  0, // No dial roles
71  0, // Assign Dials (NOT USED)
72  0, // Get Dials (NOT USED)
73  0, // Procees dial move events (NOT USED)
74  },
75  #endif
76 };
77 
78 GHandle gwinGLabelCreate(GDisplay *g, GLabelObject *widget, GWidgetInit *pInit) {
79  uint16_t flags = 0;
80 
81  // auto assign width
82  if (pInit->g.width <= 0) {
83 
84  flags |= GLABEL_FLG_WAUTO;
85  pInit->g.width = getwidth(pInit->text, gwinGetDefaultFont(), gdispGGetWidth(g) - pInit->g.x);
86  }
87 
88  // auto assign height
89  if (pInit->g.height <= 0) {
90  flags |= GLABEL_FLG_HAUTO;
91  pInit->g.height = getheight(pInit->text, gwinGetDefaultFont(), gdispGGetWidth(g) - pInit->g.x);
92  }
93 
94  if (!(widget = (GLabelObject *)_gwidgetCreate(g, &widget->w, pInit, &labelVMT)))
95  return 0;
96 
97  #if GWIN_LABEL_ATTRIBUTE
98  widget->tab = 0;
99  widget->attr = 0;
100  #endif
101 
102  widget->w.g.flags |= flags;
103  gwinSetVisible(&widget->w.g, pInit->g.show);
104 
105  return (GHandle)widget;
106 }
107 
108 void gwinLabelSetBorder(GHandle gh, bool_t border) {
109  // is it a valid handle?
110  if (gh->vmt != (gwinVMT *)&labelVMT)
111  return;
112 
113  if (border)
114  gh2obj->w.g.flags |= GLABEL_FLG_BORDER;
115  else
116  gh2obj->w.g.flags &=~ GLABEL_FLG_BORDER;
117 }
118 
119 #if GWIN_LABEL_ATTRIBUTE
120  void gwinLabelSetAttribute(GHandle gh, coord_t tab, const char* attr) {
121  // is it a valid handle?
122  if (gh->vmt != (gwinVMT *)&labelVMT)
123  return;
124 
125  gh2obj->tab = tab;
126  gh2obj->attr = attr;
127 
128  gwinRedraw(gh);
129  }
130 #endif // GWIN_LABEL_ATTRIBUTE
131 
132 static void gwinLabelDraw(GWidgetObject *gw, justify_t justify) {
133  coord_t w, h;
134  color_t c;
135 
136  // is it a valid handle?
137  if (gw->g.vmt != (gwinVMT *)&labelVMT)
138  return;
139 
140  w = (gw->g.flags & GLABEL_FLG_WAUTO) ? getwidth(gw->text, gw->g.font, gdispGGetWidth(gw->g.display) - gw->g.x) : gw->g.width;
141  h = (gw->g.flags & GLABEL_FLG_HAUTO) ? getheight(gw->text, gw->g.font, gdispGGetWidth(gw->g.display) - gw->g.x) : gw->g.height;
142  c = (gw->g.flags & GWIN_FLG_SYSENABLED) ? gw->pstyle->enabled.text : gw->pstyle->disabled.text;
143 
144  if (gw->g.width != w || gw->g.height != h) {
145  /* Only allow the widget to be resize if it will grow larger.
146  * Growing smaller is problematic because it requires a temporary change in visibility.
147  * This is a work-around for a crashing bug caused by calling gwinResize() in the draw function
148  * (dubious) as it may try to reclaim the drawing lock.
149  */
150  if (gw->g.width < w || gw->g.height < h) {
151  gwinResize(&gw->g, (w > gw->g.width ? w : gw->g.width), (h > gw->g.height ? h : gw->g.height));
152  return;
153  }
154  w = gw->g.width;
155  h = gw->g.height;
156  }
157 
158  #if GWIN_LABEL_ATTRIBUTE
159  if (gw2obj->attr) {
160  gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw2obj->tab, h, gw2obj->attr, gw->g.font, c, gw->pstyle->background, justify);
161  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);
162  } else
163  gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, w, h, gw->text, gw->g.font, c, gw->pstyle->background, justify);
164  #else
165  gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, w, h, gw->text, gw->g.font, c, gw->pstyle->background, justify);
166  #endif
167 
168  // render the border (if any)
169  if (gw->g.flags & GLABEL_FLG_BORDER)
170  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);
171 }
172 
173 void gwinLabelDrawJustifiedLeft(GWidgetObject *gw, void *param) {
174  (void)param;
175 
176  gwinLabelDraw(gw, justifyLeft);
177 }
178 
179 void gwinLabelDrawJustifiedRight(GWidgetObject *gw, void *param) {
180  (void)param;
181 
182  gwinLabelDraw(gw, justifyRight);
183 }
184 
185 void gwinLabelDrawJustifiedCenter(GWidgetObject *gw, void *param) {
186  (void)param;
187 
188  gwinLabelDraw(gw, justifyCenter);
189 }
190 
191 #undef gh2obj
192 #undef gw2obj
193 #endif // GFX_USE_GWIN && GFX_NEED_LABEL
void gdispGFillStringBox(GDisplay *g, coord_t x, coord_t y, coord_t cx, coord_t cy, const char *str, font_t font, color_t color, color_t bgColor, justify_t justify)
Draw a text string vertically centered within the specified box. The box background is filled with th...
const struct mf_font_s * font_t
The type of a font.
Definition: gdisp.h:93
const char * text
Definition: gwin_widget.h:99
void gwinLabelDrawJustifiedLeft(GWidgetObject *gw, void *param)
Renders a label with the text left jestified.
const struct gwinVMT * vmt
Definition: gwin.h:45
void gwinRedraw(GHandle gh)
Redraw a window.
uint32_t flags
Definition: gwin.h:53
int16_t coord_t
The type for a coordinate or length on the screen.
Definition: gdisp.h:39
void gwinLabelDrawJustifiedRight(GWidgetObject *gw, void *param)
Renders a label with the text right jestified.
The structure to initialise a widget.
Definition: gwin_widget.h:97
color_t background
Definition: gwin_widget.h:53
coord_t y
Definition: gwin.h:48
coord_t x
Definition: gwin.h:47
GHandle gwinGLabelCreate(GDisplay *g, GLabelObject *widget, GWidgetInit *pInit)
Create a label widget.
void gwinResize(GHandle gh, coord_t width, coord_t height)
Resize a window.
color_t text
Definition: gwin_widget.h:38
GWindowInit g
Definition: gwin_widget.h:98
#define GINPUT_NEED_MOUSE
Should mouse/touch functions be included.
font_t gwinGetDefaultFont(void)
Get the current default font.
coord_t gdispGetFontMetric(font_t font, fontmetric_t metric)
Get a metric of a font.
GWindowObject g
Definition: gwin_widget.h:119
coord_t gdispGetStringWidth(const char *str, font_t font)
Get the pixel width of an entire string.
void gwinLabelSetBorder(GHandle gh, bool_t border)
Border settings for the default rendering routine.
#define GLABEL_FLG_WAUTO
The internal label flags.
Definition: gwin_label.h:41
void gwinLabelSetAttribute(GHandle gh, coord_t tab, const char *attr)
Add an text attribute in front of the normal label text.
coord_t gdispGGetWidth(GDisplay *g)
Get the display width in pixels.
justify
Type for the text justification.
Definition: gdisp.h:60
GColorSet disabled
Definition: gwin_widget.h:56
GColorSet enabled
Definition: gwin_widget.h:55
const char * text
Definition: gwin_widget.h:120
void gwinSetVisible(GHandle gh, bool_t visible)
Sets whether a window is visible or not.
const GWidgetStyle * pstyle
Definition: gwin_widget.h:123
The GWIN Widget structure.
Definition: gwin_widget.h:118
bool_t show
Definition: gwin.h:80
GDisplay * display
Definition: gwin.h:46
coord_t height
Definition: gwin.h:79
coord_t x
Definition: gwin.h:76
coord_t height
Definition: gwin.h:50
The Virtual Method Table for a widget.
Definition: gwin_class.h:85
coord_t width
Definition: gwin.h:49
void gdispGDrawBox(GDisplay *g, coord_t x, coord_t y, coord_t cx, coord_t cy, color_t color)
Draw a rectangular box.
The Virtual Method Table for a GWIN window.
Definition: gwin_class.h:55
A window object structure.
Definition: gwin.h:40
void gwinLabelDrawJustifiedCenter(GWidgetObject *gw, void *param)
Renders a label with the text center jestified.
COLOR_TYPE color_t
The color type definition.
Definition: gdisp_colors.h:412
coord_t width
Definition: gwin.h:78
color_t edge
Definition: gwin_widget.h:39