home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / MacSource / ProgressDialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-14  |  6.0 KB  |  209 lines  |  [TEXT/CWIE]

  1. /*
  2. ==============================================================================
  3. Project:    POV-Ray
  4.  
  5. Version:    3
  6.  
  7. File Name:    ProgressDialog.c
  8.  
  9. Description:
  10.     General-purpose progress bar handling for dialogs.
  11.  
  12.     This is the main source file, containing the private definitions and
  13.     code to implement all the needed external and internal support functions.
  14.  
  15. Related Files:
  16.     ProgressDialog.h    - Header for these routines
  17. ------------------------------------------------------------------------------
  18. Author:
  19.     Eduard [esp] Schwan
  20. ------------------------------------------------------------------------------
  21.     from Persistence of Vision(tm) Ray Tracer
  22.     Copyright 1996 Persistence of Vision Team
  23. ------------------------------------------------------------------------------
  24.     NOTICE: This source code file is provided so that users may experiment
  25.     with enhancements to POV-Ray and to port the software to platforms other 
  26.     than those supported by the POV-Ray Team.  There are strict rules under
  27.     which you are permitted to use this file.  The rules are in the file
  28.     named POVLEGAL.DOC which should be distributed with this file. If 
  29.     POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  30.     Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  31.     Forum.  The latest version of POV-Ray may be found there as well.
  32.  
  33.     This program is based on the popular DKB raytracer version 2.12.
  34.     DKBTrace was originally written by David K. Buck.
  35.     DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  36. ------------------------------------------------------------------------------
  37. Change History:
  38.     920820    [esp]    Created
  39.     920920    [esp]    changed highestVal from int to long
  40.     930619    [esp]    added dispose fn
  41.     930903    [esp]    Worked on display bug in showProgress_UProc
  42.     930911    [esp]    Finally fixed the display bug in showProgress_UProc
  43.     931001    [esp]    version 2.0 finished (Released on 10/4/93)
  44.     931119    [djh]    2.0.1 conditionally compiles for PPC machine, keyword __powerc
  45.     940416    [PFS]    2.2.1 greatly reworked to clean up PPC support and provide CodeWarrior projects
  46. ==============================================================================
  47. */
  48.  
  49. #include "ProgressDialog.h"    // our defs
  50.  
  51. /* Macintosh-specific headers */
  52. #include <Types.h>
  53. #include <Dialogs.h>
  54. #include <QuickDraw.h>
  55.  
  56.  
  57.  
  58. /*  gProgressItemValue will be set to 0 to 100 by CalculateProgressValue() */
  59.  
  60. static unsigned long    gProgressItemValue;
  61. static short            gProgressItemNum;
  62. static Rect                gProgressItemRect;
  63. static RgnHandle        gProgressItemRgn = NULL;
  64.  
  65.  
  66. // ==============================================
  67. // Set up a user item proc for drawing progress bar
  68. static pascal void showProgress_UProc(DialogPtr theDialog, short theItem)
  69. {
  70. #pragma unused (theItem)
  71.     PenState    SavePen;
  72.     short        progressPos;
  73.     Rect        dispRect,
  74.                 outerRect,
  75.                 progressRect;
  76.  
  77.     // remember original penstate
  78.     SetPort(theDialog);
  79.     GetPenState(&SavePen);
  80.  
  81.     // find progress bar rectangle
  82.     dispRect = gProgressItemRect;
  83.  
  84.     // outer frame
  85.     outerRect = dispRect;
  86.     InsetRect(&outerRect, 1, 1);
  87.     PenSize(1, 1);
  88.     ForeColor(blackColor);
  89.     FrameRect(&outerRect);
  90.  
  91.     // set up progress rect
  92.     progressRect = dispRect;
  93.     InsetRect(&progressRect, 3, 3);
  94.  
  95.     // calculate inner bar progress position
  96.     if (gProgressItemValue > 100)
  97.         gProgressItemValue = 100;
  98.     progressPos = ((unsigned long)(progressRect.right - progressRect.left) * gProgressItemValue) / 100L;
  99.  
  100.     // draw inner bar (left filled side)
  101.     if (progressPos > 0)
  102.     {
  103.         progressRect.right = dispRect.left + progressPos;
  104.         ForeColor(blackColor);
  105.         PaintRect(&progressRect);
  106.  
  107.         // draw inner bar (right open side)
  108.         if (progressPos < 100)
  109.         {
  110.             progressRect.left = dispRect.left + progressPos;
  111.             ForeColor(whiteColor);
  112.             PaintRect(&progressRect);
  113.         }
  114.     }
  115.  
  116.     // restore state
  117.     SetPenState(&SavePen);
  118.  
  119. } // showProgress_UProc
  120.  
  121.  
  122. // ==============================================
  123. // Sets dialog item's display proc to draw progress bar
  124. static void SetupProgressItem(DialogPtr theDialog, short theItemNum)
  125. {
  126.     short    itemtype;
  127.     Handle    tempHandle;
  128.     UserItemUPP    drawProcUPP;
  129.  
  130.     drawProcUPP = NewUserItemProc((ProcPtr)showProgress_UProc);
  131.  
  132.     gProgressItemValue = 0;
  133.  
  134.     // Set up User item to display a progress bar
  135.     GetDialogItem(theDialog, theItemNum, &itemtype, &tempHandle, &gProgressItemRect);
  136.     SetDialogItem(theDialog, theItemNum, itemtype, (Handle)drawProcUPP, &gProgressItemRect);
  137.  
  138.     // remember.. for later updates
  139.     gProgressItemRgn = NewRgn();
  140.     RectRgn(gProgressItemRgn, &gProgressItemRect);
  141.     gProgressItemNum = theItemNum;
  142. } // SetupProgressItem
  143.  
  144.  
  145. // ==============================================
  146. // Loads Dialog resource and sets up progress bar user item proc
  147. DialogPtr GetNewProgressDialog(short theDialogID, short theProgressItemNum)
  148. {
  149.     DialogPtr    theDialog = NULL;
  150.  
  151.     theDialog = GetNewDialog(theDialogID, NULL, (WindowPtr)-1);
  152.  
  153.     if (theDialog)
  154.         SetupProgressItem(theDialog, theProgressItemNum);
  155.  
  156.     return (theDialog);
  157.  
  158. } // GetNewProgressDialog
  159.  
  160.  
  161. // ==============================================
  162. // Calculates current value for progress bar
  163. static void CalculateProgressValue(long lowestVal, long highestVal, long currentVal)
  164. {
  165.     // Clip against upper & lower bounds
  166.     if (currentVal < lowestVal)
  167.         currentVal = lowestVal;
  168.     else
  169.         if (currentVal > highestVal)
  170.             currentVal = highestVal;
  171.  
  172.     // Now calculate current value
  173.     gProgressItemValue = (currentVal-lowestVal) * 100L / (highestVal-lowestVal);
  174.  
  175. } // CalculateProgressValue
  176.  
  177.  
  178. // ==============================================
  179. // Recalculate progress bar and redisplay dialog
  180. void updateProgressDialog(DialogPtr pDialogPtr, long lowestVal, long highestVal, long currentVal)
  181. {
  182.     CalculateProgressValue(lowestVal, highestVal, currentVal);
  183.     SetPort(pDialogPtr);
  184.     InvalRect(&gProgressItemRect);
  185.  
  186.     BeginUpdate(pDialogPtr);
  187.     UpdateDialog(pDialogPtr, gProgressItemRgn);
  188.     EndUpdate(pDialogPtr);
  189.  
  190. } // updateProgressDialog
  191.  
  192.  
  193. // ==============================================
  194. // dispose the progress bar dialog
  195. void disposeProgressDialog(DialogPtr pDialogPtr)
  196. {
  197.     if (pDialogPtr)
  198.     {
  199.         DisposeDialog(pDialogPtr);
  200.         pDialogPtr = NULL;
  201.     }
  202.  
  203.     if (gProgressItemRgn)
  204.     {
  205.         DisposeRgn(gProgressItemRgn);
  206.         gProgressItemRgn = NULL;
  207.     }
  208. } // disposeProgressDialog
  209.