update drobilla's fascistic dir-locals.el to force emacs users into whitespace submis...
[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 #include "ardour_ui.h"
27 #include "midi_region_view.h"
28 #include "canvas_patch_change.h"
29 #include "editor.h"
30 #include "editor_drag.h"
31
32 using namespace Gnome::Canvas;
33 using namespace MIDI::Name;
34 using namespace std;
35
36 /** @param x x position in pixels.
37  */
38 CanvasPatchChange::CanvasPatchChange(
39                 MidiRegionView& region,
40                 Group&          parent,
41                 const string&   text,
42                 double          height,
43                 double          x,
44                 double          y,
45                 string&         model_name,
46                 string&         custom_device_mode,
47                 ARDOUR::MidiModel::PatchChangePtr patch)
48         : CanvasFlag(
49                         region,
50                         parent,
51                         height,
52                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeOutline.get(),
53                         ARDOUR_UI::config()->canvasvar_MidiPatchChangeFill.get(),
54                         x,
55                         y)
56         , _model_name(model_name)
57         , _custom_device_mode(custom_device_mode)
58         , _patch (patch)
59         , _popup_initialized(false)
60 {
61         set_text(text);
62 }
63
64 CanvasPatchChange::~CanvasPatchChange()
65 {
66 }
67
68 void
69 CanvasPatchChange::initialize_popup_menus()
70 {
71         boost::shared_ptr<ChannelNameSet> channel_name_set =
72                 MidiPatchManager::instance()
73                 .find_channel_name_set(_model_name, _custom_device_mode, _patch->channel());
74
75         if (!channel_name_set) {
76                 return;
77         }
78
79         const ChannelNameSet::PatchBanks& patch_banks = channel_name_set->patch_banks();
80
81         // fill popup menu:
82         Gtk::Menu::MenuList& patch_bank_menus = _popup.items();
83
84         for (ChannelNameSet::PatchBanks::const_iterator bank = patch_banks.begin();
85              bank != patch_banks.end();
86              ++bank) {
87                 Glib::RefPtr<Glib::Regex> underscores = Glib::Regex::create("_");
88                 std::string replacement(" ");
89
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 = underscores->replace((*patch)->name().c_str(), -1, 0, replacement);
99
100                         patch_menus.push_back(
101                                 Gtk::Menu_Helpers::MenuElem(
102                                         name,
103                                         sigc::bind(
104                                                 sigc::mem_fun(*this, &CanvasPatchChange::on_patch_menu_selected),
105                                                 (*patch)->patch_primary_key())) );
106                 }
107
108
109                 std::string name = underscores->replace((*bank)->name().c_str(), -1, 0, replacement);
110
111                 patch_bank_menus.push_back(
112                         Gtk::Menu_Helpers::MenuElem(
113                                 name,
114                                 patch_bank_menu) );
115         }
116 }
117
118 void
119 CanvasPatchChange::on_patch_menu_selected(const PatchPrimaryKey& key)
120 {
121         cerr << " got patch program number " << key.program_number << endl;
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                         _popup.popup(ev->button.button, ev->button.time);
157                         return true;
158                 }
159                 break;
160         }
161
162         case GDK_KEY_PRESS:
163                 switch (ev->key.keyval) {
164                 case GDK_Up:
165                 case GDK_KP_Up:
166                 case GDK_uparrow:
167                         _region.previous_patch (*this);
168                         break;
169                 case GDK_Down:
170                 case GDK_KP_Down:
171                 case GDK_downarrow:
172                         _region.next_patch (*this);
173                         break;
174                 default:
175                         break;
176                 }
177                 break;
178
179         case GDK_SCROLL:
180                 if (ev->scroll.direction == GDK_SCROLL_UP) {
181                         _region.previous_patch (*this);
182                         return true;
183                 } else if (ev->scroll.direction == GDK_SCROLL_DOWN) {
184                         _region.next_patch (*this);
185                         return true;
186                 }
187                 break;
188
189         case GDK_ENTER_NOTIFY:
190                 _region.patch_entered (this);
191                 break;
192
193         case GDK_LEAVE_NOTIFY:
194                 _region.patch_left (this);
195                 break;
196
197         default:
198                 break;
199         }
200
201         return false;
202 }