themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Cairo_Theme.cxx
blob8a6593b8d6ade03373321c312b9d3b4a383362cf
2 /*******************************************************************************/
3 /* Copyright (C) 2012 Jonathan Moore Liles */
4 /* Copyright (C) 2001-2005 by Colin Jones */
5 /* */
6 /* This program is free software; you can redistribute it and/or modify it */
7 /* under the terms of the GNU General Public License as published by the */
8 /* Free Software Foundation; either version 2 of the License, or (at your */
9 /* option) any later version. */
10 /* */
11 /* This program is distributed in the hope that it will be useful, but WITHOUT */
12 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
13 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
14 /* more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License along */
17 /* with This program; see the file COPYING. If not,write to the Free Software */
18 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /*******************************************************************************/
21 #include <cairo/cairo.h>
23 #include <FL/Fl.H>
24 #include <FL/Fl_Cairo.H>
25 #include <FL/x.H>
26 #include <FL/fl_draw.H>
28 #include "FL/Fl_Theme.H"
29 #include <math.h>
31 float fl_box_saturation = 0.8f;
32 bool fl_boxes_use_gradients = true;
33 bool fl_debug_boxes = false;
35 static const int DX = 1;
37 #define BSCALE 0.00392156862f
39 static void cairo_color(Fl_Color c)
41 cairo_t *cr = Fl::cairo_cc();
43 uchar r,g,b;
45 c = fl_color_average( c, FL_GRAY, fl_box_saturation );
47 Fl_Color bc = Fl::draw_box_active() ? c : fl_inactive( c );
49 fl_color( bc );
51 Fl::get_color( bc, r, g, b );
53 cairo_set_source_rgb( cr, r * BSCALE, g * BSCALE, b * BSCALE );
56 static void rect_path ( int x, int y, int w, int h, double radius )
58 cairo_t *cr = Fl::cairo_cc();
60 double degrees = M_PI / 180.0;
62 x += DX; y += DX; w -= DX*2; h -= DX*2;
64 cairo_new_sub_path (cr);
65 cairo_arc (cr, x + w - radius, y + radius, radius, -90 * degrees, 0 * degrees);
66 cairo_arc (cr, x + w - radius, y + h - radius, radius, 0 * degrees, 90 * degrees);
67 cairo_arc (cr, x + radius, y + h - radius, radius, 90 * degrees, 180 * degrees);
68 cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
69 cairo_close_path (cr);
72 static void draw_rect(int x, int y, int w, int h, Fl_Color bc, double radius = 1.5 )
74 cairo_t *cr = Fl::cairo_cc();
76 rect_path( x, y, w, h, radius );
78 cairo_color( bc );
80 cairo_set_line_width (cr, DX);
81 cairo_stroke (cr);
82 cairo_set_line_width (cr, 1);
85 static void draw_rectf(int x, int y, int w, int h, Fl_Color bc, double radius = 1.5 )
87 /* // Draw the outline around the perimeter of the box */
88 /* fl_color(fl_color_average(FL_BLACK, FL_BACKGROUND_COLOR, .1)); */
90 cairo_t *cr = Fl::cairo_cc();
92 rect_path( x, y, w, h, radius );
94 uchar r,g,b, br,bg,bb;
96 cairo_color( bc );
98 Fl::get_color( fl_color(), r, g, b );
100 Fl::get_color( FL_BACKGROUND_COLOR, br, bg, bb );
102 float rf = r * BSCALE,
103 gf = g * BSCALE,
104 bf = b * BSCALE,
105 brf = br * BSCALE,
106 bgf = bg * BSCALE,
107 bbf = bb * BSCALE;
109 cairo_pattern_t *grad = 0;
111 if ( fl_boxes_use_gradients )
114 /* grad = cairo_pattern_create_linear( x, y, x, y + h );//, 350.0, 350.0); */
115 grad = cairo_pattern_create_linear( x, y, x, y + 5 );//, 350.0, 350.0);
117 /* cairo_pattern_add_color_stop_rgb( grad, 0.8, rf + 0.05, gf + 0.05, bf + 0.05 ); */
118 /* cairo_pattern_add_color_stop_rgb( grad, 0.2, rf + 0.05, gf + 0.05, bf + 0.05 ); */
119 /* cairo_pattern_add_color_stop_rgb( grad, 0.0, rf + 0.2, gf + 0.2, bf + 0.2 ); */
121 cairo_pattern_add_color_stop_rgb( grad, 1.0, rf + 0.05, gf + 0.05, bf + 0.05 );
122 cairo_pattern_add_color_stop_rgb( grad, 0.0, rf + 0.2, gf + 0.2, bf + 0.2 );
124 cairo_set_source( cr, grad );
126 else
128 cairo_set_source_rgb (cr, rf, gf, bf );
131 cairo_fill_preserve (cr);
132 cairo_set_line_width (cr, DX);
134 if ( brf + bgf + bbf > 1.5f )
136 Fl::get_color( fl_color_average( FL_BLACK, fl_color(), 0.5f),
137 r,g,b);
139 else
141 Fl::get_color( fl_color_average( FL_BLACK, fl_color(), 0.8f),
142 r,g,b);
145 cairo_set_source_rgba(cr, r*BSCALE,g*BSCALE,b*BSCALE,1);
147 /* cairo_set_source_rgba (cr, 0, 0, 0, 0.6 ); */
148 cairo_stroke (cr);
150 if ( grad )
151 cairo_pattern_destroy( grad );
153 cairo_set_line_width (cr, 1);
156 static void shade_rect_up(int x, int y, int w, int h, Fl_Color bc)
158 draw_rectf( x, y, w, h, bc );
161 static void frame_rect_up(int x, int y, int w, int h, Fl_Color bc)
163 draw_rect( x,y,w,h, bc );
166 static void frame_rect_down(int x, int y, int w, int h, Fl_Color bc)
168 draw_rect( x,y,w,h, bc );
171 static void shade_rect_down(int x, int y, int w, int h, Fl_Color bc)
173 draw_rectf( x, y, w, h, bc );
176 static void up_frame(int x, int y, int w, int h, Fl_Color c)
178 frame_rect_up(x, y, w, h, c);
181 static void thin_up_box(int x, int y, int w, int h, Fl_Color c)
183 shade_rect_up(x, y, w, h, c);
186 static void up_box(int x, int y, int w, int h, Fl_Color c)
188 shade_rect_up(x, y, w, h, c);
191 static void down_frame(int x, int y, int w, int h, Fl_Color c)
193 frame_rect_down(x, y, w, h, fl_darker(c));
196 static void down_box(int x, int y, int w, int h, Fl_Color c)
198 shade_rect_down(x, y, w, h, c == FL_BACKGROUND_COLOR || c == FL_BACKGROUND2_COLOR ? fl_darker(c) : c );
201 static void thin_down_box(int x, int y, int w, int h, Fl_Color c)
203 down_box(x, y, w, h,
204 FL_BACKGROUND_COLOR == c || FL_BACKGROUND2_COLOR == c
205 ? fl_darker(c)
206 : c );
209 static void border_box(int x, int y, int w, int h, Fl_Color c)
211 cairo_color(c);
213 fl_rectf( x,y,w,h );
215 cairo_color(fl_darker(c));
217 fl_rect(x,y,w,h);
220 static void
221 init_theme ( void )
223 Fl::set_boxtype( FL_UP_BOX, up_box, DX,DX,DX*2,DX*2 );
224 Fl::set_boxtype( FL_DOWN_BOX, down_box, DX,DX,DX*2,DX*2 );
225 Fl::set_boxtype( FL_THIN_UP_BOX, thin_up_box, DX,DX,DX*2,DX*2 );
226 Fl::set_boxtype( FL_THIN_DOWN_BOX, thin_down_box, DX,DX,DX*2,DX*2 );
227 Fl::set_boxtype( FL_UP_FRAME, up_frame, DX,DX,DX*2,DX*2 );
228 Fl::set_boxtype( FL_DOWN_FRAME, down_frame, DX,DX,DX*2,DX*2 );
229 /* Fl::set_boxtype( FL_THIN_UP_BOX, thin_up_box, 1,1,1,1 ); */
230 /* Fl::set_boxtype( FL_THIN_DOWN_BOX, thin_down_box, 1,1,1,1 ); */
231 Fl::set_boxtype( FL_ROUND_UP_BOX, up_box, DX,DX,DX*2,DX*2 );
232 Fl::set_boxtype( FL_ROUND_DOWN_BOX, down_box, DX,DX,DX*2,DX*2 );
233 Fl::set_boxtype( FL_BORDER_BOX, border_box, 1,1,2,2 );
236 void
237 init_cairo_theme ( void )
239 Fl_Theme *t = new Fl_Theme( "Cairo", "Pure Cairo Theme", "Jonathan Moore Liles", init_theme );
241 Fl_Theme::add( t );