Merge branch 'master' into cairocanvas
[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  * 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 NoteBase : public sigc::trackable
55 {
56 public:
57         typedef Evoral::Note<ARDOUR::MidiModel::TimeType> NoteType;
58
59         NoteBase (
60                 MidiRegionView&                   region,
61                 bool,
62                 const boost::shared_ptr<NoteType> note = boost::shared_ptr<NoteType>()
63                 );
64
65         virtual ~NoteBase ();
66
67         void set_item (ArdourCanvas::Item *);
68
69         static PBD::Signal1<void, NoteBase*> NoteBaseDeleted;
70
71         virtual void show() = 0;
72         virtual void hide() = 0;
73
74         bool valid() const { return _valid; }
75         void invalidate ();
76         void validate ();
77
78         bool selected() const { return _selected; }
79         void set_selected(bool yn);
80
81         virtual void move_event(double dx, double dy) = 0;
82
83         uint32_t base_color();
84
85         void show_velocity();
86         void hide_velocity();
87
88         /** Channel changed for this specific event */
89         void on_channel_change(uint8_t channel);
90
91         /** Channel selection changed */
92         void on_channel_selection_change(uint16_t selection);
93
94         void show_channel_selector();
95         void hide_channel_selector();
96
97         virtual void set_outline_color(uint32_t c) = 0;
98         virtual void set_fill_color(uint32_t c) = 0;
99
100         virtual ArdourCanvas::Coord x0 () const = 0;
101         virtual ArdourCanvas::Coord y0 () const = 0;
102         virtual ArdourCanvas::Coord x1 () const = 0;
103         virtual ArdourCanvas::Coord y1 () const = 0;
104
105         float mouse_x_fraction() const { return _mouse_x_fraction; }
106         float mouse_y_fraction() const { return _mouse_y_fraction; }
107
108         const boost::shared_ptr<NoteType> note() const { return _note; }
109         MidiRegionView& region_view() const { return _region; }
110
111         inline static uint32_t meter_style_fill_color(uint8_t vel, bool selected) {
112                 if (selected) {
113                         if (vel < 64) {
114                                 return UINT_INTERPOLATE(
115                                         ARDOUR_UI::config()->canvasvar_SelectedMidiNoteColorBase.get(),
116                                         ARDOUR_UI::config()->canvasvar_SelectedMidiNoteColorMid.get(),
117                                         (vel / (double)63.0));
118                         } else {
119                                 return UINT_INTERPOLATE(
120                                         ARDOUR_UI::config()->canvasvar_SelectedMidiNoteColorMid.get(),
121                                         ARDOUR_UI::config()->canvasvar_SelectedMidiNoteColorTop.get(),
122                                         ((vel-64) / (double)63.0));
123                         }
124                 } else {
125                         if (vel < 64) {
126                                 return UINT_INTERPOLATE(
127                                         ARDOUR_UI::config()->canvasvar_MidiNoteColorBase.get(),
128                                         ARDOUR_UI::config()->canvasvar_MidiNoteColorMid.get(),
129                                         (vel / (double)63.0));
130                         } else {
131                                 return UINT_INTERPOLATE(
132                                         ARDOUR_UI::config()->canvasvar_MidiNoteColorMid.get(),
133                                         ARDOUR_UI::config()->canvasvar_MidiNoteColorTop.get(),
134                                         ((vel-64) / (double)63.0));
135                         }
136                 }
137         }
138
139         /// calculate outline colors from fill colors of notes
140         inline static uint32_t calculate_outline(uint32_t color) {
141                 return UINT_INTERPOLATE(color, 0x000000ff, 0.5);
142         }
143
144         /// hue circle divided into 16 equal-looking parts, courtesy Thorsten Wilms
145         static const uint32_t midi_channel_colors[16];
146
147         bool mouse_near_ends () const;
148         bool big_enough_to_trim () const;
149
150 protected:
151         enum State { None, Pressed, Dragging };
152
153         MidiRegionView&                   _region;
154         ArdourCanvas::Item*               _item;
155         ArdourCanvas::Text*               _text;
156 //      Widget*                           _channel_selector_widget;
157         State                             _state;
158         const boost::shared_ptr<NoteType> _note;
159         bool                              _with_events;
160         bool                              _own_note;
161         bool                              _selected;
162         bool                              _valid;
163         float                             _mouse_x_fraction;
164         float                             _mouse_y_fraction;
165         
166         void set_mouse_fractions (GdkEvent*);
167
168 private:
169         bool event_handler (GdkEvent *);
170 };
171
172 #endif /* __gtk_ardour_note_h__ */