
#import <MiscAppKit/MiscRubberband.h>

#import "TestRBView.h"



@implementation TestRBView

- initWithFrame:(NSRect)frameRect
{
	[super initWithFrame:frameRect];
        _selectionRect = NSZeroRect;
        _hasSelection = NO;
	return self;
}

- (void)drawRect:(NSRect)r
{   
	PSsetgray(NSWhite);
	NSRectFill([self bounds]);
        if(_hasSelection){
            	PSsetgray(NSBlack);
                NSFrameRect(_selectionRect);
        }
}

- (void)mouseDown:(NSEvent *)event
{
    	NSRect r;
    	MiscRubberband *rubberband = [MiscRubberband rubberbandWithView:self];

        if([pivotMatrix selectedTag] == 0)
            	[rubberband setPivot:MiscCenterPivot];
        else
            	[rubberband setPivot:MiscCornerPivot];
        [rubberband setUsesBounds:[boundsToggle state]];
        [rubberband doStretch:event];
        r = [rubberband currentRect];
        if(_hasSelection){
            	if(r.size.width == 0 && r.size.height == 0)
			_hasSelection = NO;
                _selectionRect = r;
        } else {
            	if(r.size.width > 0 && r.size.height > 0)
                	_hasSelection = YES;
        	_selectionRect = r;            	
        }    
        [self display];    
}

- (NSRect)selectionRect
{
    	if(!_hasSelection)
            	return NSZeroRect;
        else
            	return _selectionRect;
}

- (void)setSelectionRect:(NSRect)aRect
{
    	_selectionRect = aRect;
        _hasSelection = YES;
        [self display];
}


@end

