--- gigedit/trunk/src/gigedit/regionchooser.cpp 2007/07/22 15:07:08 1262 +++ gigedit/trunk/src/gigedit/regionchooser.cpp 2010/11/21 12:38:41 2151 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006, 2007 Andreas Persson + * Copyright (C) 2006-2010 Andreas Persson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -18,30 +18,107 @@ */ #include "regionchooser.h" + +#include +#include +#include + +#include +#include #include #include #include #include -#include -#include -#define _(String) gettext(String) +#include "global.h" -RegionChooser::RegionChooser() -{ - Glib::RefPtr colormap = get_default_colormap(); +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 10) || GTKMM_MAJOR_VERSION < 2 + +#define create_cairo_context() \ + gobj() ? Cairo::RefPtr( \ + new Cairo::Context(gdk_cairo_create(get_window()->gobj()))) : \ + Cairo::RefPtr() + +namespace Gdk { + namespace Cairo { + void set_source_color(const ::Cairo::RefPtr< ::Cairo::Context >& cr, + const Gdk::Color& color) { + gdk_cairo_set_source_color(cr->cobj(), + const_cast(color.gobj())); + } + } +} +#endif + +#define REGION_BLOCK_HEIGHT 20 +#define KEYBOARD_HEIGHT 40 + +void SortedRegions::update(gig::Instrument* instrument) { + // Usually, the regions in a gig file are ordered after their key + // range, but there are files where they are not. The + // RegionChooser code needs a sorted list of regions. + regions.clear(); + if (instrument) { + for (gig::Region* r = instrument->GetFirstRegion() ; + r ; + r = instrument->GetNextRegion()) { + regions.push_back(r); + } + sort(regions.begin(), regions.end(), *this); + } +} + +gig::Region* SortedRegions::first() { + region_iterator = regions.begin(); + return region_iterator == regions.end() ? 0 : *region_iterator; +} +gig::Region* SortedRegions::next() { + region_iterator++; + return region_iterator == regions.end() ? 0 : *region_iterator; +} + + + +RegionChooser::RegionChooser() : + m_VirtKeybModeChoice(_("Virtual Keyboard Mode")), + currentActiveKey(-1) +{ red = Gdk::Color("#8070ff"); grey1 = Gdk::Color("#b0b0b0"); + activeKeyColor = Gdk::Color("#ff0000"); + white = Gdk::Color("#ffffff"); + black = Gdk::Color("#000000"); - colormap->alloc_color(red); - colormap->alloc_color(grey1); instrument = 0; region = 0; resize.active = false; + move.active = false; cursor_is_resize = false; - h1 = 20; - width = 800; + h1 = REGION_BLOCK_HEIGHT; + + // properties of the virtual keyboard + { + const char* choices[] = { _("normal"), _("chord"), NULL }; + static const virt_keyboard_mode_t values[] = { + VIRT_KEYBOARD_MODE_NORMAL, + VIRT_KEYBOARD_MODE_CHORD + }; + m_VirtKeybModeChoice.set_choices(choices, values); + m_VirtKeybModeChoice.set_value(VIRT_KEYBOARD_MODE_NORMAL); + } + m_VirtKeybVelocityLabelDescr.set_text(_("Note-On Velocity:")); + m_VirtKeybVelocityLabel.set_text("-"); + m_VirtKeybOffVelocityLabelDescr.set_text(_("Note-Off Velocity:")); + m_VirtKeybOffVelocityLabel.set_text("-"); + m_VirtKeybPropsBox.pack_start(m_VirtKeybModeChoice.label, Gtk::PACK_SHRINK); + m_VirtKeybPropsBox.pack_start(m_VirtKeybModeChoice.widget, Gtk::PACK_SHRINK); + m_VirtKeybPropsBox.pack_start(m_VirtKeybVelocityLabelDescr, Gtk::PACK_SHRINK); + m_VirtKeybPropsBox.pack_start(m_VirtKeybVelocityLabel, Gtk::PACK_SHRINK); + m_VirtKeybPropsBox.pack_start(m_VirtKeybOffVelocityLabelDescr, Gtk::PACK_SHRINK); + m_VirtKeybPropsBox.pack_start(m_VirtKeybOffVelocityLabel, Gtk::PACK_SHRINK); + m_VirtKeybPropsBox.set_spacing(10); + m_VirtKeybPropsBox.show(); actionGroup = Gtk::ActionGroup::create(); actionGroup->add(Gtk::Action::create("Properties", @@ -78,8 +155,22 @@ add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK); - dimensionManager.articulation_changed_signal.connect( - sigc::mem_fun(*this, &RegionChooser::on_dimension_manager_changed) + dimensionManager.region_to_be_changed_signal.connect( + region_to_be_changed_signal.make_slot() + ); + dimensionManager.region_changed_signal.connect( + region_changed_signal.make_slot() + ); + dimensionManager.region_changed_signal.connect( + sigc::hide( + sigc::mem_fun(*this, &RegionChooser::on_dimension_manager_changed) + ) + ); + keyboard_key_hit_signal.connect( + sigc::mem_fun(*this, &RegionChooser::on_note_on_event) + ); + keyboard_key_released_signal.connect( + sigc::mem_fun(*this, &RegionChooser::on_note_off_event) ); } @@ -87,103 +178,137 @@ { } -void RegionChooser::on_realize() -{ - // We need to call the base on_realize() - Gtk::DrawingArea::on_realize(); +template inline std::string ToString(T o) { + std::stringstream ss; + ss << o; + return ss.str(); +} - // Now we can allocate any additional resources we need - Glib::RefPtr window = get_window(); - gc = Gdk::GC::create(window); - window->clear(); +void RegionChooser::on_note_on_event(int key, int velocity) { + draw_key(key, activeKeyColor); + m_VirtKeybVelocityLabel.set_text(ToString(velocity)); +} + +void RegionChooser::on_note_off_event(int key, int velocity) { + if (is_black_key(key)) { + draw_key(key, black); + } else { + draw_key(key, key >= 21 && key <= 108 ? white : grey1); + } + m_VirtKeybOffVelocityLabel.set_text(ToString(velocity)); } + bool RegionChooser::on_expose_event(GdkEventExpose* event) { Glib::RefPtr window = get_window(); - window->clear(); - const int h = 40; - const int w = width - 1; - const int bh = int(h * 0.55); - - Glib::RefPtr black = get_style()->get_black_gc(); - Glib::RefPtr white = get_style()->get_white_gc(); - - Glib::RefPtr context = get_pango_context(); - Glib::RefPtr layout = Pango::Layout::create(context); - - window->draw_rectangle(black, false, 0, h1, w, h - 1); - gc->set_foreground(grey1); - int x1 = int(w * 20.5 / 128.0 + 0.5); - int x2 = int(w * 109.5 / 128.0 + 0.5); - window->draw_rectangle(gc, true, 1, h1 + 1, - x1 - 1, h - 2); - window->draw_rectangle(white, true, x1 + 1, h1 + 1, x2 - x1 - 1, h - 2); - window->draw_rectangle(gc, true, x2 + 1, h1 + 1, - w - x2 - 1, h - 2); - int octave = -1; - for (int i = 0 ; i < 128 ; i++) { - int note = (i + 3) % 12; - int x = int(w * i / 128.0 + 0.5); - - if (note == 1 || note == 4 || note == 6 || note == 9 || note == 11) { - int x2 = int(w * (i + 0.5) / 128.0 + 0.5); - window->draw_line(black, x2, h1 + bh, x2, h1 + h); - - int x3 = int(w * (i + 1) / 128.0 + 0.5); - window->draw_rectangle(black, true, x, h1 + 1, x3 - x + 1, bh); - } else if (note == 3 || note == 8) { - window->draw_line(black, x, h1 + 1, x, h1 + h); - } - if (note == 3) { - char buf[30]; - sprintf(buf, "%d", 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++; - } - } + if (window) { + Cairo::RefPtr cr = window->create_cairo_context(); + if (event) { + cr->rectangle(event->area.x, event->area.y, + event->area.width, event->area.height); + cr->clip(); + } + const int h = KEYBOARD_HEIGHT; + const int w = get_width() - 1; + const int bh = int(h * 0.55); + + cr->save(); + cr->set_line_width(1); + + const Gdk::Color bg = get_style()->get_bg(Gtk::STATE_NORMAL); + Gdk::Cairo::set_source_color(cr, bg); + cr->paint(); + + Gdk::Cairo::set_source_color(cr, black); + cr->rectangle(0.5, h1 + 0.5, w, h - 1); + cr->stroke(); + + int x1 = int(w * 20.5 / 128.0 + 0.5); + int x2 = int(w * 109.5 / 128.0 + 0.5); + + Gdk::Cairo::set_source_color(cr, grey1); + cr->rectangle(1, h1 + 1, x1 - 1, h - 2); + cr->fill(); + + Gdk::Cairo::set_source_color(cr, white); + cr->rectangle(x1 + 1, h1 + 1, x2 - x1 - 1, h - 2); + cr->fill(); + + Gdk::Cairo::set_source_color(cr, grey1); + cr->rectangle(x2 + 1, h1 + 1, w - x2 - 1, h - 2); + cr->fill(); + + Gdk::Cairo::set_source_color(cr, black); + for (int i = 0 ; i < 128 ; i++) { + int note = (i + 3) % 12; + int x = int(w * i / 128.0 + 0.5); + + if (note == 1 || note == 4 || note == 6 || + note == 9 || note == 11) { + int x2 = int(w * (i + 0.5) / 128.0 + 0.5); + cr->move_to(x2 + 0.5, h1 + bh + 0.5); + cr->line_to(x2 + 0.5, h1 + h - 1); + cr->stroke(); + + int x3 = int(w * (i + 1) / 128.0 + 0.5); + cr->rectangle(x, h1 + 1, x3 - x + 1, bh); + cr->fill(); + } else if (note == 3 || note == 8) { + cr->move_to(x + 0.5, h1 + 1); + cr->line_to(x + 0.5, h1 + h - 1); + cr->stroke(); + + if (note == 3) draw_digit(i); + } + } + + if (instrument) { + int i = 0; + gig::Region* next_region; + int x3 = -1; + for (gig::Region* r = regions.first() ; r ; r = next_region) { + + if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5); + next_region = regions.next(); + if (!next_region || + r->KeyRange.high + 1 != next_region->KeyRange.low) { + int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); + cr->move_to(x3, 0.5); + cr->line_to(x2 + 0.5, 0.5); + cr->line_to(x2 + 0.5, h1 - 0.5); + cr->line_to(x3, h1 - 0.5); + cr->stroke(); + + Gdk::Cairo::set_source_color(cr, white); + cr->rectangle(x3 + 1, 1, x2 - x3 - 1, h1 - 2); + cr->fill(); + Gdk::Cairo::set_source_color(cr, black); - if (instrument) { - int i = 0; - gig::Region *next_region; - int x3 = -1; - for (gig::Region *r = instrument->GetFirstRegion() ; - r ; - r = next_region) { + x3 = -1; + } + i++; + } - if (x3 < 0) x3 = int(w * (r->KeyRange.low) / 128.0 + 0.5); - next_region = instrument->GetNextRegion(); - if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { - int x2 = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); - window->draw_line(black, x3, 0, x2, 0); - window->draw_line(black, x3, h1 - 1, x2, h1 - 1); - window->draw_line(black, x2, 1, x2, h1 - 2); - window->draw_rectangle(white, true, x3 + 1, 1, x2 - x3 - 1, h1 - 2); - x3 = -1; + for (gig::Region* r = regions.first() ; r ; r = regions.next()) { + int x = int(w * (r->KeyRange.low) / 128.0 + 0.5); + cr->move_to(x + 0.5, 1); + cr->line_to(x + 0.5, h1 - 1); + cr->stroke(); } - i++; - } - for (gig::Region *r = instrument->GetFirstRegion() ; - r ; - r = instrument->GetNextRegion()) { - int x = int(w * (r->KeyRange.low) / 128.0 + 0.5); - window->draw_line(black, x, 1, x, h1 - 2); + if (region) { + int x1 = int(w * (region->KeyRange.low) / 128.0 + 0.5); + int x2 = int(w * (region->KeyRange.high + 1) / 128.0 + 0.5); + Gdk::Cairo::set_source_color(cr, red); + cr->rectangle(x1 + 1, 1, x2 - x1 - 1, h1 - 2); + cr->fill(); + } } - if (region) { - int x1 = int(w * (region->KeyRange.low) / 128.0 + 0.5); - int x2 = int(w * (region->KeyRange.high + 1) / 128.0 + 0.5); - gc->set_foreground(red); - window->draw_rectangle(gc, true, x1 + 1, 1, x2 - x1 - 1, h1 - 2); - } + cr->restore(); } + return true; } @@ -191,71 +316,137 @@ void RegionChooser::on_size_request(GtkRequisition* requisition) { *requisition = GtkRequisition(); - requisition->height = 40 + 20; + requisition->height = KEYBOARD_HEIGHT + REGION_BLOCK_HEIGHT; requisition->width = 500; } +bool RegionChooser::is_black_key(int key) { + const int note = (key + 3) % 12; + return note == 1 || note == 4 || note == 6 || note == 9 || note == 11; +} + +void RegionChooser::draw_digit(int key) { + const int h = KEYBOARD_HEIGHT; + const int w = get_width() - 1; + Glib::RefPtr layout = Pango::Layout::create(get_pango_context()); + char buf[30]; + sprintf(buf, "%d", key / 12 - 1); + 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 x = w * (key + 0.75) / 128.0; + Cairo::RefPtr cr = get_window()->create_cairo_context(); + Gdk::Cairo::set_source_color(cr, black); + cr->move_to(int(x - text_w / 2 + 1), int(h1 + h - text_h + 0.5)); +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 16) || GTKMM_MAJOR_VERSION < 2 + pango_cairo_show_layout(cr->cobj(), layout->gobj()); +#else + layout->show_in_cairo_context(cr); +#endif +} -// not used -void RegionChooser::draw_region(int from, int to, const Gdk::Color& color) +void RegionChooser::draw_key(int key, const Gdk::Color& color) { - const int h = 40; - const int w = width; + const int h = KEYBOARD_HEIGHT; + const int w = get_width() - 1; const int bh = int(h * 0.55); - Glib::RefPtr window = get_window(); - gc->set_foreground(color); + Cairo::RefPtr cr = get_window()->create_cairo_context(); + Gdk::Cairo::set_source_color(cr, color); - for (int i = from ; i < to ; i++) { - int note = (i + 3) % 12; - int x = int(w * i / 128.0 + 0.5) + 1; - int x2 = int(w * (i + 1.5) / 128.0 + 0.5); - int x3 = int(w * (i + 1) / 128.0 + 0.5); - int x4 = int(w * (i - 0.5) / 128 + 0.5) + 1; - int w1 = x3 - x; - switch (note) { - case 0: case 5: case 10: - window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); - window->draw_rectangle(gc, true, x4, h1 + bh + 1, x2 - x4, h - bh - 2); - break; - case 2: case 7: - window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); - window->draw_rectangle(gc, true, x4, h1 + bh + 1, x3 - x4, h - bh - 2); - break; - case 3: case 8: - window->draw_rectangle(gc, true, x, h1 + 1, w1, bh); - window->draw_rectangle(gc, true, x, h1 + bh + 1, x2 - x, h - bh - 2); - break; - default: - window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1); - break; - } + int note = (key + 3) % 12; + int x = int(w * key / 128.0 + 0.5) + 1; + int x2 = int(w * (key + 1.5) / 128.0 + 0.5); + int x3 = int(w * (key + 1) / 128.0 + 0.5); + int x4 = int(w * (key - 0.5) / 128.0 + 0.5); + int w1 = x3 - x; + switch (note) { + case 0: case 5: case 10: + cr->rectangle(x, h1 + 1, w1, bh); + cr->fill(); + cr->rectangle(x4 + 1, h1 + bh + 1, x2 - x4 - 1, h - bh - 2); + cr->fill(); + break; + case 2: case 7: + cr->rectangle(x, h1 + 1, w1, bh); + cr->fill(); + cr->rectangle(x4 + 1, h1 + bh + 1, x3 - x4 - 1, h - bh - 2); + cr->fill(); + break; + case 3: case 8: + cr->rectangle(x, h1 + 1, w1, bh); + cr->fill(); + cr->rectangle(x, h1 + bh + 1, x2 - x, h - bh - 2); + cr->fill(); + if (note == 3) draw_digit(key); + break; + default: + cr->rectangle(x, h1 + 1, w1, bh - 1); + cr->fill(); + break; } } void RegionChooser::set_instrument(gig::Instrument* instrument) { this->instrument = instrument; - region = instrument ? instrument->GetFirstRegion() : 0; + regions.update(instrument); + region = regions.first(); queue_draw(); region_selected(); + dimensionManager.set_region(region); } bool RegionChooser::on_button_release_event(GdkEventButton* event) { + const int k = int(event->x / (get_width() - 1) * 128.0); + + // handle-note off on virtual keyboard + if (event->type == GDK_BUTTON_RELEASE) { + int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 : + int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1; + if (velocity <= 0) velocity = 1; + switch (m_VirtKeybModeChoice.get_value()) { + case VIRT_KEYBOARD_MODE_CHORD: + if (event->y >= REGION_BLOCK_HEIGHT) + keyboard_key_released_signal.emit(k, velocity); + break; + case VIRT_KEYBOARD_MODE_NORMAL: + default: + if (currentActiveKey >= 0 && currentActiveKey <= 127) { + keyboard_key_released_signal.emit(currentActiveKey, velocity); + currentActiveKey = -1; + } + break; + } + } + if (resize.active) { get_window()->pointer_ungrab(event->time); resize.active = false; if (resize.mode == resize.moving_high_limit) { if (resize.region->KeyRange.high != resize.pos - 1) { - resize.region->KeyRange.high = resize.pos - 1; - instrument_changed(); + instrument_struct_to_be_changed_signal.emit(instrument); + resize.region->SetKeyRange( + resize.region->KeyRange.low, // low + resize.pos - 1 // high + ); + regions.update(instrument); + instrument_changed.emit(); + instrument_struct_changed_signal.emit(instrument); } } else if (resize.mode == resize.moving_low_limit) { if (resize.region->KeyRange.low != resize.pos) { - resize.region->KeyRange.low = resize.pos; - instrument_changed(); + instrument_struct_to_be_changed_signal.emit(instrument); + resize.region->SetKeyRange( + resize.pos, // low + resize.region->KeyRange.high // high + ); + regions.update(instrument); + instrument_changed.emit(); + instrument_struct_changed_signal.emit(instrument); } } @@ -268,22 +459,14 @@ move.active = false; if (move.pos) { - region->KeyRange.low += move.pos; - region->KeyRange.high += move.pos; - - // find the r which is the first one to the right of region - // at its new position - gig::Region* r; - gig::Region* prev_region = 0; - for (r = instrument->GetFirstRegion() ; r ; r = instrument->GetNextRegion()) { - if (r->KeyRange.low > region->KeyRange.low) break; - prev_region = r; - } - - // place region before r if it's not already there - if (prev_region != region) { - instrument->MoveRegion(region, r); - } + instrument_struct_to_be_changed_signal.emit(instrument); + region->SetKeyRange( + region->KeyRange.low + move.pos, + region->KeyRange.high + move.pos + ); + regions.update(instrument); + instrument_changed.emit(); + instrument_struct_changed_signal.emit(instrument); } if (is_in_resize_zone(event->x, event->y)) { @@ -298,14 +481,25 @@ { if (!instrument) return true; - int k = int(event->x / (width - 1) * 128.0); + const int k = int(event->x / (get_width() - 1) * 128.0); + + if (event->type == GDK_BUTTON_PRESS) { + if (event->y >= REGION_BLOCK_HEIGHT) { + int velocity = (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 : + int(float(event->y - REGION_BLOCK_HEIGHT) / float(KEYBOARD_HEIGHT) * 128.0f) + 1; + currentActiveKey = k; + keyboard_key_hit_signal.emit(k, velocity); + } + } + if (event->y >= REGION_BLOCK_HEIGHT) return true; if (event->type == GDK_BUTTON_PRESS && event->button == 3) { gig::Region* r = get_region(k); if (r) { region = r; queue_draw(); region_selected(); + dimensionManager.set_region(region); popup_menu_inside_region->popup(event->button, event->time); } else { new_region_pos = k; @@ -325,6 +519,7 @@ region = r; queue_draw(); region_selected(); + dimensionManager.set_region(region); get_window()->pointer_grab(false, Gdk::BUTTON_RELEASE_MASK | @@ -344,9 +539,8 @@ { gig::Region* prev_region = 0; gig::Region* next_region; - for (gig::Region *r = instrument->GetFirstRegion() ; r ; - r = next_region) { - next_region = instrument->GetNextRegion(); + for (gig::Region* r = regions.first() ; r ; r = next_region) { + next_region = regions.next(); if (key < r->KeyRange.low) return 0; if (key <= r->KeyRange.high) { @@ -361,8 +555,7 @@ void RegionChooser::motion_resize_region(int x, int y) { - const int w = width - 1; - Glib::RefPtr window = get_window(); + const int w = get_width() - 1; int k = int(double(x) / w * 128.0 + 0.5); @@ -370,6 +563,9 @@ else if (k > resize.max) k = resize.max; if (k != resize.pos) { + Cairo::RefPtr cr = get_window()->create_cairo_context(); + cr->set_line_width(1); + if (resize.mode == resize.undecided) { if (k < resize.pos) { // edit high limit of prev_region @@ -382,44 +578,65 @@ resize.mode = resize.moving_low_limit; } } - Glib::RefPtr black = get_style()->get_black_gc(); - Glib::RefPtr white = get_style()->get_white_gc(); - if (region == resize.region) { - gc->set_foreground(red); - white = gc; - } - Glib::RefPtr bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL); + const Gdk::Color white = + region == resize.region ? red : get_style()->get_white(); + const Gdk::Color bg = get_style()->get_bg(Gtk::STATE_NORMAL); + int prevx = int(w * resize.pos / 128.0 + 0.5); x = int(w * k / 128.0 + 0.5); if (resize.mode == resize.moving_high_limit) { if (k > resize.pos) { - window->draw_rectangle(white, true, prevx, 1, x - prevx, h1 - 2); - window->draw_line(black, prevx, 0, x, 0); - window->draw_line(black, prevx, h1 - 1, x, h1 - 1); + Gdk::Cairo::set_source_color(cr, white); + cr->rectangle(prevx, 1, x - prevx, h1 - 2); + cr->fill(); + + Gdk::Cairo::set_source_color(cr, black); + cr->move_to(prevx, 0.5); + cr->line_to(x + 1, 0.5); + cr->move_to(prevx, h1 - 0.5); + cr->line_to(x + 1, h1 - 0.5); + cr->stroke(); } else { - int xx = ((resize.pos == resize.max && resize.max != 128) ? 1 : 0); - window->draw_rectangle(bg, true, x + 1, 0, prevx - x - xx, h1); + int xx = (resize.pos == resize.max && + resize.max != 128) ? 1 : 0; + Gdk::Cairo::set_source_color(cr, bg); + cr->rectangle(x + 1, 0, prevx - x - xx, h1); + cr->fill(); } } else { if (k < resize.pos) { - window->draw_rectangle(white, true, x + 1, 1, prevx - x, h1 - 2); - window->draw_line(black, x, 0, prevx, 0); - window->draw_line(black, x, h1 - 1, prevx, h1 - 1); + Gdk::Cairo::set_source_color(cr, white); + cr->rectangle(x + 1, 1, prevx - x, h1 - 2); + cr->fill(); + + Gdk::Cairo::set_source_color(cr, black); + cr->move_to(x, 0.5); + cr->line_to(prevx, 0.5); + cr->move_to(x, h1 - 0.5); + cr->line_to(prevx, h1 - 0.5); + cr->stroke(); } else { - int xx = ((resize.pos == resize.min && resize.min != 0) ? 1 : 0); - window->draw_rectangle(bg, true, prevx + xx, 0, x - prevx - xx, h1); + int xx = (resize.pos == resize.min && + resize.min != 0) ? 1 : 0; + Gdk::Cairo::set_source_color(cr, bg); + cr->rectangle(prevx + xx, 0, x - prevx - xx, h1); + cr->fill(); } } - window->draw_line(black, x, 1, x, h1 - 2); + Gdk::Cairo::set_source_color(cr, black); + cr->move_to(x + 0.5, 1); + cr->line_to(x + 0.5, h1 - 1); + cr->stroke(); resize.pos = k; } } void RegionChooser::motion_move_region(int x, int y) { - const int w = width - 1; - Glib::RefPtr window = get_window(); + const int w = get_width() - 1; + Cairo::RefPtr cr = get_window()->create_cairo_context(); + cr->set_line_width(1); int k = int(double(x - move.from_x) / w * 128.0 + 0.5); if (k == move.pos) return; @@ -428,8 +645,7 @@ bool new_touch_right; int a = 0; if (k > move.pos) { - for (gig::Region* r = instrument->GetFirstRegion() ; ; - r = instrument->GetNextRegion()) { + for (gig::Region* r = regions.first() ; ; r = regions.next()) { if (r != region) { int b = r ? r->KeyRange.low : 128; @@ -459,8 +675,7 @@ } } } else { - for (gig::Region* r = instrument->GetFirstRegion() ; ; - r = instrument->GetNextRegion()) { + for (gig::Region* r = regions.first() ; ; r = regions.next()) { if (r != region) { int b = r ? r->KeyRange.low : 128; @@ -494,35 +709,61 @@ k = new_k; if (k == move.pos) return; - Glib::RefPtr bg = get_style()->get_bg_gc(Gtk::STATE_NORMAL); + const Gdk::Color bg = get_style()->get_bg(Gtk::STATE_NORMAL); int prevx = int(w * (move.pos + region->KeyRange.low) / 128.0 + 0.5); x = int(w * (k + region->KeyRange.low) / 128.0 + 0.5); int prevx2 = int(w * (move.pos + region->KeyRange.high + 1) / 128.0 + 0.5); int x2 = int(w * (k + region->KeyRange.high + 1) / 128.0 + 0.5); - Glib::RefPtr black = get_style()->get_black_gc(); - gc->set_foreground(red); + const Gdk::Color black = get_style()->get_black(); - if (!new_touch_left) window->draw_line(black, x, 1, x, h1 - 2); - if (!new_touch_right) window->draw_line(black, x2, 1, x2, h1 - 2); + if (!new_touch_left) { + Gdk::Cairo::set_source_color(cr, black); + cr->move_to(x + 0.5, 1); + cr->line_to(x + 0.5, h1 - 1); + cr->stroke(); + } + if (!new_touch_right) { + Gdk::Cairo::set_source_color(cr, black); + cr->move_to(x2 + 0.5, 1); + cr->line_to(x2 + 0.5, h1 - 1); + cr->stroke(); + } if (k > move.pos) { - window->draw_rectangle(bg, true, prevx + (move.touch_left ? 1 : 0), 0, - std::min(x, prevx2 + 1 - (move.touch_right ? 1 : 0)) - - (prevx + (move.touch_left ? 1 : 0)), h1); - - window->draw_line(black, std::max(x, prevx2 + 1), 0, x2, 0); - window->draw_line(black, std::max(x, prevx2 + 1), h1 - 1, x2, h1 - 1); - window->draw_rectangle(gc, true, std::max(x + 1, prevx2), 1, - x2 - std::max(x + 1, prevx2), h1 - 2); + Gdk::Cairo::set_source_color(cr, bg); + cr->rectangle(prevx + (move.touch_left ? 1 : 0), 0, + std::min(x, prevx2 + 1 - (move.touch_right ? 1 : 0)) - + (prevx + (move.touch_left ? 1 : 0)), h1); + cr->fill(); + + Gdk::Cairo::set_source_color(cr, black); + cr->move_to(std::max(x, prevx2 + 1), 0.5); + cr->line_to(x2 + 1, 0.5); + cr->move_to(std::max(x, prevx2 + 1), h1 - 0.5); + cr->line_to(x2 + 1, h1 - 0.5); + cr->stroke(); + + Gdk::Cairo::set_source_color(cr, red); + cr->rectangle(std::max(x + 1, prevx2), 1, + x2 - std::max(x + 1, prevx2), h1 - 2); + cr->fill(); } else { - window->draw_rectangle(bg, true, std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), 0, - prevx2 + 1 - (move.touch_right ? 1 : 0) - - std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), h1); - - window->draw_line(black, x, 0, std::min(x2, prevx - 1), 0); - window->draw_line(black, x, h1 - 1, std::min(x2, prevx - 1), h1 - 1); - - window->draw_rectangle(gc, true, x + 1, 1, std::min(x2 - 1, prevx) - x, h1 - 2); + Gdk::Cairo::set_source_color(cr, bg); + cr->rectangle(std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), 0, + prevx2 + 1 - (move.touch_right ? 1 : 0) - + std::max(x2 + 1, prevx + (move.touch_left ? 1 : 0)), h1); + cr->fill(); + + Gdk::Cairo::set_source_color(cr, black); + cr->move_to(x, 0.5); + cr->line_to(std::min(x2, prevx - 1) + 1, 0.5); + cr->move_to(x, h1 - 0.5); + cr->line_to(std::min(x2, prevx - 1) + 1, h1 - 0.5); + cr->stroke(); + + Gdk::Cairo::set_source_color(cr, red); + cr->rectangle(x + 1, 1, std::min(x2 - 1, prevx) - x, h1 - 2); + cr->fill(); } move.pos = k; @@ -538,6 +779,25 @@ Gdk::ModifierType state = Gdk::ModifierType(0); window->get_pointer(x, y, state); + // handle virtual MIDI keyboard + if (m_VirtKeybModeChoice.get_value() != VIRT_KEYBOARD_MODE_CHORD && + currentActiveKey > 0 && + event->y >= REGION_BLOCK_HEIGHT && + event->y < REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT) + { + const int k = int(event->x / (get_width() - 1) * 128.0); + if (k != currentActiveKey) { + int velocity = + (event->y >= REGION_BLOCK_HEIGHT + KEYBOARD_HEIGHT - 1) ? 127 : + int(float(event->y - REGION_BLOCK_HEIGHT) / + float(KEYBOARD_HEIGHT) * 128.0f) + 1; + if (velocity <= 0) velocity = 1; + keyboard_key_released_signal.emit(currentActiveKey, velocity); + currentActiveKey = k; + keyboard_key_hit_signal.emit(k, velocity); + } + } + if (resize.active) { motion_resize_region(x, y); } else if (move.active) { @@ -558,13 +818,13 @@ } bool RegionChooser::is_in_resize_zone(double x, double y) { - const int w = width - 1; + const int w = get_width() - 1; if (instrument && y >= 0 && y <= h1) { gig::Region* prev_region = 0; gig::Region* next_region; - for (gig::Region* r = instrument->GetFirstRegion() ; r ; r = next_region) { - next_region = instrument->GetNextRegion(); + for (gig::Region* r = regions.first(); r ; r = next_region) { + next_region = regions.next(); int lo = int(w * (r->KeyRange.low) / 128.0 + 0.5); if (x <= lo - 2) break; @@ -580,13 +840,13 @@ resize.mode = resize.undecided; resize.min = prev_region->KeyRange.low + 1; resize.prev_region = prev_region; - return true; + return resize.min != resize.max; } // edit low limit resize.mode = resize.moving_low_limit; resize.min = prev_region ? prev_region->KeyRange.high + 1 : 0; - return true; + return resize.min != resize.max; } if (!next_region || r->KeyRange.high + 1 != next_region->KeyRange.low) { int hi = int(w * (r->KeyRange.high + 1) / 128.0 + 0.5); @@ -598,7 +858,7 @@ resize.mode = resize.moving_high_limit; resize.min = r->KeyRange.low + 1; resize.max = next_region ? next_region->KeyRange.low : 128; - return true; + return resize.min != resize.max; } } prev_region = r; @@ -607,12 +867,12 @@ return false; } -sigc::signal RegionChooser::signal_region_selected() +sigc::signal& RegionChooser::signal_region_selected() { return region_selected; } -sigc::signal RegionChooser::signal_instrument_changed() +sigc::signal& RegionChooser::signal_instrument_changed() { return instrument_changed; } @@ -620,9 +880,9 @@ void RegionChooser::show_region_properties() { if (!region) return; - Gtk::Dialog dialog("Region Properties", true /*modal*/); + Gtk::Dialog dialog(_("Region Properties"), true /*modal*/); // add "Keygroup" checkbox - Gtk::CheckButton checkBoxKeygroup("Member of a Keygroup (Exclusive Group)"); + Gtk::CheckButton checkBoxKeygroup(_("Member of a Keygroup (Exclusive Group)")); checkBoxKeygroup.set_active(region->KeyGroup); dialog.get_vbox()->pack_start(checkBoxKeygroup); checkBoxKeygroup.show(); @@ -644,26 +904,31 @@ void RegionChooser::add_region() { - gig::Region* r; - for (r = instrument->GetFirstRegion() ; r ; r = instrument->GetNextRegion()) { - if (r->KeyRange.low > new_region_pos) break; - } + instrument_struct_to_be_changed_signal.emit(instrument); region = instrument->AddRegion(); - region->KeyRange.low = region->KeyRange.high = new_region_pos; + region->SetKeyRange(new_region_pos, new_region_pos); + + instrument_struct_changed_signal.emit(instrument); + regions.update(instrument); - instrument->MoveRegion(region, r); queue_draw(); region_selected(); + dimensionManager.set_region(region); instrument_changed(); } void RegionChooser::delete_region() { + instrument_struct_to_be_changed_signal.emit(instrument); instrument->DeleteRegion(region); + instrument_struct_changed_signal.emit(instrument); + regions.update(instrument); + region = 0; queue_draw(); region_selected(); + dimensionManager.set_region(region); instrument_changed(); } @@ -678,3 +943,27 @@ region_selected(); instrument_changed(); } + +sigc::signal& RegionChooser::signal_instrument_struct_to_be_changed() { + return instrument_struct_to_be_changed_signal; +} + +sigc::signal& RegionChooser::signal_instrument_struct_changed() { + return instrument_struct_changed_signal; +} + +sigc::signal& RegionChooser::signal_region_to_be_changed() { + return region_to_be_changed_signal; +} + +sigc::signal& RegionChooser::signal_region_changed_signal() { + return region_changed_signal; +} + +sigc::signal& RegionChooser::signal_keyboard_key_hit() { + return keyboard_key_hit_signal; +} + +sigc::signal& RegionChooser::signal_keyboard_key_released() { + return keyboard_key_released_signal; +}