More logical arrangement of port matrix inputs and outputs, hopefully;
[ardour.git] / gtk2_ardour / port_matrix_column_labels.cc
1 /*
2     Copyright (C) 2002-2009 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 <iostream>
21 #include "ardour/bundle.h"
22 #include "port_matrix_column_labels.h"
23 #include "port_matrix.h"
24
25 PortMatrixColumnLabels::PortMatrixColumnLabels (PortMatrixBody* b, Location l)
26         : PortMatrixComponent (b), _location (l)
27 {
28
29 }
30
31 void
32 PortMatrixColumnLabels::compute_dimensions ()
33 {
34         GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
35         gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
36         cairo_t* cr = gdk_cairo_create (pm);
37
38         /* width of the longest bundle name */
39         _longest_bundle_name = 0;
40         /* width of the longest channel name */
41         _longest_channel_name = 0;
42         /* height of highest bit of text */
43         _highest_text = 0;
44         
45         for (std::vector<boost::shared_ptr<ARDOUR::Bundle> >::const_iterator i = _body->column_bundles().begin (); i != _body->column_bundles().end(); ++i) {
46
47                 cairo_text_extents_t ext;
48                 cairo_text_extents (cr, (*i)->name().c_str(), &ext);
49                 if (ext.width > _longest_bundle_name) {
50                         _longest_bundle_name = ext.width;
51                 }
52                 if (ext.height > _highest_text) {
53                         _highest_text = ext.height;
54                 }
55
56                 for (uint32_t j = 0; j < (*i)->nchannels (); ++j) {
57                         
58                         cairo_text_extents (
59                                 cr,
60                                 (*i)->channel_name (j).c_str(),
61                                 &ext
62                                 );
63                         
64                         if (ext.width > _longest_channel_name) {
65                                 _longest_channel_name = ext.width;
66                         }
67                         if (ext.height > _highest_text) {
68                                 _highest_text = ext.height;
69                         }
70                 }
71         }
72
73         cairo_destroy (cr);
74         gdk_pixmap_unref (pm);
75
76         /* width and height of the whole thing */
77         
78         _width = 0;
79         for (std::vector<boost::shared_ptr<ARDOUR::Bundle> >::const_iterator i = _body->column_bundles().begin (); i != _body->column_bundles().end(); ++i) {
80                 _width += (*i)->nchannels() * column_width();
81         }
82
83         _height =
84                 (_longest_bundle_name + _longest_channel_name + 4 * name_pad()) * sin (angle())
85                 + _highest_text * cos (angle());
86
87         _width += _height / tan (angle ());
88 }
89
90 double
91 PortMatrixColumnLabels::basic_text_x_pos (int c) const
92 {
93         return column_width() / 2 +
94                 _highest_text / (2 * sin (angle ()));
95 }
96
97 void
98 PortMatrixColumnLabels::render (cairo_t* cr)
99 {
100         /* BACKGROUND */
101
102         set_source_rgb (cr, background_colour());
103         cairo_rectangle (cr, 0, 0, _width, _height);
104         cairo_fill (cr);
105
106         /* BUNDLE PARALLELOGRAM-TYPE-THING AND NAME */
107
108         double x = 0;
109         for (std::vector<boost::shared_ptr<ARDOUR::Bundle> >::const_iterator i = _body->column_bundles().begin (); i != _body->column_bundles().end(); ++i) {
110
111                 Gdk::Color colour = get_a_bundle_colour (i - _body->column_bundles().begin ());
112                 set_source_rgb (cr, colour);
113
114                 double const w = (*i)->nchannels() * column_width();
115
116                 double x_ = x;
117                 double y_ = _height;
118                 
119                 cairo_move_to (cr, x_, y_);
120                 x_ += w;
121                 cairo_line_to (cr, x_, y_);
122                 x_ += _height / tan (angle ());
123                 y_ -= _height;
124                 cairo_line_to (cr, x_, y_);
125                 x_ -= w;
126                 cairo_line_to (cr, x_, y_);
127                 cairo_line_to (cr, x, _height);
128                 cairo_fill_preserve (cr);
129                 set_source_rgb (cr, background_colour());
130                 cairo_set_line_width (cr, label_border_width());
131                 cairo_stroke (cr);
132
133                 set_source_rgb (cr, text_colour());
134
135
136                 if (_location == TOP) {
137                         
138                         double const rl = 3 * name_pad() + _longest_channel_name;
139                         cairo_move_to (
140                                 cr,
141                                 x + basic_text_x_pos (0) + rl * cos (angle()),
142                                 _height - rl * sin (angle())
143                                 );
144                         
145                 } else if (_location == BOTTOM) {
146
147                         cairo_move_to (
148                                 cr,
149                                 x + basic_text_x_pos (0),
150                                 _height - name_pad() * sin (angle())
151                                 );
152                 }
153                         
154                 cairo_save (cr);
155                 cairo_rotate (cr, -angle());
156                 cairo_show_text (cr, (*i)->name().c_str());
157                 cairo_restore (cr);
158                 
159                 x += (*i)->nchannels () * column_width();
160         }
161         
162
163         /* PORT NAMES */
164
165         x = 0;
166         for (std::vector<boost::shared_ptr<ARDOUR::Bundle> >::const_iterator i = _body->column_bundles().begin(); i != _body->column_bundles().end(); ++i) {
167                 
168                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
169
170                         double const lc = _longest_channel_name + (2 * name_pad());
171                         double const lb = _longest_bundle_name + (2 * name_pad());
172                         double const w = column_width();
173
174                         if (_location == BOTTOM) {
175
176                                 double x_ = x + _height / tan (angle()) + w;
177                                 double const ix = x_;
178                                 double y_ = 0;
179                                 cairo_move_to (cr, x_, y_);
180                                 x_ -= w;
181                                 cairo_line_to (cr, x_, y_);
182                                 x_ -= lb * cos (angle());
183                                 y_ += lb * sin (angle());
184                                 cairo_line_to (cr, x_, y_);
185                                 x_ += w * pow (sin (angle()), 2);
186                                 y_ += w * sin (angle()) * cos (angle());
187                                 cairo_line_to (cr, x_, y_);
188                                 cairo_line_to (cr, ix, 0);
189
190                         } else if (_location == TOP) {
191
192                                 double x_ = x;
193                                 double y_ = _height;
194                                 cairo_move_to (cr, x_, y_);
195                                 x_ += w;
196                                 cairo_line_to (cr, x_, y_);
197                                 x_ += lc * cos (angle());
198                                 y_ -= lc * sin (angle());
199                                 cairo_line_to (cr, x_, y_);
200                                 x_ -= column_width() * pow (sin (angle()), 2);
201                                 y_ -= column_width() * sin (angle()) * cos (angle());
202                                 cairo_line_to (cr, x_, y_);
203                                 cairo_line_to (cr, x, _height);
204
205                         }
206
207                         Gdk::Color colour = get_a_bundle_colour (i - _body->column_bundles().begin());
208                         set_source_rgb (cr, colour);
209                         cairo_fill_preserve (cr);
210                         set_source_rgb (cr, background_colour());
211                         cairo_set_line_width (cr, label_border_width());
212                         cairo_stroke (cr);
213
214                         set_source_rgb (cr, text_colour());
215
216                         if (_location == TOP) {
217                                 cairo_move_to (cr, x + basic_text_x_pos(j), _height - name_pad() * sin (angle()));
218                         } else if (_location == BOTTOM) {
219                                 double const rl = 3 * name_pad() + _longest_bundle_name;
220                                 cairo_move_to (cr, x + basic_text_x_pos(j) + rl * cos (angle ()), _height - rl * sin (angle()));
221                         }
222                         
223                         cairo_save (cr);
224                         cairo_rotate (cr, -angle());
225
226                         cairo_show_text (
227                                 cr,
228                                 (*i)->channel_name(j).c_str()
229                                 );
230                         
231                         cairo_restore (cr);
232
233                         x += column_width();
234                 }
235         }
236 }
237