incomplete merge of master into windows (requires upcoming changes to master to be...
[ardour.git] / gtk2_ardour / canvas-note-event.h
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David 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 "ardour/midi_model.h"
28
29 #include "rgb_macros.h"
30 #include "ardour_ui.h"
31 #include "canvas-noevent-text.h"
32 #include "ui_config.h"
33
34 class Editor;
35 class MidiRegionView;
36
37 namespace Evoral { template<typename T> class Note; }
38
39 namespace Gnome {
40 namespace Canvas {
41
42
43 /** This manages all the event handling for any MIDI event on the canvas.
44  *
45  * This is not actually a canvas item itself to avoid the dreaded diamond,
46  * since various types of canvas items (Note (rect), Hit (diamond), etc)
47  * need to share this functionality but can't share an ancestor.
48  *
49  * Note: Because of this, derived classes need to manually bounce events to
50  * on_event, it won't happen automatically.
51  *
52  * A newer, better canvas should remove the need for all the ugly here.
53  */
54 class CanvasNoteEvent : virtual public sigc::trackable
55 {
56   public:
57         typedef Evoral::Note<ARDOUR::MidiModel::TimeType> NoteType;
58
59         CanvasNoteEvent(
60                         MidiRegionView&                   region,
61                         Item*                             item,
62                         const boost::shared_ptr<NoteType> note = boost::shared_ptr<NoteType>());
63
64         virtual ~CanvasNoteEvent();
65
66         static PBD::Signal1<void,CanvasNoteEvent*> CanvasNoteEventDeleted;
67
68         virtual void show() = 0;
69         virtual void hide() = 0;
70         virtual bool on_event(GdkEvent* ev);
71
72         bool valid() const { return _valid; }
73         void invalidate ();
74         void validate ();
75
76         bool selected() const { return _selected; }
77         void set_selected(bool yn);
78
79         virtual void move_event(double dx, double dy) = 0;
80
81         uint32_t base_color();
82
83         void show_velocity();
84         void hide_velocity();
85
86         /** Channel changed for this specific event */
87         void on_channel_change(uint8_t channel);
88
89         /** Channel selection changed */
90         void on_channel_selection_change(uint16_t selection);
91
92         virtual void set_outline_color(uint32_t c) = 0;
93         virtual void set_fill_color(uint32_t c) = 0;
94
95         virtual double x1() const = 0;
96         virtual double y1() const = 0;
97         virtual double x2() const = 0;
98         virtual double y2() const = 0;
99
100         float mouse_x_fraction() const { return _mouse_x_fraction; }
101         float mouse_y_fraction() const { return _mouse_y_fraction; }
102
103         const boost::shared_ptr<NoteType> note() const { return _note; }
104         MidiRegionView& region_view() const { return _region; }
105
106         inline static uint32_t meter_style_fill_color(uint8_t vel, bool selected) {
107                 if (selected) {
108                         if (vel < 64) {
109                                 return UINT_INTERPOLATE(
110                                         ARDOUR_UI::config()->canvasvar_SelectedMidiNoteColorBase.get(),
111                                         ARDOUR_UI::config()->canvasvar_SelectedMidiNoteColorMid.get(),
112                                         (vel / (double)63.0));
113                         } else {
114                                 return UINT_INTERPOLATE(
115                                         ARDOUR_UI::config()->canvasvar_SelectedMidiNoteColorMid.get(),
116                                         ARDOUR_UI::config()->canvasvar_SelectedMidiNoteColorTop.get(),
117                                         ((vel-64) / (double)63.0));
118                         }
119                 } else {
120                         if (vel < 64) {
121                                 return UINT_INTERPOLATE(
122                                         ARDOUR_UI::config()->canvasvar_MidiNoteColorBase.get(),
123                                         ARDOUR_UI::config()->canvasvar_MidiNoteColorMid.get(),
124                                         (vel / (double)63.0));
125                         } else {
126                                 return UINT_INTERPOLATE(
127                                         ARDOUR_UI::config()->canvasvar_MidiNoteColorMid.get(),
128                                         ARDOUR_UI::config()->canvasvar_MidiNoteColorTop.get(),
129                                         ((vel-64) / (double)63.0));
130                         }
131                 }
132         }
133
134         /// calculate outline colors from fill colors of notes
135         inline static uint32_t calculate_outline(uint32_t color) {
136                 return UINT_INTERPOLATE(color, 0x000000ff, 0.5);
137         }
138
139         /// hue circle divided into 16 equal-looking parts, courtesy Thorsten Wilms
140         static const uint32_t midi_channel_colors[16];
141
142         bool mouse_near_ends () const;
143         bool big_enough_to_trim () const;
144
145   protected:
146         enum State { None, Pressed, Dragging };
147
148         MidiRegionView&                   _region;
149         Item* const                       _item;
150         NoEventText*                      _text;
151         State                             _state;
152         const boost::shared_ptr<NoteType> _note;
153         bool                              _selected;
154         bool                              _valid;
155         float                             _mouse_x_fraction;
156         float                             _mouse_y_fraction;
157         uint16_t                          _channel_selection;
158
159         void set_mouse_fractions (GdkEvent*);
160 };
161
162 } // namespace Gnome
163 } // namespace Canvas
164
165 #endif /* __gtk_ardour_canvas_midi_event_h__ */