Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / arc.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <algorithm>
20 #include <cairomm/context.h>
21 #include "pbd/compose.h"
22 #include "canvas/circle.h"
23 #include "canvas/types.h"
24 #include "canvas/debug.h"
25 #include "canvas/utils.h"
26 #include "canvas/canvas.h"
27
28 using namespace std;
29 using namespace ArdourCanvas;
30
31 Arc::Arc (Group* parent)
32         : Item (parent)
33         , Outline (parent)
34         , Fill (parent)
35         , _radius (0.0)
36         , _arc_degrees (0.0)
37         , _start_degrees (0.0)
38 {
39
40 }
41
42 void
43 Arc::compute_bounding_box () const
44 {
45         Rect bbox;
46
47         /* this could be smaller in the case of small _arc values
48            but I can't be bothered to optimize it.
49         */
50         
51         bbox.x0 = _center.x - _radius;
52         bbox.y0 = _center.y - _radius;
53         bbox.x1 = _center.x + _radius;
54         bbox.y1 = _center.y + _radius;
55
56         bbox = bbox.expand (0.5 + (_outline_width / 2));
57
58         _bounding_box = bbox;
59         _bounding_box_dirty = false;
60 }
61
62 void
63 Arc::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
64 {
65         if (_radius <= 0.0 || _arc_degrees <= 0.0) {
66                 return;
67         }
68         
69         Duple c = item_to_window (Duple (_center.x, _center.y));
70
71         context->arc (c.x, c.y, _radius, _start_degrees * (M_PI/180.0), _arc_degrees * (M_PI/180.0));
72         setup_fill_context (context);
73         context->fill_preserve ();
74         setup_outline_context (context);
75         context->stroke ();
76 }
77
78 void
79 Arc::set_center (Duple const & c)
80 {
81         begin_change ();
82
83         _center = c;
84
85         _bounding_box_dirty = true;
86         end_change ();
87 }
88
89 void
90 Arc::set_radius (Coord r)
91 {
92         begin_change ();
93         
94         _radius = r;
95
96         _bounding_box_dirty = true;
97         end_change ();
98 }       
99
100
101 void
102 Arc::set_arc (double deg)
103 {
104         begin_change ();
105         
106         _arc_degrees = deg;
107
108         _bounding_box_dirty = true;
109         end_change ();
110 }       
111
112
113 void
114 Arc::set_start (double deg)
115 {
116         begin_change ();
117         
118         _start_degrees = deg;
119         
120         _bounding_box_dirty = true;
121         end_change ();
122 }       
123