ff28295c6e4cf9f5672928ea2215eb3af1d71a7d
[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 "gtkmm2ext/keyboard.h"
25 #include "ardour/instrument_info.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                 ARDOUR::InstrumentInfo& info,
48                 ARDOUR::MidiModel::PatchChangePtr patch,
49                 bool active_channel)
50         : CanvasFlag(
51                         region,
52                         parent,
53                         height,
54                         active_channel ?
55                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeOutline.get() :
56                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeInactiveChannelOutline.get(),
57                         active_channel ?
58                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeFill.get() :
59                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeInactiveChannelFill.get(),
60                         x,
61                         y)
62         , _info (info)
63         , _patch (patch)
64         , _popup_initialized(false)
65 {
66         set_text (text);
67 }
68
69 CanvasPatchChange::~CanvasPatchChange()
70 {
71 }
72
73 void
74 CanvasPatchChange::initialize_popup_menus()
75 {
76         boost::shared_ptr<ChannelNameSet> channel_name_set = _info.get_patches (_patch->channel());
77
78         if (!channel_name_set) {
79                 return;
80         }
81
82         const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
83
84         // fill popup menu:
85         Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
86
87         for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
88              bank != patch_banks.end();
89              ++bank) {
90                 Gtk::Menu& patch_bank_menu = *manage(new Gtk::Menu());
91
92                 const PatchBank::PatchNameList& patches = (*bank)->patch_name_list();
93                 Gtk::Menu::MenuList& patch_menus = patch_bank_menu.items();
94
95                 for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
96                      patch != patches.end();
97                      ++patch) {
98                         std::string name = (*patch)->name();
99                         boost::replace_all (name, "_", " ");
100
101                         patch_menus.push_back(
102                                 Gtk::Menu_Helpers::MenuElem(
103                                         name,
104                                         sigc::bind(
105                                                 sigc::mem_fun(*this, &CanvasPatchChange::on_patch_menu_selected),
106                                                 (*patch)->patch_primary_key())) );
107                 }
108
109                 std::string name = (*bank)->name();
110                 boost::replace_all (name, "_", " ");
111
112                 patch_bank_menus.push_back(
113                         Gtk::Menu_Helpers::MenuElem(
114                                 name,
115                                 patch_bank_menu) );
116         }
117 }
118
119 void
120 CanvasPatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
121 {
122         _region.change_patch_change (*this, key);
123 }
124
125 bool
126 CanvasPatchChange::on_event (GdkEvent* ev)
127 {
128         switch (ev->type) {
129         case GDK_BUTTON_PRESS:
130         {
131                 /* XXX: icky dcast */
132                 Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
133                 if (e->current_mouse_mode() == Editing::MouseObject && e->internal_editing()) {
134
135                         if (Gtkmm2ext::Keyboard::is_delete_event (&ev->button)) {
136
137                                 _region.delete_patch_change (this);
138                                 return true;
139
140                         } else if (Gtkmm2ext::Keyboard::is_edit_event (&ev->button)) {
141
142                                 _region.edit_patch_change (this);
143                                 return true;
144
145                         } else if (ev->button.button == 1) {
146                                 e->drags()->set (new PatchChangeDrag (e, this, &_region), ev);
147                                 return true;
148                         }
149                 }
150
151                 if (ev->button.button == 3) {
152                         if (!_popup_initialized) {
153                                 initialize_popup_menus();
154                                 _popup_initialized = true;
155                         }
156                         if (!_popup.items().empty()) {
157                                 _popup.popup(ev->button.button, ev->button.time);
158                         }
159                         return true;
160                 }
161                 break;
162         }
163
164         case GDK_KEY_PRESS:
165                 switch (ev->key.keyval) {
166                 case GDK_Up:
167                 case GDK_KP_Up:
168                 case GDK_uparrow:
169                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
170                                 _region.previous_bank (*this);
171                         } else {
172                                 _region.previous_patch (*this);
173                         }
174                         break;
175                 case GDK_Down:
176                 case GDK_KP_Down:
177                 case GDK_downarrow:
178                         if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
179                                 _region.next_bank (*this);
180                         } else {
181                                 _region.next_patch (*this);
182                         }
183                         break;
184                 default:
185                         break;
186                 }
187                 break;
188
189         case GDK_SCROLL:
190         {
191                 /* XXX: icky dcast */
192                 Editor* e = dynamic_cast<Editor*> (&_region.get_time_axis_view().editor());
193                 if (e->current_mouse_mode() == Editing::MouseObject && e->internal_editing()) {
194                         if (ev->scroll.direction == GDK_SCROLL_UP) {
195                                 if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
196                                         _region.previous_bank (*this);
197                                 } else {
198                                         _region.previous_patch (*this);
199                                 }
200                                 return true;
201                         } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
202                                 if (Keyboard::modifier_state_contains (ev->scroll.state, Keyboard::PrimaryModifier)) {
203                                         _region.next_bank (*this);
204                                 } else {
205                                         _region.next_patch (*this);
206                                 }
207                                 return true;
208                         }
209                         break;
210                 }
211         }
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         default:
222                 break;
223         }
224
225         return false;
226 }