home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 February / macformat-047.iso / Shareware Plus / Utilities / ServerPower 1.0 / source / ShowInitIcon.c < prev    next >
Encoding:
Text File  |  1995-12-23  |  6.0 KB  |  164 lines  |  [TEXT/CWIE]

  1. // ShowInitIcon - version 1.0.1, May 30th, 1995
  2. // This code is intended to let INIT writers easily display an icon at startup time.
  3. // View in Geneva 9pt, 4-space tabs
  4.  
  5. // Written by: Peter N Lewis <peter@mail.peter.com.au>, Jim Walker <JWWalker@aol.com>
  6. // and François Pottier <pottier@dmi.ens.fr>, with thanks to previous ShowINIT authors.
  7. // Send comments and bug reports to François Pottier.
  8.  
  9. // This version features:
  10. // - Short and readable code.
  11. // - Correctly wraps around when more than one row of icons has been displayed.
  12. // - works with System 6
  13. // - Built with Universal Headers & CodeWarrior. Should work with other headers/compilers.
  14.  
  15. #undef SystemSixOrLater
  16. #define SystemSixOrLater 1
  17.  
  18. #include <Memory.h>
  19. #include <Resources.h>
  20. #include <Icons.h>
  21. #include <OSUtils.h>
  22. #include "ShowInitIcon.h"
  23.  
  24. // You should set SystemSixOrLater in your headers to avoid including glue for SysEnvirons.
  25.  
  26. // ---------------------------------------------------------------------------------------------------------------------
  27. // Set this flag to 1 if you want to compile this file into a stand-alone resource (see note below).
  28. // Set it to 0 if you want to include this source file into your INIT project.
  29.  
  30. #if 0
  31. #define ShowInitIcon main
  32. #endif
  33.  
  34. // ---------------------------------------------------------------------------------------------------------------------
  35. // The ShowINIT mechanism works by having each INIT read/write data from these globals.
  36. // The MPW C compiler doesn't accept variables declared at an absolute address, so I use these macros instead.
  37. // Only one macro is defined per variable; there is no need to define a Set and a Get accessor like in <LowMem.h>.
  38.  
  39. #define    LMVCoord            (* (short*) 0x92A)
  40. #define    LMVCheckSum        (* (short*) 0x928)
  41. #define    LMHCoord            (* (short*) 0x92C)
  42. #define    LMHCheckSum        (* (short*) 0x92E)
  43.  
  44. // ---------------------------------------------------------------------------------------------------------------------
  45. // Prototypes for the subroutines. The main routine comes first; this is necessary to make THINK C's "Custom Header" option work.
  46.  
  47. static unsigned short CheckSum (unsigned short x);
  48. static void ComputeIconRect (Rect* iconRect, Rect* screenBounds);
  49. static void AdvanceIconPosition (Rect* iconRect);
  50. static void DrawBWIcon (short iconID, Rect *iconRect);
  51.  
  52. // ---------------------------------------------------------------------------------------------------------------------
  53. // Main routine.
  54.  
  55. typedef struct {
  56.     QDGlobals            qd;                                    // Storage for the QuickDraw globals
  57.     long                qdGlobalsPtr;                            // A5 points to this place; it will contain a pointer to qd
  58. } QDStorage;
  59.  
  60. pascal void ShowInitIcon (short iconFamilyID, Boolean advance)
  61. {
  62.     long                oldA5;                                // Original value of register A5
  63.     QDStorage            qds;                                    // Fake QD globals
  64.     CGrafPort            colorPort;
  65.     GrafPort            bwPort;
  66.     Rect                destRect;
  67.     SysEnvRec        environment;                            // Machine configuration.
  68.     
  69.     oldA5 = SetA5((long) &qds.qdGlobalsPtr);                        // Tell A5 to point to the end of the fake QD Globals
  70.     InitGraf(&qds.qd.thePort);                                // Initialize the fake QD Globals
  71.     
  72.     SysEnvirons(curSysEnvVers, &environment);                    // Find out what kind of machine this is
  73.  
  74.     ComputeIconRect(&destRect, &qds.qd.screenBits.bounds);            // Compute where the icon should be drawn
  75.  
  76.     if (environment.systemVersion >= 0x0700 && environment.hasColorQD) {
  77.         OpenCPort(&colorPort);
  78.         PlotIconID(&destRect, atNone, ttNone, iconFamilyID);
  79.         CloseCPort(&colorPort);
  80.     }
  81.     else {
  82.         OpenPort(&bwPort);
  83.         DrawBWIcon(iconFamilyID, &destRect);
  84.         ClosePort(&bwPort);
  85.     }
  86.     
  87.     if (advance)
  88.         AdvanceIconPosition (&destRect);
  89.         
  90.     SetA5(oldA5);                                             // Restore A5 to its previous value
  91. }
  92.  
  93. // ---------------------------------------------------------------------------------------------------------------------
  94. // A checksum is used to make sure that the data in there was left by another ShowINIT-aware INIT.
  95.  
  96. static unsigned short CheckSum (unsigned short x)
  97. {
  98.     return ((x << 1) | (x >> 15)) ^ 0x1021;
  99. }
  100.  
  101. // ---------------------------------------------------------------------------------------------------------------------
  102. // ComputeIconRect computes where the icon should be displayed.
  103.  
  104. static void ComputeIconRect (Rect* iconRect, Rect* screenBounds)
  105. {
  106.     if (CheckSum(LMHCoord) != LMHCheckSum)                    // If we are first, we need to initialize the shared data.
  107.         LMHCoord = 8;
  108.     if (CheckSum(LMVCoord) != LMVCheckSum)
  109.         LMVCoord = screenBounds->bottom - 40;
  110.     
  111.     if (LMHCoord + 34 > screenBounds->right) {                    // Check whether we must wrap
  112.         iconRect->left = 8;
  113.         iconRect->top = LMVCoord - 40;
  114.     }
  115.     else {
  116.         iconRect->left = LMHCoord;
  117.         iconRect->top = LMVCoord;
  118.     }
  119.     iconRect->right = iconRect->left + 32;
  120.     iconRect->bottom = iconRect->top + 32;
  121. }
  122.  
  123. // AdvanceIconPosition updates the shared global variables so that the next extension will draw its icon beside ours.
  124.  
  125. static void AdvanceIconPosition (Rect* iconRect)
  126. {
  127.     LMHCoord = iconRect->left + 40;                            // Update the shared data
  128.     LMVCoord = iconRect->top;
  129.     LMHCheckSum = CheckSum(LMHCoord);
  130.     LMVCheckSum = CheckSum(LMVCoord);
  131. }
  132.  
  133. // DrawBWIcon draws the 'ICN#' member of the icon family. It works under System 6.
  134.  
  135. static void DrawBWIcon (short iconID, Rect *iconRect)
  136. {
  137.     Handle        icon;
  138.     BitMap        source, destination;
  139.     GrafPtr        port;
  140.     
  141.     icon = Get1Resource('ICN#', iconID);
  142.     if (icon != NULL) {
  143.         HLock(icon);
  144.                                                         // Prepare the source and destination bitmaps.
  145.         source.baseAddr = *icon + 128;                        // Mask address.
  146.         source.rowBytes = 4;
  147.         SetRect(&source.bounds, 0, 0, 32, 32);
  148.         GetPort(&port);
  149.         destination = port->portBits;
  150.                                                         // Transfer the mask.
  151.         CopyBits(&source, &destination, &source.bounds, iconRect, srcBic, nil);
  152.                                                         // Then the icon.
  153.         source.baseAddr = *icon;
  154.         CopyBits(&source, &destination, &source.bounds, iconRect, srcOr, nil);
  155.     }
  156. }
  157.  
  158. // ---------------------------------------------------------------------------------------------------------------------
  159. // Notes
  160.  
  161. // Checking for PlotIconID:
  162. // We (PNL) now check for system 7 and colour QD, and use colour graf ports and PlotIconID only if both are true
  163. // Otherwise we use B&W grafport and draw using PlotBWIcon.
  164.