Context menu is powerful tool to provide quick and context specific options to users. SAP Module pool (classical Dynpro) does support context menus and in this blog I will explain how it works with a sample ABAP program.

On module pool screen elements like label, text, box etc. you can specify a form name which will be called when user right clicks on that screen element. And within this form context menu, which is PF status of type context menu, is loaded by calling static method CL_CTMENU=>LOAD_GUI_STATUS. If user right clicks on an element which does not defines context menu form then system will search for context menu form of its parent. For example, if you have element text, without any context menu form, within a box element and you have context menu form defined on box element then right click on text element will trigger context menu form of box element. Taking this concept further in case box element doesn’t have context menu form defined then system will look for context menu defined on screen.
In nutshell, system look for context menu hierarchically starting with most specific menu defined at element to generic context menu defined at screen level.
In this sample program I will have a screen with context menu defined on box element and one on screen itself. You will see how system trigger different context menu depending on where you click.
Create report ZPWCONTEXT with screen 100 in it. See screen shot below which show how screen 100 looks like. I have used field from dictionary option and inserted field from MARA table. Make sure you have some field inside box element Detail.
sap context menu
Double click on box element Detail and define Context Menu Form BOX in Attributes. When user right clicks on this box element or any other element within this box, unless it has context menu of its own, will call form ON_CTMENU_BOX in program.
sap context menu
We are going to define another context menu form but this one at screen level. For this open attributes tab of screen and enter SCREEN in context menu form. Right click on screen where there isn’t any specific context menu define will trigger a call to form ON_CTMENU_SCREEN.
sap context menu
Define two PF status SCR_100 and BOX_100 of type Context Menu.
sap context menu
Put code OPTION1 in context menu SCR_100
sap context menu
And code OPTION2 in context menu BOX_100.
sap context menu
Define normal PF status MAIN with function code BACK, EXIT and CANC for screen.
sap context menu
And a titlebar T01.
sap context menu
Finish program by copying following code in report.
*&---------------------------------------------------------------------*
*& Report  ZPWCONTEXT
*&---------------------------------------------------------------------*
 
REPORT  zpwcontext.
 
CALL SCREEN 100.
 
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'MAIN'.
  SET TITLEBAR 'T01'.
ENDMODULE.                 " STATUS_0100  OUTPUT
 
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANC' .
      LEAVE TO SCREEN 0 .
    WHEN 'OPTION1'.
      MESSAGE i001(00) WITH 'Right Click on screen-Option1' .
    WHEN 'OPTION2'.
      MESSAGE i001(00) WITH 'Right Click on box-Option2' .
 
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
 
*&---------------------------------------------------------------------*
*&      Form  on_ctmenu_screen
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->OB_MENU    text
*----------------------------------------------------------------------*
FORM on_ctmenu_screen USING ob_menu TYPE REF TO cl_ctmenu .
 
  CALL METHOD cl_ctmenu=>load_gui_status
    EXPORTING
      program = sy-cprog
      status  = 'SCR_100'
      menu    = ob_menu.
 
ENDFORM.                    "on_ctmenu_scr_right
 
*&---------------------------------------------------------------------*
*&      Form  on_ctmenu_screen
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->OB_MENU    text
*----------------------------------------------------------------------*
FORM on_ctmenu_box USING ob_menu TYPE REF TO cl_ctmenu .
 
  CALL METHOD cl_ctmenu=>load_gui_status
    EXPORTING
      program = sy-cprog
      status  = 'BOX_100'
      menu    = ob_menu.
 
ENDFORM.                    "on_ctmenu_scr_right
Activate all objects and execute.
When you right click on box or anywhere within box Detail system calls form ON_CTMENU_BOX and within this form we have called context menu BOX_100.
sap context menu
However when you right click outside the box, context menu form ON_CTMENU_SCREEN is called and within this form we have called context menu SCR_100.




sap context menu
Serkan AKKAVAK
Computer Engineer / ABAP Developer
serkurumsal@yandex.com
You can ask me all your questions about SAP and Abap .