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