1 /***************************************************************************
2 guilabel.cpp -- labels management
3 -------------------
4 created : Fri Aug 13 22:22:12 CEST 1999
5 copyright : (C) 1999-2013 by Eric Espie, Bernhard Wymann
6 email : torcs@free.fr
7 version : $Id: guilabel.cpp,v 1.2.2.5 2013/08/28 13:07:33 berniw Exp $
8 ***************************************************************************/
9
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19 /** @file
20 GUI labels management.
21 @author <a href=mailto:torcs@free.fr>Eric Espie</a>
22 @version $Id: guilabel.cpp,v 1.2.2.5 2013/08/28 13:07:33 berniw Exp $
23 @ingroup gui
24 */
25
26 #include <stdlib.h>
27 #ifdef WIN32
28 #include <windows.h>
29 #endif
30 #include <tgfclient.h>
31 #include "gui.h"
32 #include "guifont.h"
33
34 void
gfuiLabelInit(void)35 gfuiLabelInit(void)
36 {
37 }
38
39 /** Create a new label (extended version).
40 @ingroup gui
41 @param scr Screen where to add the label
42 @param text Text of the label
43 @param fgColor Pointer on color static array (RGBA)
44 @param font Font id
45 @param x Position of the label on the screen
46 @param y Position of the label on the screen
47 @param align Alignment:
48 <br>GFUI_ALIGN_HR_VB horizontal right, vertical bottom
49 <br>GFUI_ALIGN_HR_VC horizontal right, vertical center
50 <br>GFUI_ALIGN_HR_VT horizontal right, vertical top
51 <br>GFUI_ALIGN_HC_VB horizontal center, vertical bottom
52 <br>GFUI_ALIGN_HC_VB horizontal center, vertical center
53 <br>GFUI_ALIGN_HC_VB horizontal center, vertical top
54 <br>GFUI_ALIGN_HL_VB horizontal left, vertical bottom
55 <br>GFUI_ALIGN_HL_VB horizontal left, vertical center
56 <br>GFUI_ALIGN_HL_VB horizontal left, vertical top
57 @param maxlen Maximum length of the button string (used when the label is changed)
58 <br>0 for the text length.
59 @return label Id
60 @see GfuiSetLabelText
61 */
62 int
GfuiLabelCreateEx(void * scr,const char * text,float * fgColor,int font,int x,int y,int align,int maxlen)63 GfuiLabelCreateEx(void *scr, const char *text, float *fgColor, int font, int x, int y, int align, int maxlen)
64 {
65 tGfuiLabel *label;
66 tGfuiObject *object;
67 int width;
68 tGfuiScreen *screen = (tGfuiScreen*)scr;
69
70 object = (tGfuiObject*)calloc(1, sizeof(tGfuiObject));
71 object->widget = GFUI_LABEL;
72 object->focusMode = GFUI_FOCUS_NONE;
73 object->visible = 1;
74 object->id = screen->curId++;
75
76 if (maxlen == 0) maxlen = strlen(text);
77 label = &(object->u.label);
78 label->text = (char*)calloc(maxlen+1, 1);
79 strncpy(label->text, text, maxlen);
80 label->text[maxlen] = '\0';
81 label->maxlen = maxlen;
82
83 label->bgColor = screen->bgColor;
84 //label->fgColor = fgColor;
85 label->fgColor.setRGBA(fgColor);
86
87 label->font = gfuiFont[font];
88 width = gfuiFont[font]->getWidth((const char *)label->text);
89 label->align = align;
90 switch(align&0xF0) {
91 case 0x00 /* LEFT */:
92 label->x = object->xmin = x;
93 label->y = y - gfuiFont[font]->getDescender();
94 object->ymin = y;
95 object->xmax = x + width;
96 object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
97 break;
98 case 0x10 /* CENTER */:
99 label->x = object->xmin = x - width / 2;
100 label->y = y - gfuiFont[font]->getDescender();
101 object->ymin = y;
102 object->xmax = x + width / 2;
103 object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
104 break;
105 case 0x20 /* RIGHT */:
106 label->x = object->xmin = x - width;
107 label->y = y - gfuiFont[font]->getDescender();
108 object->ymin = y;
109 object->xmax = x;
110 object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
111 break;
112 }
113
114 gfuiAddObject(screen, object);
115
116 return object->id;
117 }
118
119 /** Add a label to a screen.
120 @ingroup gui
121 @param scr Screen where to add the label
122 @param text Text of the label
123 @param font Font id
124 @param x Position of the label on the screen
125 @param y Position of the label on the screen
126 @param align Alignment:
127 <br>GFUI_ALIGN_HR_VB horizontal right, vertical bottom
128 <br>GFUI_ALIGN_HR_VC horizontal right, vertical center
129 <br>GFUI_ALIGN_HR_VT horizontal right, vertical top
130 <br>GFUI_ALIGN_HC_VB horizontal center, vertical bottom
131 <br>GFUI_ALIGN_HC_VB horizontal center, vertical center
132 <br>GFUI_ALIGN_HC_VB horizontal center, vertical top
133 <br>GFUI_ALIGN_HL_VB horizontal left, vertical bottom
134 <br>GFUI_ALIGN_HL_VB horizontal left, vertical center
135 <br>GFUI_ALIGN_HL_VB horizontal left, vertical top
136 @param maxlen Maximum length of the button string (used when the label is changed)
137 <br>0 for the text length.
138 @return label Id
139 @see GfuiSetLabelText
140 */
141 int
GfuiLabelCreate(void * scr,const char * text,int font,int x,int y,int align,int maxlen)142 GfuiLabelCreate(void *scr, const char *text, int font, int x, int y, int align, int maxlen)
143 {
144 return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_LABELCOLOR][0]), font, x, y, align, maxlen);
145 }
146
147 /** Add a Tip (generally associated with a button).
148 @param scr Screen where to add the label
149 @param text Text of the label
150 @param maxlen Maximum length of the button string (used when the label is changed)
151 @return label Id
152 @see GfuiSetLabelText
153 */
154 int
GfuiTipCreate(void * scr,const char * text,int maxlen)155 GfuiTipCreate(void *scr, const char *text, int maxlen)
156 {
157 return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_TIPCOLOR][0]), GFUI_FONT_SMALL, 320, 15, GFUI_ALIGN_HC_VB, maxlen);
158 }
159
160 /** Add a Title to the screen.
161 @ingroup gui
162 @param scr Screen where to add the label
163 @param text Text of the title
164 @param maxlen Maximum length of the button string (used when the label is changed)
165 <br>0 for the text length.
166 @return label Id
167 @see GfuiSetLabelText
168 */
169 int
GfuiTitleCreate(void * scr,const char * text,int maxlen)170 GfuiTitleCreate(void *scr, const char *text, int maxlen)
171 {
172 return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_TITLECOLOR][0]), GFUI_FONT_BIG, 320, 440, GFUI_ALIGN_HC_VB, maxlen);
173 }
174
175 void
gfuiSetLabelText(tGfuiObject * curObject,tGfuiLabel * label,const char * text)176 gfuiSetLabelText(tGfuiObject *curObject, tGfuiLabel *label, const char *text)
177 {
178 int pw, w;
179
180 if (!text) {
181 return;
182 }
183
184 pw = label->font->getWidth((const char *)label->text);
185 strncpy(label->text, text, label->maxlen);
186 label->text[label->maxlen] = '\0';
187 w = label->font->getWidth((const char *)label->text);
188
189 switch(label->align&0xF0) {
190 case 0x00 /* LEFT */:
191 curObject->xmax = label->x + w;
192 break;
193 case 0x10 /* CENTER */:
194 label->x = curObject->xmin = label->x + pw / 2 - w / 2;
195 curObject->xmax = curObject->xmax - pw / 2 + w / 2;
196 break;
197 case 0x20 /* RIGHT */:
198 label->x = curObject->xmin = curObject->xmax - w;
199 break;
200 }
201 }
202
203 /** Change the text of a label.
204 @ingroup gui
205 @param scr Screen where to add the label
206 @param id Id of the label
207 @param text Text of the label
208 @attention The maximum length is set at the label creation
209 @see GfuiAddLabel
210 */
211 void
GfuiLabelSetText(void * scr,int id,const char * text)212 GfuiLabelSetText(void *scr, int id, const char *text)
213 {
214 tGfuiObject *curObject;
215 tGfuiScreen *screen = (tGfuiScreen*)scr;
216
217 curObject = screen->objects;
218 if (curObject != NULL) {
219 do {
220 curObject = curObject->next;
221 if (curObject->id == id) {
222 if (curObject->widget == GFUI_LABEL) {
223 gfuiSetLabelText(curObject, &(curObject->u.label), text);
224 }
225 return;
226 }
227 } while (curObject != screen->objects);
228 }
229 }
230
231 /** Change the color of a label.
232 @ingroup gui
233 @param scr Screen where to add the label
234 @param id Id of the label
235 @param color an array of 4 floats (RGBA)
236 @see GfuiAddLabel
237 */
238 void
GfuiLabelSetColor(void * scr,int id,float * color)239 GfuiLabelSetColor(void *scr, int id, float *color)
240 {
241 tGfuiObject *curObject;
242 tGfuiScreen *screen = (tGfuiScreen*)scr;
243
244 curObject = screen->objects;
245 if (curObject != NULL) {
246 do {
247 curObject = curObject->next;
248 if (curObject->id == id) {
249 if (curObject->widget == GFUI_LABEL) {
250 curObject->u.label.fgColor.setRGBA(color);
251 }
252 return;
253 }
254 } while (curObject != screen->objects);
255 }
256 }
257
258
259 void
gfuiDrawLabel(tGfuiObject * obj)260 gfuiDrawLabel(tGfuiObject *obj)
261 {
262 tGfuiLabel *label;
263
264 label = &(obj->u.label);
265 if (label->bgColor[3] != 0.0) {
266 glColor4fv(label->bgColor);
267 glBegin(GL_QUADS);
268 glVertex2i(obj->xmin, obj->ymin);
269 glVertex2i(obj->xmin, obj->ymax);
270 glVertex2i(obj->xmax, obj->ymax);
271 glVertex2i(obj->xmax, obj->ymin);
272 glEnd();
273 }
274 glColor4fv(label->fgColor.getRGBA());
275 gfuiPrintString(label->x, label->y, label->font, label->text);
276 }
277
278 void
gfuiReleaseLabel(tGfuiObject * obj)279 gfuiReleaseLabel(tGfuiObject *obj)
280 {
281 tGfuiLabel *label;
282
283 label = &(obj->u.label);
284
285 free(label->text);
286 free(obj);
287 }
288