Fix editor sizing issue introduced in 4dc65e66
[ardour.git] / gtk2_ardour / tempo_curve.cc
1 #include <sigc++/bind.h>
2 #include "ardour/tempo.h"
3
4 #include "canvas/rectangle.h"
5 #include "canvas/container.h"
6 #include "canvas/curve.h"
7 #include "canvas/canvas.h"
8 #include "canvas/debug.h"
9
10 #include "ui_config.h"
11
12 #include "tempo_curve.h"
13 #include "public_editor.h"
14 #include "utils.h"
15 #include "rgb_macros.h"
16
17 #include <gtkmm2ext/utils.h>
18
19 #include "pbd/i18n.h"
20
21 PBD::Signal1<void,TempoCurve*> TempoCurve::CatchDeletion;
22
23 static double curve_height = 13.0;
24
25 void TempoCurve::setup_sizes(const double timebar_height)
26 {
27         curve_height = floor (timebar_height) - 2.5;
28 }
29 /* ignores Tempo note type - only note_types_per_minute is potentially curved */
30 TempoCurve::TempoCurve (PublicEditor& ed, ArdourCanvas::Container& parent, guint32 rgba, ARDOUR::TempoSection& temp, samplepos_t sample, bool handle_events)
31
32         : editor (ed)
33         , _parent (&parent)
34         , _curve (0)
35         , _shown (false)
36         , _color (rgba)
37         , _min_tempo (temp.note_types_per_minute())
38         , _max_tempo (temp.note_types_per_minute())
39         , _tempo (temp)
40         , _start_text (0)
41         , _end_text (0)
42 {
43         sample_position = sample;
44         unit_position = editor.sample_to_pixel (sample);
45
46         group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple (unit_position, 1));
47 #ifdef CANVAS_DEBUG
48         group->name = string_compose ("TempoCurve::group for %1", _tempo.note_types_per_minute());
49 #endif
50
51         _curve = new ArdourCanvas::FramedCurve (group);
52 #ifdef CANVAS_DEBUG
53         _curve->name = string_compose ("TempoCurve::curve for %1", _tempo.note_types_per_minute());
54 #endif
55         _curve->set_points_per_segment (3);
56         points = new ArdourCanvas::Points ();
57         _curve->set (*points);
58
59         _start_text = new ArdourCanvas::Text (group);
60         _end_text = new ArdourCanvas::Text (group);
61         _start_text->set_font_description (ARDOUR_UI_UTILS::get_font_for_style (N_("MarkerText")));
62         _end_text->set_font_description (ARDOUR_UI_UTILS::get_font_for_style (N_("MarkerText")));
63         _start_text->set_color (RGBA_TO_UINT (255,255,255,255));
64         _end_text->set_color (RGBA_TO_UINT (255,255,255,255));
65         char buf[10];
66         snprintf (buf, sizeof (buf), "%.3f/%.0f", _tempo.note_types_per_minute(), _tempo.note_type());
67         _start_text->set (buf);
68         snprintf (buf, sizeof (buf), "%.3f", _tempo.end_note_types_per_minute());
69         _end_text->set (buf);
70
71         set_color_rgba (rgba);
72
73         editor.ZoomChanged.connect (sigc::mem_fun (*this, &TempoCurve::reposition));
74
75         /* events will be handled by both the group and the mark itself, so
76          * make sure they can both be used to lookup this object.
77          */
78
79         _curve->set_data ("tempo curve", this);
80
81         if (handle_events) {
82                 //group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
83         }
84
85         group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), _curve, this));
86
87 }
88
89 TempoCurve::~TempoCurve ()
90 {
91         CatchDeletion (this); /* EMIT SIGNAL */
92
93         /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
94         delete group;
95 }
96
97 void TempoCurve::reparent(ArdourCanvas::Container & parent)
98 {
99         group->reparent (&parent);
100         _parent = &parent;
101 }
102
103 void
104 TempoCurve::canvas_height_set (double h)
105 {
106         _canvas_height = h;
107 }
108
109 ArdourCanvas::Item&
110 TempoCurve::the_item() const
111 {
112         return *group;
113 }
114
115 void
116 TempoCurve::set_position (samplepos_t sample, samplepos_t end_sample)
117 {
118         unit_position = editor.sample_to_pixel (sample);
119         group->set_x_position (unit_position);
120         sample_position = sample;
121         _end_sample = end_sample;
122
123         points->clear();
124         points = new ArdourCanvas::Points ();
125
126         points->push_back (ArdourCanvas::Duple (0.0, curve_height));
127
128         if (sample >= end_sample) {
129                 /* shouldn't happen but ..*/
130                 const double tempo_at = _tempo.note_types_per_minute();
131                 const double y_pos =  (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
132
133                 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
134                 points->push_back (ArdourCanvas::Duple (1.0, y_pos));
135
136         } else if (_tempo.type() == ARDOUR::TempoSection::Constant || _tempo.c() == 0.0) {
137                 const double tempo_at = _tempo.note_types_per_minute();
138                 const double y_pos =  (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
139
140                 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
141                 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample), y_pos));
142         } else {
143
144                 const samplepos_t sample_step = max ((end_sample - sample) / 5, (samplepos_t) 1);
145                 samplepos_t current_sample = sample;
146
147                 while (current_sample < end_sample) {
148                         const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_sample (current_sample)).note_types_per_minute();
149                         const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
150
151                         points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (current_sample - sample), min (y_pos, curve_height)));
152
153                         current_sample += sample_step;
154                 }
155
156                 const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_sample (end_sample)).note_types_per_minute();
157                 const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
158
159                 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample), min (y_pos, curve_height)));
160         }
161
162         _curve->set (*points);
163
164         char buf[10];
165         snprintf (buf, sizeof (buf), "%.3f/%.0f", _tempo.note_types_per_minute(), _tempo.note_type());
166         _start_text->set (buf);
167         snprintf (buf, sizeof (buf), "%.3f", _tempo.end_note_types_per_minute());
168         _end_text->set (buf);
169
170         _start_text->set_position (ArdourCanvas::Duple (10, .5 ));
171         _end_text->set_position (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample) - _end_text->text_width() - 10, .5 ));
172
173         if (_end_text->text_width() + _start_text->text_width() + 20 > editor.sample_to_pixel (end_sample - sample)) {
174                 _start_text->hide();
175                 _end_text->hide();
176         } else {
177                 _start_text->show();
178                 _end_text->show();
179         }
180 }
181
182 void
183 TempoCurve::reposition ()
184 {
185         set_position (sample_position, _end_sample);
186 }
187
188 void
189 TempoCurve::show ()
190 {
191         _shown = true;
192
193         group->show ();
194 }
195
196 void
197 TempoCurve::hide ()
198 {
199         _shown = false;
200
201         group->hide ();
202 }
203
204 void
205 TempoCurve::set_color_rgba (uint32_t c)
206 {
207         _color = c;
208         _curve->set_fill_color (UIConfiguration::instance().color_mod (_color, "selection rect"));
209         _curve->set_outline_color (_color);
210
211 }