#define __VERSION__ 	"39"
#define __REVISION__ 	"6"
#define __NAME__			"Gallery"
#define __AUTHOR__		"Markus Hillenbrand"

char *V = "$VER: " __NAME__ " " __VERSION__ "." __REVISION__ " (" __DATE__ ")";

#include <GUIC_Classes/all.hpp>

#define COPYRIGHT	"<SMALL> index file created with 'Gallery', (c) 1997 by <A HREF=\"http://www.student.uni-kl.de/~hillenbr\" TARGET=_top>Markus Hillenbrand</A> </SMALL>"
#define FILEFILTER 	"#?.(jpeg|jpg|gif|iff|ilbm|iff24|bmp|pcx|tiff)"

// Defines 
#define MINLINES							3
#define MINCOLUMNS					3	
#define MAXLINES							200
#define MAXCOLUMNS					200	
#define DEFAULTLINES					3
#define DEFAULTCOLUMNS			5
#define MENUFRAMEWIDTH			150
#define MINTHUMBNAILWIDTH 		50
#define MINTHUMBNAILHEIGHT		50
#define MAXTHUMBNAILWIDTH		200
#define MAXTHUMBNAILHEIGHT	200
#define DEFTHUMBNAILWIDTH 		100
#define DEFTHUMBNAILHEIGHT		100
#define MINTABLEBORDERSIZE		0
#define MAXTABLEBORDERSIZE	20
#define DEFTABLEBORDERSIZE		4

GUIC_ListC errorList;
LONG lastDay=0,lastMonth=0,lastYear=0;


class GalleryEntryC : public GUIC_ObjectC
	{
	public:
		GalleryEntryC			(STRPTR f, LONG s, LONG d, LONG m, LONG y) 	{ fileName=f; size=s; day=d; month=m; year=y; }
		LONG		compare	(GUIC_ObjectC &o);
		VOID		print		(VOID) { cout << fileName << endl; }
		STRING 	fileName;
		LONG 	day, month, year;
		LONG 	size;
	protected:
		VOID 		cleanUp	(VOID) {};
	};
	
LONG GalleryEntryC::compare(GUIC_ObjectC &o)
{ 
	// First we must convert the argument to what it really is:
	GalleryEntryC *g = (GalleryEntryC *)&o; 
	
	return Stricmp(this->fileName, g->fileName); // Just compare the two file names	
}

STRING 	makeThumbnail	(GUIC_FileExamineC &file, LONG thumbnailWidth, LONG thumbnailHeight)
{
	STRING fileName = file.getName();

	LONG i = fileName.length();
	while (--i) if (fileName[i] == '.') break;

	STRING thumbName = fileName.left(i);
	thumbName+="_.jpg";

	try
		{
		GUIC_FileExamineC f(thumbName);
		if (f.newer(file)) return thumbName;
		}
	catch (GUIC_SystemX &e) { }

	char args[1024];
	sprintf ( args, "<>NIL: \"%s\" TO \"%s\" FORMAT JPEG QUALITY 90 BOXFIT %ld %ld", (char *)fileName, (char *)thumbName, thumbnailWidth, thumbnailHeight);

	cout << " - creating thumbnail for file '" << fileName << "' ... " << flush;
	BOOL result = GUIC_SystemC::runProgram("GfxCon_68020", 100000, args);
	if (result) cout << "done." << endl; else cout << "error occured." << endl;

	return thumbName;
}
VOID 		createGallery		(GUIC_ListC &fileList, STRPTR pattern, LONG linesInTable, LONG columnsInTable, BOOL picClick, BOOL showSize, BOOL showDate, LONG menuFrameWidth, LONG thumbnailWidth, LONG thumbnailHeight, LONG tableBorderSize, STRPTR baseName, BOOL noFrames)
{
	GalleryEntryC *key = (GalleryEntryC *)fileList.objectAt(1);
	GUIC_FileExamineC firstFile(key->fileName);
	STRING currentDir = firstFile.getPathPart();
	
	ldiv_t d = ldiv ( fileList.length(), columnsInTable );
	LONG lines = d.quot;
	if (d.rem) lines++;
	
	d = ldiv (lines, linesInTable);
	LONG galleries = d.quot;
	if (d.rem) galleries++;

	cout << "Creating " << galleries << " galleries in directory '" << currentDir << "' " << endl;
	
	fileList.sort();
	
	GUIC_FileExamineC directoryName(currentDir);

	if (! noFrames) // Create frames and overview
		{
		// Create the index file with the frames
		STRPTR st = 0;
		GUIC_SystemC::reallocString(&st, baseName, ".html");
		GUIC_FileC gallery(currentDir, st, GUIC_Write);
		
		gallery.write("<TITLE>Gallery '");
		gallery.write(directoryName.getFilePart());
		gallery.writeLn("'</TITLE>");
		gallery.write("<FRAMESET COLS=");
		gallery.write(menuFrameWidth);
		gallery.writeLn(",*>");
		gallery.write("<FRAME NAME=F1 SRC=\"");
		gallery.write(baseName);
		gallery.writeLn("0.html\" MarginHeight=0 MarginWidth=0 Scrolling=\"auto\" FrameBorder=0>");
		gallery.write("<FRAME NAME=F2 SRC=\"");
		gallery.write(baseName);
		gallery.writeLn("1.html\" MarginHeight=0 MarginWidth=0 Scrolling=\"auto\" FrameBorder=0>");
		gallery.writeLn("</FRAMESET>");
	
		// now create the overview over the galleries
		GUIC_SystemC::reallocString(&st, baseName, "0.html");
		GUIC_FileC overview (currentDir, st, GUIC_Write);
	
		overview.writeLn("<HTML>");
		overview.write("<H2 ALIGN=Center>"); 
		overview.write(directoryName.getFilePart());
		overview.writeLn("</H2><BR><UL>");
		for (LONG i=1; i<=galleries; i++)
			{
			overview.write("<LI><A HREF=\"");
			overview.write(baseName);
			overview.write(i);
			overview.write(".html\" TARGET=F2>Gallery ");
			overview.write(i);
			overview.writeLn("</A> <BR>");
			}
		
		overview.write("</UL><BR> <BR> <IMG SRC=internal-gopher-menu> <A HREF=\"../");
		overview.write(baseName);
		overview.writeLn(".html\" TARGET=_top> up </A>");
		overview.writeLn("</HTML>");
		}

	// create each gallery
	LONG count = 0;
	for (LONG i=1; i<=galleries; i++)
		{
		char filename[256];
		sprintf(filename, "%s%ld.html", baseName, i);
		GUIC_FileC indexFile(currentDir, filename, GUIC_Write);

		// write head and title	
		indexFile.writeLn("<HTML>");
		indexFile.writeLn("<HEAD>");
	
		indexFile.write("<TITLE> "); indexFile.write(directoryName.getFilePart());	indexFile.writeLn(" </TITLE>");
		indexFile.writeLn("</HEAD>");
		indexFile.write("<BODY");
		if (pattern)
			{
			indexFile.write(" Background=\"");
			indexFile.write(pattern);
			indexFile.write("\"");
			}
		indexFile.writeLn(">");
		
		indexFile.write("<CENTER><H2>"); 
		indexFile.write(directoryName.getFilePart()); 
		indexFile.write("</H2> (Part ");
		indexFile.write(i);
		indexFile.write(" of ");
		indexFile.write(galleries);
		indexFile.writeLn(")<BR>");

		indexFile.writeLn("<HR>");
		indexFile.write("<TABLE Border=");
		indexFile.write(tableBorderSize);
		indexFile.writeLn(" CellSpacing=4 CellPadding=4>");
		
		LONG boundary = count + columnsInTable*linesInTable;
		if (boundary>fileList.length()) boundary = fileList.length();
		
		for (LONG j=count; j<boundary; j++)
			{
			key = (GalleryEntryC *)fileList.objectAt(j+1);
			GUIC_FileExamineC file(key->fileName);
			STRING thumbName = "";
			
			// Create the thumbnail now			
			try
				{
				thumbName = makeThumbnail(file, thumbnailWidth, thumbnailHeight);
				GUIC_FileExamineC thumb (thumbName);
				thumbName = thumb.getFilePart();
				}
			catch (GUIC_SystemX &e) { GalleryEntryC *s = new GalleryEntryC(key->fileName,0,0,0,0); errorList.addTail(*s); }

			// Check the date
			GUIC_DateC oldDate(lastDay,lastMonth,lastYear),*fileDate = file.getDate();;
			if (fileDate->greater(oldDate) )
				{
				lastDay   = fileDate->getDay();
				lastMonth = fileDate->getMonth();
				lastYear  = fileDate->getYear();
				}

			// Check if there are enough entries per line in the table
			if (count++ % columnsInTable == 0) indexFile.write("<TR> ");

			// write the picture data
			indexFile.write("<TD> <CENTER>");
			
			if (picClick)
				{
				indexFile.write("<A HREF=\"");
				indexFile.write(file.getFilePart());
				indexFile.write("\"> <IMG SRC=\"");
				indexFile.write(thumbName);
				indexFile.write("\" ALT=Thumbnail BORDER=0> <BR>");
				}
			else
				{
				indexFile.write("<IMG SRC=\"");
				indexFile.write(thumbName);
				indexFile.write("\" ALT=Thumbnail> <BR>");
				indexFile.write("<A HREF=\"");
				indexFile.write(file.getFilePart());
				indexFile.write("\">");
				}
				
			indexFile.write(file.getFilePart());
			indexFile.write(" </A>");
			if (showSize)
				{
				indexFile.write("<BR> Size: ");
				indexFile.write(key->size);
				}
			if (showDate)
				{
				indexFile.write("<BR> Date: ");
				indexFile.write(key->day);
				indexFile.write(".");
				indexFile.write(key->month);
				indexFile.write(".");
				indexFile.write(key->year);
				}
			indexFile.writeLn("</TD>");
			}
			
		indexFile.writeLn("</TABLE>");
		
		if (noFrames && galleries>1) // then create jump points
			{
			indexFile.writeLn("<BR>");
			
			if (i > 1) 
				{
				indexFile.write("<A HREF=\"");
				indexFile.write(baseName);
				indexFile.write(i-1);
				indexFile.writeLn(".html\">[prev] </A>");
				}
			else indexFile.writeLn("[prev] ");
			
			for (LONG gal=1; gal<=galleries; gal++)
				{
				if (i != gal)
					{
					indexFile.write(" <A HREF=\"");
					indexFile.write(baseName);
					indexFile.write(gal);
					indexFile.write(".html\">");
					indexFile.write(gal);
					indexFile.writeLn("</A> ");
					}
				else 
					{
					indexFile.write(" ");
					indexFile.write(gal);
					indexFile.writeLn(" ");
					}
				}
			
			if (i < galleries) 
				{
				indexFile.write("<A HREF=\"");
				indexFile.write(baseName);
				indexFile.write(i+1);
				indexFile.writeLn(".html\"> [next]</A>");
				}
			else indexFile.writeLn("[next]");
			
			indexFile.write("<BR> <IMG SRC=internal-gopher-menu> <A HREF=\"../");
			indexFile.write(baseName);
			indexFile.writeLn(".html\"> up </A>");

			}

		indexFile.writeLn("<HR>");
		indexFile.writeLn(COPYRIGHT);
		indexFile.writeLn("</BODY>");
		indexFile.writeLn("</HTML>");
		}
}
VOID 		createHTML		(GUIC_ListC &fileList, STRPTR pattern, BOOL showDate, LONG tableBorderSize, STRPTR baseName, BOOL noFrames)
{
	GalleryEntryC *key = (GalleryEntryC *)fileList.objectAt(1);
	GUIC_FileExamineC firstFile(key->fileName);

	STRPTR currentDir = firstFile.getPathPart(), st = 0;
	GUIC_SystemC::reallocString(&st, baseName, ".html");
	GUIC_FileC indexFile(currentDir, st, GUIC_Write);
	
	GUIC_FileExamineC directoryName(currentDir);

	STRING titleString = directoryName.getFilePart();
	if (titleString == STRING("") ) titleString = directoryName.getName();

	cout << "Creating file '" << indexFile.getName() << "'" << endl;

	indexFile.writeLn("<HTML>");
	indexFile.writeLn("<HEAD>");
	indexFile.write("<TITLE> "); indexFile.write(titleString); indexFile.writeLn(" </TITLE>");
	indexFile.writeLn("</HEAD>");
	indexFile.write("<BODY");
	if (pattern)
		{
		indexFile.write(" Background=\"");
		indexFile.write(pattern);
		indexFile.write("\"");
		}
	indexFile.writeLn(">");
	indexFile.write("<CENTER><H1>"); indexFile.write(titleString); indexFile.writeLn("</H1>");
	indexFile.writeLn("<HR><BR><BR>");
	
	// Create the table of galleries
	
	indexFile.write("<TABLE Border=");
	indexFile.write(tableBorderSize);
	indexFile.writeLn(" CellSpacing=4 CellPadding=4>");
	indexFile.write("<TR> <TH> <CENTER> <H3> Gallery Name </H3> </TH> <TH> <CENTER> <H3> Entries </H3> </TH>");
	if (showDate) indexFile.write("<TH> <CENTER> <H3> Last Update </H3> </TH>");
	indexFile.writeLn("");
	
	fileList.sort();
	
	for (LONG i=1; i<=fileList.length(); i++)
		{
		key = (GalleryEntryC *)fileList.objectAt(i);
		GUIC_FileExamineC file (key->fileName);

		indexFile.write("<TR> <TD> <CENTER> <A HREF=\"");
		indexFile.write(file.getFilePart());
		indexFile.write("/");
		indexFile.write(baseName);
		
		if (noFrames) indexFile.write("1");
		
		indexFile.write(".html\"");
		indexFile.write("> ");
		indexFile.write(file.getFilePart());
		indexFile.write(" </A> </TD> <TD> <CENTER> ");
		indexFile.write(key->size);
		
		if (showDate)
			{			
			indexFile.write(" </TD> <TD> <CENTER> ");

			if (key->day)
				{
				indexFile.write(key->day);
				indexFile.write(".");
				indexFile.write(key->month);
				indexFile.write(".");
				indexFile.write(key->year);
				}
			else indexFile.write("(Directory)");
			}
			
		indexFile.writeLn(" </TD>");
		}

	indexFile.writeLn("</TABLE>");

	indexFile.write("<BR> <IMG SRC=internal-gopher-menu> <A HREF=\"../");
	indexFile.write(baseName);
	indexFile.writeLn(".html\" TARGET=_top> up </A> <BR>");

	indexFile.writeLn("<HR>");
	indexFile.writeLn(COPYRIGHT);
	indexFile.writeLn("</BODY>");
	indexFile.writeLn("</HTML>");
}
LONG 	scanDir				(STRPTR startDir, STRPTR pattern, LONG linesInTable, LONG columnsInTable, BOOL picClick, BOOL showSize, BOOL showDate, LONG menuFrameWidth, LONG thumbnailWidth, LONG thumbnailHeight, LONG tableBorderSize, STRPTR baseName, BOOL noFrames, BOOL createAll)
{
	GUIC_ListC fileList,dirList;
	GalleryEntryC *key = 0;
	GUIC_FileExamineC *filex = 0;
	LONG entries = 0;

	GUIC_FileExamineC *examine = new GUIC_FileExamineC(startDir);
	if (!examine->isDirectory()) throw GUIC_SystemX("Error in function ScanDir (directory name expected).");
	STRING dirName = examine->getName();
	delete examine; examine=0;

	GUIC_DirectoryExamineC *direx= new GUIC_DirectoryExamineC(dirName);
	BOOL hasSubDirs = direx->hasSubdirectory();
	if (!hasSubDirs) direx->setFilter(FILEFILTER);
		
	try
		{
		while ( (filex = direx->examineNext()) )
			{
			STRING fileName  = filex->getName();
			GUIC_DateC *date = filex->getDate();
			
			if (filex->isDirectory())
				{
				STRING newPattern = "../";
				newPattern += pattern;
				
				LONG entriesInDir  = scanDir(fileName, pattern ? (STRPTR) newPattern : 0, linesInTable, columnsInTable, picClick, showSize, showDate, menuFrameWidth, thumbnailWidth, thumbnailHeight, tableBorderSize, baseName, noFrames, createAll);
				if (entriesInDir)
					{
					key = new GalleryEntryC(fileName, entriesInDir, lastDay, lastMonth, lastYear);
					dirList.addTail(*key);
					entries+=entriesInDir;
					}
				lastDay=0; lastMonth=0; lastYear=0;
				}
			else if (! hasSubDirs && (fileName.right(5) != STRING("_.jpg") ) )
				{
				key = new GalleryEntryC(fileName, filex->getSize(), date->getDay(), date->getMonth(), date->getYear() );
				fileList.addTail(*key);
				entries++;
				}
			else if (createAll && hasSubDirs && (fileName.right(5) != STRING("_.jpg") ) )
				{
				if (filex->getFileType() == GUIC_PictureFile)
					{
					makeThumbnail ( *filex, thumbnailWidth, thumbnailHeight);
					}
				}
			}
		}
	catch (GUIC_SystemX &e) { cerr << "Exception caught while processing directory '" << direx->getName() << "': " << e.getMessage() << endl; }
	delete direx;direx=0; /* Damit der lock auf das Verzeichnis freigegeben wird ! */

	if (dirList.length()) 	createHTML		(dirList,pattern, showDate, tableBorderSize, baseName, noFrames);
	if (fileList.length())	createGallery		(fileList,pattern, linesInTable, columnsInTable, picClick, showSize, showDate, menuFrameWidth, thumbnailWidth, thumbnailHeight, tableBorderSize, baseName, noFrames);

	while (fileList.length()) delete (GalleryEntryC *)fileList.remove(1);
	while ( dirList.length()) delete (GalleryEntryC *) dirList.remove(1);

	return entries;
}


class GalleryWindowC : public GUIC_WindowC
	{
	public:
		GalleryWindowC		(GUIC_ApplicationC *app, GUIC_ScreenC *screen);
		~GalleryWindowC 	(VOID);
		BOOL action			(GUIC_EventC &);
	private:
		GUIC_ButtonC				*start, *quit;
		GUIC_RegisterC			*reg;
		GUIC_FileStringC			*pattern;
		GUIC_PathStringC		*directory;
		GUIC_SlidingIntegerC 	*lines, *columns, *width, *height, *tableBorder;
		GUIC_CheckboxC		*click, *size, *date, *noFrames, *createAll;
		GUIC_IntegerC				*frame;
		GUIC_LabelC				*dirLabel, *pattLabel, *linesLabel, *columnsLabel, *clickLabel, *sizeLabel, *dateLabel, *frameLabel, *widthLabel, *heightLabel, *tableBorderLabel, *baseNameLabel, *noFramesLabel, *createAllLabel;
		GUIC_StringC				*baseName;
	};
	
GalleryWindowC::GalleryWindowC	(GUIC_ApplicationC *app, GUIC_ScreenC *screen) : GUIC_WindowC (-1,-1,48,24)
{
	// Register erstellen
	reg = new GUIC_RegisterC			(1,1,46,19,4);
	
	reg -> setName(1, "Global");
	reg -> setName(2, "Tables");
	reg -> setName(3, "HTML");
	reg -> setName(4, "Thumbnail");

	reg -> setShortcut ('g');
		
	app -> addPrefs("Register", reg);

	add ( reg );


	// Erstes Register (GLOBAL)
	dirLabel				= new GUIC_LabelC			( 0, 4,10,2,"_Directory:");
	pattLabel			= new GUIC_LabelC			( 0, 6,10,2,"_Pattern:");
	baseNameLabel	= new GUIC_LabelC			( 0, 9,30,2,"_Base name for HTML files:");
	directory				= new GUIC_PathStringC	(11, 4,33,2,"");
	pattern				= new GUIC_FileStringC		(11, 6,33,2,"");
	baseName			= new GUIC_StringC			(31, 9,13,2,"index");
	
	pattern		->setFilter(GUIC_PictureFile);

	directory		->setHelp("Directory for the first index file.");
	pattern		->setHelp("Background pattern in HTML pages.");
	baseName	->setHelp("Enter base file name without a suffix."),

	directory		->setShortcut('d');
	pattern		->setShortcut('p');
	baseName	->setShortcut('b');

	app->addPrefs("StartDirectory"		, directory);
	app->addPrefs("BackgroundPattern" , pattern);
	app->addPrefs("BaseName"				, baseName);
	
	reg->add(1, dirLabel);
	reg->add(1, pattLabel);
	reg->add(1, directory);
	reg->add(1, pattern);
	reg->add(1, baseNameLabel);
	reg->add(1, baseName);


	// Zweites Register (TABLES)
	linesLabel				= new GUIC_LabelC				( 0, 0,10,2,"_Lines:");
	columnsLabel			= new GUIC_LabelC				( 0, 2,10,2,"_Columns:");
	sizeLabel				= new GUIC_LabelC				( 0, 5,30,2,"Sh_ow size of picture:");
	dateLabel				= new GUIC_LabelC				( 0, 7,31,2,"Sho_w date of picture:");
	clickLabel				= new GUIC_LabelC				( 0,10,31,2,"_Use thumbnail picture as link:");
	tableBorderLabel	= new GUIC_LabelC				( 0,13,31,2,"Size of table's _border:");
	lines						= new GUIC_SlidingIntegerC	(11, 0,33,2,MINLINES,MAXLINES,DEFAULTLINES);
	columns					= new GUIC_SlidingIntegerC	(11, 2,33,2,MINCOLUMNS,MAXCOLUMNS,DEFAULTCOLUMNS);	
	size						= new GUIC_CheckboxC			(41, 5, 3,2,TRUE);
	date						= new GUIC_CheckboxC			(41, 7, 3,2,TRUE);
	click						= new GUIC_CheckboxC			(41,10, 3,2,FALSE);
	tableBorder				= new GUIC_SlidingIntegerC	(30,13,14,2,MINTABLEBORDERSIZE, MAXTABLEBORDERSIZE, DEFTABLEBORDERSIZE);
	
	lines			->setHelp("The number of lines per HTML table.");
	columns		->setHelp("The number of columns per HTML table.");
	size			->setHelp("Show picture's size in gallery?");
	date			->setHelp("Show picture's date in gallery?");
	click			->setHelp("Click on thumbnails to view picture?");
	tableBorder	->setHelp("Size of the border around the tables.");
	
	lines			->setShortcut('l');
	columns		->setShortcut('c');
	size			->setShortcut('o');
	date			->setShortcut('w');
	click			->setShortcut('u');
	tableBorder	->setShortcut('b');

	reg->add (2, linesLabel );
	reg->add (2, lines );
	reg->add (2, columnsLabel );
	reg->add (2, columns );
	reg->add (2, sizeLabel );
	reg->add (2, size );
	reg->add (2, dateLabel );
	reg->add (2, date );
	reg->add (2, clickLabel );
	reg->add (2, click );
	reg->add (2, tableBorderLabel);
	reg->add (2, tableBorder);
	

	// Drittes Register (HTML)
	noFramesLabel	= new GUIC_LabelC		( 0, 6,35,2,"Don't _create frames:");
	noFrames			= new GUIC_CheckboxC	(41, 6, 3,2,FALSE);
	frameLabel			= new GUIC_LabelC		( 0, 8,35,2,"Width of menu _frame (left frame):");
	frame					= new GUIC_IntegerC		(36, 8,8,2,MENUFRAMEWIDTH);

	noFrames	->setHelp("Check if you don't want to have frames.");
	noFrames	->setShortcut('c');
	
	frame		->setJustification(GUIC_Right);
	frame		->setHelp("No. of pixels for the left hand frame.");
	frame		->setShortcut('f');

	reg->add (3, noFramesLabel);
	reg->add (3, noFrames);
	reg->add (3, frameLabel );
	reg->add (3, frame );
	

	// Viertes Register (THUMBNAIL)
	widthLabel			= new GUIC_LabelC				( 0, 4,26, 2,"Max. _width of thumbnails:");
	heightLabel			= new GUIC_LabelC				( 0, 6,26, 2,"Max. _height of thumbnails:");
	createAllLabel	= new GUIC_LabelC				( 0, 9,40, 2,"Create _thumbnail for all pictures found:");
	width					= new GUIC_SlidingIntegerC	(27, 4,17, 2,MINTHUMBNAILWIDTH,MAXTHUMBNAILWIDTH,DEFTHUMBNAILWIDTH);
	height					= new GUIC_SlidingIntegerC	(27, 6,17, 2,MINTHUMBNAILHEIGHT,MAXTHUMBNAILHEIGHT,DEFTHUMBNAILHEIGHT);
	createAll				= new GUIC_CheckboxC			(41, 9, 3, 2,FALSE);

	width		->setHelp("Maximum width for each thumbnail.");
	height		->setHelp("Maximum height for each thumbnail.");
	createAll	->setHelp("Do it even in directories with subdirs.");
	
	width		->setShortcut('w');
	height		->setShortcut('h');
	createAll ->setShortcut('t');
	
	reg->add (4, widthLabel );
	reg->add (4, width );
	reg->add (4, heightLabel );
	reg->add (4, height );
	reg->add (4, createAllLabel);
	reg->add (4, createAll);


	// Start und Quit Buttons
	start				= new GUIC_ButtonC				( 1,21,15,2,"_Start");
	quit					= new GUIC_ButtonC				(32,21,15,2,"_Quit");
	
	start		->setHelp("Start the creation of the gallery.");
	quit			->setHelp("Quit the program.");
	
	start		->setShortcut('s');
	quit			->setShortcut('q');

	add ( start );
	add ( quit );


	// Prefs für das Fenster 
	app->addPrefs("GalleryWindow"			, this);
	app->addPrefs("NumberOfLines"			, lines);
	app->addPrefs("NumberOfColumns"		, columns);
	app->addPrefs("PictureAsLink"				, click);
	app->addPrefs("TableFrameSize"			, tableBorder);
	app->addPrefs("ShowSize"					, size);
	app->addPrefs("ShowDate"					, date);
	app->addPrefs("NoFrames"					, noFrames);
	app->addPrefs("MenuFrameWidth"		, frame);
	app->addPrefs("ThumbnailWidth"			, width);
	app->addPrefs("ThumbnailHeight"			, height);
		

	setTitle(__NAME__ " - (c) by " __AUTHOR__);
	setHelp(TRUE);
	
	activate();
}
GalleryWindowC::~GalleryWindowC	(VOID)
{
	delete reg;
	delete dirLabel;
	delete directory;
	delete pattLabel;
	delete pattern;
	delete linesLabel;
	delete lines;
	delete columnsLabel;
	delete columns;
	delete clickLabel;
	delete click;
	delete sizeLabel;
	delete size;
	delete dateLabel;
	delete date;
	delete frameLabel;
	delete frame;
	delete widthLabel;
	delete width;
	delete heightLabel;
	delete height;
	delete tableBorderLabel;
	delete tableBorder;
	delete start;
	delete quit;
	delete createAllLabel;
	delete createAll;
}

BOOL GalleryWindowC::action(GUIC_EventC &e)
{
	switch (e.id)
		{
		case GUIC_GadgetEvent:
			if (e.gadget == (GUIC_GadgetC *)start)
				{
				if (! strcmp(directory->get(), "") )
					{
					GUIC_ErrorC e("You must at least enter a directory name!");
					e.request(this);
					}
				else 
					{
					GUIC_SystemC::openConsole();
					sleep();
					activate();
					try 
						{
						scanDir (directory->get(), pattern->get(), lines->get(), columns->get(), click->get(), size->get(), date->get(), frame->get(), width->get(), height->get(), tableBorder->get(), baseName->get(), noFrames->get(), createAll->get() );
						}
					catch (GUIC_Exception &x) { GUIC_ErrorC e("Exception caught:", x.getMessage() ); e.request(this); }
					waken();
					}
				}
			else if (e.gadget == (GUIC_GadgetC *)quit) 
				{
				dispose();
				}
			return TRUE;
			break;
		case GUIC_OpenWindow:
			return TRUE;
			break;
		case GUIC_CloseWindow: 
			return FALSE;
			break;
		default:
			cerr << "Got an event: " << e.id << endl;
		}
	
	return FALSE;
}



VOID		workbench			(VOID)
{
	GUIC_ApplicationC	app("Gallery");

	app.setAuthor		(__AUTHOR__);
	app.setVersion	(__VERSION__);
	app.setRevision	(__REVISION__);
	app.setDate		(__DATE__);
	app.setTime		(__TIME__);
	app.setInitializer	(TRUE);

	GUIC_ScreenC			screen;
	GalleryWindowC 	window(&app, &screen);
	
	screen.add(window);
	app.add(screen);

	app.start();

	BOOL running = TRUE;
	GUIC_EventC *event = 0;

	LONG count = 0;
	
	while (running && (event = app.wait()) )
		{
		// As we have only one window, it is not neccessary to check for the window id
		if (event->id == GUIC_CloseWindow) 
			{
			GUIC_MessageC	message	("Do you really want to quit ?", "Yes|No");
			if (1 == message.request(*event->window)) running = FALSE;
			}
		}

	app.stop();
}
VOID 		shell					(VOID)
{
	LONG result[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
	STRPTR templ = "STARTDIR/A,LINES/N,COLUMNS/N,BGPATTERN,PICCLICK/S,NOSIZE/S,NODATE/S,MENUFRAMEWIDTH/N,MAXTHUMBNAILWIDTH/N,MAXTHUMBNAILHEIGHT/N,TABLEBORDERSIZE/N,BASENAME,NOFRAMES/S,CREATEALLTHUMBNAILS/S";
	GUIC_ProgramArgsC args;
	if (! args.fit(templ, result))
		{
		workbench();
		return;
		}
	
	LONG lines = result[1] ? *(LONG *)result[1] : DEFAULTLINES;
	if (lines < MINLINES) 		lines=MINLINES;
	if (lines > MAXLINES)	lines=MAXLINES;
	
	LONG columns = result[2] ? *(LONG *)result[2] : DEFAULTCOLUMNS;
	if (columns < MINCOLUMNS) 	columns=MINCOLUMNS;
	if (columns > MAXCOLUMNS)	columns=MAXCOLUMNS;
	
	BOOL picclick		= result[4];
	BOOL showSize	= ! result[5];
	BOOL showDate	= ! result[6];
	
	LONG menuFrameWidth = result[7] ? *(LONG *)result[7] : MENUFRAMEWIDTH;
	
	LONG thumbnailWidth = result[8] ? *(LONG *)result[8] : DEFTHUMBNAILWIDTH;
	if (thumbnailWidth < MINTHUMBNAILWIDTH) 	thumbnailWidth=MINTHUMBNAILWIDTH;
	if (thumbnailWidth > MAXTHUMBNAILWIDTH)	thumbnailWidth=MAXTHUMBNAILWIDTH;

	LONG thumbnailHeight = result[9] ? *(LONG *)result[9] : DEFTHUMBNAILHEIGHT;
	if (thumbnailHeight < MINTHUMBNAILHEIGHT) 	thumbnailHeight=MINTHUMBNAILHEIGHT;
	if (thumbnailHeight > MAXTHUMBNAILHEIGHT)	thumbnailHeight=MAXTHUMBNAILHEIGHT;
	
	LONG tableBorderSize = result[10] ? *(LONG *)result[10] : DEFTABLEBORDERSIZE;
	if (tableBorderSize < MINTABLEBORDERSIZE) 	tableBorderSize=MINTABLEBORDERSIZE;
	if (tableBorderSize > MAXTABLEBORDERSIZE)	tableBorderSize=MAXTABLEBORDERSIZE;
	
	STRPTR baseName = result[11] ? (STRPTR) result[11] : "index";
	
	BOOL noFrames 	= result[12];
	BOOL createAll		= result[13];
	
	scanDir((STRPTR)result[0], (STRPTR)result[3], lines, columns, picclick, showSize, showDate, menuFrameWidth, thumbnailWidth, thumbnailHeight, tableBorderSize, baseName, noFrames, createAll); 

	if (errorList.length())
		{
		cerr << "The following files could not be converted to a thumbnail:" << endl;
		while (errorList.length())
			{
			GalleryEntryC *s = (GalleryEntryC *)errorList.remove(1);
			cerr << "- " << s->fileName << endl;
			delete s;
			}
		cerr << "Please check them and run 'Gallery' again." << endl;
		}
}

void 		main					(int argc, char **argv)
{
	GUIC_SystemC::checkStackSize(50000);
	if (GUIC_SystemC::runFromShell() ) shell(); else workbench();
}

