--- gigedit/trunk/src/gigedit/dimregionchooser.cpp 2007/12/01 10:21:07 1533 +++ gigedit/trunk/src/gigedit/dimregionchooser.cpp 2017/04/28 12:41:12 3134 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006, 2007 Andreas Persson + * Copyright (C) 2006-2017 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 @@ -17,209 +17,510 @@ * 02110-1301 USA. */ +#include #include "dimregionchooser.h" +#include +#include #include +#include +#include +#include +#include +#include + +#include "global.h" +#include "gfx/builtinpix.h" + +//TODO: this function and dimensionCaseOf() from global.h are duplicates, eliminate either one of them! +static DimensionCase caseOfDimRegion(gig::DimensionRegion* dr, bool* isValidZone) { + DimensionCase dimCase; + if (!dr) { + *isValidZone = false; + return dimCase; + } + + gig::Region* rgn = (gig::Region*) dr->GetParent(); + + // find the dimension region index of the passed dimension region + int drIndex; + for (drIndex = 0; drIndex < 256; ++drIndex) + if (rgn->pDimensionRegions[drIndex] == dr) + break; + + // not found in region, something's horribly wrong + if (drIndex == 256) { + fprintf(stderr, "DimRegionChooser: ERROR: index of dim region not found!\n"); + *isValidZone = false; + return DimensionCase(); + } + + for (int d = 0, baseBits = 0; d < rgn->Dimensions; ++d) { + const int bits = rgn->pDimensionDefinitions[d].bits; + dimCase[rgn->pDimensionDefinitions[d].dimension] = + (drIndex >> baseBits) & ((1 << bits) - 1); + baseBits += bits; + // there are also DimensionRegion objects of unused zones, skip them + if (dimCase[rgn->pDimensionDefinitions[d].dimension] >= rgn->pDimensionDefinitions[d].zones) { + *isValidZone = false; + return DimensionCase(); + } + } -DimRegionChooser::DimRegionChooser() + *isValidZone = true; + return dimCase; +} + +DimRegionChooser::DimRegionChooser(Gtk::Window& window) : + red("#ff476e"), + blue("#4796ff"), + black("black"), + white("white") { - // get_window() would return 0 because the Gdk::Window has not yet been realized - // So we can only allocate the colors here - the rest will happen in on_realize(). - Glib::RefPtr colormap = get_default_colormap(); - - black = Gdk::Color("black"); - white = Gdk::Color("white"); - red = Gdk::Color("#8070ff"); - blue = Gdk::Color("blue"); - green = Gdk::Color("green"); - - colormap->alloc_color(black); - colormap->alloc_color(white); - colormap->alloc_color(red); - colormap->alloc_color(blue); - colormap->alloc_color(green); + // make sure blue hatched pattern pixmap is loaded + loadBuiltInPix(); + + // create blue hatched pattern + { + const int width = blueHatchedPattern->get_width(); + const int height = blueHatchedPattern->get_height(); + const int stride = blueHatchedPattern->get_rowstride(); + + // manually convert from RGBA to ARGB + this->blueHatchedPatternARGB = blueHatchedPattern->copy(); + const int pixelSize = stride / width; + const int totalPixels = width * height; + assert(pixelSize == 4); + unsigned char* ptr = this->blueHatchedPatternARGB->get_pixels(); + for (int iPixel = 0; iPixel < totalPixels; ++iPixel, ptr += pixelSize) { + const unsigned char r = ptr[0]; + const unsigned char g = ptr[1]; + const unsigned char b = ptr[2]; + const unsigned char a = ptr[3]; + ptr[0] = b; + ptr[1] = g; + ptr[2] = r; + ptr[3] = a; + } + + Cairo::RefPtr imageSurface = Cairo::ImageSurface::create( + this->blueHatchedPatternARGB->get_pixels(), Cairo::FORMAT_ARGB32, width, height, stride + ); + this->blueHatchedSurfacePattern = Cairo::SurfacePattern::create(imageSurface); + this->blueHatchedSurfacePattern->set_extend(Cairo::EXTEND_REPEAT); + } + instrument = 0; region = 0; - dimregno = -1; + maindimregno = -1; + maindimtype = gig::dimension_none; // initialize with invalid dimension type focus_line = 0; resize.active = false; cursor_is_resize = false; - h = 20; - w = 800; - set_flags(Gtk::CAN_FOCUS); + h = 24; + multiSelectKeyDown = false; + primaryKeyDown = false; + shiftKeyDown = false; + modifybothchannels = modifyalldimregs = modifybothchannels = false; + set_can_focus(); + + const Glib::ustring txtUseCheckBoxAllRegions = + _("Use checkbox 'all regions' to control whether this should apply to all regions."); + + actionGroup = Gtk::ActionGroup::create(); + actionSplitDimZone = Gtk::Action::create("SplitDimZone", _("Split Dimensions Zone"), txtUseCheckBoxAllRegions); + actionSplitDimZone->set_tooltip(txtUseCheckBoxAllRegions); //FIXME: doesn't work? why??? + actionGroup->add( + actionSplitDimZone, + sigc::mem_fun(*this, &DimRegionChooser::split_dimension_zone) + ); + actionDeleteDimZone = Gtk::Action::create("DeleteDimZone", _("Delete Dimension Zone"), txtUseCheckBoxAllRegions); + actionDeleteDimZone->set_tooltip(txtUseCheckBoxAllRegions); //FIXME: doesn't work? why??? + actionGroup->add( + actionDeleteDimZone, + sigc::mem_fun(*this, &DimRegionChooser::delete_dimension_zone) + ); + + uiManager = Gtk::UIManager::create(); + uiManager->insert_action_group(actionGroup); + Glib::ustring ui_info = + "" + " " + " " + " " + " " +// " " +// " " +// " " + ""; + uiManager->add_ui_from_string(ui_info); + + popup_menu_inside_dimregion = dynamic_cast( + uiManager->get_widget("/PopupMenuInsideDimRegion")); +// popup_menu_outside_dimregion = dynamic_cast( +// uiManager->get_widget("/PopupMenuOutsideDimRegion")); + add_events(Gdk::BUTTON_PRESS_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK); - for (int i = 0 ; i < 256 ; i++) dimvalue[i] = 0; + labels_changed = true; + + set_tooltip_text(_( + "Right click here for options on altering dimension zones. Press and " + "hold CTRL key for selecting multiple dimension zones simultaniously." + )); + + window.signal_key_press_event().connect( + sigc::mem_fun(*this, &DimRegionChooser::onKeyPressed) + ); + window.signal_key_release_event().connect( + sigc::mem_fun(*this, &DimRegionChooser::onKeyReleased) + ); } DimRegionChooser::~DimRegionChooser() { } -void DimRegionChooser::on_realize() -{ - // We need to call the base on_realize() - Gtk::DrawingArea::on_realize(); +void DimRegionChooser::setModifyBothChannels(bool b) { + modifybothchannels = b; +} - // Now we can allocate any additional resources we need - Glib::RefPtr window = get_window(); - gc = Gdk::GC::create(window); +void DimRegionChooser::setModifyAllDimensionRegions(bool b) { + modifyalldimregs = b; } -bool DimRegionChooser::on_expose_event(GdkEventExpose* event) +void DimRegionChooser::setModifyAllRegions(bool b) { + modifyallregions = b; + + actionDeleteDimZone->set_label(b ? _("Delete Dimension Zone [ALL REGIONS]") : _("Delete Dimension Zone")); + actionSplitDimZone->set_label(b ? _("Split Dimensions Zone [ALL REGIONS]") : _("Split Dimensions Zone")); +} + +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 +bool DimRegionChooser::on_expose_event(GdkEventExpose* e) { + double clipx1 = e->area.x; + double clipx2 = e->area.x + e->area.width; + double clipy1 = e->area.y; + double clipy2 = e->area.y + e->area.height; + + const Cairo::RefPtr& cr = + get_window()->create_cairo_context(); +#else +bool DimRegionChooser::on_draw(const Cairo::RefPtr& cr) +{ + double clipx1, clipx2, clipy1, clipy2; + cr->get_clip_extents(clipx1, clipy1, clipx2, clipy2); +#endif + if (!region) return true; // This is where we draw on the window - Glib::RefPtr window = get_window(); + int w = get_width(); Glib::RefPtr context = get_pango_context(); Glib::RefPtr layout = Pango::Layout::create(context); + cr->set_line_width(1); - window->clear(); - - // draw labels on the left (reflecting the dimension type) int y = 0; - double maxwidth = 0; - for (int i = 0 ; i < region->Dimensions ; i++) { - int nbZones = region->pDimensionDefinitions[i].zones; - if (nbZones) { - const char* dstr; - char dstrbuf[10]; - switch (region->pDimensionDefinitions[i].dimension) { - case gig::dimension_none: dstr="none"; break; - case gig::dimension_samplechannel: dstr="samplechannel"; break; - case gig::dimension_layer: dstr="layer"; break; - case gig::dimension_velocity: dstr="velocity"; break; - case gig::dimension_channelaftertouch: dstr="channelaftertouch"; break; - case gig::dimension_releasetrigger: dstr="releasetrigger"; break; - case gig::dimension_keyboard: dstr="keyboard"; break; - case gig::dimension_roundrobin: dstr="roundrobin"; break; - case gig::dimension_random: dstr="random"; break; - case gig::dimension_smartmidi: dstr="smartmidi"; break; - case gig::dimension_roundrobinkeyboard: dstr="roundrobinkeyboard"; break; - case gig::dimension_modwheel: dstr="modwheel"; break; - case gig::dimension_breath: dstr="breath"; break; - case gig::dimension_foot: dstr="foot"; break; - case gig::dimension_portamentotime: dstr="portamentotime"; break; - case gig::dimension_effect1: dstr="effect1"; break; - case gig::dimension_effect2: dstr="effect2"; break; - case gig::dimension_genpurpose1: dstr="genpurpose1"; break; - case gig::dimension_genpurpose2: dstr="genpurpose2"; break; - case gig::dimension_genpurpose3: dstr="genpurpose3"; break; - case gig::dimension_genpurpose4: dstr="genpurpose4"; break; - case gig::dimension_sustainpedal: dstr="sustainpedal"; break; - case gig::dimension_portamento: dstr="portamento"; break; - case gig::dimension_sostenutopedal: dstr="sostenutopedal"; break; - case gig::dimension_softpedal: dstr="softpedal"; break; - case gig::dimension_genpurpose5: dstr="genpurpose5"; break; - case gig::dimension_genpurpose6: dstr="genpurpose6"; break; - case gig::dimension_genpurpose7: dstr="genpurpose7"; break; - case gig::dimension_genpurpose8: dstr="genpurpose8"; break; - case gig::dimension_effect1depth: dstr="effect1depth"; break; - case gig::dimension_effect2depth: dstr="effect2depth"; break; - case gig::dimension_effect3depth: dstr="effect3depth"; break; - case gig::dimension_effect4depth: dstr="effect4depth"; break; - case gig::dimension_effect5depth: dstr="effect5depth"; break; - default: - sprintf(dstrbuf, "%d", - region->pDimensionDefinitions[i].dimension); - dstr = dstrbuf; - break; - } - layout->set_text(dstr); - - Pango::Rectangle rectangle = layout->get_logical_extents(); - double text_w = double(rectangle.get_width()) / Pango::SCALE; - if (text_w > maxwidth) maxwidth = text_w; - double text_h = double(rectangle.get_height()) / Pango::SCALE; - Glib::RefPtr fg = get_style()->get_fg_gc(get_state()); - window->draw_layout(fg, 4, int(y + (h - text_h) / 2 + 0.5), layout); + if (labels_changed || label_width - 10 > clipx1) { + // draw labels on the left (reflecting the dimension type) + double maxwidth = 0; + for (int i = 0 ; i < region->Dimensions ; i++) { + int nbZones = region->pDimensionDefinitions[i].zones; + if (nbZones) { + const char* dstr; + char dstrbuf[10]; + switch (region->pDimensionDefinitions[i].dimension) { + case gig::dimension_none: dstr=_("none"); break; + case gig::dimension_samplechannel: dstr=_("samplechannel"); + break; + case gig::dimension_layer: dstr=_("layer"); break; + case gig::dimension_velocity: dstr=_("velocity"); break; + case gig::dimension_channelaftertouch: + dstr=_("channelaftertouch"); break; + case gig::dimension_releasetrigger: + dstr=_("releasetrigger"); break; + case gig::dimension_keyboard: dstr=_("keyswitching"); break; + case gig::dimension_roundrobin: dstr=_("roundrobin"); break; + case gig::dimension_random: dstr=_("random"); break; + case gig::dimension_smartmidi: dstr=_("smartmidi"); break; + case gig::dimension_roundrobinkeyboard: + dstr=_("roundrobinkeyboard"); break; + case gig::dimension_modwheel: dstr=_("modwheel"); break; + case gig::dimension_breath: dstr=_("breath"); break; + case gig::dimension_foot: dstr=_("foot"); break; + case gig::dimension_portamentotime: + dstr=_("portamentotime"); break; + case gig::dimension_effect1: dstr=_("effect1"); break; + case gig::dimension_effect2: dstr=_("effect2"); break; + case gig::dimension_genpurpose1: dstr=_("genpurpose1"); break; + case gig::dimension_genpurpose2: dstr=_("genpurpose2"); break; + case gig::dimension_genpurpose3: dstr=_("genpurpose3"); break; + case gig::dimension_genpurpose4: dstr=_("genpurpose4"); break; + case gig::dimension_sustainpedal: + dstr=_("sustainpedal"); break; + case gig::dimension_portamento: dstr=_("portamento"); break; + case gig::dimension_sostenutopedal: + dstr=_("sostenutopedal"); break; + case gig::dimension_softpedal: dstr=_("softpedal"); break; + case gig::dimension_genpurpose5: dstr=_("genpurpose5"); break; + case gig::dimension_genpurpose6: dstr=_("genpurpose6"); break; + case gig::dimension_genpurpose7: dstr=_("genpurpose7"); break; + case gig::dimension_genpurpose8: dstr=_("genpurpose8"); break; + case gig::dimension_effect1depth: + dstr=_("effect1depth"); break; + case gig::dimension_effect2depth: + dstr=_("effect2depth"); break; + case gig::dimension_effect3depth: + dstr=_("effect3depth"); break; + case gig::dimension_effect4depth: + dstr=_("effect4depth"); break; + case gig::dimension_effect5depth: + dstr=_("effect5depth"); break; + default: + sprintf(dstrbuf, "%d", + region->pDimensionDefinitions[i].dimension); + dstr = dstrbuf; + break; + } + // Since bold font yields in larger label width, we first always + // set the bold text variant, retrieve its dimensions (as worst + // case dimensions of the label) ... + layout->set_markup("" + Glib::ustring(dstr) + ""); + Pango::Rectangle rectangle = layout->get_logical_extents(); + // ... and then reset the label to regular font style in case + // the line is not selected. Otherwise the right hand side + // actual dimension zones would jump around on selection change. + bool isSelectedLine = (focus_line == i); + if (!isSelectedLine) + layout->set_markup(dstr); + + double text_w = double(rectangle.get_width()) / Pango::SCALE; + if (text_w > maxwidth) maxwidth = text_w; + + if (y + h > clipy1 && y < clipy2 && text_w >= clipx1) { + double text_h = double(rectangle.get_height()) / + Pango::SCALE; +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 + const Gdk::Color fg = get_style()->get_fg(get_state()); +#else + const Gdk::RGBA fg = + get_style_context()->get_color(get_state_flags()); +#endif + Gdk::Cairo::set_source_rgba(cr, fg); + cr->move_to(4, int(y + (h - text_h) / 2 + 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 + } + } + y += h; } - y += h; + label_width = int(maxwidth + 10); + labels_changed = false; } + if (label_width >= clipx2) return true; // draw dimensions' zones areas y = 0; int bitpos = 0; - label_width = int(maxwidth + 10); for (int i = 0 ; i < region->Dimensions ; i++) { int nbZones = region->pDimensionDefinitions[i].zones; if (nbZones) { - // draw focus rectangle around dimension's label and zones - if (has_focus() && focus_line == i) { - Gdk::Rectangle farea(0, y, 150, 20); - get_style()->paint_focus(window, get_state(), farea, *this, "", - 0, y, label_width, 20); - } - - Glib::RefPtr black = get_style()->get_black_gc(); - // draw top and bottom lines of dimension's zones - window->draw_line(black, label_width, y, w - 1, y); - window->draw_line(black, w - 1, y + h - 1, label_width, y + h - 1); - // erase whole dimension's zones area - window->draw_rectangle(get_style()->get_white_gc(), true, - label_width + 1, y + 1, (w - label_width - 2), h - 2); - - int c = 0; - if (dimregno >= 0) { - int mask = ~(((1 << region->pDimensionDefinitions[i].bits) - 1) << bitpos); - c = dimregno & mask; // mask away this dimension - } - bool customsplits = - ((region->pDimensionDefinitions[i].split_type == gig::split_type_normal && - region->pDimensionRegions[c]->DimensionUpperLimits[i]) || - (region->pDimensionDefinitions[i].dimension == gig::dimension_velocity && - region->pDimensionRegions[c]->VelocityUpperLimit)); + const gig::dimension_t dimension = region->pDimensionDefinitions[i].dimension; - // draw dimension's zone borders - if (customsplits) { - window->draw_line(black, label_width, y + 1, label_width, y + h - 2); - for (int j = 0 ; j < nbZones ; j++) { - gig::DimensionRegion *d = region->pDimensionRegions[c + (j << bitpos)]; - int upperLimit = d->DimensionUpperLimits[i]; - if (!upperLimit) upperLimit = d->VelocityUpperLimit; - int v = upperLimit + 1; - int x = int((w - label_width - 1) * v / 128.0 + 0.5); - window->draw_line(black, label_width + x, y + 1, label_width + x, y + h - 2); + if (y >= clipy2) break; + if (y + h > clipy1) { + // draw focus rectangle around dimension's label and zones + if (has_focus() && focus_line == i) { +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 + Gdk::Rectangle farea(0, y, 150, h); + get_style()->paint_focus(get_window(), get_state(), farea, + *this, "", + 0, y, label_width, h); +#else + get_style_context()->render_focus(cr, + 0, y, label_width, h); +#endif } - } else { - for (int j = 0 ; j <= nbZones ; j++) { - int x = int((w - label_width - 1) * j / double(nbZones) + 0.5); - window->draw_line(black, label_width + x, y + 1, label_width + x, y + h - 2); + + // draw top and bottom lines of dimension's zones + Gdk::Cairo::set_source_rgba(cr, black); + cr->move_to(label_width, y + 0.5); + cr->line_to(w, y + 0.5); + cr->move_to(w, y + h - 0.5); + cr->line_to(label_width, y + h - 0.5); + cr->stroke(); + + // erase whole dimension's zones area + Gdk::Cairo::set_source_rgba(cr, white); + cr->rectangle(label_width + 1, y + 1, + (w - label_width - 2), h - 2); + cr->fill(); + + int c = 0; + if (maindimregno >= 0) { + int mask = + ~(((1 << region->pDimensionDefinitions[i].bits) - 1) << + bitpos); + c = maindimregno & mask; // mask away this dimension } - } + bool customsplits = + ((region->pDimensionDefinitions[i].split_type == + gig::split_type_normal && + region->pDimensionRegions[c]->DimensionUpperLimits[i]) || + (region->pDimensionDefinitions[i].dimension == + gig::dimension_velocity && + region->pDimensionRegions[c]->VelocityUpperLimit)); - // draw fill for currently selected zone - if (dimregno >= 0) { - gc->set_foreground(red); - int dr = (dimregno >> bitpos) & ((1 << region->pDimensionDefinitions[i].bits) - 1); + // draw dimension zones + Gdk::Cairo::set_source_rgba(cr, black); if (customsplits) { - int x1 = 0; + cr->move_to(label_width + 0.5, y + 1); + cr->line_to(label_width + 0.5, y + h - 1); + int prevX = label_width; + int prevUpperLimit = -1; + for (int j = 0 ; j < nbZones ; j++) { - gig::DimensionRegion *d = region->pDimensionRegions[c + (j << bitpos)]; + // draw dimension zone's borders for custom splits + gig::DimensionRegion* d = + region->pDimensionRegions[c + (j << bitpos)]; int upperLimit = d->DimensionUpperLimits[i]; if (!upperLimit) upperLimit = d->VelocityUpperLimit; int v = upperLimit + 1; - int x2 = int((w - label_width - 1) * v / 128.0 + 0.5); - if (j == dr && x1 < x2) { - window->draw_rectangle(gc, true, label_width + x1 + 1, y + 1, - (x2 - x1) - 1, h - 2); - break; + int x = int((w - label_width - 1) * v / 128.0 + 0.5) + + label_width; + if (x >= clipx2) break; + if (x < clipx1) continue; + Gdk::Cairo::set_source_rgba(cr, black); + cr->move_to(x + 0.5, y + 1); + cr->line_to(x + 0.5, y + h - 1); + cr->stroke(); + + // draw fill for zone + bool isSelectedZone = this->dimzones[dimension].count(j); + bool isMainSelection = + this->maindimcase.find(dimension) != this->maindimcase.end() && + this->maindimcase[dimension] == j; + if (isMainSelection) + Gdk::Cairo::set_source_rgba(cr, blue); + else if (isSelectedZone) + cr->set_source(blueHatchedSurfacePattern); + else + Gdk::Cairo::set_source_rgba(cr, white); + + cr->rectangle(prevX + 1, y + 1, x - prevX - 1, h - 1); + cr->fill(); + + // draw text showing the beginning of the dimension zone + // as numeric value to the user + { + Glib::RefPtr layout = Pango::Layout::create(context); + layout->set_text(Glib::Ascii::dtostr(prevUpperLimit+1)); + Gdk::Cairo::set_source_rgba(cr, black); + // get the text dimensions + int text_width, text_height; + layout->get_pixel_size(text_width, text_height); + // move text to the left end of the dimension zone + cr->move_to(prevX + 3, y + (h - text_height) / 2); +#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 + } + // draw text showing the end of the dimension zone + // as numeric value to the user + { + Glib::RefPtr layout = Pango::Layout::create(context); + layout->set_text(Glib::Ascii::dtostr(upperLimit)); + Gdk::Cairo::set_source_rgba(cr, black); + // get the text dimensions + int text_width, text_height; + layout->get_pixel_size(text_width, text_height); + // move text to the left end of the dimension zone + cr->move_to(x - 3 - text_width, y + (h - text_height) / 2); +#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 } - x1 = x2; + + prevX = x; + prevUpperLimit = upperLimit; } } else { - if (dr < nbZones) { - int x1 = int((w - label_width - 1) * dr / double(nbZones) + 0.5); - int x2 = int((w - label_width - 1) * (dr + 1) / double(nbZones) + 0.5); - window->draw_rectangle(gc, true, label_width + x1 + 1, y + 1, - (x2 - x1) - 1, h - 2); - } + int prevX = 0; + for (int j = 0 ; j <= nbZones ; j++) { + // draw dimension zone's borders for normal splits + int x = int((w - label_width - 1) * j / + double(nbZones) + 0.5) + label_width; + if (x >= clipx2) break; + if (x < clipx1) continue; + Gdk::Cairo::set_source_rgba(cr, black); + cr->move_to(x + 0.5, y + 1); + cr->line_to(x + 0.5, y + h - 1); + cr->stroke(); + + if (j != 0) { + // draw fill for zone + bool isSelectedZone = this->dimzones[dimension].count(j-1); + bool isMainSelection = + this->maindimcase.find(dimension) != this->maindimcase.end() && + this->maindimcase[dimension] == (j-1); + if (isMainSelection) + Gdk::Cairo::set_source_rgba(cr, blue); + else if (isSelectedZone) + cr->set_source(blueHatchedSurfacePattern); + else + Gdk::Cairo::set_source_rgba(cr, white); + cr->rectangle(prevX + 1, y + 1, x - prevX - 1, h - 1); + cr->fill(); + + // draw text showing the beginning of the dimension zone + // as numeric value to the user + { + Glib::RefPtr layout = Pango::Layout::create(context); + layout->set_text(Glib::Ascii::dtostr((j-1) * 128/nbZones)); + Gdk::Cairo::set_source_rgba(cr, black); + // get the text dimensions + int text_width, text_height; + layout->get_pixel_size(text_width, text_height); + // move text to the left end of the dimension zone + cr->move_to(prevX + 3, y + (h - text_height) / 2); +#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 + } + // draw text showing the end of the dimension zone + // as numeric value to the user + { + Glib::RefPtr layout = Pango::Layout::create(context); + layout->set_text(Glib::Ascii::dtostr(j * 128/nbZones - 1)); + Gdk::Cairo::set_source_rgba(cr, black); + // get the text dimensions + int text_width, text_height; + layout->get_pixel_size(text_width, text_height); + // move text to the left end of the dimension zone + cr->move_to(x - 3 - text_width, y + (h - text_height) / 2); +#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 + } + } + prevX = x; + } } } - y += h; } bitpos += region->pDimensionDefinitions[i].bits; @@ -228,17 +529,10 @@ return true; } -void DimRegionChooser::on_size_request(GtkRequisition* requisition) -{ - *requisition = GtkRequisition(); - requisition->height = region ? nbDimensions * 20 : 0; - requisition->width = 800; -} - void DimRegionChooser::set_region(gig::Region* region) { this->region = region; - dimregno = 0; + maindimregno = 0; nbDimensions = 0; if (region) { int bitcount = 0; @@ -246,120 +540,240 @@ if (region->pDimensionDefinitions[dim].bits == 0) continue; nbDimensions++; - int z = std::min(dimvalue[region->pDimensionDefinitions[dim].dimension], + int z = std::min(maindimcase[region->pDimensionDefinitions[dim].dimension], region->pDimensionDefinitions[dim].zones - 1); - dimregno |= (z << bitcount); + maindimregno |= (z << bitcount); bitcount += region->pDimensionDefinitions[dim].bits; } - dimreg = region->pDimensionRegions[dimregno]; - } else { - dimreg = 0; } dimregion_selected(); + set_size_request(800, region ? nbDimensions * h : 0); + + labels_changed = true; queue_resize(); + queue_draw(); } +void DimRegionChooser::refresh_all() { + set_region(region); +} void DimRegionChooser::get_dimregions(const gig::Region* region, bool stereo, std::set& dimregs) const { - int dimregno = 0; - int bitcount = 0; - int stereo_bit = 0; - for (int dim = 0 ; dim < region->Dimensions ; dim++) { - if (region->pDimensionDefinitions[dim].bits == 0) continue; - if (stereo && - region->pDimensionDefinitions[dim].dimension == gig::dimension_samplechannel) { - stereo_bit = (1 << bitcount); - } else { - int z = std::min(dimvalue[region->pDimensionDefinitions[dim].dimension], - region->pDimensionDefinitions[dim].zones - 1); - dimregno |= (z << bitcount); + for (int iDimRgn = 0; iDimRgn < 256; ++iDimRgn) { + gig::DimensionRegion* dimRgn = region->pDimensionRegions[iDimRgn]; + if (!dimRgn) continue; + bool isValidZone; + std::map dimCase = caseOfDimRegion(dimRgn, &isValidZone); + if (!isValidZone) continue; + for (std::map::const_iterator it = dimCase.begin(); + it != dimCase.end(); ++it) + { + if (stereo && it->first == gig::dimension_samplechannel) continue; // is selected + + std::map >::const_iterator itSelectedDimension = + this->dimzones.find(it->first); + if (itSelectedDimension != this->dimzones.end() && + itSelectedDimension->second.count(it->second)) continue; // is selected + + goto notSelected; } - bitcount += region->pDimensionDefinitions[dim].bits; + + dimregs.insert(dimRgn); + + notSelected: + ; } - dimregs.insert(region->pDimensionRegions[dimregno]); - if (stereo_bit) dimregs.insert(region->pDimensionRegions[dimregno | stereo_bit]); } - -bool DimRegionChooser::on_button_release_event(GdkEventButton* event) +void DimRegionChooser::update_after_resize() { - if (resize.active) { - get_window()->pointer_ungrab(event->time); - resize.active = false; + const uint8_t upperLimit = resize.pos - 1; + gig::Instrument* instr = (gig::Instrument*)region->GetParent(); - if (region->pDimensionDefinitions[resize.dimension].dimension == gig::dimension_velocity) { + int bitpos = 0; + for (int j = 0 ; j < resize.dimension ; j++) { + bitpos += region->pDimensionDefinitions[j].bits; + } - int bitpos = 0; - for (int j = 0 ; j < resize.dimension ; j++) { - bitpos += region->pDimensionDefinitions[j].bits; + const int stereobitpos = + (modifybothchannels) ? baseBits(gig::dimension_samplechannel, region) : -1; + + // the velocity dimension must be handled differently than all other + // dimension types, because + // 1. it is currently the only dimension type which allows different zone + // sizes for different cases + // 2. for v2 format VelocityUpperLimit has to be set, DimensionUpperLimits for v3 + if (region->pDimensionDefinitions[resize.dimension].dimension == gig::dimension_velocity) { + int mask = + ~(((1 << region->pDimensionDefinitions[resize.dimension].bits) - 1) << bitpos); + int c = maindimregno & mask; // mask away this dimension + + if (region->pDimensionRegions[c]->DimensionUpperLimits[resize.dimension] == 0) { + // the velocity dimension didn't previously have + // custom v3 splits, so we initialize all splits with + // default values + int nbZones = region->pDimensionDefinitions[resize.dimension].zones; + for (int j = 0 ; j < nbZones ; j++) { + gig::DimensionRegion* d = region->pDimensionRegions[c + (j << bitpos)]; + d->DimensionUpperLimits[resize.dimension] = int(128.0 * (j + 1) / nbZones - 1); } - int mask = - ~(((1 << region->pDimensionDefinitions[resize.dimension].bits) - 1) << bitpos); - int c = dimregno & mask; // mask away this dimension - - if (region->pDimensionRegions[c]->DimensionUpperLimits[resize.dimension] == 0) { - // the velocity dimension didn't previously have - // custom v3 splits, so we initialize all splits with - // default values - int nbZones = region->pDimensionDefinitions[resize.dimension].zones; - for (int j = 0 ; j < nbZones ; j++) { - gig::DimensionRegion *d = region->pDimensionRegions[c + (j << bitpos)]; - d->DimensionUpperLimits[resize.dimension] = int(128.0 * (j + 1) / nbZones - 1); + } + if (region->pDimensionRegions[c]->VelocityUpperLimit == 0) { + // the velocity dimension didn't previously have + // custom v2 splits, so we initialize all splits with + // default values + int nbZones = region->pDimensionDefinitions[resize.dimension].zones; + for (int j = 0 ; j < nbZones ; j++) { + gig::DimensionRegion* d = region->pDimensionRegions[c + (j << bitpos)]; + d->VelocityUpperLimit = int(128.0 * (j + 1) / nbZones - 1); + } + } + + int index = c + (resize.zone << bitpos); + gig::DimensionRegion* d = region->pDimensionRegions[index]; + // update both v2 and v3 values + d->DimensionUpperLimits[resize.dimension] = upperLimit; + d->VelocityUpperLimit = upperLimit; + if (modifybothchannels && stereobitpos >= 0) { // do the same for the other audio channel's dimregion ... + gig::DimensionRegion* d = region->pDimensionRegions[index ^ (1 << stereobitpos)]; + d->DimensionUpperLimits[resize.dimension] = upperLimit; + d->VelocityUpperLimit = upperLimit; + } + + if (modifyalldimregs) { + gig::Region* rgn = NULL; + for (int key = 0; key < 128; ++key) { + if (!instr->GetRegion(key) || instr->GetRegion(key) == rgn) continue; + rgn = instr->GetRegion(key); + if (!modifyallregions && rgn != region) continue; // hack to reduce overall code amount a bit + gig::dimension_def_t* dimdef = rgn->GetDimensionDefinition(resize.dimensionDef.dimension); + if (!dimdef) continue; + if (dimdef->zones != resize.dimensionDef.zones) continue; + const int iDim = getDimensionIndex(resize.dimensionDef.dimension, rgn); + assert(iDim >= 0 && iDim < rgn->Dimensions); + + // the dimension layout might be completely different in this + // region, so we have to recalculate bitpos etc for this region + const int bitpos = baseBits(resize.dimensionDef.dimension, rgn); + const int stencil = ~(((1 << dimdef->bits) - 1) << bitpos); + const int selection = resize.zone << bitpos; + + // primitive and inefficient loop implementation, however due to + // this circumstance the loop code is much simpler, and its lack + // of runtime efficiency should not be notable in practice + for (int idr = 0; idr < 256; ++idr) { + const int index = (idr & stencil) | selection; + assert(index >= 0 && index < 256); + gig::DimensionRegion* dr = rgn->pDimensionRegions[index]; + if (!dr) continue; + dr->DimensionUpperLimits[iDim] = upperLimit; + d->VelocityUpperLimit = upperLimit; + } + } + } else if (modifyallregions) { // implies modifyalldimregs is false ... + // resolve the precise case we need to modify for all other regions + DimensionCase dimCase = dimensionCaseOf(d); + // apply the velocity upper limit change to that resolved dim case + // of all regions ... + gig::Region* rgn = NULL; + for (int key = 0; key < 128; ++key) { + if (!instr->GetRegion(key) || instr->GetRegion(key) == rgn) continue; + rgn = instr->GetRegion(key); + gig::dimension_def_t* dimdef = rgn->GetDimensionDefinition(resize.dimensionDef.dimension); + if (!dimdef) continue; + if (dimdef->zones != resize.dimensionDef.zones) continue; + const int iDim = getDimensionIndex(resize.dimensionDef.dimension, rgn); + assert(iDim >= 0 && iDim < rgn->Dimensions); + + std::vector dimrgns = dimensionRegionsMatching(dimCase, rgn); + for (int i = 0; i < dimrgns.size(); ++i) { + gig::DimensionRegion* dr = dimrgns[i]; + dr->DimensionUpperLimits[iDim] = upperLimit; + dr->VelocityUpperLimit = upperLimit; } } - if (region->pDimensionRegions[c]->VelocityUpperLimit == 0) { - // the velocity dimension didn't previously have - // custom v2 splits, so we initialize all splits with - // default values + } + } else { + for (int i = 0 ; i < region->DimensionRegions ; ) { + if (region->pDimensionRegions[i]->DimensionUpperLimits[resize.dimension] == 0) { + // the dimension didn't previously have custom + // limits, so we have to set default limits for + // all the dimension regions int nbZones = region->pDimensionDefinitions[resize.dimension].zones; + for (int j = 0 ; j < nbZones ; j++) { - gig::DimensionRegion *d = region->pDimensionRegions[c + (j << bitpos)]; - d->VelocityUpperLimit = int(128.0 * (j + 1) / nbZones - 1); + gig::DimensionRegion* d = region->pDimensionRegions[i + (j << bitpos)]; + d->DimensionUpperLimits[resize.dimension] = int(128.0 * (j + 1) / nbZones - 1); } } - - gig::DimensionRegion *d = region->pDimensionRegions[c + resize.offset]; - // update both v2 and v3 values - d->DimensionUpperLimits[resize.dimension] = resize.pos - 1; - d->VelocityUpperLimit = resize.pos - 1; - - } else { - for (int i = 0 ; i < region->DimensionRegions ; ) { - - if (region->pDimensionRegions[i]->DimensionUpperLimits[resize.dimension] == 0) { - // the dimension didn't previously have custom - // limits, so we have to set default limits for - // all the dimension regions - int bitpos = 0; - for (int j = 0 ; j < resize.dimension ; j++) { - bitpos += region->pDimensionDefinitions[j].bits; - } - int nbZones = region->pDimensionDefinitions[resize.dimension].zones; - - for (int j = 0 ; j < nbZones ; j++) { - gig::DimensionRegion *d = region->pDimensionRegions[i + (j << bitpos)]; - d->DimensionUpperLimits[resize.dimension] = int(128.0 * (j + 1) / nbZones - 1); - } + int index = i + (resize.zone << bitpos); + gig::DimensionRegion* d = region->pDimensionRegions[index]; + d->DimensionUpperLimits[resize.dimension] = upperLimit; +#if 0 // the following is currently not necessary, because ATM the gig format uses for all dimension types except of the veleocity dimension the same zone sizes for all cases + if (modifybothchannels && stereobitpos >= 0) { // do the same for the other audio channel's dimregion ... + gig::DimensionRegion* d = region->pDimensionRegions[index ^ (1 << stereobitpos)]; + d->DimensionUpperLimits[resize.dimension] = upperLimit; + } +#endif + int bitpos = 0; + int j; + for (j = 0 ; j < region->Dimensions ; j++) { + if (j != resize.dimension) { + int maxzones = 1 << region->pDimensionDefinitions[j].bits; + int dimj = (i >> bitpos) & (maxzones - 1); + if (dimj + 1 < region->pDimensionDefinitions[j].zones) break; } - gig::DimensionRegion *d = region->pDimensionRegions[i + resize.offset]; - d->DimensionUpperLimits[resize.dimension] = resize.pos - 1; + bitpos += region->pDimensionDefinitions[j].bits; + } + if (j == region->Dimensions) break; + i = (i & ~((1 << bitpos) - 1)) + (1 << bitpos); + } - int bitpos = 0; - int j; - for (j = 0 ; j < region->Dimensions ; j++) { - if (j != resize.dimension) { - int maxzones = 1 << region->pDimensionDefinitions[j].bits; - int dimj = (i >> bitpos) & (maxzones - 1); - if (dimj + 1 < region->pDimensionDefinitions[j].zones) break; - } - bitpos += region->pDimensionDefinitions[j].bits; + if (modifyallregions) { // TODO: this code block could be merged with the similar (and more generalized) code block of the velocity dimension above + gig::Region* rgn = NULL; + for (int key = 0; key < 128; ++key) { + if (!instr->GetRegion(key) || instr->GetRegion(key) == rgn) continue; + rgn = instr->GetRegion(key); + gig::dimension_def_t* dimdef = rgn->GetDimensionDefinition(resize.dimensionDef.dimension); + if (!dimdef) continue; + if (dimdef->zones != resize.dimensionDef.zones) continue; + const int iDim = getDimensionIndex(resize.dimensionDef.dimension, rgn); + assert(iDim >= 0 && iDim < rgn->Dimensions); + + // the dimension layout might be completely different in this + // region, so we have to recalculate bitpos etc for this region + const int bitpos = baseBits(resize.dimensionDef.dimension, rgn); + const int stencil = ~(((1 << dimdef->bits) - 1) << bitpos); + const int selection = resize.zone << bitpos; + + // this loop implementation is less efficient than the above's + // loop implementation (which skips unnecessary dimension regions) + // however this code is much simpler, and its lack of runtime + // efficiency should not be notable in practice + for (int idr = 0; idr < 256; ++idr) { + const int index = (idr & stencil) | selection; + assert(index >= 0 && index < 256); + gig::DimensionRegion* dr = rgn->pDimensionRegions[index]; + if (!dr) continue; + dr->DimensionUpperLimits[iDim] = upperLimit; } - if (j == region->Dimensions) break; - i = (i & ~((1 << bitpos) - 1)) + (1 << bitpos); } } + } +} + +bool DimRegionChooser::on_button_release_event(GdkEventButton* event) +{ + if (resize.active) { +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 + get_window()->pointer_ungrab(event->time); +#else + Glib::wrap(event->device, true)->ungrab(event->time); +#endif + resize.active = false; + region_changed(); if (!is_in_resize_zone(event->x, event->y) && cursor_is_resize) { @@ -372,16 +786,28 @@ bool DimRegionChooser::on_button_press_event(GdkEventButton* event) { + int w = get_width(); if (region && event->y < nbDimensions * h && event->x >= label_width && event->x < w) { if (is_in_resize_zone(event->x, event->y)) { - Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW); +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 get_window()->pointer_grab(false, Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK, - double_arrow, event->time); + Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW), + event->time); +#else + Glib::wrap(event->device, true)->grab(get_window(), + Gdk::OWNERSHIP_NONE, + false, + Gdk::BUTTON_RELEASE_MASK | + Gdk::POINTER_MOTION_MASK | + Gdk::POINTER_MOTION_HINT_MASK, + Gdk::Cursor::create(Gdk::SB_H_DOUBLE_ARROW), + event->time); +#endif resize.active = true; } else { int ydim = int(event->y / h); @@ -400,9 +826,9 @@ } int i = dim; - if (dimregno < 0) dimregno = 0; + if (maindimregno < 0) maindimregno = 0; int mask = ~(((1 << region->pDimensionDefinitions[i].bits) - 1) << bitpos); - int c = dimregno & mask; // mask away this dimension + int c = this->maindimregno & mask; // mask away this dimension bool customsplits = ((region->pDimensionDefinitions[i].split_type == gig::split_type_normal && @@ -414,12 +840,12 @@ if (region->pDimensionRegions[c]->DimensionUpperLimits[i]) { for (z = 0 ; z < nbZones ; z++) { - gig::DimensionRegion *d = region->pDimensionRegions[c + (z << bitpos)]; + gig::DimensionRegion* d = region->pDimensionRegions[c + (z << bitpos)]; if (val <= d->DimensionUpperLimits[i]) break; } } else { for (z = 0 ; z < nbZones ; z++) { - gig::DimensionRegion *d = region->pDimensionRegions[c + (z << bitpos)]; + gig::DimensionRegion* d = region->pDimensionRegions[c + (z << bitpos)]; if (val <= d->VelocityUpperLimit) break; } } @@ -432,15 +858,38 @@ region->pDimensionDefinitions[dim].split_type, region->pDimensionDefinitions[dim].zones, region->pDimensionDefinitions[dim].zone_size); - dimvalue[region->pDimensionDefinitions[dim].dimension] = z; - - dimregno = c | (z << bitpos); + this->maindimcase[region->pDimensionDefinitions[dim].dimension] = z; + this->maindimregno = c | (z << bitpos); + this->maindimtype = region->pDimensionDefinitions[dim].dimension; + + if (multiSelectKeyDown) { + if (dimzones[this->maindimtype].count(z)) { + if (dimzones[this->maindimtype].size() > 1) { + dimzones[this->maindimtype].erase(z); + } + } else { + dimzones[this->maindimtype].insert(z); + } + } else { + this->dimzones.clear(); + for (std::map::const_iterator it = this->maindimcase.begin(); + it != this->maindimcase.end(); ++it) + { + this->dimzones[it->first].insert(it->second); + } + } focus_line = dim; if (has_focus()) queue_draw(); else grab_focus(); - dimreg = region->pDimensionRegions[dimregno]; dimregion_selected(); + + if (event->button == 3) { + printf("dimregion right click\n"); + popup_menu_inside_dimregion->popup(event->button, event->time); + } + + queue_draw(); } } return true; @@ -454,6 +903,7 @@ window->get_pointer(x, y, state); if (resize.active) { + int w = get_width(); int k = int((x - label_width) * 128.0 / (w - label_width - 1) + 0.5); if (k < resize.min) k = resize.min; @@ -462,48 +912,32 @@ if (k < 2) k = 2; // k is upper limit + 1, upper limit 0 is forbidden if (k != resize.pos) { - Glib::RefPtr black = get_style()->get_black_gc(); - Glib::RefPtr white = get_style()->get_white_gc(); - int prevx = int((w - label_width - 1) * resize.pos / 128.0 + 0.5) + label_width; int x = int((w - label_width - 1) * k / 128.0 + 0.5) + label_width; int y = resize.dimension * h; - - if (resize.selected == resize.none) { - if (resize.pos != resize.min && resize.pos != resize.max) { - window->draw_line(white, prevx, y + 1, prevx, y + h - 2); - } + int x1, x2; + if (k > resize.pos) { + x1 = prevx; + x2 = x; } else { - gc->set_foreground(red); - - Glib::RefPtr left; - Glib::RefPtr right; - if (resize.selected == resize.left) { - left = gc; - right = white; - } else { - left = white; - right = gc; - } - - if (k > resize.pos) { - int xx = resize.pos == resize.min ? 1 : 0; - window->draw_rectangle(left, true, - prevx + xx, y + 1, x - prevx - xx, h - 2); - } else { - int xx = resize.pos == resize.max ? 0 : 1; - window->draw_rectangle(right, true, - x, y + 1, prevx - x + xx, h - 2); - } + x1 = x; + x2 = prevx; } - window->draw_line(black, x, y + 1, x, y + h - 2); + Gdk::Rectangle rect(x1, y + 1, x2 - x1 + 1, h - 2); + resize.pos = k; + update_after_resize(); + get_window()->invalidate_rect(rect, false); // not sufficient ... + queue_draw(); // ... so do a complete redraw instead. } } else { if (is_in_resize_zone(x, y)) { if (!cursor_is_resize) { - Gdk::Cursor double_arrow(Gdk::SB_H_DOUBLE_ARROW); - window->set_cursor(double_arrow); +#if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 + window->set_cursor(Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW)); +#else + window->set_cursor(Gdk::Cursor::create(Gdk::SB_H_DOUBLE_ARROW)); +#endif cursor_is_resize = true; } } else if (cursor_is_resize) { @@ -516,6 +950,7 @@ bool DimRegionChooser::is_in_resize_zone(double x, double y) { + int w = get_width(); if (region && y < nbDimensions * h && x >= label_width && x < w) { int ydim = int(y / h); int dim; @@ -529,9 +964,9 @@ int nbZones = region->pDimensionDefinitions[dim].zones; int c = 0; - if (dimregno >= 0) { + if (maindimregno >= 0) { int mask = ~(((1 << region->pDimensionDefinitions[dim].bits) - 1) << bitpos); - c = dimregno & mask; // mask away this dimension + c = maindimregno & mask; // mask away this dimension } const bool customsplits = ((region->pDimensionDefinitions[dim].split_type == gig::split_type_normal && @@ -543,7 +978,7 @@ if (region->pDimensionDefinitions[dim].split_type != gig::split_type_bit) { int prev_limit = 0; for (int iZone = 0 ; iZone < nbZones - 1 ; iZone++) { - gig::DimensionRegion *d = region->pDimensionRegions[c + (iZone << bitpos)]; + gig::DimensionRegion* d = region->pDimensionRegions[c + (iZone << bitpos)]; const int upperLimit = (customsplits) ? (d->DimensionUpperLimits[dim]) ? @@ -554,17 +989,18 @@ if (x <= limitx - 2) break; if (x <= limitx + 2) { resize.dimension = dim; - resize.offset = iZone << bitpos; + resize.dimensionDef = region->pDimensionDefinitions[dim]; + resize.zone = iZone; resize.pos = limit; resize.min = prev_limit; - int dr = (dimregno >> bitpos) & + int dr = (maindimregno >> bitpos) & ((1 << region->pDimensionDefinitions[dim].bits) - 1); resize.selected = dr == iZone ? resize.left : dr == iZone + 1 ? resize.right : resize.none; iZone++; - gig::DimensionRegion *d = region->pDimensionRegions[c + (iZone << bitpos)]; + gig::DimensionRegion* d = region->pDimensionRegions[c + (iZone << bitpos)]; const int upperLimit = (customsplits) ? @@ -595,8 +1031,8 @@ bool DimRegionChooser::on_focus(Gtk::DirectionType direction) { - // TODO: kolla att region finns osv, dvs att det går att sätta - // fokus. + // TODO: check that region exists etc, that is, that it's possible + // to set focus if (direction == Gtk::DIR_TAB_FORWARD || direction == Gtk::DIR_DOWN) { if (!has_focus()) { @@ -628,10 +1064,332 @@ } } } else if (!has_focus()) { - // TODO: kolla att focus_line finns! + // TODO: check that focus_line exists grab_focus(); return true; } else { - // TODO: öka eller minska värde! + // TODO: increase or decrease value + } + return false; +} + +void DimRegionChooser::split_dimension_zone() { + printf("split_dimension_zone() type=%d, zone=%d\n", maindimtype, maindimcase[maindimtype]); + try { + if (!modifyallregions) { + region->SplitDimensionZone(maindimtype, maindimcase[maindimtype]); + } else { + gig::Instrument* instr = (gig::Instrument*)region->GetParent(); + gig::dimension_def_t* pMaindimdef = region->GetDimensionDefinition(maindimtype); + assert(pMaindimdef != NULL); + // retain structure by value since the original region will be + // modified in the loop below as well + gig::dimension_def_t maindimdef = *pMaindimdef; + std::vector ignoredAll; + std::vector ignoredMinor; + std::vector ignoredCritical; + gig::Region* rgn = NULL; + for (int key = 0; key < 128; ++key) { + if (!instr->GetRegion(key) || instr->GetRegion(key) == rgn) continue; + rgn = instr->GetRegion(key); + + // ignore all regions which do not exactly match the dimension + // layout of the selected region where this operation was emitted + gig::dimension_def_t* dimdef = rgn->GetDimensionDefinition(maindimtype); + if (!dimdef) { + ignoredAll.push_back(rgn); + ignoredMinor.push_back(rgn); + continue; + } + if (dimdef->zones != maindimdef.zones) { + ignoredAll.push_back(rgn); + ignoredCritical.push_back(rgn); + continue; + } + + rgn->SplitDimensionZone(maindimtype, maindimcase[maindimtype]); + } + if (!ignoredAll.empty()) { + Glib::ustring txt; + if (ignoredCritical.empty()) + txt = ToString(ignoredMinor.size()) + _(" regions have been ignored since they don't have that dimension type."); + else if (ignoredMinor.empty()) + txt = ToString(ignoredCritical.size()) + _(" regions have been ignored due to different amount of dimension zones!"); + else + txt = ToString(ignoredCritical.size()) + _(" regions have been ignored due to different amount of dimension zones (and ") + + ToString(ignoredMinor.size()) + _(" regions have been ignored since they don't have that dimension type)!"); + Gtk::MessageType type = (ignoredCritical.empty()) ? Gtk::MESSAGE_INFO : Gtk::MESSAGE_WARNING; + Gtk::MessageDialog msg(txt, false, type); + msg.run(); + } + } + } catch (RIFF::Exception e) { + Gtk::MessageDialog msg(e.Message, false, Gtk::MESSAGE_ERROR); + msg.run(); + } catch (...) { + Glib::ustring txt = _("An unknown exception occurred!"); + Gtk::MessageDialog msg(txt, false, Gtk::MESSAGE_ERROR); + msg.run(); + } + refresh_all(); +} + +void DimRegionChooser::delete_dimension_zone() { + printf("delete_dimension_zone() type=%d, zone=%d\n", maindimtype, maindimcase[maindimtype]); + try { + if (!modifyallregions) { + region->DeleteDimensionZone(maindimtype, maindimcase[maindimtype]); + } else { + gig::Instrument* instr = (gig::Instrument*)region->GetParent(); + gig::dimension_def_t* pMaindimdef = region->GetDimensionDefinition(maindimtype); + assert(pMaindimdef != NULL); + // retain structure by value since the original region will be + // modified in the loop below as well + gig::dimension_def_t maindimdef = *pMaindimdef; + std::vector ignoredAll; + std::vector ignoredMinor; + std::vector ignoredCritical; + gig::Region* rgn = NULL; + for (int key = 0; key < 128; ++key) { + if (!instr->GetRegion(key) || instr->GetRegion(key) == rgn) continue; + rgn = instr->GetRegion(key); + + // ignore all regions which do not exactly match the dimension + // layout of the selected region where this operation was emitted + gig::dimension_def_t* dimdef = rgn->GetDimensionDefinition(maindimtype); + if (!dimdef) { + ignoredAll.push_back(rgn); + ignoredMinor.push_back(rgn); + continue; + } + if (dimdef->zones != maindimdef.zones) { + ignoredAll.push_back(rgn); + ignoredCritical.push_back(rgn); + continue; + } + + rgn->DeleteDimensionZone(maindimtype, maindimcase[maindimtype]); + } + if (!ignoredAll.empty()) { + Glib::ustring txt; + if (ignoredCritical.empty()) + txt = ToString(ignoredMinor.size()) + _(" regions have been ignored since they don't have that dimension type."); + else if (ignoredMinor.empty()) + txt = ToString(ignoredCritical.size()) + _(" regions have been ignored due to different amount of dimension zones!"); + else + txt = ToString(ignoredCritical.size()) + _(" regions have been ignored due to different amount of dimension zones (and ") + + ToString(ignoredMinor.size()) + _(" regions have been ignored since they don't have that dimension type)!"); + Gtk::MessageType type = (ignoredCritical.empty()) ? Gtk::MESSAGE_INFO : Gtk::MESSAGE_WARNING; + Gtk::MessageDialog msg(txt, false, type); + msg.run(); + } + } + } catch (RIFF::Exception e) { + Gtk::MessageDialog msg(e.Message, false, Gtk::MESSAGE_ERROR); + msg.run(); + } catch (...) { + Glib::ustring txt = _("An unknown exception occurred!"); + Gtk::MessageDialog msg(txt, false, Gtk::MESSAGE_ERROR); + msg.run(); + } + refresh_all(); +} + +// Cmd key on Mac, Ctrl key on all other OSs +static const guint primaryKeyL = + #if defined(__APPLE__) + GDK_KEY_Meta_L; + #else + GDK_KEY_Control_L; + #endif + +static const guint primaryKeyR = + #if defined(__APPLE__) + GDK_KEY_Meta_R; + #else + GDK_KEY_Control_R; + #endif + +bool DimRegionChooser::onKeyPressed(GdkEventKey* key) { + //printf("key down 0x%x\n", key->keyval); + if (key->keyval == GDK_KEY_Control_L || key->keyval == GDK_KEY_Control_R) + multiSelectKeyDown = true; + if (key->keyval == primaryKeyL || key->keyval == primaryKeyR) + primaryKeyDown = true; + if (key->keyval == GDK_KEY_Shift_L || key->keyval == GDK_KEY_Shift_R) + shiftKeyDown = true; + + //FIXME: hmm, for some reason GDKMM does not fire arrow key down events, so we are doing those handlers in the key up handler instead for now + /*if (key->keyval == GDK_KEY_Left) + select_prev_dimzone(); + if (key->keyval == GDK_KEY_Right) + select_next_dimzone(); + if (key->keyval == GDK_KEY_Up) + select_prev_dimension(); + if (key->keyval == GDK_KEY_Down) + select_next_dimension();*/ + return false; +} + +bool DimRegionChooser::onKeyReleased(GdkEventKey* key) { + //printf("key up 0x%x\n", key->keyval); + if (key->keyval == GDK_KEY_Control_L || key->keyval == GDK_KEY_Control_R) + multiSelectKeyDown = false; + if (key->keyval == primaryKeyL || key->keyval == primaryKeyR) + primaryKeyDown = false; + if (key->keyval == GDK_KEY_Shift_L || key->keyval == GDK_KEY_Shift_R) + shiftKeyDown = false; + + if (!has_focus()) return false; + + // avoid conflict with Ctrl+Left and Ctrl+Right accelerators on mainwindow + // (which is supposed to switch between regions) + if (primaryKeyDown) return false; + + // avoid conflict with Alt+Shift+Left and Alt+Shift+Right accelerators on + // mainwindow + if (shiftKeyDown) return false; + + if (key->keyval == GDK_KEY_Left) + select_prev_dimzone(); + if (key->keyval == GDK_KEY_Right) + select_next_dimzone(); + if (key->keyval == GDK_KEY_Up) + select_prev_dimension(); + if (key->keyval == GDK_KEY_Down) + select_next_dimension(); + + return false; +} + +void DimRegionChooser::resetSelectedZones() { + this->dimzones.clear(); + if (!region) { + queue_draw(); // redraw required parts + return; + } + if (maindimregno < 0 || maindimregno >= region->DimensionRegions) { + queue_draw(); // redraw required parts + return; } + if (!region->pDimensionRegions[maindimregno]) { + queue_draw(); // redraw required parts + return; + } + gig::DimensionRegion* dimrgn = region->pDimensionRegions[maindimregno]; + + bool isValidZone; + this->maindimcase = dimensionCaseOf(dimrgn); + + for (std::map::const_iterator it = this->maindimcase.begin(); + it != this->maindimcase.end(); ++it) + { + this->dimzones[it->first].insert(it->second); + } + + // redraw required parts + queue_draw(); +} + +bool DimRegionChooser::select_dimregion(gig::DimensionRegion* dimrgn) { + if (!region) return false; //.selection failed + + for (int dr = 0; dr < region->DimensionRegions && region->pDimensionRegions[dr]; ++dr) { + if (region->pDimensionRegions[dr] == dimrgn) { + // reset dim region zone selection to the requested specific dim region case + maindimregno = dr; + resetSelectedZones(); + + // emit signal that dimregion selection has changed, for external entities + dimregion_selected(); + + return true; // selection success + } + } + + return false; //.selection failed +} + +void DimRegionChooser::select_next_dimzone(bool add) { + select_dimzone_by_dir(+1, add); +} + +void DimRegionChooser::select_prev_dimzone(bool add) { + select_dimzone_by_dir(-1, add); +} + +void DimRegionChooser::select_dimzone_by_dir(int dir, bool add) { + if (!region) return; + if (!region->Dimensions) return; + if (focus_line < 0) focus_line = 0; + if (focus_line >= region->Dimensions) focus_line = region->Dimensions - 1; + + maindimtype = region->pDimensionDefinitions[focus_line].dimension; + if (maindimtype == gig::dimension_none) { + printf("maindimtype -> none\n"); + return; + } + + if (maindimcase.empty()) { + maindimcase = dimensionCaseOf(region->pDimensionRegions[maindimregno]); + if (maindimcase.empty()) { + printf("caseOfDimregion(%d) -> empty\n", maindimregno); + return; + } + } + + int z = (dir > 0) ? maindimcase[maindimtype] + 1 : maindimcase[maindimtype] - 1; + if (z < 0) z = 0; + if (z >= region->pDimensionDefinitions[focus_line].zones) + z = region->pDimensionDefinitions[focus_line].zones - 1; + + maindimcase[maindimtype] = z; + + ::gig::DimensionRegion* dr = dimensionRegionMatching(maindimcase, region); + if (!dr) { + printf("select_dimzone_by_dir(%d) -> !dr\n", dir); + return; + } + + maindimregno = getDimensionRegionIndex(dr); + + if (!add) { + // reset selected dimregion zones + dimzones.clear(); + } + for (DimensionCase::const_iterator it = maindimcase.begin(); + it != maindimcase.end(); ++it) + { + dimzones[it->first].insert(it->second); + } + + dimregion_selected(); + + // disabled: would overwrite dimregno with wrong value + //refresh_all(); + // so requesting just a raw repaint instead: + queue_draw(); +} + +void DimRegionChooser::select_next_dimension() { + if (!region) return; + focus_line++; + if (focus_line >= region->Dimensions) + focus_line = region->Dimensions - 1; + this->maindimtype = region->pDimensionDefinitions[focus_line].dimension; + queue_draw(); +} + +void DimRegionChooser::select_prev_dimension() { + if (!region) return; + focus_line--; + if (focus_line < 0) + focus_line = 0; + this->maindimtype = region->pDimensionDefinitions[focus_line].dimension; + queue_draw(); +} + +gig::DimensionRegion* DimRegionChooser::get_main_dimregion() const { + if (!region) return NULL; + return region->pDimensionRegions[maindimregno]; }