convert codebase to use Temporal for various time types
[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
25 #include "temporal/beats.h"
26 #include "canvas/types.h"
27
28 #include "rgb_macros.h"
29 #include "ui_config.h"
30
31 class Editor;
32 class MidiRegionView;
33
34 namespace Evoral {
35         template<typename T> class Note;
36         class Beats;
37 }
38
39 namespace ArdourCanvas {
40         class Item;
41         class Text;
42 }
43
44 /** Base class for canvas notes (sustained note rectangles and hit diamonds).
45  *
46  * This is not actually a canvas item itself to avoid the dreaded diamond
47  * inheritance pattern, since various types of canvas items (Note (rect), Hit
48  * (diamond), etc) need to share this functionality but can't share an
49  * ancestor.
50  *
51  * Note: Because of this, derived classes need to manually bounce events to
52  * on_event, it won't happen automatically.
53  */
54 class NoteBase : public sigc::trackable
55 {
56 public:
57         typedef Evoral::Note<Temporal::Beats> 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         ArdourCanvas::Item* item() const { return _item; }
64
65         virtual void show() = 0;
66         virtual void hide() = 0;
67
68         bool valid() const { return _valid; }
69         void invalidate ();
70         void validate ();
71
72         bool selected() const { return _selected; }
73         void set_selected(bool yn);
74
75         virtual void move_event(double dx, double dy) = 0;
76
77         uint32_t base_color();
78
79         void show_velocity();
80         void hide_velocity();
81
82         /** Channel changed for this specific event */
83         void on_channel_change(uint8_t channel);
84
85         /** Channel selection changed */
86         void on_channel_selection_change(uint16_t selection);
87
88         virtual void set_outline_color(uint32_t c) = 0;
89         virtual void set_fill_color(uint32_t c) = 0;
90
91         virtual void set_ignore_events(bool ignore) = 0;
92
93         virtual ArdourCanvas::Coord x0 () const = 0;
94         virtual ArdourCanvas::Coord y0 () const = 0;
95         virtual ArdourCanvas::Coord x1 () const = 0;
96         virtual ArdourCanvas::Coord y1 () const = 0;
97
98         float mouse_x_fraction() const { return _mouse_x_fraction; }
99         float mouse_y_fraction() const { return _mouse_y_fraction; }
100
101         const boost::shared_ptr<NoteType> note() const { return _note; }
102         MidiRegionView& region_view() const { return _region; }
103
104         static void set_colors ();
105
106         inline static uint32_t meter_style_fill_color(uint8_t vel, bool selected) {
107                 if (selected) {
108                         return _selected_mod_col;
109                 } else if (vel < 64) {
110                         return UINT_INTERPOLATE(_min_col, _mid_col, (vel / (double)63.0));
111                 } else {
112                         return UINT_INTERPOLATE(_mid_col, _max_col, ((vel - 64) / (double)63.0));
113                 }
114         }
115
116         /// calculate outline colors from fill colors of notes
117         inline static uint32_t calculate_outline(uint32_t color, bool selected=false) {
118                 if (selected) {
119                         return _selected_outline_col;
120                 } else {
121                         return UINT_INTERPOLATE(color, 0x000000ff, 0.5);
122                 }
123         }
124
125         /// hue circle divided into 16 equal-looking parts, courtesy Thorsten Wilms
126         static const uint32_t midi_channel_colors[16];
127
128         bool mouse_near_ends () const;
129         virtual bool big_enough_to_trim () const;
130
131 protected:
132         enum State { None, Pressed, Dragging };
133
134         MidiRegionView&                   _region;
135         ArdourCanvas::Item*               _item;
136         ArdourCanvas::Text*               _text;
137         State                             _state;
138         const boost::shared_ptr<NoteType> _note;
139         bool                              _with_events;
140         bool                              _own_note;
141         bool                              _selected;
142         bool                              _valid;
143         float                             _mouse_x_fraction;
144         float                             _mouse_y_fraction;
145
146         void set_mouse_fractions (GdkEvent*);
147
148 private:
149         bool event_handler (GdkEvent *);
150
151         static uint32_t _selected_mod_col;
152         static uint32_t _selected_outline_col;
153         static uint32_t _selected_col;
154         static uint32_t _min_col;
155         static uint32_t _mid_col;
156         static uint32_t _max_col;
157         static bool _color_init;
158 };
159
160 #endif /* __gtk_ardour_note_h__ */