b4da0d5b460a6fbd57a7c251297a8684b5644dd0
[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 <glibmm/regex.h>
23
24 #include "gtkmm2ext/keyboard.h"
25 #include "ardour/midi_patch_manager.h"
26
27 #include "ardour_ui.h"
28 #include "midi_region_view.h"
29 #include "canvas_patch_change.h"
30 #include "editor.h"
31 #include "editor_drag.h"
32
33 using namespace Gnome::Canvas;
34 using namespace MIDI::Name;
35 using namespace Gtkmm2ext;
36 using namespace std;
37
38 /** @param x x position in pixels.
39  */
40 CanvasPatchChange::CanvasPatchChange(
41                 MidiRegionView& region,
42                 Group&          parent,
43                 const string&   text,
44                 double          height,
45                 double          x,
46                 double          y,
47                 string&         model_name,
48                 string&         custom_device_mode,
49                 ARDOUR::MidiModel::PatchChangePtr patch)
50         : CanvasFlag(
51                         region,
52                         parent,
53                         height,
54                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeOutline.get(),
55                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeFill.get(),
56                         x,
57                         y)
58         , _model_name(model_name)
59         , _custom_device_mode(custom_device_mode)
60         , _patch (patch)
61         , _popup_initialized(false)
62 {
63         set_text(text);
64 }
65
66 CanvasPatchChange::~CanvasPatchChange()
67 {
68 }
69
70 void
71 CanvasPatchChange::initialize_popup_menus()
72 {
73         boost::shared_ptr<ChannelNameSet> channel_name_set =
74                 MidiPatchManager::instance()
75                 .find_channel_name_set(_model_name, _custom_device_mode, _patch->channel());
76
77         if (!channel_name_set) {
78                 return;
79         }
80
81         const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
82
83         // fill popup menu:
84         Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
85
86         for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
87              bank != patch_banks.end();
88              ++bank) {
89                 Glib::RefPtr<Glib::Regex> underscores = Glib::Regex::create("_");
90                 std::string replacement(" ");
91
92                 Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
93
94                 const PatchBank::PatchNameList& patches = (*bank)->patch_name_list();
95                 Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
96
97                 for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
98                      patch != patches.end();
99                      ++patch) {
100                         std::string name = underscores->replace((*patch)->name().c_str(), -1, 0, replacement);
101
102                         patch_menus.push_back(
103                                 Gtk::Menu_Helpers::MenuElem(
104                                         name,
105                                         sigc::bind(
106                                                 sigc::mem_fun(*this, &CanvasPatchChange::on_patch_menu_selected),
107                                                 (*patch)->patch_primary_key())) );
108                 }
109
110
111                 std::string name = underscores->replace((*bank)->name().c_str(), -1, 0, replacement);
112
113                 patch_bank_menus.push_back(
114                         Gtk::Menu_Helpers::MenuElem(
115                                 name,
116                                 patch_bank_menu) );
117         }
118 }
119
120 void
121 CanvasPatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
122 {
123         _region.change_patch_change (*this, key);
124 }
125
126 bool
127 CanvasPatchChange::on_event (GdkEvent* ev)
128 {
129         switch (ev->type) {
130         case GDK_BUTTON_PRESS:
131         {
132                 /* XXX: icky dcast */
133                 Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
134                 if (e->current_mouse_mode() == Editing::MouseObject && e->internal_editing()) {
135
136                         if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
137
138                                 _region.delete_patch_change (this);
139                                 return true;
140
141                         } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
142
143                                 _region.edit_patch_change (this);
144                                 return true;
145
146                         } else if (ev->button.button == 1) {
147                                 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
148                                 return true;
149                         }
150                 }
151
152                 if (ev->button.button == 3) {
153                         if (!_popup_initialized) {
154                                 initialize_popup_menus();
155                                 _popup_initialized = true;
156                         }
157                         if (!_popup.items().empty()) {
158                                 _popup.popup(ev->button.button, ev->button.time);
159                         }
160                         return true;
161                 }
162                 break;
163         }
164
165         case GDK_KEY_PRESS:
166                 switch (ev->key.keyval) {
167                 case GDK_Up:
168                 case GDK_KP_Up:
169                 case GDK_uparrow:
170                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
171                                 _region.previous_bank (*this);
172                         } else {
173                                 _region.previous_patch (*this);
174                         }
175                         break;
176                 case GDK_Down:
177                 case GDK_KP_Down:
178                 case GDK_downarrow:
179                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
180                                 _region.next_bank (*this);
181                         } else {
182                                 _region.next_patch (*this);
183                         }
184                         break;
185                 default:
186                         break;
187                 }
188                 break;
189
190         case GDK_SCROLL:
191                 if (ev->scroll.direction == GDK_SCROLL_UP) {
192                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
193                                 _region.previous_bank (*this);
194                         } else {
195                                 _region.previous_patch (*this);
196                         }
197                         return true;
198                 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
199                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
200                                 _region.next_bank (*this);
201                         } else {
202                                 _region.next_patch (*this);
203                         }
204                         return true;
205                 }
206                 break;
207
208         case GDK_ENTER_NOTIFY:
209                 _region.patch_entered (this);
210                 break;
211
212         case GDK_LEAVE_NOTIFY:
213                 _region.patch_left (this);
214                 break;
215
216         case GDK_BUTTON_RELEASE:
217                 return true;
218
219         default:
220                 break;
221         }
222
223         return false;
224 }