Scripting Resources for DigitalMicrograph™ |
Example: Progress Bar in Dialog |
|
Function |
An example script which shows how to implement a progress bar in a dialog. This is useful where script processes may take a long time. The use of ValidateView() to force updates of the dialog is also demonstrated. |
Version |
version:20050918, v1.0 |
Author |
D. R. G. Mitchell |
Acknowledgements |
Vincent Hou and Douglas Hague are thanked for useful tips on getting this to work. |
Comments |
|
System Requirements |
Should be compatible with all recent versions of DigitalMicrograph. |
Known Issues |
|
Supported |
Yes |
Included Files |
Main script file. |
Source Code |
// D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam to make this email address work) // v1.0, Sept 2005 // Example script which shows how to implement a progress bar within a dialog. // Essential for processes which take a long time. // version:20050918
// Demo of Progress bar in dialogs
// Thanks to V. Hou/Douglas Hague for tips on use of the progressbar functions // note also use of the ValidateView() method, which forces an update of the dialog // this will also be useful where other dialog elements, such as number fields, require // updating during a process (from within the main thread)
// variables
object dialog_frame taggroup firstbutton, secondbutton number true=1 number false=0
// the class createbuttondialog is of the type user interface frame (UIFrame), and responds to interactions // with the dialog - in this case pressing buttons
class CreateButtonDialog : uiframe {
void button1response(object self) {
//this is the response when the Start button is pressed
self.Setelementisenabled("first", false);// these commands set the button as enabled or not enabled self.Setelementisenabled("second", true);// "second" in this command is the identifier of the button 'secondbutton'
number i=0 // a loop to 'do something' which requires a progress bar
for(i=0; i<1001; i++) { self.validateView() // this forces an update of the dialog dialog_frame.dlgsetprogress("progbar",i/1000) } beep() return
}
void button2response(object self) {
//this is the response when the Clear button is pressed
Beep() self.Setelementisenabled("first",true); self.Setelementisenabled("second",false); dialog_frame.dlgsetprogress("progbar",0)
}
}
// this function creates a button taggroup which returns the taggroup 'box' which is added to // the dialog in the createdialog function.
taggroup MakeButton() {
// Creates a box in the dialog which surrounds the button
taggroup box_items taggroup box=dlgcreatebox(" Buttons ", box_items) box.dlgexternalpadding(5,5) box.dlginternalpadding(25,25)
// Creates the first button
firstButton = DLGCreatePushButton("Start Progress Bar", "button1response") dlgenabled(firstbutton,1) // sets the button as enabled when the dialog is first created dlgidentifier(firstbutton, "first") // identifiers are strings which identify an element, such as a button // they are used to change the enabled/disabled status of the element in the button response functions above firstbutton.dlginternalpadding(5,0)
box_items.dlgaddelement(firstbutton)
// Creates the second button
secondButton = DLGCreatePushButton(" Reset Progress Bar ", "button2response") dlgenabled(secondbutton,0) secondbutton.dlgexternalpadding(0,10) dlgidentifier(secondbutton, "second") box_items.dlgaddelement(secondbutton)
// Creates the progress bar
taggroup progbar taggroup progressb=dlgcreateprogressbar("progbar").dlgfill("X") box_items.dlgaddelement(progressb) return box
}
// This function creates the dialog, drawing togther the parts (buttons etc) which make it up // and alloc 'ing' the dialog with the response, so that one responds to the other. It also // displays the dialog
void CreateDialogExample() {
// Configure the positioning in the top right of the application window
TagGroup position; position = DLGBuildPositionFromApplication() position.TagGroupSetTagAsTagGroup( "Width", DLGBuildAutoSize() ) position.TagGroupSetTagAsTagGroup( "Height", DLGBuildAutoSize() ) position.TagGroupSetTagAsTagGroup( "X", DLGBuildRelativePosition( "Inside", 1 ) ) position.TagGroupSetTagAsTagGroup( "Y", DLGBuildRelativePosition( "Inside", 1 ) )
TagGroup dialog_items; TagGroup dialog = DLGCreateDialog("Example Dialog", dialog_items).dlgposition(position);
dialog_items.dlgaddelement( MakeButton() );
dialog_frame = alloc(CreateButtonDialog).init(dialog) dialog_frame.display("Dialog Template");
}
// calls the above function which puts it all together
createdialogexample() |