* first working version of editing MIDI channels of individual notes, see: http:...
[ardour.git] / gtk2_ardour / tempo_lines.cc
1 /*
2     Copyright (C) 2002-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <libgnomecanvasmm/canvas.h>
21 #include <libgnomecanvasmm/group.h>
22 #include "tempo_lines.h"
23 #include "ardour_ui.h"
24
25
26 ArdourCanvas::SimpleLine *
27 TempoLines::get_line ()
28 {
29         ArdourCanvas::SimpleLine *line;
30
31         if (_free_lines.empty()) {
32                 line = new ArdourCanvas::SimpleLine (*_group);
33                 _used_lines.push_back (line);
34         } else {
35                 line = _free_lines.front();
36                 _free_lines.erase (_free_lines.begin());
37                 _used_lines.push_back (line);
38         }
39
40         return line;
41 }
42
43
44 void
45 TempoLines::hide ()
46 {
47         for (Lines::iterator i = _used_lines.begin(); i != _used_lines.end(); ++i) {
48         (*i)->hide();
49                 _free_lines.push_back (*i);
50         }
51         _used_lines.clear ();
52 }
53
54
55 void
56 TempoLines::draw (ARDOUR::TempoMap::BBTPointList& points, double frames_per_unit)
57 {
58         ARDOUR::TempoMap::BBTPointList::iterator i;
59         ArdourCanvas::SimpleLine *line;
60         gdouble xpos;
61         double who_cares;
62         double x1, x2, y1, y2, beat_density;
63
64         uint32_t beats = 0;
65         uint32_t bars = 0;
66         uint32_t color;
67
68         _canvas.get_scroll_region (x1, y1, x2, who_cares);
69         _canvas.root()->get_bounds(who_cares, who_cares, who_cares, y2);
70
71         // FIXME use canvas height
72         //y2 = TimeAxisView::hLargest*5000; // five thousand largest tracks should be enough.. :)
73         //y2 = 500000; // five thousand largest tracks should be enough.. :)
74
75         /* get the first bar spacing */
76
77         i = points.end();
78         i--;
79         bars = (*i).bar - (*points.begin()).bar;
80         beats = points.size() - bars;
81
82         beat_density =  (beats * 10.0f) / _canvas.get_width ();
83
84         if (beat_density > 4.0f) {
85                 /* if the lines are too close together, they become useless
86                  */
87                 return;
88         }
89         
90         for (i = points.begin(); i != points.end(); ++i) {
91
92                 switch ((*i).type) {
93                 case ARDOUR::TempoMap::Bar:
94                         break;
95
96                 case ARDOUR::TempoMap::Beat:
97                         
98                         if ((*i).beat == 1) {
99                                 color = ARDOUR_UI::config()->canvasvar_MeasureLineBar.get();
100                         } else {
101                                 color = ARDOUR_UI::config()->canvasvar_MeasureLineBeat.get();
102
103                                 if (beat_density > 2.0) {
104                                         /* only draw beat lines if the gaps between beats are large.
105                                         */
106                                         break;
107                                 }
108                         }
109
110                         xpos = rint((*i).frame / (double)frames_per_unit);
111                         line = get_line ();
112                         line->property_x1() = xpos;
113                         line->property_x2() = xpos;
114                         line->property_y2() = y2;
115                         line->property_color_rgba() = color;
116                         //line->raise_to_top();
117                         line->show();   
118                         break;
119                 }
120         }
121 }
122