Tweak themes for more color consistency.
[ntk.git] / src / Cairo_Theme.cxx
blob39fbe5d9be200fe484d0eedaf980eb841cfa900a
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;
96 cairo_color( bc );
98 Fl::get_color( fl_color(), r, g, b );
100 float rf = r * BSCALE;
101 float gf = g * BSCALE;
102 float bf = b * BSCALE;
104 cairo_pattern_t *grad = 0;
106 if ( fl_boxes_use_gradients )
109 grad = cairo_pattern_create_linear( x, y, x, y + h );//, 350.0, 350.0);
111 cairo_pattern_add_color_stop_rgb( grad, 0.0, rf, gf, bf );
112 cairo_pattern_add_color_stop_rgb( grad, 0.4, rf, gf, bf );
113 cairo_pattern_add_color_stop_rgb( grad, 1.0, rf + 0.1, gf + 0.1, bf + 0.1 );
115 cairo_set_source( cr, grad );
117 else
119 cairo_set_source_rgb (cr, rf, gf, bf );
122 cairo_fill_preserve (cr);
123 cairo_set_line_width (cr, DX);
124 cairo_set_source_rgba (cr, 0, 0, 0, 0.6 );
125 cairo_stroke (cr);
127 if ( grad )
128 cairo_pattern_destroy( grad );
130 cairo_set_line_width (cr, 1);
133 static void shade_rect_up(int x, int y, int w, int h, Fl_Color bc)
135 draw_rectf( x, y, w, h, bc );
138 static void frame_rect_up(int x, int y, int w, int h, Fl_Color bc)
140 draw_rect( x,y,w,h, bc );
143 static void frame_rect_down(int x, int y, int w, int h, Fl_Color bc)
145 draw_rect( x,y,w,h, bc );
148 static void shade_rect_down(int x, int y, int w, int h, Fl_Color bc)
150 draw_rectf( x, y, w, h, bc );
153 static void up_frame(int x, int y, int w, int h, Fl_Color c)
155 frame_rect_up(x, y, w, h, c);
158 static void thin_up_box(int x, int y, int w, int h, Fl_Color c)
160 shade_rect_up(x, y, w, h, c);
163 static void up_box(int x, int y, int w, int h, Fl_Color c)
165 shade_rect_up(x, y, w, h, c);
168 static void down_frame(int x, int y, int w, int h, Fl_Color c)
170 frame_rect_down(x, y, w, h, fl_darker(c));
173 static void down_box(int x, int y, int w, int h, Fl_Color c)
175 shade_rect_down(x, y, w, h, c == FL_BACKGROUND_COLOR || c == FL_BACKGROUND2_COLOR ? fl_darker(c) : c );
178 static void thin_down_box(int x, int y, int w, int h, Fl_Color c)
180 down_box(x, y, w, h,
181 FL_BACKGROUND_COLOR == c || FL_BACKGROUND2_COLOR == c
182 ? fl_darker(c)
183 : c );
186 static void border_box(int x, int y, int w, int h, Fl_Color c)
188 cairo_color(c);
190 fl_rectf( x,y,w,h );
192 cairo_color(fl_darker(c));
194 fl_rect(x,y,w,h);
197 static void
198 init_theme ( void )
200 Fl::set_boxtype( FL_UP_BOX, up_box, DX,DX,DX*2,DX*2 );
201 Fl::set_boxtype( FL_DOWN_BOX, down_box, DX,DX,DX*2,DX*2 );
202 Fl::set_boxtype( FL_THIN_UP_BOX, thin_up_box, DX,DX,DX*2,DX*2 );
203 Fl::set_boxtype( FL_THIN_DOWN_BOX, thin_down_box, DX,DX,DX*2,DX*2 );
204 Fl::set_boxtype( FL_UP_FRAME, up_frame, DX,DX,DX*2,DX*2 );
205 Fl::set_boxtype( FL_DOWN_FRAME, down_frame, DX,DX,DX*2,DX*2 );
206 /* Fl::set_boxtype( FL_THIN_UP_BOX, thin_up_box, 1,1,1,1 ); */
207 /* Fl::set_boxtype( FL_THIN_DOWN_BOX, thin_down_box, 1,1,1,1 ); */
208 Fl::set_boxtype( FL_ROUND_UP_BOX, up_box, DX,DX,DX*2,DX*2 );
209 Fl::set_boxtype( FL_ROUND_DOWN_BOX, down_box, DX,DX,DX*2,DX*2 );
210 Fl::set_boxtype( FL_BORDER_BOX, border_box, 1,1,2,2 );
213 void
214 init_cairo_theme ( void )
216 Fl_Theme *t = new Fl_Theme( "Cairo", "Pure Cairo Theme", "Jonathan Moore Liles", init_theme );
218 Fl_Theme::add( t );