--- gigedit/trunk/src/gigedit/regionchooser.cpp 2007/09/10 19:56:26 1339 +++ gigedit/trunk/src/gigedit/regionchooser.cpp 2008/02/10 08:50:35 1677 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006, 2007 Andreas Persson + * Copyright (C) 2006-2008 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,31 +18,92 @@ */ #include "regionchooser.h" +#include #include #include #include #include -#include #include +#include -#define _(String) gettext(String) +#include "global.h" -RegionChooser::RegionChooser() +#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) { Glib::RefPtr colormap = get_default_colormap(); 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); + colormap->alloc_color(activeKeyColor); + colormap->alloc_color(white); + colormap->alloc_color(black); 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", @@ -90,12 +151,37 @@ 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) + ); } RegionChooser::~RegionChooser() { } +template inline std::string ToString(T o) { + std::stringstream ss; + ss << o; + return ss.str(); +} + +void RegionChooser::on_note_on_event(int key, int velocity) { + draw_region(key, key+1, activeKeyColor); + m_VirtKeybVelocityLabel.set_text(ToString(velocity)); +} + +void RegionChooser::on_note_off_event(int key, int velocity) { + if (is_black_key(key)) + draw_region(key, key+1, black); + else + draw_region(key, key+1, white); + m_VirtKeybOffVelocityLabel.set_text(ToString(velocity)); +} + void RegionChooser::on_realize() { // We need to call the base on_realize() @@ -111,16 +197,13 @@ { Glib::RefPtr window = get_window(); window->clear(); - const int h = 40; - const int w = width - 1; + const int h = KEYBOARD_HEIGHT; + const int w = get_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); @@ -130,7 +213,6 @@ 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); @@ -144,30 +226,17 @@ } 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 (note == 3) draw_digit(i); } if (instrument) { int i = 0; gig::Region *next_region; int x3 = -1; - for (gig::Region *r = instrument->GetFirstRegion() ; - r ; - r = next_region) { + 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 = instrument->GetNextRegion(); + 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); window->draw_line(black, x3, 0, x2, 0); @@ -179,9 +248,7 @@ i++; } - for (gig::Region *r = instrument->GetFirstRegion() ; - r ; - r = instrument->GetNextRegion()) { + for (gig::Region *r = regions.first() ; r ; r = regions.next()) { int x = int(w * (r->KeyRange.low) / 128.0 + 0.5); window->draw_line(black, x, 1, x, h1 - 2); } @@ -200,16 +267,34 @@ 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; + get_window()->draw_layout(get_style()->get_black_gc(), int(x - text_w / 2 + 1), + int(h1 + h - text_h + 0.5), layout); +} -// not used void RegionChooser::draw_region(int from, int to, 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(); @@ -220,20 +305,21 @@ 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 x4 = int(w * (i - 0.5) / 128.0 + 0.5); 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); + window->draw_rectangle(gc, true, x4 + 1, h1 + bh + 1, x2 - x4 - 1, 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); + window->draw_rectangle(gc, true, x4 + 1, h1 + bh + 1, x3 - x4 - 1, 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); + if (note == 3) draw_digit(i); break; default: window->draw_rectangle(gc, true, x, h1 + 1, w1, bh - 1); @@ -245,13 +331,37 @@ 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; @@ -263,6 +373,7 @@ resize.region->KeyRange.low, // low resize.pos - 1 // high ); + regions.update(instrument); instrument_changed.emit(); instrument_struct_changed_signal.emit(instrument); } @@ -273,6 +384,7 @@ resize.pos, // low resize.region->KeyRange.high // high ); + regions.update(instrument); instrument_changed.emit(); instrument_struct_changed_signal.emit(instrument); } @@ -292,6 +404,8 @@ region->KeyRange.low + move.pos, region->KeyRange.high + move.pos ); + regions.update(instrument); + instrument_changed.emit(); instrument_struct_changed_signal.emit(instrument); } @@ -307,14 +421,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; @@ -334,6 +459,7 @@ region = r; queue_draw(); region_selected(); + dimensionManager.set_region(region); get_window()->pointer_grab(false, Gdk::BUTTON_RELEASE_MASK | @@ -353,9 +479,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) { @@ -370,7 +495,7 @@ void RegionChooser::motion_resize_region(int x, int y) { - const int w = width - 1; + const int w = get_width() - 1; Glib::RefPtr window = get_window(); int k = int(double(x) / w * 128.0 + 0.5); @@ -427,7 +552,7 @@ void RegionChooser::motion_move_region(int x, int y) { - const int w = width - 1; + const int w = get_width() - 1; Glib::RefPtr window = get_window(); int k = int(double(x - move.from_x) / w * 128.0 + 0.5); @@ -437,8 +562,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; @@ -468,8 +592,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; @@ -547,6 +670,21 @@ 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); + 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) { @@ -567,13 +705,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; @@ -589,13 +727,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); @@ -607,7 +745,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; @@ -659,9 +797,11 @@ region->SetKeyRange(new_region_pos, new_region_pos); instrument_struct_changed_signal.emit(instrument); + regions.update(instrument); queue_draw(); region_selected(); + dimensionManager.set_region(region); instrument_changed(); } @@ -670,10 +810,12 @@ 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(); } @@ -704,3 +846,11 @@ 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; +}