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