// SplitMenu implementation
// Copyright   1996 Stefan Schneider

#import "Localization.h"
#import "SplitMenu.h"

@implementation SplitMenu

- (int) mkdirs: (const char *)aPath mode: (int)mode
// make 'path' directory, including subdirectories where necessary
{
	if (access(aPath, F_OK)) {
		char path[MAXPATHLEN + 1], *last_slash;
		int ret = 0;
		
		strcpy(path, aPath);
		last_slash = strrchr(path, '/');
		
		if (last_slash) {
			*last_slash = '\0';
			ret = [self mkdirs:path mode:mode];
			*last_slash = '/';
		}
		
		if (ret == 0)
			ret = mkdir(path, mode);
		
		return ret;
	}
	
	return 0;
}

- (BOOL) splitMenu: (const char *)menuFile
// menuFile must be a ".tiff" file
{
	NXImage *menu_image;
	NXSize menu_size;
	const char *last_slash;
	char path[MAXPATHLEN + 1], *last_dot;
	NXRect rect;
	NXCoord y;
	int i;
	
	// open NXImage on menuFile
	
	if ((menu_image = [[NXImage alloc] initFromFile:menuFile]) == nil) {
		NXLogError(LS(SM, INIT_FAILED), menuFile);
		return NO;
	}
	[menu_image getSize:&menu_size];
	
	// create tmp output path
	
	strcpy(path, LS(SM, TMP_PATH));
	if (last_slash = strrchr(menuFile, '/'))
		strcat(path, last_slash + 1);
	else
		strcat(path, menuFile);
	if (last_dot = strrchr(path, '.'))
		*last_dot = '\0';
	
	if ([self mkdirs:path mode:0775] != 0) {
		NXLogError(LS(SM, CREATE_FAILED), path);
		[menu_image free];
		return NO;
	}
	
	// walk menu cells
	
	rect.origin.x = rect.origin.y = 0;
	rect.size.width = menu_size.width;
	
	for (y = menu_size.height - 23, i = 0; y >= 0; y -= 20, i++) {
		NXImage *cell_image;
		NXStream *stream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
		char file[MAXPATHLEN + 1];
		NXRect irect;
		
		rect.size.height = (i == 0) ? 23 : 21;
		irect = rect;
		irect.origin.y = y;
		
		// copy cell region to an extra NXImage
		
		cell_image = [[NXImage alloc] initSize:&rect.size];
		[cell_image lockFocus];
		[menu_image composite:NX_COPY fromRect:&irect toPoint:&rect.origin];
		
		if (i == 0) {		// top 'black' cell: erase cross button
			NXRect crect;
			crect.origin.x = rect.origin.x + rect.size.width - 19;
			crect.origin.y = rect.origin.y + 5;
			crect.size.width = crect.size.height = 14;
			PSsetgray(NX_BLACK);
			NXRectFill(&crect);
			
		} else {				// normal 'gray' cell: make top line dark gray
			PSsetgray(NX_DKGRAY);
			PSmoveto(rect.origin.x, rect.origin.y + rect.size.height);
			PSrlineto(rect.size.width, 0);
			PSstroke();
		}
		
		[cell_image unlockFocus];
		
		// write that extra NXImage to file
		
		[cell_image writeTIFF:stream allRepresentations:NO usingCompression:NX_TIFF_COMPRESSION_LZW andFactor:0];
		NXFlush(stream);
		
		sprintf(file, LS(SM, CELL_FILE), path, i);
		if (NXSaveToFile(stream, file) != 0)
			NXLogError(LS(SM, WRITE_FAILED), file);
		NXCloseMemory(stream, NX_FREEBUFFER);
		
		[cell_image free];
	}

	// done

	(void)NXRunAlertPanel(LS(SM, SPLIT_TITLE), LS(SM, SPLIT_DONE), LS(SM, OK), NULL, NULL, menuFile, path);
	[menu_image free];
	return YES;
}

- open: sender
{
	static char *filetypes[2] = { "tiff", NULL };
	
	if (open_panel == nil) {
		open_panel = [OpenPanel new];
		[open_panel allowMultipleFiles:NO];
		[open_panel chooseDirectories:NO];
		[open_panel setTitle:LS(SM, SPLIT_TITLE)];
	}
	
	if ([open_panel runModalForDirectory:NXGetDefaultValue([self appName], "OpenPath") file:NULL types:filetypes] == 0)
		return self;
	
	(void)[self splitMenu:[open_panel filename]];
	
	NXWriteDefault([self appName], "OpenPath", [open_panel directory]);
	return self;
}

- (int) openFile: (const char *)fullPath ok: (int *)flag
{
	const char *last_dot = strrchr(fullPath, '.');
		
	if (last_dot && !strcmp(last_dot, ".tiff"))
		*flag = [self splitMenu:fullPath];
	else
		*flag = NO;
	
	return 0;
}

@end
