Garth,
I'm having a heckuva time getting my gadgets to work properly, specifically,
to store their data properly. I have, on one form, 16 of essentially the
same gadget (same appearance and behavior, but different gadgets in the
resource files, each with a different ID, of course). I've written a
function to initialize the data for all 16 of them, and then another
function to draw all of them, and part of the draw function is supposed to
display the data from each one on each one. (Each one could be a number,
from 1-16, or a letter, from A-P.)
I guess I'm not clear on *exactly* how a gadget stores its data. I know it
really stores just a pointer, and the data is what the pointer is pointing
to. And I'm not having a problem assigning the data to each gadget as I
iterate through my "for" loop - I can see it happening as I step through the
debugger. But when I get to the drawing function, the first 5-6 draw OK, and
display the data that got set in the initializing function, and then I get a
warning from the Emulator that says I'm reading from an unlocked chunk of
memory, and the data for the rest of the gadgets is not the data that I
"loaded" in my initializing function (it all comes back as 242, which I'm
guessing is just whatever that pointer happened to be pointing at...). In
other words, the pointers that the gadgets are using to store their data
don't keep the value I give them in the initialization function, when it
comes time to read the data in the draw function. (I could put everything
into a singel function, but I'm going to need to access all these gadgets'
data later in the program, on different forms - is that even possible? Or
does the gadget "die" when the form is no longer active?)
I'm also not clear on the whole issue of how pointers and handles are used.
I understand that a pointer is not moveable, and a handle is, but I don't
know when to use one or the other. Do you use them together, or do you use
one or the other? None of the documentation I've found is very clear to me,
and gives no examples.
I don't expect you to try to educate me on this, but I wonder if you might
consider looking at my three functions that initialize and draw the gadgets?
They are below, in case you've got the time and inclination... (And thanks a
million for any help you might be able to give!)
Brian Smith
MainFormsInitItems calls InitOneItem. I did this to make sure I get a
"clean" pointer for each gadget, with MemPtrNew( ).
After MainFormInitItems is finished, MainFormsDrawItems is called.
void MainFormsInitItems(FormPtr pForm) { //Iterates thru all 16 gadgets and
call InitOneItem( ) for each one.
UInt8 nCurrentItem;
UInt16 nGadgetID = MainItem1Gadget // GadgetIDs are sequential
(1061-1076)
// so I can
iterate thru them.
UInt16 nGadgetIndex;
// Code right here to set the proper starting nCurrentItem - I've chopped
it out for clarity.
while (nGadgetID <= MainItem16Gadget) {
nGadgetIndex = FrmGetObjectIndex(pForm, nGadgetID);
InitOneItem(pForm, nCurrentItem, nGadgetIndex);
nGadgetID++;
nCurrentItem++;
}
} // MainFormsInitItems
void InitOneItem(FormPtr pForm, UInt8 nCurrentItem, UInt16 nGadgetIndex) {
UInt8 *pItem = MemPtrNew(sizeof(UInt8));
if (pItem != NULL) {
*pItem = nCurrentItem;
FrmSetGadgetData(pForm, nGadgetIndex, pItem);
}
MemPtrFree(pItem);
}
void MainFormsDrawItems(FormPtr pForm) {
RectangleType rect;
UInt16 nGadgetID = MainItem1Gadget;
UInt16 nGadgetIndex = 0;
UInt8 *pItemNum;
char szItemNum[3];
if (pForm == NULL)
return;
while (nGadgetID <= MainItem16Gadget) {
pItemNum = MemPtrNew(sizeof(UInt8));
nGadgetIndex = FrmGetObjectIndex(pForm, nGadgetID);
pItemNum = FrmGetGadgetData(pForm, nGadgetIndex);
if (pItemNum != NULL) {
FrmGetObjectBounds(pForm, nGadgetIndex, &rect);
WinEraseRectangle(&rect, 0);
WinDrawRectangleFrame(roundFrame, &rect);
StrPrintF(szItemNum, "%2d", *pItemNum); // on about the 5th or 6th
iteration through the "while" loop,
//
the Emulator says this line is reading from an unlocked chunk.
WinDrawChars(szItemNum, 2, rect.topLeft.x + 3, rect.topLeft.y + 2);
} // if
nGadgetID++;
} // while
MemPtrFree(pItemNum);
} // MainFormsDrawItems()
> -----Original Message-----
> From: Garth Watkins [mailto:[EMAIL PROTECTED]]
> Sent: Sunday, April 29, 2001 9:52 PM
> To: Palm Developer Forum
> Subject: Re: Avoiding globals
>
>
> Sure. If you look at the structure of a gadget
> (FrmGadgetType) you'll see
> that the data member is a placeholder for a void pointer. That pointer
> variable can be a pointer to memory that you allocated off
> the dynamic heap,
> or a pointer to basically anything. I use gadgets quite a lot for this
> purpose. Usually in my forms initializtion code, I grab some
> dynamic memory
> for things related to that form, and then in response to the
> frmCloseEvent,
> I release the memory.
> Use FrmSetGadgetData. and FrmGetGadgetData to set and
> retrieve the pointer.
>
> Garth
>
> ----- Original Message -----
> From: "Brian Smith" <[EMAIL PROTECTED]>
> To: "Palm Developer Forum" <[EMAIL PROTECTED]>
> Sent: Sunday, April 29, 2001 8:01 AM
> Subject: RE: Avoiding globals
>
>
> > On a related note: Can the data field of a gadget store ANY
> kind of data?
> > Like a pointer to a structure, containing several data
> elements? If so, is
> > there anything to watch out for, or is it pretty straightforward?
> >
> > Thanks,
> >
> > Brian Smith
> >
> > > The technique you propose could be made to work, but a more
> > > compatible method would be to put a gadget on each form from
> > > which you plan to access your globals, and use its data field
> > > to store a handle to your global block.
> > >
> > > Regards,
> > >
> > > Jim Schram
> > > Palm Incorporated
> > > Partner Engineering
> > >
> > >
> > > --
> > > For information on using the Palm Developer Forums, or to
> > > unsubscribe, please see
> http://www.palmos.com/dev/tech/support/forums/
> > >
> >
> > --
> > For information on using the Palm Developer Forums, or to
> unsubscribe,
> please see http://www.palmos.com/dev/tech/support/forums/
> >
>
>
> --
> For information on using the Palm Developer Forums, or to
> unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
>
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/