// CustomFileDialog.cp
#define CUSTOMFILEDIALOG_SRC

#include "checksys.h"
#include "CustomFileDialog.h"

// fSpec ダイアログで指定したファイルを返す
// fType 保存するファイルの種類を返す。OSTypeにしていないのは、TEXTでも
//　フォーマットの異なるものが存在しうるからである。具体的にはアプリケーションごと
//　に設定するべきである。
Boolean CustomSaveAs(Str255 prompt, Str255 defaultName, StandardFileReply *sfReply, short *fType)
{
	Point				where = {-1,-1};
	SFData				sfUserData;
	UniversalProcPtr	dlgHookUPP;
	
	sfUserData.selType = *fType;
	
	if(isStandardFile())	{
		dlgHookUPP = NewDlgHookYDProc(CustomSaveDlgHook);
		CustomPutFile(prompt, defaultName, sfReply, mySaveAsDialogID,
					where,
					dlgHookUPP,0,
					0,0,&sfUserData);

		if (sfReply->sfGood) {
			*fType = sfUserData.selType;
		}
	}
	return sfReply->sfGood;
}

// fTypeにOpenするファイルの型を返す。
Boolean CustomOpen(UniversalProcPtr fileFilter, int numTypes, SFTypeList typeList, 
				   StandardFileReply *sfReply, short *fType)
{
	Point				where = {-1,-1};
	SFData				sfUserData;
	UniversalProcPtr	dlgHookUPP;
	short activelist[2];

	sfUserData.selType = *fType;
	activelist[0] = 0;
	if(isStandardFile())	{
		dlgHookUPP = NewDlgHookYDProc(CustomOpenDlgHook);
		
		CustomGetFile(fileFilter, numTypes, typeList, sfReply, myOpenDialogID,
					where,
					dlgHookUPP,0,
					0,0, 
					&sfUserData);

		if (sfReply->sfGood) {
			*fType = sfUserData.selType;
		}
	}
	return sfReply->sfGood;
}

pascal Boolean FilterInvisFiles(CInfoPBPtr pb, Ptr myDataPtr)
{
	if((pb->hFileInfo.ioFlFndrInfo.fdFlags) & fInvisible)
		return true;
	else
		return false;
}

pascal short CustomSaveDlgHook(short item, DialogPtr theDlg, void *userData)
{
	SFDataPtr	sfUserData;
	short		itemType;
	Rect		box;
	Handle		popupCntlH;
		
	if (GetWRefCon(theDlg) != sfMainDialogRefCon)
		return item;
	sfUserData = (SFDataPtr)userData;
	switch(item)	{
		case	sfItemOpenButton:	//ファイルタイプを得る
					GetDialogItem(theDlg, sfItemSaveFileType, &itemType, &popupCntlH, &box);
					sfUserData->selType = GetControlValue((ControlHandle)popupCntlH);
					break;
		case	sfItemCancelButton:		break;
		case	sfItemBalloonHelp:		break;
		case	sfItemVolumeUser:		break;
		case	sfItemEjectButton:		break;
		case	sfItemDesktopButton:	break;
		case	sfItemFileListUser:		break;
		case	sfItemPopUpMenuUser:	break;
		case	sfItemFileNameTextEdit:	break;
		case	sfItemNewFolderUser:	break;
		
		case	sfHookFirstCall:		break;
		case	sfHookNullEvent:		break;
		case	sfHookRebuildList:		break;
		case	sfHookFolderPopUp:		break;
		case	sfHookOpenFolder:		break;
		case	sfHookOpenAlias:		break;
		case	sfHookGoToDesktop:		break;
		case	sfHookGoToAliasTarget:	break;
		case	sfHookGoToParent:		break;
		case	sfHookGoToNextDrive:	break;
		case	sfHookGoToPrevDrive:	break;
		case	sfHookChangeSelection:	break;
	}
	return item;
}

pascal short CustomOpenDlgHook(short item,DialogPtr theDlg, void *userData)
{
	SFDataPtr	sfUserData;
	short		itemType;
	Rect		box;
	Handle		popupCntlH;

	if (GetWRefCon(theDlg) != sfMainDialogRefCon)
		return item;
	sfUserData = (SFDataPtr)userData;
	switch(item)	{
		case	sfItemOpenButton:	//ファイルタイプを得る
					GetDialogItem(theDlg, sfItemOpenFileType, &itemType, &popupCntlH, &box);
					sfUserData->selType = GetControlValue((ControlHandle)popupCntlH);
					break;
		case	sfItemCancelButton:		break;
		case	sfItemBalloonHelp:		break;
		case	sfItemVolumeUser:		break;
		case	sfItemEjectButton:		break;
		case	sfItemDesktopButton:	break;
		case	sfItemFileListUser:		break;
		case	sfItemPopUpMenuUser:	break;
		case	sfItemFileNameTextEdit:	break;
		case	sfItemNewFolderUser:	break;
		
		case	sfHookFirstCall:		break;
		case	sfHookNullEvent:		break;
		case	sfHookRebuildList:		break;
		case	sfHookFolderPopUp:		break;
		case	sfHookOpenFolder:		break;
		case	sfHookOpenAlias:		break;
		case	sfHookGoToDesktop:		break;
		case	sfHookGoToAliasTarget:	break;
		case	sfHookGoToParent:		break;
		case	sfHookGoToNextDrive:	break;
		case	sfHookGoToPrevDrive:	break;
		case	sfHookChangeSelection:	break;
	}
	return item;
}



