Src/DialogUtilities.c

#include "DialogUtilities.h"
#include <Packages.h>
 
/*
 * User clicked on a checkbox.
 */
void
SelectCheckBox(
        DialogPtr           theDialog,
        short               itemNumber,
        Boolean             *value
    )
{
        short               temp;
        
        temp = *value;
        SelectRadioButton(theDialog, itemNumber, itemNumber, itemNumber, &temp);
        *value = temp;
}
 
/*
 * Initalize a check box.
 */
void
SetCheckBox(
        DialogPtr           theDialog,
        short               itemNumber,
        Boolean             value
    )
{
        SetRadioButtons(theDialog, itemNumber, itemNumber, value);
}
 
/*
 * User clicked on a radio button, change the group.
 */
void
SelectRadioButton(
        DialogPtr           theDialog,
        short               first,
        short               last,
        short               item,
        short               *value      /* Has/gets zero to last-1  */
    )
{
        if (item < first || item > last)
            ;
        else if (last > first) {        /* Radio button             */
            SetDialogButton(theDialog, *value + first, kActiveButton);
            SetDialogButton(theDialog, item, kCheckedButton);
            *value = item - first;
        }
        else {                          /* Check box                */
            *value = !(*value);
            SetDialogButton(
                theDialog,
                first,
                (*value != 0) ? kCheckedButton : kActiveButton
            );
        }
}
 
/*
 * Clear out a radio button group, then check the desired one.
 */
void
SetRadioButtons(
        DialogPtr           theDialog,
        short               first,
        short               last,
        short               value
    )
{
        short               i;
        
        if (first == last) {            /* Check box                */
            SetDialogButton(
                theDialog,
                first,
                (value != 0) ? kCheckedButton : kActiveButton
            );
        }
        else {
            for (i = first; i <= last; i++) {
                SetDialogButton(theDialog, i, kActiveButton);
                SetDialogButton(theDialog, first + value, kCheckedButton);
            }
        }
}
 
void
SetDialogButton(
        DialogPtr           theDialog,
        short               item,
        short               value
    )
{
        short               type;
        Handle              handle;
        Rect                box;
        
        GetDItem(theDialog, item, &type, &handle, &box);
        switch (type & ~itemDisable) {
        case (ctrlItem + btnCtrl):
            if (value == kActiveButton)
                HiliteControl((ControlHandle) handle, 0);
            else if (value == kDisabledButton) {
                HiliteControl((ControlHandle) handle, 255);
            }
            break;
        case (ctrlItem + chkCtrl):
        case (ctrlItem + radCtrl):
            if (value == kDisabledButton)
                HiliteControl((ControlHandle) handle, 255);
            else {
                HiliteControl((ControlHandle) handle, 0);
                SetCtlValue((ControlHandle) handle, value);
            }
            break;
        case (userItem):
            if (((WindowPeek) theDialog)->visible
             && (value == kActiveButton || value == kCheckedButton))
                InvertRect(&box);
            break;
        }   
}
 
/*
 * Return the theDialog button's value (kActiveButton, or kCheckedButton).
 */
short
GetDialogButton(
        DialogPtr           theDialog,
        short               item
    )
{
        short               type;
        ControlHandle       handle;
        Rect                box;
        
        GetDItem(theDialog, item, &type, (Handle *) &handle, &box);
        return (GetCtlValue(handle));
}
 
void
EnableDialogItem(
        DialogPtr           theDialog,
        short               item,
        Boolean             enableItem
    )
{
        short               type;
        ControlHandle       handle;
        Rect                box;
        Boolean             isEnabled;
        
        GetDItem(theDialog, item, &type, (Handle *) &handle, &box);
        isEnabled = (type & itemDisable) == 0;
        if (isEnabled != enableItem) {
            if (enableItem)
                type &= ~itemDisable;
            else {
                type |= itemDisable;
            }
            SetDItem(theDialog, item, type, (Handle) handle, &box);
            switch (type & ~itemDisable) {
            case (ctrlItem + btnCtrl):
            case (ctrlItem + chkCtrl):
            case (ctrlItem + radCtrl):
            case (ctrlItem + resCtrl):
                if (enableItem)
                    HiliteControl(handle, 0);
                else {
                    HiliteControl(handle, 255);
                }
                break;
            }
        }   
 
}
 
signed long
GetDialogValue(
        DialogPtr           theDialog,
        short               item
    )
{
        short               type;
        Handle              handle;
        Rect                box;
        signed long         result;
        Str255              work;
 
        GetDItem(theDialog, item, &type, &handle, &box);    
        GetIText(handle, work);
        StringToNum(work, &result);
        return (result);
}
 
void
SetDialogValue(
        DialogPtr           theDialog,
        short               item,
        signed long         value
    )
{
        Str255              work;
 
        NumToString(value, work);
        SetDialogText(theDialog, item, work);
}
 
void
SetDialogText(
        DialogPtr           theDialog,
        short               item,
        ConstStr255Param    theText
    )
{
        short               type;
        Handle              handle;
        Rect                box;
 
        GetDItem(theDialog, item, &type, &handle, &box);
        SetIText(handle, theText);
}