A GUI toolkit would be considerably less useful if it did not feature a way to let the user access a local file system. Therefore, most GUI toolkits contain dialogs for file selection, and the Two-Layered GUI Toolkit is no exception in this respect: Its bindings wrap the dialog boxes for file and directory selection, where possible, and otherwise supply custom replacements of these dialogs.
This part of the tutorial will be using settings types and Gtk# types, so the following using
directives should be added right away:
using TwoLayeredGUI.FileSystem; using TwoLayeredGUI.Gtk.FileSystem;
To configure a file dialog, the TwoLayeredGUI.FileSystem.FileDialogSettings
class has to be used. For a start, we will simply create a new instance with default settings. Then, the dialog is shown with the TwoLayeredGUI.Gtk.FileSystem.FileDialog
class. To select a single file, the SelectFile
method is invoked:
FileDialogSettings settings = new FileDialogSettings(); string fn = FileDialog.SelectFile(settings); if (fn != null) { MessageBoxSettings msgBox = new MessageBoxSettings(fn); MessageBox.Show(msgBox); }
SelectFile
returns the selected file name as a string (or null
, if the user closed the dialog with Cancel).
As you may have noticed, the above code displays a dialog box for opening files. For saving files, the dialog mode has to be adapted:
FileDialogSettings settings = new FileDialogSettings(); settings.Mode = FileDialogMode.Save; string fn = FileDialog.SelectFile(settings); if (fn != null) { MessageBoxSettings msgBox = new MessageBoxSettings(fn); MessageBox.Show(msgBox); }
So far, files in the file views of the dialog boxes are not yet filtered in any way. Luckily, the FileDialogSettings
class has a Filters
property. As the structure of the file filters is a bit complicated, it will be outlined here:
FileDialogSettings.Filters
contains FileFilter
instances. In the file selection dialog box, only one filter from this list can be active at a given time.FileFilter
stores a human-readable name and a list of patterns. The human-readable name will be shown to the user in the filter selection list of the dialog box, whereas the pattern list will be used to perform the actual filtering.FileNamePattern
consists of various parts and represents a single allowable pattern for file names that are still visible when the containing file filter is active.FileNamePatternPart
either represents a literal part of the pattern or a wildcard.As an example, a file filter named Image files that should only display JPEG and PNG files would be created as follows:
var imgFilter = new TwoLayeredGUI.FileSystem.FileFilter("Image files"); imgFilter.Patterns.Add(new FileNamePattern(new FileNamePatternWildcardPart(true), new FileNamePatternLiteralPart(".jpg"))); imgFilter.Patterns.Add(new FileNamePattern(new FileNamePatternWildcardPart(true), new FileNamePatternLiteralPart(".jpeg"))); imgFilter.Patterns.Add(new FileNamePattern(new FileNamePatternWildcardPart(true), new FileNamePatternLiteralPart(".png")));
That is, the single patterns are combined from a wildcard (the true
argument indicates that the wildcard should match any number of characters rather than exactly one) and a literal part (containing the dot and the file extension).
This filter could then be added to a FileDialogSettings
instance:
settings.Filters.Add(imgFilter); settings.Filters.Add(TwoLayeredGUI.FileSystem.FileFilter.AllFilesFilter);
Note that we also add a filter for *.*
, which is provided by the static AllFilesFilter
property.
All of the above example dialogs were only suitable for selecting one file at a time. However, FileDialog
also provides a SelectFiles
method that allows for the selection of several file names:
FileDialogSettings settings = new FileDialogSettings(); string[] fn = FileDialog.SelectFiles(settings); if (fn != null) { MessageBoxSettings msgBox = new MessageBoxSettings(fn.Length.ToString()); MessageBox.Show(msgBox); }
This also works for save file dialogs. The Two-Layered GUI Toolkit ensures that there is a reasonable way of selecting several files, and if a native GUI toolkit does not support this, an extra dialog box with a files list will be displayed.
Beside the file selection dialog, the aforementioned namespaces also provide a directory selection dialog. It does not have many configuration options, though, and hence is pretty straightforward to use:
DirectoryDialogSettings settings = new DirectoryDialogSettings(); string dn = DirectoryDialog.SelectDirectory(settings); if (dn != null) { MessageBoxSettings msgBox = new MessageBoxSettings(dn); MessageBox.Show(msgBox); }
Wiki: Help-Tutorial-SettingsObjects-Other
Wiki: Help-Tutorial-SettingsObjects-WizardFramework
Wiki: Help-Tutorial-SettingsObjects