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 "pbd/stacktrace.h"
26 #include "gtkmm2ext/keyboard.h"
27 #include "ardour/instrument_info.h"
29 #include "ardour_ui.h"
30 #include "midi_region_view.h"
31 #include "canvas_patch_change.h"
33 #include "editor_drag.h"
35 using namespace Gnome::Canvas;
36 using namespace MIDI::Name;
37 using namespace Gtkmm2ext;
40 /** @param x x position in pixels.
42 CanvasPatchChange::CanvasPatchChange(
43 MidiRegionView& region,
49 ARDOUR::InstrumentInfo& info,
50 ARDOUR::MidiModel::PatchChangePtr patch,
57 ? ARDOUR_UI::config()->canvasvar_MidiPatchChangeOutline.get()
58 : ARDOUR_UI::config()->canvasvar_MidiPatchChangeInactiveChannelOutline.get()),
60 ? ARDOUR_UI::config()->canvasvar_MidiPatchChangeFill.get()
61 : ARDOUR_UI::config()->canvasvar_MidiPatchChangeInactiveChannelFill.get()),
66 , _popup_initialized(false)
71 CanvasPatchChange::~CanvasPatchChange()
76 CanvasPatchChange::initialize_popup_menus()
78 boost::shared_ptr<ChannelNameSet> channel_name_set = _info.get_patches (_patch->channel());
80 if (!channel_name_set) {
84 const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
86 if (patch_banks.size() > 1) {
88 Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
90 for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
91 bank != patch_banks.end();
93 Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
95 const PatchBank::PatchNameList& patches = (*bank)->patch_name_list();
96 Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
98 for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
99 patch != patches.end();
101 std::string name = (*patch)->name();
102 boost::replace_all (name, "_", " ");
104 patch_menus.push_back(
105 Gtk::Menu_Helpers::MenuElem(
107 sigc::bind(sigc::mem_fun(*this, &CanvasPatchChange::on_patch_menu_selected),
108 (*patch)->patch_primary_key())) );
111 std::string name = (*bank)->name();
112 boost::replace_all (name, "_", " ");
114 patch_bank_menus.push_back(
115 Gtk::Menu_Helpers::MenuElem(
120 /* only one patch bank, so make it the initial menu */
122 const PatchBank::PatchNameList& patches = patch_banks.front()->patch_name_list();
123 Gtk::Menu::MenuList& patch_menus = _popup.items();
125 for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
126 patch != patches.end();
128 std::string name = (*patch)->name();
129 boost::replace_all (name, "_", " ");
131 patch_menus.push_back (
132 Gtk::Menu_Helpers::MenuElem (
134 sigc::bind (sigc::mem_fun(*this, &CanvasPatchChange::on_patch_menu_selected),
135 (*patch)->patch_primary_key())));
141 CanvasPatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
143 _region.change_patch_change (*this, key);
147 in_edit_mode(Editor* editor)
149 return (editor->internal_editing() &&
150 (editor->current_mouse_mode() == Editing::MouseObject ||
151 editor->current_mouse_mode() == Editing::MouseDraw));
155 CanvasPatchChange::on_event (GdkEvent* ev)
160 case GDK_BUTTON_PRESS:
161 /* XXX: icky dcast */
162 e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
163 if (in_edit_mode(e)) {
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 if (!_popup.items().empty()) {
187 _popup.popup(ev->button.button, ev->button.time);
194 switch (ev->key.keyval) {
198 if (Keyboard::modifier_state_contains (ev->key.state, Keyboard::PrimaryModifier)) {
199 _region.previous_bank (*this);
201 _region.previous_patch (*this);
207 if (Keyboard::modifier_state_contains (ev->key.state, Keyboard::PrimaryModifier)) {
208 _region.next_bank (*this);
210 _region.next_patch (*this);
215 _region.delete_patch_change (this);
223 /* XXX: icky dcast */
224 e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
225 if (in_edit_mode(e)) {
226 if (ev->scroll.direction == GDK_SCROLL_UP) {
227 if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
228 _region.previous_bank (*this);
230 _region.previous_patch (*this);
232 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
233 if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
234 _region.next_bank (*this);
236 _region.next_patch (*this);
244 case GDK_ENTER_NOTIFY:
245 _region.patch_entered (this);
249 case GDK_LEAVE_NOTIFY:
250 _region.patch_left (this);