Entering content frameContext Menus Locate the document in its SAP Library structure

The user interface of a screen is defined by a GUI status, which you define in the Menu Painter and assign the type Dialog status. 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 (or choosing Shift+F10 ). The standard context menu contains all of the function keys to which functions are assigned. 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, you can define context-specific menus for any of the following screen elements:

When you select one of these elements using the right-hand mouse button, you can create a dynamic context menu in the ABAP program. This may contain any functions, and is not restricted to function keys. You cannot assign context menus to pushbuttons, checkboxes, or radio buttons. However, you can assign unique function codes to them instead.

Global Class CL_CTMENU

Context menus are objects of the global ABAP Objects class CL_CTMENU. In the class library, the class belongs to the Frontend Services component, in which the classes of the Control Framework are also stored (see Custom Controls). It contains methods that allow you to define context menus dynamically in a program. As a template, you can create your own context menus statically in the Menu Painter.

The most important methods of the class CL_CTMENU are:

Method

Function

LOAD_GUI_STATUS

Assigns a context menu that has already been defined statically in the Menu Painter to a local context menu in a program

ADD_FUNCTION

Assigns a single function to a context menu in a program

ADD_MENU

Assigns another local context menu to the current local context menu in the program

ADD_SUBMENU

Assigns another local context menu to the current local context menu in the 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

Sets a default function, which is highlighted when the menu is displayed

In the above table, "local context menu in a program" means an object of class CL_CTMENU. You use CL_CTMENU in different ways depending on the context:

If you use a context menu in a control, whether or not you 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 handling of the control. For further information, refer to the documentation of the individual control classes.

When you define a context menu on a screen (or list), the relevant objects of class CL_CTMENU are created automatically by the runtime environment, 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 Screens

In order to link a context menu with one of the screen elements above, you only need to enter an ID <context> in the Context menu field in the element attributes in the Screen Painter. If you do not define a context menu for a particular screen element, it inherits the context menu from the hierarchically next-highest element. 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 linked with a context menu – either its own 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. The PAI event is not triggered. You use this subroutine (callback routine) to define the context menu dynamically. You must program it in the processing logic. If the subroutine does not exist, the context menu is not displayed.

You can link the same context menu <context> to any number of screen elements. They then all work with the same subroutine.

Defining Context Menus in the Processing Logic

For each context menu that you want to call for an element on a screen, you need to program a corresponding callback routine:

FORM ON_CTMENU_<context> USING <l_menu> TYPE REF TO cl_ctmenu.
  ...
ENDFORM.

Each routine 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 clicking the right-hand mouse button, 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 – the context menu contains no entries. In the subroutine, you can work with the methods of the object (as listed above) 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 you can define in the Menu Painter, namely a context menu. To find out how to create context menus in the Menu Painter, refer to Structure link Creating Context Menus.

Predefined context menus allow you to make groups of statically-defined function coded available context-specifically. The method LOAD_GUI_STATUS allows you to load a context menu from any ABAP program into a local context menu in a program. As a rule, you use predefined context menus to reuse the function codes from a dialog status with the same semantics, but context-specifically. Once you have loaded a predefined context menu into a local context menu in a program, you can modify it in any way (append other predefined context menus, add or remove functions, add other context menus).

Defining New Context Menus

You can create new context-specific menus either by modifying existing ones or by constructing new menus.

You can add any number of new functions to a context menu. When you add a new function, you must specify the function text, function code, and function type (for example, E for an unconditional module call).

However, you can also add any other local context menu from the program. In this case, you only have to pass a reference to another context menu (see example below). You can create a collection of context menu objects in your program and use and combine them as necessary. You can also construct submenus. You can have deep-nested menus by adding submenus to existing submenus.

Ergonomic Guidelines

When you create context menus, you should observe the following rules:

Displaying the Context Menu

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.

Example

The following example shows some of the technical possibilities for creating context menus, but does not necessarily observe all of the style guidelines.

Example

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 next screen (statically defined) for screen 100 is itself. It has the following layout:

This graphic is explained in the accompanying text

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 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.

 

 

 

Leaving content frame