Fix french translation of "meterbridge" -- closes #5744
[ardour.git] / gtk2_ardour / canvas_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 "pbd/stacktrace.h"
25
26 #include "gtkmm2ext/keyboard.h"
27 #include "ardour/instrument_info.h"
28 #include "midi++/midnam_patch.h"
29
30 #include "ardour_ui.h"
31 #include "midi_region_view.h"
32 #include "canvas_patch_change.h"
33 #include "editor.h"
34 #include "editor_drag.h"
35
36 using namespace Gnome::Canvas;
37 using namespace MIDI::Name;
38 using namespace Gtkmm2ext;
39 using namespace std;
40
41 /** @param x x position in pixels.
42  */
43 CanvasPatchChange::CanvasPatchChange(
44         MidiRegionView&                   region,
45         Group&                            parent,
46         const string&                     text,
47         double                            height,
48         double                            x,
49         double                            y,
50         ARDOUR::InstrumentInfo&           info,
51         ARDOUR::MidiModel::PatchChangePtr patch,
52         bool                              active_channel)
53         : CanvasFlag(
54                 region,
55                 parent,
56                 height,
57                 (active_channel
58                  ? ARDOUR_UI::config()->canvasvar_MidiPatchChangeOutline.get()
59                  : ARDOUR_UI::config()->canvasvar_MidiPatchChangeInactiveChannelOutline.get()),
60                 (active_channel
61                  ? ARDOUR_UI::config()->canvasvar_MidiPatchChangeFill.get()
62                  : ARDOUR_UI::config()->canvasvar_MidiPatchChangeInactiveChannelFill.get()),
63                 x,
64                 y)
65         , _info (info)
66         , _patch (patch)
67         , _popup_initialized(false)
68 {
69         set_text (text);
70 }
71
72 CanvasPatchChange::~CanvasPatchChange()
73 {
74 }
75
76 void
77 CanvasPatchChange::initialize_popup_menus()
78 {
79         boost::shared_ptr<ChannelNameSet> channel_name_set = _info.get_patches (_patch->channel());
80
81         if (!channel_name_set) {
82                 return;
83         }
84
85         const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
86
87         if (patch_banks.size() > 1) {
88                 // fill popup menu:
89                 Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
90                 
91                 for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
92                      bank != patch_banks.end();
93                      ++bank) {
94                         Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
95                         
96                         const PatchNameList& patches = (*bank)->patch_name_list();
97                         Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
98                         
99                         for (PatchNameList::const_iterator patch = patches.begin();
100                              patch != patches.end();
101                              ++patch) {
102                                 std::string name = (*patch)->name();
103                                 boost::replace_all (name, "_", " ");
104                                 
105                                 patch_menus.push_back(
106                                         Gtk::Menu_Helpers::MenuElem(
107                                                 name,
108                                                 sigc::bind(sigc::mem_fun(*this, &CanvasPatchChange::on_patch_menu_selected),
109                                                            (*patch)->patch_primary_key())) );
110                         }
111                         
112                         std::string name = (*bank)->name();
113                         boost::replace_all (name, "_", " ");
114                         
115                         patch_bank_menus.push_back(
116                                 Gtk::Menu_Helpers::MenuElem(
117                                         name,
118                                         patch_bank_menu) );
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, &CanvasPatchChange::on_patch_menu_selected),
136                                                     (*patch)->patch_primary_key())));
137                 }
138         }
139 }
140         
141 void
142 CanvasPatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
143 {
144         _region.change_patch_change (*this, key);
145 }
146
147 static bool
148 in_edit_mode(Editor* editor)
149 {
150         return (editor->internal_editing() &&
151                 (editor->current_mouse_mode() == Editing::MouseObject ||
152                  editor->current_mouse_mode() == Editing::MouseDraw));
153 }
154
155 bool
156 CanvasPatchChange::on_event (GdkEvent* ev)
157 {
158         /* XXX: icky dcast */
159         Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
160         
161         if (!in_edit_mode(e)) {
162                 return false;
163         }
164
165         switch (ev->type) {
166         case GDK_BUTTON_PRESS:
167                 if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
168                         
169                         _region.delete_patch_change (this);
170                         return true;
171                         
172                 } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
173                         
174                         _region.edit_patch_change (this);
175                         return true;
176                         
177                 } else if (ev->button.button == 1) {
178                         e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
179                         return true;
180                 }
181
182                 if (ev->button.button == 3) {
183                         if (!_popup_initialized) {
184                                 initialize_popup_menus();
185                                 _popup_initialized = true;
186                         }
187                         if (!_popup.items().empty()) {
188                                 _popup.popup(ev->button.button, ev->button.time);
189                         }
190                         return true;
191                 }
192                 break;
193
194         case GDK_KEY_PRESS:
195                 switch (ev->key.keyval) {
196                 case GDK_Up:
197                 case GDK_KP_Up:
198                 case GDK_uparrow:
199                         if (Keyboard::modifier_state_contains (ev->key.state, Keyboard::PrimaryModifier)) {
200                                 _region.previous_bank (*this);
201                         } else {
202                                 _region.previous_patch (*this);
203                         }
204                         break;
205                 case GDK_Down:
206                 case GDK_KP_Down:
207                 case GDK_downarrow:
208                         if (Keyboard::modifier_state_contains (ev->key.state, Keyboard::PrimaryModifier)) {
209                                 _region.next_bank (*this);
210                         } else {
211                                 _region.next_patch (*this);
212                         }
213                         break;
214                 case GDK_Delete:
215                 case GDK_BackSpace:
216                         _region.delete_patch_change (this);
217                         break;
218                 default:
219                         break;
220                 }
221                 break;
222
223         case GDK_SCROLL:
224                 if (ev->scroll.direction == GDK_SCROLL_UP) {
225                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
226                                 _region.previous_bank (*this);
227                         } else {
228                                 _region.previous_patch (*this);
229                         }
230                 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
231                                 if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
232                                         _region.next_bank (*this);
233                                 } else {
234                                         _region.next_patch (*this);
235                                 }
236                 }
237                 return true;
238
239         case GDK_ENTER_NOTIFY:
240                 _region.patch_entered (this);
241                 return true;
242                 break;
243
244         case GDK_LEAVE_NOTIFY:
245                 _region.patch_left (this);
246                 return true;
247                 break;
248
249         default:
250                 break;
251         }
252
253         return false;
254 }