Coding Example

This example is intended to accompany the section Using the SAP Toolbar Control. It contains the necessary ABAP coding extracts for creating an instance of the toolbar. However, it contains no other processing logic, and is therefore not a working program.

The example produces the following toolbar:

The Expand and Collapse buttons each have a statically-assigned dropdown menu (implemented using a context menu). The Details button is a normal pushbutton, and Active/Inactive is a toggle button.

First Data Declarations

* Object references
DATA:   toolbar TYPE REF TO cl_gui_toolbar,
        ct_expand TYPE REF TO cl_ctmenu
        ct_collapse TYPE REF TO cl_ctmenu.

* Data structures for passing data to toolbar 
DATA:   buttons_data TYPE ttb_button,
        button_data TYPE LINE OF ttb_button,
        table_ctxmenu TYPE ttb_btnmnu,
        wa_ctxmenu TYPE LINE OF ttb_btnmnu.

* Data structures for events
DATA:   events TYPE cntl_simple_events,
        event TYPE cntl_simple_event.

2. Event Handler Class

CLASS lcl_toolbar_events DEFINITION.

* Here, you define instance methods or
* static methods to handle the events
* of the toolbar.

CLASS-METHODS: on_function_selected
        FOR EVENT function_selected OF cl_gui_toolbar
        IMPORTING fcode = fcode.
ENDCLASS.
CLASS lcl_toolbar_events IMPLEMENTATION.
METHOD on_function_selected.

* Implementation of the method. It has the parameter
* FCODE that you can use to work out which function was
* chosen

ENDMETHOD.
ENDCLASS.

3. Creating and Filling the Toolbar

CREATE OBJECT toolbar EXPORTING parent = parent.

* Information for first button
button_data-function = 'EXPAND'.
button_data-icon = '@3S@'.
button_data-quickinfo = text-001.
button_data-text = text-002.
button_data-butn_type = cntb_btype_menu.
append button_data to buttons_data.

* Repeat for other buttons

* Add buttons to toolbar
CALL METHOD toolbar->add_button_group
        EXPORTING data_table = buttons_data.

4. Creating and Filling the Dropdown Menus

CREATE OBJECT ct_expand.
* Use this syntax to add functions to the context menu
CALL METHOD ct_expand->add_function
        EXPORTING fcode = <fcode>
                text = <text>.
wa_ctxmenu-function = 'EXPAND'.
wa_ctxmenu-ctmenu = ct_expand.

APPEND wa_ctxmenu TO table_ctxmenu

* Repeat for other context menu
* Add dropdown context menus to toolbar
CALL METHOD toolbar->assign_static_ctxmenu_table
        EXPORTING table_ctxmenu = table_ctxmenu.

5. Registering the events

event-eventid = cl_gui_toolbar=>m_id_function_selected.
event-appl_event = ' '.
append event to events.

CALL METHOD toolbar->set_registered_events
        EXPORTING events = events.

SET HANDLER lcl_toolbar_handler=>on_function_selected for toolbar.