//=============================================================================
//
//		Copyright (C) 1996 by Paul S. McCarthy and Eric Sunshine.
//				Written by Paul S. McCarthy and Eric Sunshine.
//							All Rights Reserved.
//
//		This notice may not be removed from this source code.
//
//		This object is included in the MiscKit by permission from the authors
//		and its use is governed by the MiscKit license, found in the file
//		"License.rtf" in the MiscKit distribution.	Please refer to that file
//		for a list of all applicable permissions and restrictions.
//
//=============================================================================
//-----------------------------------------------------------------------------
// MiscExporter.M
//
//		Object that exports the contents of an MiscTableScroll in
//		various useful formats.
//
// BUGS:
//		* Enable / disable save controls based on format.
// TODO:
//		* Add more export formats: rtf0, Lotus 1-2-3, Quattro, Excel, etc.
//		* Maybe provide option for <CR><LF> line terminators.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// $Id: MiscExporterAccessoryView.M,v 1.1 96/09/25 11:56:04 zarnuk Exp $
// $Log:		MiscExporterAccessoryView.M,v $
// Revision 1.1	 96/09/25  11:56:04	 zarnuk
// SavePanel accessory view for use by the MiscExporter.
// 
//-----------------------------------------------------------------------------
#import "MiscExporterAccessoryView.h"

extern "Objective-C" {
#import <appkit/Box.h>
#import <appkit/Button.h>
#import <appkit/Matrix.h>
#import <appkit/Panel.h>
#import <appkit/PopUpList.h>
}

extern "C" {
#import <math.h>
#import <string.h>				// strerror()
}


static char const* const MISC_FORMAT_NAME[] =
		{
		"ASCII-Fixed",
		"ASCII-Tab",
		"ASCII-Delimited",
		"dBASE III (.DBF)"
		};

static char const* const MISC_TITLE_MODE_NAME[] =
		{
		"Off",
		"On",
		"Wrap",
		"Truncate"
		};

static char const* const MISC_GRID_MODE_NAME[] =
		{
		"Off",
		"Line",
		"Space"
		};


//-----------------------------------------------------------------------------
// name_to_tag
//-----------------------------------------------------------------------------
static int name_to_tag( char const* nm, char const* const* nm_array,
						int first, int last, int fallback )
	{
	int const n = (last - first) + 1;
	for (int i = 0; i < n; i++)
		if (strcmp( nm_array[i], nm ) == 0)
			return i + first;
	return fallback;
	}


inline static MiscExportFormat name_to_format( char const* nm )
	{
	return (MiscExportFormat)
		name_to_tag( nm, MISC_FORMAT_NAME, MISC_EXPORT_FORMAT_FIRST,
						MISC_EXPORT_FORMAT_LAST, MISC_EXPORT_FORMAT_DEFAULT );
	}


inline static MiscExportTitleMode name_to_title_mode
		( char const* nm, MiscExportTitleMode fallback )
	{
	return (MiscExportTitleMode)
		name_to_tag( nm, MISC_TITLE_MODE_NAME, MISC_EXPORT_TITLES_FIRST,
						MISC_EXPORT_TITLES_LAST, (int) fallback );
	}


inline static MiscExportTitleMode name_to_row_title_mode( char const* nm )
	{ return name_to_title_mode( nm, MISC_EXPORT_TITLES_ROW_DEFAULT ); }


inline static MiscExportTitleMode name_to_col_title_mode( char const* nm )
	{ return name_to_title_mode( nm, MISC_EXPORT_TITLES_COL_DEFAULT ); }



inline static MiscExportGridMode name_to_grid_mode
		( char const* nm, MiscExportGridMode fallback )
	{
	return (MiscExportGridMode)
		name_to_tag( nm, MISC_GRID_MODE_NAME, MISC_EXPORT_GRID_FIRST,
						MISC_EXPORT_GRID_LAST, (int)fallback );
	}


inline static MiscExportGridMode name_to_row_grid_mode( char const* nm )
	{ return name_to_grid_mode( nm, MISC_EXPORT_GRID_ROW_DEFAULT ); }


inline static MiscExportGridMode name_to_col_grid_mode( char const* nm )
	{ return name_to_grid_mode( nm, MISC_EXPORT_GRID_COL_DEFAULT ); }


//-----------------------------------------------------------------------------
// new_popup
//-----------------------------------------------------------------------------
static Button* new_popup( char const* const* names,
		int first, int last, int init_choice )
	{
	int const n = (last - first) + 1;
	PopUpList* popup = [[PopUpList alloc] init];
	for (int i = 0; i < n; i++)
		[popup addItem:names[i]];
	[[popup itemList] selectCellAt:(init_choice - first):0];
	return (Button*) NXCreatePopUpListButton( popup );
	}


//-----------------------------------------------------------------------------
// new_box
//-----------------------------------------------------------------------------
static Box* new_box( Button* button, char const* title )
	{
	NXCoord const OFFSET = 2;
	NXCoord const TITLE_HEIGHT = 12;
	NXRect frame;
	[button getFrame:&frame];
	frame.size.width = floor( frame.size.width + (2*OFFSET) );
	frame.size.height = floor( frame.size.height + (2*OFFSET) + TITLE_HEIGHT);
	frame.origin.x = 0;
	frame.origin.y = 0;
	Box* box = [[Box alloc] initFrame:&frame];
	[box addSubview: button];
	[box setOffsets: OFFSET : OFFSET ];
	[box setTitle:title];
	[box sizeToFit];
	return box;
	}


//=============================================================================
// IMPLEMENTATION
//=============================================================================
@implementation MiscExporterAccessoryView
- (MiscExportFormat) format
		{ return name_to_format( [formatPop title] ); }

- (MiscExportTitleMode) rowTitleMode;
		{ return name_to_row_title_mode( [rowTitlePop title] ); }

- (MiscExportTitleMode) colTitleMode;
		{ return name_to_col_title_mode( [colTitlePop title] ); }

- (MiscExportGridMode) rowGrid;
		{ return name_to_row_grid_mode( [rowGridPop title] ); }

- (MiscExportGridMode) colGrid;
		{ return name_to_col_grid_mode( [colGridPop title] ); }

//-----------------------------------------------------------------------------
// -initFrame:format:rowTitle:colTitle:rowGrid:colGrid:
//-----------------------------------------------------------------------------
- initFrame:(NXRect const*)rect
	format:(MiscExportFormat)format
	rowTitle:(MiscExportTitleMode)rowTitle
	colTitle:(MiscExportTitleMode)colTitle
	rowGrid:(MiscExportGridMode)rowGrid
	colGrid:(MiscExportGridMode)colGrid
	{
	[super initFrame:rect];

	formatPop	= new_popup( MISC_FORMAT_NAME, MISC_EXPORT_FORMAT_FIRST,
						MISC_EXPORT_FORMAT_LAST, format );
	rowTitlePop = new_popup( MISC_TITLE_MODE_NAME, MISC_EXPORT_TITLES_FIRST,
						MISC_EXPORT_TITLES_LAST, rowTitle );
	colTitlePop = new_popup( MISC_TITLE_MODE_NAME, MISC_EXPORT_TITLES_FIRST,
						MISC_EXPORT_TITLES_LAST, colTitle );
	rowGridPop	= new_popup( MISC_GRID_MODE_NAME, MISC_EXPORT_GRID_FIRST,
						MISC_EXPORT_GRID_LAST, rowGrid );
	colGridPop	= new_popup( MISC_GRID_MODE_NAME, MISC_EXPORT_GRID_FIRST,
						MISC_EXPORT_GRID_LAST, colGrid );

	formatBox	= new_box( formatPop, "Format" );
	rowTitleBox = new_box( rowTitlePop, "Row Titles" );
	colTitleBox = new_box( colTitlePop, "Col Titles" );
	rowGridBox	= new_box( rowGridPop, "Col Grid" );
	colGridBox	= new_box( colGridPop, "Col Grid" );
	[rowGridBox setTitle:"Row Grid"];	// "Row Grid" is wider.

	NXCoord const FORMAT_X		= 8;
	NXCoord const FORMAT_Y		= 44;

	NXCoord const TITLE_X		= 143;
	NXCoord const GRID_X		= 234;

	NXCoord const ROW_Y			= 44;
	NXCoord const COL_Y			= 3;

	NXCoord const TOTAL_WIDTH	= 316;
	NXCoord const TOTAL_HEIGHT	= 86;

	[self sizeTo:TOTAL_WIDTH:TOTAL_HEIGHT];

	[self addSubview:formatBox];
	[self addSubview:rowTitleBox];
	[self addSubview:colTitleBox];
	[self addSubview:rowGridBox];
	[self addSubview:colGridBox];

	[formatBox moveTo:FORMAT_X :FORMAT_Y];
	[rowTitleBox moveTo:TITLE_X :ROW_Y];
	[colTitleBox moveTo:TITLE_X :COL_Y];
	[rowGridBox moveTo:GRID_X :ROW_Y];
	[colGridBox moveTo:GRID_X :COL_Y];

	return self;
	}

@end
