#include ONCE "window9.bi"

dim shared as GSList ptr pListFileNames

dim shared as Long iFlagNextSelectedFilename

function NextSelectedFilename() as string export
	
	static as GSList ptr node
	
	dim as string sRet
	
	if iFlagNextSelectedFilename = 0 then node = pListFileNames
	
	iFlagNextSelectedFilename = 1
	
	if node then
		
		sRet = *cast(zstring ptr , node->data)
	
		node = node->next		

	EndIf
	
	return sRet

End Function

function OpenFileRequester(sTitle as string , sCurDir as string, sPattern as string = "All files (*.*)"+Chr(0)+"*.*"+Chr(0), iFlag as Long = 0, sTemplateFileName as string = "",  hParentWin as HWND = 0) as string export

	dim as GtkWidget ptr dialog
	
	dim as GtkFileChooser ptr chooser
	
	dim as Long iAction = GTK_FILE_CHOOSER_ACTION_OPEN
	
	dim as gint res
	
	if iFlag and OFN_DIRECTORY then
	
		iAction = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER

	EndIf
	
	dialog = gtk_file_chooser_dialog_new(sTitle, cast(any ptr, hParentWin) , iAction, "Cancel",_
									   GTK_RESPONSE_CANCEL, "OK", GTK_RESPONSE_ACCEPT, NULL)
									   
	chooser = GTK_FILE_CHOOSER(dialog)
	
	if sCurDir <> "" then

		gtk_file_chooser_set_current_folder(chooser , sCurDir)

	EndIf									   
	
	if iFlag and OFN_ALLOWMULTISELECT then
	
		gtk_file_chooser_set_select_multiple(chooser , 1)	

	EndIf
	
	SetFilterRequester( sPattern , chooser)
	
	res = gtk_dialog_run(GTK_DIALOG(dialog))
	
	if res = GTK_RESPONSE_ACCEPT then
	
		if iFlag and OFN_ALLOWMULTISELECT then

			if pListFileNames then
		
				dim as GSList ptr node
				
				node = pListFileNames
				
				while node
					
					g_free(node->data)
				
					node = node->next

				Wend
				
				g_slist_free(pListFileNames)			

			EndIf	

			pListFileNames = gtk_file_chooser_get_filenames(chooser)
			
			dim as string sRet
			
			sRet = *cast(zstring ptr , pListFileNames->data)
			
			iFlagNextSelectedFilename = 0
			
			gtk_widget_destroy(dialog)
			
			return GetPathPart(sRet)
	
		else
		
			dim as zstring ptr psRet = gtk_file_chooser_get_filename (chooser)
			
			dim as string sRet = *psRet
			
			g_free (psRet)	
			
			gtk_widget_destroy(dialog)
			
			return sRet

		EndIf

	EndIf
	
	gtk_widget_destroy(dialog)

End Function