The OpenFile class implements the pre-defined file-selection dialog to allow the user to choose a file to open. This is a top-level object (i.e. it should not be opened as the child of a window).
To use the dialog, you must first create it using
The style property defines the behavior of the dialog. It is an integer (default 0), as follows:
1 = Allow multi-select
Under Windows and Linux, you can also add one or more of the following style codes (these are ignored under MacOS):
2 = Disable re-sizing 4 = Show 'Open as read-only' control 8 = Check 'Open As Read-Only' 16 = Path must exist 32 = File must exist
Under Windows and Linux, the default property defines the default file extension which will automatically be added if the user enters a filename without an extension. (Not implemented under MacOS).
The filter property sets one or more filters (file extensions or types which will be allowed). It comprises one or more sets of descriptive text, a | character, and a semi-colon delimited list of supported filters. Multiple filters should be separated by a vertical bar. Under Windows and Linux, you specify file extensions as file wildcards. For example, the filter 'Text files|*.txt;*.log|All files|*.*' gives the user the choice of selecting 'Text files' (in which case extensions .txt and .log are accepted), or 'All files' (any extension is accepted).
Under MacOS, you can choose to filter either using file extensions (in which case the syntax is identical to that used for Windows and Linux), or using file types. In the latter case the filters are four-character file types rather than file extensions beginning with the wildcard '*.'. For example: 'Picture files|JPEG;PICT' would display the description "Picture files" to the user, and allow selection of files of type JPEG and PICT. The file type '****' matches all file types. You can also combine both types of filter; for example, 'Text files|*.txt;TEXT' would match files which either have the extension .txt or which are of type TEXT.
The filterindex property determines which of the filters (in index origin 1) is displayed from the drop-down list.
Displaying the dialog
When you are ready to show the dialog, call the Show method. This displays the modal dialog. The user can then select a directory; if the Cancel button is pressed, the Show method returns 0. If the OK button is pressed, the Show method returns 1. You can then read the file property to retrieve the file selected by the user. If the multi-select bit was on in the style property, this will be a nested vector of file names.
Finally, you will normally call the Delete method to dispose of the dialog. Alternatively, you can keep it for re-use later in your application.
∇DEMO_OpenFile;VERSION;⎕IO;DLG  ⍝ Sample function demonstrating use of the OpenFile object  DLG←'⎕' ⎕NEW 'OpenFile'  ⍝  ⍝ Are we running on Windows/Mac/Linux?  ⍝ The way we specify the file filter differs  ⎕IO←1  VERSION←'⎕' ⎕WI 'version'  :If VERSION≠0  ⍝ Running under Windows or Linux:  ⍝ Set up default file  :If VERSION=1  DLG.file←'c:\temp.txt'  :Else  DLG.file←'/temp.txt'  :EndIf  ⍝  ⍝ Set up filter  DLG.filter←'Text files (*.txt *.log)|*.txt;*.log|APL Workspaces (*.aws)| *.aws'  :Else  ⍝ Running under MacOS:  ⍝ Set up default file  DLG.file←'temp.txt'  ⍝  ⍝ Set up filter  DLG.filter←'Text files|TEXT|APL Workspaces|AXWS'  :EndIf  ⍝  ⍝ Show the dialog. If user presses Cancel, quit  →(0=DLG.Show)/0  ⍝ He pressed OK.  'File selected: ',DLG.file ∇
Copyright © 1996-2010 MicroAPL Ltd