C (57/254)

From:Donald W Millican
Date:10 Apr 00 at 14:17:39
Subject:GadTools event loop problem

I have written a calculator program which has all the normal functions of a
calculator.
I am now trying to add a second window containing scientific functions to
the calculator. As I was having problems with this I have created a separate
program which simply opens a window and allows the user to open and close
the second window via a menu item - this much works.
Each window can contain one or more buttons and the main window contains a
text gadget. Each window has to be fully responsive to user actions without
compromising the other window`s responsiveness.

My question is this:
Normally my programs enter a Wait state until a message arrives at the
window`s message port. When it arrives they get the required information out
of the message, reply to it and then deal with whatever has occurred. How do
I code my Wait statement so that it waits for an event in more than one
window, identifies which window the event occurred in and then takes the
appropriate action?

I include a summarised version of my Wait loop:
(For the purposes of the test each window contains a button which when
clicked displays an EasyRequest.
The second window does not have a close gadget as it is opened and closed
via a ticked/unticked menu item in the first window).

/* Start of code fragment */

// Function containing event loop
void process_window_events(struct Screen *mysc, struct Window *mywin,
struct Gadget *my_gads[], struct Menu *mainmenustrip)
{
// Structure of second window
struct Window *sciwin;
struct IntuiMessage *imsg;
ULONG imsgClass;
UWORD imsgCode;
// Variable used to indicate program shutdown
BOOL terminated = FALSE;
struct Gadget *gad;
UWORD menu_number, which_menu, which_item;
MenuItem *item;
// Parameter variables for opening and closing second window
BOOL sci_open = FALSE;
void *vi2;
struct Gadget *glist2, *my_gads2[1];

// Begin event loop
while (!terminated) // While Close button or MenuItem 'Quit' not chosen
{
// Put program to sleep until message arives
// This command only waits for messages for my first window
Wait (1 << mywin->UserPort->mp_SigBit);

// Deal with message
while ((!terminated) &&
(imsg = GT_GetIMsg(mywin->UserPort)))
{

// Extract information
gad = (struct Gadget *)imsg->IAddress;

imsgClass = imsg->Class;
imsgCode = imsg->Code;
menu_number = imsg->Code;

// Reply to message
GT_ReplyIMsg(imsg);

// Discover type of event
switch (imsgClass)
{
// Button has been clicked
// What happens in the handleGadgetEvent function varies with
// which button has been clicked and is not important here
case IDCMP_GADGETUP:
// Which window are we dealing with?
// Are we told in the message?
handleGadgetEvent(mysc, mywin, gad, imsgCode, my_gads);
// This would be the function for the second window
// Is this the correct place to call it?
// handleGadgetEvent(mysc, sciwin, gad, imsgCode,
my_gads2);

break;
// Menu item chosen
case IDCMP_MENUPICK:
which_menu=MENUNUM(menu_number);
which_item=ITEMNUM(menu_number);

while( menu_number != MENUNULL )
{
/* Get the address of the item: */
item = (struct MenuItem *) ItemAddress( mainmenustrip,
menu_number );

/* Check which item was selected: */
if( which_menu == 0 )
{
// Second window is to be opened or closed
if( which_item == 0 )
{
// If second window is currently closed
// then open it
if(sci_open == FALSE)
{
// Open other window
Open_Sci(mysc, &sciwin, &vi2, &glist2,
my_gads2);
sci_open = TRUE;
}
// If second window is currently open
// then close it
else if(sci_open == TRUE)
{
// Close other window
Close_Sci(&sciwin, &vi2, &glist2);
sci_open = FALSE;
}
}
// Quit chosen
if( which_item == 1 )
{
terminated = TRUE;
}
}
/* Get the following item's menu number: */
menu_number = item->NextSelect;
}
break;
case IDCMP_CLOSEWINDOW:
terminated = TRUE;
break;
case IDCMP_REFRESHWINDOW:
GT_BeginRefresh(mywin);
GT_EndRefresh(mywin, TRUE);
break;
}
}
}
}
/* End of code fragment */

Can anyone help?

------------------------------------------------------------------------
PERFORM CPR ON YOUR APR!
Get a NextCard Visa, in 30 seconds! Get rates as low as
0.0% Intro or 9.9% Fixed APR and no hidden fees.
Apply NOW!
http://click.egroups.com/1/2121/1/_/451227/_/955387979/
------------------------------------------------------------------------