fix crash when copy'ing latent plugins
[ardour.git] / gtk2_ardour / hit.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: Dave Robillard
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 "evoral/Beats.hpp"
21 #include "evoral/Note.hpp"
22
23 #include "canvas/polygon.h"
24 #include "canvas/debug.h"
25
26 #include "hit.h"
27
28 using namespace ARDOUR;
29 using namespace ArdourCanvas;
30
31 Hit::Hit (MidiRegionView& region, Item* parent, double size, const boost::shared_ptr<NoteType> note, bool with_events)
32         : NoteBase (region, with_events, note)
33 {
34         _polygon = new ArdourCanvas::Polygon (parent);
35         CANVAS_DEBUG_NAME (_polygon, "note");
36         set_item (_polygon);
37         set_height (size);
38 }
39
40 Hit::~Hit ()
41 {
42         delete _polygon;
43 }
44
45 void
46 Hit::move_event (double dx, double dy)
47 {
48         Points points = _polygon->get();
49         Points moved;
50         for (Points::iterator p = points.begin(); p != points.end(); ++p) {
51                 moved.push_back ((*p).translate (ArdourCanvas::Duple (dx, dy)));
52         }
53         _polygon->set (moved);
54 }
55
56 void
57 Hit::set_outline_color (uint32_t color)
58 {
59         _polygon->set_outline_color (color);
60 }
61
62 void
63 Hit::set_fill_color (uint32_t color)
64 {
65         _polygon->set_fill_color (color);
66 }
67
68 void
69 Hit::show ()
70 {
71         _polygon->show ();
72 }
73
74 void
75 Hit::hide ()
76 {
77         _polygon->hide ();
78 }
79
80 Points
81 Hit::points(Distance height)
82 {
83         /* draw a diamond */
84
85         Points p;
86
87         const double half_height = height/2.0;
88         p.push_back (Duple (-half_height, 0)); // left, middle
89         p.push_back (Duple (0, -half_height)); // top
90         p.push_back (Duple (+half_height, 0)); // right, middle
91         p.push_back (Duple (0, +half_height)); // bottom
92
93         return p;
94 }
95
96 void
97 Hit::set_height (Distance height)
98 {
99         _polygon->set (points(height));
100 }
101
102 Duple
103 Hit::position ()
104 {
105         return _polygon->position ();
106 }
107
108 void
109 Hit::set_position (Duple position)
110 {
111         _polygon->set_position (position);
112 }
113
114 Coord
115 Hit::x0 () const
116 {
117         /* left vertex */
118         return _polygon->position().x + _polygon->get()[0].x;
119 }
120
121 Coord
122 Hit::x1 () const
123 {
124         /* right vertex */
125         return _polygon->position().x + _polygon->get()[2].x;
126 }
127
128 Coord
129 Hit::y0 () const
130 {
131         /* top vertex */
132         return _polygon->position().y + _polygon->get()[1].y;
133 }
134
135 Coord
136 Hit::y1 () const
137 {
138         /* bottom vertex */
139         return _polygon->position().y + _polygon->get()[3].y;
140 }
141
142 void
143 Hit::set_ignore_events (bool ignore)
144 {
145         _polygon->set_ignore_events (ignore);
146 }