I will be the first to admit that I am a bit confused about how to use
gadgets. Let me go over what I am trying to do, and what I know so far,
and then over what I am completely clueless about.
What I want to do is to have something like an image that, once clicked,
will give some visual/audible feedback and then draw some text out (either
on its top (picture a little rectangle on the top of the image with the
text) or below it). So, far, I have been testing my code using a normal
button. Now I want to replace the button with the image. From what I read
here, in the Palm developer's site, and in the *evil* O'Reilly book,
gadgets are the best way to do it.
If we assume 3.1 (listing 5.5, p.124 of the Companion does confuse me as I
do not see how is the routine called), I would have my gadget first be
called in response to a penDownEvent:
static Boolean MyFormHandleEvent(EventPtr event)
{
Boolean handled = false; // Has the even been taken care of?
#ifdef __GNUC__
CALLBACK_PROLOGUE
#endif
switch(event->eType)
{
[...]
case penDownEvent:
handled = HandlePenDown(myGadget, event);
break;
}
[...]
here, it would see if the pendown event was inside the rectangle for
myGadget (or another gadget of interest). If so, it would do something
useful with it.
static Boolean HandlePenDown(UInt16 gadgetID, EventPtr event)
{
FormPtr pFrm = FrmGetActiveForm();
UInt16 gadgetIndex = FrmGetObjectIndex(pFrm, gadgetID);
RectangleType bounds;
FrmGetObjectBounds(pFrm, gadgetIndex, &bounds);
if(RctPtInRectangle(event->screenX, event->screenY, &bounds))
{
GadgetTrap(pFrm, gadgetID, event);
return true;
}
}
I was going to have my routine to draw the gadget directly here, but wanted
first to understand GadgetTrap(), which is defined in the Evil O'Reilly
book, p.112. One line in that code I do not understand (it may be that I
do not know C enough =) is the one that goes like this
*wPtr = !(*wPtr)
Yes, it seems that there is a ";" missing, but there is way more to it. I
asked some people and was told that could be a boolean flip. So, we would
have something like *wPtr = !(true) => *wPtr = false, but it does not make
sense to me. After all, isn't wPtr a normal memory pointer?
Also in the same routine, he uses WinInverRectangle() to emulate a
highlight. How could I do something similar to it, but only invert the
bitmap inside it (assume anything outside the bitmap is background and
should be left alone)? Should I just give up and create an "inverted"
bitmap and swap them? Continuing with my easy questions, From what I
understood, I should make the gadget be redrawn after the form is drawn in
a frmOpenEvent and FrmUpdateEvent events. The routine to draw the bitmap
is currently something like
static Boolean DrawGadget(UInt16 gadgetID)
{
switch(gadgetID)
{
case CookieGadget:
{
MemHandle hCookieImage = DmGetResource(bitmapRsc,
bitmapCookieBtn);
BitmapType *bitmap = MemHandleLock(hCookieImage);
WinDrawBitmap(bitmap, 48, 80);
// WinDrawBitmap(bitmap, x, y);
DmReleaseResource(hCookieImage);
}
break;
}
}
I hard coded the coordinate for now. Later on I can get it from the bounds
for the gadget.
Back to the events, I am confused about where the form gets a
frmUpdateEvent. The way the code is shown in the book (since the original
hello sample), ApplicationHandleEvent() is written to initialize the form
and then feeds the events to the form handle event routine by setting the
event handler using something like FrmSetEventHandler(pFrm,
HandleFormEvents). Wouldn't then HandleFormEvents() be the most
appropriate place to listen for frmUpdateEvent? That is where I placed it
but it is not being updated. I may try to make it beep so I have some
feedback that the event is being detected (since dbx in unix, I have always
avoided using debuggers as most of the time they just get me confused.
When doing stuff in Xwindows, I used to write my own debugging messages in
a file that I would tail -rf then =)?
The big question of the day: am I in the right target here? Is this too
slow? If I were to rewrite it in a 3.5 style, how much speed/code size
would I gain/loose? Also, if I wanted to used some of that code to track
down a moving image (think of a game), what to do then?
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/