Gadgets (at least pre-OS 3.5) are pretty simple beasts. You basically get a
user interface element with known bounds. I cover using gadgets in my book
(link below) -- the key points (and code snippets from the book) are:
* draw the gadget when the form opens, after you draw the form:
case frmOpenEvent:
frm = FrmGetActiveForm();
FrmDrawForm(frm);
objIndex = FrmGetObjectIndex(frm, Main_FakeRB_Gadget);
FrmGetObjectPosition(frm, objIndex, &x, &y);
// Draw Gadget
bitmapRez = DmGetResource ('Tbmp', RBUnSet_Bitmap);
theBitmap = (BitmapPtr) MemHandleLock(bitmapRez);
WinDrawBitmap(theBitmap, x, y);
WinDrawChars("Tap me!", 7, x+theBitmap->width+4, y+2);
DmReleaseResource(bitmapRez);
// Set Gadget State
// set value to 0
buttonSet = 0;
FrmSetGadgetData (frm, objIndex, (VoidPtr)0);
handled = true;
break;
* handle the pen down event, checking to see if the pen goes up within the
bounds of your gadget
do whatever action you want if the pen up event is inside the bounds...
case penDownEvent:
frm = FrmGetActiveForm();
objIndex = FrmGetObjectIndex(frm, Main_FakeRB_Gadget);
FrmGetObjectBounds (frm, objIndex, &gadgetRect);
if (RctPtInRectangle(event->screenX,
event->screenY,&gadgetRect)) {
Boolean penDown;
BitmapPtr lastDraw, thisDraw, drawInBounds,
drawOutBounds;
buttonSet = (DWord)FrmGetGadgetData (frm, objIndex);
bitmapRez = DmGetResource ('Tbmp', RBUnSet_Bitmap);
theBitmap = (BitmapPtr) MemHandleLock(bitmapRez);
checkedRez = DmGetResource ('Tbmp', RBSet_Bitmap);
checkedBitmap = (BitmapPtr)
MemHandleLock(checkedRez);
if (buttonSet == 0) {
drawInBounds = checkedBitmap;
drawOutBounds = theBitmap;
}
else {
drawInBounds = theBitmap;
drawOutBounds = checkedBitmap;
}
lastDraw = drawOutBounds;
// Track pen until it's lifted
do {
EvtGetPen (&x, &y,&penDown);
if (RctPtInRectangle(x, y, &gadgetRect))
thisDraw = drawInBounds;
else
thisDraw = drawOutBounds;
if (lastDraw != thisDraw) {
WinDrawBitmap(thisDraw,
gadgetRect.topLeft.x, gadgetRect.topLeft.y);
lastDraw = thisDraw;
}
SysTaskDelay (SysTicksPerSecond()/10); //
check every 1/10th second
} while (penDown);
// Toggle gadget, if pen was down in bounds
if (lastDraw == drawInBounds) {
buttonSet = (buttonSet == 0) ? 1 : 0;
FrmSetGadgetData (frm, objIndex,
(VoidPtr)buttonSet);
}
DmReleaseResource(bitmapRez);
DmReleaseResource(checkedRez);
handled = true;
}
break;
==-
John Schettino author of
Palm OS Programming For Dummies, http://schettino.tripod.com
-----Original Message-----
Subject: Gadgets
From: Pete Angiuoli
I'm trying to figure out if there is something I'm missing about Gadgets.
I'm looking to build a re-usable component and was looking into whether or
not the gadget could help me. Besides being able to set/get user data and
having the ability to position it through constructor, it doesn't seem to
offer much. Is there a way to create an event handler for the gadget so all
events happening within the gadget can be handled directly? I checked the
palm docs throughout and haven't found anything useful. Any help would be
appreciated.