[comp.sys.next] C funtions for opening a window

uad1121@dircon.co.uk (Jonathan Tilley) (04/23/91)

I am trying to port a graph drawing program written in C,
using X windows to the NeXT.
I have changed all the drawing commands to the appropriate
postscript PS type calls but I can not work out how to open 
a window and make it active using only c funtions.
For the moment I just want to port the program without using
the interface builder or objective C. Has anyone any thoughts?

Jonathan Tilley

patrice@cs.mcgill.ca (Patrice BELLEVILLE) (04/23/91)

Jonathan Tilley writes :
>
>I am trying to port a graph drawing program written in C,
>using X windows to the NeXT.
>I have changed all the drawing commands to the appropriate
>postscript PS type calls but I can not work out how to open 
>a window and make it active using only c funtions.
>For the moment I just want to port the program without using
>the interface builder or objective C. Has anyone any thoughts?
>
>Jonathan Tilley
>

	Well, the good news is that it can be done. The bad one is
that you have to use a few internal functions, and that as far as
I know this is not documented properly anywhere. I spent a weekend
figuring this out, so I decided to post this instead of mailing it
since perhaps others are trying to do the same thing.

	The basic idea is to (1) create a new 'context' (2) set it
up as the current one (not forgetting to save the previous one).
(3) Do your stuff (care is required with windows, see the example
below) (4) Call DPSFlushContext to have what you did appear on the
screen (5) Clean up all of the above before you return.

	Here is an example which draws a big black window over
everything else and exits right afterwards (deleting the window
beforehand, of course). This was part of an attempt to modify a
screensaver to not only dim the screen but also set it to black.
(By the way, does anyone know how to convince the window server to
execute some code automatically when it starts ?)

	I hope that this will help.

--- cut here ---
#import <dpsclient/wraps.h>
#import <dpsclient/dpsNeXT.h>
#import <dpsclient/dpswraps.h>
#import <dpsclient/dpsclient.h>

#define	WIDTH 	1120.0
#define HEIGHT	832.0

int	   my_window,obj_window;
DPSContext _dpsCurCtxt,oldCtxt;

/*
 * Main program.
 */

void main( int argc, char **argv)
{

  /* Bunch of irrelevant stuff deleted. */
  
  /* Now set up a new context and save the old one. */
   
   _dpsCurCtxt = DPSCreateContext(NULL,NULL,NULL,NULL);
   oldCtxt = DPSGetCurrentContext();
   DPSSetContext(_dpsCurCtxt);
			
  /* Create a new window. Note that some operations refer     */
  /* to the windown number returned by PSwindow, while others */
  /* use an 'object' referring to this window. I have no idea */
  /* why, ask someone from NeXT about this.		      */
  
  PSwindow(0.0,0.0,WIDTH,HEIGHT,NX_NONRETAINED,&my_window);
  
  /* Push window number on stack and get object back. */
  
  PSsendint(my_window);
  obj_window = DPSDefineUserObject(0);
  
  /* Now perform a few PostScript operations. */
  
  PSwindowdeviceround(obj_window);
  PSsetwindowlevel(99,obj_window);
  PSorderwindow(NX_ABOVE,0,obj_window);
  PSsetgray(0.0);
  PSrectfill(0.0,0.0,WIDTH,HEIGHT);
  
  /* Nothing will appear on screen unless you flush the context. */
  /* This will be done for you when the context is destroyed, but*/
  /* you won't have a change of seeing much as it goes by with a */
  /* flash just before your window is deleted.			 */
  
  DPSFlushContext(_dpsCurCtxt);

  /* More code which is not relevant. */
  
  /* Now terminate : first delete the window. */
  
  PStermwindow(obj_window);
  DPSUndefineUserObject(obj_window);
  
  /* Finally restore the previous context and delete ours. */

  DPSFlushContext(_dpsCurCtxt);
  DPSSetContext(oldCtxt);
  DPSDestroyContext(_dpsCurCtxt);
  
  /* Done! */
  
  exit(0);
}

--- cut here ---

/---------------------------------------------------------------------------/
/-  Patrice Belleville				 patrice@muff.cs.mcgill.ca -/
/-        --- Nobody but myself is responsible for what I said. ---        -/
/---------------------------------------------------------------------------/