This document is part of the JEWL library, copyright © John English 2000. The JEWL library is released for public use under the terms of the GNU General Public License, and may be freely copied, distributed, used and modified under the terms of that license. Suggestions, comments, bug reports and modifications should be reported to the author <je@brighton.ac.uk>.
1. Introduction
2. Non-GUI input and output facilities
3. String input
4. Input of standard types
5. Generic input facilities
6. Message boxes
7. File dialogs
JEWL (John English's Window Library) is a collection of Ada packages aimed at novices which enables reasonably sophisticated GUI applications to be built with a minimum of effort. The packages are designed to be platform-independent, but at present the only implementation is for Microsoft Windows. The examples in this document are all based on the Windows implementation.
JEWL.IO is a package which is a more-or-less direct replacement for the standard package Ada.Text_IO and its relations Ada.Integer_Text_IO and Ada.Float_Text_IO, which means that traditional text-based programs can be converted to GUI-based programs with a minimum of effort. JEWL.IO uses GUI dialogs for input, and all input is also echoed to the standard output. This means that the input part of any program can be made to use GUI dialogs, while output will still be written to the text-based standard output. The fact that all input is echoed to the standard output means that the output log will exactly mirror an equivalent program whose input is completely text-based.
JEWL.IO also contains some additional facilities: for example, facilities for generating error messages and other user alerts as message boxes (which are also echoed to the standard output); functions to convert data to strings and to concatenate strings to simplify data output; and generalisations such as the provision of Put_Line procedures for types other than strings.
JEWL.IO provides similar facilities to Ada.Text_IO together with some new features:
In all the dialogs except the user alerts there is a Cancel button which allows input to be aborted. Pressing the Cancel button (or the close button in the dialog's title bar) will in all cases close the dialog and raise an Input_Cancelled exception.
The following sections describe each of these in more detail. In all cases, the names used to describe the parameters are the actual names used in the declarations, so a procedure described as Get(Prompt,Default) can be called like this:
Get ("My prompt", "My default");
or like this:
Get (Prompt => "My prompt", Default => "My default");
or like this:
Get (Default => "My default", Prompt => "My prompt");
Also, the name of each operation in its description is hyperlinked to its declaration in the HTML version of the corresponding package specification, so that you can view the formal specification of any operation by clicking on the link in its description.
Output facilities are dealt with first, as they have the greatest similarity to those provided by Ada.Text_IO. They are common to all the data types handled by JEWL.IO (String, Integer, Float and Boolean; any enumerated type, integral type or floating-point type) as well as the standard type Character. The output operations available are as follows:
There are also some standard type conversion operations:
The length of the result of To_String(Item) will depend on the value of the Item parameter; similarly, the length of S & X will depend on the values of S and X. Such unconstrained strings can be passed to a subprogram which expects an unconstrained string as its parameter (e.g. Put) but can only be assigned to a String variable (or a slice of a string variable) if it is exactly the correct size. For example, this will generate a Constraint_Error exception if the result of To_String(Item) is not exactly 20 characters long:
S : String(1..20) := To_String(Item);
It is often simpler to declare a String variable of the correct size and initialise it using a declaration like this:
declare S : String := To_String(Item); begin -- use the value of S here end;
The bounds of S do not need to be specified; the result of To_String(Item) will be used to determine them.
Some extra input facilities for use with files are provided:
Strings are handled slightly differently to other types, since String is an unconstrained type whose length is variable. The operations available reflect this. They are as follows:
Since the string's length is unknown, the only places where this function can be used is in contexts where an unconstrained string is acceptable. These are as the parameter to another procedure or function:
Put(Get(Prompt,Default));
or in a declaration:
S : String := Get(Prompt,Default);
Both the Prompt and Default parameters are optional, so that the simplest form of using Get is like this:
S : String := Get;
By default, Prompt is "Enter your text:" and Default is the empty string ("", zero characters in length).
For the sake of compatibility with the names of operations in Ada.Text_IO, the last two are both available under different names:
There is also a version of Get which will read a single character from a file:
The standard types handled by JEWL.IO (apart from String, which was dealt with above) are Integer, Float and Boolean. There are no input facilities provided for single characters, as a String or enumerated type would usually be a more suitable choice of input type. If necessary, the input facilities for enumerated types described below can be used with type Character.
For Integer, Float and Boolean, the following input operations are available:
Integer and Float dialogs use the same dialog box as for String, except that the input is checked to make sure it is valid when the OK button is pressed. If it is not valid, an error message will be displayed as a message box (see Error, below) and the dialog will be redisplayed if the "OK" button is pressed in response.
Boolean dialogs have different forms depending on whether a default value is specified. If there is no default, the dialog box contains the specified prompt and three buttons labelled "Yes", "No" and "Cancel". Pressing the "Yes" button returns the value True, "No" returns False, and "Cancel" raises an Input_Cancelled exception as usual.
If there is a default, a checkbox is displayed (labelled with the specified prompt) together with "OK" and "Cancel" buttons. The checkbox is checked initially if the default value is True.
JEWL.IO provides generic subpackages for input and output of enumeration types, integral types and floating-point types. To use these, they must be instantiated with the appropriate type:
package My_Enumerated_IO is new JEWL.IO.Enumeration_IO (My_Enum); package My_Integral_IO is new JEWL_IO.Integer_IO (My_Int); package My_Float_IO is new JEWL_IO.Float_IO (My_Float);
The operations available in these packages are the standard
output operations and the following input operations (the same as described
for the input of standard types, above):
The dialogs for integral and floating-point types use the same dialog box as for String, except that the input is checked to make sure it is valid when the OK button is pressed. If it is not valid, an error message which specifies the allowed range of values will be displayed as a message box (see Error , below) and the dialog will be redisplayed if the "OK" button is pressed in response.
Note that you should avoid providing a use clause for subtypes of Integer or Float, as this will produce an ambiguity between the existing operations for Integer and Float and those provided in the new package. For example, consider what will happen if Integer_IO is instantiated for Natural (a subtype of Integer):
package Natural_IO is new JEWL.IO.Integer_IO(Natural); use Natural_IO;
If Get is called for an Integer or Natural variable, the compiler will be unable to distinguish between the two versions of Get available:
Get(Item); -- is this Get(Item:Integer) or Get(Item:Natural)?
To resolve the ambiguity, you would need to specify the package name in both cases:
JEWL.IO.Get(Item); -- Get(Item:Integer) Natural_IO.Get(Item); -- Get(Item:Natural)
If there is no use clause for Natural_IO, the version of Get for type Natural will still need to be qualified as Natural_IO.Get as in the second case above but Get(Item) without any extra qualification will now be interpreted unambiguously as a reference to the version of Get for type Integer (the first case above).
The dialog for enumeration types has a combobox instead of an edit box, and if a default value has been supplied this will be the value initially displayed in the combobox.. Pressing the button at the right of the combobox will display a pull-down list of the possible values of the enumerated type. Only values displayed in this list can be selected; the value in the combobox cannot be edited directly.
A message box is a dialog which can be used to notify the user of an error or other information, or to ask a yes/no question. There are three operations which display message boxes:
|
![]() |
|
![]() |
|
![]() |
A selection of standard dialogs are provided to open and create files. These display a directory tree and allow you to select or manually type in a filename to be opened or created. In each case a parameter File identifies the file being opened or created, and must be a variable of type File_Type. The procedures which use these dialogs are as follows:
The dialogs will look like this:
Files should be closed when they are no longer needed, as follows: