[comp.windows.open-look] Mixing X11/NeWS

steve@hanauma.Stanford.EDU (Steve Cole) (02/13/91)

Several people asked me to summarize the responses I got
to my query on how to display NeWS graphics in an X11 program.
Here is a summary of what I've learned from replies and from my
own experience. Please let me know if there are errors or omissions,
or if anything is not clear. 
-Steve
 
-----------------------
The most difficult task in displaying NeWS graphics in an X11 program
is telling the NeWS server which X11 window to display in. There 
are two ways (at least) to accomplish this.
 
The first way (which I've used) involves the xvps (XView/PostScript) library.
This library is included with OpenWindows 2 and there is a document
describing it in $OPENWINHOME/share/xvps.ps.
Given the XID of your X11 window, you first call:
ps_token_from_xid(xid,newstoken)
this returns newstoken, a NeWS-compatible pointer to the X11 window. Then call:
ps_setcanvas(newstoken)
which tells the server to display its graphics in the window pointed
to by newstoken. These calls are both described in the document mentioned 
above.

The second way is to write your own CPS (C to PostScript) macro to
make this connection. I was sent some CPS code to do this by
R. Martin Chavez (chavez@entelechy.quorum.com) but it is proprietary
code that you would need permission to use, so I will not include
it here. You can write to him directly if you are interested.
Personally I found the xvps route to be painless, and I don't
think the extra overhead in including -lxvps is very large.
 
Once you've made this connection of the NeWS server to an X11 window,
it is a pretty simple matter to plot NeWS graphics. See the NeWS
Programmer's Guide for more details. First you must open a connection to
the server by calling ps_open_PostScript(), part of the CPS (C to
PostScript) package. Once the connection is open, you have three ways 
to plot:

1) Use the psio package to send PostScript code directly to
   the server (see man psio). 
2) Make use of pre-defined CPS macros such as ps_moveto and ps_lineto.
   There are a number of predefined macros covering most of the popular
   PostScript operators.
3) Write your own CPS macros. There are some examples in the NeWS
   programmers guide, and in the OpenWindows directories.
 
Then you close the connection with ps_close_PostScript(). 

I've found two more calls to be needed to ensure that NeWS and X11
do not interfere with one another. First, I call XSync before
opening the NeWS connection. This flushes the X buffer and waits for all 
oustanding X events to be processed. Then I open the NeWS connection,
plot, and before closing the connection, I call ps_flush_PostScript
to flush the NeWS buffer. With these two calls I've found that X11
and NeWS co-exist happily.
-----------------------------------------------------------------
Steve Cole  (steve@hanauma.stanford.edu, apple!hanauma!steve)
Department of Geophysics, Stanford University, Stanford, CA 94305