/* Implementation of OpenPanel class
 *
 * Copyright (C)  1994, 1995  The Board of Trustees of  
 * The Leland Stanford Junior University.  All Rights Reserved.
 *
 * Authors: Jeff Kamerer
 *
 * This file is part of an Objective-C class library for a window system
 *
 * OpenPanel.m,v 1.27 1995/04/10 17:54:14 pfkeb Exp
 *
 */

#include "OpenPanel.h"

#include "Application.h"
#include "TextField.h"
#include "NXBrowser.h"
#include <objc/List.h>
#include <stdlib.h>
#include <string.h>

@implementation OpenPanel

- initContent:(const NXRect *)contentRect style:(int)aStyle 
      backing:(int)backingType buttonMask:(int)mask defer:(BOOL)flag
{
    [super initContent:contentRect style:aStyle backing:backingType
	buttonMask:mask defer:flag];
    instancename = "OpenPanel";

    [_titleText setStringValue:"Open"];

    return self;
}

- ok:sender
{
    List 	*aList = nil;
    int		i, num;
    const char 	*formString;
    char 	*aFilename;

    aList = [browser getSelectedCells:aList];
    num = [aList count];
    if ( !num ) {
	return self;
    }

    filenames = malloc((num + 1) * sizeof(char *));
    for (i = 0; i < num; i++) {
    	filenames[i] = malloc((strlen([[aList objectAt:i] stringValue]) + 2) 
                                       *sizeof(char));
    	*(char *)filenames[i] = '\0';
    	strcpy((char *)filenames[i], [[aList objectAt:i] stringValue]);
    }    
    (char *)filenames[num] = NULL;
    [aList free];

/***** keep some old code in here in case someone asks for filename instead of filenames, even though it is not an 'NX' OpenPanel method ***********/

	formString = [form stringValue];
	aFilename = malloc((strlen(directory) + strlen(formString) + 2) * sizeof(char));
	if (formString[0] == '\0') {
		return self;
	}
	strcpy(aFilename, directory);
	strcat(aFilename, "/");
	strcat(aFilename, formString);
	strcpy(filename, aFilename);
	free(aFilename);
/*************************/

    [self performClose:self];
    [NXApp stopModal:NX_OKTAG];
    return self;
}

- allowMultipleFiles:(BOOL)flag
{
    _allowMultipleFiles = flag;
    return self;
}

- (int)runModalForDirectory:(const char *)path file:(const char *)name 
                                  types:(const char *const *)fileTypes
{
  /* need to set resources first, then */
    filetypes = fileTypes;
    return [self runModal];
}

- (int)runModalForTypes:(const char *const *)fileTypes
{
    filetypes = fileTypes;
    return [self runModal];  
}

- (int)runModalForDirectory:(const char *)path file:(const char *)name
{
  /* Needs to set the widget resources, then */
    filetypes = NULL;
    return [self runModal];
}

- (const char *const *)filenames
{
    return filenames;
}

@end

