-
Notifications
You must be signed in to change notification settings - Fork 2
/
w_label.c
187 lines (175 loc) · 4 KB
/
w_label.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
atg - a tiny GUI toolkit for SDL
Copyright (C) 2012 Edward Cree
See atg.h for license information
w_label.c: implements the LABEL widget
*/
#include "atg.h"
#include "atg_internals.h"
#include <SDL_ttf.h>
#define MAXFONTSIZE 24
bool ttfinit=false;
#ifdef WINDOWS
const char *monofont="./LiberationMono-Regular.ttf";
#else // !WINDOWS
#ifdef MONOFONTPATH
const char *monofont=MONOFONTPATH;
#else // !MONOFONT
const char *monofont="/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf";
#endif // MONOFONT
#endif // WINDOWS
TTF_Font *monottf[MAXFONTSIZE];
void initttf(void)
{
if(TTF_Init()) return;
for(unsigned int i=0;i<MAXFONTSIZE;i++)
monottf[i]=NULL;
ttfinit=true;
}
SDL_Surface *atg_render_label(const atg_element *e)
{
if(!ttfinit)
initttf();
if(!ttfinit) return(NULL);
if(!e) return(NULL);
atg_label *l=e->elemdata;
if(!l) return(NULL);
if(!l->text) return(NULL);
if((l->fontsize>MAXFONTSIZE)||!l->fontsize) return(NULL);
if(!monottf[l->fontsize-1])
{
if(!(monottf[l->fontsize-1]=TTF_OpenFont(monofont, l->fontsize)))
return(NULL);
}
SDL_Surface *text=TTF_RenderUTF8_Blended(monottf[l->fontsize-1], l->text, (SDL_Color){.r=l->colour.r, .g=l->colour.g, .b=l->colour.b, .unused=l->colour.a});
if(text&&(e->w||e->h))
{
SDL_Surface *rv=atg_resize_surface(text, e);
SDL_FreeSurface(text);
return(rv);
}
return(text);
}
atg_label *atg_create_label_nocopy(char *text, unsigned int fontsize, atg_colour colour)
{
atg_label *rv=malloc(sizeof(atg_label));
if(rv)
{
rv->text=text;
rv->fontsize=fontsize;
rv->colour=colour;
}
return(rv);
}
atg_label *atg_create_label(const char *text, unsigned int fontsize, atg_colour colour)
{
if(text)
{
atg_label *rv=NULL;
char *dtext=strdup(text);
if(dtext)
rv=atg_create_label_nocopy(dtext, fontsize, colour);
return(rv);
}
else
{
return(atg_create_label_nocopy(NULL, fontsize, colour));
}
}
atg_element *atg_copy_label(const atg_element *e)
{
if(!e) return(NULL);
atg_label *l=e->elemdata;
if(!l) return(NULL);
atg_element *rv=malloc(sizeof(atg_element));
if(!rv) return(NULL);
*rv=*e;
atg_label *l2=rv->elemdata=malloc(sizeof(atg_label));
if(!l2)
{
free(rv);
return(NULL);
}
*l2=*l;
l2->text=l->text?strdup(l->text):NULL;
return(rv);
}
void atg_free_label(atg_element *e)
{
if(!e) return;
atg_label *label=e->elemdata;
if(label)
free(label->text);
free(label);
}
void atg_free_label_refer(atg_element *e)
{
if(!e) return;
atg_label *label=e->elemdata;
free(label);
}
atg_element *atg_create_element_label_refer(char *text, unsigned int fontsize, atg_colour colour)
{
atg_element *rv=malloc(sizeof(atg_element));
if(!rv) return(NULL);
atg_label *l=atg_create_label_nocopy(text, fontsize, colour);
if(!l)
{
free(rv);
return(NULL);
}
rv->w=rv->h=0;
rv->type="__builtin_label";
rv->elemdata=l;
rv->clickable=false;
rv->hidden=false;
rv->cache=false;
rv->cached=NULL;
rv->userdata=NULL;
rv->render_callback=atg_render_label;
rv->match_click_callback=NULL;
rv->pack_callback=NULL;
rv->copy_callback=atg_copy_label;
rv->free_callback=atg_free_label_refer;
return(rv);
}
atg_element *atg_create_element_label_nocopy(char *text, unsigned int fontsize, atg_colour colour)
{
atg_element *rv=malloc(sizeof(atg_element));
if(!rv) return(NULL);
atg_label *l=atg_create_label_nocopy(text, fontsize, colour);
if(!l)
{
free(rv);
return(NULL);
}
rv->w=rv->h=0;
rv->type="__builtin_label";
rv->elemdata=l;
rv->clickable=false;
rv->hidden=false;
rv->cache=false;
rv->cached=NULL;
rv->userdata=NULL;
rv->render_callback=atg_render_label;
rv->match_click_callback=NULL;
rv->pack_callback=NULL;
rv->copy_callback=atg_copy_label;
rv->free_callback=atg_free_label;
return(rv);
}
atg_element *atg_create_element_label(const char *text, unsigned int fontsize, atg_colour colour)
{
if(text)
{
atg_element *rv=NULL;
char *dtext=strdup(text);
if(dtext)
rv=atg_create_element_label_nocopy(dtext, fontsize, colour);
return(rv);
}
else
{
return(atg_create_element_label_nocopy(NULL, fontsize, colour));
}
}