SystemExec Lua bindings (vfork, fire+forget)
[ardour.git] / gtk2_ardour / plugin_display.cc
1 /*
2     Copyright (C) 2017 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 <gtkmm/container.h>
21
22 #include "gtkmm2ext/colors.h"
23 #include "gtkmm2ext/gtk_ui.h"
24 #include "gtkmm2ext/gui_thread.h"
25 #include "gtkmm2ext/utils.h"
26
27 #include "ui_config.h"
28
29 #include "plugin_display.h"
30
31
32
33 PluginDisplay::PluginDisplay (boost::shared_ptr<ARDOUR::Plugin> p, uint32_t max_height)
34         : _plug (p)
35         , _surf (0)
36         , _max_height (max_height)
37         , _cur_height (1)
38         , _scroll (false)
39 {
40         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
41         _plug->DropReferences.connect (_death_connection, invalidator (*this), boost::bind (&PluginDisplay::plugin_going_away, this), gui_context());
42         _plug->QueueDraw.connect (_qdraw_connection, invalidator (*this),
43                         boost::bind (&Gtk::Widget::queue_draw, this), gui_context ());
44 }
45
46 PluginDisplay::~PluginDisplay ()
47 {
48         if (_surf) {
49                 cairo_surface_destroy (_surf);
50         }
51 }
52
53 bool
54 PluginDisplay::on_button_press_event (GdkEventButton *ev)
55 {
56         return false;
57 }
58
59 bool
60 PluginDisplay::on_button_release_event (GdkEventButton *ev)
61 {
62         return false;
63 }
64
65 void
66 PluginDisplay::on_size_request (Gtk::Requisition* req)
67 {
68         req->width = 300;
69         req->height = _cur_height;
70 }
71
72
73 void
74 PluginDisplay::update_height_alloc (uint32_t height)
75 {
76         uint32_t shm = std::min (_max_height, height);
77
78         Gtk::Container* pr = get_parent();
79         for (uint32_t i = 0; i < 4 && pr; ++i) {
80                 // VBox, EventBox, ViewPort, ScrolledWindow
81                 pr = pr->get_parent();
82         }
83
84         if (shm != _cur_height) {
85                 if (_cur_height < shm) {
86                         queue_resize ();
87                 }
88                 _cur_height = shm;
89         }
90 }
91
92 uint32_t
93 PluginDisplay::render_inline (cairo_t* cr, uint32_t width)
94 {
95         ARDOUR::Plugin::Display_Image_Surface* dis = _plug->render_inline_display (width, _max_height);
96         if (!dis) {
97                 return 0;
98         }
99
100         /* allocate a local image-surface,
101          * We cannot re-use the data via cairo_image_surface_create_for_data(),
102          * since pixman keeps a reference to it.
103          * we'd need to hand over the data and ha cairo_surface_destroy to free it.
104          * it might be possible to work around via cairo_surface_set_user_data().
105          */
106         if (!_surf
107                         || dis->width !=  cairo_image_surface_get_width (_surf)
108                         || dis->height !=  cairo_image_surface_get_height (_surf)
109                  ) {
110                 if (_surf) {
111                         cairo_surface_destroy (_surf);
112                 }
113                 _surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, dis->width, dis->height);
114         }
115
116         if (cairo_image_surface_get_stride (_surf) == dis->stride) {
117                 memcpy (cairo_image_surface_get_data (_surf), dis->data, dis->stride * dis->height);
118         } else {
119                 unsigned char *src = dis->data;
120                 unsigned char *dst = cairo_image_surface_get_data (_surf);
121                 const int dst_stride =  cairo_image_surface_get_stride (_surf);
122                 for (int y = 0; y < dis->height; ++y) {
123                         memcpy (dst, src, dis->width * 4 /*ARGB32*/);
124                         src += dis->stride;
125                         dst += dst_stride;
126                 }
127         }
128
129         cairo_surface_flush(_surf);
130         cairo_surface_mark_dirty(_surf);
131         const double xc = floor ((width - dis->width) * .5);
132         cairo_set_source_surface(cr, _surf, xc, 0);
133         cairo_paint (cr);
134
135         return dis->height;
136 }
137
138 bool
139 PluginDisplay::on_expose_event (GdkEventExpose* ev)
140 {
141         Gtk::Allocation a = get_allocation();
142         double const width = a.get_width();
143         double const height = a.get_height();
144
145         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
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 (Gtk::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         cairo_save (cr);
155         cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
156         display_frame(cr, width, height);
157         cairo_clip (cr);
158         cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
159
160         uint32_t ht = render_inline (cr, width);
161         cairo_restore (cr);
162
163         if (ht == 0) {
164                 hide ();
165                 if (_cur_height != 1) {
166                         _cur_height = 1;
167                         queue_resize ();
168                 }
169                 cairo_destroy (cr);
170                 return true;
171         } else {
172                 update_height_alloc (ht);
173         }
174
175         bool failed = false;
176         std::string name = get_name();
177         Gtkmm2ext::Color fill_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
178
179         display_frame(cr, width, height);
180         cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
181         cairo_set_line_width(cr, 1.0);
182         if (failed) {
183                 cairo_set_source_rgba (cr, .75, .75, .75, 1.0);
184         } else {
185                 Gtkmm2ext::set_source_rgb_a (cr, fill_color, 1.0);
186         }
187         cairo_stroke (cr);
188
189         cairo_destroy(cr);
190         return true;
191 }
192
193 void
194 PluginDisplay::display_frame (cairo_t* cr, double w, double h)
195 {
196         cairo_rectangle (cr, 0.0, 0.0, w, h);
197 }