import fsys.dlg;
import com.interface.IShellItemArray;
import com.interface.IFileDialog;

namespace  com.interface;

class IFileOpenDialog {
	ctor(){
		this = ..com.interface.IFileDialog(); 
	}; 
	ptr GetResults = "int(ptr &ppenum)";
	ptr GetSelectedItems = "int(ptr &ppsai)";
	getDisplayFilePaths = function(){
		var hr,pItem = owner.GetResults()
		if( hr>=0/*_SUCCEEDED*/){
			var itemArray = ..com.interface(pItem,..com.interface.IShellItemArray )
			..com.Release(pItem);
			
			var hr,count = itemArray.GetCount(0);
			var result = {}
			if(hr>=0/*_SUCCEEDED*/){
				for(i=1;count;1){
					var hr,pItem = itemArray.GetItemAt(i-1);
					if( hr>=0/*_SUCCEEDED*/){
						var item = ..com.interface.IShellItem.query(pItem);
						..com.Release(pItem);
						
						var path = item.getDisplayFilePath();
						..com.Release(item);
						
						if(path){
							..table.push(result,path);
						} 
					}
				}
				
			}
			..com.Release(itemArray);
				
			return result;
		}
	} 
}

IFileOpenDialog.IID = "{d57c7288-d4ad-4768-be02-9d969532d960}"
	
IFileOpenDialog.create = function(){
	return create("{DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7}", IFileOpenDialog);	
}

IFileOpenDialog.dir = function(path,hwndOwner,title,okLabel,clientGuid,multiSel){
	if(_WINXP){
		return ..fsys.dlg.opendir(path,hwndOwner,title);
	}
	
	var opt = 0x20/*_FOS_PICKFOLDERS*/
	if(multiSel) opt = opt | 0x200/*_FOS_ALLOWMULTISELECT*/

	var dlg = IFileOpenDialog.create();
	dlg.SetOptions(opt);
	
	if(title)dlg.SetTitle(title);
	if(okLabel)dlg.SetOkButtonLabel(okLabel);
	if(clientGuid)dlg.setClientGuid(clientGuid);
	
	if(path){ dlg.setFolderPath(path); }
	
	var ret;
	var hr = dlg.Show(hwndOwner[["hwnd"]]:hwndOwner:0);
	if( hr >= 0/*_SUCCEEDED*/){
		
		if(multiSel){
			ret =  dlg.getDisplayFilePaths(); 
		}
		else {
			ret = dlg.getDisplayFilePath();
		}
	}
	
	..com.Release(dlg);
	return ret;
}

/**intellisense(com.interface.IFileOpenDialog)
create() = 创建IFileOpenDialog接口对象\n!IFileOpenDialog.
dir( = 打开目录对话框,XP系统自动降级为 fsys.dlg.opendir
dir(.(path,hwndOwner,title,okLabel,clientGuid,multiSel) = 打开目录对话框,所有参数可省略,\n参数@path 指定打开的目录,\n参数@hwndOwner 指定父窗口,\n参数@title 指定窗口标题\n参数@okLabel 指定确定按钮上的文本,\n参数@clientGuid 指定单独存储对话框状态的GUID,\n参数@multiSel 如果为true，则允许选择多目录并返回数组,\n默认仅能选择单个目录并返回选中的路径,\n取消返回null
end intellisense**/

/**intellisense(!IFileOpenDialog)
Show(__) = 显示对话框,\n参数指定父窗口句柄,例如 winform.hwnd
SetOptions(0x20__) = 设置选项
SetTitle(__) = 设置标题
getDisplayFilePaths() = 返回数组，\n包含用户选择的一个或多个路径
getDisplayFilePath() = 返回用户选择的路径
setFolderPath(__) = 参数@1指定默认目录
end intellisense**/