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 "midi++/midnam_patch.h"
31 #include "canvas/debug.h"
33 #include "ardour_ui.h"
35 #include "editor_drag.h"
36 #include "midi_region_view.h"
37 #include "patch_change.h"
39 using namespace MIDI::Name;
41 using Gtkmm2ext::Keyboard;
43 /** @param x x position in pixels.
45 PatchChange::PatchChange(MidiRegionView& region,
46 ArdourCanvas::Container* parent,
51 ARDOUR::InstrumentInfo& info,
52 ARDOUR::MidiModel::PatchChangePtr patch)
56 , _popup_initialized(false)
58 _flag = new ArdourCanvas::Flag (
61 ARDOUR_UI::config()->color ("midi patch change outline"),
62 ARDOUR_UI::config()->color_mod ("midi patch change fill", "midi patch change fill"),
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_font_description (ARDOUR_UI::config()->get_SmallFont());
70 _flag->set_text(text);
73 PatchChange::~PatchChange()
79 PatchChange::initialize_popup_menus()
81 using namespace MIDI::Name;
83 boost::shared_ptr<ChannelNameSet> channel_name_set = _info.get_patches (_patch->channel());
85 if (!channel_name_set) {
89 const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
91 if (patch_banks.size() > 1) {
94 Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
96 for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
97 bank != patch_banks.end();
99 Glib::RefPtr<Glib::Regex> underscores = Glib::Regex::create("_");
100 std::string replacement(" ");
102 Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
104 const PatchNameList& patches = (*bank)->patch_name_list();
105 Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
107 for (PatchNameList::const_iterator patch = patches.begin();
108 patch != patches.end();
110 std::string name = underscores->replace((*patch)->name().c_str(), -1, 0, replacement);
112 patch_menus.push_back(
113 Gtk::Menu_Helpers::MenuElem(
116 sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected),
117 (*patch)->patch_primary_key())) );
121 std::string name = underscores->replace((*bank)->name().c_str(), -1, 0, replacement);
123 patch_bank_menus.push_back(
124 Gtk::Menu_Helpers::MenuElem(
130 /* only one patch bank, so make it the initial menu */
132 const PatchNameList& patches = patch_banks.front()->patch_name_list();
133 Gtk::Menu::MenuList& patch_menus = _popup.items();
135 for (PatchNameList::const_iterator patch = patches.begin();
136 patch != patches.end();
138 std::string name = (*patch)->name();
139 boost::replace_all (name, "_", " ");
141 patch_menus.push_back (
142 Gtk::Menu_Helpers::MenuElem (
144 sigc::bind (sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected),
145 (*patch)->patch_primary_key())));
151 PatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
153 _region.change_patch_change (*this, key);
157 PatchChange::event_handler (GdkEvent* ev)
159 /* XXX: icky dcast */
160 Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
162 if (!e->internal_editing()) {
167 case GDK_BUTTON_PRESS:
168 if (e->current_mouse_mode() == Editing::MouseContent) {
170 if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
172 _region.delete_patch_change (this);
175 } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
177 _region.edit_patch_change (this);
180 } else if (ev->button.button == 1) {
181 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
186 if (ev->button.button == 3) {
187 if (!_popup_initialized) {
188 initialize_popup_menus();
189 _popup_initialized = true;
191 _popup.popup(ev->button.button, ev->button.time);
197 switch (ev->key.keyval) {
202 *this, Keyboard::modifier_state_contains(ev->key.state, Keyboard::TertiaryModifier), 1);
208 *this, Keyboard::modifier_state_contains(ev->key.state, Keyboard::TertiaryModifier), -1);
215 case GDK_KEY_RELEASE:
216 switch (ev->key.keyval) {
219 _region.delete_patch_change (this);
226 if (ev->scroll.direction == GDK_SCROLL_UP) {
228 *this, Keyboard::modifier_state_contains(ev->scroll.state, Keyboard::TertiaryModifier), 1);
230 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
232 *this, Keyboard::modifier_state_contains(ev->scroll.state, Keyboard::TertiaryModifier), -1);
237 case GDK_ENTER_NOTIFY:
238 _region.patch_entered (this);
241 case GDK_LEAVE_NOTIFY:
242 _region.patch_left (this);
253 PatchChange::move (ArdourCanvas::Duple d)
259 PatchChange::set_height (ArdourCanvas::Distance height)
261 _flag->set_height (height);