2006-09-11 02:49:56 +00:00
|
|
|
//
|
|
|
|
// VLSheetView.mm
|
|
|
|
// Vocalese
|
|
|
|
//
|
|
|
|
// Created by Matthias Neeracher on 12/17/05.
|
|
|
|
// Copyright 2005 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "VLSheetView.h"
|
|
|
|
#import "VLSheetViewInternal.h"
|
|
|
|
#import "VLSheetViewChords.h"
|
|
|
|
#import "VLSheetViewNotes.h"
|
2006-10-02 08:32:25 +00:00
|
|
|
#import "VLSoundOut.h"
|
2006-09-11 02:49:56 +00:00
|
|
|
|
|
|
|
#import "VLDocument.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
@implementation VLSheetView
|
|
|
|
|
|
|
|
static NSImage ** sMusic;
|
|
|
|
|
|
|
|
static NSString * sElementNames[kMusicElements] = {
|
|
|
|
@"g-clef",
|
|
|
|
@"flat",
|
|
|
|
@"sharp",
|
|
|
|
@"natural",
|
|
|
|
@"whole-notehead",
|
|
|
|
@"half-notehead",
|
|
|
|
@"notehead",
|
|
|
|
@"whole-rest",
|
|
|
|
@"half-rest",
|
|
|
|
@"quarter-rest",
|
|
|
|
@"eighth-rest",
|
|
|
|
@"sixteenth-rest",
|
|
|
|
@"thirtysecondth-rest",
|
|
|
|
@"eighth-flag",
|
|
|
|
@"sixteenth-flag",
|
|
|
|
@"thirtysecondth-flag",
|
2006-10-16 09:01:25 +00:00
|
|
|
@"notecursor",
|
2006-10-21 09:23:37 +00:00
|
|
|
@"flatcursor",
|
|
|
|
@"sharpcursor",
|
|
|
|
@"naturalcursor",
|
|
|
|
@"restcursor",
|
|
|
|
@"killcursor"
|
2006-09-11 02:49:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static float sSharpPos[] = {
|
2006-10-02 05:29:37 +00:00
|
|
|
4.0f*kLineH, // F#
|
|
|
|
2.5f*kLineH, // C#
|
|
|
|
4.5f*kLineH, // G#
|
|
|
|
3.0f*kLineH, // D#
|
|
|
|
1.5f*kLineH, // A#
|
|
|
|
3.5f*kLineH, // E#
|
|
|
|
2.0f*kLineH, // B#
|
2006-09-11 02:49:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static float sFlatPos[] = {
|
2006-10-02 05:29:37 +00:00
|
|
|
2.0f*kLineH, // Bb
|
|
|
|
3.5f*kLineH, // Eb
|
|
|
|
1.5f*kLineH, // Ab
|
|
|
|
3.0f*kLineH, // Db
|
|
|
|
1.0f*kLineH, // Gb
|
|
|
|
2.5f*kLineH, // Cb
|
|
|
|
0.5f*kLineH, // Fb
|
2006-09-11 02:49:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
- (id)initWithFrame:(NSRect)frame
|
|
|
|
{
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
if (self) {
|
|
|
|
if (!sMusic) {
|
|
|
|
NSBundle * b = [NSBundle mainBundle];
|
|
|
|
sMusic = new NSImage * [kMusicElements];
|
|
|
|
for (int i=0; i<kMusicElements; ++i) {
|
|
|
|
NSString * name =
|
|
|
|
[b pathForResource:sElementNames[i] ofType:@"eps"
|
|
|
|
inDirectory:@"Music"];
|
|
|
|
sMusic[i] = [[NSImage alloc] initWithContentsOfFile: name];
|
|
|
|
NSSize sz = [sMusic[i] size];
|
|
|
|
sz.width *= kImgScale;
|
|
|
|
sz.height*= kImgScale;
|
|
|
|
[sMusic[i] setScalesWhenResized:YES];
|
|
|
|
[sMusic[i] setSize:sz];
|
|
|
|
}
|
|
|
|
}
|
2006-10-08 05:56:25 +00:00
|
|
|
fNeedsRecalc = kFirstRecalc;
|
2006-10-16 09:01:25 +00:00
|
|
|
fClickMode = ' ';
|
2006-10-02 06:07:44 +00:00
|
|
|
fDisplayScale = 1.0f;
|
2006-10-02 08:32:25 +00:00
|
|
|
fCursorPitch = VLNote::kNoPitch;
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2006-10-08 05:56:25 +00:00
|
|
|
- (BOOL)acceptsFirstResponder
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
- (VLDocument *) document
|
|
|
|
{
|
|
|
|
return [[[self window] windowController] document];
|
|
|
|
}
|
|
|
|
|
2006-10-28 09:18:55 +00:00
|
|
|
- (VLEditable *) editTarget
|
|
|
|
{
|
|
|
|
return [[[self window] windowController] editTarget];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setEditTarget:(VLEditable *)editable
|
|
|
|
{
|
|
|
|
[[[self window] windowController] setEditTarget:editable];
|
|
|
|
}
|
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
- (VLSong *) song
|
|
|
|
{
|
|
|
|
return [[self document] song];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSImage *) musicElement:(VLMusicElement)elt
|
|
|
|
{
|
|
|
|
return sMusic[elt];
|
|
|
|
}
|
|
|
|
|
2006-10-02 05:29:37 +00:00
|
|
|
- (float) systemY:(int)system
|
|
|
|
{
|
|
|
|
NSRect b = [self bounds];
|
|
|
|
|
|
|
|
return kSystemY+b.origin.y+b.size.height-(system+1)*kSystemH;
|
|
|
|
}
|
|
|
|
|
2006-10-21 09:23:37 +00:00
|
|
|
int8_t sSemi2Pitch[2][12] = {{
|
|
|
|
// C Db D Eb E F Gb G Ab A Bb B
|
|
|
|
0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6,
|
|
|
|
},{
|
|
|
|
// C C# D D# E F F# G G# A A# B
|
|
|
|
0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6,
|
|
|
|
}};
|
|
|
|
|
|
|
|
#define S kMusicSharp,
|
|
|
|
#define F kMusicFlat,
|
|
|
|
#define N kMusicNatural,
|
|
|
|
#define _ kMusicNothing,
|
|
|
|
|
|
|
|
VLMusicElement sSemi2Accidental[12][12] = {
|
|
|
|
// C DbD EbE F GbG AbA BbB
|
|
|
|
{N _ N _ N _ _ N _ N _ N}, // Gb major - 6 flats
|
|
|
|
{_ _ N _ N _ _ N _ N _ N}, // Db major - 5 flats
|
|
|
|
{_ _ N _ N _ F _ _ N _ N}, // Ab major - 4 flats
|
|
|
|
{_ F _ _ N _ F _ _ N _ N}, // Eb major - 3 flats
|
|
|
|
{_ F _ _ N _ F _ F _ _ N}, // Bb major - 2 flats
|
|
|
|
{_ F _ F _ _ F _ F _ _ N}, // F major - 1 flat
|
|
|
|
// C C#D D#E F F#G G#A A#B
|
|
|
|
{_ S _ S _ _ S _ S _ S _}, // C major
|
|
|
|
{_ S _ S _ N _ _ S _ S _}, // G major - 1 sharp
|
|
|
|
{N _ _ S _ N _ _ S _ S _}, // D major - 2 sharps
|
|
|
|
{N _ _ S _ N _ N _ _ S _}, // A major - 3 sharps
|
|
|
|
{N _ N _ _ N _ N _ _ S _}, // E major - 4 sharps
|
|
|
|
{N _ N _ _ N _ N _ N _ _}, // B major - 5 sharps
|
|
|
|
};
|
|
|
|
|
|
|
|
#undef S
|
|
|
|
#undef F
|
|
|
|
#undef N
|
|
|
|
#undef _
|
|
|
|
|
|
|
|
- (int) stepWithPitch:(int)pitch
|
|
|
|
{
|
|
|
|
int semi = pitch % 12;
|
|
|
|
int key = [self song]->fProperties.front().fKey;
|
|
|
|
bool useSharps = key >= 0;
|
|
|
|
|
|
|
|
return sSemi2Pitch[useSharps][semi];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) noteYWithPitch:(int)pitch accidental:(VLMusicElement*)accidental
|
2006-09-11 02:49:56 +00:00
|
|
|
{
|
|
|
|
int semi = pitch % 12;
|
|
|
|
int octave = (pitch / 12) - 5;
|
2006-10-21 09:23:37 +00:00
|
|
|
int key = [self song]->fProperties.front().fKey;
|
|
|
|
|
|
|
|
*accidental = sSemi2Accidental[key+6][semi];
|
|
|
|
|
|
|
|
return (octave*3.5f+[self stepWithPitch:pitch]*0.5f-1.0f)*kLineH;
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 09:23:37 +00:00
|
|
|
- (float) noteYInMeasure:(int)measure withPitch:(int)pitch accidental:(VLMusicElement*)accidental
|
2006-10-02 08:32:25 +00:00
|
|
|
{
|
2006-10-21 09:23:37 +00:00
|
|
|
return [self systemY:measure/fMeasPerSystem]
|
|
|
|
+ [self noteYWithPitch:pitch accidental:accidental];
|
2006-10-02 08:32:25 +00:00
|
|
|
}
|
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
- (float) noteXInMeasure:(int)measure at:(VLFraction)at
|
|
|
|
{
|
|
|
|
const VLProperties & prop = [self song]->fProperties.front();
|
2006-10-02 06:07:44 +00:00
|
|
|
const float mx = fClefKeyW+(measure%fMeasPerSystem)*fMeasureW;
|
2006-09-11 02:49:56 +00:00
|
|
|
|
2006-10-09 07:28:49 +00:00
|
|
|
at *= 4 * prop.fDivisions;
|
2006-09-11 02:49:56 +00:00
|
|
|
int div = at.fNum / at.fDenom;
|
|
|
|
|
2006-10-02 06:07:44 +00:00
|
|
|
return mx + (div + (div / fDivPerGroup) + 1)*kNoteW;
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
|
2006-10-09 07:28:49 +00:00
|
|
|
- (void) scrollMeasureToVisible:(int)measure
|
|
|
|
{
|
|
|
|
NSRect r = NSMakeRect(fClefKeyW+(measure%fMeasPerSystem)*fMeasureW,
|
|
|
|
[self systemY:measure/fMeasPerSystem]-kSystemY,
|
|
|
|
fMeasureW, kSystemH);
|
|
|
|
[self scrollRectToVisible:r];
|
|
|
|
}
|
|
|
|
|
2006-10-16 09:01:25 +00:00
|
|
|
- (void) setTrackingRect
|
|
|
|
{
|
|
|
|
NSRect r = [self bounds];
|
|
|
|
NSPoint mouse =
|
|
|
|
[self convertPoint:[[self window] mouseLocationOutsideOfEventStream]
|
|
|
|
fromView: nil];
|
|
|
|
BOOL within = [self mouse:mouse inRect:r];
|
|
|
|
|
|
|
|
fCursorTracking = [self addTrackingRect:r owner:self
|
|
|
|
userData:nil assumeInside:within];
|
|
|
|
[[self window] setAcceptsMouseMovedEvents:within];
|
2006-10-28 09:18:55 +00:00
|
|
|
if (within && ![self editTarget])
|
2006-10-16 09:01:25 +00:00
|
|
|
[[self window] makeFirstResponder:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) clearTrackingRect
|
|
|
|
{
|
|
|
|
[self removeTrackingRect:fCursorTracking];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)resetCursorRects
|
|
|
|
{
|
|
|
|
[super resetCursorRects];
|
|
|
|
[self clearTrackingRect];
|
|
|
|
[self setTrackingRect];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)viewWillMoveToWindow:(NSWindow *)win
|
|
|
|
{
|
|
|
|
if (!win && [self window]) [self clearTrackingRect];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)viewDidMoveToWindow
|
|
|
|
{
|
|
|
|
if ([self window]) {
|
|
|
|
[self setTrackingRect];
|
|
|
|
[[self window] makeFirstResponder:self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
- (void) recalculateDimensions
|
|
|
|
{
|
2006-10-02 08:32:25 +00:00
|
|
|
NSScrollView * scroll = [self enclosingScrollView];
|
2006-09-11 02:49:56 +00:00
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
NSSize sz = [scroll contentSize];
|
|
|
|
sz.width /= fDisplayScale;
|
|
|
|
sz.height /= fDisplayScale;
|
2006-10-02 05:29:37 +00:00
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
const VLSong * song = [self song];
|
|
|
|
const VLProperties & prop = song->fProperties.front();
|
|
|
|
|
2006-10-02 06:07:44 +00:00
|
|
|
fGroups = prop.fTime.fNum / std::max(prop.fTime.fDenom / 4, 1);
|
|
|
|
fQuarterBeats = (prop.fTime.fNum*4) / prop.fTime.fDenom;
|
|
|
|
fDivPerGroup = prop.fDivisions * (fQuarterBeats / fGroups);
|
|
|
|
fClefKeyW = kClefX+kClefW+(std::labs(prop.fKey)+1)*kKeyW;
|
|
|
|
fMeasureW = fGroups*(fDivPerGroup+1)*kNoteW;
|
|
|
|
fMeasPerSystem = (int)std::floor((sz.width-fClefKeyW) / fMeasureW);
|
|
|
|
fNumSystems = (song->CountMeasures()+fMeasPerSystem-1)/fMeasPerSystem;
|
|
|
|
sz.height = fNumSystems*kSystemH;
|
2006-09-11 02:49:56 +00:00
|
|
|
|
2006-10-02 06:07:44 +00:00
|
|
|
NSSize frameSz = {sz.width * fDisplayScale, sz.height * fDisplayScale};
|
2006-10-02 05:29:37 +00:00
|
|
|
|
|
|
|
[self setFrameSize:frameSz];
|
2006-10-02 06:07:44 +00:00
|
|
|
[self setBoundsSize:sz];
|
2006-10-02 05:29:37 +00:00
|
|
|
[self setNeedsDisplay:YES];
|
2006-10-08 05:56:25 +00:00
|
|
|
|
|
|
|
if (fNeedsRecalc == kFirstRecalc) {
|
|
|
|
NSView *dv = [scroll documentView];
|
|
|
|
NSView *cv = [scroll contentView];
|
|
|
|
|
|
|
|
[dv scrollPoint:
|
|
|
|
NSMakePoint(0.0, NSMaxY([dv frame])-NSHeight([cv bounds]))];
|
|
|
|
}
|
|
|
|
|
|
|
|
fNeedsRecalc = kNoRecalc;
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
- (void)drawGridForSystem:(int)system
|
2006-09-11 02:49:56 +00:00
|
|
|
{
|
2006-10-02 05:29:37 +00:00
|
|
|
static NSDictionary * sMeasNoFont = nil;
|
|
|
|
if (!sMeasNoFont)
|
|
|
|
sMeasNoFont =
|
|
|
|
[[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSFont fontWithName: @"Helvetica" size: 10],
|
|
|
|
NSFontAttributeName,
|
|
|
|
nil];
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
const float kSystemY = [self systemY:system];
|
|
|
|
const float kLineW = fClefKeyW + fMeasPerSystem*fMeasureW;
|
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
const VLSong * song = [self song];
|
|
|
|
const VLProperties & prop = song->fProperties.front();
|
|
|
|
|
|
|
|
NSBezierPath * bz = [NSBezierPath bezierPath];
|
|
|
|
|
|
|
|
//
|
|
|
|
// Draw lines
|
|
|
|
//
|
|
|
|
[bz setLineWidth:0.0];
|
2006-10-02 08:32:25 +00:00
|
|
|
for (int line = 0; line<5; ++line) {
|
|
|
|
const float y = kSystemY+line*kLineH;
|
|
|
|
[bz moveToPoint: NSMakePoint(kLineX, y)];
|
|
|
|
[bz lineToPoint: NSMakePoint(kLineX+kLineW, y)];
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
[bz stroke];
|
|
|
|
[bz removeAllPoints];
|
|
|
|
//
|
|
|
|
// Draw measure lines
|
|
|
|
//
|
|
|
|
[bz setLineWidth:2.0];
|
2006-10-02 08:32:25 +00:00
|
|
|
for (int measure = 0; measure<=fMeasPerSystem; ++measure) {
|
|
|
|
const float x = fClefKeyW+measure*fMeasureW;
|
|
|
|
const float yy = kSystemY+4.0f*kLineH;
|
|
|
|
[bz moveToPoint: NSMakePoint(x, kSystemY)];
|
|
|
|
[bz lineToPoint: NSMakePoint(x, yy)];
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
[bz stroke];
|
|
|
|
[bz removeAllPoints];
|
|
|
|
|
|
|
|
//
|
|
|
|
// Draw division lines
|
|
|
|
//
|
|
|
|
[bz setLineWidth:0.0];
|
|
|
|
[[NSColor colorWithDeviceWhite:0.8f alpha:1.0f] set];
|
2006-10-02 08:32:25 +00:00
|
|
|
for (int measure = 0; measure<fMeasPerSystem; ++measure) {
|
|
|
|
const float mx = fClefKeyW+measure*fMeasureW;
|
|
|
|
const float y0 = kSystemY-2.0f*kLineH;
|
|
|
|
const float yy = kSystemY+6.0f*kLineH;
|
|
|
|
for (int group = 0; group < fGroups; ++group) {
|
|
|
|
for (int div = 0; div < fDivPerGroup; ++div) {
|
|
|
|
const float x = mx+(group*(fDivPerGroup+1)+div+1)*kNoteW;
|
|
|
|
[bz moveToPoint: NSMakePoint(x, y0)];
|
|
|
|
[bz lineToPoint: NSMakePoint(x, yy)];
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-02 08:32:25 +00:00
|
|
|
}
|
2006-09-11 02:49:56 +00:00
|
|
|
[bz stroke];
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
//
|
|
|
|
// Draw clef
|
|
|
|
//
|
|
|
|
[[self musicElement:kMusicGClef]
|
|
|
|
compositeToPoint: NSMakePoint(kClefX, kSystemY+kClefY)
|
|
|
|
operation: NSCompositeSourceOver];
|
|
|
|
//
|
|
|
|
// Draw measure #
|
|
|
|
//
|
|
|
|
[[NSString stringWithFormat:@"%d", system*fMeasPerSystem+1]
|
|
|
|
drawAtPoint: NSMakePoint(kMeasNoX, kSystemY+kMeasNoY)
|
|
|
|
withAttributes: sMeasNoFont];
|
|
|
|
//
|
|
|
|
// Draw key (sharps & flats)
|
|
|
|
//
|
|
|
|
if (prop.fKey > 0) {
|
|
|
|
float x = kClefX+kClefW;
|
|
|
|
for (int i=0; i<prop.fKey; ++i) {
|
|
|
|
[[self musicElement:kMusicSharp]
|
|
|
|
compositeToPoint:
|
|
|
|
NSMakePoint(x, kSystemY+sSharpPos[i]+kSharpY)
|
|
|
|
operation: NSCompositeSourceOver];
|
|
|
|
x += kAccW;
|
|
|
|
}
|
|
|
|
} else if (prop.fKey < 0) {
|
|
|
|
float x = kClefX+kClefW;
|
|
|
|
for (int i=0; -i>prop.fKey; ++i) {
|
|
|
|
[[self musicElement: kMusicFlat]
|
|
|
|
compositeToPoint:
|
|
|
|
NSMakePoint(x, kSystemY+sFlatPos[i]+kFlatY)
|
|
|
|
operation: NSCompositeSourceOver];
|
|
|
|
x += kAccW;
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-02 08:32:25 +00:00
|
|
|
}
|
2006-09-11 02:49:56 +00:00
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
- (void)drawRect:(NSRect)rect
|
|
|
|
{
|
|
|
|
if (fNeedsRecalc)
|
|
|
|
[self recalculateDimensions];
|
|
|
|
|
|
|
|
const float kLineW = fClefKeyW + fMeasPerSystem*fMeasureW;
|
|
|
|
for (int system = 0; system<fNumSystems; ++system) {
|
|
|
|
const float kSystemY = [self systemY:system];
|
|
|
|
if (!NSIntersectsRect(rect,
|
|
|
|
NSMakeRect(kLineX, kSystemY+kClefY,
|
|
|
|
kLineW, kSystemH-kClefY)
|
|
|
|
))
|
|
|
|
continue; // This system does not need to be drawn
|
|
|
|
[self drawGridForSystem:system];
|
|
|
|
[self drawNotesForSystem:system];
|
|
|
|
[self drawChordsForSystem:system];
|
2006-10-08 05:56:25 +00:00
|
|
|
}
|
2006-10-28 09:18:55 +00:00
|
|
|
[[self editTarget] highlightCursor];
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) setKey:(id)sender
|
|
|
|
{
|
2006-10-14 10:09:42 +00:00
|
|
|
[[NSAlert alertWithMessageText:@"Transpose Song?"
|
|
|
|
defaultButton:@"Transpose"
|
|
|
|
alternateButton:@"Cancel"
|
|
|
|
otherButton:@"Change Key"
|
|
|
|
informativeTextWithFormat:
|
|
|
|
@"Do you want to transpose the song into the new key?"]
|
|
|
|
beginSheetModalForWindow:[self window]
|
|
|
|
modalDelegate:self didEndSelector:@selector(setKey:returnCode:contextInfo:)
|
|
|
|
contextInfo:sender];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setKey:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(id)sender
|
|
|
|
{
|
|
|
|
if (returnCode == NSAlertAlternateReturn)
|
|
|
|
return;
|
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
int key = [[sender selectedItem] tag];
|
2006-10-14 10:09:42 +00:00
|
|
|
[[self document] setKey:key transpose:returnCode==NSAlertDefaultReturn];
|
2006-10-08 05:56:25 +00:00
|
|
|
fNeedsRecalc = kRecalc;
|
2006-10-13 08:09:51 +00:00
|
|
|
[self setNeedsDisplay: YES];
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) setTime:(id)sender
|
|
|
|
{
|
|
|
|
int time = [[sender selectedItem] tag];
|
|
|
|
|
|
|
|
[[self document] setTimeNum: time >> 8 denom: time & 0xFF];
|
2006-10-08 05:56:25 +00:00
|
|
|
fNeedsRecalc = kRecalc;
|
2006-09-11 02:49:56 +00:00
|
|
|
[self setNeedsDisplay: YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction) setDivisions:(id)sender
|
|
|
|
{
|
|
|
|
int div = [[sender selectedItem] tag];
|
|
|
|
|
|
|
|
[[self document] setDivisions: div];
|
2006-10-08 05:56:25 +00:00
|
|
|
fNeedsRecalc = kRecalc;
|
2006-09-11 02:49:56 +00:00
|
|
|
[self setNeedsDisplay: YES];
|
|
|
|
}
|
|
|
|
|
2006-09-12 07:55:28 +00:00
|
|
|
- (IBAction)hideFieldEditor:(id)sender
|
|
|
|
{
|
2006-10-02 06:07:44 +00:00
|
|
|
[fFieldEditor setAction:nil];
|
2006-09-12 07:55:28 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 08:09:51 +00:00
|
|
|
const float kSemiFloor = -3.0f*kLineH;
|
2006-10-21 09:23:37 +00:00
|
|
|
static int8_t sSemiToPitch[] = {
|
2006-10-02 08:32:25 +00:00
|
|
|
53, // F
|
|
|
|
55, 57, // A
|
|
|
|
59, 60, // Middle C
|
|
|
|
62, 64, // E
|
|
|
|
65, 67, // G
|
|
|
|
69, 71, // B
|
|
|
|
72, 74, // D
|
|
|
|
76, 77, // F
|
|
|
|
79, 81, // A
|
|
|
|
83, 84, // C
|
|
|
|
86, 88 // E
|
|
|
|
};
|
|
|
|
|
2006-10-21 09:23:37 +00:00
|
|
|
static int8_t sFlatAcc[] = {
|
|
|
|
6, // Cb
|
|
|
|
11,
|
|
|
|
4, // Db
|
|
|
|
9,
|
|
|
|
2, // Eb
|
|
|
|
7, // Fb
|
|
|
|
12,
|
|
|
|
5, // Gb
|
|
|
|
10,
|
|
|
|
3, // Ab
|
|
|
|
8,
|
|
|
|
1, // Bb
|
|
|
|
};
|
|
|
|
|
|
|
|
static int8_t sSharpAcc[] = {
|
|
|
|
2, // C# is the 2nd sharp
|
|
|
|
9,
|
|
|
|
4, // D#
|
|
|
|
11,
|
|
|
|
6, // E#
|
|
|
|
1, // F#
|
|
|
|
8,
|
|
|
|
3, // G#
|
|
|
|
10,
|
|
|
|
5, // A#
|
|
|
|
12,
|
|
|
|
7, // B#
|
|
|
|
};
|
|
|
|
|
|
|
|
- (void) accidentalFromEvent:(NSEvent *)event
|
|
|
|
{
|
|
|
|
const VLProperties & prop = [self song]->fProperties.front();
|
|
|
|
|
|
|
|
fCursorAccidental = (VLMusicElement)0;
|
|
|
|
if (prop.fKey >= 0) {
|
|
|
|
if (prop.fKey >= sSharpAcc[fCursorPitch % 12]) { // Sharp in Key
|
|
|
|
switch ([event modifierFlags] & (NSAlternateKeyMask|NSCommandKeyMask)) {
|
|
|
|
case NSAlternateKeyMask:
|
|
|
|
fCursorAccidental = kMusicFlatCursor; // G# -> Gb
|
|
|
|
fCursorActualPitch = fCursorPitch-1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case NSCommandKeyMask:
|
|
|
|
fCursorActualPitch = fCursorPitch+1;
|
|
|
|
break; // G# -> G#
|
|
|
|
case NSAlternateKeyMask|NSCommandKeyMask:
|
|
|
|
fCursorAccidental = kMusicNaturalCursor; // G# -> G
|
|
|
|
fCursorActualPitch = fCursorPitch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (prop.fKey <= -sFlatAcc[fCursorPitch % 12]) { // Flat in Key
|
|
|
|
switch ([event modifierFlags] & (NSAlternateKeyMask|NSCommandKeyMask)) {
|
|
|
|
default:
|
|
|
|
case NSAlternateKeyMask:
|
|
|
|
fCursorActualPitch = fCursorPitch-1;
|
|
|
|
break; // Gb -> Gb
|
|
|
|
case NSCommandKeyMask:
|
|
|
|
fCursorAccidental = kMusicSharpCursor; // Gb -> G#
|
|
|
|
fCursorActualPitch = fCursorPitch+1;
|
|
|
|
break;
|
|
|
|
case NSAlternateKeyMask|NSCommandKeyMask:
|
|
|
|
fCursorAccidental = kMusicNaturalCursor; // Gb -> G
|
|
|
|
fCursorActualPitch = fCursorPitch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Natural
|
|
|
|
//
|
|
|
|
switch ([event modifierFlags] & (NSAlternateKeyMask|NSCommandKeyMask)) {
|
|
|
|
case NSAlternateKeyMask:
|
|
|
|
fCursorAccidental = kMusicFlatCursor; // G -> Gb
|
|
|
|
fCursorActualPitch = fCursorPitch-1;
|
|
|
|
break;
|
|
|
|
case NSCommandKeyMask:
|
|
|
|
fCursorAccidental = kMusicSharpCursor; // G -> G#
|
|
|
|
fCursorActualPitch = fCursorPitch+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case NSAlternateKeyMask|NSCommandKeyMask:
|
|
|
|
fCursorActualPitch = fCursorPitch;
|
|
|
|
break; // G -> G
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
- (VLRegion) findRegionForEvent:(NSEvent *) event
|
|
|
|
{
|
|
|
|
fCursorPitch = VLNote::kNoPitch;
|
|
|
|
|
|
|
|
const VLProperties & prop = [self song]->fProperties.front();
|
|
|
|
NSPoint loc = [event locationInWindow];
|
|
|
|
loc = [self convertPoint:loc fromView:nil];
|
|
|
|
|
|
|
|
if (loc.y < 0.0f || loc.y >= fNumSystems*kSystemH)
|
2006-10-03 17:52:54 +00:00
|
|
|
return fCursorRegion = kRegionNowhere;
|
2006-10-02 08:32:25 +00:00
|
|
|
|
|
|
|
int system = fNumSystems - static_cast<int>(loc.y / kSystemH) - 1;
|
|
|
|
loc.y = fmodf(loc.y, kSystemH);
|
|
|
|
|
|
|
|
loc.x -= fClefKeyW;
|
|
|
|
if (loc.x < 0.0f || loc.x >= fMeasPerSystem*fMeasureW)
|
2006-10-03 17:52:54 +00:00
|
|
|
return fCursorRegion = kRegionNowhere;
|
2006-10-02 08:32:25 +00:00
|
|
|
|
|
|
|
int measure = static_cast<int>(loc.x / fMeasureW);
|
|
|
|
loc.x -= measure*fMeasureW;
|
|
|
|
int group = static_cast<int>(loc.x / ((fDivPerGroup+1)*kNoteW));
|
|
|
|
loc.x -= group*(fDivPerGroup+1)*kNoteW;
|
|
|
|
int div = static_cast<int>(roundf(loc.x / kNoteW))-1;
|
|
|
|
div = std::min(std::max(div, 0), fDivPerGroup-1);
|
|
|
|
fCursorAt = VLFraction(div+group*fDivPerGroup, 4*prop.fDivisions);
|
|
|
|
fCursorMeasure = measure+system*fMeasPerSystem;
|
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
if (fCursorMeasure > [self song]->fMeasures.size())
|
|
|
|
return fCursorRegion = kRegionNowhere;
|
|
|
|
|
|
|
|
if (loc.y >= kSystemY+kChordY) {
|
|
|
|
//
|
|
|
|
// Chord, round to quarters
|
|
|
|
//
|
|
|
|
int scale = fCursorAt.fDenom / 4;
|
|
|
|
fCursorAt = VLFraction(fCursorAt.fNum / scale, 4);
|
|
|
|
return fCursorRegion = kRegionChord;
|
|
|
|
} else if (loc.y < kSystemY+kLyricsY) {
|
|
|
|
return fCursorRegion = kRegionLyrics;
|
|
|
|
}
|
2006-10-02 08:32:25 +00:00
|
|
|
|
2006-10-21 09:23:37 +00:00
|
|
|
loc.y -= kSystemY+kSemiFloor;
|
|
|
|
int semi = static_cast<int>(roundf(loc.y / (0.5f*kLineH)));
|
|
|
|
fCursorPitch = sSemiToPitch[semi];
|
|
|
|
|
|
|
|
[self accidentalFromEvent:event];
|
2006-10-02 08:32:25 +00:00
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
return fCursorRegion = kRegionNote;
|
2006-10-02 08:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseMoved:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([event modifierFlags] & NSAlphaShiftKeyMask)
|
|
|
|
return; // Keyboard mode, ignore mouse
|
|
|
|
|
|
|
|
bool hadCursor = fCursorPitch != VLNote::kNoPitch;
|
|
|
|
[self findRegionForEvent:event];
|
|
|
|
bool hasCursor = fCursorPitch != VLNote::kNoPitch;
|
|
|
|
|
|
|
|
[self setNeedsDisplay:(hadCursor || hasCursor)];
|
|
|
|
}
|
|
|
|
|
2006-10-21 09:23:37 +00:00
|
|
|
- (void)flagsChanged:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if (fCursorPitch != VLNote::kNoPitch) {
|
|
|
|
[self accidentalFromEvent:event];
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
- (void) mouseEntered:(NSEvent *)event
|
|
|
|
{
|
|
|
|
[[self window] setAcceptsMouseMovedEvents:YES];
|
|
|
|
[self mouseMoved:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseExited:(NSEvent *)event
|
|
|
|
{
|
|
|
|
[[self window] setAcceptsMouseMovedEvents:NO];
|
|
|
|
[self mouseMoved:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseDown:(NSEvent *)event
|
|
|
|
{
|
|
|
|
switch ([self findRegionForEvent:event]) {
|
|
|
|
case kRegionNote:
|
|
|
|
[self addNoteAtCursor];
|
|
|
|
break;
|
2006-10-03 17:52:54 +00:00
|
|
|
case kRegionChord:
|
|
|
|
[self editChord];
|
|
|
|
break;
|
2006-10-02 08:32:25 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) keyDown:(NSEvent *)event
|
|
|
|
{
|
|
|
|
NSString * k = [event charactersIgnoringModifiers];
|
|
|
|
|
|
|
|
switch ([k characterAtIndex:0]) {
|
|
|
|
case '\r':
|
|
|
|
[self startKeyboardCursor];
|
|
|
|
[self addNoteAtCursor];
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
[self startKeyboardCursor];
|
|
|
|
VLSoundOut::Instance()->PlayNote(VLNote(1, fCursorPitch));
|
|
|
|
break;
|
|
|
|
case 'r':
|
2006-10-16 09:01:25 +00:00
|
|
|
if (fClickMode == 'r')
|
|
|
|
fClickMode = ' ';
|
|
|
|
else
|
|
|
|
fClickMode = 'r';
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
if (fClickMode == 'k')
|
|
|
|
fClickMode = ' ';
|
|
|
|
else
|
|
|
|
fClickMode = 'k';
|
2006-10-02 08:32:25 +00:00
|
|
|
break;
|
2006-10-16 09:01:25 +00:00
|
|
|
[self setNeedsDisplay:YES];
|
2006-10-02 08:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-03 17:52:54 +00:00
|
|
|
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
|
|
|
|
{
|
2006-10-28 09:18:55 +00:00
|
|
|
return [[self editTarget] validValue:[fFieldEditor stringValue]];
|
2006-10-03 17:52:54 +00:00
|
|
|
}
|
|
|
|
|
2006-10-08 05:56:25 +00:00
|
|
|
- (void)controlTextDidEndEditing:(NSNotification *)note
|
|
|
|
{
|
2006-10-28 09:18:55 +00:00
|
|
|
VLEditable * editable = [self editTarget];
|
2006-10-08 05:56:25 +00:00
|
|
|
switch ([[[note userInfo] objectForKey:@"NSTextMovement"] intValue]) {
|
|
|
|
case NSTabTextMovement:
|
|
|
|
[editable moveToNext];
|
|
|
|
break;
|
|
|
|
case NSBacktabTextMovement:
|
|
|
|
[editable moveToPrev];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
[editable autorelease];
|
|
|
|
editable = nil;
|
|
|
|
}
|
2006-10-28 09:18:55 +00:00
|
|
|
[self setEditTarget:editable];
|
2006-10-08 05:56:25 +00:00
|
|
|
if (editable)
|
|
|
|
[fFieldEditor selectText:self];
|
|
|
|
[[self window] performSelectorOnMainThread:@selector(makeFirstResponder:)
|
|
|
|
withObject:(editable ? fFieldEditor : self)
|
|
|
|
waitUntilDone:NO];
|
2006-10-09 07:28:49 +00:00
|
|
|
[self setNeedsDisplay: YES];
|
2006-10-08 05:56:25 +00:00
|
|
|
}
|
|
|
|
|
2006-09-11 02:49:56 +00:00
|
|
|
@end
|