Context Menus
Use
The user interface of a dynpro is defined by a GUI status with type dialog status, defined in Menu Painter. For each dialog status, the system automatically creates a standard context menu, which the user can display by clicking the right-hand mouse button on the screen of the dynpro (or by choosing Shift+F10). The standard context menu contains all of the function keys with which functions are associated. It therefore makes it easy to access any function code that is available using the keyboard, since normally only the most important are assigned to the application toolbar.
However, as well as the standard context menu, context-specific menus can be defined on dynpros for any of the following screen elements:
-
Input/output fields
-
Text fields
-
Table controls
-
Group boxes
-
Subscreens
When one of these elements is selected using the right-hand mouse button, a dynamic context menu can be created in the ABAP program. This context menu can contain any function, not just the function keys. Pushbuttons, checkboxes, and radio buttons cannot have their own context menus. They can, however, be given unique function codes.
The Class CL_CTMENU
Context menus are objects of the global class CL_CTMENU. In the class library, this class is one of the Frontend Services components, alongside the Control Framework classes (see Custom Controls). It contains methods that allow context menus to be defined dynamically in the local program. As a template, dedicated context menus can be created statically in Menu Painter.
The most important methods of the class CL_CTMENU are:
|
Method |
Function |
|---|---|
|
LOAD_GUI_STATUS |
Appends a context menu predefined statically in Menu Painter to a context menu in the local program. |
|
ADD_FUNCTION |
Appends a single function to a context menu in the local program. |
|
ADD_MENU |
Appends another context menu to the current context menu in the local program. |
|
ADD_SUBMENU |
Appends another context menu to the current context menu in the local program as a cascading menu. |
|
ADD_SEPARATOR |
Adds a separator. |
|
HIDE_FUNCTIONS |
Hides functions |
|
SHOW_FUNCTIONS |
Shows functions |
|
DISABLE_FUNCTIONS |
Deactivates functions |
|
ENABLE_FUNCTIONS |
Activates functions |
|
SET_DEFAULT_FUNCTION |
Defines a default function, which is highlighted when the menu is displayed. |
In the above table, a context menu in the local program means an object of class CL_CTMENU. CL_CTMENU is used in different ways depending on the context:
If a context menu is used in a control, the need to create objects of the class CL_CTMENU depends on the wrapper of the class. (The relevant functions may already be encapsulated in the control class.) Normally, control users do not have to create their own context menus. This ensures that no conflicts occur with the event handler of the control. For more information, see the documentation of the individual control classes.
When context menus are defined on dynpros (or on lists), the associated objects in class CL_CTMENU are created automatically by the runtime environment and not explicitly in the program. References to the object are passed as the parameters of special callback routines in the ABAP program.
Context Menus for Elements on Dynpros
To associate a context menu with one of the screen elements above, only the ID context needs to be entered in the Context Menu field in the element attributes in Screen Painter. If a context menu is not defined for a particular screen element, it inherits the context menu from the next highest element in the hierarchy. For example, all screen elements in a group box that do not have their own context menu would inherit the context menu of the group box. The highest hierarchy level is the default context menu, containing all of the key settings of the current dialog status.
If a screen element is associated with a context menu (either its own menu or one that it has inherited) a special subroutine
ON_CTMENU_context
is called in the ABAP program when the user clicks the right-hand mouse button (or SHIFT F10). The PAI event is not triggered. This subroutine (callback routine) is used to define the context menu dynamically and must be programmed in the processing logic. If an appropriate subroutine does not exist, the context menu is not displayed.
The same context menu context can be associated with any number of screen elements. They then all work with the same subroutine.
Defining Context Menus in the Processing Logic
For each context menu context to be called for an element on a dynpro, a corresponding callback routine must be programmed in the ABAP program:
FORM ON_CTMENU_context USING l_menu TYPE REF TO cl_ctmenu. ... ENDFORM.
Each of these routines must have a single USING parameter, typed as a reference variable to class CL_CTMENU. For each context menu assigned to an element on a screen, the runtime environment automatically creates an object of the class. When the user requests the context menu by right-clicking with the mouse (or by using SHIFT F10), the system calls the corresponding subroutine and passes a reference to the corresponding object to the formal parameter.
When the object is passed it is initial and the context menu does not contain any entries. In the subroutine, the methods of the object (as listed above) can now be used to construct the context menu dynamically.
Using Predefined Context Menus
As well as dialog statuses and dialog box statuses, there is a third kind of GUI status that can be defined in Menu Painter, namely context menus. For information about creating context menus in Menu Painter in ABAP Workbench, see Creating Context Menus.
Predefined context menus enable groups of statically defined function code from an ABAP program to be provided in a context-specific form. The method LOAD_GUI_STATUS makes it possible to append a context menu from any ABAP program to a context menu in the local program. As a rule, predefined context menus reuse the function codes from a dialog status with the same semantics, but in a context-specific way. Once a predefined context menu is loaded to a context menu in the local program, it can be modified in any way (other predefined context menus can be appended, functions added or removed, other context menus added).
Defining New Context Menus
Completely new context-specific menus can be created either by modifying predefined context menus or by constructing menus from new functions.
Any number of new functions can be added to a context menu. When a new function is added, a function text, function code, and function type (for example, E for an unconditional module call) must be specified.
However, it is also possible to add any other context menu objects from the local program. In this case, it is enough just to pass a reference to another context menu (see example below). A collection of context menu objects can be created in the program and used and combined as necessary. Submenus can also be constructed. Deep-nested menus can be created by adding submenus to existing submenus.
Style Guidelines
When creating context menus, the following rules should be observed:
-
The functions in a context menu should be a subset of the functions in the program This can be observed by using predefined context menus.
-
Context menus should not contain more than ten entries at a single level.
-
If a context menu is used for a screen element, it should ideally contain all of the functions possible for that element, at the very least the standard commands, like Select, Copy, Cut, and Paste.
-
The order of the functions should be: object-specific commands, copying commands, further commands.
-
Functions that can be selected using the left-hand mouse button should not be duplicated in a context menu.
Displaying the Context Menu
Once the context menu is defined dynamically in the callback routine, it is displayed on the screen immediately. When the user chooses a function from the menu, the system triggers the PAI event and passes the corresponding function code to the ABAP program in sy-ucomm and the OK field.
Example
The following example shows some of the technical options for creating context menus, without necessarily observing all of the style guidelines.
REPORT demo_dynpro_context_menu.
DATA: field1 TYPE i VALUE 10,
field2 TYPE p DECIMALS 4.
DATA: prog TYPE sy-repid,
flag(1) TYPE c VALUE 'X'.
DATA: ok_code TYPE sy-ucomm,
save_ok TYPE sy-ucomm.
prog = sy-repid.
CALL SCREEN 100.
MODULE status_0100 OUTPUT.
SET TITLEBAR 'TIT100'.
IF flag = 'X'.
SET PF-STATUS 'SCREEN_100' EXCLUDING 'REVEAL'.
ELSEIF flag = ' '.
SET PF-STATUS 'SCREEN_100' EXCLUDING 'HIDE'.
ENDIF.
LOOP AT SCREEN.
IF screen-group1 = 'MOD'.
IF flag = 'X'.
screen-active = '1'.
ELSEIF flag = ' '.
screen-active = '0'.
ENDIF.
MODIFY SCREEN.
ELSEIF screen-name = 'TEXT_IN_FRAME'.
IF flag = 'X'.
screen-active = '0'.
ELSEIF flag = ' '.
screen-active = '1'.
ENDIF.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDMODULE.
MODULE cancel INPUT.
LEAVE PROGRAM.
ENDMODULE.
MODULE user_command_0100.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'HIDE'.
flag = ' '.
WHEN 'REVEAL'.
flag = 'X'.
WHEN 'SQUARE'.
field2 = field1 ** 2.
WHEN 'CUBE'.
field2 = field1 ** 3.
WHEN 'SQUAREROOT'.
field2 = field1 ** ( 1 / 2 ).
WHEN 'CUBICROOT'.
field2 = field1 ** ( 1 / 3 ).
ENDCASE.
ENDMODULE.
*******************************************************
* Callback-Routines
*******************************************************
FORM on_ctmenu_text USING l_menu TYPE REF TO cl_ctmenu.
CALL METHOD:l_menu->load_gui_status
EXPORTING program = prog
status = 'CONTEXT_MENU_1'
menu = l_menu.
ENDFORM.
FORM on_ctmenu_frame USING l_menu TYPE REF TO cl_ctmenu.
CALL METHOD:l_menu->load_gui_status
EXPORTING program = prog
status = 'CONTEXT_MENU_2'
menu = l_menu,
l_menu->load_gui_status
EXPORTING program = prog
status = 'CONTEXT_MENU_1'
menu = l_menu,
l_menu->set_default_function
EXPORTING fcode = 'HIDE'.
ENDFORM.
FORM on_ctmenu_reveal USING l_menu TYPE REF TO cl_ctmenu.
CALL METHOD:l_menu->load_gui_status
EXPORTING program = prog
status = 'CONTEXT_MENU_3'
menu = l_menu,
l_menu->load_gui_status
EXPORTING program = prog
status = 'CONTEXT_MENU_1'
menu = l_menu,
l_menu->set_default_function
EXPORTING fcode = 'REVEAL'.
ENDFORM.
FORM on_ctmenu_input USING l_menu TYPE REF TO cl_ctmenu.
DATA calculate_menu TYPE REF TO cl_ctmenu.
CREATE OBJECT calculate_menu.
CALL METHOD: calculate_menu->add_function
EXPORTING fcode = 'SQUARE'
text = text-001,
calculate_menu->add_function
EXPORTING fcode = 'CUBE'
text = text-002,
calculate_menu->add_function
EXPORTING fcode = 'SQUAREROOT'
text = text-003,
calculate_menu->add_function
EXPORTING fcode = 'CUBICROOT'
text = text-004,
l_menu->add_submenu
EXPORTING menu = calculate_menu
text = text-005.
ENDFORM.
The static next dynpro number of dynpro 100 is 100. Its layout is as follows:
The part of the element list relevant for context menus is as follows:
|
Name |
Type |
Content |
Context menu |
|
TEXT |
Text |
Try the... |
TEXT |
|
FRAME |
Frame |
FRAME |
|
|
TEXT1 |
Text |
Input |
INPUT |
|
FIELD1 |
I/O |
INPUT |
|
|
TEXT2 |
Text |
Result |
|
|
FIELD2 |
I/O |
||
|
TEXT_IN_FRAME |
Text |
Show event |
REVEAL |
The elements TEXT2 and FIELD2 do not have context menus of their own. They inherit the context menu FRAME from the group box. They are given the modification group MOD.
The dynpro flow logic is as follows:
PROCESS BEFORE OUTPUT.
MODULE status_0100.
PROCESS AFTER INPUT.
MODULE cancel AT EXIT-COMMAND.
MODULE user_command_0100.
The following function codes and GUI status are defined statically for this ABAP program:
|
Function codes |
BACK |
CANCEL |
EXIT |
HIDE |
REVEAL |
|
Dialog status |
|||||
|
SCREEN_100 |
X |
X |
X |
X |
X |
|
Context menus |
|||||
|
CONTEXT_MENU_1 |
X |
||||
|
CONTEXT_MENU_2 |
X |
||||
|
CONTEXT_MENU_3 |
X |
The table shows the function codes that each GUI status contains.
The dialog status SCREEN_!00 is set statically at PBO time. The function codes HIDE and REVEAL are displayed or hidden, depending on the contents of the FLAG field.
The context menus for the screen elements are constructed in the callback routines as follows:
TEXT: Loads the static context menu CONTEXT_MENU_1 without modifying it. This context menu has a single row, Cancel.
FRAME: Constructs the context menu from the static context menus CONTEXT_MENU_2 and CONTEXT_MENU_1. This has two rows, Hide Result and Cancel. The row for the function code HIDE is highlighted.
REVEAL: Constructs the context menu from the static context menus CONTEXT_MENU_3 and CONTEXT_MENU_1. This has two rows, Show Result and Cancel. The row for the function code REVEAL is highlighted.
INPUT: Constructs the context menu by including the four-row local context menu CALCULATE_MENU as a submenu. To do this, the program declares a local reference variable with reference to class CL_CTMENU, then creates the object and adds the function codes SQUARE, CUBE, SQUAREROOT, and CUBICROOT. When included in the context menu for INPUT, a text must be specified for the entry to which the submenu is appended.
When the program is executed and the user right-clicks or presses SHIFT F10 on the first row, the context menu TEXT is displayed. On the second row, the context menu INPUT is displayed, and on the third line, FRAME is displayed. The fourth row is hidden by the program. For all other areas of the screen, the system displays the standard context menu, with all of the static function codes plus F1 and F4.
When the user chooses one of the new dynamic functions, the calculations are performed using the number in the input field FIELD1 and the result is passed to FIELD2.
When the user chooses Hide Result (HIDE), the screen is modified dynamically. The fourth row then becomes visible and the user can display the context menu REVEAL.