[comp.sys.amiga.tech] Problem with Menu Events

e4ajwake@augean.ua.OZ.AU (Wakefield) (08/30/90)

Dear Gurus, (or similar)

I have a problem with intuition returning the address of a menu item when
the item causes an event.  The code that I am using is shown below, cut down
of course.

The file "menu.h" contains all the menu and screen etc structures required to
run the program.  I have previously been using the class & code fields to
extract the menu item, and used switch statements to recover the menu item
and call the apporpriate function (replaced here by printf's).

However it seems that it should be possible to use the code outlined below to
do this more efficently and simply.

The problem:

The address field of the message structure is alays ZERO!!!

Questions:

Is what I am trying to do possible ?
Has anyone out there done this before ?
Does anyone have some sample code ?
Is there a simple solution ?

Now the code...


#include "menu.h"

void HandleEvent();

main()

{
   APTR address;
   struct IntuiMessage *message;

   for(;;) {
      Wait(1L << MyWindow1->UserPort->mp_SigBit);

      while(message = (struct IntuiMessage *)
                         GetMsg(MyWindow1->UserPort))
         {
            address = message->IAddress;
            ReplyMsg(message);
            HandleEvent(address);
         }
   }

void HandleEvent(object)
APTR object;
{
  if (object == (APTR)&MenuItem8) { printf("about function\n"); return; }
  if (object == (APTR)&MenuItem9) { printf("quit function\n"); return; }
}

Notes:
------
MenuItem8 is the about item.
MenuItem9 is the quit item.

these both have values which are non zero and seem reasonable, so I think
that they are OK.


Mail any hints/abuse/suggestions/code/etc... to

e4ajwake@augean.ua.oz.au

Thanks in advance

Alex Wakefield.

( Electronic & Electrical Engineering Dept, )
( University of Adelaide, South Australia.  )

mitchell@cbmvax.commodore.com (Fred Mitchell - Product Assurance) (08/30/90)

In article <906@augean.ua.OZ.AU> e4ajwake@augean.ua.OZ.AU (Wakefield) writes:
>Dear Gurus, (or similar)
>
>I have a problem with intuition returning the address of a menu item when
>the item causes an event.  The code that I am using is shown below, cut down
>of course.
>
>The file "menu.h" contains all the menu and screen etc structures required to
>run the program.  I have previously been using the class & code fields to
>extract the menu item, and used switch statements to recover the menu item
>and call the apporpriate function (replaced here by printf's).
>
>However it seems that it should be possible to use the code outlined below to
>do this more efficently and simply.
>
>The problem:
>
>The address field of the message structure is alays ZERO!!!
>
>Questions:
>
>Is what I am trying to do possible ?
>Has anyone out there done this before ?
>Does anyone have some sample code ?
>Is there a simple solution ?
>
>Now the code...
>
>
>#include "menu.h"
>
>void HandleEvent();
>
>main()
>
>{
>   APTR address;
>   struct IntuiMessage *message;
>
>   for(;;) {
>      Wait(1L << MyWindow1->UserPort->mp_SigBit);
>
>      while(message = (struct IntuiMessage *)
>                         GetMsg(MyWindow1->UserPort))
>         {
>            address = message->IAddress;
>            ReplyMsg(message);
>            HandleEvent(address);
>         }
>   }

The way to get the address of the MenuItem structure is to
take the Menu number returned in the Code field and call
ItemAddress() as follows:

	struct MenuItem *mi;
		...

	mi = ItemAddress(yourMenuStrip, message->Code);
		...

Also, you'll want to check the NextSelect field in your MenuItem to see
if there are any other selections:

	while (mi->NextSelect != MENUNULL) {
		mi = ItemAddress(yourMenuStrip, mi->NextSelect)
		...process it...
		}

And that should do the trick for you.

-Mitchell

Yes, I remember my first taste of Intuition...

ken@cbmvax.commodore.com (Ken Farinsky - CATS) (08/31/90)

In article <906@augean.ua.OZ.AU> e4ajwake@augean.ua.OZ.AU (Wakefield) writes:
>Dear Gurus, (or similar)
>
>The problem:
>
>The address field of the message structure is alays ZERO!!!
>
> while(message = (struct IntuiMessage *)GetMsg(MyWindow1->UserPort))
>         {
>            address = message->IAddress;

No.  Use ItemAddress() to get the address of the MenuItem.

-- 
--
Ken Farinsky - CATS - (215) 431-9421 - Commodore Business Machines
uucp: ...{uunet,rutgers}!cbmvax!ken
bix:  kfarinsky

rosenber@ra.abo.fi (Robin Rosenberg INF) (08/31/90)

Hey! Stop there. You must handle extended selection too. For that you
must still use ItemAddress() to follow all NextSelect fields. No short
cuts please...

-------------
Robin Rosenberg