[ABAP] Eventhandling für die Klasse cl_gui_picture

* Demoprogramm: SAP_PICTURE_DEMO

* Eventhandlerklasse
CLASS lcl_events DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: on_picture_dblclick FOR EVENT picture_dblclick OF cl_gui_picture
      IMPORTING
          mouse_pos_x
          mouse_pos_y
          sender.

    CLASS-METHODS: on_create_context_menu FOR EVENT context_menu OF cl_gui_picture
      IMPORTING
          sender.

    CLASS-METHODS: on_context_menu_selection FOR EVENT context_menu_selected OF cl_gui_picture
      IMPORTING
          fcode
          sender.
ENDCLASS.

CLASS lcl_events IMPLEMENTATION.

  METHOD on_picture_dblclick.

    MESSAGE |{ mouse_pos_x }{ mouse_pos_y }| TYPE 'I'.

  ENDMETHOD.

  METHOD on_create_context_menu.
    DATA(o_menu) = NEW cl_ctmenu( ).

    o_menu->add_function( fcode = 'FUNC1' text = 'Normal' ).
    o_menu->add_function( fcode = 'FUNC2' text = 'Center' ).

    sender->display_context_menu( context_menu = o_menu ).
  ENDMETHOD.

  METHOD on_context_menu_selection.

    CASE fcode.
      WHEN 'FUNC1'.
        sender->set_display_mode( display_mode = cl_gui_picture=>display_mode_normal ).
      WHEN 'FUNC2'.
        sender->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).
    ENDCASE.

  ENDMETHOD.

ENDCLASS.

* Docking-Container für Bilddarstellung
DATA: o_dock TYPE REF TO cl_gui_docking_container.

* URL zur Google-API für die Erstellung des QR-Codes
PARAMETERS: p_url TYPE swk_url DEFAULT 'http://chart.apis.google.com/chart?chs=200x200&cht=qr&chld=|1&chl=viele%20lustige%20Zeichen/chart.png' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
    o_dock = NEW #( repid = sy-repid
                    dynnr = sy-dynnr
                    side  = cl_gui_docking_container=>dock_at_bottom
                    ratio = 30 ).

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).

    TRY.
* Eventhandler registrieren
        DATA: it_events TYPE cntl_simple_events.
        it_events = VALUE #( ( eventid = cl_gui_picture=>eventid_picture_dblclick
                               appl_event = abap_true )
                             ( eventid = cl_gui_picture=>eventid_context_menu
                               appl_event = abap_true )
                             ( eventid = cl_gui_picture=>eventid_context_menu_selected
                               appl_event = abap_true ) ).

        o_pic->set_registered_events( events = it_events ).

        SET HANDLER lcl_events=>on_picture_dblclick FOR o_pic.
        SET HANDLER lcl_events=>on_create_context_menu FOR o_pic.
        SET HANDLER lcl_events=>on_context_menu_selection FOR o_pic.

* Bild laden
        o_pic->load_picture_from_url_async( p_url ).

      CATCH cx_root INTO DATA(e).
        MESSAGE e->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
    ENDTRY.
  ENDIF.

[ABAP] Bilder und Icons von einem Webserver (URL) anzeigen

Variante 1 (load_picture_from_url_async)

* Docking-Container für Bilddarstellung
DATA: o_dock TYPE REF TO cl_gui_docking_container.

PARAMETERS: p_url TYPE char255 DEFAULT 'http://url.de/1234.jpg' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
    o_dock = NEW #( repid = sy-repid
                    dynnr = sy-dynnr
                    side  = cl_gui_docking_container=>dock_at_bottom
                    ratio = 30 ).

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).

    TRY.
        o_pic->load_picture_from_url_async( p_url ).
      CATCH cx_root INTO DATA(e).
        MESSAGE e->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
    ENDTRY.
  ENDIF.

Variante 2 (load_picture_from_url mit Fehlerbehandlung)

* Docking-Container für Bilddarstellung
DATA: o_dock TYPE REF TO cl_gui_docking_container.

PARAMETERS: p_url TYPE char255 DEFAULT 'http://url.de/1234.jpg' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
    o_dock = NEW #( repid = sy-repid
                    dynnr = sy-dynnr
                    side  = cl_gui_docking_container=>dock_at_bottom
                    ratio = 30 ).

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).
    TRY.
        DATA(lv_load_ok) = 0.

* Bild laden
        o_pic->load_picture_from_url( EXPORTING url = p_url IMPORTING result = lv_load_ok ).

* Flush ist wichtig für die Abarbeitung des GUI-Queues, sonst gibt es einen Core-Dump -> "SYSTEM_POINTER_PENDING"
        cl_gui_cfw=>flush( ).

        CASE lv_load_ok.
          WHEN 0.
* Fehler
            MESSAGE 'Bild nicht vorhanden.' TYPE 'S' DISPLAY LIKE 'E'.
          WHEN 1.
* Ok
            MESSAGE 'Bild konnte geladen werden.' TYPE 'S' DISPLAY LIKE 'I'.
        ENDCASE.
      CATCH cx_root INTO DATA(e).
        MESSAGE e->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
    ENDTRY.

  ENDIF.

Variante 3 (load_picture_from_sap_icons)

* Docking-Container für Bilddarstellung
DATA: o_dock TYPE REF TO cl_gui_docking_container.

PARAMETERS: p_url TYPE char255 DEFAULT 'http://url.de/1234.jpg' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
    o_dock = NEW #( repid = sy-repid
                    dynnr = sy-dynnr
                    side  = cl_gui_docking_container=>dock_at_bottom
                    ratio = 30 ).

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).

    TRY.
        o_pic->load_picture_from_sap_icons( icon_annotation ).
      CATCH cx_root INTO DATA(e).
        MESSAGE e->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
    ENDTRY.
  ENDIF.

[ABAP] Bild (JPEG) aus dem MIME-Repository laden und anzeigen

* Demo-Programm: SAP_PICTURE_DEMO
DATA: o_dock TYPE REF TO cl_gui_docking_container.

PARAMETERS: p_name TYPE string. " Dummy-Parameter

AT SELECTION-SCREEN OUTPUT.

  IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
    o_dock = NEW #( repid = sy-repid
                    dynnr = sy-dynnr
                    side  = cl_gui_docking_container=>dock_at_right
                    ratio = 50 ).

    DATA: lv_xstr TYPE xstring.

* MIME-API holen
    DATA(o_mime_rep) = cl_mime_repository_api=>get_api( ).

* Bild aus dem MIME-Repository laden
    o_mime_rep->get( EXPORTING
                       i_url = '/SAP/PUBLIC/Test123.jpg'
                       i_check_authority = abap_false
                     IMPORTING
                       e_content = lv_xstr ).

* xstring nach binary (RAW) wandeln
    DATA(it_bin_data) = cl_bcs_convert=>xstring_to_solix( lv_xstr ).

    DATA: lv_url TYPE swk_url.

* temporäre URL auf das Bild erzeugen
    CALL FUNCTION 'DP_CREATE_URL'
      EXPORTING
        type                 = 'image/jpeg' " https://wiki.selfhtml.org/wiki/MIME-Type/%C3%9Cbersicht
        subtype              = 'jpg'
      TABLES
        data                 = it_bin_data
      CHANGING
        url                  = lv_url
      EXCEPTIONS
        dp_invalid_parameter = 1
        dp_error_put_table   = 2
        dp_error_general     = 3
        OTHERS               = 4.

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).
    o_pic->load_picture_from_url_async( lv_url ).
  ENDIF.

Weiterführende Infos: Link