fix crash when copy'ing latent plugins
[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 #include "canvas/colors.h"
32
33 using namespace std;
34 using namespace ArdourCanvas;
35
36 Fill::Fill (Item& self)
37         : _self (self)
38         , _fill_color (0x000000ff)
39         , _fill (true)
40         , _transparent (false)
41 {
42 }
43
44 void
45 Fill::set_fill_color (Color color)
46 {
47         if (_fill_color != color) {
48                 _self.begin_visual_change ();
49                 _fill_color = color;
50
51                 double r, g, b, a;
52                 color_to_rgba (color, r, g, b, a);
53                 if (a == 0.0) {
54                         _transparent = true;
55                 } else {
56                         _transparent = false;
57                 }
58
59                 _self.end_visual_change ();
60         }
61 }
62
63 void
64 Fill::set_fill (bool fill)
65 {
66         if (_fill != fill) {
67                 _self.begin_visual_change ();
68                 _fill = fill;
69                 _self.end_visual_change ();
70         }
71 }
72
73 void
74 Fill::setup_fill_context (Cairo::RefPtr<Cairo::Context> context) const
75 {
76         if (_pattern) {
77                 context->set_source (_pattern);
78         } else {
79                 set_source_rgba (context, _fill_color);
80         }
81 }
82
83 void
84 Fill::setup_gradient_context (Cairo::RefPtr<Cairo::Context> context, Rect const & self, Duple const & draw_origin) const
85 {
86         Cairo::RefPtr<Cairo::LinearGradient> _gradient;
87
88         if (_vertical_gradient) {
89                 _gradient = Cairo::LinearGradient::create (draw_origin.x, self.y0, draw_origin.x, self.y1);
90         } else {
91                 _gradient = Cairo::LinearGradient::create (self.x0, draw_origin.y, self.x1, draw_origin.y);
92         }
93
94         for (StopList::const_iterator s = _stops.begin(); s != _stops.end(); ++s) {
95                 double r, g, b, a;
96                 color_to_rgba (s->second, r, g, b, a);
97                 _gradient->add_color_stop_rgba (s->first, r, g, b, a);
98         }
99
100         context->set_source (_gradient);
101 }
102
103 void
104 Fill::set_pattern (Cairo::RefPtr<Cairo::Pattern> p)
105 {
106         _pattern = p;
107 }
108
109 void
110 Fill::set_gradient (StopList const & stops, bool vertical)
111 {
112         _self.begin_visual_change ();
113
114         if (stops.empty()) {
115                 _stops.clear ();
116         } else {
117                 _stops = stops;
118                 _vertical_gradient = vertical;
119         }
120
121         _self.end_visual_change ();
122 }