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> |
27 |
|
|
28 |
#include "global.h" |
#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 |
{ |
{ |
59 |
Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap(); |
Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap(); |
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(); |
162 |
Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc(); |
Glib::RefPtr<const Gdk::GC> white = get_style()->get_white_gc(); |
163 |
|
|
|
Glib::RefPtr<Pango::Context> context = get_pango_context(); |
|
|
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(context); |
|
|
|
|
164 |
window->draw_rectangle(black, false, 0, h1, w, h - 1); |
window->draw_rectangle(black, false, 0, h1, w, h - 1); |
165 |
gc->set_foreground(grey1); |
gc->set_foreground(grey1); |
166 |
int x1 = int(w * 20.5 / 128.0 + 0.5); |
int x1 = int(w * 20.5 / 128.0 + 0.5); |
170 |
window->draw_rectangle(white, true, x1 + 1, h1 + 1, x2 - x1 - 1, h - 2); |
window->draw_rectangle(white, true, x1 + 1, h1 + 1, x2 - x1 - 1, h - 2); |
171 |
window->draw_rectangle(gc, true, x2 + 1, h1 + 1, |
window->draw_rectangle(gc, true, x2 + 1, h1 + 1, |
172 |
w - x2 - 1, h - 2); |
w - x2 - 1, h - 2); |
|
int octave = -1; |
|
173 |
for (int i = 0 ; i < 128 ; i++) { |
for (int i = 0 ; i < 128 ; i++) { |
174 |
int note = (i + 3) % 12; |
int note = (i + 3) % 12; |
175 |
int x = int(w * i / 128.0 + 0.5); |
int x = int(w * i / 128.0 + 0.5); |
183 |
} else if (note == 3 || note == 8) { |
} else if (note == 3 || note == 8) { |
184 |
window->draw_line(black, x, h1 + 1, x, h1 + h); |
window->draw_line(black, x, h1 + 1, x, h1 + h); |
185 |
} |
} |
186 |
if (note == 3) { |
if (note == 3) draw_digit(i); |
|
char buf[30]; |
|
|
sprintf(buf, "<span size=\"8000\">%d</span>", octave); |
|
|
layout->set_markup(buf); |
|
|
Pango::Rectangle rectangle = layout->get_logical_extents(); |
|
|
double text_w = double(rectangle.get_width()) / Pango::SCALE; |
|
|
double text_h = double(rectangle.get_height()) / Pango::SCALE; |
|
|
double x2 = w * (i + 0.75) / 128.0; |
|
|
window->draw_layout(black, int(x2 - text_w / 2 + 1), |
|
|
int(h1 + h - text_h + 0.5), layout); |
|
|
octave++; |
|
|
} |
|
187 |
} |
} |
188 |
|
|
189 |
if (instrument) { |
if (instrument) { |
190 |
int i = 0; |
int i = 0; |
191 |
gig::Region *next_region; |
gig::Region *next_region; |
192 |
int x3 = -1; |
int x3 = -1; |
193 |
for (gig::Region *r = instrument->GetFirstRegion() ; |
for (gig::Region *r = regions.first() ; r ; r = next_region) { |
|
r ; |
|
|
r = next_region) { |
|
194 |
|
|
195 |
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); |
196 |
next_region = instrument->GetNextRegion(); |
next_region = regions.next(); |
197 |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
198 |
int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
199 |
window->draw_line(black, x3, 0, x2, 0); |
window->draw_line(black, x3, 0, x2, 0); |
205 |
i++; |
i++; |
206 |
} |
} |
207 |
|
|
208 |
for (gig::Region *r = instrument->GetFirstRegion() ; |
for (gig::Region *r = regions.first() ; r ; r = regions.next()) { |
|
r ; |
|
|
r = instrument->GetNextRegion()) { |
|
209 |
int x = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
int x = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
210 |
window->draw_line(black, x, 1, x, h1 - 2); |
window->draw_line(black, x, 1, x, h1 - 2); |
211 |
} |
} |
228 |
requisition->width = 500; |
requisition->width = 500; |
229 |
} |
} |
230 |
|
|
231 |
|
bool RegionChooser::is_black_key(int key) { |
232 |
|
const int note = (key + 3) % 12; |
233 |
|
return note == 1 || note == 4 || note == 6 || note == 9 || note == 11; |
234 |
|
} |
235 |
|
|
236 |
|
void RegionChooser::draw_digit(int key) { |
237 |
|
const int h = 40; |
238 |
|
const int w = get_width() - 1; |
239 |
|
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(get_pango_context()); |
240 |
|
char buf[30]; |
241 |
|
sprintf(buf, "<span size=\"8000\">%d</span>", key / 12 - 1); |
242 |
|
layout->set_markup(buf); |
243 |
|
Pango::Rectangle rectangle = layout->get_logical_extents(); |
244 |
|
double text_w = double(rectangle.get_width()) / Pango::SCALE; |
245 |
|
double text_h = double(rectangle.get_height()) / Pango::SCALE; |
246 |
|
double x = w * (key + 0.75) / 128.0; |
247 |
|
get_window()->draw_layout(get_style()->get_black_gc(), int(x - text_w / 2 + 1), |
248 |
|
int(h1 + h - text_h + 0.5), layout); |
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() - 1; |
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(); |
262 |
int x = int(w * i / 128.0 + 0.5) + 1; |
int x = int(w * i / 128.0 + 0.5) + 1; |
263 |
int x2 = int(w * (i + 1.5) / 128.0 + 0.5); |
int x2 = int(w * (i + 1.5) / 128.0 + 0.5); |
264 |
int x3 = int(w * (i + 1) / 128.0 + 0.5); |
int x3 = int(w * (i + 1) / 128.0 + 0.5); |
265 |
int x4 = int(w * (i - 0.5) / 128 + 0.5) + 1; |
int x4 = int(w * (i - 0.5) / 128.0 + 0.5); |
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, 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 + 1, 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 |
|
if (note == 3) draw_digit(i); |
280 |
break; |
break; |
281 |
default: |
default: |
282 |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1); |
window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1); |
288 |
void RegionChooser::set_instrument(gig::Instrument* instrument) |
void RegionChooser::set_instrument(gig::Instrument* instrument) |
289 |
{ |
{ |
290 |
this->instrument = instrument; |
this->instrument = instrument; |
291 |
region = instrument ? instrument->GetFirstRegion() : 0; |
regions.update(instrument); |
292 |
|
region = regions.first(); |
293 |
queue_draw(); |
queue_draw(); |
294 |
region_selected(); |
region_selected(); |
295 |
} |
} |
307 |
resize.region->KeyRange.low, // low |
resize.region->KeyRange.low, // low |
308 |
resize.pos - 1 // high |
resize.pos - 1 // high |
309 |
); |
); |
310 |
|
regions.update(instrument); |
311 |
instrument_changed.emit(); |
instrument_changed.emit(); |
312 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
313 |
} |
} |
318 |
resize.pos, // low |
resize.pos, // low |
319 |
resize.region->KeyRange.high // high |
resize.region->KeyRange.high // high |
320 |
); |
); |
321 |
|
regions.update(instrument); |
322 |
instrument_changed.emit(); |
instrument_changed.emit(); |
323 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
324 |
} |
} |
338 |
region->KeyRange.low + move.pos, |
region->KeyRange.low + move.pos, |
339 |
region->KeyRange.high + move.pos |
region->KeyRange.high + move.pos |
340 |
); |
); |
341 |
|
regions.update(instrument); |
342 |
instrument_changed.emit(); |
instrument_changed.emit(); |
343 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
344 |
} |
} |
355 |
{ |
{ |
356 |
if (!instrument) return true; |
if (!instrument) return true; |
357 |
|
|
358 |
int k = int(event->x / (width - 1) * 128.0); |
int k = int(event->x / (get_width() - 1) * 128.0); |
359 |
|
|
360 |
if (event->type == GDK_BUTTON_PRESS && event->button == 3) { |
if (event->type == GDK_BUTTON_PRESS && event->button == 3) { |
361 |
gig::Region* r = get_region(k); |
gig::Region* r = get_region(k); |
401 |
{ |
{ |
402 |
gig::Region* prev_region = 0; |
gig::Region* prev_region = 0; |
403 |
gig::Region* next_region; |
gig::Region* next_region; |
404 |
for (gig::Region *r = instrument->GetFirstRegion() ; r ; |
for (gig::Region *r = regions.first() ; r ; r = next_region) { |
405 |
r = next_region) { |
next_region = regions.next(); |
|
next_region = instrument->GetNextRegion(); |
|
406 |
|
|
407 |
if (key < r->KeyRange.low) return 0; |
if (key < r->KeyRange.low) return 0; |
408 |
if (key <= r->KeyRange.high) { |
if (key <= r->KeyRange.high) { |
417 |
|
|
418 |
void RegionChooser::motion_resize_region(int x, int y) |
void RegionChooser::motion_resize_region(int x, int y) |
419 |
{ |
{ |
420 |
const int w = width - 1; |
const int w = get_width() - 1; |
421 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
Glib::RefPtr<Gdk::Window> window = get_window(); |
422 |
|
|
423 |
int k = int(double(x) / w * 128.0 + 0.5); |
int k = int(double(x) / w * 128.0 + 0.5); |
474 |
|
|
475 |
void RegionChooser::motion_move_region(int x, int y) |
void RegionChooser::motion_move_region(int x, int y) |
476 |
{ |
{ |
477 |
const int w = width - 1; |
const int w = get_width() - 1; |
478 |
Glib::RefPtr<Gdk::Window> window = get_window(); |
Glib::RefPtr<Gdk::Window> window = get_window(); |
479 |
|
|
480 |
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); |
484 |
bool new_touch_right; |
bool new_touch_right; |
485 |
int a = 0; |
int a = 0; |
486 |
if (k > move.pos) { |
if (k > move.pos) { |
487 |
for (gig::Region* r = instrument->GetFirstRegion() ; ; |
for (gig::Region* r = regions.first() ; ; r = regions.next()) { |
|
r = instrument->GetNextRegion()) { |
|
488 |
if (r != region) { |
if (r != region) { |
489 |
int b = r ? r->KeyRange.low : 128; |
int b = r ? r->KeyRange.low : 128; |
490 |
|
|
514 |
} |
} |
515 |
} |
} |
516 |
} else { |
} else { |
517 |
for (gig::Region* r = instrument->GetFirstRegion() ; ; |
for (gig::Region* r = regions.first() ; ; r = regions.next()) { |
|
r = instrument->GetNextRegion()) { |
|
518 |
if (r != region) { |
if (r != region) { |
519 |
int b = r ? r->KeyRange.low : 128; |
int b = r ? r->KeyRange.low : 128; |
520 |
|
|
612 |
} |
} |
613 |
|
|
614 |
bool RegionChooser::is_in_resize_zone(double x, double y) { |
bool RegionChooser::is_in_resize_zone(double x, double y) { |
615 |
const int w = width - 1; |
const int w = get_width() - 1; |
616 |
|
|
617 |
if (instrument && y >= 0 && y <= h1) { |
if (instrument && y >= 0 && y <= h1) { |
618 |
gig::Region* prev_region = 0; |
gig::Region* prev_region = 0; |
619 |
gig::Region* next_region; |
gig::Region* next_region; |
620 |
for (gig::Region* r = instrument->GetFirstRegion() ; r ; r = next_region) { |
for (gig::Region* r = regions.first(); r ; r = next_region) { |
621 |
next_region = instrument->GetNextRegion(); |
next_region = regions.next(); |
622 |
|
|
623 |
int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5); |
624 |
if (x <= lo - 2) break; |
if (x <= lo - 2) break; |
634 |
resize.mode = resize.undecided; |
resize.mode = resize.undecided; |
635 |
resize.min = prev_region->KeyRange.low + 1; |
resize.min = prev_region->KeyRange.low + 1; |
636 |
resize.prev_region = prev_region; |
resize.prev_region = prev_region; |
637 |
return true; |
return resize.min != resize.max; |
638 |
} |
} |
639 |
|
|
640 |
// edit low limit |
// edit low limit |
641 |
resize.mode = resize.moving_low_limit; |
resize.mode = resize.moving_low_limit; |
642 |
resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0; |
resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0; |
643 |
return true; |
return resize.min != resize.max; |
644 |
} |
} |
645 |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { |
646 |
int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); |
652 |
resize.mode = resize.moving_high_limit; |
resize.mode = resize.moving_high_limit; |
653 |
resize.min = r->KeyRange.low + 1; |
resize.min = r->KeyRange.low + 1; |
654 |
resize.max = next_region ? next_region->KeyRange.low : 128; |
resize.max = next_region ? next_region->KeyRange.low : 128; |
655 |
return true; |
return resize.min != resize.max; |
656 |
} |
} |
657 |
} |
} |
658 |
prev_region = r; |
prev_region = r; |
704 |
region->SetKeyRange(new_region_pos, new_region_pos); |
region->SetKeyRange(new_region_pos, new_region_pos); |
705 |
|
|
706 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
707 |
|
regions.update(instrument); |
708 |
|
|
709 |
queue_draw(); |
queue_draw(); |
710 |
region_selected(); |
region_selected(); |
716 |
instrument_struct_to_be_changed_signal.emit(instrument); |
instrument_struct_to_be_changed_signal.emit(instrument); |
717 |
instrument->DeleteRegion(region); |
instrument->DeleteRegion(region); |
718 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
719 |
|
regions.update(instrument); |
720 |
|
|
721 |
region = 0; |
region = 0; |
722 |
queue_draw(); |
queue_draw(); |