//=============================================================================
//
//	Copyright (C) 1996-1997 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.3 97/03/10 10:28:49 sunshine Exp $
// $Log:	MiscExporterAccessoryView.M,v $
// Revision 1.3  97/03/10  10:28:49  sunshine
// v113.1: For OpenStep conformance, many 'col' methods rename to 'column'.
// 
// Revision 1.2  97/02/07  13:53:25  sunshine
// v108: Ported to OPENSTEP 4.1 (gamma).
// 
// 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/NSBox.h>
#import	<AppKit/NSButton.h>
#import	<AppKit/NSMatrix.h>
#import	<AppKit/NSPanel.h>
#import	<AppKit/NSPopUpButton.h>
}

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


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

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

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


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


inline static MiscExportFormat name_to_format( NSString* 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
	( NSString* 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( NSString* nm )
    { return name_to_title_mode( nm, MISC_EXPORT_TITLES_ROW_DEFAULT ); }


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



inline static MiscExportGridMode name_to_grid_mode
	( NSString* 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( NSString* nm )
    { return name_to_grid_mode( nm, MISC_EXPORT_GRID_ROW_DEFAULT ); }


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


//-----------------------------------------------------------------------------
// new_popup
//-----------------------------------------------------------------------------
static NSPopUpButton* new_popup( NSString* const* names,
	int first, int last, int init_choice )
    {
    int const n = (last - first) + 1;
    NSPopUpButton* popup = [[NSPopUpButton alloc] init];
    for (int i = 0; i < n; i++)
	[popup addItemWithTitle:names[i]];
    [popup sizeToFit];
    [popup selectItemAtIndex:(init_choice - first)];
    return [popup autorelease];
    }


//-----------------------------------------------------------------------------
// new_box
//-----------------------------------------------------------------------------
static NSBox* new_box( NSPopUpButton* button, NSString* title )
    {
    float const OFFSET = 2;
    float const TITLE_HEIGHT = 12;
    NSRect frame = [button 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;
    NSBox* box = [[NSBox alloc] initWithFrame:frame];
    [box addSubview:button];
    [box setContentViewMargins:NSMakeSize( OFFSET, OFFSET )];
    [box setTitle:title];
    [box sizeToFit];
    return [box autorelease];
    }


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

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

- (MiscExportTitleMode)columnTitleMode;
	{ return name_to_col_title_mode( [colTitlePop titleOfSelectedItem] ); }

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

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

//-----------------------------------------------------------------------------
// -initWithFrame:format:rowTitle:colTitle:rowGrid:colGrid:
//-----------------------------------------------------------------------------
- (id)initWithFrame:(NSRect)rect
    format:(MiscExportFormat)format
    rowTitle:(MiscExportTitleMode)rowTitle
    colTitle:(MiscExportTitleMode)colTitle
    rowGrid:(MiscExportGridMode)rowGrid
    colGrid:(MiscExportGridMode)colGrid
    {
    [super initWithFrame:rect];

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

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

    float const FORMAT_X = 8;
    float const FORMAT_Y = 44;

    float const TITLE_X = 143;
    float const GRID_X = 234;

    float const ROW_Y = 44;
    float const COL_Y = 3;

    float const TOTAL_WIDTH = 316;
    float const TOTAL_HEIGHT = 86;

    [self setFrameSize:NSMakeSize( TOTAL_WIDTH, TOTAL_HEIGHT )];

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

    [formatBox   setFrameOrigin:NSMakePoint( FORMAT_X, FORMAT_Y )];
    [rowTitleBox setFrameOrigin:NSMakePoint( TITLE_X,  ROW_Y    )];
    [colTitleBox setFrameOrigin:NSMakePoint( TITLE_X,  COL_Y    )];
    [rowGridBox  setFrameOrigin:NSMakePoint( GRID_X,   ROW_Y    )];
    [colGridBox  setFrameOrigin:NSMakePoint( GRID_X,   COL_Y    )];

    return self;
    }


//-----------------------------------------------------------------------------
// -dealloc
//-----------------------------------------------------------------------------
- (void)dealloc
    {
    [formatPop   release];
    [rowTitlePop release];
    [colTitlePop release];
    [rowGridPop  release];
    [colGridPop  release];
    [formatBox   release];
    [rowTitleBox release];
    [colTitleBox release];
    [rowGridBox  release];
    [colGridBox  release];
    [super dealloc];
    }

@end
