1 //
2 // "$Id$"
3 //
4 // Font demo program for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file.  If this
10 // file is missing or damaged, see the license at:
11 //
12 //     http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 //     http://www.fltk.org/str.php
17 //
18 
19 #include <FL/Fl.H>
20 #include <FL/Fl_Double_Window.H>
21 #include <FL/Fl_Tile.H>
22 #include <FL/Fl_Hold_Browser.H>
23 #include <FL/fl_draw.H>
24 #include <FL/Fl_Box.H>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 Fl_Double_Window *form;
30 Fl_Tile *tile;
31 
32 class FontDisplay : public Fl_Widget {
33   void draw();
34 public:
35   int font, size;
FontDisplay(Fl_Boxtype B,int X,int Y,int W,int H,const char * L=0)36   FontDisplay(Fl_Boxtype B, int X, int Y, int W, int H, const char* L = 0) :
37     Fl_Widget(X,Y,W,H,L) {box(B); font = 0; size = 14;}
38 };
draw()39 void FontDisplay::draw() {
40   draw_box();
41   fl_font((Fl_Font)font, size);
42   fl_color(FL_BLACK);
43   fl_draw(label(), x()+3, y()+3, w()-6, h()-6, align());
44 }
45 
46 FontDisplay *textobj;
47 
48 Fl_Hold_Browser *fontobj, *sizeobj;
49 
50 int **sizes;
51 int *numsizes;
52 int pickedsize = 14;
53 
font_cb(Fl_Widget *,long)54 void font_cb(Fl_Widget *, long) {
55   int fn = fontobj->value();
56   if (!fn) return;
57   fn--;
58   textobj->font = fn;
59   sizeobj->clear();
60   int n = numsizes[fn];
61   int *s = sizes[fn];
62   if (!n) {
63     // no sizes
64   } else if (s[0] == 0) {
65     // many sizes;
66     int j = 1;
67     for (int i = 1; i<64 || i<s[n-1]; i++) {
68       char buf[20];
69       if (j < n && i==s[j]) {sprintf(buf,"@b%d",i); j++;}
70       else sprintf(buf,"%d",i);
71       sizeobj->add(buf);
72     }
73     sizeobj->value(pickedsize);
74   } else {
75     // some sizes
76     int w = 0;
77     for (int i = 0; i < n; i++) {
78       if (s[i]<=pickedsize) w = i;
79       char buf[20];
80       sprintf(buf,"@b%d",s[i]);
81       sizeobj->add(buf);
82     }
83     sizeobj->value(w+1);
84   }
85   textobj->redraw();
86 }
87 
size_cb(Fl_Widget *,long)88 void size_cb(Fl_Widget *, long) {
89   int i = sizeobj->value();
90   if (!i) return;
91   const char *c = sizeobj->text(i);
92   while (*c < '0' || *c > '9') c++;
93   pickedsize = atoi(c);
94   textobj->size = pickedsize;
95   textobj->redraw();
96 }
97 
98 char label[0x1000];
99 
create_the_forms()100 void create_the_forms() {
101   // create the sample string
102   int n = 0;
103   strcpy(label, "Hello, world!\n");
104   int i = strlen(label);
105   ulong c;
106   for (c = ' '+1; c < 127; c++) {
107     if (!(c&0x1f)) label[i++]='\n';
108     if (c=='@') label[i++]=c;
109     label[i++]=c;
110   }
111   label[i++] = '\n';
112   for (c = 0xA1; c < 0x600; c += 9) {
113     if (!(++n&(0x1f))) label[i++]='\n';
114     i += fl_utf8encode((unsigned int)c, label + i);
115   }
116   label[i] = 0;
117 
118   // create the basic layout
119   form = new Fl_Double_Window(550,370);
120 
121   tile = new Fl_Tile(0, 0, 550, 370);
122 
123   Fl_Group *textgroup = new Fl_Group(0, 0, 550, 185);
124   textgroup->box(FL_FLAT_BOX);
125   textobj = new FontDisplay(FL_FRAME_BOX,10,10,530,170,label);
126   textobj->align(FL_ALIGN_TOP|FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_CLIP);
127   textobj->color(9,47);
128   textgroup->resizable(textobj);
129   textgroup->end();
130 
131   Fl_Group *fontgroup = new Fl_Group(0, 185, 550, 185);
132   fontgroup->box(FL_FLAT_BOX);
133   fontobj = new Fl_Hold_Browser(10, 190, 390, 170);
134   fontobj->box(FL_FRAME_BOX);
135   fontobj->color(53,3);
136   fontobj->callback(font_cb);
137   sizeobj = new Fl_Hold_Browser(410, 190, 130, 170);
138   sizeobj->box(FL_FRAME_BOX);
139   sizeobj->color(53,3);
140   sizeobj->callback(size_cb);
141   fontgroup->resizable(fontobj);
142   fontgroup->end();
143 
144   tile->end();
145 
146   form->resizable(tile);
147   form->end();
148 }
149 
150 #include <FL/fl_ask.H>
151 
main(int argc,char ** argv)152 int main(int argc, char **argv) {
153   Fl::scheme(NULL);
154   Fl::args(argc, argv);
155   Fl::get_system_colors();
156   create_the_forms();
157 
158 // For the Unicode test, get all fonts...
159 //#ifdef __APPLE__
160   int i = 0;
161 //#else
162 //  int i = fl_choice("Which fonts:","-*","iso8859","All");
163 //#endif
164   int k = Fl::set_fonts(i ? (i>1 ? "*" : 0) : "-*");
165   sizes = new int*[k];
166   numsizes = new int[k];
167   for (i = 0; i < k; i++) {
168     int t; const char *name = Fl::get_font_name((Fl_Font)i,&t);
169     char buffer[128];
170 #if 1
171     if (t) {
172       char *p = buffer;
173       if (t & FL_BOLD) {*p++ = '@'; *p++ = 'b';}
174       if (t & FL_ITALIC) {*p++ = '@'; *p++ = 'i';}
175 	  *p++ = '@'; *p++ = '.'; // Suppress subsequent formatting - some MS fonts have '@' in their name
176       strcpy(p,name);
177       name = buffer;
178     }
179 #else // this is neat, but really slow on some X servers:
180     sprintf(buffer, "@F%d@.%s", i, name);
181     name = buffer;
182 #endif
183     fontobj->add(name);
184     int *s; int n = Fl::get_font_sizes((Fl_Font)i, s);
185     numsizes[i] = n;
186     if (n) {
187       sizes[i] = new int[n];
188       for (int j=0; j<n; j++) sizes[i][j] = s[j];
189     }
190   }
191   fontobj->value(1);
192   font_cb(fontobj,0);
193   form->show(argc,argv);
194   return Fl::run();
195 }
196 
197 //
198 // End of "$Id$".
199 //
200