home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / scnote / fapp300.009 / UFracApp300.p < prev   
Encoding:
Text File  |  1988-08-17  |  12.9 KB  |  320 lines

  1. {-------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    MacApp Color QuickDraw Fractal Printing Sample Application
  6. #
  7. #    FracApp300
  8. #
  9. #    UFracApp300.p    -    Pascal Source
  10. #
  11. #    Copyright ⌐ 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    MFracApp300.p        August 1, 1988
  17. #                UFracApp300.p        August 1, 1988
  18. #                UFracApp300.inc1.p    August 1, 1988
  19. #                FracApp300.r        August 1, 1988
  20. #                FracApp300.make        August 1, 1988
  21. #
  22. #    The FracApp300 program is a version of the FracApp program that is
  23. #    set up to be as compatible as possible.  It uses the PrGeneral call
  24. #    in order to print at high resolution.  It demonstrates how to create
  25. #    and save 300 dpi PICT files.  It uses an offscreen port to calculate
  26. #    the data, and CopyBits to update the window on the screen.  When the
  27. #    documents are written or read, the QuickDraw bottlenecks are used to
  28. #    avoid having a huge memory hit during saving or opening. (800K of RAM
  29. #    not needed, on a 640x480 screen, a big win.)  The Palette Manager is
  30. #    used very slightly, only to associate a small palette with only Black
  31. #    and White with each window.  This avoids needing the system palette
  32. #    for each window, when no colors are used.  Since we are printing to
  33. #    normal printers, we only use B&W in this version.
  34. #            Written in MacApp Object Pascal code.
  35. #            Compatibility rating = 1.  (The use of PrGeneral is slightly 
  36. #                out of the ordinary, although supported.)
  37. #
  38. #    The program is a complete Macintosh application written in Object 
  39. #    Pascal using MacApp.  It supports multiple windows, calculations in the
  40. #    background under MultiFinder, use of the Palette Manager, reading and
  41. #    writing of PICT files using the bottlenecks, and shows how to calculate
  42. #    the Mandelbrot set.
  43. #
  44. #    There is a resource file that is necessary as well, to define the Menus, Window,
  45. #    Dialog, and Palette resources used in the program.  
  46. -------------------------------------------------------------}
  47. {Copyright 1988 by Bob.  All rights reserved, since Bob has all rights.
  48.     February 1, 1988.
  49.     Written by Bo3b Johnson of Developer Technical Support. }
  50.  
  51. UNIT UFracApp300;
  52.  
  53. (*
  54.  
  55. This is a not too small application which can calculate a fractal in full color
  56.     on the Mac II, using direct 881 code for speed.  It saves files on disk as PICT.
  57.     For full info, see the implementation.
  58.  
  59. *)
  60.  
  61. INTERFACE
  62.  
  63. USES
  64.     {$LOAD FracApp881.LOAD}
  65.         MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
  66.         UObject, UList, UMacApp,
  67.         PaletteMgr, UPrinting,
  68.     {$LOAD}
  69.         MacPrint;
  70.  
  71. CONST
  72.  
  73.   {Command numbers}
  74.  
  75.     kSignature                    = 'Arf3';        {Application signature}
  76.     kFileType                    = 'PICT';        {File-type code used for document files
  77.                                                                 created by this application}
  78.     kFracAppWindowID    = 1001;        {This is passed to NewSimpleWindow as the
  79.                                                             resource ID of the the WIND Resource
  80.                                                             which defines the window for this
  81.                                                             application's documents}
  82.     kStaggerAmount            = 30;
  83.     kPICTHeaderSize         = 512;        { 512 bytes off the file are used for our info and print info. }
  84.     kSelPattern                    = 128;        { pattern resource Id. }
  85.     kNewFractal                 = 1000;        { item Id for New Fractal menu option. }
  86.     kWrongMachine            = 1000;        { error code if we cannot run, from ForceEnvirons. }
  87.     kBadPrinter                = 1001;        { error code if printer doesn't support PrGeneral. }
  88.     kRectRight                    = 2240;        { Full page size rectangle right edge at 300 dpi }
  89.     kRectBottom                = 3000;        { Full page size for bottom at 300 dpi }
  90.     
  91.     envDontCare                 = 0;                { don't care is always 0, for ForceEnvirons. }
  92.  
  93. TYPE
  94.     { The header record for each of the files saved by FracApp.  This header includes the
  95.         pertinent data that allows a fractal to be restarted, as well as displayed
  96.         on the screen.  The print record follows this info in the first 512 bytes of a file,
  97.         but that info is read by the standard DoRead/DoWrite methods.  Some of the fields
  98.         are LongInts to avoid rounding errors during calculations. }
  99.     FracRecord  = RECORD
  100.         fType:        OSType;            { 4  set as 'Arf3' for these documents. }
  101.         hdrId:        Integer;            { 2  as 'FA' FracApp header ID. }
  102.         version:    Integer;              { 2  file version decides type of file. }
  103.         done:        Boolean;            { 2  if the fractal is finished or not. }
  104.         realMin:    Extended;            { 12  minimum value of fractal on real/p axis. }
  105.         realMax:    Extended;            { 12  maximum value of fractal on real/p axis. }
  106.         imagMin:    Extended;            { 12  minimum on imaginary/q axis. }
  107.         imagMax:    Extended;            { 12  maximum on imaginary/q axis. }
  108.         deltaP:        Extended;            { 12  horizontal step size in fractal space. }
  109.         deltaQ:        Extended;            { 12  vertical step size in fractal space. }
  110.         plotWidth: LongInt;            { 4  width of fractal area in pixels. }
  111.         plotHeight: LongInt;            { 4  height of fractal area in pixels. }
  112.         calcRect:    Rect;                { 8  rectangle surrounding the window it was built for. }
  113.         curRow:    LongInt;            { 4  counter for current pixel position to be done.  Vertical. }
  114.         curCol:        LongInt;            { 4  counter for current pixel.  Horizontal. }
  115.         elapsedTime: LongInt;        { 4  amount of time to do this fractal in seconds. }
  116.     END;  { FracRecord. }
  117.  
  118.  
  119.  
  120. {-------------------------------  Application  -------------------------------}
  121.  
  122.     TFracAppApplication = OBJECT(TApplication)
  123.  
  124.  
  125.         PROCEDURE TFracAppApplication.IFracAppApplication (itsMainFileType: OSType);
  126.             {Initializes the application and globals.}
  127.  
  128.         FUNCTION  TFracAppApplication.DoMakeDocument(
  129.                 itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  130.             {Launches a TFracAppDocument; called when application's icon is
  131.                 opened, and when New or Open is requested by the user.
  132.                 Every application which uses Documents MUST override this
  133.                 method}
  134.  
  135.         PROCEDURE  TFracAppApplication.DoIdle (Phase: IdlePhase); OVERRIDE;
  136.             { Performs Idle time processing for the application.  This will do the
  137.                 fractal calculation during the idle times.  It will allow each open
  138.                 view a chance to calculate. }
  139.  
  140.     END;    { TFracAppApplication }
  141.  
  142.  
  143. {-------------------------------  Document  -------------------------------}
  144.  
  145.     TFracAppDocument = OBJECT(TDocument)
  146.  
  147.         { Now the fields that are special to the fractal itself.  These are the
  148.             variables that make up the display state and the definition of the
  149.             fractal itself.  They are associated with the document so they can
  150.             be stored in a file, and read back in.  Essentially a global state for
  151.             each fractal document. }
  152.  
  153.         fFracHeader:                        FracRecord;        { global state on fractal. }
  154.         fStartTime:                            LongInt;                { starting time of calculations. }
  155.         fBigBuff:                                Ptr;                        { pointer to offscreen data }
  156.         fDrawingPort:                        GrafPtr;                { pointer to drawing buffer. }
  157.  
  158.         fFracAppView:                        TFracAppView;
  159.             {Every document object must preserve references to all the views it
  160.                 creates as fields of the document object, since DoMakeWindows
  161.                 will need to know which views to install in the windows it
  162.                 creates}
  163.  
  164.  
  165.         PROCEDURE  TFracAppDocument.BuildOffWorld (sizeOfDoc: Rect);
  166.             { Allocates offscreen gDevice and port for document data. }
  167.  
  168.         PROCEDURE  TFracAppDocument.SetUpConstants;
  169.             { Sets up starting constants for calculation. }
  170.  
  171.         PROCEDURE      TFracAppDocument.IFracAppDocument;
  172.             {Init routine for the document, sets up the object, then the fractal default state. }
  173.  
  174.         PROCEDURE TFracAppDocument.DoInitialState;  OVERRIDE;
  175.             { Does the work for a New operation, where we start with a new fractal
  176.                 that doesn't have any stored data.  This is set up the view with no
  177.                 data and set up the fractal coordinates to the default. }
  178.  
  179.         PROCEDURE TFracAppDocument.DoMakeWindows; OVERRIDE;
  180.             {Launches the window which will represent the document on the
  181.                 screen.  Every document which has any screen display MUST
  182.                 override this method}
  183.  
  184.         PROCEDURE TFracAppDocument.DoMakeViews(forPrinting: BOOLEAN); OVERRIDE;
  185.             {Launches the view which is seen in the document's window.  Every
  186.                 document which has any screen display or which can be printed
  187.                 MUST override this method}
  188.  
  189.         PROCEDURE TFracAppDocument.DoNeedDiskSpace(VAR dataForkBytes,
  190.                                                 rsrcForkBytes: LONGINT);  OVERRIDE;
  191.             { Finds out the entire size of the object to be saved into the file so that the
  192.                 correct amount of disk space can be used.  }
  193.  
  194.         PROCEDURE TFracAppDocument.DoRead(aRefNum: INTEGER; rsrcExists,
  195.                                                 forPrinting: BOOLEAN);    OVERRIDE;
  196.             { Reads in the data out of the data fork of the file, and stows it into the
  197.                 documents bitMap.  Also resets the vars in the document object to match
  198.                 the saved values from the header. }
  199.  
  200.         PROCEDURE TFracAppDocument.DoWrite(aRefNum: INTEGER;
  201.                                                 makingCopy: BOOLEAN);    OVERRIDE;
  202.             { Converts the data in the documents port into a PICT, then writes that block
  203.                 out to the file, making it into a standard PICT file. }
  204.  
  205.         PROCEDURE TFracAppDocument.FreeData; OVERRIDE;
  206.             {}
  207.  
  208.         PROCEDURE TFracAppDocument.Free; OVERRIDE;
  209.             { Free method for our document.  It disposes the data block of the picture
  210.                 data that was read in from the disk, and kills offscreen stuff. }
  211.  
  212.         PROCEDURE  TFracAppDocument.CalcCity;
  213.             { Does the idle time calculations for the document.  This is for the actual
  214.                 fractal calculations.  It is called by the handler for the Application
  215.                 DoIdle.  It is called for all open documents. }
  216.  
  217.     END;    { TFracAppDocument }
  218.  
  219.  
  220.  
  221. {-------------------------------  View  -------------------------------}
  222.  
  223.     TFracAppView = OBJECT(TView)
  224.  
  225.         { These fields are for the use of the view, and help define our specific view, that
  226.             is the offscreen bitMap representation of the fractal. }
  227.         fSelectionRect:                    Rect;        { rectangle that is current selection }
  228.         fFracAppDocument:                TFracAppDocument;    { handy to avoid type coercion. }
  229.  
  230.         PROCEDURE     TFracAppView.IFracAppView (itsDocument: TFracAppDocument;
  231.                 sizeOfView: Rect);
  232.             { Inits the view object itself. }
  233.  
  234.         PROCEDURE TFracAppView.Draw(area: Rect); OVERRIDE;
  235.             {Draws the view seen in the window.  Every nonblank view MUST
  236.                 override this method}
  237.  
  238.         FUNCTION TFracAppView.DoMenuCommand(
  239.                                                 aCmdNumber: CmdNumber): TCommand;  OVERRIDE;
  240.             { Handle the menu choices for New Fractal out of the Fractal Menu. }
  241.  
  242.         PROCEDURE TFracAppView.DoSetupMenus;  OVERRIDE;
  243.             { Set up the New Fractal menus choice in Fractal Menu, based on selection. }
  244.  
  245.         FUNCTION  TFracAppView.DoMouseCommand(VAR downLocalPoint: Point;
  246.                         VAR info: EventInfo;
  247.                         VAR hysteresis: Point): TCommand;    OVERRIDE;
  248.             { Handle the mouse events in the view.  Needs to do the selection of a new
  249.                 range for the next fractal to be calculated.  }
  250.  
  251.         PROCEDURE TFracAppView.DoHighlightSelection(fromHL, toHL: HLState); OVERRIDE;
  252.             { Highlight the current selection rectangle if there is one.  }
  253.  
  254.     END;    { TFracAppView }
  255.  
  256.  
  257. {-------------------------------  Command  -------------------------------}
  258.  
  259.     TAreaSelector = OBJECT(TCommand)
  260.  
  261.         { These fields are for the command objects that we use.  The first one we need
  262.             for sure is the selection object to handle the mouse selection in the content
  263.             region of the view.  }
  264.         fOwnerView:    TFracAppView;
  265.  
  266.  
  267.         PROCEDURE TAreaSelector.IAreaSelector(ownerView: TFracAppView; startPt: Point);
  268.             { Initialize the selection object itself.  This basically sets up with IObject,
  269.                 then sets the fSelectionRect to be a minimal rectangle. }
  270.  
  271.         FUNCTION  TAreaSelector.TrackMouse(aTrackPhase: TrackPhase;
  272.                         VAR anchorPoint, previousPoint, nextPoint: Point;
  273.                         mouseDidMove: BOOLEAN): TCommand;  OVERRIDE;
  274.             { Track the mouse while the button is down in the view. }
  275.  
  276.         PROCEDURE TAreaSelector.TrackFeedback(anchorPoint, nextPoint: Point;
  277.                                         turnItOn, mouseDidMove: BOOLEAN);  OVERRIDE;
  278.             { Feedback that matches better, using the selection pattern. }
  279.  
  280.         PROCEDURE TAreaSelector.TrackConstrain(anchorPoint, previousPoint: Point;
  281.                         VAR nextPoint: Point);  OVERRIDE;
  282.             { Constrain the mouse movement to be a rect which matches the screen. }
  283.  
  284.     END;    { TAreaSelector }
  285.  
  286. {-------------------------------  TFracAppPrintHandler  --------------------------}
  287.  
  288. { A print handler for doing 300 dpi printing, bypassing the print manager for most of
  289.     it.  Uses the port created for calculating the fractal, and prints it using PostScript
  290.     commands.  It is a print handler object so we get the fields and methods of the
  291.     printing object.  Since it is an event handler, it will handle the menu events for
  292.     the print operation, as well as enable their use. }
  293.  
  294.     TFracAppPrintHandler = OBJECT (TPrintHandler)
  295.     
  296.         fOwningView:    TFracAppView;        { so we can avoid type coercion. }
  297.  
  298.         
  299.         PROCEDURE     TFracAppPrintHandler.IFracAppPrintHandler (itsView: TFracAppView);
  300.             { Inits the print handler object itself. }
  301.  
  302.         FUNCTION TFracAppPrintHandler.Print(itsCmdNumber: CmdNumber; 
  303.                             VAR proceed: BOOLEAN): TCommand; OVERRIDE;
  304.             { Override the print method so we can pass off the Finder printing to DoPrintingCommand }
  305.             
  306.         PROCEDURE TFracAppPrintHandler.DoSetupPrintingMenus;  OVERRIDE;
  307.             { Set up the New Fractal menus choice in Fractal Menu, based on selection. }
  308.  
  309.         FUNCTION  TFracAppPrintHandler.DoPrintingCommand(
  310.                                                 aCmdNumber: CmdNumber): TCommand;  OVERRIDE;
  311.             { Handle the menu choices for New Fractal out of the Fractal Menu. }
  312.  
  313.     END;    { TFracAppPrintHandler }
  314.  
  315. IMPLEMENTATION
  316.  
  317. {$I UFracApp300.inc1.p}
  318.  
  319. END.
  320.