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

  1. '***********************************************************
  2. '* 
  3. '* Program Name: GpiSeg.BAS
  4. '*
  5. '* Include File: GpiSeg.BI
  6. '*
  7. '* Functions   :
  8. '*               GpiOpenSegment
  9. '*               GpiCloseSegment
  10. '*               GpiDrawSegment
  11. '*               GpiDeleteSegment
  12. '*               GpiDeleteSegments
  13. '*               GpiQuerySegmentNames
  14. '*               GpiSetInitialSegmentAttrs
  15. '*               GpiQueryInitialSegmentAttrs
  16. '*               GpiSetSegmentAttrs
  17. '*               GpiQuerySegmentAttrs
  18. '*               GpiSetSegmentPriority
  19. '*               GpiQuerySegmentPriority
  20. '*               GpiDrawChain
  21. '*               GpiDrawDynamics
  22. '*               GpiRemoveDynamics
  23. '*               GpiGetData
  24. '*               GpiPutData
  25. '*               GpiComment         GpiComment is not demonstrated
  26. '*                                  because it has no purpose.
  27. '*
  28. '* Description : This program demonstrates the segment functions.
  29. '*               These functions allow graphics statements to be
  30. '*               stored in RAM to then be displayed. These can be
  31. '*               displayed alone (GpiDrawSegment), dynamically
  32. '*               (GpiDrawDynamics), in groups (GpiDrawFrom),
  33. '*               or in entire chains (GpiDrawChain).
  34. '*
  35. '*               Using segments requires creating a presentation
  36. '*               space (WinCreatePS) and associating it with the
  37. '*               appropriate device context (WinOpenWindowDC).
  38. '*
  39. '*               GpiComment is not demonstrated because it seems
  40. '*               to have no purpose. It allows you to place a
  41. '*               comment on a segment, but there is no way to
  42. '*               retrieve that comment.
  43. '***********************************************************
  44.  
  45. '*********         Initialization section        ***********
  46.  
  47. REM $INCLUDE: 'PMBase.BI'
  48. REM $INCLUDE: 'GpiSeg.BI'
  49. REM $INCLUDE: 'GpiCont.BI'        Needed for Create/DestroyPS
  50. REM $INCLUDE: 'WinInput.BI'       Needed for messages
  51. REM $INCLUDE: 'OS2Def.BI'         Needed for POINTL type
  52. CONST  DROFILL        =  1
  53. DECLARE FUNCTION GpiMove%(BYVAL hps AS LONG, BYVAL pptl AS LONG)
  54. DECLARE FUNCTION GpiFullArc%(BYVAL hps AS LONG,_
  55.                              BYVAL flFlags AS LONG,_
  56.                              BYVAL fxMult AS LONG)
  57. DECLARE FUNCTION GpiSetColor%(BYVAL hps AS LONG, BYVAL clr AS LONG)
  58. DECLARE FUNCTION WinOpenWindowDC&(BYVAL hwnd AS LONG)
  59.  
  60. CONST FirstSeg = 1
  61. CONST LastSeg  = 10
  62.  
  63. 'Global anchor block and presentation space
  64. COMMON SHARED /Handles/ hab&, hps&
  65.  
  66. DIM aqmsg AS QMSG
  67.  
  68. flFrameFlags& =  FCFTITLEBAR      OR FCFSYSMENU OR _
  69.                  FCFSIZEBORDER    OR FCFMINMAX  OR _
  70.                  FCFSHELLPOSITION OR FCFTASKLIST
  71.  
  72. szClientClass$ = "ClassName" + CHR$(0)
  73.  
  74. hab&  = WinInitialize    (0)
  75. hmq&  = WinCreateMsgQueue(hab&, 0)
  76.  
  77. bool% = WinRegisterClass(_
  78.         hab&,_
  79.         MakeLong(VARSEG(szClientClass$), SADD(szClientClass$)),_
  80.         RegBas,_
  81.         0,_
  82.         0)
  83.  
  84. hwndFrame& = WinCreateStdWindow (_
  85.              HWNDDESKTOP,_
  86.              WSVISIBLE,_
  87.              MakeLong (VARSEG(flFrameFlags&),  VARPTR(flFrameFlags&)),_
  88.              MakeLong (VARSEG(szClientClass$), SADD(szClientClass$)),_
  89.              0,_
  90.              0,_
  91.              0,_
  92.              0,_
  93.              MakeLong (VARSEG(hwndClient&), VARPTR(hwndClient&)))
  94.  
  95. '**************         Message loop         ***************
  96.  
  97. WHILE WinGetMsg(hab&, MakeLong(VARSEG(aqmsg), VARPTR(aqmsg)), 0, 0, 0)
  98.   bool% = WinDispatchMsg(hab&, MakeLong(VARSEG(aqmsg), VARPTR(aqmsg)))
  99. WEND
  100.  
  101. '***********         Finalize section        ***************
  102.  
  103. bool% = WinDestroyWindow   (hwndFrame&)
  104. bool% = WinDestroyMsgQueue (hmq&)
  105. bool% = WinTerminate       (hab&)
  106.  
  107. END
  108.  
  109. '***********         Window procedure        ***************
  110.  
  111. FUNCTION ClientWndProc& (hwnd&, msg%, mp1&, mp2&) STATIC
  112.      DIM ClientRect AS RECTL
  113.      ClientWndProc&=0
  114.      SELECT CASE msg%
  115.      CASE WMCREATE
  116.     CALL SegmentSetup(hwnd&)
  117.         NumSegs% = LastSeg       'Initial number of segments is max seg value
  118.      CASE WMCHAR             'Key press causes second segment to be dynamic
  119.         IF (mp1& AND KCKEYUP)=0 THEN
  120.  
  121.           'Set attribute if not already
  122.           IF ATTROFF=GpiQuerySegmentAttrs(hps&, FirstSeg+1, ATTRDYNAMIC) THEN
  123.             bool%   =GpiSetSegmentAttrs  (hps&, FirstSeg+1, ATTRDYNAMIC, ATTRON)
  124.           END IF
  125.  
  126.           'draw dynamic segment
  127.       bool% = GpiDrawDynamics(hps&)
  128.           SLEEP 1
  129.  
  130.           'Remove and reset dynamic segment
  131.           bool% = GpiRemoveDynamics (hps&, FirstSeg,   LastSeg)
  132.           bool% = GpiSetSegmentAttrs(hps&, FirstSeg+1, ATTRDYNAMIC, ATTROFF)
  133.  
  134.     END IF
  135.      CASE WMBUTTON1DOWN            '1st Button deletes middle segment
  136.         DIM SegNames&(NumSegs% - 1)     'Array for valid segment names
  137.  
  138.         'Fill array with valid segments
  139.         retNum&  = GpiQuerySegmentNames (hps&,_
  140.                    FirstSeg, LastSeg, NumSegs%,_
  141.                    MakeLong(VARSEG(SegNames&(0)), VARPTR(SegNames&(0))))
  142.         Deleted% = NumSegs% \ 2       'Select segment to delete
  143.  
  144.         'Draw segment to be deleted
  145.         bool% = GpiErase      (hps&)
  146.     bool% = GpiDrawSegment(hps&, SegNames&(Deleted%))
  147.     SLEEP 1
  148.  
  149.         'Draw lower part of chain
  150.         bool% = GpiErase   (hps&)
  151.     bool% = GpiDrawFrom(hps&, FirstSeg, SegNames&(Deleted%-1))
  152.     SLEEP 1
  153.  
  154.         'Draw upper part of chain
  155.         bool% = GpiErase   (hps&)
  156.     bool% = GpiDrawFrom(hps&, SegNames&(Deleted%+1), LastSeg)
  157.     SLEEP 1
  158.  
  159.         'Delete segment and decrement number of segments
  160.         bool%    = GpiDeleteSegment(hps&, SegNames&(Deleted%))
  161.     NumSegs% = NumSegs% - 1
  162.         ERASE SegNames&         'Erase array so it can be reallocated
  163.  
  164.         'Send WMPAINT to draw full chain
  165.         bool% = WinSendMsg(hwnd&, WMPAINT, 0, 0)
  166.  
  167.      CASE WMBUTTON2DOWN        '2nd Button changes priority of first segment
  168.  
  169.         'Get next higher and lower priority segments (only 1 call will succeed)
  170.     Higher& = GpiQuerySegmentPriority(hps&, FirstSeg, HIGHERPRI)
  171.         Lower&  = GpiQuerySegmentPriority(hps&, FirstSeg, LOWERPRI)
  172.  
  173.         'Trade priorities with successful segment
  174.     IF Higher& = FirstSeg+1 THEN
  175.           bool%    = GpiSetSegmentPriority(hps&, FirstSeg, Higher&, HIGHERPRI)
  176.     ELSE
  177.           bool%    = GpiSetSegmentPriority(hps&, FirstSeg, Lower&, LOWERPRI)
  178.         END IF
  179.  
  180.         'Send WMPAINT to draw chain
  181.     bool% = WinSendMsg(hwnd&, WMPAINT, 0, 0)
  182.      CASE WMPAINT     'Erase the window and draw chain
  183.  
  184.         'Draw current chain
  185.         bool% = GpiErase     (hps&)
  186.         bool% = GpiDrawChain (hps&)
  187.  
  188.         hps2& = WinBeginPaint(hwnd&,0,0)    ' WinBegin/EndPaint to
  189.         bool% = WinEndPaint  (hps2&)        ' terminate WMPAINT message.
  190.  
  191.      CASE WMCLOSE
  192.     bool% = GpiDeleteSegments(hps&, FirstSeg, LastSeg)
  193.         bool% = GpiDestroyPS     (hps&)
  194.         ClientWndProc& = WinDefWindowProc(hwnd&, msg%, mp1&, mp2&)
  195.      CASE ELSE        'Pass control to system for other messages
  196.         ClientWndProc& = WinDefWindowProc(hwnd&, msg%, mp1&, mp2&)
  197.      END SELECT
  198. END FUNCTION
  199.  
  200. SUB SegmentSetup(hwnd&) STATIC
  201.  
  202.    'Get device context for window to use with GpiCreatePS
  203.    hdcWin& = WinOpenWindowDC(hwnd&)
  204.  
  205.    'Define size of presentation space for GpiCreatePS
  206.    DIM szl AS SIZEL
  207.    szl.cx = 640 : szl.cy = 480
  208.  
  209.    'Create a presentation space because segments can not be
  210.    'used with micro presentation spaces.
  211.    hps& = GpiCreatePS(hab&, hdcWin&,_
  212.           MakeLong(VARSEG(szl), VARPTR(szl)),_
  213.           PUPELS OR GPIAASSOC)
  214.  
  215.    'Set drawing mode and initial segment attributes
  216.    bool%       = GpiSetDrawingMode (hps&, DMRETAIN)
  217.    IF (ATTROFF = GpiQueryInitialSegmentAttrs(hps&, ATTRCHAINED)) THEN
  218.      bool%     = GpiSetInitialSegmentAttrs  (hps&, ATTRCHAINED, ATTRON)
  219.    END IF
  220.  
  221.    'Make a segment to be copied into each new segment
  222.    radius& = 25 * &H10000          'radius for GpiFullArc (fixed type in C)
  223.    bool%   = GpiOpenSegment (hps&, LastSeg+1)
  224.      bool% = GpiFullArc     (hps&, DROFILL, radius&)
  225.    bool%   = GpiCloseSegment(hps&)
  226.  
  227.    'Copy commands from above segment into a buffer to be used with GpiPutData
  228.    DIM Buffer AS STRING * 512
  229.    NumBytes& = GpiGetData(hps&, LastSeg + 1,_
  230.                MakeLong(VARSEG(Offset&), VARPTR(Offset&)),_
  231.                DFORMNOCONV, 512,_
  232.                MakeLong(VARSEG(Buffer),  VARPTR(Buffer)))
  233.  
  234.    'Set initial position for segments
  235.    DIM ptl AS POINTL
  236.    ptl.x = 30 : ptl.y = 30
  237.  
  238.    'Set up 10 segments (drawn diagonally)
  239.    FOR s% = FirstSeg TO LastSeg
  240.      bool% = GpiOpenSegment(hps&, s%)
  241.      bool% = GpiSetColor   (hps&, s%)   'Change color to distinguish segments
  242.      bool% = GpiMove       (hps&, MakeLong(VARSEG(ptl), VARPTR(ptl)))
  243.  
  244.      'Copy data from buffer (from original segment) to current segment
  245.      PutBytes& = GpiPutData(hps&, DFORMNOCONV,_
  246.                  MakeLong(VARSEG(NumBytes&), VARPTR(NumBytes&)),_
  247.                  MakeLong(VARSEG(Buffer),    VARPTR(Buffer)))
  248.  
  249.      bool% = GpiCloseSegment(hps&)
  250.      ptl.x = ptl.x + 20 : ptl.y = ptl.y + 20   'Increment point position
  251.    NEXT s%
  252.  
  253.    bool% = GpiDeleteSegment  (hps&, LastSeg+1) 'Remove initial segment
  254.    bool% = GpiSetDrawingMode (hps&, DMDRAW)    'Reset drawing mode
  255. END SUB
  256.