[comp.windows.x] XtMainLoop -vs- XtAppMainLoop

davy@RIACS.EDU (12/16/88)

Here's a weird one.  I have an application which does the following:

	toplevelwidget = XtInititalize(....)

	CreateSomeWidgets();

	XtRealize(toplevelwidget);
	XFlush(...);

	/* generally the user positions the first widget through his wm
	   now, unless he specified a location for it */

	CreateSomeMoreWidgets();
	XtMainLoop();

All widgets are children of toplevelwidget (or some child of it).  If
I seem to have to call XFlush to map the toplevelwidget widgets so
that the widgets made in CreateSomeMoreWidgets will be mapped.  If I
leave out the XFlush, then they aren't mapped.  I assume this is
because the user's window manager hasn't asked him to position the
toplevelwidget window yet, and so it isn't mapped, meaning the children
don't get mapped either.

Anyway, this all works just fine.  The problem is when I try to use
XtAppMainLoop instead of XtMainLoop, since the manual says XtMainLoop
is obsolete.  I tried two different ways of calling it:

	XtAppMainLoop(XtCreateApplicationContext())
		- The widgets created in CreateSomeWidgets come up,
		  the ones in CreateSomeMoreWidgets do not.  The
		  widgets which do come up don't seem to receive
		  any of the events they're supposed to.

	XtAppMainLoop(XtWidgetToApplicationContext(toplevelwidget))
		- The widgets created in CreateSomeWidgets come up,
		  the ones in CreateSomeMoreWidgets do not.  The
		  events seem to work okay now, though.

What am I doing wrong?  I can't find much of an explanation about what
the application context is, or what it is for.  There's a little tiny
bit about it on page 21 of the Xt manual, but that's all I can find.

--Dave

asente@decwrl.dec.com (Paul Asente) (12/17/88)

An application context holds all the state necessary to process one
"logical application".  Its principal element is a list of the displays
this application has open.  This list is used by XtAppMainLoop to see
which displays it should look at for input.

To start an application, you call XtToolkitInitialize to initialize the
toolkit, XtCreateApplicationContext to create an application context, and
XtAppCreateShell to create an application shell.

For backwards compatibility, and to make it easier to write simple
programs, there is a call XtInitialize that does all this for you and
creates a hidden, default application context.  This default application
context is used in XtMainLoop (and XtNextEvent, XtPending...).

So the long and short of it is, if you use XtInitialize, you should use
XtMainLoop.  If you create an application context explicitly using
XtCreateApplicationContext you should use XtAppMainLoop and pass your
application context in.

	-paul asente
	    asente@decwrl.dec.com	decwrl!asente

rws@EXPO.LCS.MIT.EDU (Bob Scheifler) (12/17/88)

Paul, I don't think you quite answered his apparent problem.
He is pulling the AppContext out of one of the widgets to
pass to XtAppMainLoop.  This should be equivalent to calling
XtMainLoop, I don't see where the spec implies this should fail
(but apparently it does).

net@TUB.BITNET (Oliver Laumann) (12/19/88)

> An application context holds all the state necessary to process one
> "logical application".
>
> To start an application, you call XtToolkitInitialize to initialize the
> toolkit, XtCreateApplicationContext to create an application context, and
> XtAppCreateShell to create an application shell.

However, as I have pointed out earlier, in X Version 11 Release 3 this
only works if you are not using widgets that register resource type
converters (such as, for instance, the Athena `clock' widget).
The reason for this is that the converters are always added to the
`default application context'; not to the application context that
you have created by XtCreateApplicationContext.
This seems also to be true for timeouts (see Load.c).

Until this bug is fixed, you can replace the call to
XtCreateApplicationContext by a call to _XtDefaultAppContext in your
application (provided, of course, that you do not want to use more
that one application context).

Regards,
--
Oliver Laumann              net@TUB.BITNET              net@tub.UUCP

swick@ATHENA.MIT.EDU (Ralph R. Swick) (12/19/88)

> you can replace the call to
> XtCreateApplicationContext by a call to _XtDefaultAppContext ...

Using XtWidgetToApplicationContext() on the shell returned by
XtInitialize() would be much better.

net@TUB.BITNET (Oliver Laumann) (12/20/88)

> > you can replace the call to
> > XtCreateApplicationContext by a call to _XtDefaultAppContext ...
>
> Using XtWidgetToApplicationContext() on the shell returned by
> XtInitialize() would be much better.

The original article discussed a scenario where a client calls
XtToolkitInitialize, XtCreateApplicationContext, and XtAppCreateShell
explicitly (the usual way to initialize an application if one wants to
make use of application contexts).  In this case,
XtWidgetToApplicationContext does not return the default application context.

Is using `XtInitialize' still a `sanctioned' way to initialize an application
under Release 3, or is it likely to become obsolete in future releases?

Anyway, the existence of an automatically created default application
context and the co-existence of `XtAddFoo' and `XtAppAddFoo' functions
is a mess (there is not even a corresponding XtAddFoo for each XtAppAddFoo!).
Resource converters should be made a global resource (I don't see a reason
why they are stored in application contexts).

Regards,
--
Oliver Laumann              net@TUB.BITNET              net@tub.UUCP

davy@riacs.edu (Dave Curry) (12/20/88)

In article <8812191042.AA16226@tub.UUCP> net@TUB.BITNET (Oliver Laumann) writes:
>> To start an application, you call XtToolkitInitialize to initialize the
>> toolkit, XtCreateApplicationContext to create an application context, and
>> XtAppCreateShell to create an application shell.
>
>However, as I have pointed out earlier, in X Version 11 Release 3 this
>only works if you are not using widgets that register resource type
>converters (such as, for instance, the Athena `clock' widget).

It's also true, unfortunately, for more commonly used widgets like
Text widgets, as I discovered this afternoon after converting my
program to use the above sequence.

Trying to put a scroll bar on a text widget works fine when using
XtInitialize/XtMainLoop.

When using the above sequence however, you get an error message:

X Toolkit Warning: No type converter registered for 'String' to 'EditMode'
conversion.

and you get a Text widget with no scrollbar.

Guess I'll go back to XtInitialize/XtMainLoop for awhile.

--Dave

P.S. - Note to X Consortium folks: I have a short program (< 100 lines)
       which demonstrates the above, if you want it.