1. program bcalvc_print.
  2. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3. * Purpose:
  4. * ~~~~~~~~
  5. * This program illustrates how the events for print processing
  6. * - PRINT_TOP_OF_PAGE
  7. * - PRINT_END_OF_PAGE
  8. * - PRINT_TOP_OF_LIST
  9. * - PRINT_END_OF_LIST
  10. *
  11. * are handled. The corresponding handler methods control the
  12. * appearance of the list printed.
  13. *-----------------------------------------------------------------
  14. * To check program behavior
  15. * ~~~~~~~~~~~~~~~~~~~~~~~~~
  16. * Print the list shown (It has got only three pages).
  17. * Remark: If you choose "Druckansicht" (preview?!) before printing,
  18. * the output for event PRINT_END_OF_PAGE is left out due
  19. * to scrolling.
  20. * Create a spool entry and preview your printout by calling
  21. * TA sp01 to reduce paper output please.
  22. *-----------------------------------------------------------------
  23. * Essential steps (Search for '§')
  24. * ~~~~~~~~~~~~~~~
  25. * 1. Define a (local) class for event handling
  26. * 2. Define a method for each print event you need.
  27. * 3. Implement your event handler methods. Use WRITE to provide output.
  28. * 4. Link used print events and event handler methods.
  29. * 5. In case of PRINT_END_OF_PAGE, you must set 'reservelns' to
  30. * the number of reserved lines at the end of a page.
  31. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

  32. *********
  33. * Predefine a local class for event handling to allow the
  34. * declaration of a reference variable.
  35. class lcl_event_receiver definition deferred.
  36. *
  37. *********


  38. data: ok_code like sy-ucomm,
  39.       g_max type i value 100,
  40.       gt_sflight type table of sflight,
  41.       g_repid like sy-repid,
  42.       gs_print type lvc_s_prnt,
  43.       gs_layout type lvc_s_layo,
  44.       mycontainer type scrfname value 'BCALVC_EVENT1_CONT1',
  45. * reference to custom container: neccessary to bind ALV Control
  46.   custom_container type ref to cl_gui_custom_container,
  47.       grid1 type ref to cl_gui_alv_grid,
  48.       event_receiver type ref to lcl_event_receiver.


  49. * § Step 1. Define a (local) class for event handling
  50. ****************************************************************
  51. * LOCAL CLASSES: Definition
  52. ****************************************************************
  53. *===============================================================
  54. * class c_event_receiver: local class to handle print events...
  55. * - PRINT_TOP_OF_PAGE (page header)
  56. * - PRINT_END_OF_PAGE (page footer)
  57. * - PRINT_TOP_OF_LIST (list header)
  58. * - PRINT_END_OF_LIST (list footer)
  59. *
  60. * Definition:
  61. * ~~~~~~~~~~~
  62. class lcl_event_receiver definition.

  63.   public section.
  64. * § 2. Define a method for each print event you need.
  65.     methods:
  66.     handle_top_of_page
  67.         for event print_top_of_page of cl_gui_alv_grid,

  68.     handle_end_of_page
  69.         for event print_end_of_page of cl_gui_alv_grid,

  70.     handle_top_of_list
  71.         for event print_top_of_list of cl_gui_alv_grid,

  72.     handle_end_of_list
  73.         for event print_end_of_list of cl_gui_alv_grid.

  74.   private section.
  75.     data: pagenum type i.

  76. endclass.
  77. *
  78. * c_event_receiver (Definition)
  79. *===============================================================


  80. ****************************************************************
  81. * LOCAL CLASSES: Implementation
  82. ****************************************************************
  83. *===============================================================
  84. * class c_event_receiver (Implementation)
  85. *
  86. class lcl_event_receiver implementation.
  87. *§ 3. Implement your event handler methods. Use WRITE to provide output.
  88.   method handle_top_of_page.
  89.     data: tablename(30) type c.
  90.     perform get_tablename changing tablename.
  91.     write: /,'Event: PRINT_TOP_OF_PAGE'(001),
  92.              'Table: '(002),tablename.

  93.   endmethod. "handle_top_of_page
  94. *-------------------------------------------
  95.   method handle_end_of_page.
  96.     data: tablename(30) type c.

  97.     perform get_tablename changing tablename.
  98.     add 1 to pagenum.
  99.     write: /,'Event: PRINT_END_OF_PAGE'(003),
  100.              text-002,tablename,
  101.              'Number of pages so far: '(004), pagenum.

  102.   endmethod. "handle_end_of_page
  103. *-------------------------------------------
  104.   method handle_top_of_list.
  105.     data: tablename(30) type c.
  106.     clear pagenum.
  107.     perform get_tablename changing tablename.
  108.     write: /,'Event: PRINT_TOP_OF_LIST'(005),
  109.              text-002,tablename.

  110.   endmethod. "handle_top_of_list
  111. *-------------------------------------------
  112.   method handle_end_of_list.
  113.     data: tablename(30) type c.
  114.     perform get_tablename changing tablename.
  115.     write: /,'Event: PRINT_END_OF_LIST'(006),
  116.              text-002,tablename.

  117.   endmethod. "handle_end_of_list
  118. *-------------------------------------------
  119. endclass.
  120. *
  121. * c_event_receiver (Implementation)
  122. *===================================================================

  123. start-of-selection.
  124.   select * from sflight into table gt_sflight up to g_max rows.
  125. *
  126. end-of-selection.
  127.   g_repid = sy-repid.
  128.   call screen 100.

  129. *---------------------------------------------------------------------*
  130. * MODULE PBO OUTPUT *
  131. *---------------------------------------------------------------------*
  132. module pbo output.
  133.   set pf-status 'MAIN100'.
  134.   set titlebar 'MAIN100'.
  135.   if custom_container is initial.
  136. * create a custom container control for our ALV Control
  137.     create object custom_container
  138.         exporting
  139.             container_name = mycontainer
  140.         exceptions
  141.             cntl_error = 1
  142.             cntl_system_error = 2
  143.             create_error = 3
  144.             lifetime_error = 4
  145.             lifetime_dynpro_dynpro_link = 5.
  146.     if sy-subrc ne 0.
  147. * add your handling, for example
  148.       call function 'POPUP_TO_INFORM'
  149.            exporting
  150.                 titel = g_repid
  151.                 txt2 = sy-subrc
  152.                 txt1 = 'The control could not be created'(010).
  153.     endif.
  154. * create an instance of alv control
  155.     create object grid1
  156.           exporting i_parent = custom_container.
  157. *
  158. * Set a titlebar for the grid control
  159. *
  160.     gs_layout-grid_title = 'Flights'(100).

  161. * § 5. In case of PRINT_END_OF_PAGE, you must set 'reservelns' to
  162. * the number of reserved lines at the end of a page.
  163. *
  164. * reserve two lines for the PRINT_END_OF_PAGE event
  165. *
  166.     gs_print-reservelns = 2.


  167. ********
  168. * ->Create Object to receive events and link them to handler methods.
  169. * When the ALV Control raises the event for the specified instance
  170. * the corresponding method is automatically called.
  171. *

  172. ********

  173. * § 4. Link used print events and event handler methods.
  174.     create object event_receiver.
  175.     set handler event_receiver->handle_top_of_list for grid1.
  176.     set handler event_receiver->handle_top_of_page for grid1.
  177.     set handler event_receiver->handle_end_of_list for grid1.
  178.     set handler event_receiver->handle_end_of_page for grid1.
  179. *
  180.   call method grid1->set_table_for_first_display
  181.          exporting i_structure_name = 'SFLIGHT'
  182.                    is_print = gs_print
  183.                    is_layout = gs_layout
  184.          changing it_outtab = gt_sflight.

  185.   endif.
  186. * Controls are not integrated into the TAB-Order
  187. * Call "set_focus" if you want to make sure that 'the cursor'
  188. * is active in your control.
  189.   call method cl_gui_control=>set_focus exporting control = grid1.

  190. * Control Framework flushes at the end of PBO
  191. endmodule.
  192. *---------------------------------------------------------------------*
  193. * MODULE PAI INPUT *
  194. *---------------------------------------------------------------------*
  195. module pai input.
  196.   case ok_code.
  197.     when 'EXIT'.
  198.       perform exit_program.
  199.   endcase.
  200.   clear ok_code.
  201. endmodule.
  202. *---------------------------------------------------------------------*
  203. * FORM EXIT_PROGRAM *
  204. *---------------------------------------------------------------------*
  205. form exit_program.
  206.   call method custom_container->free.
  207.   call method cl_gui_cfw=>flush.
  208.   if sy-subrc ne 0.
  209. * add your handling, for example
  210.     call function 'POPUP_TO_INFORM'
  211.          exporting
  212.               titel = g_repid
  213.               txt2 = sy-subrc
  214.               txt1 = 'Error in Flush'(009).
  215.   endif.
  216.   leave program.
  217. endform.
  218. *&---------------------------------------------------------------------*
  219. *& Form GET_TABLENAME
  220. *&---------------------------------------------------------------------*
  221. * text
  222. *----------------------------------------------------------------------*
  223. * <--P_TABLENAME text
  224. *----------------------------------------------------------------------*
  225. form get_tablename changing p_tablename.
  226.   data: lt_fieldcat type standard table of lvc_s_fcat,
  227.         ls_fieldcat type lvc_s_fcat.

  228.   call method grid1->get_frontend_fieldcatalog
  229.                 importing et_fieldcatalog = lt_fieldcat.


  230.   call method cl_gui_cfw=>flush.
  231.   if sy-subrc <> 0.
  232.     p_tablename = 'No tablename in fieldcatalog!'(008).
  233.     call function 'POPUP_TO_INFORM'
  234.          exporting
  235.               titel = g_repid
  236.               txt2 = p_tablename
  237.               txt1 = 'Error in Flush'(011).
  238.   else.
  239.     read table lt_fieldcat index 1 into ls_fieldcat.
  240.     p_tablename = ls_fieldcat-ref_table.
  241.   endif.

  242. endform.

10-12 18:43