Grey-out patch changes on inactive channels rather than not
[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                 bool active_channel)
51         : CanvasFlag(
52                         region,
53                         parent,
54                         height,
55                         active_channel ?
56                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeOutline.get() :
57                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeInactiveChannelOutline.get(),
58                         active_channel ?
59                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeFill.get() :
60                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeInactiveChannelFill.get(),
61                         x,
62                         y)
63         , _model_name(model_name)
64         , _custom_device_mode(custom_device_mode)
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 =
79                 MidiPatchManager::instance()
80                 .find_channel_name_set(_model_name, _custom_device_mode, _patch->channel());
81
82         if (!channel_name_set) {
83                 return;
84         }
85
86         const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
87
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                 Glib::RefPtr<Glib::Regex> underscores = Glib::Regex::create("_");
95                 std::string replacement(" ");
96
97                 Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
98
99                 const PatchBank::PatchNameList& patches = (*bank)->patch_name_list();
100                 Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
101
102                 for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
103                      patch != patches.end();
104                      ++patch) {
105                         std::string name = underscores->replace((*patch)->name().c_str(), -1, 0, replacement);
106
107                         patch_menus.push_back(
108                                 Gtk::Menu_Helpers::MenuElem(
109                                         name,
110                                         sigc::bind(
111                                                 sigc::mem_fun(*this, &CanvasPatchChange::on_patch_menu_selected),
112                                                 (*patch)->patch_primary_key())) );
113                 }
114
115
116                 std::string name = underscores->replace((*bank)->name().c_str(), -1, 0, replacement);
117
118                 patch_bank_menus.push_back(
119                         Gtk::Menu_Helpers::MenuElem(
120                                 name,
121                                 patch_bank_menu) );
122         }
123 }
124
125 void
126 CanvasPatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
127 {
128         _region.change_patch_change (*this, key);
129 }
130
131 bool
132 CanvasPatchChange::on_event (GdkEvent* ev)
133 {
134         switch (ev->type) {
135         case GDK_BUTTON_PRESS:
136         {
137                 /* XXX: icky dcast */
138                 Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
139                 if (e->current_mouse_mode() == Editing::MouseObject && e->internal_editing()) {
140
141                         if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
142
143                                 _region.delete_patch_change (this);
144                                 return true;
145
146                         } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
147
148                                 _region.edit_patch_change (this);
149                                 return true;
150
151                         } else if (ev->button.button == 1) {
152                                 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
153                                 return true;
154                         }
155                 }
156
157                 if (ev->button.button == 3) {
158                         if (!_popup_initialized) {
159                                 initialize_popup_menus();
160                                 _popup_initialized = true;
161                         }
162                         if (!_popup.items().empty()) {
163                                 _popup.popup(ev->button.button, ev->button.time);
164                         }
165                         return true;
166                 }
167                 break;
168         }
169
170         case GDK_KEY_PRESS:
171                 switch (ev->key.keyval) {
172                 case GDK_Up:
173                 case GDK_KP_Up:
174                 case GDK_uparrow:
175                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
176                                 _region.previous_bank (*this);
177                         } else {
178                                 _region.previous_patch (*this);
179                         }
180                         break;
181                 case GDK_Down:
182                 case GDK_KP_Down:
183                 case GDK_downarrow:
184                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
185                                 _region.next_bank (*this);
186                         } else {
187                                 _region.next_patch (*this);
188                         }
189                         break;
190                 default:
191                         break;
192                 }
193                 break;
194
195         case GDK_SCROLL:
196                 if (ev->scroll.direction == GDK_SCROLL_UP) {
197                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
198                                 _region.previous_bank (*this);
199                         } else {
200                                 _region.previous_patch (*this);
201                         }
202                         return true;
203                 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
204                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
205                                 _region.next_bank (*this);
206                         } else {
207                                 _region.next_patch (*this);
208                         }
209                         return true;
210                 }
211                 break;
212
213         case GDK_ENTER_NOTIFY:
214                 _region.patch_entered (this);
215                 break;
216
217         case GDK_LEAVE_NOTIFY:
218                 _region.patch_left (this);
219                 break;
220
221         case GDK_BUTTON_RELEASE:
222                 return true;
223
224         default:
225                 break;
226         }
227
228         return false;
229 }