home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Appearance SDK 1.0.4 / Appearance Sample Code / Source / StandardAlert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-16  |  7.7 KB  |  256 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        StandardAlert.c
  3.  
  4.     Contains:    Sample code showing the use of StandardAlert.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (edv)    Ed Voas
  21.  
  22.     Change History (most recent first):
  23.  
  24.          <3>    12/17/97    edv        Move comment to the right place!
  25.          <2>     10/1/97    edv        Add comment for ModalDialog call.
  26.          <1>     9/11/97    edv        First checked in.
  27. */
  28.  
  29. #include <Appearance.h>
  30. #include "UDialogUtils.h"
  31.  
  32. //——————————————————————————————————————————————————————————————————————————
  33. //    Dialog Item numbers
  34. //——————————————————————————————————————————————————————————————————————————
  35.  
  36. enum
  37. {
  38.     kOKButton            = 1,
  39.     kCancelButton        = 2,
  40.     kErrorText            = 4,
  41.     kExplainText        = 6,
  42.     kMovableCheck        = 7,
  43.     kButtonGroup        = 8,
  44.     kButton1Group        = 9,
  45.     kButton2Group        = 10,
  46.     kButton3Group        = 11,
  47.     kButton2Check        = 13,
  48.     kButton3Check        = 14,
  49.     kUseDefault1Check    = 15,
  50.     kUseDefault2Check    = 16,
  51.     kUseDefault3Check    = 17,
  52.     kButton1Text        = 18,
  53.     kButton2Text        = 19,
  54.     kButton3Text        = 20,
  55.     kTypePopup            = 22,
  56.     kHelpCheck            = 23
  57. };
  58.  
  59. void    TestStandardAlert();
  60.  
  61. //———————————————————————————————————————————————————————————————————————————
  62. //    • TestStandardAlert
  63. //———————————————————————————————————————————————————————————————————————————
  64. //    This routine puts up an alert using the StandardAlert routine. It first
  65. //    requests information via a dialog. It uses a 'dlog' resource for this
  66. //    dialog and has embedding enabled. After reviewing the DITL in ResEdit
  67. //    and looking at the code below, it should provide some good clues as to
  68. //    how you can take advantage of embedding features. You'll notice that I've
  69. //    used it implicity below when I disable the contents of the secondary
  70. //    groups containing the 'Use Default' check box and the button name edit
  71. //    text box. This allows me to properly save the state of the Use Default
  72. //    check box when checking and unchecking the 'Button <n>' check boxes. If I
  73. //    had the 'Use Default' check box off when I click the Button 2 check box
  74. //    off, the next time I turn the Button 2 check box on, the Use Default button
  75. //    will still be enabled.
  76. //
  77. //    You'll also notice just how easy it is to disable edit text fields here.
  78. //    Its just a call to the utility to enable/disable a dialog item. When in
  79. //    embedding mode, all items are controls, so you can treat everything the
  80. //    same way!
  81. //
  82. //    This routine also shows an example of the SetControlFontStyle routine,
  83. //    which allows you to set the font of any control that supports font
  84. //    styles. All new controls delivered with appearance that display text
  85. //    obey the new font style doodad. Alternatively, we could have created a
  86. //    'dftb' resource to set the font styles.
  87. //
  88. void
  89. TestStandardAlert()
  90. {
  91.     DialogPtr        dialog;
  92.     SInt16            itemHit;
  93.     Str255            text, desc;
  94.     Str63            button1Text, button2Text, button3Text;
  95.     StringPtr        btn1, btn2, btn3;
  96.     Boolean            movable;
  97.     SInt16            alertType;
  98.     ControlFontStyleRec    style;
  99.     Boolean            useHelp;
  100.     AlertStdAlertParamRec    param;
  101.     
  102.     dialog = GetNewDialog( 1000, nil, (WindowPtr)-1L );
  103.     if ( dialog == nil ) return;
  104.     
  105.     SetDialogDefaultItem( dialog, kOKButton );
  106.     SetDialogCancelItem( dialog, kCancelButton );
  107.     
  108.     SelectDialogItemText( dialog, kErrorText, 0, 32768 );
  109.     
  110.     UDialogUtils::ToggleCheckBox( dialog, kUseDefault1Check );
  111.     UDialogUtils::ToggleCheckBox( dialog, kUseDefault2Check );
  112.     UDialogUtils::ToggleCheckBox( dialog, kUseDefault3Check );
  113.  
  114.     UDialogUtils::EnableDialogItem( dialog, kButton1Text, false );
  115.     UDialogUtils::EnableDialogItem( dialog, kButton2Text, false );
  116.     UDialogUtils::EnableDialogItem( dialog, kButton3Text, false );
  117.  
  118.     UDialogUtils::EnableDialogItem( dialog, kButton2Group, false );
  119.     UDialogUtils::EnableDialogItem( dialog, kButton3Group, false );
  120.  
  121.     style.flags = kControlUseFontMask;
  122.     style.font = kControlFontSmallBoldSystemFont;
  123.     UDialogUtils::SetFontStyle( dialog, kButtonGroup, style );
  124.  
  125.     ShowWindow( dialog );
  126.     
  127.     itemHit = 0;
  128.     while ( itemHit != kOKButton && itemHit != kCancelButton )
  129.     {
  130.         ModalDialog( nil, &itemHit );
  131.         
  132.         switch ( itemHit )
  133.         {
  134.             case kMovableCheck:
  135.             case kHelpCheck:
  136.                 UDialogUtils::ToggleCheckBox( dialog, itemHit );
  137.                 break;
  138.             
  139.             case kButton2Check:
  140.                 UDialogUtils::ToggleCheckBox( dialog, kButton2Check );
  141.                 if ( UDialogUtils::GetDialogItemValue( dialog, kButton2Check ) == 0 )
  142.                     UDialogUtils::EnableDialogItem( dialog, kButton2Group, false );
  143.                 else
  144.                     UDialogUtils::EnableDialogItem( dialog, kButton2Group, true );
  145.                 break;
  146.             
  147.             case kButton3Check:
  148.                 UDialogUtils::ToggleCheckBox( dialog, kButton3Check );
  149.                 if ( UDialogUtils::GetDialogItemValue( dialog, kButton3Check ) == 0 )
  150.                     UDialogUtils::EnableDialogItem( dialog, kButton3Group, false );
  151.                 else
  152.                     UDialogUtils::EnableDialogItem( dialog, kButton3Group, true );
  153.                 break;
  154.             
  155.             case kUseDefault1Check:
  156.                 UDialogUtils::ToggleCheckBox( dialog, kUseDefault1Check );
  157.                 if ( UDialogUtils::GetDialogItemValue( dialog, kUseDefault1Check ) == 1 )
  158.                     UDialogUtils::EnableDialogItem( dialog, kButton1Text, false );
  159.                 else
  160.                     UDialogUtils::EnableDialogItem( dialog, kButton1Text, true );
  161.                 break;
  162.                 
  163.             case kUseDefault2Check:
  164.                 UDialogUtils::ToggleCheckBox( dialog, kUseDefault2Check );
  165.                 if ( UDialogUtils::GetDialogItemValue( dialog, kUseDefault2Check ) == 1 )
  166.                     UDialogUtils::EnableDialogItem( dialog, kButton2Text, false );
  167.                 else
  168.                     UDialogUtils::EnableDialogItem( dialog, kButton2Text, true );
  169.                 break;
  170.                 
  171.             case kUseDefault3Check:
  172.                 UDialogUtils::ToggleCheckBox( dialog, kUseDefault3Check );
  173.                 if ( UDialogUtils::GetDialogItemValue( dialog, kUseDefault3Check ) == 1 )
  174.                     UDialogUtils::EnableDialogItem( dialog, kButton3Text, false );
  175.                 else
  176.                     UDialogUtils::EnableDialogItem( dialog, kButton3Text, true );
  177.                 break;
  178.         }
  179.             
  180.     }
  181.     if ( itemHit == kOKButton )
  182.     {
  183.         
  184.         alertType = UDialogUtils::GetDialogItemValue( dialog, kTypePopup ) - 1;
  185.         
  186.         movable = UDialogUtils::GetDialogItemValue( dialog, kMovableCheck ) == 1;
  187.         
  188.         UDialogUtils::GetDialogItemText( dialog, kErrorText, text );
  189.         UDialogUtils::GetDialogItemText( dialog, kExplainText, desc );
  190.         
  191.         if ( UDialogUtils::GetDialogItemValue( dialog, kUseDefault1Check ) == 1 )
  192.         {
  193.             btn1 = (StringPtr)-1L;
  194.         }
  195.         else
  196.         {
  197.             UDialogUtils::GetDialogItemText( dialog, kButton1Text, button1Text );
  198.             btn1 = button1Text;
  199.         }
  200.         
  201.         if ( UDialogUtils::GetDialogItemValue( dialog, kButton2Check ) == 1 )
  202.         {
  203.             if ( UDialogUtils::GetDialogItemValue( dialog, kUseDefault2Check ) == 1 )
  204.             {
  205.                 btn2 = (StringPtr)-1L;
  206.             }
  207.             else
  208.             {
  209.                 UDialogUtils::GetDialogItemText( dialog, kButton2Text, button2Text );
  210.                 btn2 = button2Text;
  211.             }
  212.         }
  213.         else
  214.             btn2 = nil;
  215.             
  216.         if ( UDialogUtils::GetDialogItemValue( dialog, kButton3Check ) == 1 )
  217.         {
  218.             if ( UDialogUtils::GetDialogItemValue( dialog, kUseDefault3Check ) == 1 )
  219.             {
  220.                 btn3 = (StringPtr)-1L;
  221.             }
  222.             else
  223.             {
  224.                 UDialogUtils::GetDialogItemText( dialog, kButton3Text, button3Text );
  225.                 btn3 = button3Text;
  226.             }
  227.         }
  228.         else
  229.             btn3 = nil;
  230.         
  231.         useHelp = ( UDialogUtils::GetDialogItemValue( dialog, kHelpCheck ) == 1 );
  232.  
  233.             // Finally! Now we can call StandardAlert.
  234.             
  235.         DisposeDialog( dialog );
  236.         
  237.             // I'm not passing a filter proc here. So, if you want to handle
  238.             // update events in the background windows, add one here. I've left that
  239.             // as an exercise for you, the good people of Mac-dom.
  240.             
  241.         param.movable         = movable;
  242.         param.filterProc     = nil;
  243.         param.defaultText     = btn1;
  244.         param.cancelText     = btn2;
  245.         param.otherText     = btn3;
  246.         param.helpButton     = useHelp;
  247.         param.defaultButton = kAlertStdAlertOKButton;
  248.         param.cancelButton     = btn2 ? kAlertStdAlertCancelButton : 0;
  249.         param.position         = 0;
  250.         
  251.         StandardAlert( alertType, text, desc, ¶m, &itemHit );
  252.     }
  253.     else
  254.         DisposeDialog( dialog );
  255. }
  256.