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