6528b286591dfe51e2a0f9e8e15804e5053c795e
[ardour.git] / gtk2_ardour / note_base.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_note_base_h__
21 #define __gtk_ardour_note_base_h__
22
23 #include <boost/shared_ptr.hpp>
24 #include "canvas/types.h"
25 #include "ardour/midi_model.h"
26
27 #include "rgb_macros.h"
28 #include "ardour_ui.h"
29 #include "ui_config.h"
30
31 class Editor;
32 class MidiRegionView;
33
34 namespace Evoral {
35         template<typename T> class Note;
36 }
37
38 namespace ArdourCanvas {
39         class Item;
40         class Text;
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  * inheritance pattern, since various types of canvas items (Note (rect), Hit
47  * (diamond), etc) need to share this functionality but can't share an
48  * ancestor. 
49  *
50  * Note: Because of this, derived classes need to manually bounce events to
51  * on_event, it won't happen automatically.
52  */
53
54 class NoteBase : public sigc::trackable
55 {
56 public:
57         typedef Evoral::Note<ARDOUR::MidiModel::TimeType> NoteType;
58
59         NoteBase (MidiRegionView& region, bool, const boost::shared_ptr<NoteType> note = boost::shared_ptr<NoteType>());
60         virtual ~NoteBase ();
61
62         void set_item (ArdourCanvas::Item *);
63
64         static PBD::Signal1<void, NoteBase*> NoteBaseDeleted;
65
66         virtual void show() = 0;
67         virtual void hide() = 0;
68
69         bool valid() const { return _valid; }
70         void invalidate ();
71         void validate ();
72
73         bool selected() const { return _selected; }
74         void set_selected(bool yn);
75
76         virtual void move_event(double dx, double dy) = 0;
77
78         uint32_t base_color();
79
80         void show_velocity();
81         void hide_velocity();
82
83         /** Channel changed for this specific event */
84         void on_channel_change(uint8_t channel);
85
86         /** Channel selection changed */
87         void on_channel_selection_change(uint16_t selection);
88
89         virtual void set_outline_color(uint32_t c) = 0;
90         virtual void set_fill_color(uint32_t c) = 0;
91
92         virtual ArdourCanvas::Coord x0 () const = 0;
93         virtual ArdourCanvas::Coord y0 () const = 0;
94         virtual ArdourCanvas::Coord x1 () const = 0;
95         virtual ArdourCanvas::Coord y1 () const = 0;
96
97         float mouse_x_fraction() const { return _mouse_x_fraction; }
98         float mouse_y_fraction() const { return _mouse_y_fraction; }
99
100         const boost::shared_ptr<NoteType> note() const { return _note; }
101         MidiRegionView& region_view() const { return _region; }
102
103         inline static uint32_t meter_style_fill_color(uint8_t vel, bool selected) {
104                 if (selected) {
105                         if (vel < 64) {
106                                 return UINT_INTERPOLATE(
107                                         ARDOUR_UI::config()->get_canvasvar_SelectedMidiNoteColorBase(),
108                                         ARDOUR_UI::config()->get_canvasvar_SelectedMidiNoteColorMid(),
109                                         (vel / (double)63.0));
110                         } else {
111                                 return UINT_INTERPOLATE(
112                                         ARDOUR_UI::config()->get_canvasvar_SelectedMidiNoteColorMid(),
113                                         ARDOUR_UI::config()->get_canvasvar_SelectedMidiNoteColorTop(),
114                                         ((vel-64) / (double)63.0));
115                         }
116                 } else {
117                         if (vel < 64) {
118                                 return UINT_INTERPOLATE(
119                                         ARDOUR_UI::config()->get_canvasvar_MidiNoteColorBase(),
120                                         ARDOUR_UI::config()->get_canvasvar_MidiNoteColorMid(),
121                                         (vel / (double)63.0));
122                         } else {
123                                 return UINT_INTERPOLATE(
124                                         ARDOUR_UI::config()->get_canvasvar_MidiNoteColorMid(),
125                                         ARDOUR_UI::config()->get_canvasvar_MidiNoteColorTop(),
126                                         ((vel-64) / (double)63.0));
127                         }
128                 }
129         }
130
131         /// calculate outline colors from fill colors of notes
132         inline static uint32_t calculate_outline(uint32_t color) {
133                 return UINT_INTERPOLATE(color, 0x000000ff, 0.5);
134         }
135
136         /// hue circle divided into 16 equal-looking parts, courtesy Thorsten Wilms
137         static const uint32_t midi_channel_colors[16];
138
139         bool mouse_near_ends () const;
140         bool big_enough_to_trim () const;
141
142 protected:
143         enum State { None, Pressed, Dragging };
144
145         MidiRegionView&                   _region;
146         ArdourCanvas::Item*               _item;
147         ArdourCanvas::Text*               _text;
148         State                             _state;
149         const boost::shared_ptr<NoteType> _note;
150         bool                              _with_events;
151         bool                              _own_note;
152         bool                              _selected;
153         bool                              _valid;
154         float                             _mouse_x_fraction;
155         float                             _mouse_y_fraction;
156         
157         void set_mouse_fractions (GdkEvent*);
158
159 private:
160         bool event_handler (GdkEvent *);
161 };
162
163 #endif /* __gtk_ardour_note_h__ */