--- gigedit/trunk/src/gigedit/dimregionchooser.cpp 2011/08/19 10:55:41 2246 +++ gigedit/trunk/src/gigedit/dimregionchooser.cpp 2017/11/14 18:07:25 3364 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2011 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,37 +17,396 @@ * 02110-1301 USA. */ +#include "global.h" +#include "compat.h" +#include #include "dimregionchooser.h" #include +#include #include #include +#if HAS_GDKMM_SEAT +# include +#endif +#include +#if HAS_GTKMM_STOCK +# include +#endif +#include +#include +#include + +#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; + } -#include "global.h" + 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(); + } -DimRegionChooser::DimRegionChooser() : - red("#8070ff"), + 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(); + } + } + + *isValidZone = true; + return dimCase; +} + +DimRegionChooser::DimRegionChooser(Gtk::Window& window) : + red("#ff476e"), + blue("#4796ff"), black("black"), white("white") { + // make sure blue hatched pattern pixmap is loaded + loadBuiltInPix(); + + // create blue hatched pattern 1 + { + 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( +#if HAS_CAIROMM_CPP11_ENUMS + this->blueHatchedPatternARGB->get_pixels(), Cairo::Surface::Format::ARGB32, width, height, stride +#else + this->blueHatchedPatternARGB->get_pixels(), Cairo::FORMAT_ARGB32, width, height, stride +#endif + ); + this->blueHatchedSurfacePattern = Cairo::SurfacePattern::create(imageSurface); +#if HAS_CAIROMM_CPP11_ENUMS + this->blueHatchedSurfacePattern->set_extend(Cairo::Pattern::Extend::REPEAT); +#else + this->blueHatchedSurfacePattern->set_extend(Cairo::EXTEND_REPEAT); +#endif + } + + // create blue hatched pattern 2 + { + const int width = blueHatchedPattern2->get_width(); + const int height = blueHatchedPattern2->get_height(); + const int stride = blueHatchedPattern2->get_rowstride(); + + // manually convert from RGBA to ARGB + this->blueHatchedPattern2ARGB = blueHatchedPattern2->copy(); + const int pixelSize = stride / width; + const int totalPixels = width * height; + assert(pixelSize == 4); + unsigned char* ptr = this->blueHatchedPattern2ARGB->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( +#if HAS_CAIROMM_CPP11_ENUMS + this->blueHatchedPattern2ARGB->get_pixels(), Cairo::Surface::Format::ARGB32, width, height, stride +#else + this->blueHatchedPattern2ARGB->get_pixels(), Cairo::FORMAT_ARGB32, width, height, stride +#endif + ); + this->blueHatchedSurfacePattern2 = Cairo::SurfacePattern::create(imageSurface); +#if HAS_CAIROMM_CPP11_ENUMS + this->blueHatchedSurfacePattern2->set_extend(Cairo::Pattern::Extend::REPEAT); +#else + this->blueHatchedSurfacePattern2->set_extend(Cairo::EXTEND_REPEAT); +#endif + } + + // create gray blue hatched pattern + { + const int width = grayBlueHatchedPattern->get_width(); + const int height = grayBlueHatchedPattern->get_height(); + const int stride = grayBlueHatchedPattern->get_rowstride(); + + // manually convert from RGBA to ARGB + this->grayBlueHatchedPatternARGB = grayBlueHatchedPattern->copy(); + const int pixelSize = stride / width; + const int totalPixels = width * height; + assert(pixelSize == 4); + unsigned char* ptr = this->grayBlueHatchedPatternARGB->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( +#if HAS_CAIROMM_CPP11_ENUMS + this->grayBlueHatchedPatternARGB->get_pixels(), Cairo::Surface::Format::ARGB32, width, height, stride +#else + this->grayBlueHatchedPatternARGB->get_pixels(), Cairo::FORMAT_ARGB32, width, height, stride +#endif + ); + this->grayBlueHatchedSurfacePattern = Cairo::SurfacePattern::create(imageSurface); +#if HAS_CAIROMM_CPP11_ENUMS + this->grayBlueHatchedSurfacePattern->set_extend(Cairo::Pattern::Extend::REPEAT); +#else + this->grayBlueHatchedSurfacePattern->set_extend(Cairo::EXTEND_REPEAT); +#endif + } + 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; + 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 = ActionGroup::create(); +#if USE_GLIB_ACTION + actionSplitDimZone = actionGroup->add_action( + "SplitDimZone", sigc::mem_fun(*this, &DimRegionChooser::split_dimension_zone) + ); + actionDeleteDimZone = actionGroup->add_action( + "DeleteDimZone", sigc::mem_fun(*this, &DimRegionChooser::delete_dimension_zone) + ); + insert_action_group("PopupMenuInsideDimRegion", actionGroup); +#else + actionSplitDimZone = 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) + ); +#endif + +#if USE_GTKMM_BUILDER + uiManager = Gtk::Builder::create(); + Glib::ustring ui_info = + "" + " " + "
" + " " + " Split Dimensions Zone" + " PopupMenuInsideDimRegion.SplitDimZone" + " " + " " + " Delete Dimension Zone" + " PopupMenuInsideDimRegion.DeleteDimZone" + " " + "
" + "
" + "
"; + uiManager->add_from_string(ui_info); + + popup_menu_inside_dimregion = new Gtk::Menu( + Glib::RefPtr::cast_dynamic( + uiManager->get_object("menu-PopupMenuInsideDimRegion") + ) + ); +#else + 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")); + +#endif // USE_GTKMM_BUILDER + + +#if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION > 22) +# warning GTKMM4 event registration code missing for dimregionchooser! + //add_events(Gdk::EventMask::BUTTON_PRESS_MASK); +#else add_events(Gdk::BUTTON_PRESS_MASK | Gdk::POINTER_MOTION_MASK | Gdk::POINTER_MOTION_HINT_MASK); +#endif - 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::setModifyBothChannels(bool b) { + modifybothchannels = b; + // redraw required parts + queue_draw(); +} + +void DimRegionChooser::setModifyAllDimensionRegions(bool b) { + modifyalldimregs = b; + // redraw required parts + queue_draw(); +} + +void DimRegionChooser::setModifyAllRegions(bool b) { + modifyallregions = b; + +#if USE_GTKMM_BUILDER + auto menuItemSplit = Glib::RefPtr::cast_dynamic( + uiManager->get_object("item-split") + ); + auto menuItemDelete = Glib::RefPtr::cast_dynamic( + uiManager->get_object("item-delete") + ); + menuItemDelete->set_label(b ? _("Delete Dimension Zone [ALL REGIONS]") : _("Delete Dimension Zone")); + menuItemSplit->set_label(b ? _("Split Dimensions Zone [ALL REGIONS]") : _("Split Dimensions Zone")); +#else + actionDeleteDimZone->set_label(b ? _("Delete Dimension Zone [ALL REGIONS]") : _("Delete Dimension Zone")); + actionSplitDimZone->set_label(b ? _("Split Dimensions Zone [ALL REGIONS]") : _("Split Dimensions Zone")); +#endif + + // redraw required parts + queue_draw(); +} + +void DimRegionChooser::drawIconsFor( + gig::dimension_t dimension, uint zone, + const Cairo::RefPtr& cr, + int x, int y, int w, int h) +{ + DimensionCase dimCase; + dimCase[dimension] = zone; + + std::vector dimregs = + dimensionRegionsMatching(dimCase, region, true); + + if (dimregs.empty()) return; + + int iSampleRefs = 0; + int iLoops = 0; + + for (uint i = 0; i < dimregs.size(); ++i) { + if (dimregs[i]->pSample) iSampleRefs++; + if (dimregs[i]->SampleLoops) iLoops++; + } + + bool bShowLoopSymbol = (iLoops > 0); + bool bShowSampleRefSymbol = (iSampleRefs < dimregs.size()); + + if (bShowLoopSymbol || bShowSampleRefSymbol) { + const int margin = 1; + + cr->save(); + cr->set_line_width(1); + cr->rectangle(x, y + margin, w, h - 2*margin); + cr->clip(); + if (bShowSampleRefSymbol) { + const int wPic = 8; + const int hPic = 8; + Gdk::Cairo::set_source_pixbuf( + cr, (iSampleRefs) ? yellowDot : redDot, + x + (w-wPic)/2.f, + y + ( + (bShowLoopSymbol) ? margin : (h-hPic)/2.f + ) + ); + cr->paint(); + } + if (bShowLoopSymbol) { + const int wPic = 12; + const int hPic = 14; + Gdk::Cairo::set_source_pixbuf( + cr, (iLoops == dimregs.size()) ? blackLoop : grayLoop, + x + (w-wPic)/2.f, + y + ( + (bShowSampleRefSymbol) ? h - hPic - margin : (h-hPic)/2.f + ) + ); + cr->paint(); + } + cr->restore(); + } +} + #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 bool DimRegionChooser::on_expose_event(GdkEventExpose* e) { @@ -58,9 +417,6 @@ const Cairo::RefPtr& cr = get_window()->create_cairo_context(); -#if 0 -} -#endif #else bool DimRegionChooser::on_draw(const Cairo::RefPtr& cr) { @@ -139,9 +495,19 @@ dstr = dstrbuf; break; } - layout->set_text(dstr); + // 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; @@ -152,7 +518,11 @@ const Gdk::Color fg = get_style()->get_fg(get_state()); #else const Gdk::RGBA fg = +# if GTKMM_MAJOR_VERSION >= 3 + get_style_context()->get_color(); +# else get_style_context()->get_color(get_state_flags()); +# endif #endif Gdk::Cairo::set_source_rgba(cr, fg); cr->move_to(4, int(y + (h - text_h) / 2 + 0.5)); @@ -176,18 +546,20 @@ for (int i = 0 ; i < region->Dimensions ; i++) { int nbZones = region->pDimensionDefinitions[i].zones; if (nbZones) { + const gig::dimension_t dimension = region->pDimensionDefinitions[i].dimension; + 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, 20); + Gdk::Rectangle farea(0, y, 150, h); get_style()->paint_focus(get_window(), get_state(), farea, *this, "", - 0, y, label_width, 20); + 0, y, label_width, h); #else get_style_context()->render_focus(cr, - 0, y, label_width, 20); + 0, y, label_width, h); #endif } @@ -206,11 +578,11 @@ cr->fill(); int c = 0; - if (dimregno >= 0) { + if (maindimregno >= 0) { int mask = ~(((1 << region->pDimensionDefinitions[i].bits) - 1) << bitpos); - c = dimregno & mask; // mask away this dimension + c = maindimregno & mask; // mask away this dimension } bool customsplits = ((region->pDimensionDefinitions[i].split_type == @@ -220,13 +592,16 @@ gig::dimension_velocity && region->pDimensionRegions[c]->VelocityUpperLimit)); - // draw dimension's zone borders + // draw dimension zones Gdk::Cairo::set_source_rgba(cr, black); if (customsplits) { 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++) { + // draw dimension zone's borders for custom splits gig::DimensionRegion* d = region->pDimensionRegions[c + (j << bitpos)]; int upperLimit = d->DimensionUpperLimits[i]; @@ -236,60 +611,153 @@ 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; + bool isCheckBoxSelected = + modifyalldimregs || + (modifybothchannels && + dimension == gig::dimension_samplechannel); + if (isMainSelection) + Gdk::Cairo::set_source_rgba(cr, blue); + else if (isSelectedZone) + cr->set_source(blueHatchedSurfacePattern2); + else if (isCheckBoxSelected) + cr->set_source(blueHatchedSurfacePattern); + else + Gdk::Cairo::set_source_rgba(cr, white); + + const int wZone = x - prevX - 1; + + cr->rectangle(prevX + 1, y + 1, wZone, h - 1); + cr->fill(); + + // draw icons + drawIconsFor(dimension, j, cr, prevX, y, wZone, h); + + // 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 + } + + prevX = x; + prevUpperLimit = upperLimit; } } else { + 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(); + cr->stroke(); + + if (j != 0) { + const int wZone = x - prevX - 1; + + // 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); + bool isCheckBoxSelected = + modifyalldimregs || + (modifybothchannels && + dimension == gig::dimension_samplechannel); + if (isMainSelection) + Gdk::Cairo::set_source_rgba(cr, blue); + else if (isSelectedZone) + cr->set_source(blueHatchedSurfacePattern2); + else if (isCheckBoxSelected) + cr->set_source(blueHatchedSurfacePattern); + else + Gdk::Cairo::set_source_rgba(cr, white); + cr->rectangle(prevX + 1, y + 1, wZone, h - 1); + cr->fill(); + + // draw icons + drawIconsFor(dimension, j - 1, cr, prevX, y, wZone, h); - // draw fill for currently selected zone - if (dimregno >= 0) { - Gdk::Cairo::set_source_rgba(cr, red); - int dr = (dimregno >> bitpos) & - ((1 << region->pDimensionDefinitions[i].bits) - 1); - if (customsplits) { - int x1 = label_width; - for (int j = 0 ; j < nbZones && x1 + 1 < clipx2 ; j++) { - gig::DimensionRegion* d = - region->pDimensionRegions[c + (j << bitpos)]; - int upperLimit = d->DimensionUpperLimits[i]; - if (!upperLimit) { - upperLimit = d->VelocityUpperLimit; + // 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 } - int v = upperLimit + 1; - int x2 = int((w - label_width - 1) * v / 128.0 + - 0.5) + label_width; - if (j == dr && x1 < x2) { - cr->rectangle(x1 + 1, y + 1, - (x2 - x1) - 1, h - 2); - cr->fill(); - break; + // 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 } - x1 = x2; - } - } 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); - cr->rectangle(label_width + x1 + 1, y + 1, - (x2 - x1) - 1, h - 2); - cr->fill(); } - } + prevX = x; + } } } - y += h; } bitpos += region->pDimensionDefinitions[i].bits; @@ -301,7 +769,7 @@ void DimRegionChooser::set_region(gig::Region* region) { this->region = region; - dimregno = 0; + maindimregno = 0; nbDimensions = 0; if (region) { int bitcount = 0; @@ -309,56 +777,75 @@ 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 * 20 : 0); + 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]); } void DimRegionChooser::update_after_resize() { - if (region->pDimensionDefinitions[resize.dimension].dimension == gig::dimension_velocity) { + const uint8_t upperLimit = resize.pos - 1; + gig::Instrument* instr = (gig::Instrument*)region->GetParent(); - 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 = dimregno & mask; // mask away this dimension + int c = maindimregno & mask; // mask away this dimension if (region->pDimensionRegions[c]->DimensionUpperLimits[resize.dimension] == 0) { // the velocity dimension didn't previously have @@ -381,22 +868,76 @@ } } - gig::DimensionRegion* d = region->pDimensionRegions[c + resize.offset]; + int index = c + (resize.zone << bitpos); + gig::DimensionRegion* d = region->pDimensionRegions[index]; // update both v2 and v3 values - d->DimensionUpperLimits[resize.dimension] = resize.pos - 1; - d->VelocityUpperLimit = resize.pos - 1; + 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; + } + } + } } 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++) { @@ -404,9 +945,15 @@ d->DimensionUpperLimits[resize.dimension] = int(128.0 * (j + 1) / nbZones - 1); } } - gig::DimensionRegion* d = region->pDimensionRegions[i + resize.offset]; - d->DimensionUpperLimits[resize.dimension] = resize.pos - 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++) { @@ -420,6 +967,37 @@ if (j == region->Dimensions) break; i = (i & ~((1 << bitpos) - 1)) + (1 << bitpos); } + + 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; + } + } + } } } @@ -429,7 +1007,11 @@ #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 get_window()->pointer_ungrab(event->time); #else +# if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20) Glib::wrap(event->device, true)->ungrab(event->time); +# else + gdk_device_ungrab(Glib::wrap(event->device, true)->gobj(), event->time); +# endif #endif resize.active = false; @@ -458,6 +1040,7 @@ Gdk::Cursor(Gdk::SB_H_DOUBLE_ARROW), event->time); #else +# if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20) Glib::wrap(event->device, true)->grab(get_window(), Gdk::OWNERSHIP_NONE, false, @@ -466,6 +1049,21 @@ Gdk::POINTER_MOTION_HINT_MASK, Gdk::Cursor::create(Gdk::SB_H_DOUBLE_ARROW), event->time); +# else + gdk_device_grab( + Glib::wrap(event->device, true)->gobj(), + get_window()->gobj(), + GDK_OWNERSHIP_NONE, + false, + GdkEventMask(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK), + Gdk::Cursor::create( + Glib::wrap(event->device, true)->get_seat()->get_display(), + Gdk::SB_H_DOUBLE_ARROW + )->gobj(), + event->time + ); +# endif #endif resize.active = true; } else { @@ -485,9 +1083,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 && @@ -517,15 +1115,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; @@ -535,8 +1156,14 @@ { Glib::RefPtr window = get_window(); int x, y; +#if HAS_GDKMM_SEAT + x = event->x; + y = event->y; + Gdk::ModifierType state = Gdk::ModifierType(event->state); +#else Gdk::ModifierType state = Gdk::ModifierType(0); window->get_pointer(x, y, state); +#endif if (resize.active) { int w = get_width(); @@ -563,7 +1190,8 @@ resize.pos = k; update_after_resize(); - get_window()->invalidate_rect(rect, false); + get_window()->invalidate_rect(rect, false); // not sufficient ... + queue_draw(); // ... so do a complete redraw instead. } } else { if (is_in_resize_zone(x, y)) { @@ -571,7 +1199,16 @@ #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)); + window->set_cursor( +# if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20) + Gdk::Cursor::create(Gdk::SB_H_DOUBLE_ARROW) +# else + Gdk::Cursor::create( + Glib::wrap(event->device, true)->get_seat()->get_display(), + Gdk::SB_H_DOUBLE_ARROW + ) +# endif + ); #endif cursor_is_resize = true; } @@ -599,9 +1236,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 && @@ -624,11 +1261,12 @@ 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; @@ -665,8 +1303,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()) { @@ -698,10 +1336,343 @@ } } } 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 + +#if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && (GTKMM_MINOR_VERSION > 91 || (GTKMM_MINOR_VERSION == 91 && GTKMM_MICRO_VERSION >= 2))) // GTKMM >= 3.91.2 +bool DimRegionChooser::onKeyPressed(Gdk::EventKey& _key) { + GdkEventKey* key = _key.gobj(); +#else +bool DimRegionChooser::onKeyPressed(GdkEventKey* key) { +#endif + //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; +} + +#if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && (GTKMM_MINOR_VERSION > 91 || (GTKMM_MINOR_VERSION == 91 && GTKMM_MICRO_VERSION >= 2))) // GTKMM >= 3.91.2 +bool DimRegionChooser::onKeyReleased(Gdk::EventKey& _key) { + GdkEventKey* key = _key.gobj(); +#else +bool DimRegionChooser::onKeyReleased(GdkEventKey* key) { +#endif + //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; + } + + // commented out: re-evaluate maindimcase, since it might not been reset from a previous instrument which causes errors if it got different dimension types + //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]; }