Once you have defined the context menu dynamically in the callback routine, the system displays it on the screen immediately. When the user chooses a function from the menu, the system triggers the PAI event and places the corresponding function code in SY-UCOMM and the OK CODE field.
Context Menus
Example
The following example shows some of the technical possibilities for creating context menus, but does not necessarily observe 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,
Context Menus
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 next screen (statically defined) for screen 100 is itself. It has the following layout:
Try the right-hand mouse button (Shift+F10) in various positions
Show event Event Input
The relevant extract of the element list is as follows:
Name Type Contents 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
Elements TEXT2 and FIELD2 have no context menus of their own. They inherit the context menu FRAME from the group box. They are assigned to modification group MOD.
The 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 assigned to 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 in the PBO. 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 modification. This context menu has a single line- Cancel.
FRAME:
Constructed from the static context menus CONTEXT_MENU_2 and
CONTEXT_MENU_1. This has two lines – Hide result and Cancel. The entry for function code HIDE is highlighted.
REVEAL:
Constructed from the static context menus CONTEXT_MENU_3 and
CONTEXT_MENU_1. This has two lines – Show result and Cancel. The entry for function code REVEAL is highlighted.
INPUT:
Constructed by including the four-line 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 we include it in the context menu for INPUT, we must specify a text for the menu entry behind which it stands.
When a user runs the program and right-clicks the first line, the context menu TEXT is displayed. When he or she right-clicks the second line, context menu INPUT is
Context Menus
displayed, and for the third line, FRAME is displayed. The fourth line 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 system calculates using the number in input field FIELD1 and places the result in FIELD2.
When the user chooses Hide result (HIDE), the program modifies the screen dynamically. The fourth line becomes visible, and the user can now display the context menu REVEAL.