Folder Browser DBE

This function implements a folder browser dialog element. Use it in a similar fashion to the builtin fileName() function.

Usage:

DBE foldername = smartFolderBrowser(DB dbParent, string label, bool readOnly)

Source file:

//<  Smart Folder Browser

/*
    S M A R T   D X L   L I M I T E D

    Copyright:          (c) 2008 Smart DXL Limited.

	Description:

	Folder Browser Facility. 

    provides smartFolderBrowser() which may be used in place of fileName()
    when browsing for a folder rather than a file.

    Uses COM to invoke windows folder browser.

	Change History:

	25-NOV-2008		Tony Goodman	

*/

/************************************
	Field and Browse button that are created on the parent dialog box.
*************************************/
DBE dbeBrowse         = null
DBE dbeDirectory      = null

/************************************
	getWindowsFolder
*************************************/
string getWindowsFolder(string &dirPath)
{
	OleAutoObj  objDialog     = null
	OleAutoObj  objFolder     = null
	OleAutoObj  objFolderItem = null
	OleAutoArgs args          = create
	string      folderPath    = ""
	string      res           = ""

	dirPath = ""

	objDialog = oleCreateAutoObject("Shell.Application")

	if (null objDialog)
	{
		return("Failed to create dialog object")
	}

	put(args, 0)                         // always zero
	put(args, "Please select a folder")  // title
	put(args, 0)                         // options
	//put(args, "c:\\temp")              // root folder

	res = oleMethod(objDialog, "BrowseForFolder", args, objFolder)

	if (null objFolder)
	{
		if (res "" != "")
		{
			return("Failed to launch browser: " res)
		}
		else
		{
			return("")
		}
	}

	res = oleGet(objFolder, "Self", objFolderItem)

	if (null objFolderItem)
	{
		return("Failed to get folder item: " res)
	}

	oleGet(objFolderItem, "Path", folderPath)

	dirPath = folderPath

	return(res)
}

/************************************
	doBrowse
*************************************/
void doBrowse(DBE dbe)
{
	string res           = ""
	string directoryName = ""

	res = getWindowsFolder(directoryName)

	if (res == "")
	{
		set(dbeDirectory, directoryName)
	}
	else
	{
		infoBox("Error: " res)
	}
}

/************************************
	smartFolderBrowser
*************************************/
DBE smartFolderBrowser(DB     dbParent,
                     string sLabel,
                     bool   readOnly)
{
    // use default if parameter is null
    if (dbParent == null)
    {
        dbParent = dbExplorer
    }

    // Create Field and Browse button on parent dialog
    dbeDirectory = field(dbParent, sLabel, "", 40, readOnly)
    beside(dbParent)
   	dbeBrowse = button(dbParent, "Browse...", doBrowse)
   	left(dbParent)

    // return handle on directory field dbe
    return(dbeDirectory)
}