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 "gtkmm2ext/utils.h"
29 #include "ardour/midi_patch_manager.h"
31 #include "canvas/debug.h"
33 #include "ardour_ui.h"
34 #include "midi_region_view.h"
35 #include "patch_change.h"
37 #include "editor_drag.h"
39 using namespace MIDI::Name;
42 /** @param x x position in pixels.
44 PatchChange::PatchChange(
45 MidiRegionView& region,
46 ArdourCanvas::Group* parent,
51 ARDOUR::InstrumentInfo& info,
52 ARDOUR::MidiModel::PatchChangePtr patch)
56 , _popup_initialized(false)
58 _flag = new ArdourCanvas::Flag (
61 ARDOUR_UI::config()->get_canvasvar_MidiPatchChangeOutline(),
62 ARDOUR_UI::config()->get_canvasvar_MidiPatchChangeFill(),
63 ArdourCanvas::Duple (x, y)
66 CANVAS_DEBUG_NAME (_flag, text);
68 _flag->Event.connect (sigc::mem_fun (*this, &PatchChange::event_handler));
69 _flag->set_text(text);
72 PatchChange::~PatchChange()
78 PatchChange::initialize_popup_menus()
80 using namespace MIDI::Name;
82 boost::shared_ptr<ChannelNameSet> channel_name_set = _info.get_patches (_patch->channel());
84 if (!channel_name_set) {
88 const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
90 if (patch_banks.size() > 1) {
93 Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
95 for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
96 bank != patch_banks.end();
98 Glib::RefPtr<Glib::Regex> underscores = Glib::Regex::create("_");
99 std::string replacement(" ");
101 Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
103 const PatchNameList& patches = (*bank)->patch_name_list();
104 Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
106 for (PatchNameList::const_iterator patch = patches.begin();
107 patch != patches.end();
109 std::string name = underscores->replace((*patch)->name().c_str(), -1, 0, replacement);
111 patch_menus.push_back(
112 Gtk::Menu_Helpers::MenuElem(
115 sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected),
116 (*patch)->patch_primary_key())) );
120 std::string name = underscores->replace((*bank)->name().c_str(), -1, 0, replacement);
122 patch_bank_menus.push_back(
123 Gtk::Menu_Helpers::MenuElem(
129 /* only one patch bank, so make it the initial menu */
131 const PatchNameList& patches = patch_banks.front()->patch_name_list();
132 Gtk::Menu::MenuList& patch_menus = _popup.items();
134 for (PatchNameList::const_iterator patch = patches.begin();
135 patch != patches.end();
137 std::string name = (*patch)->name();
138 boost::replace_all (name, "_", " ");
140 patch_menus.push_back (
141 Gtk::Menu_Helpers::MenuElem (
143 sigc::bind (sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected),
144 (*patch)->patch_primary_key())));
150 PatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
152 _region.change_patch_change (*this, key);
156 PatchChange::event_handler (GdkEvent* ev)
158 /* XXX: icky dcast */
159 Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
162 case GDK_BUTTON_PRESS:
163 if (e->current_mouse_mode() == Editing::MouseObject && e->internal_editing()) {
165 if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
167 _region.delete_patch_change (this);
170 } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
172 _region.edit_patch_change (this);
175 } else if (ev->button.button == 1) {
176 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
181 if (ev->button.button == 3) {
182 if (!_popup_initialized) {
183 initialize_popup_menus();
184 _popup_initialized = true;
186 _popup.popup(ev->button.button, ev->button.time);
192 switch (ev->key.keyval) {
196 _region.previous_patch (*this);
201 _region.next_patch (*this);
209 if (ev->scroll.direction == GDK_SCROLL_UP) {
210 _region.previous_patch (*this);
212 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
213 _region.next_patch (*this);
218 case GDK_ENTER_NOTIFY:
219 _region.patch_entered (this);
222 case GDK_LEAVE_NOTIFY:
223 _region.patch_left (this);
234 PatchChange::move (ArdourCanvas::Duple d)
240 PatchChange::set_height (ArdourCanvas::Distance height)
242 _flag->set_height (height);