/*  Quinto.m
 * Copyright Edmund Ronald 1991.
 * This source code, may be duplicated and distributed 
 *  by anyone, provided  that the icon er.tiff and makefile are distributed together with any copy .
 */

#import <appkit/appkit.h> // the kitchen sink?

id  theArray[5][5];

@interface MyView:View
- ButtonClicked:sender;
- newGame;
@end

@implementation MyView
- newGame
{ int i, j;
  for(i=0; i<5; i++) for(j=0; j<5; j++) [theArray[i][j] setState:0];
  return self;
}

- ButtonClicked:sender
{  int i,j,t;
   t= [sender tag];
   j = t/5;
   i = t % 5;
  
  if(i>0) [(theArray[i-1][j]) setState:![(theArray[i-1][j])state]];
  if(i<4) [(theArray[i+1][j]) setState:![(theArray[i+1][j])state]];
  if(j>0) [(theArray[i][j-1]) setState:![(theArray[i][j-1])state]];
  if(j<4) [(theArray[i][j+1]) setState:![(theArray[i][j+1])state]];
     
   return self;
}
@end

void setUp(void)
{
	id	 myWindow, myPanel, myMenu, windowText, myButton, theView;
	NXRect	 aRect;
	int i,j,t;
	
	// set up main window
	NXSetRect(&aRect, 100.0, 100.0, 210.0, 210.0); // location, extent
	myWindow = [[Window alloc] initContent:&aRect
					style:NX_TITLEDSTYLE
					backing:NX_BUFFERED
					buttonMask:NX_MINIATURIZEBUTTONMASK
					defer:NO];
	[myWindow setTitle:"QUINTO 1.0"];
        
	// set up a subview
        NXSetRect(&aRect, 0.0, 0.0, 210.0, 210.0);
	theView = [[MyView alloc] initFrame:&aRect];
        [[myWindow contentView] addSubview:theView];
	
 	//set up the buttons in the subview
	j=0;
	while(j<5) { i=0;  while(i<5){
		NXSetRect(&aRect,i*30.0+30.0, j*30.0+30.0, 30.0, 30.0);
        	myButton =  [[Button alloc] initFrame:&aRect] ;     
   		t=(5*j +i);
		[myButton setTag:t ];
		[myButton setTarget:theView];
		[myButton setAction:@selector(ButtonClicked:)];
	        [myButton setType:NX_PUSHONPUSHOFF ];                                                                                                                                        
		[theView addSubview:myButton];
		theArray[i][j] = myButton;
		 i++;}  j++;}

	//set up info text
	NXSetRect(&aRect, 0.0, 0.0, 400.0, 200.0);
	windowText = [[Text alloc] initFrame:&aRect
					text:"\nCopyright Edmund Ronald 1991\n"
					"Use and distribute freely for non-commercial purposes\n"
					"but do not change copyright or program name\n"
					"NO WARRANTY WHATSOEVER\n\n"
					"eronald@cnam.cnam.fr\n"
					"\n"
					"AIM OF GAME: HILITE ALL BUTTONS\n *IT©S POSSIBLE*" 
					 alignment:NX_CENTERED];
	[windowText setOpaque:YES];
	
	//set up info panel
	NXSetRect(&aRect, 200.0, 500.0, 400.0, 200.0);
	myPanel = [[Panel alloc] initContent:&aRect
					style:NX_TITLEDSTYLE
					backing:NX_BUFFERED
					buttonMask:NX_CLOSEBUTTONMASK
					defer:YES];
	[myPanel setTitle:"About Quinto"];
	[[myPanel contentView] addSubview:windowText];
        [myPanel removeFromEventMask:(NX_KEYDOWNMASK | NX_KEYUPMASK)];


	myMenu = [[Menu alloc] initTitle:"Quinto"];
	[[myMenu addItem:"Info..."
			action:@selector(orderFront:)
			keyEquivalent:'\0']
				setTarget:myPanel];
	[[myMenu addItem:"New Game"
			action:@selector(newGame)
			keyEquivalent:'n']
				setTarget:theView];			
	[myMenu addItem:"Hide"
			action:@selector(hide:)
			keyEquivalent:'h'];
	[myMenu addItem:"Quit"
			action:@selector(terminate:)
			keyEquivalent:'q'];
	[myMenu sizeToFit];
	[NXApp setMainMenu:myMenu];


	[myWindow display];
	[myWindow orderFront:nil];
	[myWindow makeKeyWindow];
}


int main()
{
	[Application new];
	setUp();
	[NXApp run];
	[NXApp free];
	return 0;
}
