1 | // |
---|
2 | // "$Id$" |
---|
3 | // |
---|
4 | // Label drawing code for the Fast Light Tool Kit (FLTK). |
---|
5 | // |
---|
6 | // Copyright 1998-2005 by Bill Spitzak and others. |
---|
7 | // |
---|
8 | // This library is free software; you can redistribute it and/or |
---|
9 | // modify it under the terms of the GNU Library General Public |
---|
10 | // License as published by the Free Software Foundation; either |
---|
11 | // version 2 of the License, or (at your option) any later version. |
---|
12 | // |
---|
13 | // This library is distributed in the hope that it will be useful, |
---|
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | // Library General Public License for more details. |
---|
17 | // |
---|
18 | // You should have received a copy of the GNU Library General Public |
---|
19 | // License along with this library; if not, write to the Free Software |
---|
20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
21 | // USA. |
---|
22 | // |
---|
23 | // Please report all bugs and problems on the following page: |
---|
24 | // |
---|
25 | // http://www.fltk.org/str.php |
---|
26 | // |
---|
27 | |
---|
28 | // Implementation of fl_draw(const char*,int,int,int,int,Fl_Align) |
---|
29 | // Used to draw all the labels and text, this routine: |
---|
30 | // Word wraps the labels to fit into their bounding box. |
---|
31 | // Breaks them into lines at the newlines. |
---|
32 | // Expands all unprintable characters to ^X or \nnn notation |
---|
33 | // Aligns them against the inside of the box. |
---|
34 | |
---|
35 | #define min(a,b) ((a)<(b)?(a):(b)) |
---|
36 | #include <FL/fl_draw.H> |
---|
37 | #include <FL/Fl_Image.H> |
---|
38 | |
---|
39 | #include "flstring.h" |
---|
40 | #include <ctype.h> |
---|
41 | #include <math.h> |
---|
42 | |
---|
43 | #define MAXBUF 1024 |
---|
44 | |
---|
45 | char fl_draw_shortcut; // set by fl_labeltypes.cxx |
---|
46 | |
---|
47 | static char* underline_at; |
---|
48 | |
---|
49 | // Copy p to buf, replacing unprintable characters with ^X and \nnn |
---|
50 | // Stop at a newline or if MAXBUF characters written to buffer. |
---|
51 | // Also word-wrap if width exceeds maxw. |
---|
52 | // Returns a pointer to the start of the next line of caharcters. |
---|
53 | // Sets n to the number of characters put into the buffer. |
---|
54 | // Sets width to the width of the string in the current font. |
---|
55 | |
---|
56 | static const char* |
---|
57 | expand(const char* from, char* buf, double maxw, int& n, double &width, |
---|
58 | int wrap, int draw_symbols) { |
---|
59 | char* o = buf; |
---|
60 | char* e = buf+(MAXBUF-4); |
---|
61 | underline_at = 0; |
---|
62 | char* word_end = o; |
---|
63 | const char* word_start = from; |
---|
64 | double w = 0; |
---|
65 | |
---|
66 | const char* p = from; |
---|
67 | for (;; p++) { |
---|
68 | |
---|
69 | int c = *p & 255; |
---|
70 | |
---|
71 | if (!c || c == ' ' || c == '\n') { |
---|
72 | // test for word-wrap: |
---|
73 | if (word_start < p && wrap) { |
---|
74 | double newwidth = w + fl_width(word_end, o-word_end); |
---|
75 | if (word_end > buf && newwidth > maxw) { // break before this word |
---|
76 | o = word_end; |
---|
77 | p = word_start; |
---|
78 | break; |
---|
79 | } |
---|
80 | word_end = o; |
---|
81 | w = newwidth; |
---|
82 | } |
---|
83 | if (!c) break; |
---|
84 | else if (c == '\n') {p++; break;} |
---|
85 | word_start = p+1; |
---|
86 | } |
---|
87 | |
---|
88 | if (o > e) break; // don't overflow buffer |
---|
89 | |
---|
90 | if (c == '\t') { |
---|
91 | for (c = (o-buf)%8; c<8 && o<e; c++) *o++ = ' '; |
---|
92 | } else if (c == '&' && fl_draw_shortcut && *(p+1)) { |
---|
93 | if (*(p+1) == '&') {p++; *o++ = '&';} |
---|
94 | else if (fl_draw_shortcut != 2) underline_at = o; |
---|
95 | } else if (c < ' ' || c == 127) { // ^X |
---|
96 | *o++ = '^'; |
---|
97 | *o++ = c ^ 0x40; |
---|
98 | #ifdef __APPLE__ |
---|
99 | } else if (c == 0xCA) { // non-breaking space in MacRoman |
---|
100 | #else |
---|
101 | } else if (c == 0xA0) { // non-breaking space in ISO 8859 |
---|
102 | #endif |
---|
103 | *o++ = ' '; |
---|
104 | } else if (c == '@' && draw_symbols) { // Symbol??? |
---|
105 | if (p[1] && p[1] != '@') break; |
---|
106 | *o++ = c; |
---|
107 | if (p[1]) p++; |
---|
108 | } else { |
---|
109 | *o++ = c; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | width = w + fl_width(word_end, o-word_end); |
---|
114 | *o = 0; |
---|
115 | n = o-buf; |
---|
116 | return p; |
---|
117 | } |
---|
118 | |
---|
119 | void fl_draw( |
---|
120 | const char* str, // the (multi-line) string |
---|
121 | int x, int y, int w, int h, // bounding box |
---|
122 | Fl_Align align, |
---|
123 | void (*callthis)(const char*,int,int,int), |
---|
124 | Fl_Image* img, int draw_symbols) { |
---|
125 | const char* p; |
---|
126 | const char* e; |
---|
127 | char buf[MAXBUF]; |
---|
128 | int buflen; |
---|
129 | char symbol[2][255], *symptr; |
---|
130 | int symwidth[2], symoffset, symtotal; |
---|
131 | |
---|
132 | // count how many lines and put the last one into the buffer: |
---|
133 | int lines; |
---|
134 | double width; |
---|
135 | |
---|
136 | symbol[0][0] = '\0'; |
---|
137 | symwidth[0] = 0; |
---|
138 | |
---|
139 | symbol[1][0] = '\0'; |
---|
140 | symwidth[1] = 0; |
---|
141 | |
---|
142 | if (draw_symbols) { |
---|
143 | if (str && str[0] == '@' && str[1] && str[1] != '@') { |
---|
144 | // Start with a symbol... |
---|
145 | for (symptr = symbol[0]; |
---|
146 | *str && !isspace(*str) && symptr < (symbol[0] + sizeof(symbol[0]) - 1); |
---|
147 | *symptr++ = *str++); |
---|
148 | *symptr = '\0'; |
---|
149 | if (isspace(*str)) str++; |
---|
150 | symwidth[0] = min(w,h); |
---|
151 | } |
---|
152 | |
---|
153 | if (str && (p = strrchr(str, '@')) != NULL && p > (str + 1) && p[-1] != '@') { |
---|
154 | strlcpy(symbol[1], p, sizeof(symbol[1])); |
---|
155 | symwidth[1] = min(w,h); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | symtotal = symwidth[0] + symwidth[1]; |
---|
160 | |
---|
161 | for (p = str, lines=0; p;) { |
---|
162 | e = expand(p, buf, w - symtotal, buflen, width, align&FL_ALIGN_WRAP, |
---|
163 | draw_symbols); |
---|
164 | lines++; |
---|
165 | if (!*e || (*e == '@' && e[1] != '@' && draw_symbols)) break; |
---|
166 | p = e; |
---|
167 | } |
---|
168 | |
---|
169 | if ((symwidth[0] || symwidth[1]) && lines) { |
---|
170 | if (symwidth[0]) symwidth[0] = lines * fl_height(); |
---|
171 | if (symwidth[1]) symwidth[1] = lines * fl_height(); |
---|
172 | } |
---|
173 | |
---|
174 | symtotal = symwidth[0] + symwidth[1]; |
---|
175 | |
---|
176 | // figure out vertical position of the first line: |
---|
177 | int xpos; |
---|
178 | int ypos; |
---|
179 | int height = fl_height(); |
---|
180 | int imgh = img ? img->h() : 0; |
---|
181 | |
---|
182 | symoffset = 0; |
---|
183 | |
---|
184 | if (align & FL_ALIGN_BOTTOM) ypos = y+h-(lines-1)*height-imgh; |
---|
185 | else if (align & FL_ALIGN_TOP) ypos = y+height; |
---|
186 | else ypos = y+(h-lines*height-imgh)/2+height; |
---|
187 | |
---|
188 | // draw the image unless the "text over image" alignment flag is set... |
---|
189 | if (img && !(align & FL_ALIGN_TEXT_OVER_IMAGE)) { |
---|
190 | if (img->w() > symoffset) symoffset = img->w(); |
---|
191 | |
---|
192 | if (align & FL_ALIGN_LEFT) xpos = x + symwidth[0]; |
---|
193 | else if (align & FL_ALIGN_RIGHT) xpos = x + w - img->w() - symwidth[1]; |
---|
194 | else xpos = x + (w - img->w() - symtotal) / 2 + symwidth[0]; |
---|
195 | |
---|
196 | img->draw(xpos, ypos - height); |
---|
197 | ypos += img->h(); |
---|
198 | } |
---|
199 | |
---|
200 | // now draw all the lines: |
---|
201 | if (str) { |
---|
202 | int desc = fl_descent(); |
---|
203 | for (p=str; ; ypos += height) { |
---|
204 | if (lines>1) e = expand(p, buf, w - symtotal, buflen, width, |
---|
205 | align&FL_ALIGN_WRAP, draw_symbols); |
---|
206 | else e = ""; |
---|
207 | |
---|
208 | if (width > symoffset) symoffset = (int)(width + 0.5); |
---|
209 | |
---|
210 | if (align & FL_ALIGN_LEFT) xpos = x + symwidth[0]; |
---|
211 | else if (align & FL_ALIGN_RIGHT) xpos = x + w - (int)(width + .5) - symwidth[1]; |
---|
212 | else xpos = x + (w - (int)(width + .5) - symtotal) / 2 + symwidth[0]; |
---|
213 | |
---|
214 | callthis(buf,buflen,xpos,ypos-desc); |
---|
215 | |
---|
216 | if (underline_at && underline_at >= buf && underline_at < (buf + buflen)) |
---|
217 | callthis("_",1,xpos+int(fl_width(buf,underline_at-buf)),ypos-desc); |
---|
218 | |
---|
219 | if (!*e || (*e == '@' && e[1] != '@')) break; |
---|
220 | p = e; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | // draw the image if the "text over image" alignment flag is set... |
---|
225 | if (img && (align & FL_ALIGN_TEXT_OVER_IMAGE)) { |
---|
226 | if (img->w() > symoffset) symoffset = img->w(); |
---|
227 | |
---|
228 | if (align & FL_ALIGN_LEFT) xpos = x + symwidth[0]; |
---|
229 | else if (align & FL_ALIGN_RIGHT) xpos = x + w - img->w() - symwidth[1]; |
---|
230 | else xpos = x + (w - img->w() - symtotal) / 2 + symwidth[0]; |
---|
231 | |
---|
232 | img->draw(xpos, ypos); |
---|
233 | } |
---|
234 | |
---|
235 | // draw the symbols, if any... |
---|
236 | if (symwidth[0]) { |
---|
237 | // draw to the left |
---|
238 | if (align & FL_ALIGN_LEFT) xpos = x; |
---|
239 | else if (align & FL_ALIGN_RIGHT) xpos = x + w - symtotal - symoffset; |
---|
240 | else xpos = x + (w - symoffset - symtotal) / 2; |
---|
241 | |
---|
242 | if (align & FL_ALIGN_BOTTOM) ypos = y + h - symwidth[0]; |
---|
243 | else if (align & FL_ALIGN_TOP) ypos = y; |
---|
244 | else ypos = y + (h - symwidth[0]) / 2; |
---|
245 | |
---|
246 | fl_draw_symbol(symbol[0], xpos, ypos, symwidth[0], symwidth[0], fl_color()); |
---|
247 | } |
---|
248 | |
---|
249 | if (symwidth[1]) { |
---|
250 | // draw to the right |
---|
251 | if (align & FL_ALIGN_LEFT) xpos = x + symoffset + symwidth[0]; |
---|
252 | else if (align & FL_ALIGN_RIGHT) xpos = x + w - symwidth[1]; |
---|
253 | else xpos = x + (w - symoffset - symtotal) / 2 + symoffset + symwidth[0]; |
---|
254 | |
---|
255 | if (align & FL_ALIGN_BOTTOM) ypos = y + h - symwidth[1]; |
---|
256 | else if (align & FL_ALIGN_TOP) ypos = y; |
---|
257 | else ypos = y + (h - symwidth[1]) / 2; |
---|
258 | |
---|
259 | fl_draw_symbol(symbol[1], xpos, ypos, symwidth[1], symwidth[1], fl_color()); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | void fl_draw( |
---|
264 | const char* str, // the (multi-line) string |
---|
265 | int x, int y, int w, int h, // bounding box |
---|
266 | Fl_Align align, |
---|
267 | Fl_Image* img, |
---|
268 | int draw_symbols) { |
---|
269 | if ((!str || !*str) && !img) return; |
---|
270 | if (w && h && !fl_not_clipped(x, y, w, h) && (align & FL_ALIGN_INSIDE)) return; |
---|
271 | if (align & FL_ALIGN_CLIP) fl_clip(x, y, w, h); |
---|
272 | fl_draw(str, x, y, w, h, align, fl_draw, img, draw_symbols); |
---|
273 | if (align & FL_ALIGN_CLIP) fl_pop_clip(); |
---|
274 | } |
---|
275 | |
---|
276 | void fl_measure(const char* str, int& w, int& h, int draw_symbols) { |
---|
277 | if (!str || !*str) {w = 0; h = 0; return;} |
---|
278 | h = fl_height(); |
---|
279 | const char* p; |
---|
280 | const char* e; |
---|
281 | char buf[MAXBUF]; |
---|
282 | int buflen; |
---|
283 | int lines; |
---|
284 | double width; |
---|
285 | int W = 0; |
---|
286 | char symbol[2][255], *symptr; |
---|
287 | int symwidth[2], symtotal; |
---|
288 | |
---|
289 | // count how many lines and put the last one into the buffer: |
---|
290 | symbol[0][0] = '\0'; |
---|
291 | symwidth[0] = 0; |
---|
292 | |
---|
293 | symbol[1][0] = '\0'; |
---|
294 | symwidth[1] = 0; |
---|
295 | |
---|
296 | if (draw_symbols) { |
---|
297 | if (str && str[0] == '@' && str[1] && str[1] != '@') { |
---|
298 | // Start with a symbol... |
---|
299 | for (symptr = symbol[0]; |
---|
300 | *str && !isspace(*str) && symptr < (symbol[0] + sizeof(symbol[0]) - 1); |
---|
301 | *symptr++ = *str++); |
---|
302 | *symptr = '\0'; |
---|
303 | if (isspace(*str)) str++; |
---|
304 | symwidth[0] = h; |
---|
305 | } |
---|
306 | |
---|
307 | if (str && (p = strrchr(str, '@')) != NULL && p > (str + 1) && p[-1]!='@') { |
---|
308 | strlcpy(symbol[1], p, sizeof(symbol[1])); |
---|
309 | symwidth[1] = h; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | symtotal = symwidth[0] + symwidth[1]; |
---|
314 | |
---|
315 | for (p = str, lines=0; p;) { |
---|
316 | e = expand(p, buf, w - symtotal, buflen, width, w != 0, draw_symbols); |
---|
317 | if ((int)ceil(width) > W) W = (int)ceil(width); |
---|
318 | lines++; |
---|
319 | if (!*e || (*e == '@' && draw_symbols)) break; |
---|
320 | p = e; |
---|
321 | } |
---|
322 | |
---|
323 | if ((symwidth[0] || symwidth[1]) && lines) { |
---|
324 | if (symwidth[0]) symwidth[0] = lines * fl_height(); |
---|
325 | if (symwidth[1]) symwidth[1] = lines * fl_height(); |
---|
326 | } |
---|
327 | |
---|
328 | symtotal = symwidth[0] + symwidth[1]; |
---|
329 | |
---|
330 | w = W + symtotal; |
---|
331 | h = lines*h; |
---|
332 | } |
---|
333 | |
---|
334 | // |
---|
335 | // End of "$Id$". |
---|
336 | // |
---|