Form Editor Macros
The 4D Form editor supports macros. A macro is a set of instructions to perform an action or a sequence of actions. When called upon, the macro will execute its instructions and automatically perform the action(s).
For example if you have a recurring report with specific formatting (e.g., certain text must appear in red and certain text must appear in green), you can create a macro to automatically set the color. You can create macros for the 4D Form editor that can:
- Create and execute 4D code
- Display dialogs
- Select form objects
- Add / delete / modify forms, form objects as well as their properties
- Modify project files (update, delete)
Macros code supports class functions and form object properties in JSON to let you define any custom feature in the Form editor.
Macros can been defined for the host project or for components within the project. Usually, you will create a macro and install it within the components you use for development.
When called, a macro overrides any previously specified behaviors.
Hands-on example
In this short example, you'll see how to create and call a macro that adds a "Hello World!" alert button in the top left corner of your form.
- In a
formMacros.json
file within theSources
folder of your project, you write:
{
"macros": {
"Add Hello World button": {
"class": "AddButton"
}
}
}
-
Create a 4D class named
AddButton
. -
Within the
AddButton
class, write the following function:
Function onInvoke($editor : Object)->$result : Object
var $btnHello : Object
// Create a "Hello" button
$btnHello:=New object("type"; "button"; \
"text"; "Hello World!"; \
"method"; New object("source"; "ALERT(\"Hello World!\")"); \
"events"; New collection("onClick"); \
"width"; 120; \
"height"; 20; \
"top"; 0; \
"left"; 0)
// Add button in the current page
$editor.editor.currentPage.objects.btnHello:=$btnHello
// Select the new button in the form editor
$editor.editor.currentSelection.clear() //unselect elements
$editor.editor.currentSelection.push("btnHello")
// Notify the modification to the 4D Form editor
$result:=New object("currentSelection"; $editor.editor.currentSelection;\
"currentPage"; $editor.editor.currentPage)
You can then call the macro:
Calling macros in the Form editor
When macros are defined in your 4D project, you can call a macro using the contextual menu of the Form editor:
This menu is built upon the formMacros.json
macro definition file(s). Macro items are sorted in alphabetical order.
This menu can be called in an empty area or a selection in the form. Selected object are passed to $editor.currentSelection
or $editor.target
in the onInvoke
function of the macro.
A single macro can execute several operations. If selected, the Undo feature of the Form editor can be used to reverse macro operations globally.
Location of macro file
All 4D Form Editor macros are defined within a single JSON file per project or component: FormMacros.json
.
This file must be located in the host or component's Project > Sources folder:
Declaring macros
The structure of the formMacros.json
file is the following:
{
"macros": {
<macroName>: {
"class": <className>,
<customProperty> : <value>
}
}
}
Here is the description of the JSON file contents:
Attribute | Type | Description | ||
---|---|---|---|---|
macros | object | list of defined macros | ||
<macroName> | object | macro definition | ||
class | string | macro class name | ||
<customProperty> | any | (optional) custom value to retrieve in the constructor |
Custom properties, when used, are passed to the constructor function of the macro.
Example
{
"macros": {
"Open Macros file": {
"class": "OpenMacro"
},
"Align to Right on Target Object": {
"class": "AlignOnTarget",
"myParam": "right"
},
"Align to Left on Target Object": {
"class": "AlignOnTarget",
"myParam": "left"
}
}
}
Instantiating macros in 4D
Each macro you want to instantiate in your project or component must be declared as a 4D class.
The class name must match the name defined using the class attribute of the formMacros.json
file.
Macros are instantiated at application startup. Consequently, if you modify the macro class structure (add a function, modify a parameter...) or the constructor, you will have to restart the application to apply the changes.
Macro Functions
Every macro class can contain a Class constructor
and two functions: onInvoke()
and onError()
.