fix clamping of line and rect coordinates to avoid issues with cairo when drawing...
[ardour.git] / libs / canvas / line.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 <algorithm>
21 #include <cairomm/context.h>
22 #include "pbd/compose.h"
23 #include "canvas/line.h"
24 #include "canvas/types.h"
25 #include "canvas/debug.h"
26 #include "canvas/utils.h"
27 #include "canvas/canvas.h"
28
29 using namespace std;
30 using namespace ArdourCanvas;
31
32 Line::Line (Group* parent)
33         : Item (parent)
34         , Outline (parent)
35 {
36
37 }
38
39 void
40 Line::compute_bounding_box () const
41 {
42         Rect bbox;
43         
44         bbox.x0 = min (_points[0].x, _points[1].x);
45         bbox.y0 = min (_points[0].y, _points[1].y);
46         bbox.x1 = max (_points[0].x, _points[1].x);
47         bbox.y1 = max (_points[0].y, _points[1].y);
48
49         bbox = bbox.expand (_outline_width / 2);
50
51         _bounding_box = bbox;
52         _bounding_box_dirty = false;
53 }
54
55 void
56 Line::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
57 {
58         setup_outline_context (context);
59         context->move_to (_points[0].x, _points[0].y);
60         context->line_to (_points[1].x, _points[1].y);
61         context->stroke ();
62 }
63
64 void
65 Line::set (Duple a, Duple b)
66 {
67         begin_change ();
68
69         _points[0] = a;
70         _points[1] = b;
71
72         _bounding_box_dirty = true;
73         end_change ();
74
75         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
76 }
77
78 void
79 Line::set_x0 (Coord x0)
80 {
81         begin_change ();
82         
83         _points[0].x = x0;
84
85         _bounding_box_dirty = true;
86         end_change ();
87
88         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
89 }
90
91 void
92 Line::set_y0 (Coord y0)
93 {
94         begin_change ();
95
96         _points[0].y = y0;
97
98         _bounding_box_dirty = true;
99         end_change ();
100
101         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
102 }
103
104 void
105 Line::set_x1 (Coord x1)
106 {
107         begin_change ();
108
109         _points[1].x = x1;
110
111         _bounding_box_dirty = true;
112         end_change ();
113
114         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
115 }
116
117 void
118 Line::set_y1 (Coord y1)
119 {
120         begin_change ();
121
122         _points[1].y = y1;
123
124         _bounding_box_dirty = true;
125         end_change ();
126
127         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
128 }