You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.2KB

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