2 * Copyright (C) 2013-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2014-2015 David Robillard <d@drobilla.net>
4 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <boost/algorithm/string.hpp>
25 #include <glibmm/regex.h>
27 #include "gtkmm2ext/keyboard.h"
28 #include "gtkmm2ext/menu_elems.h"
29 #include "gtkmm2ext/utils.h"
31 #include "midi++/midnam_patch.h"
33 #include "canvas/debug.h"
36 #include "editor_drag.h"
37 #include "midi_region_view.h"
38 #include "patch_change.h"
39 #include "ui_config.h"
41 using namespace MIDI::Name;
43 using Gtkmm2ext::Keyboard;
45 /** @param x x position in pixels.
47 PatchChange::PatchChange(MidiRegionView& region,
48 ArdourCanvas::Container* parent,
53 ARDOUR::InstrumentInfo& info,
54 ARDOUR::MidiModel::PatchChangePtr patch,
55 Gtkmm2ext::Color outline_color,
56 Gtkmm2ext::Color fill_color)
60 , _popup_initialized(false)
62 _flag = new ArdourCanvas::Flag (
67 ArdourCanvas::Duple (x, y),
70 CANVAS_DEBUG_NAME (_flag, text);
72 _flag->Event.connect (sigc::mem_fun (*this, &PatchChange::event_handler));
73 _flag->set_font_description (UIConfiguration::instance().get_SmallFont());
74 _flag->set_text(text);
77 PatchChange::~PatchChange()
83 PatchChange::initialize_popup_menus()
85 using namespace MIDI::Name;
87 boost::shared_ptr<ChannelNameSet> channel_name_set = _info.get_patches (_patch->channel());
89 if (!channel_name_set || channel_name_set->patch_banks().size () == 0) {
93 const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
95 if (patch_banks.size() > 1) {
98 Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
100 for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
101 bank != patch_banks.end();
103 Glib::RefPtr<Glib::Regex> underscores = Glib::Regex::create("_");
104 std::string replacement(" ");
106 Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
108 const PatchNameList& patches = (*bank)->patch_name_list();
109 Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
111 for (PatchNameList::const_iterator patch = patches.begin();
112 patch != patches.end();
114 std::string name = underscores->replace((*patch)->name().c_str(), -1, 0, replacement);
116 patch_menus.push_back(
117 Gtk::Menu_Helpers::MenuElem(
120 sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected),
121 (*patch)->patch_primary_key())) );
125 std::string name = underscores->replace((*bank)->name().c_str(), -1, 0, replacement);
127 patch_bank_menus.push_back(
128 Gtk::Menu_Helpers::MenuElem(
134 /* only one patch bank, so make it the initial menu */
136 const PatchNameList& patches = patch_banks.front()->patch_name_list();
137 Gtk::Menu::MenuList& patch_menus = _popup.items();
139 for (PatchNameList::const_iterator patch = patches.begin();
140 patch != patches.end();
142 patch_menus.push_back (Gtkmm2ext::MenuElemNoMnemonic ((*patch)->name(),
143 sigc::bind (sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected), (*patch)->patch_primary_key())));
149 PatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
151 _region.change_patch_change (*this, key);
155 PatchChange::event_handler (GdkEvent* ev)
157 /* XXX: icky dcast */
158 Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
160 if (!e->internal_editing()) {
165 case GDK_BUTTON_PRESS:
166 if (e->current_mouse_mode() == Editing::MouseContent) {
168 if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
170 _region.delete_patch_change (this);
173 } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
175 _region.edit_patch_change (this);
178 } else if (ev->button.button == 1) {
179 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
184 if (Gtkmm2ext::Keyboard::is_context_menu_event (&ev->button)) {
185 if (!_popup_initialized) {
186 initialize_popup_menus();
187 _popup_initialized = true;
189 _popup.popup(ev->button.button, ev->button.time);
195 switch (ev->key.keyval) {
200 *this, Keyboard::modifier_state_contains(ev->key.state, Keyboard::TertiaryModifier), 1);
206 *this, Keyboard::modifier_state_contains(ev->key.state, Keyboard::TertiaryModifier), -1);
213 case GDK_KEY_RELEASE:
214 switch (ev->key.keyval) {
217 _region.delete_patch_change (this);
224 if (ev->scroll.direction == GDK_SCROLL_UP) {
226 *this, Keyboard::modifier_state_contains(ev->scroll.state, Keyboard::TertiaryModifier), 1);
228 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
230 *this, Keyboard::modifier_state_contains(ev->scroll.state, Keyboard::TertiaryModifier), -1);
235 case GDK_ENTER_NOTIFY:
236 _region.patch_entered (this);
239 case GDK_LEAVE_NOTIFY:
240 _region.patch_left (this);
251 PatchChange::move (ArdourCanvas::Duple d)
257 PatchChange::set_height (ArdourCanvas::Distance height)
259 _flag->set_height (height);