//*****************************************************************************
//
//	Preferences.m.  
//
//		Preferences logic.
//
//			by	Felipe A. Rodriguez		
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//*****************************************************************************

#import "Preferences.h"
#import "PreferencesPanelController.h"
#import "SwapView.h"
#import <sys/types.h>
#import <sys/dir.h>  



@implementation Preferences

//*****************************************************************************
//
//		sent to us after nib objects are unarchived and init'd 
//
//*****************************************************************************
 
- awakeFromNib 
{
NXSize cSize;								// size of scroll's doc view
NXRect mRect;								// sizeof scroll's content view

	cSize.width = cSize.height = 60.0;

						// remem loc/size
	[[swapView window] setFrameAutosaveName:[[swapView window] title]]; 

						// adjust button preference scrollview's behaviour
	[prefScrollView setBorderType: NX_BEZEL];
	[prefScrollView setHorizScrollerRequired:YES];
	[prefScrollView setVertScrollerRequired:NO];
	[prefScrollView setLineScroll:2];

	[prefScrollView getVisibleRect:&mRect];
	bView = [[Matrix alloc] initFrame:&mRect];
	[bView setMode:NX_RADIOMODE];
	[bView setPrototype:[[[[ButtonCell alloc] initIconCell:"app"]
						setType:NX_PUSHONPUSHOFF]
						setTarget:self]];
	[bView setCellSize:&cSize];
	[bView addRow];
	[prefScrollView setDocView:bView];

    return self;
}
//*****************************************************************************
//
//		swap preferences subviews, dynamically loads it if necessary
//
//*****************************************************************************

- loadPreference:(const char *)pControl ofClass:aClass buttonMatrix:sender
{
id iPanelController;

   if(!NXGetNamedObject(pControl, NXApp))
		{
		if(![[NXBundle bundleForClass:aClass] getPath:Path forResource:pControl
																ofType:"nib"])
			NXRunAlertPanel(0, "Error getting nib path", 0, 0, 0);
		else
			[NXApp loadNibFile:Path owner:NXApp withNames:YES 
												 fromZone:[self zone]];

		if(!(iPanelController = NXGetNamedObject(pControl, NXApp)))
			NXRunAlertPanel(0, "Error getting named object", 0, 0, 0);
		[swapView addInspector:[iPanelController inspectorPanel] for:sender];
		}
	[swapView inspectorFor:sender];
	[[swapView window] makeKeyAndOrderFront:self];

    return self;
}
//*****************************************************************************
//
//		adjust the size of the scroll view as button cells are added
//
//*****************************************************************************

- resizeScrollViewFor:(int)numberOfButtons
{
NXRect fRect;								// scroll's frame rect
NXSize sSize;								// size of scroll's doc view
NXCoord newWidth;

								// get coordinates for selection button
	[prefScrollView getFrame:&fRect];
	[prefScrollView getContentSize:&sSize];

	fRect.size.width = 60.0;

								// move x origin right as buttons are added
	fRect.origin.x = (NXCoord)(fRect.size.width * numberOfButtons);
								// if origin of new button plus its width
								// exceeds the scroll's docView width
								// resize the scroll's docView 
	newWidth = fRect.origin.x + fRect.size.width + 4.0;
	if(newWidth > sSize.width)
		[[prefScrollView docView] sizeTo:newWidth :sSize.height];

    return self;
}
//*****************************************************************************
//
//		add an inpector and it's trigger button to the preferences panel
//
//*****************************************************************************

- addInspector:(const char *)aName atPosition:(int)pos withSelector:(SEL)aSel
{
NXImage *image;

								// find selector button image
	strncpy(Path, [[NXBundle mainBundle] directory] , MAXPATHLEN);	
	strncat(Path, "/", MAXPATHLEN - strlen(Path));
	strncat(Path, aName, MAXPATHLEN - strlen(Path));
	strncat(Path, ".gkPreference/icon.tiff", MAXPATHLEN - strlen(Path));
	image = [[NXImage alloc] initFromFile:Path];
	if(![image lockFocus])		// chk for valid image
		image = [NXImage findImageNamed:"app"];

								// add a selector button for this inspector
	if([bView cellCount] <= pos)	
		{
		[bView insertColAt:pos];
		[self resizeScrollViewFor:pos];
		}
	[[[[[bView cellAt:0 :pos] setImage:image]
								setTag:pos]
								setTarget:self]
								setAction:aSel];
	[bView display];
	
    return self;
}

@end

