Mintoris Forum

Author Topic: File Dialogs by WarSOFT Apps  (Read 2353 times)

Jesse

  • Sr. Member
  • ****
  • Posts: 126
  • If life throws a planet at you, pull your ripcord‼
File Dialogs by WarSOFT Apps
« on: Apr 18, 2011, 05:24 AM »
Here are the 3 file dialogs: Open Dir (it's a dir picker, so you can choose a dir, or path, without having to choose a file), Open File and Save File. There is no Save As. The code that uses this include file will have that logic and can use the Save File dialog.

Note that since these dialogs will pause program execution and wait for user input and intervention, these dialog routines do not return values themselves, like a function would. Instead you pass a starting path and filename to them by reference, and they return the path and filename of the chosen file in those variables.

Each dialog has a title, sent in Title$, which should ideally be no more than the name of your application, but it can be used to send whatever data you like to the dialogs.

Each dialog is sent variables, by reference. Pathname$ should contain the starting, or last used, directory path, and on initial use should be set to "/".  Upon return, Pathname$ will contain the path of the chosen directory or file, or it will be blank if the user cancelled the operation. Filename$ isn't used in the Open Dir dialog and should contain the name of the file only in the Save File dialog. Its content makes no difference to the Open File dialog. Upon return, Filename$ will contain the name of the chosen file or will be blank if cancelled. Note: to derive the fully qualified Filepath, you must use Pathname$ + Filename$, because Pathname$ is only the path, and Filename$ is only the name of the file.

Another point that must be considered, is these dialogs are not aware of whether Rootdir is switched on or not, and it makes no difference operationally, except if it isn't, there will be no access to dir's and files below the /Mintoris.Basic/Data/ dir. Because of this, the first time a dialog is used, Pathname$ should be set to "/" to cause the dialogs to revert to the root directory, whichever one it is. The dialog would error and program execution would be stopped if Pathname$ was set to "/Mintoris.Basic/" and Rootdir was not enabled and set. Later, once a dialog is called, Pathname$ can be left as-is so the next dialog call will place the user in the same directory they were last in. This is good programming.

Again, note that Pathname$ and Filename$ are sent to the dialogs BY REFERENCE, indicated by the '&' preceding them, like &Pathname$ and &Filename$. Title$ is not sent by reference, although nothing bad would happen if were.

One final note: I didn't include masks capabilities, or the ability to filter the files in the list by filetype. On a Windows system, in the Open File dialog, below the place you enter the filename is a list of filetypes like "*.bas|*.txt|*.c|*.cpp|*.html" as an example. Currently the File Dialogs show all files. If there is enough interest in this file, and a request or two, I will include that functionality.

Usage is as follows:

wsFDOpenDir(Title$, &Pathname$)

Since a dir picker only picks directories, there is no filename associated with this dialog. Send the starting directory in Pathname$, which should initially be set to "/". Once it is returned, it will contain the directory chosen by the user, or blank if they cancelled, and it should be left as-is for the starting point for the next dialog.

wsFDOpenFile(Title$, &Pathname$, &Filename$)

Send Pathname$ to this dialog the same as the one above. Filename$ has no meaning on the way to the dialog. Once the dialog returns to your basic program, Pathname$ will contain the dir path to the file that was chosen by the user and Filename$ will contain the name of the chosen file. Together, Pathname$ and Filename$ form the 'fully qualified filepath'. It's ok here too, to leave Pathname$ as-is for the next dialog call.

wsFDSaveFile(Title$, &Pathname$, &Filename$)

Pathname$ works the same as the two above. There are two scenarios used by the Save File dialog: Save and Save As. The Save dialog mode is when a new file hasn't been named and/or saved yet, like an unnamed or untitled file. In this scenario, Filename$ should be cleared to a blank string before calling the dialog. Pathname$ should either be left as-is, or be set to your app's default directory, depending upon the circumstances. The Save As mode is when an existing file is saved, and Filename$ should contain the name of this file. Pathname$ should contain the path of this file. When the dialog returns to your app, Pathname$ and Filename$ will indicate the path and name in which the file was Saved As under, regardless of whether it changed from the original or not.