Creation of custom modal dialogs.
Here you will find examples to use dialog.
Creation of custom modal dialogs.
For creating a dialog you need a dialog specification, which is a JSON string. This string can be created using the functions of this class, so you don't have to manually create and edit it. Each control widget (checkbox, textfield, etc...) may appear multiple times in the specification, but must have a unique ID (string) within it's own type. Afterwards, the dialog specification is handed to dialog::show to show the dialog. After the dialog has closed or an dialog event has been caught in a callback function, control results can be fetched using the r.. functions (rcheckbox, rtextfield, ...). The results can be fetched until the next call to dialog::show.
Dynamic Dialogs
By registering callback functions, you can react on certain dialog events,such as value change, selection change or click on buttons. Values and value lists as well as visibility and status of controls can be changed for the active dialog within these callback functions, thus dynamic dialogs can be implemented.
For details, see documentation of the dialog::listen function or refer to the examples below.
Element IDs
Elements of a dialog are referred by ID.
You can use arbitrary strings as ID, the only limitation is: the prefix %! is reserved for internal use.
Callback functions registered for internal IDs are processed after callback functions registered for specific IDs, e.g. a callback function registered for %!all is executed after a function registered for kOk.
It is recommended using the ID kOk for confirm buttons and kCancel for cancel buttons. In this case, you can determine the dialog result simply by calling dialg::wasconfirmed or dialog::wascancelled (which also covers acepting or rejecting the dialog using the ENTER or ESC key or window close buttons), instead of getting the last clicked button via dialog::rconfirmbutton.
Dialog specifications may be created in comet_pdf and InDesign Server, but dialogs cannot be shown.
static int title(String dialogSpec, char* title)
Set the dialog title which is shown at the top of the dialog. The default is an empty string. Calling this function multiple times on the same dialog specification will overwrite the previous title.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
title | String or char* | - | New dialog title. If available, the title is automatically translated. |
static int size(
String dialogSpec,
float width,
float height)
Set the size of the dialog. Default values are 500x300 pixels. Calling this function multiple times on the same dialog specification will overwrite the previous values.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
width | float | - | Width |
height | float | - | Heigth |
static int label(
String dialogSpec,
char* id,
char* label,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1)
Insert a simple text label.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
label | String or char* | - | Text. The label is automatically translated. |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
static int checkbox(
String dialogSpec,
char* id,
char* label,
int checked,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1)
Insert a checkbox with optional text label.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
label | String or char* | - | Text. The label is automatically translated. |
checked | int | - | Is the box checked? 0 = No, else = Yes |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
static int textfield(
String dialogSpec,
char* id,
char* text,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1)
Insert a single-line textfield.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
text | String or char* | - | Initial text |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
static int multilinetextfield(
String dialogSpec,
char* id,
char* text,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1)
Insert a multi-line textfield.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
text | String or char* | - | Initial text |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
static int multilinetextinfo(
String dialogSpec,
char* id,
char* text,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1)
Insert a multi-line textinfofield.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
text | String or char* | - | Initial text. The text is automatically translated. |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
static int floatfield(
String dialogSpec,
char* id,
float value,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1,
int type = 0)
Insert an input field, which only accepts float numbers.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
value | float | - | Initial value |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
type | int | 0 | Field type 0 : Without type 4 : Unit of measurement (X direction) 8 : Unit of measurement (Y direction) Please note that measurements are always expected in points, regardless of the current unit of measurement. |
static int intfield(
String dialogSpec,
char* id,
int value,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1)
Insert an input field, which only accepts float numbers.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
value | int | - | Initial value |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
static int dropdown(
String dialogSpec,
char* id,
StringList entries,
int index,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1)
Insert a dropdown for selecting values from a prefedined list.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
StringList | entries | - | Possible selection values. The single entries are automatically translated. |
index | int | - | Initially selected index in the entries list |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
static int confirmbutton(
String dialogSpec,
char* id,
char* label,
float x,
float y,
float width,
float height,
int enabled = 1,
int visible = 1)
Insert a button.
To insert the system-specific buttons for closing the dialog (Okay and Cancel) we recommend using the function dialog::dialogbuttons.
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
id | String or char* | - | ID of the element. IDs with the prefix %! are reserved for internal use and must not be used for elements. |
label | String or char* | - | Label. The label is automatically translated. |
x | float | - | X-Position of the element |
y | float | - | Y-Position of the element |
width | float | - | Width of the element |
height | float | - | Heigth of the element |
enabled | int | 1 | Is element enabled (1) or not (0)? |
visible | int | 1 | Is element visible (1) or not (0)? |
static int dialogbuttons(
String dialogSpec,
int cancelEnabled = 1,
int cancelVisible = 1,
int okEnabled = 1,
int okVisible = 1,
char* cancelLabel = 0,
char* okLabel = 0,
int defaultButton = 0)
Insert standard dialog buttons ("Cancel" and "Okay"). Buttons are positioned automatically according to the system UI recommondations. The IDs of these buttons are defined as follows:
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
cancelEnabled | int | 1 | State of the Cancel button 0 : Button disabled 1 : Button enabled |
cancelVisible | int | 1 | Visibility of the Cancel button 0 : Hide button 1 : Show button |
okEnabled | int | 1 | State of the Okay button 0 : Button disabled 1 : Button enabled |
okVisible | int | 1 | Visibility of the Okay button 0 : Hide button 1 : Show button |
cancelLabel | String oder char* | 0 | Label of Cancel button. The label is automatically translated. 0 or "" : "Cancel" |
okLabel | String oder char* | 0 | Label of the Okay button. The label is automatically translated. 0 or "" : "Okay" |
defaultButton | int | 0 | Which button gets the default (Enter key)? 0 : Okay button is default. rconfirmbutton returns the following values:
|
static int show(String dialogSpec)
Show a modal dialog which is defined by a dialog specification. After the dialog has been closed or withing registered callback functions (see dialog::listen), the user inputs can be fetched using the r...functions (rcheckbox, rtextfield, ...).
Name | Type | Default | Description |
Return | int | 0 or ErrorCode | |
dialogSpec | String | - | Dialog specification |
static int rcheckbox(char* id)
Determine if a checkbox of the dialog, which last sent an event (e.g. closing or value change) was checked
Name | Type | Default | Description |
Return | int | 0 = Not checked or error, 1 = checked | |
id | String or char* | - | ID of the element |
static String rtextfield(char* id)
Determine the value of a single line text field of the dialog, which last sent an event (e.g. closing or value change)
Name | Type | Default | Description |
Return | String | Value of the text field. The result is only valid directly after the call. If it is to be used again it must be copied into another string | |
id | String or char* | - | ID of the element |
static int rlabel(char* id)
Determine the value of a label of the dialog, which last sent an event (e.g. closing or value change)
Name | Type | Default | Description |
Return | int | Value of the label. The result is only valid directly after the call. If it is to be used again it must be copied into another string | |
id | String or char* | - | ID of the element |
static int rmultilinetextfield(char* id)
Determine the value of a multi line text field of the dialog, which last sent an event (e.g. closing or value change)
Name | Type | Default | Description |
Return | int | Value of the text field. The result is only valid directly after the call. If it is to be used again it must be copied into another string | |
id | String or char* | - | ID of the element |
static int rmultilinetextinfo(char* id)
Determine the value of a multi line text info field of the dialog, which last sent an event (e.g. closing or value change)
Name | Type | Default | Description |
Return | int | Value of the text field. The result is only valid directly after the call. If it is to be used again it must be copied into another string | |
id | String or char* | - | ID of the element |
static float rfloatfield(char* id)
Determine the value of a float field of the dialog, which last sent an event (e.g. closing or value change)
Name | Type | Default | Description |
Return | int | Value of the float field Please note that measurements are always given in points, regardless of the current unit of measurement. | |
id | String or char* | - | ID of the element |
static int rintfield(char* id)
Determine the value of an int field when the dialog was closed.
Name | Type | Default | Description |
Return | int | Value of the int field | |
id | String or char* | - | ID of the element |
static String rdropdown(char* id, int* oIndex = 0)
Determine the selected value of a dropdown when the dialog was closed.
Name | Type | Default | Description |
Return | String | Value of the dropdown. The result is only valid directly after the call. If it is to be used again it must be copied into another string | |
id | String or char* | - | ID of the element |
oIndex | int * | 0 | Index of the selected element |
static String rconfirmbutton()
Get the ID of the closing button. Determine the id of the button, which was pressed to close the dialog.
Name | Type | Default | Description |
Return | String | ID of the button which was pressed to close the dialog. The result is only valid directly after the call.
If it is to be used again it must be copied into another string "kOk" : Button "Okay" or Return key "kCancel" : Button "Cancel" or Command Period (CMD-.) |
static String rbutton(char* id)
Determine the value of a button of the dialog, which last sent an event (e.g. value changed or dialog has been closed).
Name | Type | Default | Description |
Return | String | value of button. The result is only valid directly after the call. If it is to be used again it must be copied into another string | |
id | String or char* | - | ID of the element |
static int wcheckbox(char* id, int checked)
Set the status (checked or unchecked) of a checkbox. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
checked | int | - | 0: unchecked, 1: checked |
static int wtextfield(char* id, char* value)
Set the value of a single line textfield. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
value | String or char* | - | New value of the element |
static int wlabel(char* id, char* value)
Set the value of a label. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
value | String or char* | - | New value of the element. The string is automatically translated. |
static int wmultilinetextfield(char* id, char* value)
Set the value of a multi line textfield. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
value | String or char* | - | New value of the element |
static int wmultilinetextinfo(char* id, char* value)
Set the value of a multi line textinfofield. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
value | String or char* | - | New value of the element. The string is automatically translated. |
static int wfloatfield(char* id, float value)
Set the value of a float field. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
value | float | - | New value of the element |
static int wintfield(char* id, int value)
Set the value of a int field. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
value | int | - | New value of the element |
static int wbutton(char* id, char* value)
Set the value (label) of a button. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
value | String or char* | - | New value of the element. The string is automatically translated. |
static int wdropdown(
char* id,
StringList entries,
int selectedIndex = -1)
Set entries and selected index of a dropdown. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist, does not match the expected type or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
entries | String or char* | - | New value of the element |
selectedIndex | int | -1 | Selected index |
static int setenabled(char* id, int enabled)
Enable or disable an element. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
enabled | int | - | 0: disable element, 1: enable element |
static int setvisible(char* id, int enabled)
Show or hide an element. This function can be used in callback functions registerd via dialog::listen and refers to the active dialog. If several dialogs are opened, the active dialog is the one opened last.
Name | Type | Default | Description |
Return | int | 0 or error code, if an element with the given ID doesn't exist or if no dialog is present. | |
id | String or char* | - | ID of the elementes |
enabled | int | - | 0: hide element, 1: show element |
static int wascancelled()
Determine, if the last dialog has been cancelled. A dialog has been cancelled, if
Name | Type | Default | Description |
Return | int | 1: dialog has been cancelled, 0: dialog has not been cancelled |
static int wasconfirmed()
Determine, if the last dialog has been confirmed. A dialog has been confirmed, if
Name | Type | Default | Description |
Return | int | 1: dialog has been confirmed, 0: dialog has not been confirmed |
static int listen(char* id, int fptr)
Register a callback function to handle dialog events.
Callback functions allow reacting on dialog events, such as value change, selection change or click on a button.
The functions must implement the following signature:
int myCallback(char * id);Whereas
Name | Type | Default | Description |
Return | int | 0 or error code | |
id | String or char* | - | ID of the element to listen |
fptr | int | - | callback function pointer |
Simple usage example:
int handleOkButton(char * id) { if (/* certainConditions ... */ ) { // stop processing this event return 0; } // continue processing this event return 1; }
int main() { String dialogSpec = string::alloc(); // ... dialog::dialogbuttons(dialogSpec); dialog::listen("kOk", handleOkButton);
dialog::show(dialogSpec); return 0; }
static int suppress_listeners(int state)
(De)activate all listener functions. The function is needed if you use a listener function that also changes the value of the calling field.
Don't forget to reactivate the deactivated listeners at the end of you listener function!
Name | Type | Default | Description |
Return | int | 0 or Error code | |
state | int | - | 1 : Deactivate all listeners 0 : Enable all listeners |
Establish a dialoge with all available controls. At the end the user input is fetched and used in the script.
int main() { String dialogSpec = string::alloc(); StringList dropDownEntries = stringlist::alloc();
stringlist::append(dropDownEntries, "Entry one"); stringlist::append(dropDownEntries, "Entry two"); stringlist::append(dropDownEntries, "-"); //separator stringlist::append(dropDownEntries, "Entry three"); stringlist::append(dropDownEntries, "Entry four");
dialog::title(dialogSpec, "Dialog Demo"); dialog::size(dialogSpec, 360.0, 400.0);
dialog::label(dialogSpec, "label1", "This is a basic dialog framework demo", 40.0, 20.0, 250.0, 30.0); dialog::checkbox(dialogSpec, "cb1", "This is a checkbox", 1, 40.0, 60.0, 200.0, 20.0); dialog::textfield(dialogSpec, "tf1", "This is a textfield", 40.0, 90.0, 200.0, 20.0); dialog::label(dialogSpec, "label2", "This is a floatfield:", 40.0, 120.0, 130.0, 20.0); dialog::floatfield(dialogSpec, "ff1", 10.5, 160.0, 120.0, 75.0, 20.0); dialog::label(dialogSpec, "label3", "This is an intfield:", 40.0, 150.0, 130.0, 20.0); dialog::intfield(dialogSpec, "if1", 333, 160.0, 150.0, 75.0, 20.0); dialog::label(dialogSpec, "label4", "This is a dropdown:", 40.0, 190.0, 130.0, 20.0); dialog::dropdown(dialogSpec, "dd1", dropDownEntries, 0, 160.0, 190.0, 125.0, 20.0); dialog::multilinetextfield(dialogSpec, "mtf1", "This is a \nmulti-\nline-\ntextfield", 40.0, 230.0, 200.0, 75.0);
dialog::dialogbuttons(dialogSpec);
dialog::show(dialogSpec);
//Fetch all control results and display them in a messagebox showmessage("Dialog results:\nClosed by confirm button: %s\nCheckbox %s: %d\nTextfield %s: %s\nMultiline Textfield %s: %s\nFloatfield %s: %f\nIntfield %s: %d\nDropdown %s: %s", dialog::rconfirmbutton(), "cb1", dialog::rcheckbox("cb1"), "tf1", dialog::rtextfield("tf1"), "mtf1", dialog::rmultilinetextfield("mtf1"), "ff1", dialog::rfloatfield("ff1"), "if1", dialog::rintfield("if1"), "dd1", dialog::rdropdown("dd1"));
return 0; }
Conditional accepting dialog
int reallySure(char * id) { char buffer[4096];
if (strcmp(id, "okButton") == 0) { strcpy (buffer, dialog::rmultilinetextinfo("info"));
if (strlen(buffer) < 100) { strreplace(buffer, "sure", "really sure"); dialog::wmultilinetextinfo("info", buffer); return 0; }
return 1; } return 1; }
int main() { String dialogSpec = string::alloc();
dialog::title(dialogSpec, "Do you really want to...?"); dialog::size(dialogSpec, 300.0, 200.0);
dialog::multilinetextinfo(dialogSpec, "info", "Are you sure you want to click Ok?", 20.0, 20.0, 260.0, 160.0);
dialog::confirmbutton(dialogSpec, "cancelButton", "Cancel", 120.0, 170.0, 70.0, 20.0); dialog::confirmbutton(dialogSpec, "okButton", "Ok", 200.0, 170.0, 70.0, 20.0);
dialog::listen("okButton", reallySure); dialog::show(dialogSpec);
return 0; }
Dynamic Dropwdown
int calculateSequence(char * id) { StringList sequence = stringlist::alloc(); char tmp [4096]; int offset = -1; int c = 1; strcpy(tmp, dialog::rdropdown("range")); if (strcmp(tmp, "odd") == 0) { offset = 1; } else if (strcmp(tmp, "even") == 0) + { offset = 0; } if (offset >= 0) { for (;c < 11; ++c) { sprintf(tmp, "%d", (c * 2) - offset); stringlist::append(sequence, tmp); } }
dialog::wdropdown("sequence", sequence, 0); dialog::setenabled("sequence", offset >= 0); dialog::setenabled("kOk", offset >= 0); stringlist::release(sequence);
return 1; }
int main() { String dialogSpec = string::alloc(); StringList range = stringlist::alloc(); StringList sequence = stringlist::alloc();
stringlist::append(range, "Select range..."); stringlist::append(range, "odd"); stringlist::append(range, "even");
stringlist::append(sequence, "Please select range first");
dialog::title(dialogSpec, "Dynamic dropdown"); dialog::size(dialogSpec, 300.0, 200.0);
dialog::dropdown(dialogSpec, "range", range, 0, 20.0, 20.0, 180.0, 20.0); dialog::dropdown(dialogSpec, "sequence", sequence, 0, 20.0, 50.0, 180.0, 20.0, 0);
dialog::dialogbuttons(dialogSpec, 1, 1, 0, 1);
dialog::listen("range", calculateSequence);
dialog::show(dialogSpec);
stringlist::release(range); stringlist::release(sequence); string::release(dialogSpec);
return 0; }
Nested dialogs
int fibonacci(char * id) { String dialogSpec = string::alloc(); char tmp[4096]; int c = string::get_token_count (id, ","); int p1 = string::to_int (string::get_token (id, ",", c - 2)); int p2 = string::to_int (string::get_token (id, ",", c - 1));
sprintf(tmp, "%s,%d", id, (p1 + p2)); wlog("", "Enter dialog, v=%s\n", tmp);
dialog::title(dialogSpec, "Fibonacci"); dialog::size(dialogSpec, 400.0, 200.0);
dialog::multilinetextinfo(dialogSpec, "fibonacci", tmp, 20.0, 20.0, 360.0, 160.0);
dialog::confirmbutton(dialogSpec, "kCancel", "Close", 120.0, 170.0, 80.0, 20.0); dialog::confirmbutton(dialogSpec, tmp, "Next", 220.0, 170.0, 80.0, 20.0);
dialog::listen(tmp, fibonacci);
dialog::show(dialogSpec);
strcpy(tmp, dialog::rmultilinetextinfo("fibonacci")); wlog("", "Leave dialog, v=%s\n", tmp);
string::release(dialogSpec);
return 0; }
int main() { fibonacci("0,1"); return 0; }
Calculator
//////////////////////////////////////////////////////////////////// // // helpers // int lpad(char * str) { // pad with blanks int c = 36 - 2 * strlen(str) - 1;
for (;c > 0; --c) { sprintf(str, "%s%s", " ", str); } return 0; }
int printfloat(float floatValue, char * buffer) { int intValue;
sprintf(buffer, "%F", floatValue); intValue = val(buffer); if ((float)intValue == floatValue) { sprintf(buffer, "%d", intValue); } else { sprintf(buffer, "%.10F", floatValue); } return 0; }
float calculate(int * err) { char op [16]; char lvalue [4096]; char rvalue [4096]; float result = 0.0; float lf, rf; strcpy(lvalue, dialog::rlabel("lvalue")); strcpy(rvalue, dialog::rtextfield("rvalue")); strcpy(op, dialog::rlabel("operator"));
lf = fval(lvalue); rf = fval(rvalue);
err = 0;
if (strcmp(op, "+") == 0) { result = lf + rf; } else if (strcmp(op, "-") == 0) { result = lf - rf; } else if (strcmp(op, "*") == 0) { result = lf * rf; } else if (strcmp(op, "/") == 0) { if (rf > 0.0 || rf < 0.0) { result = lf / rf; } else { err = 1; result = -0.0; } } else { result = rf; }
return result; }
char * gInitialValue = " 0";
//////////////////////////////////////////////////////////////////// // // callbacks // int numberClicked(char * id) { char tmp [4096];
strcpy(tmp, dialog::rtextfield("rvalue")); strtrim(tmp);
// don't allow more than 18 digits: if (strlen(tmp) >= 18) return 0;
// remove leading 0 or 'NaN' if (strcmp(tmp, "0") == 0 || strcmp(tmp, "NaN") == 0) tmp[0] = 0;
// append last number clicked strcat(tmp, id); lpad(tmp);
dialog::wtextfield("rvalue", tmp); return 0; }
int dotClicked(char * id) { char tmp[4096];
strcpy(tmp, dialog::rtextfield("rvalue")); strtrim(tmp);
if (strlen(tmp) >= 18) return 0; // already contains a 'dot' if (strstr(tmp, ".")) return 0; strcat(tmp, "."); lpad(tmp);
dialog::wtextfield("rvalue", tmp);
return 0; }
int negateClicked(char * id) { char tmp[4096];
strcpy(tmp, dialog::rtextfield("rvalue")); strtrim(tmp); if (strlen(tmp) >= 18) return 0;
if (strstr(tmp, "-") == tmp) { sprintf(tmp, "%s", tmp + 1); } else { sprintf(tmp, "-%s", tmp); } lpad(tmp);
dialog::wtextfield("rvalue", tmp);
return 0; }
int invertClicked(char * id) { char tmp[4096]; float f;
strcpy(tmp, dialog::rtextfield("rvalue")); strtrim(tmp); f = fval(tmp);
if (f > 0.0 || f < 0.0) { printfloat((1.0/f), tmp); } else { sprintf(tmp, "NaN"); } lpad(tmp);
dialog::wtextfield("rvalue", tmp);
return 0; }
int operatorClicked(char * id) { float v; char tmp[4096]; int err;
if (strcmp(id, "C") == 0) { dialog::wlabel("operator", ""); dialog::wlabel("lvalue", ""); dialog::wtextfield("rvalue", gInitialValue); } else { v = calculate(&err);
printfloat(v, tmp); lpad(tmp); if (strcmp(id, "=") == 0) { dialog::wlabel("operator", ""); dialog::wlabel("lvalue", ""); dialog::wtextfield("rvalue", tmp); } else { dialog::wlabel("operator", id); dialog::wlabel("lvalue", tmp); dialog::wtextfield("rvalue", gInitialValue); } if (err) { dialog::wlabel("operator", "E"); } }
return 0; }
//////////////////////////////////////////////////////////////////// // // main // int main() { String dialogSpec = string::alloc(); int c = 0; char tmp [4096]; dialog::title(dialogSpec, "Calculator"); dialog::size(dialogSpec, 190.0, 300.0);
dialog::label(dialogSpec, "lvalue", " ", 10.0, 10.0, 160.0, 30.0); dialog::label(dialogSpec, "operator", " ", 170.0, 10.0, 20.0, 30.0); dialog::textfield(dialogSpec, "rvalue", gInitialValue, 10.0, 50.0, 170.0, 30.0);
dialog::confirmbutton(dialogSpec, "1", "1", 20.0, 90.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "2", "2", 60.0, 90.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "3", "3", 100.0, 90.0, 30.0, 30.0);
dialog::confirmbutton(dialogSpec, "4", "4", 20.0, 130.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "5", "5", 60.0, 130.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "6", "6", 100.0, 130.0, 30.0, 30.0);
dialog::confirmbutton(dialogSpec, "7", "7", 20.0, 170.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "8", "8", 60.0, 170.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "9", "9", 100.0, 170.0, 30.0, 30.0);
dialog::confirmbutton(dialogSpec, ".", ".", 20.0, 210.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "0", "0", 60.0, 210.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "C", "C", 100.0, 210.0, 30.0, 30.0);
dialog::confirmbutton(dialogSpec, "+/-", "+/-", 20.0, 250.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "1/x", "1/x", 60.0, 250.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "=", "=", 100.0, 250.0, 70.0, 30.0);
dialog::confirmbutton(dialogSpec, "/", "/", 140.0, 90.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "*", "*", 140.0, 130.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "-", "-", 140.0, 170.0, 30.0, 30.0); dialog::confirmbutton(dialogSpec, "+", "+", 140.0, 210.0, 30.0, 30.0);
// add listener for numbers... for (; c < 10; ++c) { sprintf(tmp, "%d", c); dialog::listen(tmp, numberClicked); }
// ... dot ... dialog::listen(".", dotClicked);
// ... +/- ... dialog::listen("+/-", negateClicked); dialog::listen("1/x", invertClicked); dialog::listen("+", operatorClicked); dialog::listen("-", operatorClicked); dialog::listen("*", operatorClicked); dialog::listen("/", operatorClicked); dialog::listen("=", operatorClicked); dialog::listen("C", operatorClicked);
dialog::show(dialogSpec);
return 0; }
Alphabetic index HTML hierarchy of classes or Java