C (169/207)

From:Bob Lanham
Date:24 Dec 99 at 17:49:08
Subject:event handling problem

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)))
{
mclass = message->Class;
code = message->Code;
ReplyMsg((struct Message *)message);

switch (mclass)
{
case IDCMP_CLOSEWINDOW:
ClearMenuStrip(win);
CloseWindow(win);
win2open=FALSE;
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);
}
/****************************************************************/