home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / ShowInitAndName / ShowInitAndName.h < prev    next >
Encoding:
Text File  |  1996-01-12  |  19.3 KB  |  471 lines  |  [TEXT/CWIE]

  1. /***************************************************************************************************
  2.  
  3.                                          ShowInitAndName   
  4.    
  5.    ShowInitAndName includes routines to display an icon and title at startup, as well as
  6.    a Notification Manager routine for displaying error messages to the user.
  7.    
  8.    A few bonus support routines are also available for Pascal & C string copying, along with
  9.    easy calls for information regarding the System Software and Color Quickdraw.
  10.    
  11.    These routines make writing cool System Extensions (INITs) a whole lot easier.
  12.    I hope you’ll find them to be easy to program and stable under all conditions.
  13.    
  14.    
  15.    Please donate bug reports, feature requests, and other improvements to:
  16.  
  17.    Storm Impact, Inc.
  18.    316 Nottingham Ave.
  19.    Glenview, IL 60025-5022
  20.    USA
  21.       
  22.    StormImpac@aol.com
  23.    
  24.  
  25.    What Makes These Routines Easy To Use?
  26.    --------------------------------------
  27.    • You MAY pass nil for ANY pointer in ANY routine without bombing or corruption.
  28.      (If you don’t supply enough information, the routine will simply do nothing.)
  29.    
  30.    • You DON’T NEED TO INITIALIZE any toolbox routine (or port or whatever) before calling
  31.      ShowInitAndName. It does that work for you.
  32.    
  33.    • You DON’T NEED TO DETERMINE whether the appropriate Mac or System Software is installed.
  34.      (Each routine does its own checking of the environment.)
  35.   
  36.    • You DON’T NEED TO INCLUDE additional libraries or source code. It’s all here.
  37.      (Of course, most compilers require the inclusion of MacTraps or InterfaceLib.o for any Mac OS project)
  38.    
  39.    • You MAY use none, one, or all of these routines, in any order. It’s up to you.
  40.    
  41.    • You MAY read the “Inside Macintosh”-type documentation for each routine.
  42.      (The documentation is at the start of each routine within the “.c” file itself.)
  43.    
  44.    • You MAY examine the ShowInitAndName example source code for a number of examples
  45.      of these routines in action.
  46.    
  47.    • You MAY compile these routines to PowerPC or 680x0 targets. There’s no assembly or
  48.      processor hacks.
  49.    
  50.    • You MAY use 'cicn' resources for ShowInitAndName, or you may use the more
  51.      popular 'icl8', 'icl4', and 'ICN#' resources.
  52.      
  53.    
  54.    What If Icons Are Shown Differently In FUTURE Systems?
  55.    ------------------------------------------------------
  56.    If available, these routines call a System-based ShowInit instead of showing the icon and
  57.    name themselves. If a future programmer comes up with a completely new way of showing inits,
  58.    or if a user likes the old ShowInit style, these routines automatically yield to the System.
  59.    
  60.    Also, all calls to the Notification Manager are made with nmRefCon set to a particular
  61.    constant ('SIaN'). If a future programmer wishes to display init error messages DURING
  62.    startup, or place the error messages in a log file, there will be a constant to patch on.
  63.       
  64.    
  65.    Version History:
  66.    ----------------
  67.    96/01/12   v1.0.0   David Cook   First released version
  68.    
  69.    If you’d like, send us email at “StormImpac@aol.com”, and we’ll be glad to email you the
  70.    latest release version of these routines. Remember, it’s your responsibility to completely
  71.    test these routines to make sure the version you are using meets your needs and your
  72.    customer’s needs.
  73.   
  74.   
  75.    Requirements:
  76.    -------------
  77.    Compiled using: Metrowerks release 7 (but should also work with Think C 8 (what’s that?) or better.)
  78.    
  79.    Resulting object: System Software 6.0 or better
  80.                      Mac Plus to the newest (including the Power Macintosh w/PCI models).
  81.                      
  82.                      (Technically, I can’t see any reason why ShowInitAndName won’t run
  83.                       in earlier versions of System Software prior to 6.0, but I haven’t tried it.)
  84.                    
  85.   
  86.    Last Minute Notes:
  87.    ------------------
  88.    • Old B&W-Quickdraw Macs are hard coded to a screen size of 512x342, which means this code
  89.      may not wrap icons correctly on a Lisa or MacXL.
  90.       
  91.    • Non-32x32 'cicn' resources aren’t directly supported. However, I don’t know
  92.      of anyone that uses 'cicn's that aren’t the standard 32 pixels by 32 pixels.
  93.       
  94.    • Some old extension “managers” and “wrappers” were designed to forcibly override
  95.      the inits’ icon positioning and drawing routines. Although my code attempts to
  96.      accept the overridden drawing position, many old managers don’t provide feedback to
  97.      their overrides. In the case of Now Startup Manager 3.0.2 (as an example), the extension name
  98.      may be drawn far below the icon, because Now Startup silently moves the icon up without any
  99.      way for my code to discover the change.
  100.  
  101.  
  102.    Thanks:
  103.    -------
  104.    Thanks to the authors of “ShowInit.a”: Paul Mercer, Darin Adler, and Paul Snively, from an idea by Steve Capps.
  105.    Not only were they the first to show init icons at startup, but they made their code
  106.    publicly available so that all Macintosh developers could gain the ability.
  107.    They set the standard by which all future routines must live up to.
  108.  
  109.  
  110.    Bibliography:
  111.    -------------   
  112.         Bassindale, Brian W. “ShowINITIcon1.1.p”, Developer’s Forum. America Online, Vienna, VA., 
  113.    October 1995
  114.         
  115.         Beard, Patrick, and James W. Walker. “ShowIconFamily.c”, Developer’s Forum. America Online,
  116.    Vienna, VA., October 1995
  117.         
  118.         Faigle, Chris. “Animated SI 1.0a2.c”, Developer’s Forum. America Online, Vienna, VA.,
  119.    October 1995
  120.         
  121.         Grant, Dair. “ShowIcon.c” from Extension Shell 1.3, Developer’s Forum. America Online,
  122.    Vienna, VA., October 1995
  123.         
  124.         Lewis, Peter, and James W. Walker. “ShowInitIcon.c” [Not related to Eric Shapiro’s of the same name],
  125.    Developer’s Forum. America Online, Vienna, VA., October 1995.
  126.         
  127.         Mercer, Paul, Darin Adler, and Paul Snively. “ShowINIT.a” [7/14/88], CD Slickers, Tool Chest,
  128.    Developers CD Series. Apple Computer, Inc., Cupertino, CA., August 1993.
  129.    
  130.         †Mercer, Paul, Darin Adler, Paul Snively, Frédéric Miserey, and Alex Rosenberg. “ShowInit.a” [3/27/90],
  131.    Tool Chest, Developers CD Series. Apple Computer, Inc., Cupertino, CA., May 1995.
  132.    
  133.         †Mercer, Paul, Darin Adler, Paul Snively, Brad Lowe. “ShowINIT.a” [1/21/91], A ROM With a View,
  134.    Developers CD Series. Apple Computer, Inc., Cupertino, CA., September 1992.
  135.    
  136.         †Mercer, Paul, Darin Adler, Paul Snively, Brad Lowe, and MK. “ShowINIT.a” [9/9/92], Reference Library,
  137.    Developers CD Series. Apple Computer, Inc., Cupertino, CA., September 1995.
  138.  
  139.         Pottier, François, and Peter N. Lewis. “ShowInit-75.p”, Reference Library,
  140.    Developers CD Series. Apple Computer, Inc., Cupertino, CA., September 1995.
  141.    
  142.         Shapiro, Eric, and James W. Walker. “ShowInitIcon.c” [Not related to Peter Lewis’s of the same name],
  143.    Tool Chest, Developers CD Series. Apple Computer, Inc., Cupertino, CA., May 1995.
  144.    
  145.         Walker, James W., and Patrick C. Beard. “ShowIcon7.c”, Developer’s Forum. America Online,
  146.    Vienna, VA., October 1995
  147.    
  148.         Walker, James W., Patrick C. Beard, and Brian Bechtel. “ShowIcon7.c”, Tool Chest, Developers CD Series.
  149.    Apple Computer, Inc., Cupertino, CA., May 1995.
  150.    
  151.    
  152.    †WARNING: There are a number of versions of “ShowINIT.a” which took different branches because several
  153.              authors all started from Paul Mercer’s 7/14/88 source code. Although named identically,
  154.              these branch versions of “ShowINIT.a” are very different from each other. Beware!
  155.     
  156.     
  157.    The Vertical Position Problem:
  158.    ------------------------------
  159.    The HORIZONTAL position (and a checksum for safety) of an init’s icon and text is stored
  160.    in the last 4 bytes of the current application’s filename, as has been the method since
  161.    “ShowInit.a” 7/1/87. Since there is no current application at startup, the end of the
  162.    CurApName has continued to be an acceptable storage location.
  163.    ShowInit.a: “a GREAT place to store 4 bytes (it was Darin's idea)”
  164.    
  165.    BUT, OH NO! Two different programmers, at around the same time, came up with two
  166.    different methods for storing vertical positions to supplement ShowInit’s horizontal position.
  167.  
  168.    Starting with Frédéric Miserey’s version of “ShowInit.a” 11/25/89, the VERTICAL position
  169.    (and a checksum for safety) is stored in the 4 bytes immediately before the horizontal location
  170.    in the current application’s filename. This is the same method that we use in
  171.    ShowInitAndName.
  172.  
  173.    However, James W. Walker’s pre-1994(?) routines use a different approach to the VERTICAL
  174.    position. Those routines allow the stored horizontal position to exceed the width of the screen,
  175.    but then “wrap” the horizontal position up to the next screen row, and the next row, and so on.
  176.    This seemed like a nice idea, because it used 4 bytes fewer of CurApName space.
  177.    Unfortunately, anyone using Miserey’s approach prevents Walker’s “wrap” method
  178.    from working.
  179.    
  180.    Have you ever seen an icon appear on the 2nd row, then an icon on the 1st row, and then an
  181.    icon back on the 2nd row -- in correct horizontal order? Well, then you observed
  182.    ShowInit 1989+ followed by any other method, followed by ShowInit 1989+.
  183.  
  184.    To discover the most popular init method, I inspected most of the INIT resources
  185.    I could find. Here are some of the results:
  186.  
  187.    Doesn’t read or store vertical position:
  188.         Speech Manager 1.4   - An IconDispatch based routine
  189.         Sound Manager 3.1    - An IconDispatch based routine
  190.  
  191.         AppleScript 1.1      - “ShowInit.a” 7/14/88 [ Which Paul Mercer labeled 7/15/88 ]
  192.         On Location 2.0.1    - “ShowInit.a” 7/14/88 [ Which Paul Mercer labeled 7/15/88 ]
  193.         DebuggerINIT 1.1.2   - “ShowInit.a” 7/14/88 [ Which Paul Mercer labeled 7/15/88 ]
  194.         QuicKeys 3.0.1       - “ShowInit.a” 7/14/88 [ Which Paul Mercer labeled 7/15/88 ]
  195.         Video Startup 1.3.1  - “ShowInit.a” 7/14/88 [ Which Paul Mercer labeled 7/15/88 ]
  196.  
  197.    Reads and stores vertical position:
  198.         ATI Displays 1.0     - An IconDispatch based routine
  199.  
  200.         QuickTime 2.0.9      - A custom CopyMask and clut based routine
  201.    
  202.         Capture 4.0.2        - A CopyMask and clut based routine
  203.         AppleGuide 1.2.7     - A CopyMask and clut based routine
  204.  
  205.         Disinfectant 3.6     - “ShowInit.a” 11/25/89
  206.  
  207.         Now Utilities 5.0.1  - “ShowInit.a” 11/25/89, Modified by jbx 7/13/92
  208.  
  209.         Turbo Mouse 4.0.2    - “ShowInit.a” 03/27/90
  210.         PC Exchange 2.0.5    - “ShowInit.a” 03/27/90
  211.         ColorSync 2.0        - “ShowInit.a” 03/27/90
  212.  
  213.    The biggest shock is that in System 7.5.2 Apple itself uses at least 4 different versions of
  214.    routines for showing inits at startup! And, some of those routines fail on the second row
  215.    (or any vertical positioning).
  216.  
  217.    Names and trademarks mentioned throughout this code are not associated with David Cook or
  218.    Storm Impact, Inc. My analysis of their code could be wrong; it’s only an educated guess.
  219.  
  220.  
  221.    Legal Information:
  222.    ------------------
  223.    ShowInitAndName.c (including all C, headers, examples, and resource files) is
  224.    Copyright © 1996 by Storm Impact, Inc. All Rights Reserved. Storm Impact and the Storm Impact
  225.    logo are trademarks of Storm Impact, Inc.
  226.    
  227.    
  228.    THESE ROUTINES ARE AS-IS. THEY MAY CONTAIN SIGNIFICANT BUGS.
  229.    
  230.    You may include any or all of these routines as compiled executables, free of charge from us, and you
  231.    may also distribute COMPLETE, UNMODIFIED copies of the source code, free of charge from us. In exchange,
  232.    you assume all liabilities, risks, and costs associated with using or distributing these routines, and 
  233.    you agree to provide us a free copy of your complete product within thirty (30) days of publication or
  234.    public release. (If your product is not offered publicly, you don’t have to send us a copy.)
  235.    
  236.    If you need other legal arrangements, custom modifications, or the latest version, please feel free to
  237.    contact us at the address at the beginning of this document.
  238.    
  239.    The artwork and icons included in the examples may not be used or distributed except as part of the
  240.    complete and unmodified source code. Software Of The Month Club is prohibited from distributing
  241.    any and/or all of this under any and/or all circumstances. So there.
  242.    
  243.  
  244.    Enjoy!
  245.    
  246.    David Cook
  247.    Storm Impact, Inc.    
  248. */
  249.  
  250.  
  251. #include <Icons.h>
  252.  
  253. /* Here are some definitions if your compiler doesn’t have them */
  254. /*
  255.      #define true     1
  256.      #define false    0
  257.    
  258.      #define LMGetCurApName() ((StringPtr) 0x0910) 
  259.      #define LMSetCurApName(pascalStringPtr) ()
  260. */
  261.  
  262. #define kIconResourceIDNone      0  /* Pass this whenever you don’t want to display an icon but the routine calls for an icon resource id. */ 
  263.  
  264.  
  265.  
  266. /************ This routine is all you need to show an init with a C filename ***********/
  267.  
  268. OSErr ShowInitAndName ( short iconResourceID, char *extensionNameInCFormat, char *statusMessageInCFormat, OSErr errorEncounteredIfAny, Boolean allDone );
  269.  
  270.  
  271. /************ Or, use this routine if you have a Pascal format filename ***********/
  272. OSErr ShowInitAndPascalName ( short iconResourceID, StringPtr extensionNameInPascalFormat, StringPtr statusMessageInPascalFormat, OSErr errorEncounteredIfAny, Boolean allDone );
  273.  
  274.  
  275.  
  276.  
  277. /********************** BONUS ROUTINES ***************************
  278.     
  279. /* This routine is all you need to display a c-format string alert message to the user */
  280. OSErr NotificationManagerMessage ( char *messageInCFormat );
  281.  
  282. /* Simple environment checking routines which you may (or may not) find useful in your init */
  283. Boolean IsColorQuickdrawAvailable ( void );
  284. long GetSystemVersion ( void );
  285.  
  286.  
  287. /* Pascal and C String routines which you may (or may not) find useful in your init */
  288. unsigned long GetLengthOfCStringWithLimit ( char *pointerToCString, unsigned long countNoMoreThanThisManyCharacters );
  289. void CopyCStringFromPascalStringWithLimit ( char *pointerToDestinationCString, StringPtr pointerToSourcePascalString, Byte copyNoMoreThanThisManyCharacters );
  290.  
  291.  
  292. /* All of the above routines have “Inside Macintosh”-like documentation at the start */
  293. /* of each routine within the “ShowInitAndName.c” source file code itself. Take a look!         */
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323. /* ATTENTION: The rest of this header file contains internal routines and constants. */
  324. /* You may read it if you’d like, of course. */
  325.  
  326.  
  327. #if PRAGMA_ALIGN_SUPPORTED
  328. #pragma options align=mac68k
  329. #endif
  330. typedef struct
  331. {
  332.      char       privates[76];
  333.      long       randSeed;
  334.      BitMap       screenBits;
  335.      Cursor       arrow;
  336.      Pattern   dkGray;
  337.      Pattern   ltGray;
  338.      Pattern   gray;
  339.      Pattern   black;
  340.      Pattern   white;
  341.      GrafPtr   thePort;
  342.      long      setTheLocalA5ToHere;
  343. } QDGlobalsPlusAStartForTheLocalA5;
  344. #if PRAGMA_ALIGN_SUPPORTED
  345. #pragma options align=reset
  346. #endif
  347.  
  348.  
  349.  
  350.  
  351. #define gestaltShowInitAndName               'SIaN'
  352. #define kNotificationSentByShowInitAndName   gestaltShowInitAndName
  353.  
  354. typedef pascal OSErr (*ShowInitOverrideProcPtr)(short iconResourceID, char *extensionNameInCFormat, char *statusMessageInCFormat, OSErr errorEncounteredIfAny, Boolean allDone, long reservedForFutureExpansion );
  355.  
  356. #if GENERATINGCFM
  357. typedef UniversalProcPtr ShowInitOverrideUPP;
  358. #else
  359. typedef ShowInitOverrideProcPtr ShowInitOverrideUPP;
  360. #endif
  361.  
  362. enum {
  363.     showInitOverrideProcInfo = kThinkCStackBased
  364.          | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  365.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
  366.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char*)))
  367.          | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(char*)))
  368.          | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(OSErr)))
  369.          | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(Boolean)))
  370.          | STACK_ROUTINE_PARAMETER(6, SIZE_CODE(sizeof(long)))
  371. };
  372.  
  373. #if GENERATINGCFM
  374. #define NewShowInitOverrideProc(userRoutine)        \
  375.         (ShowInitOverrideUPP) NewRoutineDescriptor((ProcPtr)(userRoutine),(showInitOverrideProcInfo),(GetCurrentArchitecture()))
  376. #else
  377. #define NewShowInitOverrideProc(userRoutine)        \
  378.         ((ShowInitOverrideUPP) (userRoutine))
  379. #endif
  380.  
  381. #if GENERATINGCFM
  382. #define CallShowInitOverrideProc(userRoutine, iconResourceID, extensionNameInCFormat, statusMessageInCFormat, errorEncounteredIfAny, allDone, reservedForFutureExpansion)        \
  383.         CallUniversalProc((UniversalProcPtr)(userRoutine),(showInitOverrideProcInfo),(iconResourceID),(extensionNameInCFormat), (statusMessageInCFormat), (errorEncounteredIfAny), (allDone), (reservedForFutureExpansion))
  384. #else
  385. #define CallShowInitOverrideProc(userRoutine, iconResourceID, extensionNameInCFormat, statusMessageInCFormat, errorEncounteredIfAny, allDone, reservedForFutureExpansion)        \
  386.                                                                  (*(userRoutine))((iconResourceID),(extensionNameInCFormat),(statusMessageInCFormat),(errorEncounteredIfAny),(allDone),(reservedForFutureExpansion))
  387. #endif
  388.  
  389. #define kShowInitHasOverridden_NoNeedForLocalCode    1
  390.  
  391.  
  392.  
  393.  
  394.      
  395. #define kHeightOfIcon  32
  396. #define kWidthOfIcon   32
  397.  
  398. #define kMinimumPixelDepthToDrawColorIcons  4
  399.  
  400. #define kByteOffsetToICNMask                 128   /* offset to mask in ICN# resource */
  401. #define kBitmapRowBytesForBWIcon          (32/8)
  402.                                    
  403.  
  404.  
  405. #define kTextCStringFont          geneva
  406. #define kTextCStringFontSize           9
  407. #define kTextCStringAlignment   teCenter
  408. #define kTextCStringFace          normal
  409.  
  410. #define kTextCStringMaximumNumberOfCharacters   255
  411.  
  412. #define kTextCStringHeight                       12
  413. #define kTextCStringHorizontalBorderWhiteSpace    4 /* Two pixels on each side */
  414.  
  415.  
  416.  
  417. #define kShowInitMarginForMainScreenLeft       8
  418. #define kShowInitMarginForMainScreenRight      8
  419. #define kShowInitMarginForMainScreenTop        8
  420. #define kShowInitMarginForMainScreenBottom     8
  421.  
  422. #define kHorizontalMarginBetweenShowInits   8
  423. #define kVerticalMarginBetweenShowInits     8
  424.  
  425. #define kMinimumVerticalSizeOfShowInit      ( kHeightOfIcon + kTextCStringHeight )  
  426.  
  427. #define kMaximumTotalPixelWidthOfAShowInit  410
  428.  
  429.  
  430.  
  431. #define kChecksumStorageConstant    0x1021
  432.  
  433. #define kOffsetFromCurApNameToStoredVerticalChecksum      24
  434. #define kOffsetFromCurApNameToStoredVertical              26
  435. #define kOffsetFromCurApNameToStoredHorizontal            28
  436. #define kOffsetFromCurApNameToStoredHorizontalChecksum    30
  437.  
  438. #define kDefaultHorizontalOffsetFromLeftOfMainScreenIfHorizontalChecksumIsWrong       8
  439. #define kDefaultVerticalOffsetFromBottomOfMainScreenIfVerticalChecksumIsWrong        40 
  440.  
  441.  
  442.  
  443.  
  444. #define kNotificationManagerMinimumSystemRequired   0x0600
  445.  
  446. typedef struct
  447. {
  448.      NMRec notificationRecord;
  449.      
  450.      unsigned char *notificationPascalString [ ];
  451. } NMRecAndPascalStringCombinedRecord;
  452.  
  453.  
  454.  
  455.  
  456. void DrawTextCString ( char *pointerToTextCString, Rect *pointerToDestinationRect );
  457. short GetPixelWidthOfTextCString ( char *pointerToTextCString );
  458.  
  459. long GetMainScreenPixelDepth ( void );
  460. void GetMainScreenRect ( Rect *mainScreenRectPtr );
  461.  
  462. Boolean IsPlotIconIDAvailable ( void );
  463. OSErr DrawIcon ( short iconResourceID, short pixelDepthOfScreenToDrawOn, Rect *pointerToDestinationRect );
  464. void DrawCrossOut ( Rect *pointerToDestinationRect, short pixelDepthOfScreenToDrawOn );
  465.  
  466. void AdvanceShowInit ( short byHorizontalAmount );
  467. Boolean CorrectShowInitHorizontalAndVerticalIfNotOnMainScreen ( short *pointerToHorizontal, short horizontalSizeOfThisShowInit,
  468.                                                                 short *pointerToVertical, short verticalSizeOfThisShowInit      );
  469. void LMGetShowInitHorizontalAndVertical ( short *pointerToHorizontal, short *pointerToVertical );
  470. void LMSetShowInitHorizontalAndVertical ( short horizontal, short vertical );
  471. short CalculateHorizontalOrVerticalStorageChecksum ( short horizontalOrVerticalValue );