home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 December / macformat-031.iso / mac / Shareware City / Developers / CopyBits Demo 3.1 / Dialog Utilities.c < prev    next >
Encoding:
Text File  |  1995-06-27  |  3.3 KB  |  161 lines  |  [TEXT/MMCC]

  1. // Dialog Utilities.c v1.0
  2. //
  3. // by Kenneth Worley
  4. // Public Domain
  5. //
  6. // Contact me at:   America Online: KNEworley
  7. //            internet: KNEworley@aol.com or kworley@fascination.com
  8. //
  9. // Functions to make working with dialog items, controls, and
  10. // text fields (and the same in control panels) easier and
  11. // more convenient.
  12. //
  13.  
  14. #include "Dialog Utilities.h"
  15.  
  16. Handle GetItemHandle( DialogPtr dlg, short itemNo )
  17. {
  18.     short            itemType;
  19.     Handle            itemHandle;
  20.     Rect            itemRect;
  21.     
  22.     GetDialogItem( dlg, itemNo, &itemType, &itemHandle, &itemRect );
  23.     
  24.     return itemHandle;
  25. }
  26.  
  27. void GetItemRect( DialogPtr dlg, short itemNo, Rect *aRect )
  28. {
  29.     short            itemType;
  30.     Handle            itemHandle;
  31.     Rect            itemRect;
  32.     
  33.     GetDialogItem( dlg, itemNo, &itemType, &itemHandle, &itemRect );
  34.     
  35.     (*aRect) = itemRect;
  36. }
  37.  
  38. Boolean GetOnOff( DialogPtr dlg, short itemNo )
  39. {
  40.     return (Boolean)GetControlValue( (ControlHandle)
  41.         GetItemHandle( dlg, itemNo ) );
  42. }
  43.  
  44. void SetOnOff( DialogPtr dlg, short itemNo, Boolean turnOn )
  45. {
  46.     SetControlValue( (ControlHandle)
  47.         GetItemHandle( dlg, itemNo ), turnOn );
  48. }
  49.  
  50. void Toggle( DialogPtr dlg, short itemNo )
  51. {
  52.     SetOnOff( dlg, itemNo, !GetOnOff( dlg, itemNo ) );
  53. }
  54.  
  55. short GetRadioButton( DialogPtr dlg, short first, short last )
  56. {
  57.     short        aButton;
  58.     short        buttonOn = 0;
  59.     
  60.     // Make sure first < last.
  61.     
  62.     if ( first > last )
  63.         return 0;
  64.         
  65.     // Find which radio button is on.
  66.     
  67.     for ( aButton = first; aButton <= last; aButton++ )
  68.         if ( GetOnOff( dlg, aButton ) )
  69.         {
  70.             buttonOn = aButton;
  71.             break;
  72.         }
  73.     
  74.     return buttonOn;
  75. }
  76.  
  77. void SetRadioButton( DialogPtr dlg, short first, short last, short target )
  78. {
  79.     short        aButton;
  80.     short        buttonOn = 0;
  81.     
  82.     // Make sure first < last and target is in range.
  83.     
  84.     if ( (first > last) || (target < first) || (target > last) )
  85.         return;
  86.     
  87.     // Make sure all the radio buttons are off.
  88.     
  89.     for ( aButton = first; aButton <= last; aButton++ )
  90.     {
  91.         SetOnOff( dlg, aButton, false );
  92.     }
  93.     
  94.     // Turn the target button on.
  95.     
  96.     SetOnOff( dlg, target, true );
  97. }
  98.  
  99. void SetChars( DialogPtr dlg, short itemNo, char *str, short len )
  100. {
  101.     Str255            aString;
  102.     
  103.     BlockMove( str, &aString[1], len );
  104.     aString[0] = len;
  105.     
  106.     SetDialogItemText( GetItemHandle( dlg, itemNo), aString );
  107. }
  108.  
  109. void GetChars( DialogPtr dlg, short itemNo, char *str, short *len )
  110. {
  111.     Str255            aString;
  112.     
  113.     GetDialogItemText( GetItemHandle( dlg, itemNo ), aString );
  114.     
  115.     BlockMove( &aString[1], str, aString[0] );
  116.     (*len) = aString[0];
  117. }
  118.  
  119. void SetText( DialogPtr dlg, short itemNo, Str255 str )
  120. {
  121.     SetDialogItemText( GetItemHandle( dlg, itemNo ), str );
  122. }
  123.  
  124. void GetText( DialogPtr dlg, short itemNo, Str255 str )
  125. {
  126.     GetDialogItemText( GetItemHandle( dlg, itemNo ), str );
  127. }
  128.  
  129. void ActivateControl( DialogPtr dlg, short itemNo, Boolean activate )
  130. {
  131.     if ( activate )
  132.         HiliteControl( (ControlHandle)
  133.             GetItemHandle( dlg, itemNo ), 0 );
  134.     else
  135.         HiliteControl( (ControlHandle)
  136.             GetItemHandle( dlg, itemNo ), 255 );        // disable
  137. }
  138.  
  139. long GetDialogNumberField( DialogPtr dlg, short itemNo )
  140. {
  141.     Str255        theStr;
  142.     long        theNum;
  143.     
  144.     GetDialogItemText( GetItemHandle( dlg, itemNo ), theStr );
  145.     StringToNum( theStr, &theNum );
  146.     return theNum;
  147. }
  148.  
  149. void SetDialogNumberField( DialogPtr dlg, short itemNo, long theNumber )
  150. {
  151.     Str255        theStr;
  152.     
  153.     NumToString( theNumber, theStr );
  154.     SetDialogItemText( GetItemHandle( dlg, itemNo ), theStr );
  155. }
  156.  
  157. void SelectTextField( DialogPtr dlg, short itemNo )
  158. {
  159.     SelectDialogItemText( dlg, itemNo, 0, 255 );
  160. }
  161.