home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR503.W96 / GAUGE.PR_ / GAUGE.PR
Text File  |  1995-06-20  |  4KB  |  161 lines

  1. /***
  2. *
  3. *  Gauge.prg
  4. *
  5. *  Sample functions to create, display, and update a percentage completed
  6. *  progress gauge.  This function can be used for creating user interface
  7. *  options such as a status bar to indicate the current status of a process.
  8. *
  9. *  Copyright (c) 1993-1995, Computer Associates International Inc.
  10. *  All rights reserved.
  11. *
  12. *  Note: Compile with /W /N options
  13. *
  14. */
  15.  
  16. // Box array definitions
  17. #define B_LEN           9
  18. #define B_TOP           1
  19. #define B_LEFT          2
  20. #define B_BOTTOM        3
  21. #define B_RIGHT         4
  22. #define B_BACKCOLOR     5
  23. #define B_BARCOLOR      6
  24. #define B_DISPLAYNUM    7
  25. #define B_BARCHAR       8
  26. #define B_PERCENT       9
  27.  
  28. #define B_BOXLINES      "┌─┐│┘─└│"
  29.  
  30.  
  31. /***
  32. *  GaugeNew( <nRowTop>, <nColumnTop>, <nRowBottom>, <nColumnBottom>,
  33. *     [<cBackgroundColor>],
  34. *     [<cGaugeColor>],
  35. *     [<cGaugeCharacter>] ) --> aGauge
  36. *
  37. *  Create a new gauge array
  38. *
  39. */
  40. FUNCTION GaugeNew( nTop, nLeft, nBottom, nRight, ;
  41.                  cBackColor, cBarColor, cBarCharacter )
  42.  
  43.    LOCAL aHandle[ B_LEN ]     // Contains info about the gauge
  44.  
  45.    // Assign default values
  46.    aHandle[ B_TOP ]        := nTop
  47.    aHandle[ B_LEFT ]       := nLeft
  48.    aHandle[ B_BOTTOM ]     := nBottom
  49.    aHandle[ B_RIGHT ]      := nRight
  50.    aHandle[ B_BACKCOLOR ]  := "W/N"
  51.    aHandle[ B_BARCOLOR ]   := "W+/N"
  52.    aHandle[ B_DISPLAYNUM ] := .T.
  53.    aHandle[ B_BARCHAR ]    := CHR( 219 )
  54.    aHandle[ B_PERCENT ]    := 0
  55.  
  56.    // Resolve parameters
  57.    IF cBackColor <> NIL
  58.       aHandle[ B_BACKCOLOR ] := cBackColor
  59.    ENDIF
  60.  
  61.    IF cBarColor <> NIL
  62.       aHandle[ B_BARCOLOR ] := cBarColor
  63.    ENDIF
  64.  
  65.    IF cBarCharacter <> NIL
  66.       aHandle[ B_BARCHAR ] := cBarCharacter
  67.    ENDIF
  68.  
  69.    // OK, the defaults are set, now let's make sure it will fit on the
  70.    // screen correctly
  71.    IF aHandle[ B_RIGHT ] < aHandle[ B_LEFT ] + 4
  72.       aHandle[ B_RIGHT ] := aHandle[ B_LEFT ] + 4
  73.    ENDIF
  74.  
  75.    IF aHandle[ B_BOTTOM ] < aHandle[ B_TOP ] + 2
  76.       aHandle[ B_BOTTOM ] := aHandle[ B_TOP ] + 2
  77.    ENDIF
  78.  
  79.    // Determine if we can fit the bracketed number on top of the graph
  80.    IF aHandle[ B_RIGHT ] < aHandle[ B_LEFT ] + 9
  81.       aHandle[ B_DISPLAYNUM ] := .F.
  82.    ENDIF
  83.  
  84.    RETURN( aHandle )
  85.  
  86.  
  87.  
  88. /***
  89. *
  90. *  GaugeDisplay( aGauge ) --> aGauge
  91. *
  92. *  Display a gauge array to the screen
  93. *
  94. */
  95. FUNCTION GaugeDisplay( aHandle )
  96.  
  97.    LOCAL nCenter   := ROUND( (aHandle[B_RIGHT] - aHandle[B_LEFT]) / 2, 0) + 1
  98.    LOCAL cOldColor := SETCOLOR( aHandle[ B_BACKCOLOR ] )
  99.  
  100.    @ aHandle[ B_TOP ], aHandle[ B_LEFT ] CLEAR TO ;
  101.      aHandle[ B_BOTTOM ], aHandle[ B_RIGHT ]
  102.  
  103.    @ aHandle[ B_TOP ], aHandle[ B_LEFT ], ;
  104.      aHandle[ B_BOTTOM ], aHandle[ B_RIGHT ] BOX B_BOXLINES
  105.  
  106.    IF aHandle[ B_DISPLAYNUM ]
  107.       @ aHandle[ B_TOP ], nCenter SAY "[      ]"
  108.    ENDIF
  109.  
  110.    SETCOLOR( cOldColor )
  111.  
  112.    // Draw bar to show current percent
  113.    GaugeUpdate( aHandle, aHandle[ B_PERCENT ] )
  114.  
  115.    RETURN( aHandle )
  116.  
  117.  
  118.  
  119. /***
  120. *
  121. *  GaugeUpdate( aGauge, nPercent ) --> aGauge
  122. *
  123. *  Updates a gauge with a new progress value and redisplays the gauge
  124. *  to the screen to the screen
  125. *
  126. */
  127. FUNCTION GaugeUpdate( aHandle, nPercent )
  128.  
  129.    LOCAL nCenter   := ROUND( (aHandle[B_RIGHT] - aHandle[B_LEFT]) / 2, 0) + 1
  130.    LOCAL cOldColor := SETCOLOR( aHandle[ B_BARCOLOR ] )
  131.    LOCAL nBarRatio := (aHandle[ B_RIGHT ]) - (aHandle[ B_LEFT ] + 1)
  132.    LOCAL nRow      := 0
  133.    LOCAL nCols     := 0
  134.  
  135.    IF aHandle[ B_DISPLAYNUM ]
  136.       @ aHandle[ B_TOP ], nCenter + 2 SAY STR( nPercent * 100, 3 ) + "%"
  137.    ENDIF
  138.  
  139.    IF nPercent > 1
  140.       nPercent := 1
  141.    ENDIF
  142.  
  143.    IF nPercent < 0
  144.       nPercent := 0
  145.    ENDIF
  146.  
  147.    nCols := ROUND( nPercent * nBarRatio, 0 )
  148.  
  149.    @ aHandle[ B_TOP ] + 1, aHandle[ B_LEFT ] + 1 CLEAR TO ;
  150.      aHandle[ B_BOTTOM ] - 1, aHandle[ B_RIGHT ] - 1
  151.  
  152.    FOR nRow := 1 TO (aHandle[ B_BOTTOM ] - aHandle[ B_TOP ] - 1)
  153.       @ nRow + aHandle[ B_TOP ], aHandle[ B_LEFT ] + 1 SAY ;
  154.         REPLICATE( aHandle[ B_BARCHAR ], nCols )
  155.    NEXT
  156.  
  157.    SETCOLOR( cOldColor )
  158.  
  159.    RETURN( aHandle )
  160.  
  161.