VocalEasel/Sources/VLSheetViewSelection.mm

549 lines
14 KiB
Plaintext
Raw Normal View History

//
2007-04-27 06:41:34 +00:00
// File: VLSheetViewSelection.mm - Measure selection functionality
//
2007-04-27 06:41:34 +00:00
// Author(s):
//
// (MN) Matthias Neeracher
//
2011-09-08 01:19:41 +00:00
// Copyright <20> 2006-2011 Matthias Neeracher
//
#import "VLSheetView.h"
2008-05-29 18:54:30 +00:00
#import "VLSheetViewNotes.h"
#import "VLSheetViewSelection.h"
2008-05-29 18:54:30 +00:00
#import "VLSheetViewNotes.h"
#import "VLSheetViewChords.h"
#import "VLSheetViewLyrics.h"
#import "VLSheetWindow.h"
#import "VLDocument.h"
#import "VLPitchGrid.h"
2008-05-29 18:54:30 +00:00
@interface VLPlaybackEditable : VLEditable {
VLSheetView * fView;
size_t fStanza;
size_t fNoteMeasure;
VLFract fNoteAt;
int fNoteVert;
uint16_t fNoteVisual;
2008-05-29 18:54:30 +00:00
size_t fChordMeasure;
VLFract fChordAt;
}
- (VLPlaybackEditable *)initWithView:(VLSheetView *)view;
2011-09-04 23:38:01 +00:00
- (void) userEvent:(const VLMIDIUserEvent *)event;
2008-05-29 18:54:30 +00:00
- (void) highlightCursor;
2011-09-10 23:54:34 +00:00
- (BOOL) hidden;
2008-05-29 18:54:30 +00:00
@end
@implementation VLPlaybackEditable
- (VLPlaybackEditable *)initWithView:(VLSheetView *)view
{
fView = view;
fStanza = 1;
fNoteMeasure = 0x80000000;
fChordMeasure = 0x80000000;
return self;
}
2011-09-04 23:38:01 +00:00
- (void) userEvent:(const VLMIDIUserEvent *) event
2008-05-29 18:54:30 +00:00
{
if (event->fPitch) {
fNoteMeasure = event->fMeasure;
fNoteAt = event->fAt;
fNoteVisual = event->fVisual & VLNote::kAccidentalsMask;
if (event->fPitch == VLNote::kNoPitch)
fNoteVert = kCursorNoPitch;
else
fNoteVert = VLPitchToGrid(event->fPitch, fNoteVisual, [fView song]->Properties(fNoteMeasure).fKey);
2008-07-05 13:55:41 +00:00
fStanza = event->fStanza;
2011-09-10 21:33:40 +00:00
VLFraction endAt= fNoteAt+VLFraction(1,128);
[fView highlightTextInStanza:fStanza startMeasure:fNoteMeasure at:fNoteAt
endMeasure:fNoteMeasure at:endAt];
2008-05-29 18:54:30 +00:00
} else {
fChordMeasure = event->fMeasure;
fChordAt = event->fAt;
}
2008-07-05 13:56:00 +00:00
[fView scrollMeasureToVisible:event->fMeasure+1];
2008-05-29 18:54:30 +00:00
[fView setNeedsDisplay:YES];
}
- (void) highlightCursor
{
if (fNoteMeasure != 0x80000000 && fNoteVert != kCursorNoPitch)
[fView drawNoteCursor:fNoteVert inMeasure:fNoteMeasure at:fNoteAt visual:fNoteVisual];
2008-05-29 18:54:30 +00:00
if (fChordMeasure != 0x80000000)
[fView highlightChordInMeasure:fChordMeasure at:fChordAt];
}
2011-09-10 23:54:34 +00:00
- (BOOL) hidden
{
return YES;
}
2008-05-29 18:54:30 +00:00
@end
2008-01-16 13:04:01 +00:00
@interface NSMenuItem (VLSetStateToOff)
- (void) VLSetStateToOff;
@end
@implementation NSMenuItem (VLSetStateToOff)
- (void) VLSetStateToOff
{
[self setState:NSOffState];
}
@end
2007-01-02 07:09:06 +00:00
//
// We're too lazy to properly serialize our private pasteboard format.
//
static VLSong sPasteboard;
2008-05-29 18:54:30 +00:00
extern "C" void
VLSequenceCallback(
void * inClientData,
MusicSequence inSequence, MusicTrack inTrack,
MusicTimeStamp inEventTime, const MusicEventUserData *inEventData,
MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{
2011-09-04 23:38:01 +00:00
dispatch_async(dispatch_get_main_queue(), ^{
[(id)inClientData userEvent:(const VLMIDIUserEvent *)inEventData];
});
2008-05-29 18:54:30 +00:00
}
@implementation VLSheetView (Selection)
2011-09-08 01:19:41 +00:00
- (void)editSelection:(BOOL)extend
{
2011-09-08 02:09:24 +00:00
if (extend && fSelEnd > -1) {
2011-09-08 01:19:41 +00:00
if (fCursorMeasure > fSelEnd)
fSelEnd = fCursorMeasure;
else if (fCursorMeasure < fSelStart)
fSelStart = fCursorMeasure;
} else {
fSelStart = fSelEnd = fSelAnchor = fCursorMeasure;
}
2008-01-16 13:04:01 +00:00
[self updateMenus];
[self setNeedsDisplay:YES];
}
- (void)adjustSelection:(NSEvent *)event
{
switch ([self findRegionForEvent:event]) {
case kRegionNote:
case kRegionChord:
case kRegionLyrics:
2011-09-08 01:11:33 +00:00
if (fCursorAt.fNum > 0 && fCursorMeasure >= fSelAnchor)
++fCursorMeasure;
//
// Fall through
//
case kRegionMeasure:
2007-01-12 09:04:24 +00:00
fCursorMeasure =
std::max(0, std::min<int>(fCursorMeasure, [self song]->CountMeasures()));
2011-09-08 01:11:33 +00:00
if (fCursorMeasure >= fSelAnchor) {
fSelStart = fSelAnchor;
fSelEnd = fCursorMeasure;
2008-01-16 13:04:01 +00:00
[self updateMenus];
[self setNeedsDisplay:YES];
2011-09-08 01:11:33 +00:00
} else {
fSelStart = fCursorMeasure;
2011-09-08 01:11:33 +00:00
fSelEnd = fSelAnchor;
2008-01-16 13:04:01 +00:00
[self updateMenus];
[self setNeedsDisplay:YES];
}
break;
2011-09-08 01:11:33 +00:00
default:
break;
}
fCursorRegion = kRegionMeasure;
}
- (NSRange)sectionsInSelection
{
NSRange sections;
int firstSection;
int lastSection;
VLSong * song = [self song];
if (fSelEnd > -1) {
firstSection = song->fMeasures[fSelStart].fPropIdx;
2008-03-24 22:57:06 +00:00
lastSection = fSelEnd==fSelStart ? firstSection : song->fMeasures[fSelEnd-1].fPropIdx;
} else {
firstSection = 0;
lastSection = song->fMeasures.back().fPropIdx;
}
sections.location = firstSection;
sections.length = lastSection-firstSection+1;
return sections;
}
2007-01-21 11:34:56 +00:00
- (BOOL)validateMenuItem:(id) item
{
SEL action = [item action];
if (action == @selector(insertJumpToCoda:))
if (fSelStart == fSelEnd) {
[item setState:[self song]->fGoToCoda==fSelStart];
return YES;
} else
return NO;
else if (action == @selector(insertStartCoda:))
if (fSelStart == fSelEnd) {
[item setState:[self song]->fCoda==fSelStart];
2007-12-24 00:10:23 +00:00
return YES;
} else
return NO;
else if (action == @selector(insertBreak:))
if (fSelStart == fSelEnd && fSelStart > 0) {
2007-12-25 13:12:07 +00:00
VLSong * song = [self song];
bool checked = fSelStart < song->fMeasures.size();
if ([item tag] == 256)
checked = checked && song->DoesBeginSection(fSelStart);
else
checked = checked && song->fMeasures[fSelStart].fBreak == [item tag];
[item setState:checked];
2007-12-24 00:10:23 +00:00
2007-01-21 11:34:56 +00:00
return YES;
} else
return NO;
else
return [self validateUserInterfaceItem:item];
}
- (BOOL)validateUserInterfaceItem:(id) item
{
SEL action = [item action];
if (action == @selector(cut:)
|| action == @selector(copy:)
|| action == @selector(delete:)
)
return fSelStart < fSelEnd;
else if (action == @selector(editRepeat:))
return fSelEnd > fSelStart
&& [self song]->CanBeRepeat(fSelStart, fSelEnd);
else if (action == @selector(editRepeatEnding:))
return fSelEnd > fSelStart
&& [self song]->CanBeEnding(fSelStart, fSelEnd);
else if (action == @selector(paste:))
return fSelStart <= fSelEnd;
2011-09-08 02:10:49 +00:00
else if (action == @selector(insertMeasure:))
return fSelStart == fSelEnd;
else
return YES;
}
- (IBAction)cut:(id)sender
{
2007-01-02 07:09:06 +00:00
[self copy:sender];
[self delete:sender];
}
- (IBAction)copy:(id)sender
{
2007-01-02 07:09:06 +00:00
NSPasteboard * pb = [NSPasteboard generalPasteboard];
NSString * pbType = [[NSBundle mainBundle] bundleIdentifier];
[pb declareTypes:[NSArray arrayWithObject:pbType] owner:nil];
if ([pb setString:@"whatever" forType:pbType])
sPasteboard = [self song]->CopyMeasures(fSelStart, fSelEnd);
}
- (IBAction)paste:(id)sender
{
2007-01-02 07:09:06 +00:00
NSPasteboard * pb = [NSPasteboard generalPasteboard];
NSString * pbType = [[NSBundle mainBundle] bundleIdentifier];
if ([pb availableTypeFromArray:[NSArray arrayWithObject:pbType]]) {
[[self document] willChangeSong];
if (![sender tag]) // Delete on paste, but not on overwrite
[self song]->DeleteMeasures(fSelStart, fSelEnd);
[self song]->PasteMeasures(fSelStart, sPasteboard, [sender tag]);
2011-09-08 00:18:07 +00:00
[self setNumStanzas: std::max<int>([self song]->CountStanzas(), fNumStanzas)];
2007-01-02 07:09:06 +00:00
[[self document] didChangeSong];
[self setNeedsDisplay:YES];
}
}
- (IBAction)delete:(id)sender
{
2007-01-02 07:09:06 +00:00
[[self document] willChangeSong];
2007-05-07 04:01:29 +00:00
[self song]->DeleteMeasures(fSelStart, fSelEnd, [sender tag]);
2007-01-02 07:09:06 +00:00
[[self document] didChangeSong];
[self setNeedsDisplay:YES];
}
2011-09-08 02:10:49 +00:00
- (IBAction)insertMeasure:(id)sender
{
[[self document] willChangeSong];
[self song]->InsertMeasure(fSelStart);
[[self document] didChangeSong];
[self setNeedsDisplay:YES];
}
- (IBAction)editRepeat:(id)sender
{
int volta;
[self song]->CanBeRepeat(fSelStart, fSelEnd, &volta);
[fRepeatMsg setStringValue:
[NSString stringWithFormat:@"Repeat measures %d through %d",
fSelStart+1, fSelEnd]];
[NSApp beginSheet:fRepeatSheet modalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(didEndRepeatSheet:returnCode:contextInfo:)
contextInfo:nil];
}
- (void)didEndRepeatSheet:(NSWindow *)sheet returnCode:(int)returnCode
contextInfo:(void *)ctx
{
switch (returnCode) {
case NSAlertFirstButtonReturn:
[[self document] willChangeSong];
[self song]->AddRepeat(fSelStart, fSelEnd, [[self document] repeatVolta]);
[self setNeedsDisplay:YES];
[[self document] didChangeSong];
break;
case NSAlertThirdButtonReturn:
[[self document] willChangeSong];
[self song]->DelRepeat(fSelStart, fSelEnd);
[[self document] didChangeSong];
[self setNeedsDisplay:YES];
break;
default:
break;
}
[sheet orderOut:self];
}
- (IBAction)editRepeatEnding:(id)sender
{
[self song]->CanBeEnding(fSelStart, fSelEnd, &fVolta, &fVoltaOK);
[fEndingMsg setStringValue:
[NSString stringWithFormat:@"Ending in measures %d through %d applies to repeats:",
fSelStart+1, fSelEnd]];
[NSApp beginSheet:fEndingSheet modalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(didEndEndingSheet:returnCode:contextInfo:)
contextInfo:nil];
}
- (void)didEndEndingSheet:(NSWindow *)sheet returnCode:(int)returnCode
contextInfo:(void *)ctx
{
switch (returnCode) {
case NSAlertFirstButtonReturn:
[[self document] willChangeSong];
[self song]->AddEnding(fSelStart, fSelEnd, fVolta);
[self setNeedsDisplay:YES];
[[self document] didChangeSong];
break;
case NSAlertThirdButtonReturn:
[[self document] willChangeSong];
[self song]->DelEnding(fSelStart, fSelEnd);
[[self document] didChangeSong];
[self setNeedsDisplay:YES];
break;
default:
break;
}
[sheet orderOut:self];
}
//
// Data source for endings
//
2007-04-24 18:32:32 +00:00
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return 1;
}
- (id)tableView:(NSTableView*)tv objectValueForTableColumn:(NSTableColumn *)col
2007-04-24 18:32:32 +00:00
row:(int)rowIndex
{
int mask = [[col identifier] intValue];
return (fVoltaOK & mask) ? [NSNumber numberWithBool:(fVolta & mask)] : nil;
}
2007-04-24 18:32:32 +00:00
- (void)tableView:(NSTableView *)tv setObjectValue:(id)val forTableColumn:(NSTableColumn *)col row:(int)rowIndex
{
int mask = [[col identifier] intValue];
if ([val boolValue])
fVolta |= mask;
else
fVolta &= ~mask;
}
2007-01-21 11:34:56 +00:00
- (IBAction)insertJumpToCoda:(id)sender
{
[[self document] willChangeSong];
VLSong * song = [self song];
if (song->fGoToCoda == fSelStart)
song->fGoToCoda = -1;
else
song->fGoToCoda = fSelStart;
[self setNeedsDisplay:YES];
[[self document] didChangeSong];
}
- (IBAction)insertStartCoda:(id)sender
{
[[self document] willChangeSong];
VLSong * song = [self song];
if (song->fCoda == fSelStart)
song->fCoda = -1;
else
song->fCoda = fSelStart;
[self setNeedsDisplay:YES];
[[self document] didChangeSong];
}
2007-12-24 00:10:23 +00:00
- (IBAction)insertBreak:(id)sender
{
[[self document] willChangeSong];
VLSong * song = [self song];
2007-12-25 13:12:07 +00:00
if ([sender tag] == 256) {
if (song->DoesBeginSection(fSelStart))
song->DelSection(fSelStart);
else
song->AddSection(fSelStart);
} else {
VLMeasure & meas = song->fMeasures[fSelStart];
if (meas.fBreak == [sender tag])
meas.fBreak = 0;
else
meas.fBreak = [sender tag];
}
2007-12-24 00:10:23 +00:00
fNeedsRecalc = kRecalc;
[self setNeedsDisplay:YES];
[[self document] didChangeSong];
}
2008-01-16 13:04:01 +00:00
inline int KeyModeTag(const VLProperties & prop)
{
return (prop.fKey << 8) | (prop.fMode & 0xFF);
}
- (void)updateKeyMenu
{
NSMenu *menu = [fKeyMenu menu];
NSRange sections = [self sectionsInSelection];
VLSong *song = [self song];
[[menu itemArray] makeObjectsPerformSelector:@selector(VLSetStateToOff)];
int firstTag = KeyModeTag(song->fProperties[sections.location]);
[fKeyMenu selectItemWithTag:firstTag];
int firstState = NSOnState;
while (--sections.length > 0) {
int thisTag = KeyModeTag(song->fProperties[++sections.location]);
if (thisTag != firstTag) {
firstState = NSMixedState;
[[menu itemWithTag:thisTag] setState:NSMixedState];
}
}
[[menu itemWithTag:firstTag] setState:firstState];
}
inline int TimeTag(const VLProperties & prop)
{
return (prop.fTime.fNum << 8) | prop.fTime.fDenom;
}
- (void)updateTimeMenu
{
NSMenu *menu = [fTimeMenu menu];
NSRange sections = [self sectionsInSelection];
VLSong *song = [self song];
[[menu itemArray] makeObjectsPerformSelector:@selector(VLSetStateToOff)];
int firstTag = TimeTag(song->fProperties[sections.location]);
[fTimeMenu selectItemWithTag:firstTag];
int firstState = NSOnState;
while (--sections.length > 0) {
int thisTag = TimeTag(song->fProperties[++sections.location]);
if (thisTag != firstTag) {
firstState = NSMixedState;
[[menu itemWithTag:thisTag] setState:NSMixedState];
}
}
[[menu itemWithTag:firstTag] setState:firstState];
}
- (void)updateDivisionMenu
{
NSMenu *menu = [fDivisionMenu menu];
NSRange sections = [self sectionsInSelection];
VLSong *song = [self song];
[[menu itemArray] makeObjectsPerformSelector:@selector(VLSetStateToOff)];
int firstTag = song->fProperties[sections.location].fDivisions;
[fDivisionMenu selectItemWithTag:firstTag];
int firstState = NSOnState;
while (--sections.length > 0) {
int thisTag = song->fProperties[++sections.location].fDivisions;
if (thisTag != firstTag) {
firstState = NSMixedState;
[[menu itemWithTag:thisTag] setState:NSMixedState];
}
}
[[menu itemWithTag:firstTag] setState:firstState];
}
2008-01-23 01:20:09 +00:00
- (void)updateGrooveMenu
{
NSRange sections = [self sectionsInSelection];
VLSong * song = [self song];
NSMutableArray* grooves = [NSMutableArray array];
while (sections.length-- > 0) {
NSString * groove =
[NSString stringWithUTF8String:
song->fProperties[sections.location++].fGroove.c_str()];
if (![grooves containsObject:groove])
[grooves addObject:groove];
}
int selected = [grooves count];
int history = [fGrooveMenu numberOfItems]-2;
while (history-- > 0) {
NSString * groove = [fGrooveMenu itemTitleAtIndex:2];
[fGrooveMenu removeItemAtIndex:2];
if (![grooves containsObject:groove])
[grooves addObject:groove];
}
[fGrooveMenu addItemsWithTitles:grooves];
[fGrooveMenu selectItemAtIndex:2];
if (selected > 1)
while (selected-- > 0)
[[fGrooveMenu itemAtIndex:selected+2] setState:NSMixedState];
[[NSUserDefaults standardUserDefaults] setObject:grooves forKey:@"VLGrooves"];
}
2008-01-16 13:04:01 +00:00
- (void)updateMenus
{
[self updateKeyMenu];
[self updateTimeMenu];
[self updateDivisionMenu];
2008-01-23 01:20:09 +00:00
[self updateGrooveMenu];
2008-01-16 13:04:01 +00:00
}
2008-05-29 18:54:30 +00:00
- (void) willPlaySequence:(MusicSequence)music
{
VLEditable * e =
[[VLPlaybackEditable alloc] initWithView:self];
[self setEditTarget:e];
MusicSequenceSetUserCallback(music, VLSequenceCallback, e);
}
@end