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.

229 lines
6.1KB

  1. //
  2. // "$Id: label.cxx 6626 2009-01-12 14:52:45Z fabien $"
  3. //
  4. // Label test 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_Box.H>
  30. #include <FL/Fl_Hor_Value_Slider.H>
  31. #include <FL/Fl_Toggle_Button.H>
  32. #include <FL/Fl_Input.H>
  33. #include <FL/Fl_Choice.H>
  34. #include <FL/fl_draw.H>
  35. #include <math.h>
  36. #ifndef M_PI
  37. #define M_PI 3.141592654
  38. #endif
  39. Fl_Toggle_Button *leftb,*rightb,*clipb;
  40. //Fl_Box *text;
  41. Fl_Input *input;
  42. Fl_Hor_Value_Slider *fonts;
  43. Fl_Hor_Value_Slider *sizes;
  44. Fl_Hor_Value_Slider *angles;
  45. Fl_Double_Window *window;
  46. //code taken from fl_engraved_label.cxx
  47. class Rotated_Label_Box : public Fl_Widget{
  48. protected:
  49. void draw(){
  50. draw_box();
  51. fl_font(labelfont(), labelsize());
  52. fl_color(labelcolor());
  53. int dx(0),dy(0);
  54. if(rt_align&FL_ALIGN_CLIP)fl_push_clip(x(),y(),w(),h());
  55. else fl_push_no_clip();
  56. fl_measure(rt_text,dx,dy);
  57. if(rt_align&FL_ALIGN_LEFT){
  58. dx=dy=0;
  59. }else if(rt_align&FL_ALIGN_RIGHT){
  60. dy=(int)(-sin(M_PI*(double)(rt_angle+180)/180.)*(double)dx);
  61. dx=(int)(cos(M_PI*(double)(rt_angle+180)/180.)*(double)dx);
  62. }else{
  63. dy=(int)(sin(M_PI*(double)rt_angle/180.)*(double)dx);
  64. dx=(int)(-cos(M_PI*(double)rt_angle/180.)*(double)dx);
  65. dx/=2;dy/=2;
  66. }
  67. if(labeltype()==FL_SHADOW_LABEL)shadow_label(x()+w()/2+dx,y()+h()/2+dy);
  68. else if(labeltype()==FL_ENGRAVED_LABEL)engraved_label(x()+w()/2+dx,y()+h()/2+dy);
  69. else if(labeltype()==FL_EMBOSSED_LABEL)embossed_label(x()+w()/2+dx,y()+h()/2+dy);
  70. else{
  71. fl_draw(rt_angle,rt_text,x()+w()/2+dx,y()+h()/2+dy);
  72. }
  73. fl_pop_clip();
  74. draw_label();
  75. }
  76. void innards(int X, int Y, int data[][3], int n){
  77. for (int i = 0; i < n; i++) {
  78. fl_color((Fl_Color)(i < n-1 ? data[i][2] : labelcolor()));
  79. fl_draw(rt_angle,rt_text, X+data[i][0], Y+data[i][1]);
  80. }
  81. }
  82. void shadow_label(int X, int Y){
  83. static int data[2][3] = {{2,2,FL_DARK3},{0,0,0}};
  84. innards(X, Y, data, 2);
  85. }
  86. void engraved_label(int X, int Y){
  87. static int data[7][3] = {
  88. {1,0,FL_LIGHT3},{1,1,FL_LIGHT3},{0,1,FL_LIGHT3},
  89. {-1,0,FL_DARK3},{-1,-1,FL_DARK3},{0,-1,FL_DARK3},
  90. {0,0,0}};
  91. innards(X, Y, data, 7);
  92. }
  93. void embossed_label(int X, int Y){
  94. static int data[7][3] = {
  95. {-1,0,FL_LIGHT3},{-1,-1,FL_LIGHT3},{0,-1,FL_LIGHT3},
  96. {1,0,FL_DARK3},{1,1,FL_DARK3},{0,1,FL_DARK3},
  97. {0,0,0}};
  98. innards(X, Y, data, 7);
  99. }
  100. public:
  101. Rotated_Label_Box(int X, int Y, int W, int H, const char*L=0):
  102. Fl_Widget(X,Y,W,H,L),rt_angle(0),rt_align((Fl_Align)0){
  103. rt_text=input->value();
  104. };
  105. int rt_angle;
  106. const char* rt_text;
  107. Fl_Align rt_align;
  108. }*text;
  109. void button_cb(Fl_Widget *,void *) {
  110. int i = 0;
  111. if (leftb->value()) i |= FL_ALIGN_LEFT;
  112. if (rightb->value()) i |= FL_ALIGN_RIGHT;
  113. if (clipb->value()) i |= FL_ALIGN_CLIP;
  114. text->rt_align=(Fl_Align)i;
  115. window->redraw();
  116. }
  117. void font_cb(Fl_Widget *,void *) {
  118. text->labelfont(int(fonts->value()));
  119. window->redraw();
  120. }
  121. void size_cb(Fl_Widget *,void *) {
  122. text->labelsize(int(sizes->value()));
  123. window->redraw();
  124. }
  125. void angle_cb(Fl_Widget *,void *) {
  126. text->rt_angle=(int)angles->value();
  127. window->redraw();
  128. }
  129. void input_cb(Fl_Widget *,void *) {
  130. text->rt_text=input->value();
  131. window->redraw();
  132. }
  133. void normal_cb(Fl_Widget *,void *) {
  134. text->labeltype(FL_NORMAL_LABEL);
  135. window->redraw();
  136. }
  137. void shadow_cb(Fl_Widget *,void *) {
  138. text->labeltype(FL_SHADOW_LABEL);
  139. window->redraw();
  140. }
  141. void embossed_cb(Fl_Widget *,void *) {
  142. text->labeltype(FL_EMBOSSED_LABEL);
  143. window->redraw();
  144. }
  145. void engraved_cb(Fl_Widget *,void *) {
  146. text->labeltype(FL_ENGRAVED_LABEL);
  147. window->redraw();
  148. }
  149. Fl_Menu_Item choices[] = {
  150. {"FL_NORMAL_LABEL",0,normal_cb},
  151. {"FL_SHADOW_LABEL",0,shadow_cb},
  152. {"FL_ENGRAVED_LABEL",0,engraved_cb},
  153. {"FL_EMBOSSED_LABEL",0,embossed_cb},
  154. {0}};
  155. int main(int argc, char **argv) {
  156. window = new Fl_Double_Window(400,425);
  157. angles= new Fl_Hor_Value_Slider(50,400,350,25,"Angle:");
  158. angles->align(FL_ALIGN_LEFT);
  159. angles->bounds(-360,360);
  160. angles->step(1);
  161. angles->value(0);
  162. angles->callback(angle_cb);
  163. input = new Fl_Input(50,375,350,25);
  164. input->static_value("Rotate Me!!!");
  165. input->when(FL_WHEN_CHANGED);
  166. input->callback(input_cb);
  167. sizes= new Fl_Hor_Value_Slider(50,350,350,25,"Size:");
  168. sizes->align(FL_ALIGN_LEFT);
  169. sizes->bounds(1,64);
  170. sizes->step(1);
  171. sizes->value(14);
  172. sizes->callback(size_cb);
  173. fonts=new Fl_Hor_Value_Slider(50,325,350,25,"Font:");
  174. fonts->align(FL_ALIGN_LEFT);
  175. fonts->bounds(0,15);
  176. fonts->step(1);
  177. fonts->value(0);
  178. fonts->callback(font_cb);
  179. Fl_Group *g = new Fl_Group(50,300,350,25);
  180. leftb = new Fl_Toggle_Button(50,300,50,25,"left");
  181. leftb->callback(button_cb);
  182. rightb = new Fl_Toggle_Button(100,300,50,25,"right");
  183. rightb->callback(button_cb);
  184. clipb = new Fl_Toggle_Button(350,300,50,25,"clip");
  185. clipb->callback(button_cb);
  186. g->resizable(rightb);
  187. g->end();
  188. Fl_Choice *c = new Fl_Choice(50,275,200,25);
  189. c->menu(choices);
  190. text= new Rotated_Label_Box(100,75,200,100,"Widget with rotated text");
  191. text->box(FL_FRAME_BOX);
  192. text->align(FL_ALIGN_BOTTOM);
  193. window->resizable(text);
  194. window->end();
  195. window->show(argc,argv);
  196. return Fl::run();
  197. }
  198. //
  199. // End of "$Id: label.cxx 6626 2009-01-12 14:52:45Z fabien $".
  200. //