The SAP Toolbarcontrol
General
Addmethod
Add_button_groupmethod
Set_buttonstate method
Simpleexample
Advancedexample
General
See also Set up event handling for controls for a general example ofevent handling
Note: To get a list of all icons, use program SHOWICON.
Add method
Adds a new button to the toolbar
CALL METHOD go_toolbar->add_button
EXPORTING fcode ='EXIT' "Function Code for button
icon = icon_system_end "ICON name, You can use type pool ICON
is_disabled = '' "Disabled = X
butn_type = gc_button_normal "Type of button, see below
text = 'Exit' "Text on button
quickinfo = 'Exitprogram' "Quick info
is_checked = ''. "Button selected
Toolbar button types used in method ADD_BUTTON:
Value |
Constant |
Meaning |
0 |
cntb_btype_button |
Button (normal) |
1 |
cntb_btype_dropdown |
Pushbutton with menu |
2 |
cntb_btype_menu |
Menu |
3 |
cntb_btype_sep |
Seperator |
4 |
cntb_btype_group |
Pushbutton group |
5 |
cntb_btype_check |
Checkbox |
6 |
|
Menu entry |
Add_button_group method
This method is used to add a list of buttons to thetoolbar. The buttons are defined in a table of type TTB_BUTTON, and it can be filled witha button definitions using method fill_buttons_data_table of the cl_gui_toolbar class. The buttongroup is added to the toolbar using method add_button_group of the toolbar object.
* 1. Declare a table for buttons
DATA:gi_button_group TYPE ttb_button.
* 2. Create buttons in button table
CALL METHODcl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode ='Disable'
* icon =
* DISABLED =
butn_type = cntb_btype_group
* TEXT =
* QUICKINFO =
* CHECKED =
changing
data_table = gi_button_group
* EXCEPTIONS
* CNTB_BTYPE_ERROR = 1
* others = 2
.
CALL METHOD cl_gui_toolbar=>fill_buttons_data_table.....add more buttons to the table
*3. Add button group to toolbar
CALL METHOD go_toolbar->add_button_group
EXPORTING data_table =gi_button_group.
Set_button state method
Used to change the state of individual buttons atruntime. If the button should be removed, use the delete_button method.
CALL METHOD go_toolbar->set_button_state
EXPORTING
* ENABLED = 'X'
* CHECKED = ' '
fcode = "Note: This is the function code of the button that should be changed
* EXCEPTIONS
* CNTL_ERROR = 1
* CNTB_ERROR_FCODE = 2
* others = 3
.
Simple example
This example shows how to create a toolbar with a singleExit button, used to exit the program.
Steps:
Code:
REPORT sapmz_hf_toolbar .
TYPE-POOLS: icon.
CLASS cls_event_handler DEFINITION DEFERRED.
* G L O B AL D A T A
DATA:
ok_code LIKE sy-ucomm,
* Reference forconatiner
go_toolbar_container TYPE REF TOcl_gui_custom_container,
* Reference forSAP Toolbar
go_toolbar TYPE REF TO cl_gui_toolbar,
* Event handler
go_event_handler TYPE REF TO cls_event_handler.
* G L O B AL T A B L E S
DATA:
* Table forregistration of events. Note that a TYPE REF
* tocls_event_handler must be created before you can
* referencetypes cntl_simple_events and cntl_simple_event.
gi_events TYPE cntl_simple_events,
* Workspace fortable gi_events
g_event TYPE cntl_simple_event.
*---------------------------------------------------------------------*
* CLASS cls_event_handler DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS cls_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
on_function_selected
FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode,
on_dropdown_clicked
FOR EVENT dropdown_clicked OF cl_gui_toolbar
IMPORTING fcode posx posy.
ENDCLASS.
*---------------------------------------------------------------------*
* CLASS cls_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS cls_event_handler IMPLEMENTATION.
METHOD on_function_selected.
CASE fcode.
WHEN'EXIT'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMETHOD.
METHOD on_dropdown_clicked.
* Not implentedyet
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
SET SCREEN '100'.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
IF go_toolbar_container ISINITIAL.
* Createcontainer
CREATE OBJECTgo_toolbar_container
EXPORTING
container_name = 'TOOLBAR_CONTAINER'.
* Create toolbar
CREATE OBJECTgo_toolbar
EXPORTING
parent = go_toolbar_container.
* Add a button
CALL METHODgo_toolbar->add_button
EXPORTING fcode ='EXIT' "Function Code
icon = icon_system_end "ICON name
is_disabled = ' ' "Disabled = X
butn_type = cntb_btype_button "Typeof button
text = 'Exit' "Text on button
quickinfo = 'Exit program' "Quick info
is_checked = ' '. "Button selected
* Create eventtable. The event ID must be found in the
* documentationof the specific control
CLEAR g_event.
REFRESH gi_events.
g_event-eventid = go_toolbar->m_id_function_selected.
g_event-appl_event ='X'. "This is an application event
APPEND g_event TOgi_events.
g_event-eventid = go_toolbar->m_id_dropdown_clicked.
g_event-appl_event ='X'.
APPEND g_event TOgi_events.
* Use the events table to register events for the control
CALL METHODgo_toolbar->set_registered_events
EXPORTING
events = gi_events.
* Createevent handlers
CREATE OBJECTgo_event_handler.
SET HANDLERgo_event_handler->on_function_selected
FORgo_toolbar.
SET HANDLERgo_event_handler->on_dropdown_clicked
FOR go_toolbar.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
Advanced example
The toolbar in this example contains an Exit button, two buttons that enables/and disables a Print button, and a Menu button with a context menu.
The Disable/Enable buttons are of type Pushbutton group which makes them act together, so when one of thebuttons are selected (Down) the other is deselected (Up).
Note that the context menu for the Menu button, must becreated as a GUI status. You can create an empty GUI sttaus of type context,and then manually add menus. The context menu instance is created with typereference to class cl_ctmenu.
Steps:
The screen:
In this screen shot the Disable button isactivated which makes the Print button disabled:
This screenshot shows the context menu and sub menu:
The code:
REPORT sapmz_hf_toolbar .
TYPE-POOLS: icon.
CLASS cls_event_handler DEFINITION DEFERRED.
*--- G L O B AL D A T A
DATA:
ok_code LIKE sy-ucomm,
* Globalvarables for position of context menu
g_posx TYPE i,
g_posy TYPE i,
* Reference forconatiner
go_toolbar_container TYPE REF TOcl_gui_custom_container,
* Reference forSAP Toolbar
go_toolbar TYPE REF TO cl_gui_toolbar,
* Event handler
go_event_handler TYPE REF TO cls_event_handler,
* Context menu
go_context_menu TYPE REF TO cl_ctmenu.
*--- G L O B A L T A B L E S
DATA:
* Table forregistration of events. Note that a TYPE REF
* tocls_event_handler must be created before you can
* referencetypes cntl_simple_events and cntl_simple_event
gi_events TYPE cntl_simple_events,
* Workspace fortable gi_events
g_event TYPE cntl_simple_event,
* Table forbutton group
gi_button_group TYPE ttb_button.
*---------------------------------------------------------------------*
* CLASS CLS_EVENT_HANDLER
*---------------------------------------------------------------------*
* This classhandles the function_selected and dropdow_clicked events
* from thetoolbar
*---------------------------------------------------------------------*
CLASS cls_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
on_function_selected
FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode,
on_dropdown_clicked
FOR EVENT dropdown_clicked OF cl_gui_toolbar
IMPORTING fcode posx posy.
ENDCLASS.
CLASS cls_event_handler IMPLEMENTATION.
METHOD on_function_selected.
*-- Actions forbuttons and context menus
CASE fcode.
WHEN'EXIT'.
LEAVE TO SCREEN 0.
WHEN'ENABLE'.
* Enable the PRINT button
CALL METHOD go_toolbar->set_button_state
EXPORTING
enabled = 'X'
fcode = 'PRINT'.
WHEN'DISABLE'.
* Disable the PRINT button
CALL METHOD go_toolbar->set_button_state
EXPORTING
enabled = ' '
fcode = 'PRINT'.
* Other menus and context menus
WHEN'PRINT'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Printing'.
WHEN'CONTEXT1'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Menu: Do something funny'.
WHEN'CONTEXT2'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Menu: Do something crazy'.
WHEN'SUB1'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Submenu: Do something boring'.
WHEN'SUB2'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'Submenu: Do something good'.
ENDCASE.
ENDMETHOD.
METHOD on_dropdown_clicked.
*-- Fires when adropdown menu is clicked. After it has been
*-- clicked acontext menu is shown beside the button.
* Save x and y position of button for use with context menu
CLEAR: g_posx,g_posy.
g_posx = posx.
g_posy = posy.
* Create contextmenu for menu button
PERFORMcreate_context_menu.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
SET SCREEN '100'.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
IF go_toolbar_container ISINITIAL.
* Createcontainer
CREATE OBJECTgo_toolbar_container
EXPORTING
container_name = 'TOOLBAR_CONTAINER'.
* Create toolbar
CREATE OBJECTgo_toolbar
EXPORTING
parent = go_toolbar_container.
* Add a buttonto the toolbar
PERFORM add_button.
* Add a buttongroup to the toolbar
PERFORMadd_button_group.
* Create eventtable. Note that the event ID must be found in the
* documentationof the specific control
CLEAR g_event.REFRESH gi_events.
g_event-eventid = go_toolbar->m_id_function_selected.
g_event-appl_event ='X'. "This is an application event
APPEND g_event TOgi_events.
CLEAR g_event.
g_event-eventid =go_toolbar->m_id_dropdown_clicked.
g_event-appl_event ='X'.
APPEND g_event TOgi_events.
* Use the events table to register events for the control
CALL METHODgo_toolbar->set_registered_events
EXPORTING
events= gi_events.
* Createevent handlers
CREATE OBJECTgo_event_handler.
SET HANDLERgo_event_handler->on_function_selected
FORgo_toolbar.
SET HANDLERgo_event_handler->on_dropdown_clicked
FOR go_toolbar.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Form add_button
*&---------------------------------------------------------------------*
* Adds onepushbutton to the toolbar
*----------------------------------------------------------------------*
FORM add_button.
CALL METHODgo_toolbar->add_button
EXPORTINGfcode ='EXIT' "Function Code
icon = icon_system_end "ICON name
is_disabled = '' "Disabled = X
butn_type = cntb_btype_button "Type of button
text ='Exit' "Text on button
quickinfo = 'Exit program' "Quick info
is_checked = ''. "Button selected
ENDFORM. " add_button
*&---------------------------------------------------------------------*
*& Form add_button_group
*&---------------------------------------------------------------------*
* Adds a buttongroup to the toolbar.
* The buttonsare added to table gi_button_group, and the table is used
* as input tothe Add_button_group method. Note that method Fill_buttons
* is used tofill the table.
*----------------------------------------------------------------------*
FORM add_button_group.
* Add aseperator
CALL METHODcl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode ='SEP1'
icon =' '
butn_type = cntb_btype_sep
CHANGING
data_table = gi_button_group.
.
* Add an Enablebutton
CALL METHODcl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode ='ENABLE'
icon =' '
butn_type = cntb_btype_group
text ='Enable'
quickinfo = 'Enable a print button'
checked = 'X'
CHANGING
data_table = gi_button_group.
.
* Add a Disablebutton
CALL METHODcl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode ='DISABLE'
icon =''
butn_type = cntb_btype_group
text ='Disable'
quickinfo = 'Disable print button'
checked = ' '
CHANGING
data_table = gi_button_group.
* Add aseperator
CALL METHODcl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode ='SEP2'
icon =' '
butn_type = cntb_btype_sep
CHANGING
data_table = gi_button_group.
* Add printbutton
CALL METHODcl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'PRINT'
icon = icon_print
butn_type =cntb_btype_button
text = 'Print'
quickinfo = 'Printsomething'
CHANGING
data_table = gi_button_group.
* Add a menubutton
CALL METHODcl_gui_toolbar=>fill_buttons_data_table
EXPORTING
fcode = 'MENU'
icon = ' '
butn_type = cntb_btype_menu
text = 'Menu'
quickinfo = 'A menubuttonz'
CHANGING
data_table = gi_button_group.
* Add buttongroup to toolbar
CALL METHODgo_toolbar->add_button_group
EXPORTING data_table= gi_button_group.
ENDFORM. " add_button_group
*&---------------------------------------------------------------------*
*& Form create_context_menu
*&---------------------------------------------------------------------*
* This formcreates a context menu and a submenu for the menu button.
*----------------------------------------------------------------------*
FORM create_context_menu.
DATA: lo_submenu TYPE REF TOcl_ctmenu.
IF go_context_menu IS INITIAL.
*-- Createcontext menu
CREATE OBJECTgo_context_menu.
CALL METHODgo_context_menu->add_function
EXPORTING
fcode = 'CONTEXT1'
text = 'Dosomething funny'.
CALL METHOD go_context_menu->add_function
EXPORTING
fcode = 'CONTEXT2'
text = 'Do something crazy'.
CALL METHODgo_context_menu->add_separator.
* Create sub menu for the context menu
CREATE OBJECTlo_submenu.
CALL METHODlo_submenu->add_function
EXPORTING
fcode = 'SUB1'
text = 'Do something boring'.
CALL METHODlo_submenu->add_function
EXPORTING
fcode = 'SUB2'
text = 'Do something good'.
*-- Add sub menuto the context menu
CALL METHODgo_context_menu->add_submenu
EXPORTING
menu = lo_submenu
text = 'Do something else.....'.
ENDIF.
* Link menu to toolbarbutton. To position the context menu the
* x and ypositions of the menu button is used.
* These valueswas retrieved in the On_dropdown_clicked
* method ofcls_event_handler
CALL METHODgo_toolbar->track_context_menu
EXPORTING
context_menu = go_context_menu
posx = g_posx
posy = g_posy.
ENDFORM. " create_context_menu