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