2006-09-11 02:49:56 +00:00
|
|
|
//
|
|
|
|
// VLSheetViewChords.mm
|
|
|
|
// Vocalese
|
|
|
|
//
|
|
|
|
// Created by Matthias Neeracher on 1/4/06.
|
|
|
|
// Copyright 2006 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "VLSheetView.h"
|
|
|
|
#import "VLSheetViewChords.h"
|
|
|
|
#import "VLSheetViewInternal.h"
|
|
|
|
|
|
|
|
#import "VLModel.h"
|
|
|
|
#import "VLSoundOut.h"
|
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
std::string NormalizeName(NSString* rawName)
|
|
|
|
{
|
|
|
|
std::string chordName =
|
2006-10-08 05:56:25 +00:00
|
|
|
rawName ? (const char *)[[rawName lowercaseString] UTF8String] : "";
|
2006-10-03 17:52:54 +00:00
|
|
|
//
|
|
|
|
// Normalize # and b
|
|
|
|
//
|
|
|
|
for (;;) {
|
|
|
|
size_t found;
|
2006-09-11 02:49:56 +00:00
|
|
|
|
2006-10-29 07:48:39 +00:00
|
|
|
found = chordName.find(kVLSharpStr, 0, 3);
|
2006-10-03 17:52:54 +00:00
|
|
|
if (found != std::string::npos) {
|
|
|
|
chordName.replace(found, 3, 1, '#');
|
|
|
|
continue;
|
|
|
|
}
|
2006-10-29 07:48:39 +00:00
|
|
|
found = chordName.find(kVLFlatStr, 0, 3);
|
2006-10-03 17:52:54 +00:00
|
|
|
if (found != std::string::npos) {
|
|
|
|
chordName.replace(found, 3, 1, 'b');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
found = chordName.find(" ", 0, 1);
|
|
|
|
if (found != std::string::npos) {
|
|
|
|
chordName.erase(found);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return chordName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@interface NSAttributedString (Chords)
|
|
|
|
|
|
|
|
+ (NSAttributedString *) attributedStringWithCppString:(const std::string &)s
|
|
|
|
attributes:(NSDictionary *)a;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSAttributedString (Chords)
|
|
|
|
|
|
|
|
+ (NSAttributedString *) attributedStringWithCppString:(const std::string &)s
|
2006-09-11 02:49:56 +00:00
|
|
|
attributes:(NSDictionary *)a
|
|
|
|
{
|
|
|
|
return [[[NSAttributedString alloc]
|
|
|
|
initWithString:[NSString stringWithUTF8String:s.c_str()]
|
|
|
|
attributes: a]
|
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation VLChordEditable
|
|
|
|
|
2006-10-08 05:56:25 +00:00
|
|
|
- (VLChordEditable *)initWithView:(VLSheetView *)view
|
2006-10-03 17:52:54 +00:00
|
|
|
song:(VLSong *)song
|
|
|
|
measure:(int)measure
|
|
|
|
at:(VLFract)at;
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
fView = view;
|
|
|
|
fSong = song;
|
|
|
|
fMeasure= measure;
|
|
|
|
fAt = at;
|
|
|
|
|
2006-10-08 05:56:25 +00:00
|
|
|
[fView setNeedsDisplay: YES];
|
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) stringValue
|
|
|
|
{
|
|
|
|
const VLMeasure measure = fSong->fMeasures[fMeasure];
|
|
|
|
const VLChordList & chords = measure.fChords;
|
|
|
|
VLFraction at(0);
|
|
|
|
for (VLChordList::const_iterator chord = chords.begin();
|
|
|
|
chord != chords.end() && at <= fAt;
|
|
|
|
++chord
|
|
|
|
) {
|
|
|
|
if (at == fAt && chord->fPitch != VLNote::kNoPitch) {
|
|
|
|
//
|
|
|
|
// Found it!
|
|
|
|
//
|
2006-10-29 07:48:39 +00:00
|
|
|
VLSoundOut::Instance()->PlayChord(*chord);
|
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
const VLProperties & prop = fSong->fProperties.front();
|
|
|
|
std::string name, ext, root;
|
|
|
|
chord->Name(name, ext, root, prop.fKey > 0);
|
|
|
|
|
2006-10-29 07:48:39 +00:00
|
|
|
NSString * ns = [NSString stringWithUTF8String:name.c_str()];
|
|
|
|
NSString * es = [NSString stringWithUTF8String:ext.c_str()];
|
|
|
|
NSString * rs = [NSString stringWithUTF8String:root.c_str()];
|
|
|
|
|
|
|
|
return [NSString stringWithFormat:@"%@%@%s%@", ns, es,
|
|
|
|
[rs length] ? "/" : "", rs];
|
2006-10-03 17:52:54 +00:00
|
|
|
}
|
|
|
|
at += chord->fDuration;
|
|
|
|
}
|
|
|
|
return @"";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStringValue:(NSString *)val
|
|
|
|
{
|
|
|
|
std::string chordName = NormalizeName(val);
|
|
|
|
if (!chordName.size()) {
|
|
|
|
fSong->DelChord(fMeasure, fAt);
|
|
|
|
} else {
|
|
|
|
VLChord chord(chordName);
|
|
|
|
VLSoundOut::Instance()->PlayChord(chord);
|
|
|
|
fSong->AddChord(chord, fMeasure, fAt);
|
|
|
|
[fView setNeedsDisplay:YES];
|
|
|
|
}
|
2006-10-14 10:10:19 +00:00
|
|
|
[[fView document] updateChangeCount:NSChangeDone];
|
2006-10-03 17:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) validValue:(NSString *)val
|
|
|
|
{
|
|
|
|
std::string chordName = NormalizeName(val);
|
|
|
|
if (!chordName.size())
|
|
|
|
return YES;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check for valid chord
|
|
|
|
//
|
|
|
|
VLChord chord(chordName);
|
|
|
|
|
|
|
|
return chord.fPitch != VLNote::kNoPitch;
|
|
|
|
}
|
|
|
|
|
2006-10-08 05:56:25 +00:00
|
|
|
- (void) moveToNext
|
|
|
|
{
|
|
|
|
const VLProperties & prop = fSong->fProperties.front();
|
|
|
|
|
|
|
|
fAt = fAt+VLFraction(1,4);
|
|
|
|
if (fAt >= prop.fTime) {
|
|
|
|
fAt = VLFraction(0,4);
|
|
|
|
fMeasure = (fMeasure+1) % fSong->CountMeasures();
|
2006-10-09 07:28:49 +00:00
|
|
|
[fView scrollMeasureToVisible:fMeasure];
|
2006-10-08 05:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveToPrev
|
|
|
|
{
|
|
|
|
if (fAt < VLFraction(1,4)) {
|
|
|
|
const VLProperties & prop = fSong->fProperties.front();
|
|
|
|
fAt = prop.fTime - VLFraction(1,4);
|
|
|
|
fMeasure =
|
|
|
|
(fMeasure+fSong->CountMeasures()-1) % fSong->CountMeasures();
|
2006-10-09 07:28:49 +00:00
|
|
|
[fView scrollMeasureToVisible:fMeasure];
|
2006-10-08 05:56:25 +00:00
|
|
|
} else
|
|
|
|
fAt = fAt-VLFraction(1,4);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) highlightCursor
|
|
|
|
{
|
|
|
|
[fView highlightChordInMeasure:fMeasure at:fAt];
|
|
|
|
}
|
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation VLSheetView (Chords)
|
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
- (NSAttributedString *) stringWithChord:(const VLChord &)chord
|
|
|
|
{
|
|
|
|
const VLSong * song = [self song];
|
|
|
|
const VLProperties & prop = song->fProperties.front();
|
|
|
|
|
|
|
|
static NSDictionary * sBigFont = nil;
|
|
|
|
static NSDictionary * sSuperFont = nil;
|
|
|
|
if (!sBigFont)
|
|
|
|
sBigFont =
|
|
|
|
[[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSFont fontWithName: @"Helvetica" size: 14],
|
|
|
|
NSFontAttributeName,
|
|
|
|
nil];
|
|
|
|
if (!sSuperFont)
|
|
|
|
sSuperFont =
|
|
|
|
[[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSFont fontWithName: @"Helvetica" size: 12],
|
|
|
|
NSFontAttributeName,
|
|
|
|
[NSNumber numberWithInt: 1],
|
|
|
|
NSSuperscriptAttributeName,
|
|
|
|
nil];
|
|
|
|
|
|
|
|
std::string name, ext, root;
|
|
|
|
if (chord.fPitch != VLNote::kNoPitch)
|
|
|
|
chord.Name(name, ext, root, prop.fKey > 0);
|
|
|
|
|
|
|
|
NSMutableAttributedString * s =
|
|
|
|
[[[NSMutableAttributedString alloc] init] autorelease];
|
|
|
|
[s appendAttributedString:
|
2006-10-03 17:52:54 +00:00
|
|
|
[NSAttributedString attributedStringWithCppString: name
|
2006-09-11 02:49:56 +00:00
|
|
|
attributes: sBigFont]];
|
|
|
|
[s appendAttributedString:
|
2006-10-03 17:52:54 +00:00
|
|
|
[NSAttributedString attributedStringWithCppString: ext.size() ? ext : " "
|
2006-09-11 02:49:56 +00:00
|
|
|
attributes: sSuperFont]];
|
|
|
|
if (root.size())
|
|
|
|
[s appendAttributedString:
|
2006-10-03 17:52:54 +00:00
|
|
|
[NSAttributedString attributedStringWithCppString: "/" + root
|
2006-09-11 02:49:56 +00:00
|
|
|
attributes: sBigFont]];
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
- (void) drawChordsForSystem:(int)system
|
2006-09-11 02:49:56 +00:00
|
|
|
{
|
2006-10-03 17:52:54 +00:00
|
|
|
const VLSong * song = [self song];
|
|
|
|
const float kSystemY = [self systemY:system];
|
2006-09-11 02:49:56 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Build new list
|
|
|
|
//
|
2006-10-02 08:32:25 +00:00
|
|
|
for (int m = 0; m<fMeasPerSystem; ++m) {
|
|
|
|
int measIdx = m+system*fMeasPerSystem;
|
|
|
|
if (measIdx >= song->CountMeasures())
|
|
|
|
break;
|
|
|
|
const VLMeasure measure = song->fMeasures[measIdx];
|
|
|
|
const VLChordList & chords = measure.fChords;
|
|
|
|
VLFraction at(0);
|
|
|
|
for (VLChordList::const_iterator chord = chords.begin();
|
|
|
|
chord != chords.end();
|
|
|
|
++chord
|
|
|
|
) {
|
|
|
|
NSAttributedString * chordName = [self stringWithChord:*chord];
|
|
|
|
NSPoint chordLoc =
|
2006-10-03 17:52:54 +00:00
|
|
|
NSMakePoint(fClefKeyW+(m+at)*fMeasureW+0.5f*kNoteW,
|
2006-10-02 08:32:25 +00:00
|
|
|
kSystemY+kChordY);
|
|
|
|
[chordName drawAtPoint:chordLoc];
|
2006-10-03 17:52:54 +00:00
|
|
|
at += chord->fDuration;
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
- (void) editChord
|
2006-09-12 07:55:28 +00:00
|
|
|
{
|
2006-10-03 17:52:54 +00:00
|
|
|
VLEditable * e =
|
|
|
|
[[VLChordEditable alloc]
|
|
|
|
initWithView:self
|
|
|
|
song:[self song]
|
|
|
|
measure:fCursorMeasure
|
|
|
|
at:fCursorAt];
|
2006-10-28 09:18:55 +00:00
|
|
|
[self setEditTarget:e];
|
2006-10-08 05:56:25 +00:00
|
|
|
[fFieldEditor selectText:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) highlightChordInMeasure:(int)measure at:(VLFraction)at
|
|
|
|
{
|
2006-10-09 07:28:49 +00:00
|
|
|
const VLProperties & prop = [self song]->fProperties.front();
|
|
|
|
const float kSystemY = [self systemY:measure / fMeasPerSystem];
|
|
|
|
NSRect r =
|
|
|
|
NSMakeRect([self noteXInMeasure:measure at:at]-kNoteW*0.5f,
|
|
|
|
kSystemY+kChordY, prop.fDivisions*kNoteW, kChordH);
|
2006-10-08 05:56:25 +00:00
|
|
|
[[NSColor colorWithCalibratedWhite:0.8f alpha:1.0f] setFill];
|
|
|
|
NSRectFillUsingOperation(r, NSCompositePlusDarker);
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|