/******************************************************************************
FILE
    SqButton.m
DESCRIPTION
	NeXTstep user Interface for Squeak.
	This class reifies a Smalltalk mouse button.
AUTHOR
	<PJB> Pascal J. Bourguignon
MODIFICATIONS
	1998/06/25 <PJB> Creation .
LEGAL
	Copyright Pascal J. Bourguignon 1998 - 1998

	This program is free software; you can redistribute it and/or
	modify it under the terms of the version 2 of the GNU General Public 
	License as published by the Free Software Foundation.
	
	This program is distributed in the hope that it will be useful, but 
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
	hereafter for more details.
******************************************************************************/
#import "SqButton.h"

// ------------------------------------------------------------------------- //

@interface SqButton(Private)

	-(void)setMask:(int)newMask;
	-(void)updateTeam;
	
@end //SqButton(Private).

// ------------------------------------------------------------------------- //

@implementation SqButton(Private)

	-(void)setMask:(int)newMask
	{
		if(mask!=newMask){
			mask=flags|newMask;
			[nextButton setMask:mask];
		}
	}//setMask:;
	
	
	-(void)updateTeam
	{
		mask=flags;
		[nextButton setMask:mask];
	}//updateTeam;
	
	
@end //SqButton(Private).

// ------------------------------------------------------------------------- //

@implementation SqButton

	static struct {
		SEL  needer;
		char code;
	} Needers[7];
	
	+initialize
	{
		int i=0;
		Needers[i].needer=@selector(needsLeftMouse);
		Needers[i].code='l';
		i++;
		Needers[i].needer=@selector(needsRightMouse);
		Needers[i].code='r';
		i++;
		Needers[i].needer=@selector(needsCommand);
		Needers[i].code='m';
		i++;
		Needers[i].needer=@selector(needsAlternate);
		Needers[i].code='a';
		i++;
		Needers[i].needer=@selector(needsControl);
		Needers[i].code='c';
		i++;
		Needers[i].needer=@selector(needsShift);
		Needers[i].code='s';
		i++;
		Needers[i].needer=0;
		Needers[i].code='\0';
		i++;
		return(self);
	}//initialize;
			


	-(id)init
	{
		self=[super init];
		if(self!=0){
			flags=0;
			mask=0;
			nextButton=0;
		}
		return(self);
	}//init;
	
	
	-(id)free
	{
		return([super free]);
	}//free;
	
	
	-(void)setNextButton:(SqButton*)next
	{
		nextButton=next;
	}//setNextButton:;
	
	
	-(BOOL)needsLeftMouse
	{
		return((flags&SqButton_LeftMouseEventFlag)!=0);
	}//needsLeftMouse;
	
	
	-(BOOL)needsRightMouse
	{
		return((flags&SqButton_RightMouseEventFlag)!=0);
	}//needsRightMouse;
	
	
	-(BOOL)needsCommand
	{
		return((flags&NX_COMMANDMASK)!=0);
	}//needsCommand;
	
	
	-(BOOL)needsAlternate
	{
		return((flags&NX_ALTERNATEMASK)!=0);
	}//needsAlternate;
	
	
	-(BOOL)needsControl
	{
		return((flags&NX_CONTROLMASK)!=0);
	}//needsControl;
	
	
	-(BOOL)needsShift
	{
		return((flags&NX_SHIFTMASK)!=0);
	}//needsShift;
	

	-(void)setNeedsLeftMouse:(BOOL)bit
	{
		if(bit){
			flags|=SqButton_LeftMouseEventFlag;
		}else{
			flags&=(~SqButton_LeftMouseEventFlag);
		}
		[self updateTeam];
	}//setNeedsLeftMouse:;
	

	-(void)setNeedsRightMouse:(BOOL)bit
	{
		if(bit){
			flags|=SqButton_RightMouseEventFlag;
		}else{
			flags&=(~SqButton_RightMouseEventFlag);
		}
		[self updateTeam];
	}//setNeedsRightMouse:;
	

	-(void)setNeedsCommand:(BOOL)bit
	{
		if(bit){
			flags|=NX_COMMANDMASK;
		}else{
			flags&=(~NX_COMMANDMASK);
		}
		[self updateTeam];
	}//setNeedsCommand:;
	

	-(void)setNeedsAlternate:(BOOL)bit
	{
		if(bit){
			flags|=NX_ALTERNATEMASK;
		}else{
			flags&=(~NX_ALTERNATEMASK);
		}
		[self updateTeam];
	}//setNeedsAlternate:;
	

	-(void)setNeedsControl:(BOOL)bit
	{
		if(bit){
			flags|=NX_CONTROLMASK;
		}else{
			flags&=(~NX_CONTROLMASK);
		}
		[self updateTeam];
	}//setNeedsControl:;
	

	-(void)setNeedsShift:(BOOL)bit
	{
		if(bit){
			flags|=NX_SHIFTMASK;
		}else{
			flags&=(~NX_SHIFTMASK);
		}
		[self updateTeam];
	}//setNeedsShift:;
	

	-(BOOL)isPressedWhen:(int)eventFlags
	{
		return((eventFlags&mask)==flags);
	}//isPressedWhen:;
	
	
	-(void)configureMatrix:(Matrix*)matrix
	{
		[[matrix findCellWithTag:0]setState:[self needsLeftMouse]?1:0];
		[[matrix findCellWithTag:1]setState:[self needsRightMouse]?1:0];
		[[matrix findCellWithTag:2]setState:[self needsCommand]?1:0];
		[[matrix findCellWithTag:3]setState:[self needsAlternate]?1:0];
		[[matrix findCellWithTag:4]setState:[self needsControl]?1:0];
		[[matrix findCellWithTag:5]setState:[self needsShift]?1:0];
	}//matrix;
	
	
	-(void)takeFromMatrix:(Matrix*)matrix
	{
		[self setNeedsLeftMouse: [[matrix findCellWithTag:0]state]!=0];
		[self setNeedsRightMouse:[[matrix findCellWithTag:1]state]!=0];
		[self setNeedsCommand:   [[matrix findCellWithTag:2]state]!=0];
		[self setNeedsAlternate: [[matrix findCellWithTag:3]state]!=0];
		[self setNeedsControl:   [[matrix findCellWithTag:4]state]!=0];
		[self setNeedsShift:     [[matrix findCellWithTag:5]state]!=0];
		[self updateTeam];
	}//takeFromMatrix:;
	
	
	-(const char*)stringConfiguration
	{
		int i=0,j=0;;
		while(Needers[i].code!=0){
			if([self perform:Needers[i].needer]){
				configuration[j++]=Needers[i].code;
			}
			i++;
		}
		configuration[j++]='\0';
		return(configuration);
	}//stringConfiguration;
	
	
	-(void)takeFromString:(const char*)string
	{
		int i=0;
		flags=0;
		while(string[i]!='\0'){
			switch(string[i]){
			case 'l':case 'L':
				[self setNeedsLeftMouse:YES];
				break;
			case 'r':case 'R':
				[self setNeedsRightMouse:YES];
				break;
			case 'm':case 'M':
				[self setNeedsCommand:YES];
				break;
			case 'a':case 'A':
				[self setNeedsAlternate:YES];
				break;
			case 'c':case 'C':
				[self setNeedsControl:YES];
				break;
			case 's':case 'S':
				[self setNeedsShift:YES];
				break;
			default:
				break;
			}
			i++;
		}
		[self updateTeam];
	}//takeFromString:;
	
	
@end // SqButton.

/*** SqButton.m / Thu Aug 27 23:20:21 MET 1998 / PJB ***/

