#import "window.h"
#include <stdio.h>

@implementation MyView

- (void)drawRect:(NSRect)rect
{
    NSRect currentFrame;
    currentFrame = [self bounds];
    if (myImage == nil)
    {   
        frame = currentFrame;
        myImage = [NSBitmapImageRep alloc];
        [myImage
            initWithBitmapDataPlanes:NULL
            pixelsWide:frame.size.width pixelsHigh:frame.size.height
            bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO
            colorSpaceName:NSCalibratedRGBColorSpace
            bytesPerRow:(frame.size.width*3) bitsPerPixel:0
            ];
        bitmapData = [myImage bitmapData];
        memset((void*)bitmapData, -1, frame.size.width * frame.size.height * 3);
    }
    [myImage drawInRect:currentFrame];
}

- (BOOL) acceptsFirstMouse:(NSEvent *)theEvent { return NO; }
- (BOOL) acceptsFirstResponder { return YES; }
- (BOOL) isOpaque { return YES; }
- (BOOL) isFlipped { return NO; }
- (BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication { return YES; }
- (void) mouseDown:(NSEvent *)theEvent { printf("mouse down\n"); }
    //mouseDownPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
- (void) mouseUp:(NSEvent *)theEvent { printf("mouse up\n"); }
- (void) mouseDragged:(NSEvent *)theEvent { printf("mouse dragged\n"); }
- (void) keyDown:(NSEvent *)theEvent { printf("key down\n"); }
- (void) keyUp:(NSEvent *)theEvent { printf("key up\n"); }

- (void) myflush
{
    printf("myflush\n");
/*
    NSRect updateRect;
    x = viewX; y = viewY;
    w = frame.size.width;
    h = frame.size.height;
    updateRect.origin.x = 0;
    updateRect.origin.x = 0;
    updateRect.size.width = w;
    updateRect.size.height = 1;
    //    updateRect.origin.y = h - lineTable[myLine];
    
    //    [self setNeedsDisplayInRect:updateRect];
*/
    [self setNeedsDisplay:YES];
}

/*
{
    int w, h;
    int update_dwells;
    double x, y, step, currentY;
    int idx, bitmapIdx;
    int working_sequence;
    int myLine;
    NSRect updateRect;

    x = viewX; y = viewY;
    w = frame.size.width;
    h = frame.size.height;

    updateRect.origin.x = 0;
    updateRect.size.width = w;
    updateRect.size.height = 1;

        for (idx = 0, bitmapIdx = lineTable[myLine] * w * 3; idx < w; idx++, bitmapIdx += 3 )
        {
                bitmapData[bitmapIdx    ] = ( buf[idx] % 16) * 16;
                bitmapData[bitmapIdx + 1] = (( buf[idx] / 16 ) % 16 ) * 16;
                bitmapData[bitmapIdx + 2] = (( buf[idx] / 256 ) % 16 ) * 16;
        }
    //    updateRect.origin.y = h - lineTable[myLine];
    //    [self setNeedsDisplayInRect:updateRect];
    [self setNeedsDisplay:YES];
    [self setNeedsDisplay:YES];
}
*/

// delegate of NSApplication
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    printf("applicationDidFinishLaunching\n");
    NSWindow *window;
    window = [self window];
    [window makeKeyAndOrderFront:nil];
    [window setInitialFirstResponder:self];
}

@end

