#import "Controller.h"
#import "Ruler.h"
#import "RuledScrollView.h"
#import "CrosshairView.h"
#import "DragPSView.h"		// DRAGGEDFILENAME
#import	<appkit/Application.h>	// NXApp
#import	<appkit/Matrix.h>
#import	<appkit/MenuCell.h>
#import <appkit/NXImage.h>
#import	<appkit/publicWraps.h>	// NXBeep

@implementation Controller

- appDidInit:sender
{	
	xSize = 700.0;
	ySize = 300.0;
	[self  setupTileView];
    [[ruledScrollView window] orderFront:NULL];
    
    return self;
}

- setupTileView
  /* called by appDidInit */
{
	NXRect		viewRect, rulerViewRect;
	id 		ruler;
	
    [ruledScrollView setVertScrollerRequired:YES];
    [ruledScrollView setHorizScrollerRequired:YES];
    [ruledScrollView setBorderType:NX_BEZEL];

	NXSetRect(&viewRect, 0, 0, xSize, ySize);
  // create the docView and place it within its scrollView
    crosshairView = [[CrosshairView alloc] initFrame:&viewRect];
    [ruledScrollView setDocView:crosshairView];

	// here we retrieve the topRuler (added by setTopRuler)
	// so that we can set some of its attributes
	ruler = [ruledScrollView topRuler];
  // set spacing for inches on top ruler (on printout - appears smaller on-screen) 
	[[ruler setScaleSpacing:72] setInverted:YES];
	[ruler setDraggableRuler:YES];

  // Create a Ruler for the bottom - this is how we could add a ruler
  // programmatically.  The height is set to 20 points 
  // (its width will be set to main view's size) 
	NXSetRect(&rulerViewRect, (NXCoord) 0, (NXCoord) 0, (NXCoord) 0, (NXCoord) 20);
	ruler = [[Ruler alloc] initFrame:&rulerViewRect];
	[ruledScrollView addRulerView:ruler toEdge:BOTTOMEDGE];

  // retrieve the Ruler for the leftside (added by setLeftRuler)
  // set width to 25 points (its height will be set to main view's size)
	ruler = [ruledScrollView leftRuler];
	[[ruler setAlignment:NX_LEFTALIGNED] setOrientation:VERTICAL];
	[[[ruler setBackgroundGray:NX_DKGRAY] setUnits:"V"] setBorder:5];
	[ruler setDraggableRuler:YES];

  // IMPORTANT: We must send a setSize message every
  // time the size of the main view is changed
	[ruledScrollView setSize];
    return self;
}

- toggleRulers:sender
{
	if ([ruledScrollView isRulerVisible:LEFTEDGE]) {
		[ruledScrollView hideRuler:LEFTEDGE];
		[ruledScrollView hideRuler:RIGHTEDGE];
		[ruledScrollView hideRuler:TOPEDGE];
		[ruledScrollView hideRuler:BOTTOMEDGE];
		[ruledScrollView setSize];
		[rulerToggleMenuCell setTitleNoCopy:(const char *)"Turn Rulers On"];
		}
	else {
		[ruledScrollView showRuler:LEFTEDGE];
		[ruledScrollView showRuler:RIGHTEDGE];
		[ruledScrollView showRuler:TOPEDGE];
		[ruledScrollView showRuler:BOTTOMEDGE];
		[ruledScrollView setSize];
		[rulerToggleMenuCell setTitleNoCopy:(const char *)"Turn Rulers Off"];
		}
	return self;
}

- setSize:sender
{
	[ruledScrollView setSizeIfNeeded];
	return self;
}

- cleanAndTerminate:sender
{
	// remove the temp dragging file before terminating
	remove((const char *)DRAGGEDFILENAME);
	[NXApp terminate:self];
	return self;
}

- xZoomIn:sender;
{
	if (xSize <1000) {
		NXRect		viewRect;
		xSize += 100.0;
		NXSetRect(&viewRect, 0, 0, xSize, ySize);
		[[ruledScrollView docView] setFrame:&viewRect];
		[ruledScrollView setSize];
		}
	else
		NXBeep();
	return self;
}

- xZoomOut:sender;
{
	if (xSize > 100) {
		NXRect		viewRect;
		xSize -= 100.0;
		NXSetRect(&viewRect, 0, 0, xSize, ySize);
		[[ruledScrollView docView] setFrame:&viewRect];
		[ruledScrollView setSize];
		}
	else
		NXBeep();
	return self;
}

- yZoomIn:sender;
{
	if (ySize < 1000) {
		NXRect		viewRect;
		ySize += 100.0;
		NXSetRect(&viewRect, 0, 0, xSize, ySize);
		[[ruledScrollView docView] setFrame:&viewRect];
		[ruledScrollView setSize];
		}
	else
		NXBeep();
	return self;
}

- yZoomOut:sender;
{
	if (ySize > 100) {
		NXRect		viewRect;
		ySize -= 100.0;
		NXSetRect(&viewRect, 0, 0, xSize, ySize);
		[[ruledScrollView docView] setFrame:&viewRect];
		[ruledScrollView setSize];
		}
	else
		NXBeep();
	return self;
}

- zoomSet:sender
{
	NXRect		viewRect;

	xSize = ySize = [[sender selectedCell] tag];
	ySize -= 100.0;
	NXSetRect(&viewRect, 0, 0, xSize, ySize);
	[[ruledScrollView docView] setFrame:&viewRect];
	[ruledScrollView setSize];
	return self;
}
@end
