widget2.sml

  Download

More scripts: Dialog

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# WIDGET2.SML
# Sample script for Building Dialogs in SML.
# Example of setting up a dialog window with
# different types of widgets.
# The "Area Unit Conversion" window accepts 
# numeric input (an input area), uses option 
# menus to allow the user to select input and 
# output area units, and computes an output value.
################################################
# Define class instances used in procedures
################################################
class XmForm win1;
class PromptNum inarea;
class XmLabel outAreaVal;
# Other class instances are defined as needed
# so you can better understand the widget class
# hierarchy.
 
################################################
# Define global variables
################################################
numeric userval = 0;
string inAreaUnit$ = "square meters";
string outAreaUnit$ = "square meters";
################################################
# Define procedures
################################################
# Procedure to calculate and show the new output area value.
proc OnRecalculate() {
	local numeric scale, outarea;
	scale = GetUnitConvArea(inAreaUnit$,outAreaUnit$);
	outarea = userval * scale;
	outAreaVal.LabelString = sprintf("%10.2f %s",outarea,outAreaUnit$);
}
# Procedure called when input area value is changed.
proc OnNewValue() {
	userval = inarea.value;
	outAreaVal.LabelString = "";
}
# Procedure called when input unit type is selected.
proc OnInputUnitChange(class Widget parent, string unit$) {
	inAreaUnit$ = unit$;
	outAreaVal.LabelString = "";
}
# Procedure called when output unit type is selected.
proc OnOutputUnitChange(class Widget parent, string unit$) {
	outAreaUnit$ = unit$;
	outAreaVal.LabelString = "";
}
# Procedure for closing window.
proc OnClose() {
	DialogClose(win1);
	DestroyWidget(win1);
}
###################################################
# Main program
###################################################
# Set up dialog window
win1 = CreateFormDialog("Area Unit Conversion");
win1.MarginHeight = 5;
win1.MarginWidth = 5;
# Create frame that will surround input area prompt
# widget and units menu widget.
class XmFrame inputframe;
inputframe = CreateFrame(win1);
inputframe.MarginHeight = 2;
inputframe.TopWidget = win1;
inputframe.LeftWidget = win1;
inputframe.RightWidget = win1;
inputframe.ShadowType = "SHADOW_ETCHED_IN";
# A frame can contain only one direct child widget.
# So create a form widget within the frame to hold the
# input area prompt and units menu widgets. The size
# of the form and frame will adjust automatically
# to accomodate the two included widgets.
class XmForm inform;
inform = CreateForm(inputframe);
inform.MarginHeight = 4;
inform.MarginWidth = 4;
inform.TopWidget = inputframe;
inform.LeftWidget = inputframe;
inform.RightWidget = inputframe;
# Create input area widget inside form inform
# and register a callback for a change in input value.
inarea = CreatePromptNum(inform,"Enter area value:",15,2.0);
inarea.TopWidget = inform;
inarea.BottomWidget = inform;
inarea.LeftWidget = inform; 
inarea.RightWidget = inarea;
WidgetAddCallback(inarea.ValueChangedCallback,OnNewValue);
# Set up menu for selecting input area unit, inside form inform.
class XmOptionMenu units1;
units1 = CreateUnitOptionMenu(inform,"",OnInputUnitChange,1,0);
units1.TopWidget = inform;
units1.BottomWidget = inform;
units1.LeftWidget = inarea;
units1.LeftOffset = 4;
units1.RightWidget = inform;
# Set up label string for output area unit menu.
class XmLabel outUnits;
outUnits = CreateLabel(win1,"Select output units:");
outUnits.TopWidget = inputframe;
outUnits.TopOffset = 6;
outUnits.LeftWidget = win1;
outUnits.RightWidget = outUnits;		# unattached
# Set up menu for selecting output area unit.
class XmOptionMenu units2;
units2 = CreateUnitOptionMenu(win1,"",OnOutputUnitChange,1,0);
units2.TopWidget = inputframe;
units2.TopOffset = 6;
units2.LeftWidget = outUnits;
units2.LeftOffset = 4;
units2.RightWidget = win1;
# Set up label string for output area label.
class XmLabel outLabel;
outLabel = CreateLabel(win1,"Area in selected units:");
outLabel.TopWidget = units2;
outLabel.TopOffset = 4;
outLabel.LeftWidget = win1;
outLabel.RightWidget = outLabel;		# unattached
# Set up label string for output area value.
outAreaVal = CreateLabel(win1,"");
outAreaVal.TopWidget = units2;
outAreaVal.TopOffset = 4;
outAreaVal.LeftWidget = outLabel;
outAreaVal.RightWidget = outAreaVal;
# Create separator line between output area and buttons.
class XmSeparator sepline;
sepline = CreateHorizontalSeparator(win1);
sepline.TopWidget = outLabel;
sepline.TopOffset = 4;
sepline.LeftWidget = win1;
sepline.RightWidget = win1;
# Create Convert and Close pushbuttons that must be
# items in a button row.
class PushButtonItem convertButton;
class PushButtonItem closeButton;
convertButton = CreatePushButtonItem("Convert", OnRecalculate);
closeButton = CreatePushButtonItem("Close", OnClose);
# Create button row to hold Convert and Close buttons.
# Icon button rows are automatically attached to their
# parent form on the left and right.  Attach this
# button row to the separator line on top.
class XmForm buttonRow;
buttonRow = CreateButtonRow(win1,convertButton,closeButton);
buttonRow.TopWidget = sepline;
# Open dialog window and keep script active
# until the window is closed.
DialogOpen(win1);
DialogWaitForClose(win1);
# End of script.