[comp.windows.x.motif] Blocking actions in Motif and X

lampshir@airgun.wg.waii.com (gregory b. lampshir) (07/10/90)

Greeting peoples....

I am hoping that all you Motif  experts out there can help me on a
small problem I am having.  What I wish to do is create an application
modal, blocking yes/no widget.  Let me explain.

I am writing a program that must ask alot of yes/no questions.  These
questions are critical and hence, the program cannot continue without
them.  What I would like to do is use the following code segment

Boolean answer;
...
answer = DoNotReturnWithoutAnswer("Do you want to continue?");
...

to return an answer.  However, in the event driven environment, even
with the widget's dialog type set to application modal, the func
'DoNotReturnWithoutAnswer' returns before the user can respond.  The
yes/no widget, however, is left displayed waiting for the user in an
event driven manner.

What I really need is to block until the user responds, could someone
give me some skeleton code to show me how to do this?  The sequence of
events should be thus....
  
1) call the yes/no func
2) popup a small window with the question string as the display string
3) create a yes and a no push button
4) block and grab the pointer (confined to the yes/no window box)
until the user responds
5) return either True or False

I have made some progress.  Using Intrinsics and the XtPopup() group
of funcs I can make the widget popup (without any fancy borders which
is what I wanted anyway) and display the two push buttons with label
string.  No problem.  I even grabbed the pointer.  However, the event
loop necessary for the blocking action is still alittle beyond me.
How about some help?




gregory lampshire     | Searching for a computer which can do it
western geophysical   | all......

brian@padouk.ima.isc.com (Brian R. Holt) (07/11/90)

In article <858@airgun.wg.waii.com>, lampshir@airgun.wg.waii.com
(gregory b. lampshir) writes:
|> 
|> 
|> Greeting peoples....
|> 
|> I am hoping that all you Motif  experts out there can help me on a
|> small problem I am having.  What I wish to do is create an application
|> modal, blocking yes/no widget.  Let me explain.

I just use the Motif Utilities package from Chris VanHaren. I don't know
whether it is still available, but try vanharen@mit.edu. It includes
MuGetBoolean() for asking yes/no questions, and a slew of other useful
stuff.

		=brian

Email	brian@ima.isc.com	
Phone	617-661-7474 x206	
Fax	617-661-2070
near the last bend in the Charles River

argv@turnpike.Eng.Sun.COM (Dan Heller) (07/12/90)

In article <858@airgun.wg.waii.com> lampshir@airgun.wg.waii.com (gregory b. lampshir) writes:
>   What I would like to do is use the following code segment
> 
> Boolean answer;
> ...
> answer = DoNotReturnWithoutAnswer("Do you want to continue?");
> ...

> What I really need is to block until the user responds, could someone
> give me some skeleton code to show me how to do this?  The sequence of
> events should be thus....
>   
> 1) call the yes/no func
> 2) popup a small window with the question string as the display string
> 3) create a yes and a no push button
> 4) block and grab the pointer (confined to the yes/no window box)
> until the user responds
> 5) return either True or False

If you look at the WidgetWrap code the I provided for X11R3 (the
predicessor to the varargs style interface now used in X11R4),
there is a demo program that does precisely this.  It's not geared
towards motif -- it uses athena widgets by default.  However, it
is very simple to generalize this for Motif or any arbitrary widget
set.  I get requests for this all the time, so rather than ask me
for it, please get it out of the old R3 distribution (in contrib/widgets)
or query your local s-xources archive site (or uunet).  It's volume 3
issue 76.  It's also in the WidgetWrap directory in "contrib" on expo.

--
dan
----------------------------------------------------
O'Reilly && Associates   argv@sun.com / argv@ora.com
Opinions expressed reflect those of the author only.

vanharen@fries.mit.edu (Chris VanHaren) (07/12/90)

>> I just use the Motif Utilities package from Chris VanHaren. I don't know
>> whether it is still available, but try vanharen@mit.edu. It includes
>> MuGetBoolean() for asking yes/no questions, and a slew of other useful
>> stuff.

Someone here pointed out this message to me (since I don't normally read
this newsgroup).  I have already gotten 4 requests to have this code
sent out, which is flattering, and I thank brian for the nice
testimonial.

Anyway, seeing as how there are (apparently) many people interested in
this little library, I have decided to make it available for anonymous
ftp from somewhere over here (MIT).  Unfortunately, I haven't yet done
this -- I'm still making arrangements to have it put in the right place.

So, rather than everyone sending me mail asking for it, give me a day or
so to set it up and I'll post another note when it's ready and give
directions on how to snarf it and all that.

For those of you poor souls without internet access, go ahead and send
me mail and I'll try to mail the stuff back to you as quickly as I can,
although I'm a bit behind on mail right now (aren't we all :-).

For those of you who already have snapshots, you might want to grab a
new copy once I've made it available again, as this code is a bit
cleaner than the old stuff, and a few new things have been added.

Well, this is long enough for now.  More info when I have the thing
ready for anonymous ftp.
					Thanks for your patience.
						-Chris.

weikart@prl.dec.com (Chris Weikart) (07/12/90)

In article <858@airgun.wg.waii.com> lampshir@airgun.wg.waii.com (gregory
b. lampshir) writes:

> ... What I would like to do is use the following code segment
>
> Boolean answer;
> ...
> answer = DoNotReturnWithoutAnswer("Do you want to continue?");
> ...
>
> to return an answer.  However, in the event driven environment, even
> with the widget's dialog type set to application modal, the func
> 'DoNotReturnWithoutAnswer' returns before the user can respond.  The
> yes/no widget, however, is left displayed waiting for the user in an
> event driven manner.

There've already been some suggestions posted about libraries you can pick up,
but having run into the same problem myself I thought I'd post my solution.

First of all, use an XmMessageDialog, which has Ok, Cancel and Help callbacks.
Create the dialog, and change Ok/Cancel to Yes/No, and unmanage Help,
if that's what you want.

Call DialogLoop(your_dialog_widget) to launch the thing modally. Your yes and
no callbacks should each set a global (or some user_data) to indicate the
answer, then call DialogStop(your_dialog_widget) to allow DialogLoop to exit.

So your DoNotReturnWithoutAnswer can look like this:

Boolean DoNotReturnWithoutAnswer (message)
char *message;
{
    /* create dialog_widget, customize, etc. if not already */

    /* install message as text */

    DialogLoop(dialog_widget); /* won't return until DialogStop called */

    return (value_set_from_callback);
}

Here's the source for DialogLoop and DialogStop:

/*
 * CMW (Thu Jun 14 17:24:21 1990)...
 * To be really general purpose and safe, DialogLoop and DialogStop
 * should accept a pointer to a context that holds the (now global)
 * DialogLoopControl boolean, as well as an indication that allows
 * the dialog to be either modal or modeless...
 */

Static Bool DialogLoopControl;		/* global for now... */

Void DialogLoop (Dialog)
Widget Dialog;
{
    XtManageChild(Dialog);
    XtAddGrab(Dialog,TRUE,FALSE); /* hard-wired for now... */

    for (DialogLoopControl = True;  DialogLoopControl;  )
    {
	XEvent event;
	XtNextEvent(&event);
	XtDispatchEvent(&event);
    }
}

Void DialogStop (Dialog)
Widget Dialog;
{
    DialogLoopControl = False;
    XtRemoveGrab(Dialog);	/* hard-wired for now... */
    XtUnmanageChild(Dialog);
}

Have fun,
Chris