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