add option to create new routes with strict-i/o enabled.
[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
22 #include "plugin_pin_dialog.h"
23 #include "gui_thread.h"
24 #include "ui_config.h"
25
26 #include "i18n.h"
27
28 using namespace ARDOUR;
29 using namespace PBD;
30 using namespace std;
31 using namespace Gtk;
32 using namespace Gtkmm2ext;
33
34
35 PluginPinDialog::PluginPinDialog (boost::shared_ptr<ARDOUR::PluginInsert> pi)
36         : ArdourWindow (string_compose (_("Pin Configuration: %1"), pi->name ()))
37         , _pi (pi)
38 {
39         assert (pi->owner ()); // Route
40
41         _pi->PluginIoReConfigure.connect (
42                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context()
43                         );
44
45         _pi->PluginMapChanged.connect (
46                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context()
47                         );
48
49         darea.signal_expose_event().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_expose_event));
50
51         // TODO min. width depending on # of pins.
52         darea.set_size_request(600, 200);
53
54         VBox* vbox = manage (new VBox);
55         vbox->pack_start (darea, true, true);
56         add (*vbox);
57 }
58
59 PluginPinDialog::~PluginPinDialog()
60 {
61 }
62
63 void
64 PluginPinDialog::plugin_reconfigured ()
65 {
66         darea.queue_draw ();
67 }
68
69 void
70 PluginPinDialog::draw_io_pins (cairo_t* cr, double y0, double width, uint32_t n_total, uint32_t n_midi, bool input)
71 {
72         double dir = input ? 1. : -1.;
73
74         for (uint32_t i = 0; i < n_total; ++i) {
75                 double x0 = rint ((i + 1) * width / (1. + n_total)) - .5;
76                 cairo_move_to (cr, x0, y0);
77                 cairo_rel_line_to (cr, -5.,  -5. * dir);
78                 cairo_rel_line_to (cr,  0., -25. * dir);
79                 cairo_rel_line_to (cr, 10.,   0.);
80                 cairo_rel_line_to (cr,  0.,  25. * dir);
81                 cairo_close_path  (cr);
82
83                 cairo_set_source_rgb (cr, 0, 0, 0);
84                 cairo_stroke_preserve (cr);
85
86                 if (i < n_midi) {
87                         cairo_set_source_rgb (cr, 1, 0, 0);
88                 } else {
89                         cairo_set_source_rgb (cr, 0, 1, 0);
90                 }
91                 cairo_fill (cr);
92         }
93 }
94
95 double
96 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
97 {
98         if (!midi) { i += n_midi; }
99         return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
100 }
101
102 void
103 PluginPinDialog::draw_plugin_pins (cairo_t* cr, double x0, double y0, double width, uint32_t n_total, uint32_t n_midi, bool input)
104 {
105         // see also ProcessorEntry::PortIcon::on_expose_event
106         const double dxy = rint (max (6., 8. * UIConfiguration::instance().get_ui_scale()));
107
108         for (uint32_t i = 0; i < n_total; ++i) {
109                 double x = rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
110                 cairo_rectangle (cr, x - dxy * .5, input ? y0 - dxy : y0, 1 + dxy, dxy);
111
112                 if (i < n_midi) {
113                         cairo_set_source_rgb (cr, 1, 0, 0);
114                 } else {
115                         cairo_set_source_rgb (cr, 0, 1, 0);
116                 }
117
118                 cairo_fill(cr);
119         }
120 }
121
122 void
123 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi)
124 {
125         cairo_move_to (cr, x0, y0);
126         cairo_curve_to (cr, x0, y0 + 10, x1, y1 - 10, x1, y1);
127         cairo_set_line_width  (cr, 3.0);
128         cairo_set_source_rgb (cr, 1, 0, 0);
129         if (midi) {
130                 cairo_set_source_rgb (cr, 1, 0, 0);
131         } else {
132                 cairo_set_source_rgb (cr, 0, 1, 0);
133         }
134         cairo_stroke (cr);
135 }
136
137 bool
138 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
139 {
140         Gtk::Allocation a = darea.get_allocation();
141         double const width = a.get_width();
142         double const height = a.get_height();
143
144         cairo_t* cr = gdk_cairo_create (darea.get_window()->gobj());
145
146         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
147         cairo_clip (cr);
148
149         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
150         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
151         cairo_rectangle (cr, 0, 0, width, height);
152         cairo_fill (cr);
153
154         ChanCount in, out; // actual configured i/o
155         _pi->configured_io (in, out);
156
157         ChanCount sinks = _pi->natural_input_streams ();
158         ChanCount sources = _pi->natural_output_streams ();
159         uint32_t plugins = _pi->get_count ();
160
161         /* layout sizes */
162         // i/o pins
163         double y_in = 40;
164         double y_out = height - 40;
165
166         // plugin box(es)
167         double yc   = rint (height * .5);
168         double bxh2 = 18;
169         double bxw  = rint ((width * .9) / ((plugins) + .2 * (plugins - 1)));
170         double bxw2 = rint (bxw * .5);
171
172         // i/o pins
173         const uint32_t pc_in = in.n_total();
174         const uint32_t pc_in_midi = in.n_midi();
175         const uint32_t pc_out = out.n_total();
176         const uint32_t pc_out_midi = out.n_midi();
177
178         cairo_set_line_width  (cr, 1.0);
179         draw_io_pins (cr, y_in, width, pc_in, pc_in_midi, true);
180         draw_io_pins (cr, y_out, width, pc_out, pc_out_midi, false);
181
182         // draw midi-bypass (behind)
183         if (sources.n_midi() == 0 && pc_in_midi > 0 && pc_out_midi > 0) {
184                 double x0 = rint (width / (1. + pc_in)) - .5;
185                 double x1 = rint (width / (1. + pc_out)) - .5;
186                 draw_connection (cr, x0, x1, y_in, y_out, true);
187         }
188
189         for (uint32_t i = 0; i < plugins; ++i) {
190                 double x0 = rint ((i + .5) * width / (double)(plugins)) - .5;
191
192                 draw_plugin_pins (cr, x0 - bxw2, yc - bxh2, bxw, sinks.n_total (), sinks.n_midi (), true);
193                 draw_plugin_pins (cr, x0 - bxw2, yc + bxh2, bxw, sources.n_total (), sources.n_midi (), false);
194
195                 cairo_set_source_rgb (cr, .3, .3, .3);
196                 rounded_rectangle (cr, x0 - bxw2, yc - bxh2, bxw, 2 * bxh2, 7);
197                 cairo_fill (cr);
198
199                 const ChanMapping::Mappings in_map =  _pi->input_map (i).mappings();
200                 const ChanMapping::Mappings out_map =  _pi->output_map (i).mappings();
201
202                 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
203                         bool is_midi = t->first == DataType::MIDI;
204                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
205                                 uint32_t pn = (*c).first; // pin
206                                 uint32_t pb = (*c).second;
207                                 double c_x0 = pin_x_pos (pb, 0, width, pc_in, pc_in_midi, is_midi);
208                                 double c_x1 = pin_x_pos (pn, x0 - bxw2, bxw, sinks.n_total (), sinks.n_midi (), is_midi);
209                                 draw_connection (cr, c_x0, c_x1, y_in, yc - bxh2, is_midi);
210                         }
211                 }
212
213                 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
214                         bool is_midi = t->first == DataType::MIDI;
215                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
216                                 uint32_t pn = (*c).first;  // pin
217                                 uint32_t pb = (*c).second;
218                                 double c_x0 = pin_x_pos (pn, x0 - bxw2, bxw, sources.n_total (), sources.n_midi (), is_midi);
219                                 double c_x1 = pin_x_pos (pb, 0, width, pc_out, pc_out_midi, is_midi);
220                                 draw_connection (cr, c_x0, c_x1, yc + bxh2, y_out, is_midi);
221                         }
222                 }
223         }
224
225         cairo_destroy (cr);
226         return true;
227 }