2 // "$Id: fl_symbols.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Symbol drawing code for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
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.
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.
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
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 // These are small graphics drawn by the normal label-drawing
29 // code when the string starts with an '@' sign.
31 // Adapted from original code written by:
33 // Written by Mark Overmars
38 #include <FL/fl_draw.H>
44 void (*drawit
)(Fl_Color
);
50 /* Maximal number of symbols in table. Only half of them are
51 used. Should be prime. */
53 static SYMBOL symbols
[MAXSYMBOL
]; /* The symbols */
54 static int symbnumb
= -1; /* Their number */
56 static int find(const char *name
) {
57 // returns hash entry if it exists, or first empty slot:
60 name
[2] ? 71*name
[0]+31*name
[1]+name
[2] : 31*name
[0]+name
[1]
66 (name
[1]) ? 51*name
[0]+3*name
[1] : 3*name
[0]
68 hh2
%= MAXSYMBOL
; if (!hh2
) hh2
= 1;
70 if (!symbols
[pos
].notempty
) return pos
;
71 if (!strcmp(symbols
[pos
].name
,name
)) return pos
;
72 pos
= (pos
+ hh2
) % MAXSYMBOL
;
76 static void fl_init_symbols(void);
78 /**************** The routines seen by the user *************************/
81 Adds a symbol to the system.
82 \param[in] name name of symbol (without the "@")
83 \param[in] drawit function to draw symbol
84 \param[in] scalable set to 1 if \p drawit uses scalable vector drawing
85 \returns 1 on success, 0 on failure
87 int fl_add_symbol(const char *name
, void (*drawit
)(Fl_Color
), int scalable
)
91 if (symbnumb
> MAXSYMBOL
/ 2) return 0; // table is full
93 symbols
[pos
].name
= name
;
94 symbols
[pos
].drawit
= drawit
;
95 symbols
[pos
].notempty
= 1;
96 symbols
[pos
].scalable
= scalable
;
101 int fl_return_arrow(int x
,int y
,int w
,int h
);
104 Draw the named symbol in the given rectangle using the given color
105 \param[in] label name of symbol
106 \param[in] x,y position of symbol
107 \param[in] w,h size of symbol
108 \param[in] col color of symbox
109 \returns 1 on success, 0 on failure
111 // provided for back compatibility:
112 int fl_draw_symbol(const char *label
,int x
,int y
,int w
,int h
,Fl_Color col
) {
113 const char *p
= label
;
114 if (*p
++ != '@') return 0;
117 if (*p
== '#') {equalscale
= 1; p
++;}
118 if (*p
== '-' && p
[1]>='1' && p
[1]<='9') {
120 x
+= n
; y
+= n
; w
-= 2*n
; h
-= 2*n
;
122 } else if (*p
== '+' && p
[1]>='1' && p
[1]<='9') {
124 x
-= n
; y
-= n
; w
+= 2*n
; h
+= 2*n
;
127 if (w
< 10) {x
-= (10-w
)/2; w
= 10;}
128 if (h
< 10) {y
-= (10-h
)/2; h
= 10;}
129 w
= (w
-1)|1; h
= (h
-1)|1;
130 char flip_x
= 0, flip_y
= 0;
142 rotangle
= 1000*(p
[1]-'0') + 100*(p
[2]-'0') + 10*(p
[3]-'0');
145 case '1': rotangle
= 2250; break;
146 case '2': rotangle
= 2700; break;
147 case '3': rotangle
= 3150; break;
148 case '4': rotangle
= 1800; break;
150 case '6': rotangle
= 0; break;
151 case '7': rotangle
= 1350; break;
152 case '8': rotangle
= 900; break;
153 case '9': rotangle
= 450; break;
154 default: rotangle
= 0; p
--; break;
157 if (!symbols
[pos
].notempty
) return 0;
158 if (symbols
[pos
].scalable
== 3) { // kludge to detect return arrow
159 fl_return_arrow(x
,y
,w
,h
);
163 fl_translate(x
+w
/2,y
+h
/2);
164 if (symbols
[pos
].scalable
) {
165 if (equalscale
) {if (w
<h
) h
= w
; else w
= h
;}
166 fl_scale(0.5*w
, 0.5*h
);
167 fl_rotate(rotangle
/10.0);
168 if (flip_x
) fl_scale(-1.0, 1.0);
169 if (flip_y
) fl_scale(1.0, -1.0);
171 (symbols
[pos
].drawit
)(col
);
176 /******************** THE DEFAULT SYMBOLS ****************************/
178 /* Some help stuff */
180 #define BP fl_begin_polygon()
181 #define EP fl_end_polygon()
182 #define BCP fl_begin_complex_polygon()
183 #define ECP fl_end_complex_polygon()
184 #define BL fl_begin_line()
185 #define EL fl_end_line()
186 #define BC fl_begin_loop()
187 #define EC fl_end_loop()
188 #define vv(x,y) fl_vertex(x,y)
190 //for the outline color
191 static void set_outline_color(Fl_Color c
) {
192 fl_color(fl_darker(c
));
195 static void rectangle(double x
,double y
,double x2
,double y2
,Fl_Color col
) {
197 BP
; vv(x
,y
); vv(x2
,y
); vv(x2
,y2
); vv(x
,y2
); EP
;
198 set_outline_color(col
);
199 BC
; vv(x
,y
); vv(x2
,y
); vv(x2
,y2
); vv(x
,y2
); EC
;
202 /* The drawing routines */
204 static void draw_fltk(Fl_Color col
)
208 BCP
; vv(-2.0, -0.5); vv(-1.0, -0.5); vv(-1.0, -0.3); vv(-1.8, -0.3);
209 vv(-1.8, -0.1); vv(-1.2, -0.1); vv(-1.2, 0.1); vv(-1.8, 0.1);
210 vv(-1.8, 0.5); vv(-2.0, 0.5); ECP
;
212 BCP
; vv(-1.0, -0.5); vv(-0.8, -0.5); vv(-0.8, 0.3); vv(0.0, 0.3);
213 vv(0.0, 0.5); vv(-1.0, 0.5); ECP
;
215 BCP
; vv(-0.1, -0.5); vv(1.1, -0.5); vv(1.1, -0.3); vv(0.6, -0.3);
216 vv(0.6, 0.5); vv(0.4, 0.5); vv(0.4, -0.3); vv(-0.1, -0.3); ECP
;
218 BCP
; vv(1.1, -0.5); vv(1.3, -0.5); vv(1.3, -0.15); vv(1.70, -0.5);
219 vv(2.0, -0.5); vv(1.43, 0.0); vv(2.0, 0.5); vv(1.70, 0.5);
220 vv(1.3, 0.15); vv(1.3, 0.5); vv(1.1, 0.5); ECP
;
221 set_outline_color(col
);
223 BC
; vv(-2.0, -0.5); vv(-1.0, -0.5); vv(-1.0, -0.3); vv(-1.8, -0.3);
224 vv(-1.8, -0.1); vv(-1.2, -0.1); vv(-1.2, 0.1); vv(-1.8, 0.1);
225 vv(-1.8, 0.5); vv(-2.0, 0.5); EC
;
227 BC
; vv(-1.0, -0.5); vv(-0.8, -0.5); vv(-0.8, 0.3); vv(0.0, 0.3);
228 vv(0.0, 0.5); vv(-1.0, 0.5); EC
;
230 BC
; vv(-0.1, -0.5); vv(1.1, -0.5); vv(1.1, -0.3); vv(0.6, -0.3);
231 vv(0.6, 0.5); vv(0.4, 0.5); vv(0.4, -0.3); vv(-0.1, -0.3); EC
;
233 BC
; vv(1.1, -0.5); vv(1.3, -0.5); vv(1.3, -0.15); vv(1.70, -0.5);
234 vv(2.0, -0.5); vv(1.43, 0.0); vv(2.0, 0.5); vv(1.70, 0.5);
235 vv(1.3, 0.15); vv(1.3, 0.5); vv(1.1, 0.5); EC
;
238 static void draw_search(Fl_Color col
)
241 BP
; vv(-.4, .13); vv(-1.0, .73); vv(-.73, 1.0); vv(-.13, .4); EP
;
242 set_outline_color(col
);
243 fl_line_style(FL_SOLID
, 3, 0);
244 BC
; fl_circle(.2, -.2, .6); EC
;
245 fl_line_style(FL_SOLID
, 1, 0);
246 BC
; vv(-.4, .13); vv(-1.0, .73); vv(-.73, 1.0); vv(-.13, .4); EC
;
249 static void draw_arrow1(Fl_Color col
)
252 BP
; vv(-0.8,-0.4); vv(-0.8,0.4); vv(0.0,0.4); vv(0.0,-0.4); EP
;
253 BP
; vv(0.0,0.8); vv(0.8,0.0); vv(0.0,-0.8); vv(0.0,-0.4); vv(0.0,0.4); EP
;
254 set_outline_color(col
);
255 BC
; vv(-0.8,-0.4); vv(-0.8,0.4); vv(0.0,0.4); vv(0.0,0.8); vv(0.8,0.0);
256 vv(0.0,-0.8); vv(0.0,-0.4); EC
;
259 static void draw_arrow1bar(Fl_Color col
)
262 rectangle(.6,-.8,.9,.8,col
);
265 static void draw_arrow2(Fl_Color col
)
268 BP
; vv(-0.3,0.8); vv(0.50,0.0); vv(-0.3,-0.8); EP
;
269 set_outline_color(col
);
270 BC
; vv(-0.3,0.8); vv(0.50,0.0); vv(-0.3,-0.8); EC
;
273 static void draw_arrow3(Fl_Color col
)
276 BP
; vv(0.1,0.8); vv(0.9,0.0); vv(0.1,-0.8); EP
;
277 BP
; vv(-0.7,0.8); vv(0.1,0.0); vv(-0.7,-0.8); EP
;
278 set_outline_color(col
);
279 BC
; vv(0.1,0.8); vv(0.9,0.0); vv(0.1,-0.8); EC
;
280 BC
; vv(-0.7,0.8); vv(0.1,0.0); vv(-0.7,-0.8); EC
;
283 static void draw_arrowbar(Fl_Color col
)
286 BP
; vv(0.2,0.8); vv(0.6,0.8); vv(0.6,-0.8); vv(0.2,-0.8); EP
;
287 BP
; vv(-0.6,0.8); vv(0.2,0.0); vv(-0.6,-0.8); EP
;
288 set_outline_color(col
);
289 BC
; vv(0.2,0.8); vv(0.6,0.8); vv(0.6,-0.8); vv(0.2,-0.8); EC
;
290 BC
; vv(-0.6,0.8); vv(0.2,0.0); vv(-0.6,-0.8); EC
;
293 static void draw_arrowbox(Fl_Color col
)
296 BP
; vv(-0.6,0.8); vv(0.2,0.0); vv(-0.6,-0.8); EP
;
297 BC
; vv(0.2,0.8); vv(0.6,0.8); vv(0.6,-0.8); vv(0.2,-0.8); EC
;
298 set_outline_color(col
);
299 BC
; vv(0.2,0.8); vv(0.6,0.8); vv(0.6,-0.8); vv(0.2,-0.8); EC
;
300 BC
; vv(-0.6,0.8); vv(0.2,0.0); vv(-0.6,-0.8); EC
;
303 static void draw_bararrow(Fl_Color col
)
306 BP
; vv(0.1,0.8); vv(0.9,0.0); vv(0.1,-0.8); EP
;
307 BP
; vv(-0.5,0.8); vv(-0.1,0.8); vv(-0.1,-0.8); vv(-0.5,-0.8); EP
;
308 set_outline_color(col
);
309 BC
; vv(0.1,0.8); vv(0.9,0.0); vv(0.1,-0.8); EC
;
310 BC
; vv(-0.5,0.8); vv(-0.1,0.8); vv(-0.1,-0.8); vv(-0.5,-0.8); EC
;
313 static void draw_doublebar(Fl_Color col
) {
314 rectangle(-0.6,-0.8,-.1,.8,col
);
315 rectangle(.1,-0.8,.6,.8,col
);
318 static void draw_arrow01(Fl_Color col
)
319 { fl_rotate(180); draw_arrow1(col
); }
321 static void draw_arrow02(Fl_Color col
)
322 { fl_rotate(180); draw_arrow2(col
); }
324 static void draw_arrow03(Fl_Color col
)
325 { fl_rotate(180); draw_arrow3(col
); }
327 static void draw_0arrowbar(Fl_Color col
)
328 { fl_rotate(180); draw_arrowbar(col
); }
330 static void draw_0arrowbox(Fl_Color col
)
331 { fl_rotate(180); draw_arrowbox(col
); }
333 static void draw_0bararrow(Fl_Color col
)
334 { fl_rotate(180); draw_bararrow(col
); }
336 static void draw_doublearrow(Fl_Color col
)
339 BP
; vv(-0.35,-0.4); vv(-0.35,0.4); vv(0.35,0.4); vv(0.35,-0.4); EP
;
340 BP
; vv(0.15,0.8); vv(0.95,0.0); vv(0.15,-0.8); EP
;
341 BP
; vv(-0.15,0.8); vv(-0.95,0.0); vv(-0.15,-0.8); EP
;
342 set_outline_color(col
);
343 BC
; vv(-0.15,0.4); vv(0.15,0.4); vv(0.15,0.8); vv(0.95,0.0);
344 vv(0.15,-0.8); vv(0.15,-0.4); vv(-0.15,-0.4); vv(-0.15,-0.8);
345 vv(-0.95,0.0); vv(-0.15,0.8); EC
;
348 static void draw_arrow(Fl_Color col
)
351 BP
; vv(0.65,0.1); vv(1.0,0.0); vv(0.65,-0.1); EP
;
352 BL
; vv(-1.0,0.0); vv(0.65,0.0); EL
;
353 set_outline_color(col
);
354 BL
; vv(-1.0,0.0); vv(0.65,0.0); EL
;
355 BC
; vv(0.65,0.1); vv(1.0,0.0); vv(0.65,-0.1); EC
;
358 static void draw_square(Fl_Color col
)
359 { rectangle(-1,-1,1,1,col
); }
361 static void draw_circle(Fl_Color col
) {
362 fl_color(col
); BP
; fl_circle(0,0,1); EP
;
363 set_outline_color(col
);
364 BC
; fl_circle(0,0,1); EC
;
367 static void draw_line(Fl_Color col
)
368 { fl_color(col
); BL
; vv(-1.0,0.0); vv(1.0,0.0); EL
; }
370 static void draw_plus(Fl_Color col
)
373 BP
; vv(-0.9,-0.15); vv(-0.9,0.15); vv(0.9,0.15); vv(0.9,-0.15); EP
;
374 BP
; vv(-0.15,-0.9); vv(-0.15,0.9); vv(0.15,0.9); vv(0.15,-0.9); EP
;
375 set_outline_color(col
);
377 vv(-0.9,-0.15); vv(-0.9,0.15); vv(-0.15,0.15); vv(-0.15,0.9);
378 vv(0.15,0.9); vv(0.15,0.15); vv(0.9,0.15); vv(0.9,-0.15);
379 vv(0.15,-0.15); vv(0.15,-0.9); vv(-0.15,-0.9); vv(-0.15,-0.15);
383 static void draw_uparrow(Fl_Color
) {
385 BL
; vv(-.8,.8); vv(-.8,-.8); vv(.8,0); EL
;
387 BL
; vv(-.8,.8); vv(.8, 0); EL
;
390 static void draw_downarrow(Fl_Color
) {
392 BL
; vv(-.8,.8); vv(-.8,-.8); vv(.8,0); EL
;
394 BL
; vv(-.8,.8); vv(.8, 0); EL
;
397 static void draw_menu(Fl_Color col
)
399 rectangle(-0.65, 0.85, 0.65, -0.25, col
);
400 rectangle(-0.65, -0.6, 0.65, -1.0, col
);
403 // Standard UI icons...
404 static void draw_filenew(Fl_Color c
) {
415 fl_color(fl_lighter(c
));
422 fl_color(fl_darker(c
));
438 static void draw_fileopen(Fl_Color c
) {
450 fl_color(fl_darker(c
));
461 fl_color(fl_lighter(c
));
469 fl_color(fl_darker(c
));
478 static void draw_filesave(Fl_Color c
) {
491 fl_color(fl_lighter(c
));
514 fl_color(fl_darker(c
));
527 static void draw_filesaveas(Fl_Color c
) {
530 fl_color(fl_color_average(c
, FL_WHITE
, 0.25f
));
539 fl_color(fl_darker(c
));
555 static void draw_fileprint(Fl_Color c
) {
566 fl_color(fl_color_average(c
, FL_WHITE
, 0.25f
));
574 fl_color(fl_lighter(c
));
582 fl_color(fl_darker(c
));
604 static void draw_round_arrow(Fl_Color c
, float da
=5.0) {
605 double a
, r
, dr1
=0.005, dr2
=0.015;
607 for (j
=0; j
<2; j
++) {
610 set_outline_color(c
);
619 for (i
=27, a
=140.0, r
=1.0; i
>0; i
--, a
-=da
, r
-=dr1
) {
620 double ar
= a
/180.0 * M_PI
;
621 vv(cos(ar
)*r
, sin(ar
)*r
);
623 for (i
=27; i
>=0; a
+=da
, i
--, r
-=dr2
) {
624 double ar
= a
/180.0 * M_PI
;
625 vv(cos(ar
)*r
, sin(ar
)*r
);
635 static void draw_refresh(Fl_Color c
) {
642 static void draw_reload(Fl_Color c
) {
644 draw_round_arrow(c
, 10);
648 static void draw_undo(Fl_Color c
) {
649 fl_translate(0.0, 0.2);
651 draw_round_arrow(c
, 6);
653 fl_translate(0.0, -0.2);
656 static void draw_redo(Fl_Color c
) {
662 static void fl_init_symbols(void) {
663 static char beenhere
;
664 if (beenhere
) return;
668 fl_add_symbol("", draw_arrow1
, 1);
669 fl_add_symbol("->", draw_arrow1
, 1);
670 fl_add_symbol(">", draw_arrow2
, 1);
671 fl_add_symbol(">>", draw_arrow3
, 1);
672 fl_add_symbol(">|", draw_arrowbar
, 1);
673 fl_add_symbol(">[]", draw_arrowbox
, 1);
674 fl_add_symbol("|>", draw_bararrow
, 1);
675 fl_add_symbol("<-", draw_arrow01
, 1);
676 fl_add_symbol("<", draw_arrow02
, 1);
677 fl_add_symbol("<<", draw_arrow03
, 1);
678 fl_add_symbol("|<", draw_0arrowbar
, 1);
679 fl_add_symbol("[]<", draw_0arrowbox
, 1);
680 fl_add_symbol("<|", draw_0bararrow
, 1);
681 fl_add_symbol("<->", draw_doublearrow
, 1);
682 fl_add_symbol("-->", draw_arrow
, 1);
683 fl_add_symbol("+", draw_plus
, 1);
684 fl_add_symbol("->|", draw_arrow1bar
, 1);
685 fl_add_symbol("arrow", draw_arrow
, 1);
686 fl_add_symbol("returnarrow", 0, 3);
687 fl_add_symbol("square", draw_square
, 1);
688 fl_add_symbol("circle", draw_circle
, 1);
689 fl_add_symbol("line", draw_line
, 1);
690 fl_add_symbol("plus", draw_plus
, 1);
691 fl_add_symbol("menu", draw_menu
, 1);
692 fl_add_symbol("UpArrow", draw_uparrow
, 1);
693 fl_add_symbol("DnArrow", draw_downarrow
, 1);
694 fl_add_symbol("||", draw_doublebar
, 1);
695 fl_add_symbol("search", draw_search
, 1);
696 fl_add_symbol("FLTK", draw_fltk
, 1);
698 fl_add_symbol("filenew", draw_filenew
, 1);
699 fl_add_symbol("fileopen", draw_fileopen
, 1);
700 fl_add_symbol("filesave", draw_filesave
, 1);
701 fl_add_symbol("filesaveas", draw_filesaveas
, 1);
702 fl_add_symbol("fileprint", draw_fileprint
, 1);
704 fl_add_symbol("refresh", draw_refresh
, 1);
705 fl_add_symbol("reload", draw_reload
, 1);
706 fl_add_symbol("undo", draw_undo
, 1);
707 fl_add_symbol("redo", draw_redo
, 1);
709 // fl_add_symbol("file", draw_file, 1);
713 // End of "$Id: fl_symbols.cxx 7903 2010-11-28 21:06:39Z matt $".