use new action map API instead of ActionManager::get_action
[ardour.git] / gtk2_ardour / patch_change.cc
1 /*
2     Copyright (C) 2000-2010 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21
22 #include <boost/algorithm/string.hpp>
23
24 #include <glibmm/regex.h>
25
26 #include "gtkmm2ext/keyboard.h"
27 #include "gtkmm2ext/menu_elems.h"
28 #include "gtkmm2ext/utils.h"
29
30 #include "midi++/midnam_patch.h"
31
32 #include "canvas/debug.h"
33
34 #include "editor.h"
35 #include "editor_drag.h"
36 #include "midi_region_view.h"
37 #include "patch_change.h"
38 #include "ui_config.h"
39
40 using namespace MIDI::Name;
41 using namespace std;
42 using Gtkmm2ext::Keyboard;
43
44 /** @param x x position in pixels.
45  */
46 PatchChange::PatchChange(MidiRegionView&                   region,
47                          ArdourCanvas::Container*          parent,
48                          const string&                     text,
49                          double                            height,
50                          double                            x,
51                          double                            y,
52                          ARDOUR::InstrumentInfo&           info,
53                          ARDOUR::MidiModel::PatchChangePtr patch,
54                          Gtkmm2ext::Color               outline_color,
55                          Gtkmm2ext::Color               fill_color)
56         : _region (region)
57         , _info (info)
58         , _patch (patch)
59         , _popup_initialized(false)
60 {
61         _flag = new ArdourCanvas::Flag (
62                 parent,
63                 height,
64                 outline_color,
65                 fill_color,
66                 ArdourCanvas::Duple (x, y),
67                 true);
68
69         CANVAS_DEBUG_NAME (_flag, text);
70
71         _flag->Event.connect (sigc::mem_fun (*this, &PatchChange::event_handler));
72         _flag->set_font_description (UIConfiguration::instance().get_SmallFont());
73         _flag->set_text(text);
74 }
75
76 PatchChange::~PatchChange()
77 {
78         delete _flag;
79 }
80
81 void
82 PatchChange::initialize_popup_menus()
83 {
84         using namespace MIDI::Name;
85
86         boost::shared_ptr<ChannelNameSet> channel_name_set = _info.get_patches (_patch->channel());
87
88         if (!channel_name_set || channel_name_set->patch_banks().size () == 0) {
89                 return;
90         }
91
92         const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
93
94         if (patch_banks.size() > 1) {
95
96                 // fill popup menu:
97                 Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
98
99                 for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
100                      bank != patch_banks.end();
101                      ++bank) {
102                         Glib::RefPtr<Glib::Regex> underscores = Glib::Regex::create("_");
103                         std::string replacement(" ");
104
105                         Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
106
107                         const PatchNameList& patches = (*bank)->patch_name_list();
108                         Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
109
110                         for (PatchNameList::const_iterator patch = patches.begin();
111                              patch != patches.end();
112                              ++patch) {
113                                 std::string name = underscores->replace((*patch)->name().c_str(), -1, 0, replacement);
114
115                                 patch_menus.push_back(
116                                         Gtk::Menu_Helpers::MenuElem(
117                                                 name,
118                                                 sigc::bind(
119                                                         sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected),
120                                                         (*patch)->patch_primary_key())) );
121                         }
122
123
124                         std::string name = underscores->replace((*bank)->name().c_str(), -1, 0, replacement);
125
126                         patch_bank_menus.push_back(
127                                 Gtk::Menu_Helpers::MenuElem(
128                                         name,
129                                         patch_bank_menu) );
130                 }
131
132         } else {
133                 /* only one patch bank, so make it the initial menu */
134
135                 const PatchNameList& patches = patch_banks.front()->patch_name_list();
136                 Gtk::Menu::MenuList& patch_menus = _popup.items();
137
138                 for (PatchNameList::const_iterator patch = patches.begin();
139                      patch != patches.end();
140                      ++patch) {
141                         patch_menus.push_back (Gtkmm2ext::MenuElemNoMnemonic ((*patch)->name(),
142                                                 sigc::bind (sigc::mem_fun(*this, &PatchChange::on_patch_menu_selected), (*patch)->patch_primary_key())));
143                 }
144         }
145 }
146
147 void
148 PatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
149 {
150         _region.change_patch_change (*this, key);
151 }
152
153 bool
154 PatchChange::event_handler (GdkEvent* ev)
155 {
156         /* XXX: icky dcast */
157         Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
158
159         if (!e->internal_editing()) {
160                 return false;
161         }
162
163         switch (ev->type) {
164         case GDK_BUTTON_PRESS:
165                 if (e->current_mouse_mode() == Editing::MouseContent) {
166
167                         if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
168
169                                 _region.delete_patch_change (this);
170                                 return true;
171
172                         } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
173
174                                 _region.edit_patch_change (this);
175                                 return true;
176
177                         } else if (ev->button.button == 1) {
178                                 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
179                                 return true;
180                         }
181                 }
182
183                 if (Gtkmm2ext::Keyboard::is_context_menu_event (&ev->button)) {
184                         if (!_popup_initialized) {
185                                 initialize_popup_menus();
186                                 _popup_initialized = true;
187                         }
188                         _popup.popup(ev->button.button, ev->button.time);
189                         return true;
190                 }
191                 break;
192
193         case GDK_KEY_PRESS:
194                 switch (ev->key.keyval) {
195                 case GDK_Up:
196                 case GDK_KP_Up:
197                 case GDK_uparrow:
198                         _region.step_patch(
199                                 *this, Keyboard::modifier_state_contains(ev->key.state, Keyboard::TertiaryModifier), 1);
200                         return true;
201                 case GDK_Down:
202                 case GDK_KP_Down:
203                 case GDK_downarrow:
204                         _region.step_patch(
205                                 *this, Keyboard::modifier_state_contains(ev->key.state, Keyboard::TertiaryModifier), -1);
206                         return true;
207                 default:
208                         break;
209                 }
210                 break;
211
212         case GDK_KEY_RELEASE:
213                 switch (ev->key.keyval) {
214                 case GDK_BackSpace:
215                 case GDK_Delete:
216                         _region.delete_patch_change (this);
217                 default:
218                         break;
219                 }
220                 break;
221
222         case GDK_SCROLL:
223                 if (ev->scroll.direction == GDK_SCROLL_UP) {
224                         _region.step_patch(
225                                 *this, Keyboard::modifier_state_contains(ev->scroll.state, Keyboard::TertiaryModifier), 1);
226                         return true;
227                 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
228                         _region.step_patch(
229                                 *this, Keyboard::modifier_state_contains(ev->scroll.state, Keyboard::TertiaryModifier), -1);
230                         return true;
231                 }
232                 break;
233
234         case GDK_ENTER_NOTIFY:
235                 _region.patch_entered (this);
236                 break;
237
238         case GDK_LEAVE_NOTIFY:
239                 _region.patch_left (this);
240                 break;
241
242         default:
243                 break;
244         }
245
246         return false;
247 }
248
249 void
250 PatchChange::move (ArdourCanvas::Duple d)
251 {
252         _flag->move (d);
253 }
254
255 void
256 PatchChange::set_height (ArdourCanvas::Distance height)
257 {
258         _flag->set_height (height);
259 }
260
261 void
262 PatchChange::hide ()
263 {
264         _flag->hide ();
265 }
266
267 void
268 PatchChange::show ()
269 {
270         _flag->show ();
271 }