1 |
/* |
2 |
* Copyright (C) 2006-2008 Andreas Persson |
3 |
* |
4 |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License as |
6 |
* published by the Free Software Foundation; either version 2, or (at |
7 |
* your option) any later version. |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, but |
10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
* General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with program; see the file COPYING. If not, write to the Free |
16 |
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
17 |
* 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include "regionchooser.h" |
21 |
#include <algorithm> |
22 |
#include <gdkmm/cursor.h> |
23 |
#include <gtkmm/stock.h> |
24 |
#include <gtkmm/spinbutton.h> |
25 |
#include <gtkmm/dialog.h> |
26 |
#include <math.h> |
27 |
#include <sstream> |
28 |
|
29 |
#include "global.h" |
30 |
|
31 |
#define REGION_BLOCK_HEIGHT 20 |
32 |
#define KEYBOARD_HEIGHT 40 |
33 |
|
34 |
void SortedRegions::update(gig::Instrument* instrument) { |
35 |
// Usually, the regions in a gig file are ordered after their key |
36 |
// range, but there are files where they are not. The |
37 |
// RegionChooser code needs a sorted list of regions. |
38 |
regions.clear(); |
39 |
if (instrument) { |
40 |
for (gig::Region *r = instrument->GetFirstRegion() ; |
41 |
r ; |
42 |
r = instrument->GetNextRegion()) { |
43 |
regions.push_back(r); |
44 |
} |
45 |
sort(regions.begin(), regions.end(), *this); |
46 |
} |
47 |
} |
48 |
|
49 |
gig::Region* SortedRegions::first() { |
50 |
region_iterator = regions.begin(); |
51 |
return region_iterator == regions.end() ? 0 : *region_iterator; |
52 |
} |
53 |
|
54 |
gig::Region* SortedRegions::next() { |
55 |
region_iterator++; |
56 |
return region_iterator == regions.end() ? 0 : *region_iterator; |
57 |
} |
58 |
|
59 |
|
60 |
|
61 |
RegionChooser::RegionChooser() : |
62 |
m_VirtKeybModeChoice(_("Virtual Keyboard Mode")), |
63 |
currentActiveKey(-1) |
64 |
{ |
65 |
Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap(); |
66 |
|
67 |
red = Gdk::Color("#8070ff"); |
68 |
grey1 = Gdk::Color("#b0b0b0"); |
69 |
activeKeyColor = Gdk::Color("#ff0000"); |
70 |
white = Gdk::Color("#ffffff"); |
71 |
black = Gdk::Color("#000000"); |
72 |
|
73 |
colormap->alloc_color(red); |
74 |
colormap->alloc_color(grey1); |
75 |
colormap->alloc_color(activeKeyColor); |
76 |
colormap->alloc_color(white); |
77 |
colormap->alloc_color(black); |
78 |
instrument = 0; |
79 |
region = 0; |
80 |
resize.active = false; |
81 |
move.active = false; |
82 |
cursor_is_resize = false; |
83 |
h1 = REGION_BLOCK_HEIGHT; |
84 |
|
85 |
// properties of the virtual keyboard |
86 |
{ |
87 |
const char* choices[] = { "normal", "chord", NULL }; |
88 |
static const virt_keyboard_mode_t values[] = { |
89 |
VIRT_KEYBOARD_MODE_NORMAL, |
90 |
VIRT_KEYBOARD_MODE_CHORD |
91 |
}; |
92 |
m_VirtKeybModeChoice.set_choices(choices, values); |
93 |
m_VirtKeybModeChoice.set_value(VIRT_KEYBOARD_MODE_NORMAL); |
94 |
} |
95 |
m_VirtKeybVelocityLabelDescr.set_text(_("Note-On Velocity:")); |
96 |
m_VirtKeybVelocityLabel.set_text("-"); |
97 |
m_VirtKeybOffVelocityLabelDescr.set_text(_("Note-Off Velocity:")); |
98 |
m_VirtKeybOffVelocityLabel.set_text("-"); |
99 |
m_VirtKeybPropsBox.pack_start(m_VirtKeybModeChoice.label, Gtk::PACK_SHRINK); |
100 |
m_VirtKeybPropsBox.pack_start(m_VirtKeybModeChoice.widget, Gtk::PACK_SHRINK); |
101 |
m_VirtKeybPropsBox.pack_start(m_VirtKeybVelocityLabelDescr, Gtk::PACK_SHRINK); |
102 |
m_VirtKeybPropsBox.pack_start(m_VirtKeybVelocityLabel, Gtk::PACK_SHRINK); |
103 |
m_VirtKeybPropsBox.pack_start(m_VirtKeybOffVelocityLabelDescr, Gtk::PACK_SHRINK); |
104 |
m_VirtKeybPropsBox.pack_start(m_VirtKeybOffVelocityLabel, Gtk::PACK_SHRINK); |
105 |
m_VirtKeybPropsBox.set_spacing(10); |
106 |
m_VirtKeybPropsBox.show(); |
107 |
|
108 |
actionGroup = Gtk::ActionGroup::create(); |
109 |
actionGroup->add(Gtk::Action::create("Properties", |
110 |
Gtk::Stock::PROPERTIES), |
111 |
sigc::mem_fun(*this, |
112 |
&RegionChooser::show_region_properties)); |
113 |
actionGroup->add(Gtk::Action::create("Remove", Gtk::Stock::REMOVE), |
114 |
sigc::mem_fun(*this, &RegionChooser::delete_region)); |
115 |
actionGroup->add(Gtk::Action::create("Add", Gtk::Stock::ADD), |
116 |
sigc::mem_fun(*this, &RegionChooser::add_region)); |
117 |
actionGroup->add(Gtk::Action::create("Dimensions", _("Dimensions...")), |
118 |
sigc::mem_fun(*this, &RegionChooser::manage_dimensions)); |
119 |
|
120 |
uiManager = Gtk::UIManager::create(); |
121 |
uiManager->insert_action_group(actionGroup); |
122 |
Glib::ustring ui_info = |
123 |
"<ui>" |
124 |
" <popup name='PopupMenuInsideRegion'>" |
125 |
" <menuitem action='Properties'/>" |
126 |
" <menuitem action='Dimensions'/>" |
127 |
" <menuitem action='Remove'/>" |
128 |
" </popup>" |
129 |
" <popup name='PopupMenuOutsideRegion'>" |
130 |
" <menuitem action='Add'/>" |
131 |
" </popup>" |
132 |
"</ui>"; |
133 |
uiManager->add_ui_from_string(ui_info); |
134 |
|
135 |
popup_menu_inside_region = dynamic_cast<Gtk::Menu*>( |
136 |
uiManager->get_widget("/PopupMenuInsideRegion")); |
137 |
popup_menu_outside_region = dynamic_cast<Gtk::Menu*>( |
138 |
uiManager->get_widget("/PopupMenuOutsideRegion")); |
139 |
|
140 |
add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | |
141 |
Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK); |
142 |
|
143 |
dimensionManager.region_to_be_changed_signal.connect( |
144 |
region_to_be_changed_signal.make_slot() |
145 |
); |
146 |
dimensionManager.region_changed_signal.connect( |
147 |
region_changed_signal.make_slot() |
148 |
); |
149 |
dimensionManager.region_changed_signal.connect( |
150 |
sigc::hide( |
151 |
sigc::mem_fun(*this, &RegionChooser::on_dimension_manager_changed) |
152 |
) |
153 |
); |
154 |
keyboard_key_hit_signal.connect( |
155 |
sigc::mem_fun(*this, &RegionChooser::on_note_on_event) |
156 |
); |
157 |
keyboard_key_released_signal.connect( |
158 |
sigc::mem_fun(*this, &RegionChooser::on_note_off_event) |
159 |
); |
160 |
} |
161 |
|
162 |
RegionChooser::~RegionChooser() |
163 |
{ |
164 |
} |
165 |
|
166 |
template<class T> inline std::string ToString(T o) { |
167 |
std::stringstream ss; |
168 |
ss << o; |
169 |
return ss.str(); |
170 |
} |
171 |
|
172 |
void RegionChooser::on_note_on_event(int key, int velocity) { |
173 |
draw_region(key, key+1, activeKeyColor); |
174 |
m_VirtKeybVelocityLabel.set_text(ToString(velocity)); |
175 |
} |
176 |
|
177 |
void RegionChooser::on_note_off_event(int key, int velocity) { |
178 |
if (is_black_key(key)) |
179 |
draw_region(key, key+1, black); |
180 |
else |
181 |
draw_region(key, key+1, white); |
182 |
m_VirtKeybOffVelocityLabel.set_text(ToString(velocity)); |
183 |
} |
184 |
|
185 |
void RegionChooser::on_realize() |
186 |
{ |
187 |
// We need to call the base on_realize() |
188 |
Gtk::DrawingArea::on_realize(); |
189 |
|
190 |
// Now we can allocate any additional resources we need |
191 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
192 |
gc = Gdk::GC::create(window); |
193 |
window->clear(); |
194 |
} |
195 |
|
196 |
bool RegionChooser::on_expose_event(GdkEventExpose* event) |
197 |
{ |
198 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
199 |
window->clear(); |
200 |
const int h = KEYBOARD_HEIGHT; |
201 |
const int w = get_width() - 1; |
202 |
const int bh = int(h * 0.55); |
203 |
|
204 |
Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc(); |
205 |
Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc(); |
206 |
|
207 |
window->draw_rectangle(black, false, 0, h1, w, h - 1); |
208 |
gc->set_foreground(grey1); |
209 |
int x1 = int(w * 20.5 / 128.0 + 0.5); |
210 |
int x2 = int(w * 109.5 / 128.0 + 0.5); |
211 |
window->draw_rectangle(gc, true, 1, h1 + 1, |
212 |
x1 - 1, h - 2); |
213 |
window->draw_rectangle(white, true, x1 + 1, h1 + 1, x2 - x1 - 1, h - 2); |
214 |
window->draw_rectangle(gc, true, x2 + 1, h1 + 1, |
215 |
w - x2 - 1, h - 2); |
216 |
for (int i = 0 ; i < 128 ; i++) { |
217 |
int note = (i + 3) % 12; |
218 |
int x = int(w * i / 128.0 + 0.5); |
219 |
|
220 |
if (note == 1 || note == 4 || note == 6 || note == 9 || note == 11) { |
221 |
int x2 = int(w * (i + 0.5) / 128.0 + 0.5); |
222 |
window->draw_line(black, x2, h1 + bh, x2, h1 + h); |
223 |
|
224 |
int x3 = int(w * (i + 1) / 128.0 + 0.5); |
225 |
window->draw_rectangle(black, true, x, h1 + 1, x3 - x + 1, bh); |
226 |
} else if (note == 3 || note == 8) { |
227 |
window->draw_line(black, x, h1 + 1, x, h1 + h); |
228 |
} |
229 |
if (note == 3) draw_digit(i); |
230 |
} |
231 |
|
232 |
if (instrument) { |
233 |
int i = 0; |
234 |
gig::Region *next_region; |
235 |
int x3 = -1; |
236 |
for (gig::Region *r = regions.first() ; r ; r = next_region) { |
237 |
|
238 |
if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
239 |
next_region = regions.next(); |
240 |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
241 |
int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
242 |
window->draw_line(black, x3, 0, x2, 0); |
243 |
window->draw_line(black, x3, h1 - 1, x2, h1 - 1); |
244 |
window->draw_line(black, x2, 1, x2, h1 - 2); |
245 |
window->draw_rectangle(white, true, x3 + 1, 1, x2 - x3 - 1, h1 - 2); |
246 |
x3 = -1; |
247 |
} |
248 |
i++; |
249 |
} |
250 |
|
251 |
for (gig::Region *r = regions.first() ; r ; r = regions.next()) { |
252 |
int x = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
253 |
window->draw_line(black, x, 1, x, h1 - 2); |
254 |
} |
255 |
|
256 |
if (region) { |
257 |
int x1 = int(w * (region->KeyRange.low) / 128.0 + 0.5); |
258 |
int x2 = int(w * (region->KeyRange.high + 1) / 128.0 + 0.5); |
259 |
gc->set_foreground(red); |
260 |
window->draw_rectangle(gc, true, x1 + 1, 1, x2 - x1 - 1, h1 - 2); |
261 |
} |
262 |
} |
263 |
return true; |
264 |
} |
265 |
|
266 |
|
267 |
void RegionChooser::on_size_request(GtkRequisition* requisition) |
268 |
{ |
269 |
*requisition = GtkRequisition(); |
270 |
requisition->height = KEYBOARD_HEIGHT + REGION_BLOCK_HEIGHT; |
271 |
requisition->width = 500; |
272 |
} |
273 |
|
274 |
bool RegionChooser::is_black_key(int key) { |
275 |
const int note = (key + 3) % 12; |
276 |
return note == 1 || note == 4 || note == 6 || note == 9 || note == 11; |
277 |
} |
278 |
|
279 |
void RegionChooser::draw_digit(int key) { |
280 |
const int h = KEYBOARD_HEIGHT; |
281 |
const int w = get_width() - 1; |
282 |
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(get_pango_context()); |
283 |
char buf[30]; |
284 |
sprintf(buf, "<span size=\"8000\">%d</span>", key / 12 - 1); |
285 |
layout->set_markup(buf); |
286 |
Pango::Rectangle rectangle = layout->get_logical_extents(); |
287 |
double text_w = double(rectangle.get_width()) / Pango::SCALE; |
288 |
double text_h = double(rectangle.get_height()) / Pango::SCALE; |
289 |
double x = w * (key + 0.75) / 128.0; |
290 |
get_window()->draw_layout(get_style()->get_black_gc(), int(x - text_w / 2 + 1), |
291 |
int(h1 + h - text_h + 0.5), layout); |
292 |
} |
293 |
|
294 |
void RegionChooser::draw_region(int from, int to, const Gdk::Color& color) |
295 |
{ |
296 |
const int h = KEYBOARD_HEIGHT; |
297 |
const int w = get_width() - 1; |
298 |
const int bh = int(h * 0.55); |
299 |
|
300 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
301 |
gc->set_foreground(color); |
302 |
|
303 |
for (int i = from ; i < to ; i++) { |
304 |
int note = (i + 3) % 12; |
305 |
int x = int(w * i / 128.0 + 0.5) + 1; |
306 |
int x2 = int(w * (i + 1.5) / 128.0 + 0.5); |
307 |
int x3 = int(w * (i + 1) / 128.0 + 0.5); |
308 |
int x4 = int(w * (i - 0.5) / 128.0 + 0.5); |
309 |
int w1 = x3 - x; |
310 |
switch (note) { |
311 |
case 0: case 5: case 10: |
312 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); |
313 |
window->draw_rectangle(gc, true, x4 + 1, h1 + bh + 1, x2 - x4 - 1, h - bh - 2); |
314 |
break; |
315 |
case 2: case 7: |
316 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); |
317 |
window->draw_rectangle(gc, true, x4 + 1, h1 + bh + 1, x3 - x4 - 1, h - bh - 2); |
318 |
break; |
319 |
case 3: case 8: |
320 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); |
321 |
window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2); |
322 |
if (note == 3) draw_digit(i); |
323 |
break; |
324 |
default: |
325 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1); |
326 |
break; |
327 |
} |
328 |
} |
329 |
} |
330 |
|
331 |
void RegionChooser::set_instrument(gig::Instrument* instrument) |
332 |
{ |
333 |
this->instrument = instrument; |
334 |
regions.update(instrument); |
335 |
region = regions.first(); |
336 |
queue_draw(); |
337 |
region_selected(); |
338 |
dimensionManager.set_region(region); |
339 |
} |
340 |
|
341 |
bool RegionChooser::on_button_release_event(GdkEventButton* event) |
342 |
{ |
343 |
const int k = int(event->x / (get_width() - 1) * 128.0); |
344 |
|
345 |
// handle-note off on virtual keyboard |
346 |
if (event->type == GDK_BUTTON_RELEASE) { |
347 |
int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 : |
348 |
int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1; |
349 |
if (velocity <= 0) velocity = 1; |
350 |
switch (m_VirtKeybModeChoice.get_value()) { |
351 |
case VIRT_KEYBOARD_MODE_CHORD: |
352 |
if (event->y >= REGION_BLOCK_HEIGHT) |
353 |
keyboard_key_released_signal.emit(k, velocity); |
354 |
break; |
355 |
case VIRT_KEYBOARD_MODE_NORMAL: |
356 |
default: |
357 |
if (currentActiveKey >= 0 && currentActiveKey <= 127) { |
358 |
keyboard_key_released_signal.emit(currentActiveKey, velocity); |
359 |
currentActiveKey = -1; |
360 |
} |
361 |
break; |
362 |
} |
363 |
} |
364 |
|
365 |
if (resize.active) { |
366 |
get_window()->pointer_ungrab(event->time); |
367 |
resize.active = false; |
368 |
|
369 |
if (resize.mode == resize.moving_high_limit) { |
370 |
if (resize.region->KeyRange.high != resize.pos - 1) { |
371 |
instrument_struct_to_be_changed_signal.emit(instrument); |
372 |
resize.region->SetKeyRange( |
373 |
resize.region->KeyRange.low, // low |
374 |
resize.pos - 1 // high |
375 |
); |
376 |
regions.update(instrument); |
377 |
instrument_changed.emit(); |
378 |
instrument_struct_changed_signal.emit(instrument); |
379 |
} |
380 |
} else if (resize.mode == resize.moving_low_limit) { |
381 |
if (resize.region->KeyRange.low != resize.pos) { |
382 |
instrument_struct_to_be_changed_signal.emit(instrument); |
383 |
resize.region->SetKeyRange( |
384 |
resize.pos, // low |
385 |
resize.region->KeyRange.high // high |
386 |
); |
387 |
regions.update(instrument); |
388 |
instrument_changed.emit(); |
389 |
instrument_struct_changed_signal.emit(instrument); |
390 |
} |
391 |
} |
392 |
|
393 |
if (!is_in_resize_zone(event->x, event->y) && cursor_is_resize) { |
394 |
get_window()->set_cursor(); |
395 |
cursor_is_resize = false; |
396 |
} |
397 |
} else if (move.active) { |
398 |
get_window()->pointer_ungrab(event->time); |
399 |
move.active = false; |
400 |
|
401 |
if (move.pos) { |
402 |
instrument_struct_to_be_changed_signal.emit(instrument); |
403 |
region->SetKeyRange( |
404 |
region->KeyRange.low + move.pos, |
405 |
region->KeyRange.high + move.pos |
406 |
); |
407 |
regions.update(instrument); |
408 |
instrument_changed.emit(); |
409 |
instrument_struct_changed_signal.emit(instrument); |
410 |
} |
411 |
|
412 |
if (is_in_resize_zone(event->x, event->y)) { |
413 |
get_window()->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW)); |
414 |
cursor_is_resize = true; |
415 |
} |
416 |
} |
417 |
return true; |
418 |
} |
419 |
|
420 |
bool RegionChooser::on_button_press_event(GdkEventButton* event) |
421 |
{ |
422 |
if (!instrument) return true; |
423 |
|
424 |
const int k = int(event->x / (get_width() - 1) * 128.0); |
425 |
|
426 |
if (event->type == GDK_BUTTON_PRESS) { |
427 |
if (event->y >= REGION_BLOCK_HEIGHT) { |
428 |
int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 : |
429 |
int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1; |
430 |
currentActiveKey = k; |
431 |
keyboard_key_hit_signal.emit(k, velocity); |
432 |
} |
433 |
} |
434 |
|
435 |
if (event->y >= REGION_BLOCK_HEIGHT) return true; |
436 |
if (event->type == GDK_BUTTON_PRESS && event->button == 3) { |
437 |
gig::Region* r = get_region(k); |
438 |
if (r) { |
439 |
region = r; |
440 |
queue_draw(); |
441 |
region_selected(); |
442 |
dimensionManager.set_region(region); |
443 |
popup_menu_inside_region->popup(event->button, event->time); |
444 |
} else { |
445 |
new_region_pos = k; |
446 |
popup_menu_outside_region->popup(event->button, event->time); |
447 |
} |
448 |
} else { |
449 |
if (is_in_resize_zone(event->x, event->y)) { |
450 |
get_window()->pointer_grab(false, |
451 |
Gdk::BUTTON_RELEASE_MASK | |
452 |
Gdk::POINTER_MOTION_MASK | |
453 |
Gdk::POINTER_MOTION_HINT_MASK, |
454 |
Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW), event->time); |
455 |
resize.active = true; |
456 |
} else { |
457 |
gig::Region* r = get_region(k); |
458 |
if (r) { |
459 |
region = r; |
460 |
queue_draw(); |
461 |
region_selected(); |
462 |
dimensionManager.set_region(region); |
463 |
|
464 |
get_window()->pointer_grab(false, |
465 |
Gdk::BUTTON_RELEASE_MASK | |
466 |
Gdk::POINTER_MOTION_MASK | |
467 |
Gdk::POINTER_MOTION_HINT_MASK, |
468 |
Gdk::Cursor(Gdk::FLEUR), event->time); |
469 |
move.active = true; |
470 |
move.from_x = event->x; |
471 |
move.pos = 0; |
472 |
} |
473 |
} |
474 |
} |
475 |
return true; |
476 |
} |
477 |
|
478 |
gig::Region* RegionChooser::get_region(int key) |
479 |
{ |
480 |
gig::Region* prev_region = 0; |
481 |
gig::Region* next_region; |
482 |
for (gig::Region *r = regions.first() ; r ; r = next_region) { |
483 |
next_region = regions.next(); |
484 |
|
485 |
if (key < r->KeyRange.low) return 0; |
486 |
if (key <= r->KeyRange.high) { |
487 |
move.touch_left = prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low; |
488 |
move.touch_right = next_region && r->KeyRange.high + 1 == next_region->KeyRange.low; |
489 |
return r; |
490 |
} |
491 |
prev_region = r; |
492 |
} |
493 |
return 0; |
494 |
} |
495 |
|
496 |
void RegionChooser::motion_resize_region(int x, int y) |
497 |
{ |
498 |
const int w = get_width() - 1; |
499 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
500 |
|
501 |
int k = int(double(x) / w * 128.0 + 0.5); |
502 |
|
503 |
if (k < resize.min) k = resize.min; |
504 |
else if (k > resize.max) k = resize.max; |
505 |
|
506 |
if (k != resize.pos) { |
507 |
if (resize.mode == resize.undecided) { |
508 |
if (k < resize.pos) { |
509 |
// edit high limit of prev_region |
510 |
resize.max = resize.region->KeyRange.low; |
511 |
resize.region = resize.prev_region; |
512 |
resize.mode = resize.moving_high_limit; |
513 |
} else { |
514 |
// edit low limit of region |
515 |
resize.min = resize.prev_region->KeyRange.high + 1; |
516 |
resize.mode = resize.moving_low_limit; |
517 |
} |
518 |
} |
519 |
Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc(); |
520 |
Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc(); |
521 |
if (region == resize.region) { |
522 |
gc->set_foreground(red); |
523 |
white = gc; |
524 |
} |
525 |
Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL); |
526 |
int prevx = int(w * resize.pos / 128.0 + 0.5); |
527 |
x = int(w * k / 128.0 + 0.5); |
528 |
|
529 |
if (resize.mode == resize.moving_high_limit) { |
530 |
if (k > resize.pos) { |
531 |
window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2); |
532 |
window->draw_line(black, prevx, 0, x, 0); |
533 |
window->draw_line(black, prevx, h1 - 1, x, h1 - 1); |
534 |
} else { |
535 |
int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0); |
536 |
window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1); |
537 |
} |
538 |
} else { |
539 |
if (k < resize.pos) { |
540 |
window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2); |
541 |
window->draw_line(black, x, 0, prevx, 0); |
542 |
window->draw_line(black, x, h1 - 1, prevx, h1 - 1); |
543 |
} else { |
544 |
int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0); |
545 |
window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1); |
546 |
} |
547 |
} |
548 |
window->draw_line(black, x, 1, x, h1 - 2); |
549 |
resize.pos = k; |
550 |
} |
551 |
} |
552 |
|
553 |
void RegionChooser::motion_move_region(int x, int y) |
554 |
{ |
555 |
const int w = get_width() - 1; |
556 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
557 |
|
558 |
int k = int(double(x - move.from_x) / w * 128.0 + 0.5); |
559 |
if (k == move.pos) return; |
560 |
int new_k; |
561 |
bool new_touch_left; |
562 |
bool new_touch_right; |
563 |
int a = 0; |
564 |
if (k > move.pos) { |
565 |
for (gig::Region* r = regions.first() ; ; r = regions.next()) { |
566 |
if (r != region) { |
567 |
int b = r ? r->KeyRange.low : 128; |
568 |
|
569 |
// gap: from a to b (not inclusive b) |
570 |
|
571 |
if (region->KeyRange.high + move.pos >= b) { |
572 |
// not found the current gap yet, just continue |
573 |
} else { |
574 |
|
575 |
if (a > region->KeyRange.low + k) { |
576 |
// this gap is too far to the right, break |
577 |
break; |
578 |
} |
579 |
|
580 |
int newhigh = std::min(region->KeyRange.high + k, b - 1); |
581 |
int newlo = newhigh - (region->KeyRange.high - region->KeyRange.low); |
582 |
|
583 |
if (newlo >= a) { |
584 |
// yes it fits - it's a candidate |
585 |
new_k = newlo - region->KeyRange.low; |
586 |
new_touch_left = a > 0 && a == newlo; |
587 |
new_touch_right = b < 128 && newhigh + 1 == b; |
588 |
} |
589 |
} |
590 |
if (!r) break; |
591 |
a = r->KeyRange.high + 1; |
592 |
} |
593 |
} |
594 |
} else { |
595 |
for (gig::Region* r = regions.first() ; ; r = regions.next()) { |
596 |
if (r != region) { |
597 |
int b = r ? r->KeyRange.low : 128; |
598 |
|
599 |
// gap from a to b (not inclusive b) |
600 |
|
601 |
if (region->KeyRange.high + k >= b) { |
602 |
// not found the current gap yet, just continue |
603 |
} else { |
604 |
|
605 |
if (a > region->KeyRange.low + move.pos) { |
606 |
// this gap is too far to the right, break |
607 |
break; |
608 |
} |
609 |
|
610 |
int newlo = std::max(region->KeyRange.low + k, a); |
611 |
int newhigh = newlo + (region->KeyRange.high - region->KeyRange.low); |
612 |
|
613 |
if (newhigh < b) { |
614 |
// yes it fits - break as the first one is the best |
615 |
new_k = newlo - region->KeyRange.low; |
616 |
new_touch_left = a > 0 && a == newlo; |
617 |
new_touch_right = b < 128 && newhigh + 1 == b; |
618 |
break; |
619 |
} |
620 |
} |
621 |
if (!r) break; |
622 |
a = r->KeyRange.high + 1; |
623 |
} |
624 |
} |
625 |
} |
626 |
k = new_k; |
627 |
if (k == move.pos) return; |
628 |
|
629 |
Glib::RefPtr<const Gdk::GC> bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL); |
630 |
int prevx = int(w * (move.pos + region->KeyRange.low) / 128.0 + 0.5); |
631 |
x = int(w * (k + region->KeyRange.low) / 128.0 + 0.5); |
632 |
int prevx2 = int(w * (move.pos + region->KeyRange.high + 1) / 128.0 + 0.5); |
633 |
int x2 = int(w * (k + region->KeyRange.high + 1) / 128.0 + 0.5); |
634 |
Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc(); |
635 |
gc->set_foreground(red); |
636 |
|
637 |
if (!new_touch_left) window->draw_line(black, x, 1, x, h1 - 2); |
638 |
if (!new_touch_right) window->draw_line(black, x2, 1, x2, h1 - 2); |
639 |
|
640 |
if (k > move.pos) { |
641 |
window->draw_rectangle(bg, true, prevx + (move.touch_left ? 1 : 0), 0, |
642 |
std::min(x, prevx2 + 1 - (move.touch_right ? 1 : 0)) - |
643 |
(prevx + (move.touch_left ? 1 : 0)), h1); |
644 |
|
645 |
window->draw_line(black, std::max(x, prevx2 + 1), 0, x2, 0); |
646 |
window->draw_line(black, std::max(x, prevx2 + 1), h1 - 1, x2, h1 - 1); |
647 |
window->draw_rectangle(gc, true, std::max(x + 1, prevx2), 1, |
648 |
x2 - std::max(x + 1, prevx2), h1 - 2); |
649 |
} else { |
650 |
window->draw_rectangle(bg, true, std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), 0, |
651 |
prevx2 + 1 - (move.touch_right ? 1 : 0) - |
652 |
std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), h1); |
653 |
|
654 |
window->draw_line(black, x, 0, std::min(x2, prevx - 1), 0); |
655 |
window->draw_line(black, x, h1 - 1, std::min(x2, prevx - 1), h1 - 1); |
656 |
|
657 |
window->draw_rectangle(gc, true, x + 1, 1, std::min(x2 - 1, prevx) - x, h1 - 2); |
658 |
} |
659 |
|
660 |
move.pos = k; |
661 |
move.touch_left = new_touch_left; |
662 |
move.touch_right = new_touch_right; |
663 |
} |
664 |
|
665 |
|
666 |
bool RegionChooser::on_motion_notify_event(GdkEventMotion* event) |
667 |
{ |
668 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
669 |
int x, y; |
670 |
Gdk::ModifierType state = Gdk::ModifierType(0); |
671 |
window->get_pointer(x, y, state); |
672 |
|
673 |
// handle virtual MIDI keyboard |
674 |
if (m_VirtKeybModeChoice.get_value() != VIRT_KEYBOARD_MODE_CHORD && |
675 |
currentActiveKey > 0 && |
676 |
event->y >= REGION_BLOCK_HEIGHT && |
677 |
event->y < REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT) |
678 |
{ |
679 |
const int k = int(event->x / (get_width() - 1) * 128.0); |
680 |
int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 : |
681 |
int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1; |
682 |
if (velocity <= 0) velocity = 1; |
683 |
keyboard_key_released_signal.emit(currentActiveKey, velocity); |
684 |
currentActiveKey = k; |
685 |
keyboard_key_hit_signal.emit(k, velocity); |
686 |
} |
687 |
|
688 |
if (resize.active) { |
689 |
motion_resize_region(x, y); |
690 |
} else if (move.active) { |
691 |
motion_move_region(x, y); |
692 |
} else { |
693 |
if (is_in_resize_zone(x, y)) { |
694 |
if (!cursor_is_resize) { |
695 |
window->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW)); |
696 |
cursor_is_resize = true; |
697 |
} |
698 |
} else if (cursor_is_resize) { |
699 |
window->set_cursor(); |
700 |
cursor_is_resize = false; |
701 |
} |
702 |
} |
703 |
|
704 |
return true; |
705 |
} |
706 |
|
707 |
bool RegionChooser::is_in_resize_zone(double x, double y) { |
708 |
const int w = get_width() - 1; |
709 |
|
710 |
if (instrument && y >= 0 && y <= h1) { |
711 |
gig::Region* prev_region = 0; |
712 |
gig::Region* next_region; |
713 |
for (gig::Region* r = regions.first(); r ; r = next_region) { |
714 |
next_region = regions.next(); |
715 |
|
716 |
int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
717 |
if (x <= lo - 2) break; |
718 |
if (x < lo + 2) { |
719 |
resize.region = r; |
720 |
resize.pos = r->KeyRange.low; |
721 |
resize.max = r->KeyRange.high; |
722 |
|
723 |
if (prev_region && prev_region->KeyRange.high + 1 == r->KeyRange.low) { |
724 |
// we don't know yet if it's the high limit of |
725 |
// prev_region or the low limit of r that's going |
726 |
// to be edited |
727 |
resize.mode = resize.undecided; |
728 |
resize.min = prev_region->KeyRange.low + 1; |
729 |
resize.prev_region = prev_region; |
730 |
return resize.min != resize.max; |
731 |
} |
732 |
|
733 |
// edit low limit |
734 |
resize.mode = resize.moving_low_limit; |
735 |
resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0; |
736 |
return resize.min != resize.max; |
737 |
} |
738 |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
739 |
int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
740 |
if (x <= hi - 2) break; |
741 |
if (x < hi + 2) { |
742 |
// edit high limit |
743 |
resize.region = r; |
744 |
resize.pos = r->KeyRange.high + 1; |
745 |
resize.mode = resize.moving_high_limit; |
746 |
resize.min = r->KeyRange.low + 1; |
747 |
resize.max = next_region ? next_region->KeyRange.low : 128; |
748 |
return resize.min != resize.max; |
749 |
} |
750 |
} |
751 |
prev_region = r; |
752 |
} |
753 |
} |
754 |
return false; |
755 |
} |
756 |
|
757 |
sigc::signal<void>& RegionChooser::signal_region_selected() |
758 |
{ |
759 |
return region_selected; |
760 |
} |
761 |
|
762 |
sigc::signal<void>& RegionChooser::signal_instrument_changed() |
763 |
{ |
764 |
return instrument_changed; |
765 |
} |
766 |
|
767 |
void RegionChooser::show_region_properties() |
768 |
{ |
769 |
if (!region) return; |
770 |
Gtk::Dialog dialog("Region Properties", true /*modal*/); |
771 |
// add "Keygroup" checkbox |
772 |
Gtk::CheckButton checkBoxKeygroup("Member of a Keygroup (Exclusive Group)"); |
773 |
checkBoxKeygroup.set_active(region->KeyGroup); |
774 |
dialog.get_vbox()->pack_start(checkBoxKeygroup); |
775 |
checkBoxKeygroup.show(); |
776 |
// add "Keygroup" spinbox |
777 |
Gtk::Adjustment adjustment(1, 1, pow(2,32)); |
778 |
Gtk::SpinButton spinBox(adjustment); |
779 |
if (region->KeyGroup) spinBox.set_value(region->KeyGroup); |
780 |
dialog.get_vbox()->pack_start(spinBox); |
781 |
spinBox.show(); |
782 |
// add OK and CANCEL buttons to the dialog |
783 |
dialog.add_button(Gtk::Stock::OK, 0); |
784 |
dialog.add_button(Gtk::Stock::CANCEL, 1); |
785 |
dialog.show_all_children(); |
786 |
if (!dialog.run()) { // OK selected ... |
787 |
region->KeyGroup = |
788 |
(checkBoxKeygroup.get_active()) ? spinBox.get_value_as_int() : 0; |
789 |
} |
790 |
} |
791 |
|
792 |
void RegionChooser::add_region() |
793 |
{ |
794 |
instrument_struct_to_be_changed_signal.emit(instrument); |
795 |
|
796 |
region = instrument->AddRegion(); |
797 |
region->SetKeyRange(new_region_pos, new_region_pos); |
798 |
|
799 |
instrument_struct_changed_signal.emit(instrument); |
800 |
regions.update(instrument); |
801 |
|
802 |
queue_draw(); |
803 |
region_selected(); |
804 |
dimensionManager.set_region(region); |
805 |
instrument_changed(); |
806 |
} |
807 |
|
808 |
void RegionChooser::delete_region() |
809 |
{ |
810 |
instrument_struct_to_be_changed_signal.emit(instrument); |
811 |
instrument->DeleteRegion(region); |
812 |
instrument_struct_changed_signal.emit(instrument); |
813 |
regions.update(instrument); |
814 |
|
815 |
region = 0; |
816 |
queue_draw(); |
817 |
region_selected(); |
818 |
dimensionManager.set_region(region); |
819 |
instrument_changed(); |
820 |
} |
821 |
|
822 |
void RegionChooser::manage_dimensions() |
823 |
{ |
824 |
gig::Region* region = get_region(); |
825 |
if (!region) return; |
826 |
dimensionManager.show(region); |
827 |
} |
828 |
|
829 |
void RegionChooser::on_dimension_manager_changed() { |
830 |
region_selected(); |
831 |
instrument_changed(); |
832 |
} |
833 |
|
834 |
sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_to_be_changed() { |
835 |
return instrument_struct_to_be_changed_signal; |
836 |
} |
837 |
|
838 |
sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_changed() { |
839 |
return instrument_struct_changed_signal; |
840 |
} |
841 |
|
842 |
sigc::signal<void, gig::Region*>& RegionChooser::signal_region_to_be_changed() { |
843 |
return region_to_be_changed_signal; |
844 |
} |
845 |
|
846 |
sigc::signal<void, gig::Region*>& RegionChooser::signal_region_changed_signal() { |
847 |
return region_changed_signal; |
848 |
} |
849 |
|
850 |
sigc::signal<void, int/*key*/, int/*velocity*/>& RegionChooser::signal_keyboard_key_hit() { |
851 |
return keyboard_key_hit_signal; |
852 |
} |
853 |
|
854 |
sigc::signal<void, int/*key*/, int/*velocity*/>& RegionChooser::signal_keyboard_key_released() { |
855 |
return keyboard_key_released_signal; |
856 |
} |