More scripts: Dialog
Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
## xmldlg.sml
## Sample script that creates a test dialog named
## "This is a test" from an dialog specification in an
## external XML file "test.xml". The dialog has several
## tabbed panels with examples of various available controls.
## The script also demonstrates methods to set control values
## from the script and to read the dialog settings set by the
## user. Values for the dialog controls are printed to the
## Console Window as an example.
## Most class variables are declared as needed in the script to
## clarify their relevance to the remaining code.
## Variable declarations
numeric err, ret;
string xmlfile$;
class GUI_DLG dlg;
### Define test callback function #########
func TestFunc() {
PopupMessage("I Said Ignore it!");
}
### Define callback procedure to retrieve dialog settings ######
proc GetDlgValues() {
# Here are four ways to get settings out of the dialog. The extracted values are printed
# to the console window as an example.
# 1 -- Use the GetValues() class method in GUI_DLG to get all of the control settings at
# at once. They are returned to a previously-declared instance of class GUIFORMDATA.
# Use GetValue...() methods in that class to read the values as needed.
printf("Method 1...\n");
class GUI_FORMDATA data;
data = dlg.GetValues();
printf(" RadioGroup value = %s\n", data.GetValueStr("radiogroup") );
printf(" FirstName: %s\n", data.GetValueStr("fname") );
printf(" Togglebutton numeric value = %d\n", data.GetValueNum("tbutton") );
printf(" Togglebutton string value = %s\n\n", data.GetValueStr("tbutton") );
# 2 -- Use GetCtrlValueNum() and GetCtrlValueStr() class methods in GUI_DLG to ask the dialog
# for each control value individually as needed. No GUI_FORMDATA class instance is required.
# Less efficient than #1 if multiple control values are needed. Internally it calls
# dlg.GetValues(), pulls out the one value asked for, and discards the rest.
printf("Method2...\n");
printf(" RadioGroup value = %s\n", dlg.GetCtrlValueStr("radiogroup") );
printf(" FirstName: %s\n", dlg.GetCtrlValueStr("fname") );
printf(" Togglebutton numeric value = %d\n", dlg.GetCtrlValueNum("tbutton") );
printf(" Togglebutton string value = %s\n\n", dlg.GetCtrlValueStr("tbutton") );
# 3 -- Use the GetCtrlByID() method in GUI_DLG to get the control handle for a control class,
# then a GetValue...() method in the individual GUI_CTRL_... class to read the control
# value individually as needed.
printf("Method3...\n");
class GUI_CTRL_EDIT_STRING fname;
class GUI_FORM_RADIOGROUP radio;
class GUI_CTRL_TOGGLEBUTTON tbutton;
fname = dlg.GetCtrlByID("fname");
radio = dlg.GetCtrlByID("radiogroup");
tbutton = dlg.GetCtrlByID("tbutton");
printf(" RadioGroup value = %s\n", radio.GetSelected() );
printf(" FirstName: %s\n", fname.GetValue() );
printf(" Togglebutton numeric value = %d\n", tbutton.GetValueNum() );
printf(" Togglebutton string value = %s\n\n", tbutton.GetValueStr() );
# 4 -- A more compact (but perhaps less clear) version of method 3. Methods to get the control
# handle and its value are strung together, eliminating the need to declare the control handle
# class variable.
printf("Method 4...\n");
printf(" RadioGroup value = %s\n", dlg.GetCtrlByID("radiogroup").GetValueStr() );
printf(" FirstName: %s\n", dlg.GetCtrlByID("fname").GetValueStr() );
printf(" Togglebutton numeric value = %d\n", dlg.GetCtrlByID("tbutton").GetValueNum() );
printf(" Togglebutton string value = %s\n\n", dlg.GetCtrlByID("tbutton").GetValueStr() );
# NOTE: if the control value must be accessed in several places, store it as a variable for
# reuse. Values read from GUIFORMDATA or the dialog would be stored as numeric or string
# variables.
} # end GetDlgValues()
############# Main script ##################
clear(); # clear the console window
class XMLDOC doc;
xmlfile$ = _context.ScriptDir + "/test.xml"; # read and parse the dialog specification
err = doc.Read(xmlfile$);
if (err < 0) {
PopupError(err); # Popup an error dialog. "Details" button will say what's wrong.
Exit();
}
class XMLNODE dlgnode;
dlgnode = doc.GetElementByID("test"); # get the dialog element from the parsed XML
if (dlgnode == 0) {
PopupMessage("Could not find dialog node in XML document");
Exit();
}
dlg.SetXMLNode(dlgnode); # set the XML dialog element as the source for the dialog class
dlg.SetCtrlValueStr("fname", "Fred"); # set value for edittext control
dlg.SetCtrlValueStr("radiogroup", "button2"); # set value for radiogroup control
ret = dlg.DoModal(); # open as modal dialog
# Note: ret will be -1 if user hit cancel, 0 for OK
printf("DoModal() returned %d\n", ret);