home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 December / macformat-031.iso / mac / Shareware City / Developers / CopyBits Demo 3.1 / Alert Utilities.c next >
Encoding:
Text File  |  1995-05-28  |  3.7 KB  |  131 lines  |  [TEXT/MMCC]

  1. //
  2. // Alert Utilities.c v1.0
  3. //
  4. // by Kenneth Worley
  5. // Public Domain
  6. //
  7. // Contact me at:   America Online: KNEworley
  8. //             internet: KNEworley@aol.com or kworley@fascination.com
  9. //
  10. // Routines that make it easier to show a quick error or
  11. // informational dialog.
  12. //
  13.  
  14. #include "Alert Utilities.h"
  15. #include <QDOffscreen.h>
  16.  
  17. void    DoAlert( short dialogID, Boolean playAlert )
  18. {
  19.     /*    This routine calls DoAlertStr with an empty error string. It is
  20.      *    expected that the dialog ID you specify is a dialog that already
  21.      *    has appropriate error text in it.
  22.      */
  23.     
  24.     DoAlertStr( dialogID, playAlert, "\p" );
  25. }
  26.  
  27.  
  28. void    DoAlertStrID( short dialogID, Boolean playAlert, short errStrID )
  29. {
  30.     /*    This routine loads the 'STR ' resource with ID errStrID and sends
  31.      *    the string and other parameters on to DoAlertStr.
  32.      */
  33.     
  34.     StringHandle        errString;
  35.     
  36.     errString = GetString( errStrID );
  37.     HLock( (Handle)errString );
  38.     DoAlertStr( dialogID, playAlert, *errString );
  39.     HUnlock( (Handle)errString );
  40.     ReleaseResource( (Handle)errString );
  41. }
  42.  
  43. void    DoAlertStrIndexID( short dialogID, Boolean playAlert,
  44.             short errStrID, short index )
  45. {
  46.     /*    This routine loads the string in the 'STR#' resource at the index
  47.      *    specified and passes the string and other parameters to the
  48.      *    DoAlertStr routine.
  49.      */
  50.     
  51.     Str255        errString;
  52.     
  53.     GetIndString( errString, errStrID, index );
  54.     DoAlertStr( dialogID, playAlert, errString );
  55. }
  56.  
  57. void    DoAlertStr( short dialogID, Boolean playAlert, Str255 errString )
  58. {
  59.     /*    This routine displays an alert whose resource ID number is specified
  60.      *    in the parameter dialogID.  The dialog specified is expected to have
  61.      *    at least one button (usually labeled "OK") which will be the default
  62.      *    button and have an ID if 1.  When the user presses this button, the
  63.      *    alert will be dismissed. The dialog should also have a static text
  64.      *    item with an ID of 2. If the string in errString is not zero length,
  65.      *    that string is displayed in the static text field. If the string
  66.      *    is zero length, nothing is done with the text in the dialog.
  67.      */
  68.  
  69.     CGrafPtr        currentPort;    /* store the current port here */
  70.     GDHandle        currentDev;        // store current device here
  71.     DialogPtr        theDlg;            /* to store our dialog in */
  72.     
  73.     short        itemType;        /* these 3 local variables are used to */
  74.     Handle        itemHandle;        /* manipulate items in the dialog.     */
  75.     Rect        itemRect;
  76.  
  77.     short        itemHit;        /* use with ModalDialog */
  78.  
  79.     // Save the current grafPort
  80.     
  81.         GetGWorld( ¤tPort, ¤tDev );
  82.     
  83.     // Now, load our dialog resource.
  84.     
  85.         theDlg = GetNewDialog( dialogID, NULL, (WindowPtr)-1L );
  86.     
  87.     // make the dialog the current port
  88.     
  89.         SetGWorld( (CGrafPtr)theDlg, NULL );
  90.     
  91.     // if the parameter specifies, do a system beep
  92.     
  93.         if ( playAlert )
  94.             SysBeep( 3 );
  95.     
  96.     // If a string was sent, put that string in the dialog.
  97.     
  98.         if ( errString[0] )
  99.         {
  100.             GetDialogItem( theDlg, 2, &itemType, &itemHandle, &itemRect );
  101.             SetDialogItemText( itemHandle, errString );
  102.         }
  103.     
  104.     // now make sure the dialog is visible
  105.     
  106.         ShowWindow( theDlg );
  107.  
  108.     //    Get the OK button item and draw a bold border around it to show that
  109.     //    it's the default button.
  110.  
  111.          GetDialogItem( theDlg, 1, &itemType, &itemHandle, &itemRect );
  112.          InsetRect( &itemRect, -4, -4 );
  113.          PenSize( 3, 3 );
  114.          FrameRoundRect( &itemRect, 16, 16 );
  115.          PenSize( 1, 1 );
  116.     
  117.      //    Loop and call ModalDialog until the user presses the OK button
  118.      // This routine requires that whatever alert is shown, the button with
  119.      // id number kAlertOKButton is the one that should dismiss the dialog.
  120.      
  121.          ModalDialog( NULL, &itemHit );
  122.          while ( itemHit != 1 )
  123.              ModalDialog( NULL, &itemHit );
  124.          
  125.      /*    Now get rid of the dialog and set the port back to the control panel */
  126.      
  127.          DisposDialog( theDlg );
  128.          SetGWorld( currentPort, currentDev );
  129. }
  130.  
  131.