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