Whenever you are writing a script, getting information to and from users is always a very important (and often complicated) part. There needs to be a way to notify the user that something has happened or to ask a user a simple question. Legato provides a basic interface for performing these tasks using message boxes.
The sample script provided in this blog showcases a number of the message boxes available in Legato. In the blog itself, we’ll focus more on how message boxes work and the types of message boxes you can use.
Our Sample Script:
int rc;
int remember;
string text;
int main() {
rc = MessageBox('i', "We are using Legato to create a message box.");
if (rc == IDOK) {
MessageBox('i', "You pressed OK.");
}
if (rc == IDCANCEL) {
MessageBox('i', "You pressed the X.");
}
rc = YesNoRememberBox("Do you want to continue?");
remember = GetLastError();
if (rc == IDNO) {
text = "Okay, we'll stop now!";
if (remember != 0) {
text += " You wanted to remember this.";
}
MessageBox('x', text);
}
if (rc == IDYES) {
text = "Good Answer.";
if (remember != 0) {
text += " You wanted to remember this.";
}
MessageBox(text);
}
if (rc == IDCANCEL) {
MessageBox('x', "Operation cancelled!");
return rc;
}
return ERROR_NONE;
}
The Legato MessageBox SDK function displays a message box. Each message box works the same basic way. The function returns an int value containing an error code or standard Windows ID code, which corresponds to the choice the user made. The most common return codes are standard defines in Legato: IDOK, IDYES, IDNO, and IDCANCEL. These let you quickly and easily determine what the user’s response to your message box was. If the user pressed the “no” button, then the response back will be IDNO. If they closed the box by clicking the X in the upper right corner, the response will be IDCANCEL. If they click a cancel button, the response will also be IDCANCEL.
Depending on the type of message box, you may be able to define a character that represents what icon shows up in the upper left of the box. In the MessageBox function, for example, the first parameter takes either an integer or a character that defines what type of icon you’d like in the top left. The acceptable values are:
|
|
|
|
|
|
Definition |
|
Character |
|
Type |
|
MB_INFO |
|
‘I’ |
|
Information |
|
MB_NONE |
|
‘N’ |
|
None (No Icon) |
|
MB_QUESTION |
|
‘Q’ |
|
Question |
|
MB_STOP |
|
‘S’ |
|
Stop |
|
MB_EXCLAMATION |
|
‘X’ |
|
Exclamation |
You may also want to remember the user’s input to avoid asking them again. Functions that support this will have the suffix “Remember” in their name. For these functions, the value of the checkbox will be stored as the last error. You can access that error by using the GetLastError function. So, to get the value of a checkbox, the code would look like this:
rc = YesNoRememberBox("Do you want to continue?");
remember = GetLastError();
Remember that you need to use the GetLastError function immediately after your message box function call to avoid accidentally overwriting the last error with a subsequent function’s error value. The returned value from the remember boxes also uses the IDYES to signify that the box was checked. To test for it, you need to check if the variable remember was set to IDYES. You can store this value in an INI file using the PutSetting function. Note that this function will not remember if a user cancelled the message box by clicking the X button.
This is a list of all of the types of message box functions offered by Legato:
|
|
|
|
|
|
Function |
|
Supports Icon |
|
Notes |
|
MessageBox |
|
Yes |
|
Basic message box to display info to user. Has OK button. |
|
OKCancelBox |
|
Yes |
|
Displays message. Has OK and Cancel buttons. |
|
YesNoBox |
|
Yes |
|
Displays message. Has Yes and No buttons. |
|
YesNoCancelBox |
|
Yes |
|
This box is programmatically identical to the YesNoBox, except it has an explicit cancel button instead of the user having to press the X to cancel. |
|
YesNoRememberBox |
|
No |
|
Displays message, has Yes and No buttons, and has a “Remember” checkbox. Value from checkbox can be accessed by using the GetLastError function immediately after this function is called. |
|
YesNoCancelRememberBox |
|
No |
|
Programmatically identical to the YesNoRememberBox, but it has an explicit cancel button. |
In summary, message boxes are a very easy way to add some quick UI to your script. Legato is capable of more complicated custom dialog boxes as well, but that is a topic that will be covered in a different blog post.
Steven Horowitz has been working for Novaworks for over five years as a technical expert with a focus on EDGAR HTML and XBRL. Since the creation of the Legato language in 2015, Steven has been developing scripts to improve the GoFiler user experience. He is currently working toward a Bachelor of Sciences in Software Engineering at RIT and MCC. |
Additional Resources
Novaworks’ Legato Resources
Legato Script Developers LinkedIn Group
Primer: An Introduction to Legato