86f7312d1755d40ed3fa55967f5b3007a1016cf5
[ardour.git] / gtk2_ardour / canvas-note-event.h
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 #ifndef __gtk_ardour_canvas_midi_event_h__
21 #define __gtk_ardour_canvas_midi_event_h__
22
23 #include <boost/shared_ptr.hpp>
24 #include <libgnomecanvasmm/text.h>
25 #include <libgnomecanvasmm/widget.h>
26
27 #include "rgb_macros.h"
28 #include "ardour_ui.h"
29 #include "ui_config.h"
30 #include "interactive-item.h"
31
32 class Editor;
33 class MidiRegionView;
34
35 namespace Evoral { template<typename T> class Note; }
36
37 namespace Gnome {
38 namespace Canvas {
39
40
41 /** This manages all the event handling for any MIDI event on the canvas.
42  *
43  * This is not actually a canvas item itself to avoid the dreaded diamond,
44  * since various types of canvas items (Note (rect), Hit (diamond), etc)
45  * need to share this functionality but can't share an ancestor.
46  *
47  * Note: Because of this, derived classes need to manually bounce events to
48  * on_event, it won't happen automatically.
49  *
50  * A newer, better canvas should remove the need for all the ugly here.
51  */
52 class CanvasNoteEvent : virtual public sigc::trackable, public InteractiveItem {
53 public:
54         typedef Evoral::Note<double> NoteType;
55         CanvasNoteEvent(
56                         MidiRegionView&                   region,
57                         Item*                             item,
58                         const boost::shared_ptr<NoteType> note = boost::shared_ptr<NoteType>());
59
60         virtual ~CanvasNoteEvent();
61
62         virtual void show() = 0;
63         virtual void hide() = 0;
64         virtual bool on_event(GdkEvent* ev);
65
66         bool selected() const { return _selected; }
67         void selected(bool yn);
68
69         void move_event(double dx, double dy);
70         
71         uint32_t base_color();
72         
73         void show_velocity();
74         void hide_velocity();
75         
76         /** Channel changed for this specific event */
77         void on_channel_change(uint8_t channel);
78
79         /** Channel selection changed */
80         void on_channel_selection_change(uint16_t selection);
81         
82         void show_channel_selector();
83         void hide_channel_selector();
84
85         virtual void set_outline_color(uint32_t c) = 0;
86         virtual void set_fill_color(uint32_t c) = 0;
87         
88         virtual double x1() = 0;
89         virtual double y1() = 0;
90         virtual double x2() = 0;
91         virtual double y2() = 0;
92
93         const boost::shared_ptr<NoteType> note() const { return _note; }
94         
95         inline static uint32_t meter_style_fill_color(uint8_t vel) {
96                 if (vel < 64) {
97                         return UINT_INTERPOLATE(
98                                         ARDOUR_UI::config()->canvasvar_MidiNoteMeterColorBase.get(),
99                                         ARDOUR_UI::config()->canvasvar_MidiNoteMeterColorMid.get(),
100                                         (vel / (double)63.0));
101                 } else {
102                         return UINT_INTERPOLATE(
103                                         ARDOUR_UI::config()->canvasvar_MidiNoteMeterColorMid.get(),
104                                         ARDOUR_UI::config()->canvasvar_MidiNoteMeterColorTop.get(),
105                                         ((vel-64) / (double)63.0));
106                 }
107         }
108         
109         /// calculate outline colors from fill colors of notes
110         inline static uint32_t calculate_outline(uint32_t color) {
111                 return UINT_INTERPOLATE(color, 0x000000ff, 0.5);
112         }
113         
114         /// hue circle divided into 16 equal-looking parts, courtesy Thorsten Wilms
115         static const uint32_t midi_channel_colors[16];
116
117 protected:
118         enum State { None, Pressed, Dragging };
119
120         MidiRegionView&                   _region;
121         Item* const                       _item;
122         InteractiveText*                  _text;
123         Widget*                           _channel_selector_widget;
124         State                             _state;
125         const boost::shared_ptr<NoteType> _note;
126         bool                              _own_note;
127         bool                              _selected;
128 };
129
130 } // namespace Gnome
131 } // namespace Canvas
132
133 #endif /* __gtk_ardour_canvas_midi_event_h__ */