The one thing you want to remember about all controls...buttons, selector
triggers and pop triggers, is that the text for the labels is initially
stored in the form, but you can set it to your own memory space if you want.

Here is how it works....

When the form is initially loaded, the resource manager grabs the form
resource and parses it. It adds memory space to the end of the form for
each form object it encounters. When it sees a control object, it creates a
ControlType structure. It then looks at the label specified in the control
resource, and puts the text of the label just after the control structure.
It then goes back to the control structure and puts a pointer to the text
into the label pointer in the control structure.

The implications of this are that if you are going to set a label, you have
one of two ways, but you have to be careful. If you copy text straight to
the area pointed to by the label pointer, you need to make sure that your
resource specified a label big enough to hold the maximum number of
characters you are going to copy. Otherwise, it will write off the end of
the text area and corrupt the memory for the next object in the form. This
is a common gremlins bug that is hard to figure out what happened. It can
especially happen if you are translating the resources in your software.

The other way to set the text is to allocate the memory at run time and
call CtlSetLabel. But, you have to make sure that you deallocate the memory
when the form is closed.

Below is some code to use to figure out how much text is allocated in a
control. Use this code in debug code to assert that there is enough memory
allocated for a control label before you copy text to it. DO NOT use it in
release code, because Palm could always change the way a form is created,
which will break this code.

UInt ControlAllocation(FormPtr pForm, UInt controlID)
{
        UInt objIndex = FrmGetObjectIndex (pForm, controlID);
        UInt formSize = MemPtrSize (pForm);
        ControlPtr pControl = (ControlPtr) FrmGetObjectPtr (pForm, objIndex);
        CharPtr label = CtlGetLabel (pControl);

        if (label == NULL) {
                return 0;
        }

        if (label == (CharPtr)pControl + sizeof (ControlType)) {
                // we are pointing inside the form at text allocated by the
form
                if (objIndex == FrmGetNumberOfObjects (pForm) - 1) {
                        // last item in the list, compute from end of form
chunk
                        return (CharPtr)pForm + formSize - label;
                } else {
                        // compute from start of next object
                        return ((CharPtr) FrmGetObjectPtr (pForm, objIndex
+ 1)) - label;
                }
        } else {
                // assume it was allocated elsewhere
                return MemPtrSize (label);
        }
} /* ControlAllocation */


Shannon

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hands High Software
Award winning software for the Palm(tm) Computing platform

<http://www.handshigh.com/>

[EMAIL PROTECTED]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to