From 37b03e353a0513a07924c7e860a33e5ae233b57a Mon Sep 17 00:00:00 2001 From: Johannes Mueller Date: Thu, 20 Jul 2017 23:28:52 +0200 Subject: Put plugin inline display into a freely available class ... ... and let ProcessorBox::PluginInlineDisplay inherit from it. --- gtk2_ardour/plugin_display.cc | 187 ++++++++++++++++++++++++++++++++++++++++++ gtk2_ardour/plugin_display.h | 55 +++++++++++++ gtk2_ardour/processor_box.cc | 134 ++---------------------------- gtk2_ardour/processor_box.h | 23 ++---- gtk2_ardour/wscript | 1 + 5 files changed, 256 insertions(+), 144 deletions(-) create mode 100644 gtk2_ardour/plugin_display.cc create mode 100644 gtk2_ardour/plugin_display.h diff --git a/gtk2_ardour/plugin_display.cc b/gtk2_ardour/plugin_display.cc new file mode 100644 index 0000000000..73960b1f6a --- /dev/null +++ b/gtk2_ardour/plugin_display.cc @@ -0,0 +1,187 @@ +/* + Copyright (C) 2017 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include + +#include "gtkmm2ext/colors.h" +#include "gtkmm2ext/gtk_ui.h" +#include "gtkmm2ext/gui_thread.h" +#include "gtkmm2ext/utils.h" + +#include "ui_config.h" + +#include "plugin_display.h" + + + +PluginDisplay::PluginDisplay (boost::shared_ptr p, uint32_t max_height) + : _plug (p) + , _surf (0) + , _max_height (max_height) + , _cur_height (1) + , _scroll (false) +{ + add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK); + _plug->DropReferences.connect (_death_connection, invalidator (*this), boost::bind (&PluginDisplay::plugin_going_away, this), gui_context()); + _plug->QueueDraw.connect (_qdraw_connection, invalidator (*this), + boost::bind (&Gtk::Widget::queue_draw, this), gui_context ()); +} + +PluginDisplay::~PluginDisplay () +{ + if (_surf) { + cairo_surface_destroy (_surf); + } +} + +bool +PluginDisplay::on_button_press_event (GdkEventButton *ev) +{ + return false; +} + +bool +PluginDisplay::on_button_release_event (GdkEventButton *ev) +{ + return false; +} + +void +PluginDisplay::on_size_request (Gtk::Requisition* req) +{ + req->width = 56; + req->height = _cur_height; +} + + +void +PluginDisplay::update_height_alloc (uint32_t height) +{ + uint32_t shm = std::min (_max_height, height); + + Gtk::Container* pr = get_parent(); + for (uint32_t i = 0; i < 4 && pr; ++i) { + // VBox, EventBox, ViewPort, ScrolledWindow + pr = pr->get_parent(); + } + + if (shm != _cur_height) { + if (_cur_height < shm) { + queue_resize (); + } + _cur_height = shm; + } +} + +uint32_t +PluginDisplay::render_inline (cairo_t* cr, uint32_t width) +{ + ARDOUR::Plugin::Display_Image_Surface* dis = _plug->render_inline_display (width, _max_height); + if (!dis) { + return 0; + } + + /* allocate a local image-surface, + * We cannot re-use the data via cairo_image_surface_create_for_data(), + * since pixman keeps a reference to it. + * we'd need to hand over the data and ha cairo_surface_destroy to free it. + * it might be possible to work around via cairo_surface_set_user_data(). + */ + if (!_surf + || dis->width != cairo_image_surface_get_width (_surf) + || dis->height != cairo_image_surface_get_height (_surf) + ) { + if (_surf) { + cairo_surface_destroy (_surf); + } + _surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, dis->width, dis->height); + } + + if (cairo_image_surface_get_stride (_surf) == dis->stride) { + memcpy (cairo_image_surface_get_data (_surf), dis->data, dis->stride * dis->height); + } else { + unsigned char *src = dis->data; + unsigned char *dst = cairo_image_surface_get_data (_surf); + const int dst_stride = cairo_image_surface_get_stride (_surf); + for (int y = 0; y < dis->height; ++y) { + memcpy (dst, src, dis->width * 4 /*ARGB32*/); + src += dis->stride; + dst += dst_stride; + } + } + + cairo_surface_flush(_surf); + cairo_surface_mark_dirty(_surf); + const double xc = floor ((width - dis->width) * .5); + cairo_set_source_surface(cr, _surf, xc, 0); + cairo_paint (cr); + + return dis->height; +} + +bool +PluginDisplay::on_expose_event (GdkEventExpose* ev) +{ + Gtk::Allocation a = get_allocation(); + double const width = a.get_width(); + double const height = a.get_height(); + + cairo_t* cr = gdk_cairo_create (get_window()->gobj()); + cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height); + cairo_clip (cr); + + Gdk::Color const bg = get_style()->get_bg (Gtk::STATE_NORMAL); + cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ()); + cairo_rectangle (cr, 0, 0, width, height); + cairo_fill (cr); + + cairo_save (cr); + cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); + Gtkmm2ext::rounded_rectangle (cr, .5, -1.5, width - 1, height + 1, 7); + cairo_clip (cr); + cairo_set_operator (cr, CAIRO_OPERATOR_OVER); + + uint32_t ht = render_inline (cr, width); + cairo_restore (cr); + + if (ht == 0) { + hide (); + if (_cur_height != 1) { + _cur_height = 1; + queue_resize (); + } + cairo_destroy (cr); + return true; + } else { + update_height_alloc (ht); + } + + bool failed = false; + std::string name = get_name(); + Gtkmm2ext::Color fill_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed); + + Gtkmm2ext::rounded_rectangle (cr, .5, -1.5, width - 1, height + 1, 7); + cairo_set_operator (cr, CAIRO_OPERATOR_OVER); + cairo_set_line_width(cr, 1.0); + Gtkmm2ext::set_source_rgb_a (cr, fill_color, 1.0); + cairo_stroke (cr); + + cairo_destroy(cr); + return true; +} diff --git a/gtk2_ardour/plugin_display.h b/gtk2_ardour/plugin_display.h new file mode 100644 index 0000000000..4828ee7739 --- /dev/null +++ b/gtk2_ardour/plugin_display.h @@ -0,0 +1,55 @@ +/* + Copyright (C) 2017 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef __ardour_plugin_display__ +#define __ardour_plugin_display__ + +#include + +#include "ardour/plugin.h" + +class PluginDisplay : public Gtk::DrawingArea +{ +public: + PluginDisplay(boost::shared_ptr, uint32_t max_height = 80); + virtual ~PluginDisplay(); + +protected: + bool on_expose_event (GdkEventExpose *); + void on_size_request (Gtk::Requisition* req); + bool on_button_press_event (GdkEventButton *ev); + bool on_button_release_event (GdkEventButton *ev); + + void plugin_going_away () { + _qdraw_connection.disconnect (); + } + + void update_height_alloc (uint32_t inline_height); + virtual uint32_t render_inline (cairo_t *, uint32_t width); + + boost::shared_ptr _plug; + PBD::ScopedConnection _qdraw_connection; + PBD::ScopedConnection _death_connection; + cairo_surface_t* _surf; + uint32_t _max_height; + uint32_t _cur_height; + bool _scroll; +}; + +#endif diff --git a/gtk2_ardour/processor_box.cc b/gtk2_ardour/processor_box.cc index 3e39e639f2..476d6c0995 100644 --- a/gtk2_ardour/processor_box.cc +++ b/gtk2_ardour/processor_box.cc @@ -184,7 +184,7 @@ ProcessorEntry::ProcessorEntry (ProcessorBox* parent, boost::shared_ptr pi = boost::dynamic_pointer_cast (_processor); if (pi && pi->plugin() && pi->plugin()->has_inline_display()) { if (pi->plugin()->get_info()->type != ARDOUR::Lua) { - _plugin_display = new PluginDisplay (*this, pi->plugin(), + _plugin_display = new PluginInlineDisplay (*this, pi->plugin(), std::max (60.f, rintf(112.f * UIConfiguration::instance().get_ui_scale()))); } else { assert (boost::dynamic_pointer_cast(pi->plugin())); @@ -1563,20 +1563,11 @@ ProcessorEntry::RoutingIcon::expose_output_map (cairo_t* cr, const double width, } } -ProcessorEntry::PluginDisplay::PluginDisplay (ProcessorEntry& e, boost::shared_ptr p, uint32_t max_height) - : _entry (e) - , _plug (p) - , _surf (0) - , _max_height (max_height) - , _cur_height (1) +ProcessorEntry::PluginInlineDisplay::PluginInlineDisplay (ProcessorEntry& e, boost::shared_ptr p, uint32_t max_height) + : PluginDisplay (p, max_height) + , _entry (e) , _scroll (false) { - set_name ("processor prefader"); - add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK); - _plug->DropReferences.connect (_death_connection, invalidator (*this), boost::bind (&PluginDisplay::plugin_going_away, this), gui_context()); - _plug->QueueDraw.connect (_qdraw_connection, invalidator (*this), - boost::bind (&Gtk::Widget::queue_draw, this), gui_context ()); - std::string postfix = string_compose(_("\n%1+double-click to toggle inline-display"), Keyboard::tertiary_modifier_name ()); if (_plug->has_editor()) { @@ -1588,15 +1579,9 @@ ProcessorEntry::PluginDisplay::PluginDisplay (ProcessorEntry& e, boost::shared_p } } -ProcessorEntry::PluginDisplay::~PluginDisplay () -{ - if (_surf) { - cairo_surface_destroy (_surf); - } -} bool -ProcessorEntry::PluginDisplay::on_button_press_event (GdkEventButton *ev) +ProcessorEntry::PluginInlineDisplay::on_button_press_event (GdkEventButton *ev) { assert (_entry.processor ()); @@ -1623,14 +1608,8 @@ ProcessorEntry::PluginDisplay::on_button_press_event (GdkEventButton *ev) return false; } -bool -ProcessorEntry::PluginDisplay::on_button_release_event (GdkEventButton *ev) -{ - return false; -} - void -ProcessorEntry::PluginDisplay::on_size_request (Requisition* req) +ProcessorEntry::PluginInlineDisplay::on_size_request (Requisition* req) { req->width = 56; req->height = _cur_height; @@ -1638,7 +1617,7 @@ ProcessorEntry::PluginDisplay::on_size_request (Requisition* req) void -ProcessorEntry::PluginDisplay::update_height_alloc (uint32_t inline_height) +ProcessorEntry::PluginInlineDisplay::update_height_alloc (uint32_t inline_height) { /* work-around scroll-bar + aspect ratio * show inline-view -> height changes -> scrollbar gets added @@ -1667,105 +1646,8 @@ ProcessorEntry::PluginDisplay::update_height_alloc (uint32_t inline_height) _scroll = sc; } -uint32_t -ProcessorEntry::PluginDisplay::render_inline (cairo_t* cr, uint32_t width) -{ - Plugin::Display_Image_Surface* dis = _plug->render_inline_display (width, _max_height); - if (!dis) { - return 0; - } - - /* allocate a local image-surface, - * We cannot re-use the data via cairo_image_surface_create_for_data(), - * since pixman keeps a reference to it. - * we'd need to hand over the data and ha cairo_surface_destroy to free it. - * it might be possible to work around via cairo_surface_set_user_data(). - */ - if (!_surf - || dis->width != cairo_image_surface_get_width (_surf) - || dis->height != cairo_image_surface_get_height (_surf) - ) { - if (_surf) { - cairo_surface_destroy (_surf); - } - _surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, dis->width, dis->height); - } - - if (cairo_image_surface_get_stride (_surf) == dis->stride) { - memcpy (cairo_image_surface_get_data (_surf), dis->data, dis->stride * dis->height); - } else { - unsigned char *src = dis->data; - unsigned char *dst = cairo_image_surface_get_data (_surf); - const int dst_stride = cairo_image_surface_get_stride (_surf); - for (int y = 0; y < dis->height; ++y) { - memcpy (dst, src, dis->width * 4 /*ARGB32*/); - src += dis->stride; - dst += dst_stride; - } - } - - cairo_surface_flush(_surf); - cairo_surface_mark_dirty(_surf); - const double xc = floor ((width - dis->width) * .5); - cairo_set_source_surface(cr, _surf, xc, 0); - cairo_paint (cr); - - return dis->height; -} - -bool -ProcessorEntry::PluginDisplay::on_expose_event (GdkEventExpose* ev) -{ - Gtk::Allocation a = get_allocation(); - double const width = a.get_width(); - double const height = a.get_height(); - - cairo_t* cr = gdk_cairo_create (get_window()->gobj()); - cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height); - cairo_clip (cr); - - Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL); - cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ()); - cairo_rectangle (cr, 0, 0, width, height); - cairo_fill (cr); - - cairo_save (cr); - cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); - Gtkmm2ext::rounded_rectangle (cr, .5, -1.5, width - 1, height + 1, 7); - cairo_clip (cr); - cairo_set_operator (cr, CAIRO_OPERATOR_OVER); - - uint32_t ht = render_inline (cr, width); - cairo_restore (cr); - - if (ht == 0) { - hide (); - if (_cur_height != 1) { - _cur_height = 1; - queue_resize (); - } - cairo_destroy (cr); - return true; - } else { - update_height_alloc (ht); - } - - bool failed = false; - std::string name = get_name(); - Gtkmm2ext::Color fill_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed); - - Gtkmm2ext::rounded_rectangle (cr, .5, -1.5, width - 1, height + 1, 7); - cairo_set_operator (cr, CAIRO_OPERATOR_OVER); - cairo_set_line_width(cr, 1.0); - Gtkmm2ext::set_source_rgb_a (cr, fill_color, 1.0); - cairo_stroke (cr); - - cairo_destroy(cr); - return true; -} - ProcessorEntry::LuaPluginDisplay::LuaPluginDisplay (ProcessorEntry& e, boost::shared_ptr p, uint32_t max_height) - : PluginDisplay (e, p, max_height) + : PluginInlineDisplay (e, p, max_height) , _luaproc (p) , _lua_render_inline (0) { diff --git a/gtk2_ardour/processor_box.h b/gtk2_ardour/processor_box.h index 8bb7b55f9b..34fac115ca 100644 --- a/gtk2_ardour/processor_box.h +++ b/gtk2_ardour/processor_box.h @@ -54,6 +54,7 @@ #include "widgets/slider_controller.h" #include "plugin_interest.h" +#include "plugin_display.h" #include "io_selector.h" #include "send_ui.h" #include "enums.h" @@ -245,34 +246,20 @@ private: void toggle_panner_link (); void toggle_allow_feedback (); - class PluginDisplay : public Gtk::DrawingArea { + class PluginInlineDisplay : public PluginDisplay { public: - PluginDisplay(ProcessorEntry&, boost::shared_ptr, uint32_t max_height = 80); - virtual ~PluginDisplay(); + PluginInlineDisplay(ProcessorEntry&, boost::shared_ptr, uint32_t max_height = 80); + ~PluginInlineDisplay() {} protected: - bool on_expose_event (GdkEventExpose *); void on_size_request (Gtk::Requisition* req); bool on_button_press_event (GdkEventButton *ev); - bool on_button_release_event (GdkEventButton *ev); - - void plugin_going_away () { - _qdraw_connection.disconnect (); - } - void update_height_alloc (uint32_t inline_height); - virtual uint32_t render_inline (cairo_t *, uint32_t width); ProcessorEntry& _entry; - boost::shared_ptr _plug; - PBD::ScopedConnection _qdraw_connection; - PBD::ScopedConnection _death_connection; - cairo_surface_t* _surf; - uint32_t _max_height; - uint32_t _cur_height; bool _scroll; }; - class LuaPluginDisplay : public PluginDisplay { + class LuaPluginDisplay : public PluginInlineDisplay { public: LuaPluginDisplay(ProcessorEntry&, boost::shared_ptr, uint32_t max_height = 80); ~LuaPluginDisplay(); diff --git a/gtk2_ardour/wscript b/gtk2_ardour/wscript index 7df4ba2721..af61870a3d 100644 --- a/gtk2_ardour/wscript +++ b/gtk2_ardour/wscript @@ -181,6 +181,7 @@ gtk2_ardour_sources = [ 'piano_roll_header.cc', 'pingback.cc', 'playlist_selector.cc', + 'plugin_display.cc', 'plugin_eq_gui.cc', 'plugin_pin_dialog.cc', 'plugin_setup_dialog.cc', -- cgit v1.2.3