Fix drag of patch changes.
[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 "ardour_ui.h"
34 #include "editor.h"
35 #include "editor_drag.h"
36 #include "midi_region_view.h"
37 #include "patch_change.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         : _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()->color ("midi patch change outline"),
62                 ARDOUR_UI::config()->color_mod ("midi patch change fill", "midi patch change fill"),
63                 ArdourCanvas::Duple (x, y));
64         
65         CANVAS_DEBUG_NAME (_flag, text);
66
67         _flag->Event.connect (sigc::mem_fun (*this, &PatchChange::event_handler));
68         _flag->set_font_description (ARDOUR_UI::config()->get_SmallFont());
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         if (!e->internal_editing()) {
162                 return false;
163         }
164
165         switch (ev->type) {
166         case GDK_BUTTON_PRESS:
167                 if (e->current_mouse_mode() == Editing::MouseContent) {
168
169                         if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
170
171                                 _region.delete_patch_change (this);
172                                 return true;
173
174                         } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
175
176                                 _region.edit_patch_change (this);
177                                 return true;
178                                 
179                         } else if (ev->button.button == 1) {
180                                 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
181                                 return true;
182                         }
183                 }
184
185                 if (ev->button.button == 3) {
186                         if (!_popup_initialized) {
187                                 initialize_popup_menus();
188                                 _popup_initialized = true;
189                         }
190                         _popup.popup(ev->button.button, ev->button.time);
191                         return true;
192                 }
193                 break;
194
195         case GDK_KEY_PRESS:
196                 switch (ev->key.keyval) {
197                 case GDK_Up:
198                 case GDK_KP_Up:
199                 case GDK_uparrow:
200                         _region.step_patch(
201                                 *this, Keyboard::modifier_state_contains(ev->key.state, Keyboard::TertiaryModifier), 1);
202                         return true;
203                 case GDK_Down:
204                 case GDK_KP_Down:
205                 case GDK_downarrow:
206                         _region.step_patch(
207                                 *this, Keyboard::modifier_state_contains(ev->key.state, Keyboard::TertiaryModifier), -1);
208                         return true;
209                 default:
210                         break;
211                 }
212                 break;
213
214         case GDK_SCROLL:
215                 if (ev->scroll.direction == GDK_SCROLL_UP) {
216                         _region.step_patch(
217                                 *this, Keyboard::modifier_state_contains(ev->scroll.state, Keyboard::TertiaryModifier), 1);
218                         return true;
219                 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
220                         _region.step_patch(
221                                 *this, Keyboard::modifier_state_contains(ev->scroll.state, Keyboard::TertiaryModifier), -1);
222                         return true;
223                 }
224                 break;
225
226         case GDK_ENTER_NOTIFY:
227                 _region.patch_entered (this);
228                 break;
229
230         case GDK_LEAVE_NOTIFY:
231                 _region.patch_left (this);
232                 break;
233
234         default:
235                 break;
236         }
237
238         return false;
239 }
240
241 void
242 PatchChange::move (ArdourCanvas::Duple d)
243 {
244         _flag->move (d);
245 }
246
247 void
248 PatchChange::set_height (ArdourCanvas::Distance height)
249 {
250         _flag->set_height (height);
251 }
252
253 void
254 PatchChange::hide ()
255 {
256         _flag->hide ();
257 }
258
259 void
260 PatchChange::show ()
261 {
262         _flag->show ();
263 }