2 Copyright (C) 2000-2010 Paul Davis
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.
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.
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.
22 #include <boost/algorithm/string.hpp>
24 #include <glibmm/regex.h>
26 #include "gtkmm2ext/keyboard.h"
27 #include "ardour/midi_patch_manager.h"
28 #include "ardour_ui.h"
29 #include "midi_region_view.h"
30 #include "patch_change.h"
32 #include "editor_drag.h"
34 using namespace MIDI::Name;
37 /** @param x x position in pixels.
39 PatchChange::PatchChange(
40 MidiRegionView& region,
41 ArdourCanvas::Group* parent,
46 ARDOUR::InstrumentInfo& info,
47 ARDOUR::MidiModel::PatchChangePtr patch)
51 , _popup_initialized(false)
53 _flag = new ArdourCanvas::Flag (
56 ARDOUR_UI::config()->get_canvasvar_MidiPatchChangeOutline(),
57 ARDOUR_UI::config()->get_canvasvar_MidiPatchChangeFill(),
58 ArdourCanvas::Duple (x, y)
61 _flag->Event.connect (sigc::mem_fun (*this, &PatchChange::event_handler));
62 _flag->set_text(text);
65 PatchChange::~PatchChange()
70 PatchChange::initialize_popup_menus()
72 using namespace MIDI::Name;
74 boost::shared_ptr<ChannelNameSet> channel_name_set = _info.get_patches (_patch->channel());
76 if (!channel_name_set) {
80 const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
82 if (patch_banks.size() > 1) {
85 Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
87 for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
88 bank != patch_banks.end();
90 Glib::RefPtr<Glib::Regex> underscores = Glib::Regex::create("_");
91 std::string replacement(" ");
93 Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
95 const PatchNameList& patches = (*bank)->patch_name_list();
96 Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
98 for (PatchNameList::const_iterator patch = patches.begin();
99 patch != patches.end();
101 std::string name = underscores->replace((*patch)->name().c_str(), -1, 0, replacement);
103 patch_menus.push_back(
104 Gtk::Menu_Helpers::MenuElem(
107 sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected),
108 (*patch)->patch_primary_key())) );
112 std::string name = underscores->replace((*bank)->name().c_str(), -1, 0, replacement);
114 patch_bank_menus.push_back(
115 Gtk::Menu_Helpers::MenuElem(
121 /* only one patch bank, so make it the initial menu */
123 const PatchNameList& patches = patch_banks.front()->patch_name_list();
124 Gtk::Menu::MenuList& patch_menus = _popup.items();
126 for (PatchNameList::const_iterator patch = patches.begin();
127 patch != patches.end();
129 std::string name = (*patch)->name();
130 boost::replace_all (name, "_", " ");
132 patch_menus.push_back (
133 Gtk::Menu_Helpers::MenuElem (
135 sigc::bind (sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected),
136 (*patch)->patch_primary_key())));
142 PatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
144 _region.change_patch_change (*this, key);
148 PatchChange::event_handler (GdkEvent* ev)
151 case GDK_BUTTON_PRESS:
153 /* XXX: icky dcast */
154 Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
155 if (e->current_mouse_mode() == Editing::MouseObject && e->internal_editing()) {
157 if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
159 _region.delete_patch_change (this);
162 } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
164 _region.edit_patch_change (this);
167 } else if (ev->button.button == 1) {
168 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
173 if (ev->button.button == 3) {
174 if (!_popup_initialized) {
175 initialize_popup_menus();
176 _popup_initialized = true;
178 _popup.popup(ev->button.button, ev->button.time);
185 switch (ev->key.keyval) {
189 _region.previous_patch (*this);
194 _region.next_patch (*this);
202 if (ev->scroll.direction == GDK_SCROLL_UP) {
203 _region.previous_patch (*this);
205 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
206 _region.next_patch (*this);
211 case GDK_ENTER_NOTIFY:
212 _region.patch_entered (this);
215 case GDK_LEAVE_NOTIFY:
216 _region.patch_left (this);
227 PatchChange::move (ArdourCanvas::Duple d)
233 PatchChange::set_height (ArdourCanvas::Distance height)
235 _flag->set_height (height);