home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2BAS.ZIP / GPIELEM.BAS < prev    next >
BASIC Source File  |  1989-08-26  |  13KB  |  379 lines

  1. '*******************************************************************
  2. '* 
  3. '* Program Name: GpiElem.BAS
  4. '*
  5. '* Include File: GpiElem.BI
  6. '*
  7. '* Functions   :
  8. '*               GpiBeginElement
  9. '*               GpiEndElement
  10. '*               GpiLabel
  11. '*               GpiElement
  12. '*               GpiQueryElement
  13. '*               GpiDeleteElement
  14. '*               GpiDeleteElementRange
  15. '*               GpiDeleteElementsBetweenLabels
  16. '*               GpiQueryEditMode
  17. '*               GpiSetEditMode
  18. '*               GpiQueryElementPointer
  19. '*               GpiSetElementPointer
  20. '*               GpiOffsetElementPointer
  21. '*               GpiQueryElementType
  22. '*               GpiSetElementPointerAtLabel
  23. '*
  24. '* Description : This program demonstrates the element functions.
  25. '*               These functions allow graphics statements to be
  26. '*               stored in RAM to then be displayed. Most element
  27. '*               functions must be used in segments. Segments also
  28. '*               retain graphics; the difference is that elements
  29. '*               are subsets of segments.
  30. '*
  31. '*               All calls are demonstrated in SUBs after
  32. '*               the ClientWndProc. These SUBs are called when
  33. '*               a mouse or keyboard message is received.
  34. '*               QueryElements writes element info to GpiElem.OUT.
  35. '******************************************************************
  36.  
  37. '*********         Initialization section        ***********
  38.  
  39. REM $INCLUDE: 'PMBase.BI'
  40. REM $INCLUDE: 'OS2Def.BI'
  41. REM $INCLUDE: 'GpiElem.BI'
  42. REM $INCLUDE: 'GpiSeg.BI'
  43. REM $INCLUDE: 'GpiCont.BI'        Needed for Create/DestroyPS
  44. REM $INCLUDE: 'WinInput.BI'       Needed for messages
  45. CONST OCODEGCLINE   = &H81      ' polyline at current posn (from PMOrd.BI)
  46. CONST  DROFILL        =  1
  47. DECLARE FUNCTION GpiMove%(BYVAL hps AS LONG, BYVAL pptl AS LONG)
  48. DECLARE FUNCTION GpiFullArc%(BYVAL hps AS LONG,_
  49.                              BYVAL flFlags AS LONG,_
  50.                              BYVAL fxMult AS LONG)
  51. DECLARE FUNCTION GpiLine%(BYVAL hps AS LONG, BYVAL pptl AS LONG)
  52. DECLARE FUNCTION GpiSetColor%(BYVAL hps AS LONG, BYVAL clr AS LONG)
  53. DECLARE FUNCTION WinOpenWindowDC&(BYVAL hwnd AS LONG)
  54.  
  55. 'Global anchor block and presentation space
  56. COMMON SHARED /Handles/ hab&, hps&
  57. COMMON SHARED /Values/ CurrElem%, LastElem%
  58.  
  59. DIM aqmsg AS QMSG
  60.  
  61. OPEN "GpiElem.OUT" FOR OUTPUT AS #1
  62. RANDOMIZE TIMER
  63.  
  64. flFrameFlags& =  FCFTITLEBAR      OR FCFSYSMENU OR _
  65.                  FCFSIZEBORDER    OR FCFMINMAX  OR _
  66.                  FCFSHELLPOSITION OR FCFTASKLIST
  67.  
  68. szClientClass$ = "ClassName" + CHR$(0)
  69.  
  70. hab&  = WinInitialize(0)
  71. hmq&  = WinCreateMsgQueue(hab&, 0)
  72.  
  73. bool% = WinRegisterClass(_
  74.         hab&,_
  75.         MakeLong(VARSEG(szClientClass$), SADD(szClientClass$)),_
  76.         RegBas,_
  77.         0,_
  78.         0)
  79.  
  80. hwndFrame& = WinCreateStdWindow (_
  81.              HWNDDESKTOP,_
  82.              WSVISIBLE,_
  83.              MakeLong(VARSEG(flFrameFlags&),  VARPTR(flFrameFlags&)),_
  84.              MakeLong(VARSEG(szClientClass$), SADD(szClientClass$)),_
  85.              0,_
  86.              0,_
  87.              0,_
  88.              0,_
  89.              MakeLong(VARSEG(hwndClient&), VARPTR(hwndClient&)))
  90.  
  91. '**************         Message loop         ***************
  92.  
  93. WHILE WinGetMsg(hab&, MakeLong(VARSEG(aqmsg), VARPTR(aqmsg)), 0, 0, 0)
  94.   bool% = WinDispatchMsg(hab&, MakeLong(VARSEG(aqmsg), VARPTR(aqmsg)))
  95. WEND
  96.  
  97. '***********         Finalize section        ***************
  98.  
  99. bool% = WinDestroyWindow  (hwndFrame&)
  100. bool% = WinDestroyMsgQueue(hmq&)
  101. bool% = WinTerminate      (hab&)
  102.  
  103. CLOSE #1
  104. END
  105.  
  106. '***********         Window procedure        ***************
  107.  
  108. FUNCTION ClientWndProc& (hwnd&, msg%, mp1&, mp2&) STATIC
  109.      DIM ClientRect AS RECTL
  110.      ClientWndProc& = 0
  111.      SELECT CASE msg%
  112.      CASE WMCREATE
  113.         CALL ElementSetup(hwnd&)
  114.      CASE WMCHAR             'Key press Deletes CurrElem%
  115.         IF (mp1& AND KCKEYUP)=0 THEN
  116.            CALL DeleteOne
  117.  
  118.            'Send WMPAINT to draw segment
  119.            bool% = WinSendMsg(hwnd&, WMPAINT, 0, 0)
  120.     END IF
  121.      CASE WMBUTTON1DOWN            '1st Button adds a random line at CurrElem%
  122.         CALL AddElement
  123.         ClientWndProc& = WinDefWindowProc(hwnd&, msg%, mp1&, mp2&)
  124.  
  125.         'Send WMPAINT to draw segment
  126.         bool% = WinSendMsg(hwnd&, WMPAINT, 0, 0)
  127.      CASE WMBUTTON2DOWN        '2nd Button deletes between CurrElem and CE+1
  128.         CALL DeleteCurrentElements
  129.  
  130.         'Send WMPAINT to draw segment
  131.         bool% = WinSendMsg(hwnd&, WMPAINT, 0, 0)
  132.      CASE WMPAINT     'Draw the segment
  133.  
  134.         'Draw the segment
  135.         bool% = GpiErase      (hps&)
  136.         bool% = GpiDrawSegment(hps&, 1)
  137.  
  138.         hps2& = WinBeginPaint (hwnd&,0,0)    ' WinBegin/EndPaint to
  139.         bool% = WinEndPaint   (hps2&)        ' terminate WMPAINT message.
  140.  
  141.      CASE WMCLOSE        'Delete all remaining elements, segment and PS
  142.         CALL DeleteAll
  143.         bool% = GpiDeleteSegment(hps&, 1)
  144.         bool% = GpiDestroyPS    (hps&)
  145.         ClientWndProc& = WinDefWindowProc(hwnd&, msg%, mp1&, mp2&)
  146.      CASE ELSE        'Pass control to system for other messages
  147.         ClientWndProc& = WinDefWindowProc(hwnd&, msg%, mp1&, mp2&)
  148.      END SELECT
  149. END FUNCTION
  150.  
  151. '****
  152. '** ElementSetup creates a device context and a presentation space,
  153. '** then defines a segment containing 5 sets of 5 elements each.
  154. '** Each set of elements contains the following:
  155. '**      1. Label
  156. '**      2. Color
  157. '**      3. Move
  158. '**      4. Circle
  159. '**      5. Line
  160. SUB ElementSetup(hwnd&) STATIC
  161.  
  162.    'Get device context for window to use with GpiCreatePS
  163.    hdcWin& = WinOpenWindowDC(hwnd&)
  164.  
  165.    'Define size of presentation space for GpiCreatePS
  166.    DIM szl AS SIZEL
  167.    szl.cx = 640
  168.    szl.cy = 480
  169.  
  170.    'Create a presentation space because segments can not be
  171.    'used with micro presentation spaces.
  172.    hps& = GpiCreatePS(hab&, hdcWin&,_
  173.           MakeLong(VARSEG(szl), VARPTR(szl)),_
  174.           PUPELS OR GPIAASSOC)
  175.  
  176.    'Set drawing mode
  177.    bool% = GpiSetDrawingMode (hps&, DMRETAIN)
  178.  
  179.    'Setup up string for GpiElement. Uses order from PMOrd.BI.
  180.    DIM GraphicString AS STRING * 10
  181.    GraphicString = CHR$(OCODEGCLINE) + CHR$(8) + CHR$(0) + CHR$(0) + _
  182.                    CHR$(0) + CHR$(0) + CHR$(200) + CHR$(0) + CHR$(0) + CHR$(0)
  183.  
  184.    'radius for GpiFullArc (fixed type in C)
  185.    radius& = 25 * &H10000
  186.  
  187.    'Set initial position for elements
  188.    DIM ptl AS POINTL
  189.    ptl.x = 30
  190.    ptl.y = 30
  191.  
  192.    bool% = GpiOpenSegment(hps&, 1)
  193.  
  194. '** Each set of elements contains the following:
  195. '**      1. Label
  196. '**      2. Color
  197. '**      3. Move
  198. '**      4. Circle
  199. '**      5. Line
  200.      FOR e% = 1 TO 5
  201.  
  202. '**      1. Label
  203.        bool% = GpiLabel(hps&, e%)
  204.  
  205. '**      2. Color
  206.        type1$ = "First" + STR$(e%) + CHR$(0)
  207.        bool%    = GpiBeginElement(hps&, e% * 4 - 3,_
  208.                   MakeLong(VARSEG(type1$), SADD(type1$)))
  209.           bool% = GpiSetColor    (hps&, e%)  'Set color to distinguish elements
  210.        bool%    = GpiEndElement  (hps&)
  211.  
  212. '**      3. Move
  213.        type2$   = "Second" + STR$(e%) + CHR$(0)
  214.        bool%    = GpiBeginElement (hps&, e% * 4 - 2,_
  215.                   MakeLong(VARSEG(type2$), SADD(type2$)))
  216.           bool% = GpiMove       (hps&, MakeLong(VARSEG(ptl), VARPTR(ptl)))
  217.        bool%    = GpiEndElement   (hps&)
  218.  
  219. '**      4. Circle
  220.        type3$   = "Third" + STR$(e%) + CHR$(0)
  221.        bool%    = GpiBeginElement (hps&, e% * 4 - 1,_
  222.                   MakeLong(VARSEG(type3$), SADD(type3$)))
  223.           bool% = GpiFullArc    (hps&, DROFILL, radius&)
  224.        bool%    = GpiEndElement   (hps&)
  225.  
  226. '**      5. Line
  227.        type4$   = "Fourth" + STR$(e%) + CHR$(0)
  228.        pmerr&   = GpiElement(hps&, e% * 4,_
  229.                   MakeLong(VARSEG(type4$),        SADD(type4$)), 10,_
  230.                   MakeLong(VARSEG(GraphicString), VARPTR(GraphicString)))
  231.  
  232.        ptl.x = ptl.x + 20 : ptl.y = ptl.y + 20   'Increment point position
  233.      NEXT e%
  234.    bool% = GpiCloseSegment(hps&)
  235.  
  236.    'Set globals for adding/deleting elements
  237.    CurrElem% = 1
  238.    LastElem% = 20
  239.  
  240.    bool% = GpiSetDrawingMode (hps&, DMDRAW)  'Reset drawing mode
  241. END SUB
  242.  
  243. '****
  244. '** AddElement adds a random line at the CurrElem label.  This is
  245. '** called when a WMBUTTON1DOWN message occurs.
  246. SUB AddElement
  247.    DIM ptl AS POINTL
  248.    bool% = GpiSetDrawingMode (hps&, DMRETAIN)  'Reset drawing mode
  249.    bool% = GpiOpenSegment    (hps&, 1)
  250.  
  251.      'Set mode to Insert if not already
  252.      IF GpiQueryEditMode (hps&) <> SEGEMINSERT THEN
  253.        bool% = GpiSetEditMode (hps&, SEGEMINSERT)
  254.      END IF
  255.  
  256.      'Set pointer
  257.      bool% = GpiSetElementPointer        (hps&, 0)
  258.      bool% = GpiSetElementPointerAtLabel (hps&, CurrElem%)
  259.      bool% = GpiOffsetElementPointer     (hps&, 1)
  260.  
  261.      'Define Element
  262.      LastElem% = LastElem% + 1
  263.      typeNew$  = "New " + STR$(LastElem%) + CHR$(0)
  264.      ptl.x = 300 * RND
  265.      ptl.y = 300 * RND
  266.      bool%   = GpiBeginElement (hps&, LastElem%,_
  267.                MakeLong(VARSEG(typeNew$), SADD(typeNew$)))
  268.        bool% = GpiLine         (hps&, MakeLong(VARSEG(ptl), VARPTR(ptl)))
  269.      bool%   = GpiEndElement   (hps&)
  270.  
  271.    bool% = GpiCloseSegment   (hps&)
  272.    bool% = GpiSetDrawingMode (hps&, DMDRAW)  'Reset drawing mode
  273.    PRINT #1, "After AddElement"
  274.    CALL QueryElements              'Query and print elements
  275. END SUB
  276.  
  277. '****
  278. '** DeleteOne deletes the current element and decrements LastElem.
  279. '** This is caused by a WMCHAR message.
  280. SUB DeleteOne
  281.    bool% = GpiSetDrawingMode (hps&, DMRETAIN)  'Reset drawing mode
  282.    bool% = GpiOpenSegment    (hps&, 1)
  283.  
  284.      'Set pointer
  285.      bool% = GpiSetElementPointer        (hps&,0)
  286.      bool% = GpiSetElementPointerAtLabel (hps&, CurrElem%)
  287.      bool% = GpiOffsetElementPointer     (hps&, 2)
  288.  
  289.      'Delete and decrement
  290.      bool% = GpiDeleteElement(hps&)
  291.      LastElem% = LastElem% - 1
  292.  
  293.    bool% = GpiCloseSegment   (hps&)
  294.    bool% = GpiSetDrawingMode (hps&, DMDRAW)  'Reset drawing mode
  295.    PRINT #1, "After DeleteOne"
  296. END SUB
  297.  
  298. '****
  299. '** DeleteCurrentElements deletes between the current label and the
  300. '** next label and increments the CurrElem.  This is caused by a
  301. '** WMBUTTON2DOWN message.
  302. SUB DeleteCurrentElements
  303.    bool% = GpiSetDrawingMode (hps&, DMRETAIN)  'Reset drawing mode
  304.    bool% = GpiOpenSegment    (hps&, 1)
  305.      bool% = GpiDeleteElementsBetweenLabels(hps&, CurrElem%, CurrElem% + 1)
  306.      CurrElem% = CurrElem% + 1
  307.    bool% = GpiCloseSegment   (hps&)
  308.    bool% = GpiSetDrawingMode (hps&, DMDRAW)  'Reset drawing mode
  309.    PRINT #1, "After DeleteCurrentElements"
  310.    CALL QueryElements              'Query and print elements
  311. END SUB
  312.  
  313. '****
  314. '** DeleteAll deletes all remaining segments (assuming there are fewer
  315. '** than 1000 elements.  This is called on a WMCLOSE.
  316. SUB DeleteAll
  317.    bool% = GpiSetDrawingMode      (hps&, DMRETAIN)  'Set drawing mode
  318.    bool% = GpiOpenSegment         (hps&, 1)
  319.      bool% = GpiDeleteElementRange(hps&, 0, 1000)
  320.    bool% = GpiCloseSegment        (hps&)
  321.    bool% = GpiSetDrawingMode      (hps&, DMDRAW)    'Reset drawing mode
  322.    PRINT #1, "After DeleteAll"
  323.    CALL QueryElements              'Query and print elements
  324. END SUB
  325.  
  326. '****
  327. '** QueryElements queries the element info and prints it out to
  328. '** GpiElem.OUT.  This is called by AddElement, DeleteCurrentSegments,
  329. '** and DeleteAll.
  330. SUB QueryElements
  331.    DIM Buffer AS STRING * 512, EName AS STRING * 32
  332.    bool% = GpiSetDrawingMode (hps&, DMRETAIN)  'Set drawing mode
  333.    bool% = GpiOpenSegment    (hps&, 1)
  334.  
  335.      'Set pointer at the beginning of segment
  336.      bool% = GpiSetElementPointer (hps&, 0)
  337.  
  338.      'Initialize element pointer (-1)
  339.      ep& = -1
  340.  
  341.      'Query elements until same element comes up twice.
  342.      'NOTE: Querying after the last element does not return an error.
  343.      DO UNTIL oldep& = ep&
  344.  
  345.        'Set old and query new element
  346.        oldep& = ep&
  347.        ep& = GpiQueryElementPointer (hps&)
  348.  
  349.        'Query name and type
  350.        bytes& = GpiQueryElementType (hps&,_
  351.                 MakeLong(VARSEG(lType&), VARPTR(lType&)), 32,_
  352.                 MakeLong(VARSEG(EName), VARPTR(EName)))
  353.  
  354.        'Query element data
  355.        bytes& = GpiQueryElement (hps&, 0, 512,_
  356.                 MakeLong(VARSEG(Buffer), VARPTR(Buffer)))
  357.  
  358.        'Write element info
  359.        PRINT #1, "Element #";ep&
  360.        PRINT #1, "   Type:",lType&
  361.        PRINT #1, "   Name:",RTRIM$(EName)
  362.        PRINT #1, "   Data:";
  363.  
  364.        'Loop through element data
  365.        FOR i% = 1 TO bytes&
  366.          b$ = HEX$(ASC(MID$(Buffer,i%,1)))
  367.          IF LEN(b$)=1 THEN PRINT #1, " ";
  368.          PRINT #1, b$; "  ";
  369.          IF i% MOD 12 = 0 THEN PRINT #1,
  370.        NEXT i%
  371.        PRINT #1,
  372.  
  373.        'Increment element pointer
  374.        bool% = GpiOffsetElementPointer(hps&, 1)
  375.      LOOP
  376.    bool% = GpiCloseSegment   (hps&)
  377.    bool% = GpiSetDrawingMode (hps&, DMDRAW)  'Reset drawing mode
  378. END SUB
  379.