home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l360 / 3.ddi / PMHELLO.@BL / PMHELLO.CBL
Encoding:
Text File  |  1991-04-08  |  19.0 KB  |  436 lines

  1.       $set ans85 noosvs mf
  2.       ****************************************************************
  3.       *                                  *
  4.       *                                  *
  5.       *                (C) Micro Focus Ltd. 1989,1990                *
  6.       *                                  *
  7.       *               PMHELLO.CBL                 *
  8.       *                                  *
  9.       * Example program: Presentation Manager 'Hello World'         *
  10.       *                                  *
  11.       * To compile, link and run:                                    *
  12.       *                                                              *
  13.       *     cobol pmhello linklib"coblib+os2";                       *
  14.       *     link pmhello;                                            *
  15.       *     pmhello                                                  *
  16.       *                                                              *
  17.       * The LINKLIB directive to the compiler may be omitted if      *
  18.       * it is included in your COBOL.DIR. If it is not specified     *
  19.       * at compile time, then the link step becomes:                 *
  20.       *                                                              *
  21.       *     link pmhello,,,coblib+os2;                               *
  22.       *                                                              *
  23.       * Note that OS2.LIB is required for this program. This module  *
  24.       * is part of the OS/2 Software Developer's Toolkit, and is     *
  25.       * also part of the Microsoft Utilities.                        *
  26.       ****************************************************************
  27.       *                                  *
  28.       * Presentation Manager Programming                 *
  29.       *                                  *
  30.       * Please refer to printed and online documentation for more    *
  31.       * information on PM Presentation Manager programming with      *
  32.       * COBOL.                                                       *
  33.       *                                  *
  34.       ****************************************************************
  35.       *                                  *
  36.       * About PMHELLO                             *
  37.       *                                  *
  38.       * A number of extensions to the COBOL language are used in     *
  39.       * this program, and are noted in comments where they occur.    *
  40.       * See the documentation and release notes for a full           *
  41.       * description of PM Programming facilities for COBOL.          *
  42.       *                                  *
  43.       * This release of COBOL provides some prototype Systems         *
  44.       * Programming Facilities which enable among other things the   *
  45.       * COBOL programmer to utilize Presentation Manager.         *
  46.       *                                  *
  47.       * To write your own PM programs in COBOL, we recommend that    *
  48.       * you use this program as a base.                  *
  49.       *                                  *
  50.       ****************************************************************
  51.  
  52.       ****************************************************************
  53.       *                                  *
  54.       * COBOL Extension: Special-names.                  *
  55.       *                                  *
  56.       *     Call-conventions are supported as below.                 *
  57.       *                                  *
  58.       *     The meaning of the numbers is derived from decomposing   *
  59.       *     the number into binary components, with bits having      *
  60.       *     the following meanings:                     *
  61.       *                                  *
  62.       *     0    -   no bits specified means that the standard         *
  63.       *         COBOL Calling conventions are             *
  64.       *         employed.  This means parameters are passed      *
  65.       *         on a stack, last named is first pushed on the    *
  66.       *         stack.  The parameters are removed from the      *
  67.       *             stack by the CALLing program.                    *
  68.       *         Use this for compatibility with existing COBOL   *
  69.       *         programs.                         *
  70.       *     1    -   parameters are passed on a stack, first named    *
  71.       *         is first pushed.  So you could call this         *
  72.       *             convention 'REVERSED'.                           *
  73.       *     2    -   The parameters are removed from the stack         *
  74.       *             by the called routine.                           *
  75.       *                                  *
  76.       *                                  *
  77.       *     So, we get the 'OS2API' convention used by PM as         *
  78.       *     convention 3. This convention is alternatively known as  *
  79.       *     the PASCAL calling convention.                 *
  80.       *                                  *
  81.       ****************************************************************
  82.     special-names.
  83.         call-convention 3 is OS2API.
  84.  
  85.     working-storage section.
  86.  
  87.       ****************************************************************
  88.       *                                  *
  89.       * PM Toolkit supplies a number of C header files which define  *
  90.       * constants.  In COBOL you may use the H2CPY utility to create *
  91.       * the COBOL datanames and PICTURE clauses from their C         *
  92.       * equivalents. That is what has been done in PMHELLO.          *
  93.       * For more information on the H2CPY utility, please see the    *
  94.       * various printed and on-line documentation files.             *
  95.       *                                                              *
  96.       * In this program, we are using the Wm-Paint message,          *
  97.       * the System Background Color Sysclr-Window and the            *
  98.       * System default window text color Sysclr-WindowText.          *
  99.       * To translate values from C constants to COBOL constants,     *
  100.       * H2CPY uses the following rules:                              *
  101.       *                                  *
  102.       *             C        COBOL                 *
  103.       *     Hexadecimal   0xnn        h"nn"                 *
  104.       *     Decimal        nn          nn                 *
  105.       *                                  *
  106.       ****************************************************************
  107.  
  108.         78  Wm-Paint                VALUE   H"23".
  109.         78  Sysclr-Window           VALUE   -20.
  110.         78  Sysclr-WindowText       VALUE   -17.
  111.  
  112.     01  work-data.
  113.       ****************************************************************
  114.       *                                  *
  115.       * The supplied C header files define data types for all the    *
  116.       * PM data items.  For COBOL we have to use the COBOL data      *
  117.       * types.                                                       *
  118.       *                                  *
  119.       * As a general conversion rule:                     *
  120.       *                                  *
  121.       *     'C'     COBOL                         *
  122.       *     SHORT    PIC S9(4) COMP-5                 *
  123.       *     USHORT    PIC 9(4)  COMP-5                 *
  124.       *     LONG    PIC S9(9) COMP-5                 *
  125.       *     ULONG    PIC 9(9)  COMP-5                 *
  126.       *     PVOID    POINTER         (similarly for other     *
  127.       *                     pointer types)         *
  128.       *     LHANDLE     PIC 9(9)  COMP-5    (These are equivalent    *
  129.       *     LHANDLE     POINTER             for PM working)          *
  130.       *                                                              *
  131.       *                         LHANDLE is used for any 32 bit       *
  132.       *             handle, eg HAB, HMQ, HPS etc.         *
  133.       *                                  *
  134.       *     NB    PIC 9(4) COMP-5 is identical to PIC X(2) COMP-5      *
  135.       *     NB    PIC 9(9) COMP-5 is identical to PIC X(4) COMP-5      *
  136.       *                                  *
  137.       ****************************************************************
  138.             03  Hab                 POINTER.
  139.             03  Hmq                 POINTER.
  140.             03  HwndClient          PIC 9(9) comp-5.
  141.             03  HwndFrame           PIC 9(9) comp-5.
  142.  
  143.       ****************************************************************
  144.       *                                  *
  145.       * As an alternative to using the SIZE clause in the CALL         *
  146.       * statements, we can define data items with the correct         *
  147.       * size and use that.                         *
  148.       *                                  *
  149.       ****************************************************************
  150.             03  Hwnd-Desktop        PIC 9(9) COMP-5 VALUE 1.
  151.  
  152.       ****************************************************************
  153.       *                                  *
  154.       * Class styles are defined in the header files. H2CPY will     *
  155.       * give the appropriate numbers. Note that Fcf-ctl-data is      *
  156.       * set to a total of H"00000C3B" for    ... Fcf-Tasklist        *
  157.       *                                     with Fcf-Shellposition   *
  158.       *                                      and Fcf-Minmax          *
  159.       *                                      and Fcf-Sizeborder      *
  160.       *                                      and Fcf-Sysmenu         *
  161.       *                                      and Fcf-Titlebar        *
  162.       *                                  *
  163.       ****************************************************************
  164.             03  CS-Sizeredraw    PIC 9(9) COMP-5   VALUE H"04".
  165.             03  WS-Visible       PIC 9(9) COMP-5   VALUE H"80000000".
  166.             03  Fcf-ctl-data     PIC 9(9) COMP-5   VALUE H"00000c3b".
  167.  
  168.       ****************************************************************
  169.       *                                  *
  170.       * ASCIIZ strings are not natural with COBOL, and in particular *
  171.       * are not suitable for use as literals.                 *
  172.       * Where ASCIIZ strings are used, they must be declared in      *
  173.       * Working-Storage and followed by a x"00" NULL terminator.     *
  174.       * We use the literal concatenation '&' operator.               *
  175.       *                                  *
  176.       ****************************************************************
  177.             03  MyClass             pic x(9) value 'MyClass' & x'00'.
  178.  
  179.         03    loop-flag        pic x value 'C'.
  180.         88  loop-end        value 'E'.
  181.         03    bool            pic 9(4) comp-5.
  182.         88  boolTRUE        value 1.
  183.         88  boolFALSE        value 0.
  184.  
  185.       ****************************************************************
  186.       *                                  *
  187.       * Structures are supplied in C header files. H2CPY will        *
  188.       * convert them to COBOL format.                                *
  189.       * Below is a QMSG structure, and in LOCAL-STORAGE section      *
  190.       * are examples of RECTL and POINTL structures.             *
  191.       *                                  *
  192.       ****************************************************************
  193.         03    qmsg.
  194.         05  qmsghwnd        pic 9(9) comp-5.
  195.         05  qmsgmsg        pic 9(4) comp-5.
  196.         05  qmsgmp1        pic 9(9) comp-5.
  197.         05  qmsgmp2        pic 9(9) comp-5.
  198.         05  qmsgtime        pic 9(9) comp-5.
  199.         05  qmsgptl.
  200.             07    qmsgptlx    pic 9(9) comp-5.
  201.             07    qmsgptly    pic 9(9) comp-5.
  202.  
  203.       ****************************************************************
  204.       *                                  *
  205.       * COBOL Extension: Procedure-pointers                 *
  206.       *                                  *
  207.       *     Data pointers are now complemented by procedure pointers *
  208.       *                                  *
  209.       ****************************************************************
  210.         03    WndProc     procedure-pointer.
  211.  
  212.       ****************************************************************
  213.       *                                  *
  214.       * COBOL Extension: Local-Storage Section.              *
  215.       * COBOL Extension: Recursion                     *
  216.       *                                  *
  217.       *     Any data declared in the LOCAL-STORAGE SECTION is         *
  218.       *     created freshly for each instance of the program.         *
  219.       *     This data cannot currently be initialized.               *
  220.       *                                  *
  221.       ****************************************************************
  222.     local-storage section.
  223.         01  hps         pointer.
  224.         01 Rectl.
  225.            05 Rectl-Xleft                    PIC S9(9) COMP-5.
  226.            05 Rectl-Ybottom                  PIC S9(9) COMP-5.
  227.            05 Rectl-Xright                   PIC S9(9) COMP-5.
  228.            05 Rectl-Ytop                     PIC S9(9) COMP-5.
  229.  
  230.         01  Mresult     pic x(4) comp-5.
  231.  
  232.     linkage section.
  233.         01  hwnd        pointer.
  234.         01  msg         pic x(2) comp-5.
  235.         01  mp1         pic x(4) comp-5.
  236.     01  redefines mp1.
  237.             03  mp1w1   pic x(2) comp-5.
  238.             03  mp1w2   pic x(2) comp-5.
  239.         01  mp2         pic x(4) comp-5.
  240.     01  redefines mp2.
  241.             03  mp2w1   pic x(2) comp-5.
  242.             03  mp2w2   pic x(2) comp-5.
  243.  
  244.       ****************************************************************
  245.       *                                  *
  246.       * COBOL Extension: Call-conventions                 *
  247.       *                                  *
  248.       *     This use of the call-convention OS2API (declared above   *
  249.       *     in special-names) means that all the entry points in     *
  250.       *     this program follow the OS2API calling convention unless *
  251.       *     they specify otherwise                                   *
  252.       *                                  *
  253.       ****************************************************************
  254.     procedure division OS2API.
  255.     main section.
  256.  
  257.       ****************************************************************
  258.       *                                  *
  259.       * COBOL Extension: Call-conventions                 *
  260.       * COBOL Extension: SIZE clause                     *
  261.       * COBOL Extension: RETURNING phrase                 *
  262.       *                                  *
  263.       *     This use of the call-convention OS2API (declared above   *
  264.       *     in special-names) means that the target procedure         *
  265.       *     follows the OS2API calling convention.             *
  266.       *                                  *
  267.       *     Passing parameters by value allows explicit sizing.      *
  268.       *     This is to enable distinction between 2 and 4 byte         *
  269.       *     literals.                             *
  270.       *                                  *
  271.       *     The returning phrase has been added to avoid complicated *
  272.       *     and clumsy use of the RETURN-CODE special register.      *
  273.       *                                  *
  274.       ****************************************************************
  275.         call OS2API '__WinInitialize'
  276.             using    by value 0 size 2
  277.             returning hab
  278.         call OS2API '__WinCreateMsgQueue'
  279.             using by value hab
  280.                   by value 0 size 2
  281.             returning hmq
  282.  
  283.       ****************************************************************
  284.       *                                  *
  285.       * COBOL Extension: Procedure-pointers                 *
  286.       *                                  *
  287.       *     Procedure pointers can be set to point to an entry         *
  288.       *     point.  The entry point must be valid to be called         *
  289.       *     at this point in the program.                 *
  290.       *                                  *
  291.       ****************************************************************
  292.         set WndProc to ENTRY 'WndProc'
  293.         call OS2API '__WinRegisterClass'
  294.             using by value       hab
  295.                   by reference MyClass
  296.                   by value       WndProc
  297.                               by value     Cs-Sizeredraw
  298.                   by value       0        size 2
  299.             returning bool
  300.         if boolTRUE
  301.  
  302.         call OS2API '__WinCreateStdWindow'
  303.                 using by value     HWND-DESKTOP
  304.                                   by value     Ws-Visible
  305.                                   by reference Fcf-ctl-data
  306.                   by reference MyClass
  307.                                   by reference 'MyTitle' & x'00'
  308.                   by value     0    size 4
  309.                   by value     0    size 2
  310.                   by value     0    size 2
  311.                   by reference hwndClient
  312.                 returning hwndFrame
  313.  
  314.         if hwndFrame not = 0
  315.  
  316.       ****************************************************************
  317.       *                                  *
  318.       * This in-line PERFORM implements the message loop.         *
  319.       *                                  *
  320.       ****************************************************************
  321.             perform until loop-end
  322.             call OS2API '__WinGetMsg'
  323.                     using by value hab
  324.                       by reference qmsg
  325.                       by value 0        size 4
  326.                       by value 0        size 2
  327.                       by value 0        size 2
  328.                     returning bool
  329.  
  330.             if boolFALSE
  331.                 set loop-end to true
  332.             else
  333.                 call OS2API '__WinDispatchMsg'
  334.                     using by value hab
  335.                           by reference qmsg
  336.  
  337.             end-perform
  338.  
  339.             call OS2API '__WinDestroyWindow'
  340.                 using by value hwndFrame
  341.  
  342.         end-if
  343.  
  344.         end-if
  345.  
  346.         call OS2API '__WinDestroyMsgQueue' using by value hmq
  347.         call OS2API '__WinTerminate'       using by value hab
  348.  
  349.         stop run.
  350.  
  351.     MyWndProc section.
  352.       ****************************************************************
  353.       *                                  *
  354.       * COBOL Extension: ENTRY USING BY VALUE                 *
  355.       * COBOL Extension: Recursion                     *
  356.       *                                  *
  357.       *     To complement the CALL USING BY VALUE, we now allow      *
  358.       *     ENTRY USING BY VALUE.                     *
  359.       *                                  *
  360.       *     COBOL being recursive means that the call to         *
  361.       *     WinCreateStdWindow (above) can lead to control being     *
  362.       *     passed to this entry point.                  *
  363.       *     In fact, any of the calls in this section could lead     *
  364.       *     to control being passed to a new instance of this         *
  365.       *     entry point (hence the need for LOCAL-STORAGE SECTION.)  *
  366.       *                                  *
  367.       ****************************************************************
  368.     entry 'WndProc' using by value hwnd
  369.                   by value msg
  370.                   by value mp1
  371.                   by value mp2.
  372.  
  373.             move 0 to mresult
  374.         evaluate msg
  375.  
  376.       ****************************************************************
  377.       *                                  *
  378.       * The only message we are interested in is the PAINT message   *
  379.       * The sequence of actions is:                     *
  380.       *                                  *
  381.       *     Get Handle-To-Presentation-Space (HPS) for painting      *
  382.       *             in the client window             *
  383.       *     Fill the window with the System Background colour         *
  384.       *     Write the words 'Hello COBOL World' at position (20,20)  *
  385.       *     Release the HPS.                         *
  386.       *                                  *
  387.       ****************************************************************
  388.                 when    Wm-Paint
  389.             call OS2API '__WinBeginPaint'
  390.                 using by value hwnd
  391.                       by value 0 size 4
  392.                                       by reference Rectl
  393.                                 returning hps
  394.  
  395.                     call OS2API '__WinQueryWindowRect'
  396.                                 using by value hwnd
  397.                                       by reference Rectl
  398.  
  399.                     call OS2API '__WinDrawText'
  400.                                 using by value hps
  401.                                       by value 17 size 2
  402.                                       by reference 'Hello COBOL World'
  403.                                       by reference Rectl
  404.                                       by value Sysclr-WindowText size 4
  405.                                       by value Sysclr-Window     size 4
  406.                                       by value h'8500' size 2
  407.  
  408.             call OS2API '__WinEndPaint'
  409.                 using by value hps
  410.  
  411.       ****************************************************************
  412.       *                                  *
  413.       *     All other messages are despatched to the default         *
  414.       *     window procedure according to the PM rules.          *
  415.       *                                  *
  416.       ****************************************************************
  417.         when other
  418.             call OS2API '__WinDefWindowProc'
  419.                 using by value hwnd
  420.                       by value msg
  421.                       by value mp1
  422.                       by value mp2
  423.                                 returning Mresult
  424.  
  425.         end-evaluate
  426.  
  427.       ****************************************************************
  428.       *                                  *
  429.       * COBOL Extension: RETURNING phrase                 *
  430.       *                                  *
  431.       *     To complement the RETURNING phrase on the CALL, you      *
  432.       *     can also use the RETURNING phrase on the EXIT.         *
  433.       *                                  *
  434.       ****************************************************************
  435.             exit program returning Mresult.
  436.