some people put the best outside
some people keep the best inside
some people can't stand up strong
some people won't wait for long
B.Marley "Survival"
One of the standard window types in the Windows GUI is dialog boxes. They are used for entering user information. For example look at the standard for the Windows'95 File Open dialog box:
Figure 5.1 The Microsoft Windows 95 standard File Open dialog.
You can see the window with controls. The idea of dialogs is collecting all elements enabling User to input data in one window - not to overload other windows.
The dialog box above consists of three static texts (Look in, File Name, Files of type), two combo-boxes (with Desktop icon and Bitmap files), one listbox (With four icons), four picture buttons, two text buttons and one edit entry.
One of the controls is called active. All pressing keys are related to the active child. To change active child you can either click the mouse on the target or jump to it by using the buttons Tab (jump forward) or Shift-Tab (jump back).
Another aspect of dialog box functionality is its modality. If dialog box is modal then neither of all other objects outside the modal dialog can become active till the dialog has been closed. These boxes are used in case of important data entering, for example at Open File.
A modeless dialog is used in case of parallel work of the dialog box with other parts of an application. For example dialog Find in MS Winword: you can be entering some new text in your document and simultaneously be searching for a pattern.
Dialog programming is based on the "black box" idea: you prepare a dialog box, fill it with some controls and then simply redefine features set_data/get_data forgetting about what's inside. These features are called before a dialog appearing on the screen and after its closing. That's all what you need to initialize them and obtain the results of their work.
Before you run a dialog you should decide if it is modal or not. To make it modal you must call feature execute from class GROUP:
ret_code := my_window.execute(stupid_dialog)
If you want to have a modeless dialog box, you should simply add it as a child to something visible - it will be enough.
The short form of class DIALOG:
class DIALOG inherit WINDOW creation make feature {ANY} make ( title : STRING, r : RECT ) -- usual creation procedure set_resizability ( flag : BOOLEAN ) -- It is possible to specify if the dialog will be resizable -- or not ( default state ) is_modal : BOOLEAN; -- This is a way to find out if the dialog is -- executed as modal. is_resizable : BOOLEAN; -- This attribute show resizability of dialog box -- default is False next -- set next dialog child active previous -- set previous dialog child active activate_child ( c : TILE ) -- you can directly set active child terminate (code : INTEGER) -- feature to terminate window. parameter 'code' is return code -- which returned to execute feature for modal dialogs set_data -- This feature is provided for setting initial values for -- controls. Also here you _must_ specify default button and -- optionally specify cancel button. Usually you must redefine it get_data(code : INTEGER) -- most important function. Called before physically destroying -- of object. Usually redefined by User. TO moment of calling it -- all childs still alive, -- so you can take all necessary information from them, such as -- text from edit entry, state of check -- buttons, selection from listbox etc. -- parameter code used from function terminate, -- which invokes get_data set_default_button ( b : BUTTON ) -- Setting default button is necessary in every dialog. -- Dialog must have one and only one default button. -- When User presses ENTER key in dialog, a dialog feature -- "ON_CLICKED" is called with parameter equal to default -- button. set_cancel_button ( b : BUTTON ) -- Setting cancel button is optional. When User presses -- ESC key, a dialog feature "ON_CLICKED" is called with -- parameter equal to cancel button. -- If you don't set cancel button it will not be possible -- to get rid of dialog by pressing ESC. end -- class DIALOG
Each platform has its own so called predefined dialog boxes to unify the interface. For example you can use the Windows standard File Open dialog box in your application. GRAPE also supports some predefined dialog boxes. Below we give you only short descriptions of them. In fact they are very similar to each other.
Class FILE_DIALOG supports common dialogs for Open/Save (of files). This is the most popular dialog box and you can see it in almost any application. FILE_DIALOG supports the Open, Save and Open Multiply modes. It is available for User with the following creation features:
make_save ( dialog_title : STRING, file_filters : ARRAY[STRING]) make_open ( dialog_title : STRING, file_filters : ARRAY[STRING] ) make_open_multiple ( dialog_title : STRING, file_filters : ARRAY[STRING])
The first argument is a dialog title ("Save"/"Open" if Void) and the second is an array of filters - the DOS wildcard patterns. For example "All Files (*.*)" or "C/C++ files (*.c;*.cpp;*.h)". Braces are strictly recommended in the filters - the patterns are taken (extracted) from them.
Other settings are as follows:
set_default_path ( def_path, def_file : STRING ) set_default_filter ( filter_index : INTEGER )
If you want to access filters you can do it:
filters : ARRAY [ STRING ] default_filter : INTEGER default_path : STRING default_file : STRING
Now you should execute the dialog box. The execution returns zero if ESC or Cancel has been pressed. A non-zero value means that a selection has been performed. After that you can obtain the data:
selected_path : STRING selected_files : ARRAY [ STRING ]
For a single selection dialog the full path is saved into the selected_path attribute, for a multiply selection dialog - attribute selected_path contains the directory (of the selected file(s)) and attribute selected_files - its/their name(s).
Class FONT_DIALOG is very simple. Create it with feature make and set the default font with:
set_default_font ( a_font : FONT )
and you can access it with:
default_font : FONT
Execute dialog then and look at the code returned (zero means that Esc key has been pressed) and the font selected:
selected_font : FONT