#import <QuartzCore/QuartzCore.h>
#import "TuneViewController.h"
#import "TuneView.h"
#import "Tune.h"

@implementation TuneView

@synthesize delegate, scale, tune, tuneTransform;

-(void)drawRect:(CGRect)rect
{
  CGContextRef context = UIGraphicsGetCurrentContext();

	CGContextTranslateCTM( context, 0.0, self.bounds.size.height );
	CGContextScaleCTM( context, 1.0, -1.0 );

	CGContextSetRGBFillColor( context, 1.0, 1.0, 1.0, 1.0 );
	CGContextFillRect( context, self.bounds );

  CGContextTranslateCTM( context, 0, self.bounds.size.height );

  CGContextTranslateCTM( context, 10, 30 );
  CGContextScaleCTM( context, self.scale, self.scale );
  CGContextScaleCTM( context, kTuneScale, kTuneScale );
	CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
  extern CGContextRef gcx;
  extern const char *abcInputString;
  gcx = context;
  abcInputString = [tune.dots UTF8String];
  const char *argv[] = {
    "abc2ps",
    "-o",
    "-e",
    "1",
    "-maxv",
    "8",
    "tune.abc",
    NULL
  };
  extern int abc2ps_main(int argc, const char *argv[]);
  abc2ps_main( 7, argv );
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch * t = [touches anyObject];
  if( [t tapCount] == 1 )
    [[NSNotificationCenter defaultCenter] postNotificationName:@"tap" object:[self superview]];
  else if( [t tapCount] == 2 )
    [[NSNotificationCenter defaultCenter] postNotificationName:@"doubleTap" object:[self superview]];
}

@end
