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