home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / cshowint.sit / CShowINIT_INIT.c < prev    next >
C/C++ Source or Header  |  1989-10-13  |  6KB  |  140 lines

  1. /*-------------------------------------------------------------------------
  2.   Filename:     CShowINIT_INIT.c
  3.   Author:            Ken McLeod
  4.   Version of:    Wednesday, October 4, 1989 11:13:00 PM
  5.   
  6.   This file is copyright ⌐1989 by Ken McLeod.
  7.   Permission is granted to use and distribute this code freely.
  8.   
  9.   CShowINIT_INIT is a "standalone" INIT resource, designed to be pasted
  10.   into existing INIT or cdev files.  This new INIT simply finds out what
  11.   its ID is, and calls CShowINIT with that ID. By making the ID of this INIT
  12.   equal to that of the cicn or ICN# you want to display, you can add the
  13.   CShowINIT feature to any INIT file.
  14.   -------------------------------------------------------------------------*/
  15.  
  16. /* #include <MacHeaders> */
  17. #include <Color.h>
  18.  
  19. typedef struct QuickDraw {       /* struct to hold QuickDraw globals */
  20.   char private[76];
  21.   long randSeed;
  22.   BitMap screenBits;
  23.   Cursor arrow;
  24.   Pattern dkGray;
  25.   Pattern ltGray;
  26.   Pattern gray;
  27.   Pattern black;
  28.   Pattern white;
  29.   GrafPtr thePort;
  30. } QuickDraw;
  31.  
  32. /*extern short ROM85 : 0x28E;*/
  33. /*extern GDHandle MainDevice : 0x8A4;*/
  34.  
  35. extern short myH : 0x92C;        /* CurApName+28   */
  36. extern short myCheck: 0x92E;     /* CurApName+30   */
  37.  
  38. #define  firstX         8        /* left margin - offset to first icon */
  39. #define  bottomEdge     8        /* this far from bottom of screen */
  40. #define  iconWidth      32       /* size of icon (square normally) */
  41. #define  defaultMoveX   40       /* default amount to move icons */
  42. #define  checksumConst  0x1021   /* constant used for computing checksum */
  43. #define  minColorDepth  4        /* min. bits/pixel for drawing color icons */
  44. #define  maskOffset     128      /* offset to mask in ICN# resource */
  45. #define  iconRowBytes   32/8     /* 32/8 bits */
  46. #define  hasCQDBit      6        /* bit in ROM85 cleared if CQD available */
  47.  
  48. void main()
  49. {
  50.   Handle    ourCode;                            /* handle to our code resource */
  51.   Handle  theIconHdl;                      /* handle to the ICN# (or cicn) */
  52.   short    iconID;                                    /* resource ID of ICN# (or cicn) */
  53.   ResType    theType;                            /* needed for GetResInfo */
  54.   Str255    theName;                            /* needed for GetResInfo */
  55.   short     dh;                         /* for calculating horizontal offset */
  56.   short  colorFlag;             /* set if drawing a color icon */
  57.   short  theDepth;              /* depth of main screen; used for CQD only */
  58.   GDHandle theMainDevice;       /* handle to main screen device; CQD only */
  59.   Rect srcRect, destRect;       /* source & destination rectangles */
  60.   BitMap myBitMap;              /* icon bitmap; used for b/w icon only */
  61.   GrafPort myPort;              /* port we draw into */
  62.   QuickDraw qdGlobals;             /* our own personal QD globals... */
  63.   Ptr localA5;                  /* pointer to qdGlobals.thePort */
  64.   Ptr    savedA5;                                    /* storage for saved contents of A5 */
  65.   
  66.   asm {
  67.         _RecoverHandle                        ; A0 points to our code at entry
  68.         move.l    A0,ourCode                ; get handle to us
  69.         _HLock                                        ;    make sure we're locked
  70.     }
  71.     GetResInfo(ourCode, &iconID, &theType, &theName);    /* get our ID */
  72.     
  73.     asm {
  74.       move.l  A5,savedA5                ; save "real" QD globals ptr
  75.     lea          localA5,A5                ; set up A5 to point to our globals
  76.     move.l  A5,CurrentA5            ; and make them current
  77.   }
  78.   InitGraf(&qdGlobals.thePort);    /* initialize our 'qdGlobals' structure */
  79.   OpenPort(&myPort);
  80.   colorFlag = 0;                                                /* default: no color */
  81.  
  82.   if (!(BitTst(&ROM85, 7-hasCQDBit))) {    /* does CQD exist? */
  83.     theMainDevice = MainDevice;                    /* yes; get handle to main device */
  84.         theDepth = (*(*theMainDevice)->gdPMap)->pixelSize;
  85.     if (theDepth >= minColorDepth)    {        /* deep enough to draw in color? */
  86.       if ((theIconHdl = (Handle)GetCIcon(iconID)) != 0L)
  87.         colorFlag = 1;                                    /* found a color icon; set flag */
  88.     }
  89.   }
  90.  
  91.   if (!(colorFlag))    {  /* no CQD, insufficient depth, or lack of 'cicn' */
  92.     if (!(theIconHdl = GetResource('ICN#',iconID))) {
  93.       SysBeep(3);  /* can't get b/w icon; signal error and bail out */
  94.       goto out;
  95.     }
  96.   }
  97.   dh = (myH << 1) ^ checksumConst;            /* checksum to find X */
  98.   myH = ((dh == myCheck) ? (myH):(firstX));   /* reset if necessary */
  99.   /* notice that we stored the new horizontal value directly back into
  100.       the low-memory 'myH' location, rather than using a temporary variable.
  101.       This is the way the original ShowINIT works, and IconWrap relies on it. */
  102.  
  103.   destRect.bottom = myPort.portRect.bottom - bottomEdge;
  104.   destRect.left = myPort.portRect.left + myH;
  105.   destRect.top = destRect.bottom - iconWidth;
  106.   destRect.right = destRect.left + iconWidth;
  107.  
  108.   if (colorFlag) {                                /* draw color icon */
  109.       PlotCIcon(&destRect,(CIconHandle)theIconHdl);
  110.     DisposCIcon((CIconHandle)theIconHdl);
  111.   }
  112.   else {                                          /* draw b/w icon */
  113.     HLock(theIconHdl);
  114.     srcRect.top = srcRect.left = 0;
  115.     srcRect.bottom = srcRect.right = iconWidth;
  116.     myBitMap.rowBytes = iconRowBytes;
  117.     myBitMap.bounds = srcRect;
  118.     myBitMap.baseAddr = *theIconHdl + maskOffset; /* punch hole with mask */
  119.     CopyBits(&myBitMap, &myPort.portBits, &srcRect, &destRect, srcBic, 0L);
  120.     myBitMap.baseAddr = *theIconHdl;              /* now draw the icon */
  121.     CopyBits(&myBitMap, &myPort.portBits, &srcRect, &destRect, srcOr, 0L);
  122.     HUnlock(theIconHdl);
  123.     ReleaseResource(theIconHdl);
  124.   }
  125.   myH += defaultMoveX;                                              /* advance for next time */
  126.   myCheck = (myH << 1) ^ checksumConst;       /* calc new checksum */
  127.  
  128. out:
  129.   ClosePort(&myPort);
  130.   asm {
  131.     move.l  savedA5,A5
  132.     move.l  A5,CurrentA5
  133.     move.l    ourCode,A0
  134.     _HUnlock
  135.   }
  136.  
  137. }
  138.  
  139. /*-------------------------------------------------------------------------*/
  140.