home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / mg / ri / mgrinxwindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-18  |  7.1 KB  |  289 lines

  1.  
  2. #import <appkit/appkit.h>
  3. #import <appkit/graphics.h>
  4. #import <dpsclient/wraps.h>
  5.  
  6. #include "mgriP.h"
  7.  
  8. /* The MGView class is the class which handles drawing */
  9. @interface MGView:View
  10. {
  11.     mgricontext *theContext;
  12. }
  13. - (BOOL)canBecomeMainWindow;
  14. - (BOOL)canBecomeKeyWindow;
  15. - windowDidResize:sender;
  16. - windowWillClose:sender;
  17. - windowDidExpose:sender; /* nonretained windows */
  18. - windowWillMiniaturize:sender toMiniwindow:miniwindow;
  19. - windowDidDeminiaturize:sender;
  20. - windowDidBecomeKey:sender;
  21. - windowDidBecomeMain:sender;
  22. - drawSelf:(NXRect *)r :(int)c;
  23.  
  24. @end
  25.  
  26. @implementation MGView
  27.  
  28. - (BOOL)canBecomeMainWindow
  29. {
  30.   return YES;
  31. }
  32.  
  33. - (BOOL)canBecomeKeyWindow
  34. {
  35.   return YES;
  36. }
  37.  
  38. - windowDidResize:sender
  39. {
  40.     int xsize, ysize;
  41.     mgricontext *thectx =  (mgricontext *)mgri_findctx((char *)sender );
  42.  
  43.     [window display];
  44.     
  45.     if(thectx!=NULL && thectx->callback!=NULL) {
  46.         thectx->callback(thectx, MG_RIWINDOWRESIZED);
  47.     }
  48.     return self;
  49. }
  50.  
  51. - windowWillClose:sender
  52. {
  53.     mgricontext *thectx = (mgricontext *)mgri_findctx((char *)sender );
  54.     if(thectx!=NULL && thectx->callback!=NULL) {
  55.         thectx->dying=1;
  56.         thectx->callback(thectx, MG_RIWINDOWCLOSED);
  57.     
  58.     }
  59.     return self;
  60. }
  61.  
  62. - windowWillMiniaturize:sender toMiniwindow:miniwindow
  63. {
  64.     mgricontext *thectx =  (mgricontext *)mgri_findctx((char *)sender );
  65.     if(thectx!=NULL && thectx->callback!=NULL) {
  66.         thectx->callback(thectx, MG_RIWINDOWICONIFIED);
  67.     }
  68.     return self;
  69. }
  70.  
  71. - windowDidDeminiaturize:sender
  72. {
  73.     mgricontext *thectx =  (mgricontext *)mgri_findctx((char *)sender );
  74.     if(thectx!=NULL && thectx->callback!=NULL) {
  75.         thectx->callback(thectx, MG_RIWINDOWDEICONIFIED);
  76.     }
  77.     return self;
  78. }
  79.  
  80. - windowDidExpose:sender
  81. {
  82.     /* for use with non-retained windows. Is this valid? */
  83.     mgricontext *thectx =  (mgricontext *)mgri_findctx((char *)sender );
  84.     if(thectx!=NULL && thectx->callback!=NULL) {
  85.         thectx->callback(thectx, MG_RIWINDOWEXPOSED);
  86.     }
  87.     return self;
  88. }
  89.  
  90. - windowDidBecomeKey:sender
  91. {
  92.     mgricontext *thectx =  (mgricontext *)mgri_findctx((char *)sender );
  93.     if(thectx!=NULL && thectx->callback!=NULL) {
  94.         thectx->callback(thectx, MG_RIWINDOWBECAMEKEY);
  95.     }
  96.     return self;
  97. }
  98.  
  99. - windowDidBecomeMain:sender
  100. {
  101.     mgricontext *thectx =  (mgricontext *)mgri_findctx((char *)sender );
  102.     if(thectx!=NULL && thectx->callback!=NULL) {
  103.         thectx->callback(thectx, MG_RIWINDOWBECAMEMAIN);
  104.     }
  105.     return self;
  106. }
  107.  
  108. - drawSelf:(NXRect *)r :(int)c
  109. {
  110.   register ColorA *bg;
  111.   bg=&_mgc->background;
  112.   PSsetrgbcolor(bg->r, bg->g, bg->b);
  113.   NXRectFill(&bounds);
  114.   return self;
  115. }
  116.  
  117. @end
  118.  
  119. int
  120. mgri_nxwindow(int x, int y, int xs, int ys, char *name,
  121.           int noborder, unsigned int *globalNum)
  122. {
  123.     NXRect r, win;
  124.     NXPoint mgvorigin;
  125.     id dwindow, dview;
  126.     
  127.     /* if the window already exists, return */
  128.     if(_mgric->nxwindow) return 0;
  129.     
  130.     r.origin.x = x;
  131.     r.origin.y = y;
  132.     r.size.width = xs;
  133.     r.size.height = ys;
  134.     
  135.     /* check for application object */
  136.     if(NXApp==NULL) {
  137.         /* we must create an 'application' object */
  138.     /* this creates a connection with the window server */
  139.         NXApp = [Application new];
  140.     _mgric->standalone = 1;
  141.     } else _mgric->standalone = 0;
  142.  
  143.     /* create the window */
  144.     dwindow = (id)_mgric->nxwindow = (char *)[[Window alloc]
  145.         initContent:&r
  146.     style:noborder ? NX_PLAINSTYLE : NX_RESIZEBARSTYLE
  147.     backing:NX_BUFFERED
  148.     buttonMask:NX_CLOSEBUTTONMASK
  149.            |NX_MINIATURIZEBUTTONMASK
  150.     defer:NO];
  151.  
  152.     /* following used to create window resource */
  153.     NXConvertWinNumToGlobal([dwindow windowNum], globalNum);
  154.  
  155.     r.origin.x = 0;
  156.     r.origin.y = 0;
  157.         
  158.     dview = (id)_mgric->nxview = (char *)[[MGView alloc] initFrame:&r];
  159.     [(id)_mgric->nxview allocateGState]; /* for faster (un)lockfocus */
  160.     
  161.     [[(id)_mgric->nxwindow contentView] addSubview:(id)_mgric->nxview];
  162.     mgvorigin.x = 0;
  163.     mgvorigin.y = r.size.height;
  164.     [(id)_mgric->nxview convertPoint:&mgvorigin toView:nil];
  165.     [(id)_mgric->nxwindow getFrame:&win];
  166.     mgvorigin.y = win.size.height - mgvorigin.y;
  167.     _mgric->mgvorigin[0] = (int)mgvorigin.x;
  168.     _mgric->mgvorigin[1] = (int)mgvorigin.y;
  169.     [(id)_mgric->nxwindow setDelegate:(id)_mgric->nxview];
  170.     [(id)_mgric->nxwindow addToEventMask:NX_LMOUSEDRAGGEDMASK];
  171.     [(id)_mgric->nxwindow addToEventMask:NX_RMOUSEDRAGGEDMASK];
  172.     [(id)_mgric->nxwindow addToEventMask:NX_RMOUSEDOWNMASK];
  173.     [(id)_mgric->nxwindow addToEventMask:NX_RMOUSEUPMASK];
  174.  
  175.     [[(id)_mgric->nxwindow contentView]  setAutoresizeSubviews:YES];
  176.     [(id)_mgric->nxview setAutosizing:NX_WIDTHSIZABLE|NX_HEIGHTSIZABLE];
  177.     [(id)_mgric->nxwindow setTitle:(const char *)name];
  178.     [(id)_mgric->nxwindow display];
  179.     //[(id)_mgric->nxwindow makeKeyAndOrderFront:(id)_mgric->nxwindow];
  180.     return 1;
  181. }
  182.  
  183.  
  184. // MUST INCLUDE CONTEXT ON THIS ONE - CAN'T USE CURRENT CONTEXT!
  185. void
  186. mgri_closewindow(mgricontext *thectx)
  187. {
  188.     /* 
  189.      * we must check to see if the window is 'dying', which
  190.      * means the user is closing the window from the title
  191.      * bar & we are in the middle of the windowWillClose method
  192.      * so the window must stick around & will be closed later
  193.      * by the appkit
  194.      */
  195.     if(!thectx->dying) {
  196.     [(id)thectx->nxwindow performClose:(id)0];
  197.     }
  198. }
  199.  
  200. void
  201. mgri_display()
  202. {
  203.     [(id)_mgric->nxview display];
  204. }
  205.  
  206. void
  207. mgri_getnxrect(float *f)
  208. {
  209.     NXRect theRect;
  210.     [(id)_mgric->nxview getFrame:&theRect];
  211.     // Convert camera view's frame from superview to window coords.
  212.     [[(id)_mgric->nxview superview] convertRect:&theRect toView:nil];
  213.     f[0]=theRect.origin.x;
  214.     f[1]=theRect.origin.y;
  215.     f[2]=theRect.size.width;
  216.     f[3]=theRect.size.height;
  217. }
  218.  
  219. void
  220. mgri_clear()
  221. {
  222.   register ColorA *bg;
  223.   register Color *eg;
  224.   NXRect bounds;
  225.   bg=&_mgc->background;
  226.   [(id)_mgric->nxview lockFocus];
  227.   [(id)_mgric->nxview getBounds:&bounds];
  228.   PSsetrgbcolor(bg->r, bg->g, bg->b);
  229.   NXRectFill(&bounds);
  230.   [(id)_mgric->nxview unlockFocus];
  231.   NXPing();
  232. }
  233.  
  234. void
  235. mgri_flush()
  236. {
  237.     [(id)_mgric->nxwindow flushWindow];
  238. }
  239.  
  240. void mgri_processevents()
  241. {
  242.     NXEvent theEvent;
  243.     while(DPSGetEvent(DPSGetCurrentContext(), &theEvent, NX_ALLEVENTS,
  244.         0.0, NX_BASETHRESHOLD))
  245.     {
  246.         [NXApp sendEvent:&theEvent];
  247.     }
  248. }
  249.  
  250. void
  251. mgri_errorhandler(RtInt type, RtInt severity, char *message,
  252.                   RtToken routine, RtToken context)
  253. {
  254.     /* Warnings and Info are ignored. We could just ignore those
  255.        troublesome warnings such as clip plane and anisometric xform, eh? */
  256.        
  257.     if(severity==2) {
  258.         fprintf(stderr,"MGRI: QRMAN reports RIE_ERROR. type:%d message:%s\n",type,message);
  259.     return;
  260.     }
  261.     if(severity==3) {
  262.         fprintf(stderr,"MGRI: QRMAN reports RIE_SEVERE. type:%d message:%s\n",type,message);
  263.     fprintf(stderr,"SUGGEST TERMINATION!\n");
  264.     }
  265. }
  266.  
  267. void mgri_startdebugcontext()
  268. {
  269.     /* if the context does not exists, create it */
  270.     if(!_mgric->debugContext) {
  271.     _mgric->debugContext = RiBegin(NULL, NULL);
  272.     RiHider(RI_HIDDEN,RI_NULL);
  273.         _mgric->debugResource = RiResource("File", RI_ARCHIVE, RI_NULL);
  274.     }
  275.     
  276. }
  277.  
  278. void mgri_stopdebugcontext()
  279. {
  280. }
  281.  
  282. void mgri_debugcontext()
  283. {
  284.     /* toggel debug context */
  285.     if(_mgric->debug) _mgric->debug = 0;
  286.     else _mgric->debug = 1;
  287. }
  288.  
  289.