BBjFileChooser Example 1

For ensureFileNameIsVisible, getApproveButtonText, getSelectedFile, isAcceptAllFileFilterUsed, setAcceptAllFileFilterUsed, setActiveFileFilter, setApproveButtonText, and setSelectedFile

REM Open SYSGUI channel
sg = unt
open(sg)"X0"

REM Get BBj objects
api! = BBjAPI()
sg! = api!.getSysGui()

REM Create main window
mainWindow! = sg!.addWindow(100, 100, 350, 100, "File Chooser Demo", $00010083$, $$)

REM Create labels
srcLabel! = mainWindow!.addStaticText(101, 10, 10, 100, 25, "Source File")
dstLabel! = mainWindow!.addStaticText(102, 10, 50, 100, 25, "Output File")

REM Create text fields
srcFile! = mainWindow!.addEditBox(151, 120, 10, 140, 25, $$)
dstFile! = mainWindow!.addEditBox(152, 120, 50, 140, 25, $$)

REM Button for source file chooser
selSrcButton! = mainWindow!.addButton(201, 270, 10, 70, 25, "Select", $$)

REM Button for destination file chooser
selDstButton! = mainWindow!.addButton(202, 270, 50, 70, 25, "Select", $$)

REM Set callbacks to create the choosers
selSrcButton!.setCallback(api!.ON_BUTTON_PUSH, "SRC_CHOOSER")
selDstButton!.setCallback(api!.ON_BUTTON_PUSH, "DST_CHOOSER")

REM Set close callback on mainWindow!
mainWindow!.setCallback(api!.ON_CLOSE, "DO_CLOSE")

PROCESS_EVENTS

REM Callback for BBjButton openChooserButton!
SRC_CHOOSER:
    REM No special flags
    flags$ = $0000$
    targetFile! = srcFile!
    GOSUB CREATE_FC

    REM Have *.src filter
    fc!.addFileFilter("BBx Source Files", "*.src")

    REM Allow any source file names to be shown
    IF (! fc!.isAcceptAllFileFilterUsed())
        fc!.setAcceptAllFileFilterUsed(1)
    ENDIF

    REM Set default to be just *.src.
    fc!.setActiveFileFilter("BBx Source Files")
    RETURN

REM Callback for BBjButton saveChooserButton!
DST_CHOOSER:
    REM Set as a SAVE dialog.
    flags$ = $0100$
    targetFile! = dstFile!
    GOSUB CREATE_FC

    REM Transform the source file name to an appropriate BBj file name
    dstFile$ = fc!.getSelectedFile()
    IF (LEN(dstFile$) = 0)
        srcFile$ = srcFile!.getText()
        IF (LEN(srcFile$) > 0)
            idx = POS("."=srcFile$, -1)
            IF (idx = 0)
                dstFile$ = srcFile$ + ".bbj"
            ELSE
                dstFile$ = srcFile$(1,idx) + "bbj"
            ENDIF
        ENDIF

        REM Set the default file to be the transformed name.
        fc!.setSelectedFile(dstFile$)
    ENDIF

    REM Add a file filter for just BBx/BBj compiled file names.
    vec! = api!.makeVector()
    vec!.addItem("*.bbx")
    vec!.addItem("*.bbj")
    fc!.addFileFilter("BBx Compiled Files", vec!)

    REM Don't allow selecting alternative file types.
    IF (fc!.isAcceptAllFileFilterUsed())
        fc!.setAcceptAllFileFilterUsed(0)
    ENDIF

    REM This is the only filter, set it to be visible.
    fc!.setActiveFileFilter("BBx Compiled Files")

    REM Ensure that file name is visible.
    fc!.ensureFileNameIsVisible(dstFile$)
    RETURN

REM Utility subroutine to create the appropriate chooser type with flags$
CREATE_FC:
    REM Create new window for the file chooser.
    sg!.setContext(1)
    window! = sg!.addWindow(110, 110, 500, 300, "Select File", $00090002$, $$)

    REM Create new file chooser with appropriate flags
    fc! = window!.addFileChooser(101, 0, 0, 500, 300, DIR(""), flags$)
    IF (fc!.getApproveButtonText() <> "Select")
        fc!.setApproveButtonText("Select")
    ENDIF

    IF (LEN(targetFile!.getText()) <> 0)
        fc!.setSelectedFile(targetFile!.getText())
    ENDIF

    REM Set the callbacks for interaction with the File Chooser.
    window!.setCallback(api!.ON_CLOSE, "DO_DIALOG_CANCEL")
    fc!.setCallback(api!.ON_FILECHOOSER_APPROVE, "DO_APPROVE")
    fc!.setCallback(api!.ON_FILECHOOSER_CANCEL, "DO_CANCEL")
    RETURN

REM Callback for mainWindow! ON_CLOSE
DO_CLOSE:
    RELEASE

REM Callback for FileChooser approve event
DO_APPROVE:
    REM get the selected file and set on the target edit box.
    file$ = fc!.getSelectedFile()

    REM Destroy the window with the file chooser
    window!.destroy()
    targetFile!.setText(file$)

    RETURN

REM Callback for FileChooser cancel event
DO_CANCEL:
    REM Destroy the window with the file chooser
    window!.destroy()
    RETURN

REM Callback for dialog close box
DO_DIALOG_CANCEL:
    fc!.cancelSelection()
    RETURN