home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a009 / 6.ddi / SAMPLE.LIF / GAUGE.PRG < prev    next >
Encoding:
Text File  |  1991-04-14  |  3.9 KB  |  146 lines

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