update mixer-strip routing display
[ardour.git] / gtk2_ardour / plugin_pin_dialog.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2011 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include "gtkmm2ext/utils.h"
21 #include "gtkmm2ext/rgb_macros.h"
22
23 #include "plugin_pin_dialog.h"
24 #include "gui_thread.h"
25 #include "ui_config.h"
26
27 #include "i18n.h"
28
29 using namespace ARDOUR;
30 using namespace PBD;
31 using namespace std;
32 using namespace Gtk;
33 using namespace Gtkmm2ext;
34
35 PluginPinDialog::PluginPinDialog (boost::shared_ptr<ARDOUR::PluginInsert> pi)
36         : ArdourWindow (string_compose (_("Pin Configuration: %1"), pi->name ()))
37         , _pi (pi)
38         , _pin_box_size (4)
39 {
40         assert (pi->owner ()); // Route
41
42         _pi->PluginIoReConfigure.connect (
43                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context()
44                         );
45
46         _pi->PluginMapChanged.connect (
47                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context()
48                         );
49
50         darea.signal_expose_event().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_expose_event));
51
52         // TODO min. width depending on # of pins.
53         darea.set_size_request(600, 200);
54
55         HBox* hbox = manage (new HBox);
56         hbox->pack_start (darea, true, true);
57
58         // TODO add info/settings table
59         // * show  _pi->strict_io() -- inherited from route
60         // * show  _pi->custom_cfg()
61         // Add/Remove instances
62         // Add/Remove output ports
63         // Reset Button  custom-config to "Automatic"
64
65         VBox* vbox = manage (new VBox);
66         vbox->pack_start (*hbox, true, true);
67         add (*vbox);
68 }
69
70 PluginPinDialog::~PluginPinDialog()
71 {
72 }
73
74 void
75 PluginPinDialog::plugin_reconfigured ()
76 {
77         darea.queue_draw ();
78 }
79
80 void
81 PluginPinDialog::set_color (cairo_t* cr, bool midi)
82 {
83         // see also gtk2_ardour/processor_box.cc
84         static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
85         static const uint32_t midi_port_color = 0x960909FF; //Red
86
87         if (midi) {
88                 cairo_set_source_rgb (cr,
89                                 UINT_RGBA_R_FLT(midi_port_color),
90                                 UINT_RGBA_G_FLT(midi_port_color),
91                                 UINT_RGBA_B_FLT(midi_port_color));
92         } else {
93                 cairo_set_source_rgb (cr,
94                                 UINT_RGBA_R_FLT(audio_port_color),
95                                 UINT_RGBA_G_FLT(audio_port_color),
96                                 UINT_RGBA_B_FLT(audio_port_color));
97         }
98 }
99
100 void
101 PluginPinDialog::draw_io_pins (cairo_t* cr, double y0, double width, uint32_t n_total, uint32_t n_midi, bool input)
102 {
103         double dir = input ? 1. : -1.;
104
105         for (uint32_t i = 0; i < n_total; ++i) {
106                 double x0 = rint ((i + 1) * width / (1. + n_total)) - .5;
107                 cairo_move_to (cr, x0, y0);
108                 cairo_rel_line_to (cr, -5.,  -5. * dir);
109                 cairo_rel_line_to (cr,  0., -25. * dir);
110                 cairo_rel_line_to (cr, 10.,   0.);
111                 cairo_rel_line_to (cr,  0.,  25. * dir);
112                 cairo_close_path  (cr);
113
114                 cairo_set_source_rgb (cr, 0, 0, 0);
115                 cairo_stroke_preserve (cr);
116
117                 set_color (cr, i < n_midi);
118                 cairo_fill (cr);
119         }
120 }
121
122 bool
123 PluginPinDialog::is_valid_port (uint32_t i, uint32_t n_total, uint32_t n_midi, bool midi)
124 {
125         if (!midi) { i += n_midi; }
126         if (i >= n_total) {
127                 return false;
128         }
129         return true;
130 }
131
132 double
133 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
134 {
135         if (!midi) { i += n_midi; }
136         return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
137 }
138
139 void
140 PluginPinDialog::draw_plugin_pins (cairo_t* cr, double x0, double y0, double width, uint32_t n_total, uint32_t n_midi, bool input)
141 {
142         // see also ProcessorEntry::PortIcon::on_expose_event
143         const double dxy = _pin_box_size;
144
145         for (uint32_t i = 0; i < n_total; ++i) {
146                 double x = rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
147                 cairo_rectangle (cr, x - dxy * .5, input ? y0 - dxy : y0, 1 + dxy, dxy);
148
149                 set_color (cr, i < n_midi);
150                 cairo_fill(cr);
151         }
152 }
153
154 void
155 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool dashed)
156 {
157         double bz = 2 * _pin_box_size;
158
159         cairo_move_to (cr, x0, y0);
160         cairo_curve_to (cr, x0, y0 + bz, x1, y1 - bz, x1, y1);
161         cairo_set_line_width  (cr, 3.0);
162         cairo_set_line_cap  (cr,  CAIRO_LINE_CAP_ROUND);
163         cairo_set_source_rgb (cr, 1, 0, 0);
164         if (dashed) {
165                 const double dashes[] = { 5, 7 };
166                 cairo_set_dash (cr, dashes, 2, 0);
167         }
168         set_color (cr, midi);
169         cairo_stroke (cr);
170         if (dashed) {
171                 cairo_set_dash (cr, 0, 0, 0);
172         }
173 }
174
175 bool
176 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
177 {
178         Gtk::Allocation a = darea.get_allocation();
179         double const width = a.get_width();
180         double const height = a.get_height();
181
182         _pin_box_size = rint (max (6., 8. * UIConfiguration::instance().get_ui_scale()));
183
184         cairo_t* cr = gdk_cairo_create (darea.get_window()->gobj());
185
186         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
187         cairo_clip (cr);
188
189         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
190         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
191         cairo_rectangle (cr, 0, 0, width, height);
192         cairo_fill (cr);
193
194         ChanCount in, out; // actual configured i/o
195         _pi->configured_io (in, out);
196
197         ChanCount sinks = _pi->natural_input_streams ();
198         ChanCount sources = _pi->natural_output_streams ();
199         uint32_t plugins = _pi->get_count ();
200
201         /* layout sizes */
202         // i/o pins
203         double y_in = 40;
204         double y_out = height - 40;
205
206         // plugin box(es)
207         double yc   = rint (height * .5);
208         double bxh2 = 18;
209         double bxw  = rint ((width * .9) / ((plugins) + .2 * (plugins - 1)));
210         double bxw2 = rint (bxw * .5);
211
212         // i/o pins
213         const uint32_t pc_in = in.n_total();
214         const uint32_t pc_in_midi = in.n_midi();
215         const uint32_t pc_out = out.n_total();
216         const uint32_t pc_out_midi = out.n_midi();
217
218         cairo_set_line_width  (cr, 1.0);
219         draw_io_pins (cr, y_in, width, pc_in, pc_in_midi, true);
220         draw_io_pins (cr, y_out, width, pc_out, pc_out_midi, false);
221
222         // draw midi-bypass (behind)
223         if (_pi->has_midi_bypass ()) {
224                 double x0 = rint (width / (1. + pc_in)) - .5;
225                 double x1 = rint (width / (1. + pc_out)) - .5;
226                 draw_connection (cr, x0, x1, y_in, y_out, true, true);
227         }
228
229         for (uint32_t i = 0; i < plugins; ++i) {
230                 double x0 = rint ((i + .5) * width / (double)(plugins)) - .5;
231
232                 draw_plugin_pins (cr, x0 - bxw2, yc - bxh2, bxw, sinks.n_total (), sinks.n_midi (), true);
233                 draw_plugin_pins (cr, x0 - bxw2, yc + bxh2, bxw, sources.n_total (), sources.n_midi (), false);
234
235                 cairo_set_source_rgb (cr, .3, .3, .3);
236                 rounded_rectangle (cr, x0 - bxw2, yc - bxh2, bxw, 2 * bxh2, 7);
237                 cairo_fill (cr);
238
239                 const ChanMapping::Mappings in_map =  _pi->input_map (i).mappings();
240                 const ChanMapping::Mappings out_map =  _pi->output_map (i).mappings();
241
242                 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
243                         bool is_midi = t->first == DataType::MIDI;
244                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
245                                 uint32_t pn = (*c).first; // pin
246                                 uint32_t pb = (*c).second;
247                                 if (!is_valid_port (pb, pc_in, pc_in_midi, is_midi)) {
248                                         continue;
249                                 }
250                                 double c_x0 = pin_x_pos (pb, 0, width, pc_in, pc_in_midi, is_midi);
251                                 double c_x1 = pin_x_pos (pn, x0 - bxw2, bxw, sinks.n_total (), sinks.n_midi (), is_midi);
252                                 draw_connection (cr, c_x0, c_x1, y_in, yc - bxh2 - _pin_box_size, is_midi);
253                         }
254                 }
255
256                 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
257                         bool is_midi = t->first == DataType::MIDI;
258                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
259                                 uint32_t pn = (*c).first;  // pin
260                                 uint32_t pb = (*c).second;
261                                 if (!is_valid_port (pb, pc_out, pc_out_midi, is_midi)) {
262                                         continue;
263                                 }
264                                 double c_x0 = pin_x_pos (pn, x0 - bxw2, bxw, sources.n_total (), sources.n_midi (), is_midi);
265                                 double c_x1 = pin_x_pos (pb, 0, width, pc_out, pc_out_midi, is_midi);
266                                 draw_connection (cr, c_x0, c_x1, yc + bxh2 + _pin_box_size, y_out, is_midi);
267                         }
268                 }
269         }
270
271         cairo_destroy (cr);
272         return true;
273 }