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.

168 lines
5.4KB

  1. //
  2. // "$Id: arc.cxx 5115 2006-05-12 16:00:00Z fabien $"
  3. //
  4. // Arc drawing 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 <config.h>
  28. #ifdef FLTK_HAVE_CAIRO
  29. #include <FL/Fl_Cairo_Window.H>
  30. #include <FL/Fl_Box.H>
  31. #include <FL/x.H>
  32. #include <FL/fl_draw.H>
  33. #include <FL/math.h>
  34. #define DEF_WIDTH 0.03
  35. // uncomment the following line to enable cairo context autolink feature:
  36. // #define AUTOLINK
  37. // put your drawing stuff here
  38. float drawargs[7] = {90, 90, 100, 100, 0, 360, 0};
  39. const char* name[7] = {"X", "Y", "W", "H", "start", "end", "rotate"};
  40. static void centered_text(cairo_t* cr, double x0,double y0,double w0,double h0, const char * my_text) {
  41. cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_OBLIQUE,CAIRO_FONT_WEIGHT_BOLD);
  42. cairo_set_source_rgba (cr, 0.9, 0.9, 0.4, 0.6);
  43. cairo_text_extents_t extents;
  44. cairo_text_extents (cr, my_text, &extents);
  45. double x = (extents.width/2 + extents.x_bearing);
  46. double y = (extents.height/2 + extents.y_bearing);
  47. cairo_move_to (cr, x0+w0/2-x, y0+h0/2 - y);
  48. cairo_text_path(cr,my_text);
  49. cairo_fill_preserve (cr);
  50. cairo_set_source_rgba (cr, 0, 0, 0,1);
  51. cairo_set_line_width (cr, 0.004);
  52. cairo_stroke (cr);
  53. cairo_set_line_width (cr, DEF_WIDTH);
  54. }
  55. static void round_button(cairo_t* cr, double x0, double y0,
  56. double rect_width, double rect_height, double radius,
  57. double r, double g, double b) {
  58. double x1,y1;
  59. x1=x0+rect_width;
  60. y1=y0+rect_height;
  61. if (!rect_width || !rect_height)
  62. return;
  63. if (rect_width/2<radius) {
  64. if (rect_height/2<radius) {
  65. cairo_move_to (cr, x0, (y0 + y1)/2);
  66. cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
  67. cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
  68. cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
  69. cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
  70. } else {
  71. cairo_move_to (cr, x0, y0 + radius);
  72. cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
  73. cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
  74. cairo_line_to (cr, x1 , y1 - radius);
  75. cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
  76. cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
  77. }
  78. } else {
  79. if (rect_height/2<radius) {
  80. cairo_move_to (cr, x0, (y0 + y1)/2);
  81. cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
  82. cairo_line_to (cr, x1 - radius, y0);
  83. cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
  84. cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
  85. cairo_line_to (cr, x0 + radius, y1);
  86. cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
  87. } else {
  88. cairo_move_to (cr, x0, y0 + radius);
  89. cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
  90. cairo_line_to (cr, x1 - radius, y0);
  91. cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
  92. cairo_line_to (cr, x1 , y1 - radius);
  93. cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
  94. cairo_line_to (cr, x0 + radius, y1);
  95. cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
  96. }
  97. }
  98. cairo_close_path (cr);
  99. cairo_pattern_t *pat=
  100. //cairo_pattern_create_linear (0.0, 0.0, 0.0, 1.0);
  101. cairo_pattern_create_radial (0.25, 0.24, 0.11, 0.24, 0.14, 0.35);
  102. cairo_pattern_set_extend (pat, CAIRO_EXTEND_REFLECT);
  103. cairo_pattern_add_color_stop_rgba (pat, 1.0, r, g, b, 1);
  104. cairo_pattern_add_color_stop_rgba (pat, 0.0, 1, 1, 1, 1);
  105. cairo_set_source (cr, pat);
  106. cairo_fill_preserve (cr);
  107. cairo_pattern_destroy (pat);
  108. //cairo_set_source_rgb (cr, 0.5,0.5,1); cairo_fill_preserve (cr);
  109. cairo_set_source_rgba (cr, 0, 0, 0.5, 0.3);
  110. cairo_stroke (cr);
  111. cairo_set_font_size (cr, 0.08);
  112. centered_text(cr,x0,y0,rect_width, rect_height, "FLTK loves Cairo!");
  113. }
  114. // The cairo rendering cb called during Fl_Cairo_Window::draw() :
  115. static void my_cairo_draw_cb(Fl_Cairo_Window* window, cairo_t* cr) {
  116. int w= window->w(), h = window->h();
  117. cairo_set_line_width (cr, DEF_WIDTH);
  118. cairo_scale (cr, w,h);
  119. round_button(cr,0.1,0.05,0.8,0.2,0.4,0,0,1);
  120. round_button(cr,0.1,0.35,0.8,0.2,0.4,1,0,0);
  121. round_button(cr,0.1,0.65,0.8,0.2,0.4,0,1,0);
  122. return;
  123. }
  124. int main(int argc, char** argv) {
  125. Fl_Cairo_Window window(300,300);
  126. window.resizable(&window);
  127. window.color(FL_WHITE);
  128. window.set_draw_cb(my_cairo_draw_cb);
  129. window.show(argc,argv);
  130. return Fl::run();
  131. }
  132. #else
  133. #include <FL/fl_ask.H>
  134. int main(int argc, char** argv) {
  135. fl_message("please configure fltk with CAIRO enabled (--enable-cairo or --enable-cairoext)");
  136. return 0;
  137. }
  138. #endif
  139. //
  140. // End of "$Id: arc.cxx 5115 2006-05-12 16:00:00Z fabien $".
  141. //