1 |
/* |
/* |
2 |
* Copyright (C) 2006, 2007 Andreas Persson |
* Copyright (C) 2006-2008 Andreas Persson |
3 |
* |
* |
4 |
* This program is free software; you can redistribute it and/or |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License as |
* modify it under the terms of the GNU General Public License as |
18 |
*/ |
*/ |
19 |
|
|
20 |
#include "regionchooser.h" |
#include "regionchooser.h" |
21 |
|
#include <algorithm> |
22 |
#include <gdkmm/cursor.h> |
#include <gdkmm/cursor.h> |
23 |
#include <gtkmm/stock.h> |
#include <gtkmm/stock.h> |
24 |
#include <gtkmm/spinbutton.h> |
#include <gtkmm/spinbutton.h> |
25 |
#include <gtkmm/dialog.h> |
#include <gtkmm/dialog.h> |
|
#include <libintl.h> |
|
26 |
#include <math.h> |
#include <math.h> |
27 |
|
|
28 |
#define _(String) gettext(String) |
#include "global.h" |
29 |
|
|
30 |
|
void SortedRegions::update(gig::Instrument* instrument) { |
31 |
|
// Usually, the regions in a gig file are ordered after their key |
32 |
|
// range, but there are files where they are not. The |
33 |
|
// RegionChooser code needs a sorted list of regions. |
34 |
|
regions.clear(); |
35 |
|
if (instrument) { |
36 |
|
for (gig::Region *r = instrument->GetFirstRegion() ; |
37 |
|
r ; |
38 |
|
r = instrument->GetNextRegion()) { |
39 |
|
regions.push_back(r); |
40 |
|
} |
41 |
|
sort(regions.begin(), regions.end(), *this); |
42 |
|
} |
43 |
|
} |
44 |
|
|
45 |
|
gig::Region* SortedRegions::first() { |
46 |
|
region_iterator = regions.begin(); |
47 |
|
return region_iterator == regions.end() ? 0 : *region_iterator; |
48 |
|
} |
49 |
|
|
50 |
|
gig::Region* SortedRegions::next() { |
51 |
|
region_iterator++; |
52 |
|
return region_iterator == regions.end() ? 0 : *region_iterator; |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
RegionChooser::RegionChooser() |
RegionChooser::RegionChooser() |
58 |
{ |
{ |
60 |
|
|
61 |
red = Gdk::Color("#8070ff"); |
red = Gdk::Color("#8070ff"); |
62 |
grey1 = Gdk::Color("#b0b0b0"); |
grey1 = Gdk::Color("#b0b0b0"); |
63 |
|
activeKeyColor = Gdk::Color("#ff0000"); |
64 |
|
white = Gdk::Color("#ffffff"); |
65 |
|
black = Gdk::Color("#000000"); |
66 |
|
|
67 |
colormap->alloc_color(red); |
colormap->alloc_color(red); |
68 |
colormap->alloc_color(grey1); |
colormap->alloc_color(grey1); |
69 |
|
colormap->alloc_color(activeKeyColor); |
70 |
|
colormap->alloc_color(white); |
71 |
|
colormap->alloc_color(black); |
72 |
instrument = 0; |
instrument = 0; |
73 |
region = 0; |
region = 0; |
74 |
resize.active = false; |
resize.active = false; |
75 |
move.active = false; |
move.active = false; |
76 |
cursor_is_resize = false; |
cursor_is_resize = false; |
77 |
h1 = 20; |
h1 = 20; |
|
width = 800; |
|
78 |
|
|
79 |
actionGroup = Gtk::ActionGroup::create(); |
actionGroup = Gtk::ActionGroup::create(); |
80 |
actionGroup->add(Gtk::Action::create("Properties", |
actionGroup->add(Gtk::Action::create("Properties", |
128 |
{ |
{ |
129 |
} |
} |
130 |
|
|
131 |
|
void RegionChooser::on_note_on_event(int key, int velocity) { |
132 |
|
draw_region(key, key+1, activeKeyColor); |
133 |
|
} |
134 |
|
|
135 |
|
void RegionChooser::on_note_off_event(int key, int velocity) { |
136 |
|
if (is_black_key(key)) |
137 |
|
draw_region(key, key+1, black); |
138 |
|
else |
139 |
|
draw_region(key, key+1, white); |
140 |
|
} |
141 |
|
|
142 |
void RegionChooser::on_realize() |
void RegionChooser::on_realize() |
143 |
{ |
{ |
144 |
// We need to call the base on_realize() |
// We need to call the base on_realize() |
155 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
Glib::RefPtr<Gdk::Window> window = get_window(); |
156 |
window->clear(); |
window->clear(); |
157 |
const int h = 40; |
const int h = 40; |
158 |
const int w = width - 1; |
const int w = get_width() - 1; |
159 |
const int bh = int(h * 0.55); |
const int bh = int(h * 0.55); |
160 |
|
|
161 |
Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc(); |
Glib::RefPtr<const Gdk::GC> black = get_style()->get_black_gc(); |
189 |
} |
} |
190 |
if (note == 3) { |
if (note == 3) { |
191 |
char buf[30]; |
char buf[30]; |
192 |
sprintf(buf, "<span size=\"x-small\">%d</span>", octave); |
sprintf(buf, "<span size=\"8000\">%d</span>", octave); |
193 |
layout->set_markup(buf); |
layout->set_markup(buf); |
194 |
Pango::Rectangle rectangle = layout->get_logical_extents(); |
Pango::Rectangle rectangle = layout->get_logical_extents(); |
195 |
double text_w = double(rectangle.get_width()) / Pango::SCALE; |
double text_w = double(rectangle.get_width()) / Pango::SCALE; |
205 |
int i = 0; |
int i = 0; |
206 |
gig::Region *next_region; |
gig::Region *next_region; |
207 |
int x3 = -1; |
int x3 = -1; |
208 |
for (gig::Region *r = instrument->GetFirstRegion() ; |
for (gig::Region *r = regions.first() ; r ; r = next_region) { |
|
r ; |
|
|
r = next_region) { |
|
209 |
|
|
210 |
if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
211 |
next_region = instrument->GetNextRegion(); |
next_region = regions.next(); |
212 |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
213 |
int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
214 |
window->draw_line(black, x3, 0, x2, 0); |
window->draw_line(black, x3, 0, x2, 0); |
220 |
i++; |
i++; |
221 |
} |
} |
222 |
|
|
223 |
for (gig::Region *r = instrument->GetFirstRegion() ; |
for (gig::Region *r = regions.first() ; r ; r = regions.next()) { |
|
r ; |
|
|
r = instrument->GetNextRegion()) { |
|
224 |
int x = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
int x = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
225 |
window->draw_line(black, x, 1, x, h1 - 2); |
window->draw_line(black, x, 1, x, h1 - 2); |
226 |
} |
} |
243 |
requisition->width = 500; |
requisition->width = 500; |
244 |
} |
} |
245 |
|
|
246 |
|
bool RegionChooser::is_black_key(int key) { |
247 |
|
const int note = (key + 3) % 12; |
248 |
|
return note == 1 || note == 4 || note == 6 || note == 9 || note == 11; |
249 |
|
} |
250 |
|
|
|
// not used |
|
251 |
void RegionChooser::draw_region(int from, int to, const Gdk::Color& color) |
void RegionChooser::draw_region(int from, int to, const Gdk::Color& color) |
252 |
{ |
{ |
253 |
const int h = 40; |
const int h = 40; |
254 |
const int w = width; |
const int w = get_width(); |
255 |
const int bh = int(h * 0.55); |
const int bh = int(h * 0.55); |
256 |
|
|
257 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
Glib::RefPtr<Gdk::Window> window = get_window(); |
266 |
int w1 = x3 - x; |
int w1 = x3 - x; |
267 |
switch (note) { |
switch (note) { |
268 |
case 0: case 5: case 10: |
case 0: case 5: case 10: |
269 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); |
window->draw_rectangle(gc, true, x - 1, h1 + 1, w1, bh); |
270 |
window->draw_rectangle(gc, true, x4, h1 + bh + 1, x2 - x4, h - bh - 2); |
window->draw_rectangle(gc, true, x4, h1 + bh + 1, x2 - x4 - 1, h - bh - 2); |
271 |
break; |
break; |
272 |
case 2: case 7: |
case 2: case 7: |
273 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); |
274 |
window->draw_rectangle(gc, true, x4, h1 + bh + 1, x3 - x4, h - bh - 2); |
window->draw_rectangle(gc, true, x4 - 1, h1 + bh + 1, x3 - x4 + 1, h - bh - 2); |
275 |
break; |
break; |
276 |
case 3: case 8: |
case 3: case 8: |
277 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); |
278 |
window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2); |
window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2); |
279 |
break; |
break; |
280 |
default: |
default: |
281 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1); |
window->draw_rectangle(gc, true, x, h1 + 1, w1 - 1, bh - 1); |
282 |
break; |
break; |
283 |
} |
} |
284 |
} |
} |
287 |
void RegionChooser::set_instrument(gig::Instrument* instrument) |
void RegionChooser::set_instrument(gig::Instrument* instrument) |
288 |
{ |
{ |
289 |
this->instrument = instrument; |
this->instrument = instrument; |
290 |
region = instrument ? instrument->GetFirstRegion() : 0; |
regions.update(instrument); |
291 |
|
region = regions.first(); |
292 |
queue_draw(); |
queue_draw(); |
293 |
region_selected(); |
region_selected(); |
294 |
} |
} |
306 |
resize.region->KeyRange.low, // low |
resize.region->KeyRange.low, // low |
307 |
resize.pos - 1 // high |
resize.pos - 1 // high |
308 |
); |
); |
309 |
|
regions.update(instrument); |
310 |
instrument_changed.emit(); |
instrument_changed.emit(); |
311 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
312 |
} |
} |
317 |
resize.pos, // low |
resize.pos, // low |
318 |
resize.region->KeyRange.high // high |
resize.region->KeyRange.high // high |
319 |
); |
); |
320 |
|
regions.update(instrument); |
321 |
instrument_changed.emit(); |
instrument_changed.emit(); |
322 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
323 |
} |
} |
337 |
region->KeyRange.low + move.pos, |
region->KeyRange.low + move.pos, |
338 |
region->KeyRange.high + move.pos |
region->KeyRange.high + move.pos |
339 |
); |
); |
340 |
|
regions.update(instrument); |
341 |
|
instrument_changed.emit(); |
342 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
343 |
} |
} |
344 |
|
|
354 |
{ |
{ |
355 |
if (!instrument) return true; |
if (!instrument) return true; |
356 |
|
|
357 |
int k = int(event->x / (width - 1) * 128.0); |
int k = int(event->x / (get_width() - 1) * 128.0); |
358 |
|
|
359 |
if (event->type == GDK_BUTTON_PRESS && event->button == 3) { |
if (event->type == GDK_BUTTON_PRESS && event->button == 3) { |
360 |
gig::Region* r = get_region(k); |
gig::Region* r = get_region(k); |
400 |
{ |
{ |
401 |
gig::Region* prev_region = 0; |
gig::Region* prev_region = 0; |
402 |
gig::Region* next_region; |
gig::Region* next_region; |
403 |
for (gig::Region *r = instrument->GetFirstRegion() ; r ; |
for (gig::Region *r = regions.first() ; r ; r = next_region) { |
404 |
r = next_region) { |
next_region = regions.next(); |
|
next_region = instrument->GetNextRegion(); |
|
405 |
|
|
406 |
if (key < r->KeyRange.low) return 0; |
if (key < r->KeyRange.low) return 0; |
407 |
if (key <= r->KeyRange.high) { |
if (key <= r->KeyRange.high) { |
416 |
|
|
417 |
void RegionChooser::motion_resize_region(int x, int y) |
void RegionChooser::motion_resize_region(int x, int y) |
418 |
{ |
{ |
419 |
const int w = width - 1; |
const int w = get_width() - 1; |
420 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
Glib::RefPtr<Gdk::Window> window = get_window(); |
421 |
|
|
422 |
int k = int(double(x) / w * 128.0 + 0.5); |
int k = int(double(x) / w * 128.0 + 0.5); |
473 |
|
|
474 |
void RegionChooser::motion_move_region(int x, int y) |
void RegionChooser::motion_move_region(int x, int y) |
475 |
{ |
{ |
476 |
const int w = width - 1; |
const int w = get_width() - 1; |
477 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
Glib::RefPtr<Gdk::Window> window = get_window(); |
478 |
|
|
479 |
int k = int(double(x - move.from_x) / w * 128.0 + 0.5); |
int k = int(double(x - move.from_x) / w * 128.0 + 0.5); |
483 |
bool new_touch_right; |
bool new_touch_right; |
484 |
int a = 0; |
int a = 0; |
485 |
if (k > move.pos) { |
if (k > move.pos) { |
486 |
for (gig::Region* r = instrument->GetFirstRegion() ; ; |
for (gig::Region* r = regions.first() ; ; r = regions.next()) { |
|
r = instrument->GetNextRegion()) { |
|
487 |
if (r != region) { |
if (r != region) { |
488 |
int b = r ? r->KeyRange.low : 128; |
int b = r ? r->KeyRange.low : 128; |
489 |
|
|
513 |
} |
} |
514 |
} |
} |
515 |
} else { |
} else { |
516 |
for (gig::Region* r = instrument->GetFirstRegion() ; ; |
for (gig::Region* r = regions.first() ; ; r = regions.next()) { |
|
r = instrument->GetNextRegion()) { |
|
517 |
if (r != region) { |
if (r != region) { |
518 |
int b = r ? r->KeyRange.low : 128; |
int b = r ? r->KeyRange.low : 128; |
519 |
|
|
611 |
} |
} |
612 |
|
|
613 |
bool RegionChooser::is_in_resize_zone(double x, double y) { |
bool RegionChooser::is_in_resize_zone(double x, double y) { |
614 |
const int w = width - 1; |
const int w = get_width() - 1; |
615 |
|
|
616 |
if (instrument && y >= 0 && y <= h1) { |
if (instrument && y >= 0 && y <= h1) { |
617 |
gig::Region* prev_region = 0; |
gig::Region* prev_region = 0; |
618 |
gig::Region* next_region; |
gig::Region* next_region; |
619 |
for (gig::Region* r = instrument->GetFirstRegion() ; r ; r = next_region) { |
for (gig::Region* r = regions.first(); r ; r = next_region) { |
620 |
next_region = instrument->GetNextRegion(); |
next_region = regions.next(); |
621 |
|
|
622 |
int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
623 |
if (x <= lo - 2) break; |
if (x <= lo - 2) break; |
633 |
resize.mode = resize.undecided; |
resize.mode = resize.undecided; |
634 |
resize.min = prev_region->KeyRange.low + 1; |
resize.min = prev_region->KeyRange.low + 1; |
635 |
resize.prev_region = prev_region; |
resize.prev_region = prev_region; |
636 |
return true; |
return resize.min != resize.max; |
637 |
} |
} |
638 |
|
|
639 |
// edit low limit |
// edit low limit |
640 |
resize.mode = resize.moving_low_limit; |
resize.mode = resize.moving_low_limit; |
641 |
resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0; |
resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0; |
642 |
return true; |
return resize.min != resize.max; |
643 |
} |
} |
644 |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
645 |
int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
651 |
resize.mode = resize.moving_high_limit; |
resize.mode = resize.moving_high_limit; |
652 |
resize.min = r->KeyRange.low + 1; |
resize.min = r->KeyRange.low + 1; |
653 |
resize.max = next_region ? next_region->KeyRange.low : 128; |
resize.max = next_region ? next_region->KeyRange.low : 128; |
654 |
return true; |
return resize.min != resize.max; |
655 |
} |
} |
656 |
} |
} |
657 |
prev_region = r; |
prev_region = r; |
660 |
return false; |
return false; |
661 |
} |
} |
662 |
|
|
663 |
sigc::signal<void> RegionChooser::signal_region_selected() |
sigc::signal<void>& RegionChooser::signal_region_selected() |
664 |
{ |
{ |
665 |
return region_selected; |
return region_selected; |
666 |
} |
} |
667 |
|
|
668 |
sigc::signal<void> RegionChooser::signal_instrument_changed() |
sigc::signal<void>& RegionChooser::signal_instrument_changed() |
669 |
{ |
{ |
670 |
return instrument_changed; |
return instrument_changed; |
671 |
} |
} |
703 |
region->SetKeyRange(new_region_pos, new_region_pos); |
region->SetKeyRange(new_region_pos, new_region_pos); |
704 |
|
|
705 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
706 |
|
regions.update(instrument); |
707 |
|
|
708 |
queue_draw(); |
queue_draw(); |
709 |
region_selected(); |
region_selected(); |
715 |
instrument_struct_to_be_changed_signal.emit(instrument); |
instrument_struct_to_be_changed_signal.emit(instrument); |
716 |
instrument->DeleteRegion(region); |
instrument->DeleteRegion(region); |
717 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
718 |
|
regions.update(instrument); |
719 |
|
|
720 |
region = 0; |
region = 0; |
721 |
queue_draw(); |
queue_draw(); |
735 |
instrument_changed(); |
instrument_changed(); |
736 |
} |
} |
737 |
|
|
738 |
sigc::signal<void, gig::Instrument*> RegionChooser::signal_instrument_struct_to_be_changed() { |
sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_to_be_changed() { |
739 |
return instrument_struct_to_be_changed_signal; |
return instrument_struct_to_be_changed_signal; |
740 |
} |
} |
741 |
|
|
742 |
sigc::signal<void, gig::Instrument*> RegionChooser::signal_instrument_struct_changed() { |
sigc::signal<void, gig::Instrument*>& RegionChooser::signal_instrument_struct_changed() { |
743 |
return instrument_struct_changed_signal; |
return instrument_struct_changed_signal; |
744 |
} |
} |
745 |
|
|
746 |
sigc::signal<void, gig::Region*> RegionChooser::signal_region_to_be_changed() { |
sigc::signal<void, gig::Region*>& RegionChooser::signal_region_to_be_changed() { |
747 |
return region_to_be_changed_signal; |
return region_to_be_changed_signal; |
748 |
} |
} |
749 |
|
|
750 |
sigc::signal<void, gig::Region*> RegionChooser::signal_region_changed_signal() { |
sigc::signal<void, gig::Region*>& RegionChooser::signal_region_changed_signal() { |
751 |
return region_changed_signal; |
return region_changed_signal; |
752 |
} |
} |