µGFX  2.9
version 2.9
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.io/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 static const gwidgetVMT labelVMT = {
24  {
25  "Label", // The class name
26  sizeof(GLabelObject), // The object size
27  _gwidgetDestroy, // The destroy routine
28  _gwidgetRedraw, // The redraw routine
29  0, // The after-clear routine
30  },
31  gwinLabelDrawJustifiedLeft, // default drawing routine
32  #if GINPUT_NEED_MOUSE
33  {
34  0, // Process mose down events (NOT USED)
35  0, // Process mouse up events (NOT USED)
36  0, // Process mouse move events (NOT USED)
37  },
38  #endif
39  #if GINPUT_NEED_KEYBOARD || GWIN_NEED_KEYBOARD
40  {
41  0 // Process keyboard key down events
42  },
43  #endif
44  #if GINPUT_NEED_TOGGLE
45  {
46  0, // No toggle role
47  0, // Assign Toggles (NOT USED)
48  0, // Get Toggles (NOT USED)
49  0, // Process toggle off event (NOT USED)
50  0, // Process toggle on event (NOT USED)
51  },
52  #endif
53  #if GINPUT_NEED_DIAL
54  {
55  0, // No dial roles
56  0, // Assign Dials (NOT USED)
57  0, // Get Dials (NOT USED)
58  0, // Procees dial move events (NOT USED)
59  },
60  #endif
61 };
62 
63 GHandle gwinGLabelCreate(GDisplay *g, GLabelObject *widget, GWidgetInit *pInit) {
64  // auto assign width
65  if (pInit->g.width <= 0)
66  pInit->g.width = gdispGetStringWidth(pInit->text, gwinGetDefaultFont())+2; // Allow one pixel of padding on each side
67 
68  // auto assign height
69  if (pInit->g.height <= 0)
71 
72  if (!(widget = (GLabelObject *)_gwidgetCreate(g, &widget->w, pInit, &labelVMT)))
73  return 0;
74 
75  #if GWIN_LABEL_ATTRIBUTE
76  widget->tab = 0;
77  widget->attr = 0;
78  #endif
79 
80  gwinSetVisible(&widget->w.g, pInit->g.show);
81 
82  return (GHandle)widget;
83 }
84 
85 void gwinLabelSetBorder(GHandle gh, gBool border) {
86  // is it a valid handle?
87  if (gh->vmt != (gwinVMT *)&labelVMT)
88  return;
89 
90  if (border)
91  gh2obj->w.g.flags |= GLABEL_FLG_BORDER;
92  else
93  gh2obj->w.g.flags &=~ GLABEL_FLG_BORDER;
94 }
95 
96 #if GWIN_LABEL_ATTRIBUTE
97  void gwinLabelSetAttribute(GHandle gh, gCoord tab, const char* attr) {
98  // is it a valid handle?
99  if (gh->vmt != (gwinVMT *)&labelVMT)
100  return;
101 
102  gh2obj->tab = tab;
103  gh2obj->attr = attr;
104 
105  gwinRedraw(gh);
106  }
107 #endif // GWIN_LABEL_ATTRIBUTE
108 
109 void gwinLabelDrawJustified(GWidgetObject *gw, void *param) {
110  gColor c;
111  gJustify justify = (gJustify)param;
112 
113  // is it a valid handle?
114  if (gw->g.vmt != (gwinVMT *)&labelVMT)
115  return;
116 
117  c = (gw->g.flags & GWIN_FLG_SYSENABLED) ? gw->pstyle->enabled.text : gw->pstyle->disabled.text;
118 
120  if (gw2obj->attr) {
121  gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw2obj->tab, gw->g.height, gw2obj->attr, gw->g.font, c, gw->pstyle->background, justify);
122  gdispGFillStringBox(gw->g.display, gw->g.x + gw2obj->tab, gw->g.y, gw->g.width-gw2obj->tab, gw->g.height, gw->text, gw->g.font, c, gw->pstyle->background, justify);
123  } else
124  gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, gw->text, gw->g.font, c, gw->pstyle->background, justify);
125  #else
126  gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, gw->text, gw->g.font, c, gw->pstyle->background, justify);
127  #endif
128 
129  // render the border (if any)
130  if (gw->g.flags & GLABEL_FLG_BORDER)
131  gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, (gw->g.flags & GWIN_FLG_SYSENABLED) ? gw->pstyle->enabled.edge : gw->pstyle->disabled.edge);
132 }
133 
134 void gwinLabelDrawJustifiedLeft(GWidgetObject *gw, void *param) {
135  (void)param;
136 
138 }
139 
140 void gwinLabelDrawJustifiedRight(GWidgetObject *gw, void *param) {
141  (void)param;
142 
144 }
145 
146 void gwinLabelDrawJustifiedCenter(GWidgetObject *gw, void *param) {
147  (void)param;
148 
150 }
151 
152 #undef gh2obj
153 #undef gw2obj
154 #endif // GFX_USE_GWIN && GFX_NEED_LABEL
COLOR_TYPE gColor
The color type definition.
Definition: gdisp_colors.h:437
gCoord gdispGetFontMetric(gFont font, gFontmetric metric)
Get a metric of a font.
gJustify
Type for the text justification.
Definition: gdisp.h:60
void gdispGFillStringBox(GDisplay *g, gCoord x, gCoord y, gCoord cx, gCoord cy, const char *str, gFont font, gColor color, gColor bgColor, gJustify justify)
Draw a text string vertically centered within the specified box. The box background is filled with th...
gI16 gCoord
The type for a coordinate or length on the screen.
Definition: gdisp.h:39
gCoord gdispGetStringWidth(const char *str, gFont font)
Get the pixel width of an entire string.
void gdispGDrawBox(GDisplay *g, gCoord x, gCoord y, gCoord cx, gCoord cy, gColor color)
Draw a rectangular box.
@ gJustifyCenter
Definition: gdisp.h:62
@ gJustifyLeft
Definition: gdisp.h:61
@ gJustifyRight
Definition: gdisp.h:63
@ gFontHeight
Definition: gdisp.h:80
#define GWIN_LABEL_ATTRIBUTE
Enable the API to use attributes in the label widget.
Definition: gwin_options.h:324
GHandle gwinGLabelCreate(GDisplay *g, GLabelObject *widget, GWidgetInit *pInit)
Create a label widget.
#define GLABEL_FLG_BORDER
The internal label flags.
Definition: gwin_label.h:41
void gwinLabelSetAttribute(GHandle gh, gCoord tab, const char *attr)
Add an text attribute in front of the normal label text.
void gwinLabelSetBorder(GHandle gh, gBool border)
Border settings for the default rendering routine.
void gwinLabelDrawJustifiedLeft(GWidgetObject *gw, void *param)
Renders a label with the text left jestified.
void gwinLabelDrawJustified(GWidgetObject *gw, void *param)
Renders a label with the text justified based on the parameter.
void gwinLabelDrawJustifiedCenter(GWidgetObject *gw, void *param)
Renders a label with the text center jestified.
void gwinLabelDrawJustifiedRight(GWidgetObject *gw, void *param)
Renders a label with the text right jestified.
gFont gwinGetDefaultFont(void)
Get the current default font.
void gwinRedraw(GHandle gh)
Redraw a window.
void gwinSetVisible(GHandle gh, gBool visible)
Sets whether a window is visible or not.
gColor text
Definition: gwin_widget.h:38
gColor edge
Definition: gwin_widget.h:39
The structure to initialise a widget.
Definition: gwin_widget.h:97
GWindowInit g
Definition: gwin_widget.h:98
const char * text
Definition: gwin_widget.h:99
The GWIN Widget structure.
Definition: gwin_widget.h:118
GWindowObject g
Definition: gwin_widget.h:119
const GWidgetStyle * pstyle
Definition: gwin_widget.h:123
const char * text
Definition: gwin_widget.h:120
gColor background
Definition: gwin_widget.h:53
GColorSet disabled
Definition: gwin_widget.h:56
GColorSet enabled
Definition: gwin_widget.h:55
gBool show
Definition: gwin.h:80
gCoord width
Definition: gwin.h:78
gCoord height
Definition: gwin.h:79
A window object structure.
Definition: gwin.h:40
GDisplay * display
Definition: gwin.h:46
gCoord x
Definition: gwin.h:47
gCoord width
Definition: gwin.h:49
const struct gwinVMT * vmt
Definition: gwin.h:45
gU32 flags
Definition: gwin.h:53
gCoord y
Definition: gwin.h:48
gCoord height
Definition: gwin.h:50
The Virtual Method Table for a widget.
Definition: gwin_class.h:85
The Virtual Method Table for a GWIN window.
Definition: gwin_class.h:55