merge with master.
[ardour.git] / libs / canvas / fill.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <cairomm/cairomm.h>
21
22 #include "ardour/utils.h"
23
24 #include "pbd/compose.h"
25 #include "pbd/convert.h"
26
27 #include "canvas/fill.h"
28 #include "canvas/item.h"
29 #include "canvas/types.h"
30 #include "canvas/utils.h"
31
32 using namespace std;
33 using namespace ArdourCanvas;
34
35 Fill::Fill (Item& self)
36         : _self (self)
37         , _fill_color (0x000000ff)
38         , _fill (true)
39         , _transparent (false)
40 {
41 }
42
43 void
44 Fill::set_fill_color (Color color)
45 {
46         if (_fill_color != color) {
47                 _self.begin_visual_change ();
48                 _fill_color = color;
49
50                 double r, g, b, a;
51                 color_to_rgba (color, r, g, b, a);
52                 if (a == 0.0) {
53                         _transparent = true;
54                 } else {
55                         _transparent = false;
56                 }
57
58                 _self.end_visual_change ();
59         }
60 }
61
62 void
63 Fill::set_fill (bool fill)
64 {
65         if (_fill != fill) {
66                 _self.begin_visual_change ();
67                 _fill = fill;
68                 _self.end_visual_change ();
69         }
70 }
71
72 void
73 Fill::setup_fill_context (Cairo::RefPtr<Cairo::Context> context) const
74 {
75         set_source_rgba (context, _fill_color);
76 }
77
78 void
79 Fill::setup_gradient_context (Cairo::RefPtr<Cairo::Context> context, Rect const & self, Duple const & draw_origin) const
80 {
81         Cairo::RefPtr<Cairo::LinearGradient> _gradient;
82         
83         if (_vertical_gradient) {
84                 _gradient = Cairo::LinearGradient::create (draw_origin.x, self.y0, draw_origin.x, self.y1);
85         } else {
86                 _gradient = Cairo::LinearGradient::create (self.x0, draw_origin.y, self.x1, draw_origin.y);
87         }
88         
89         for (StopList::const_iterator s = _stops.begin(); s != _stops.end(); ++s) {
90                 double r, g, b, a;
91                 color_to_rgba (s->second, r, g, b, a);
92                 _gradient->add_color_stop_rgba (s->first, r, g, b, a);
93         }
94         
95         context->set_source (_gradient);
96 }
97
98 void
99 Fill::set_gradient (StopList const & stops, bool vertical)
100 {
101         _self.begin_visual_change ();
102
103         if (stops.empty()) {
104                 _stops.clear ();
105         } else {
106                 _stops = stops;
107                 _vertical_gradient = vertical;
108         }
109
110         _self.end_visual_change ();
111 }