[ABAP] Klasse für die Anzeige von Funktionbuttons im Selektionsbild

* Quelle: https://abapblog.com/articles/tricks/127-selection-screen-function-key-in-form-function-module-or-method

* Klasse greift auf Systemprogramm RSDBRUNT im Paket SLDBV zu
CLASS zcl_sel_button DEFINITION FINAL.
  PUBLIC SECTION.
    CLASS-METHODS: activate
      IMPORTING i_btn_no TYPE i.

    CLASS-METHODS: deactivate
      IMPORTING i_btn_no TYPE i.

    CLASS-METHODS: set_info
      IMPORTING i_btn_no    TYPE i
                i_text      TYPE gui_text OPTIONAL
                i_icon      TYPE icon_d OPTIONAL
                i_icon_text TYPE gui_ictext OPTIONAL
                i_quickinfo TYPE gui_info OPTIONAL.

  PRIVATE SECTION.
    CONSTANTS: co_current_screen TYPE string VALUE '(RSDBRUNT)CURRENT_SCREEN'.
    CONSTANTS: co_current_scr TYPE string VALUE '(RSDBRUNT)CURRENT_SCR'.

    CLASS-METHODS: set_visible
      IMPORTING i_btn_no  TYPE i
                i_visible TYPE abap_bool.
ENDCLASS.

CLASS zcl_sel_button IMPLEMENTATION.

* Button aktivieren
  METHOD activate.
    set_visible( i_btn_no  = i_btn_no
                 i_visible = abap_true  ).
  ENDMETHOD.

* Button deaktivieren
  METHOD deactivate.
    set_visible( i_btn_no  = i_btn_no
                 i_visible = abap_false ).
  ENDMETHOD.

* Button-Infos setzen
  METHOD set_info.

    IF i_btn_no BETWEEN 1 AND 5 AND NOT i_text IS INITIAL AND NOT i_icon IS INITIAL.

      DATA(sscr) = CONV string( |({ sy-cprog })SSCRFIELDS| ).

      ASSIGN (sscr) TO FIELD-SYMBOL(<sscr>).
      IF <sscr> IS ASSIGNED.
        ASSIGN COMPONENT |FUNCTXT_0{ i_btn_no }| OF STRUCTURE <sscr> TO FIELD-SYMBOL(<btn_text>).
        IF <btn_text> IS ASSIGNED.
          <btn_text> = VALUE smp_dyntxt( icon_id   = i_icon
                                         icon_text = i_icon_text
                                         text      = i_text
                                         quickinfo = i_quickinfo
                                       ).
        ENDIF.
      ENDIF.
    ENDIF.

  ENDMETHOD.

* Button Sichtbarkeit setzen
  METHOD set_visible.

    IF i_btn_no BETWEEN 1 AND 5.
* Funktionsbutton aktivieren/deaktivieren
      FIELD-SYMBOLS: <screen> TYPE sydb0_screen.

      ASSIGN (co_current_screen) TO <screen>.
      IF <screen> IS ASSIGNED.

        DATA(idx) = i_btn_no - 1.
        <screen>-func_keys+idx(1) = i_visible.

      ENDIF.

* Funktionsbutton anzeigen/verbergen
      FIELD-SYMBOLS: <scr> TYPE sydb0_scr_stack_line.

      ASSIGN (co_current_scr) TO <scr>.
      IF <scr> IS ASSIGNED.

        DATA(button) = |FC0{ i_btn_no }|.

        IF i_visible EQ abap_true.
          DELETE <scr>-excl WHERE fcode EQ button.
        ELSE.
          IF NOT line_exists( <scr>-excl[ fcode = button ] ).
            APPEND VALUE #( fcode = button ) TO <scr>-excl.
          ENDIF.
        ENDIF.

      ENDIF.

    ENDIF.

  ENDMETHOD.
ENDCLASS.

PARAMETERS: p_active AS CHECKBOX USER-COMMAND cba.

INITIALIZATION.
* zwei Funktions-Buttons einfügen
  zcl_sel_button=>set_info( EXPORTING
                              i_btn_no    = 1
                              i_text      = 'Text1'
                              i_icon      =  icon_abc
                              i_icon_text = 'Text1'
                              i_quickinfo = 'Info1'
                          ).
  zcl_sel_button=>activate( 1 ).

  zcl_sel_button=>set_info( EXPORTING
                              i_btn_no    = 2
                              i_text      = 'Text2'
                              i_icon      =  icon_activate
                              i_icon_text = 'Text2'
                              i_quickinfo = 'Info2'
                          ).
  zcl_sel_button=>activate( 2 ).

AT SELECTION-SCREEN OUTPUT.
* je nach Status der Checkbox p_active Funktionsbutton 3 ein- oder ausblenden
  IF p_active EQ abap_true.
    zcl_sel_button=>set_info( EXPORTING
                                i_btn_no    = 3
                                i_text      = 'Text3'
                                i_icon      =  icon_add_row
                                i_icon_text = 'Text3'
                                i_quickinfo = 'Info3'
                            ).

    zcl_sel_button=>activate( 3 ).
  ELSE.
    zcl_sel_button=>deactivate( 3 ).
  ENDIF.

AT SELECTION-SCREEN.
* Kommando abfangen (Nummerierung 'FC01' ... 'FC05')
  CASE sy-ucomm.
* wenn Funktionstaste 1 gedrückt
    WHEN 'FC01'.
      MESSAGE sy-ucomm TYPE 'S'.
* wenn Funktionstaste 2 gedrückt
    WHEN 'FC02'.
      MESSAGE sy-ucomm TYPE 'S'.
* wenn Funktionstaste 3 gedrückt
    WHEN 'FC03'.
      MESSAGE sy-ucomm TYPE 'S'.
  ENDCASE.