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