Some developers believe that automatic actions are for beginners. However they might have missed a lot of potential we added in the last years. Standard actions, such as First Record or color?value=red, can be assigned to a button in the user interface or by programming. The action can be assigned to a menu item, context menu, or invoked with a command. A benefit is that 4D also automatically handles the menu item or button deactivation whenever the action is not applicable (for instance, as if there is no next record).
And from 4D v16 R3, the list of standard actions is growing: over 100 new standard actions have been added for 4D Write Pro and Styled Text, allowing to build for instance a toolbar without writing a line of code!
As a summary, you can use Standard Actions in 3 different ways:
- Associate a standard action to an object: at design phase (using property list) or by programming
- Associate a standard action to a menu item: at design phase (using toolbox) or by programming
- Use the new INVOKE ACTION and Get action info commands
Associate a standard action to an object
At design phase
With the property list you can associate a standard action to an active form object. In this example, the Delete Record standard action is associated to a button.
For more details, please refer to this article in the documentation.
By programming
During the application runtime, you can dynamically associate for the current process a standard action to an object by programming. Thus, you overwrite the standard action defined by property list.
For example, you want to associate the Delete Record standard action to a “btnDelete” button:
OBJECT SET ACTION(*;"btnDelete";ak delete record)
or if you use the value instead of constant:
OBJECT SET ACTION(*;"btnDelete";"deleteRecord")
And if you use the value instead of constant, you can also pass a parameter with the action. In this example, you apply the magenta color to a selected text.
OBJECT SET ACTION(*;"btnDelete";"color?value=magenta")
For more details, please refer to the documentation for these commands:
Associate a standard action to a menu item
At design phase
You can assign a standard action to a menu item using the menu editor. For example, to add a menu item to open the 4D Maintenance and Security Center, just select the msc action as associated standard action in the pop-up menu:
When you execute your application, the menu displays:
You can create a sub menu with some standard action. For example, to add a sub menu to apply the font style (like Bold, Italic…), just select the fontStyle action as associated standard action in the pop-up menu:
When you execute your application, the menu displays:
For more details, please refer to this article in the documentation.
By programming
You can also pass a standard action to the SET MENU ITEM PROPERTY command. Since 4D v16 R3, if you pass the ak standard action title constant instead a title name to the APPEND MENU ITEM command ; 4D uses the localized action name.
For example, in a pop-up menu to add a menu item to go to the first, last or next record, you write the following code:
- In the Form method:
Case of
: (Form event=On Load)
// create menu
menu:=Create menu
//insert first record menu item
APPEND MENU ITEM(menu;ak standard action title)
SET MENU ITEM PROPERTY(menu;-1;Associated standard action;ak first record)
//insert last record menu item
APPEND MENU ITEM(menu;ak standard action title)
SET MENU ITEM PROPERTY(menu;-1;Associated standard action;ak last record)
//insert next record menu item
APPEND MENU ITEM(menu;ak standard action title)
SET MENU ITEM PROPERTY(menu;-1;Associated standard action;ak next record)
: (Form event=On Unload)
// release menu
RELEASE MENU(menu)
End case
- In the button method that displays the pop up menu:
$val:=Dynamic pop up menu(menu)
Then, when you execute your application in different language, the menu displays:
For more details, please refer to the documentation on these commands:
- GET MENU ITEM PROPERTY command
- SET MENU ITEM PROPERTY command
- APPEND MENU ITEM command
- INSERT MENU ITEM command
INVOKE ACTION command
The INVOKE ACTION command, added in 4D v16 R3, triggers the standard action defined by the action parameter, optionally in the target context. To know if the standard action is applicable and valid in the current context, you can use the new Get action info command.
The syntax is:
For example, to change the open the MSC, you can write:
C_OBJECT($actionInfo)
// Check if this standard action is available and valid in this current context
$actionInfo:=Get action info(ak paste;ak current form)
If (OB Get($actionInfo;"enabled"))
// This standard action is enabled
INVOKE ACTION(ak paste;ak current form)
Else
ALERT("This action is impossible. The clipboard is empty.")
End if
For more details, please read the documentation on these commands: