Fix patch flag scroll theft when in normal mode.
[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_MidiPatchChangeOutline(),
62                 ARDOUR_UI::config()->get_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         if (!e->internal_editing()) {
162                 // not in internal edit mode, so no patch change editing
163                 return false;
164         }
165
166         switch (ev->type) {
167         case GDK_BUTTON_PRESS:
168                 if (e->current_mouse_mode() == Editing::MouseObject) {
169
170                         if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
171
172                                 _region.delete_patch_change (this);
173                                 return true;
174
175                         } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
176
177                                 _region.edit_patch_change (this);
178                                 return true;
179                                 
180                         } else if (ev->button.button == 1) {
181                                 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
182                                 return true;
183                         }
184                 }
185
186                 if (ev->button.button == 3) {
187                         if (!_popup_initialized) {
188                                 initialize_popup_menus();
189                                 _popup_initialized = true;
190                         }
191                         _popup.popup(ev->button.button, ev->button.time);
192                         return true;
193                 }
194                 break;
195
196         case GDK_KEY_PRESS:
197                 switch (ev->key.keyval) {
198                 case GDK_Up:
199                 case GDK_KP_Up:
200                 case GDK_uparrow:
201                         _region.previous_patch (*this);
202                         break;
203                 case GDK_Down:
204                 case GDK_KP_Down:
205                 case GDK_downarrow:
206                         _region.next_patch (*this);
207                         break;
208                 default:
209                         break;
210                 }
211                 break;
212
213         case GDK_SCROLL:
214                 if (ev->scroll.direction == GDK_SCROLL_UP) {
215                         _region.previous_patch (*this);
216                         return true;
217                 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
218                         _region.next_patch (*this);
219                         return true;
220                 }
221                 break;
222
223         case GDK_ENTER_NOTIFY:
224                 _region.patch_entered (this);
225                 break;
226
227         case GDK_LEAVE_NOTIFY:
228                 _region.patch_left (this);
229                 break;
230
231         default:
232                 break;
233         }
234
235         return false;
236 }
237
238 void
239 PatchChange::move (ArdourCanvas::Duple d)
240 {
241         _flag->move (d);
242 }
243
244 void
245 PatchChange::set_height (ArdourCanvas::Distance height)
246 {
247         _flag->set_height (height);
248 }
249
250 void
251 PatchChange::hide ()
252 {
253         _flag->hide ();
254 }
255
256 void
257 PatchChange::show ()
258 {
259         _flag->show ();
260 }