C (172/207)

From:Steffen
Date:25 Dec 99 at 12:50:39
Subject:Re: event handling problem

From: Steffen <steffen.mars@stenloese.mail.telia.com>

On 24-Dec-99, Bob Lanham wrote:

> From: Bob Lanham <bobl@jaxproductions.com>
>
> I am trying to use a menu item to open another window, and the new
> window needs to start handling IDCMP messages. However the close gadget
> on the new window causes the program to freeze. Can anyone help me fix
> this code? Here's the event portion of it. Thanks!
>
> /*****************************************************************/
> win2open=false; // global flag
>
> void process_events(void) /* Find out which window had event. */
> {
> ULONG signals;
> BOOL done=FALSE;
>
> while(!done)
> {
> if (win2open==TRUE) // both windows are open
> {
> signals = Wait( (1L << window1->UserPort->mp_SigBit) |
> (1L << window2->UserPort->mp_SigBit));
> if (signals & (1L << window1->UserPort->mp_SigBit))
> done = handleIDCMP(window1);
> if (signals & (1L << window2->UserPort->mp_SigBit))
> done = handleIDCMP(window2);
> }
>
> else if (win2open==FALSE) // just window1 open
> {
> signals = Wait( (1L << window1->UserPort->mp_SigBit));
> if (signals & (1L << window1->UserPort->mp_SigBit))
> done = handleIDCMP(window1);
> }
> }
> }
>
>
> BOOL handleIDCMP(struct Window *win)
> {
> struct IntuiMessage *message;
> ULONG mclass;
> USHORT code;
> UWORD menuNumber;
> UWORD menuNum;
> UWORD itemNum;
> UWORD subNum;
> struct MenuItem *item;
> BOOL done=FALSE;
>
> while (NULL != (message = (struct IntuiMessage *)GetMsg(win->UserPort)))

Howabout :
while (!(message = (struct IntuiMessage *)GetMsg(win->UserPort)))

> {
> mclass = message->Class;
> code = message->Code;
> ReplyMsg((struct Message *)message);
>
> switch (mclass)
> {
> case IDCMP_CLOSEWINDOW:
> ClearMenuStrip(win);
> CloseWindow(win);
> win2open=FALSE;
> break;

After this break, you restart the cycle, getting a message from the window you just closed (this is not advisable ;-)
Perhaps inserting a:

while (message=(struct IntuiMessage *)GetMsg(win->UserPort)) ReplyMsg((struct Message *)message);

Before CloseWindow() (to answer all messages), then return (instead of break;)

>
> case IDCMP_MENUPICK:
> menuNumber=code;
> while ((menuNumber != MENUNULL)&&(!done))
> {
> item = ItemAddress(strip, menuNumber);
> menuNum = MENUNUM(menuNumber);
> itemNum = ITEMNUM(menuNumber);
> subNum = SUBNUM(menuNumber);
>
> if ((menuNum==0) && (itemNum==0)) /* open window2 */
> {
> if (win2open==TRUE) /* first close window2 if open
> */
> {
> ClearMenuStrip(window2);
> CloseWindow(window2);
> }
> openwin();
> win2open=TRUE;
> }
>
> else if ((menuNum==0) && (itemNum==1)) /* quit */
> done = TRUE;
>
> menuNumber = item->NextSelect;
> }
> break;
> }
> }
> return(done);
> }
> /****************************************************************/
>
> > -> Spread the URL: http://www.onelist.com/subscribe/amiga-c <-
>
Regards