home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Utility Library / Source / Q3UL.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  12.8 KB  |  591 lines  |  [TEXT/CWIE]

  1. /*
  2.  *
  3.  * Utility library for QuickDraw 3D.
  4.  *
  5.  * Nick Thompson, nickt@apple.com
  6.  * Send bug reports and feedback to devsupport@apple.com.
  7.  *
  8.  * ©1997 Apple Computer Inc, All Rights Reserved 
  9.  *
  10.  * Modification History:
  11.  *
  12.  */
  13.  
  14. #include <assert.h>
  15. #include <DiskInit.h>
  16. #include <Fonts.h>
  17. #include <QuickDraw.h>
  18. #include <Windows.h>
  19.  
  20. #include "QD3D.h"
  21. #include "Q3UL.h"
  22. #include "Q3ULPriv.h"
  23. #include "Q3UL_QD3DUtils.h"
  24.  
  25. /* currently we have our window list stored in an array, this
  26.  * could be extended to use a variable length array (we add items
  27.  * to the array as needed, or as a linked list.  For now we just
  28.  * want to keep it as simple as possible.
  29.  */
  30.  
  31. TQ3UL_WindowDescriptor        gWindowArray[ MAXNUMWINDOWS ] ;
  32. Boolean                        gQuitFlag = false ;
  33.  
  34. /*------------------------------------------------------------------------------
  35.  * zero out the referenced element of the windows array.
  36.  */
  37.  
  38. static void iZeroWinElement( TQ3UL_WindowRef windowID )
  39. {
  40. #if defined( DEBUGGING ) && DEBUGGING
  41.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  42. #endif 
  43.     gWindowArray[windowID].window    = NULL ;
  44.     gWindowArray[windowID].refType    = 0L ;
  45.     gWindowArray[windowID].refCon     = 0L ;
  46.     
  47.     gWindowArray[windowID].theCloseHandler        = NULL ;
  48.     gWindowArray[windowID].theKeyHandler         = NULL ;
  49.     gWindowArray[windowID].theMouseDownHandler    = NULL ;
  50.     gWindowArray[windowID].theMouseUpHandler     = NULL ;
  51.     gWindowArray[windowID].theMouseDragHandler    = NULL ;
  52.     gWindowArray[windowID].theRedrawHandler     = NULL ;
  53.     gWindowArray[windowID].theIdleHandler         = NULL ;
  54.     
  55.     gWindowArray[windowID].theView = NULL ;
  56. }
  57.  
  58. /*------------------------------------------------------------------------------
  59.  * get the index of the specified window, -1 if it can't be found.
  60.  */
  61.  
  62. static long iWindowPtrToIdx( WindowPtr theWindow )
  63. {
  64.     long    idx ;
  65.     for(idx = 0; idx<MAXNUMWINDOWS; idx++ )
  66.         if(gWindowArray[idx].window == theWindow)
  67.             break ;
  68.             
  69.     if(idx < 0 || idx >= MAXNUMWINDOWS)
  70.         idx = -1 ;
  71.  
  72.     return idx ;    
  73. }
  74.  
  75. /*------------------------------------------------------------------------------
  76.  * get the high 16 bits of a long.
  77.  */
  78.  
  79. short HiWrd(long aLong)
  80. {
  81.     return    (((aLong) >> 16) & 0xFFFF) ;
  82. }
  83.  
  84. /*------------------------------------------------------------------------------
  85.  * get the low 16 bits of a long.
  86.  */
  87.  
  88. short LoWrd(long aLong)
  89. {
  90.     return    ((aLong) & 0xFFFF) ;
  91. }
  92.  
  93. /*------------------------------------------------------------------------------
  94.  * Initialize the utility library.  Calls Q3Initialize.  Initialize any other
  95.  * data structures used by the library.
  96.  */
  97.  
  98. TQ3Status Q3UL_Initialize( void )
  99. {
  100.     TQ3Status        theStatus ;
  101.     long            i ;
  102.     
  103.     /* first step is to initialize the window array
  104.      * to known values
  105.      */
  106.      
  107.     for( i = 0; i<MAXNUMWINDOWS; i++)
  108.         iZeroWinElement( i ) ;
  109.     
  110.     /*
  111.      * next step is to initialize the toolbox managers
  112.      */
  113.     
  114.     MaxApplZone() ;
  115.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  116.     
  117.     InitGraf( &qd.thePort );
  118.     InitFonts();
  119.     InitWindows();
  120.     InitCursor();
  121.  
  122.     FlushEvents( everyEvent, 0 ) ;
  123.     
  124.     /*
  125.      * finally initialize QuickDraw 3D
  126.      */
  127.     
  128.     theStatus = Q3Initialize() ;
  129.     
  130.     return theStatus ;
  131.  
  132.  
  133. /*------------------------------------------------------------------------------
  134.  * Call Q3Exit(), clean up and dispose of anything the library allocated
  135.  */
  136. TQ3Status Q3UL_Terminate( void )
  137. {
  138.     TQ3Status        theStatus ;
  139.     
  140.     /* signal the main event loop that we are done */
  141.     gQuitFlag = true ;
  142.     
  143.     /* close the connection to the QD3D library */
  144.     theStatus = Q3Exit() ;
  145.     
  146.     return theStatus ;
  147.  
  148. /*------------------------------------------------------------------------------
  149.  * 
  150.  */
  151. void    Q3UL_SetPrivType( 
  152.     TQ3UL_WindowRef windowID, 
  153.     unsigned long refType)
  154. {
  155. #if defined( DEBUGGING ) && DEBUGGING
  156.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  157. #endif
  158.     gWindowArray[windowID].refType = refType ;
  159. }
  160.  
  161. /*------------------------------------------------------------------------------
  162.  * 
  163.  */
  164. unsigned long Q3UL_GetPrivType( 
  165.     TQ3UL_WindowRef windowID )
  166. {
  167. #if defined( DEBUGGING ) && DEBUGGING
  168.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  169. #endif
  170.     return gWindowArray[windowID].refType ;
  171. }
  172.  
  173. /*------------------------------------------------------------------------------
  174.  * 
  175.  */
  176. void    Q3UL_SetPrivData( 
  177.     TQ3UL_WindowRef windowID, 
  178.     void *priv)
  179. {
  180. #if defined( DEBUGGING ) && DEBUGGING
  181.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  182. #endif
  183.     if( priv != NULL )
  184.         gWindowArray[windowID].refCon = (unsigned long)priv ;
  185. }
  186.  
  187. /*------------------------------------------------------------------------------
  188.  * 
  189.  */
  190. void    *Q3UL_GetPrivData( 
  191.     TQ3UL_WindowRef windowID )
  192. {
  193. #if defined( DEBUGGING ) && DEBUGGING
  194.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  195. #endif
  196.     return (void *)gWindowArray[windowID].refCon ;
  197. }
  198.  
  199.  
  200. /*------------------------------------------------------------------------------
  201.  * 
  202.  */
  203.  
  204. void    Q3UL_RegisterCloseWindowHandler( 
  205.     TQ3UL_WindowRef             windowID, 
  206.     TQ3UL_CloseWindowHandler     theCloseHandler )
  207. {
  208. #if defined( DEBUGGING ) && DEBUGGING
  209.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  210. #endif
  211.     gWindowArray[windowID].theCloseHandler = theCloseHandler ;
  212.  
  213. }
  214.  
  215. /*------------------------------------------------------------------------------
  216.  * 
  217.  */
  218.  
  219. void    Q3UL_RegisterKeyHandler( 
  220.     TQ3UL_WindowRef     windowID, 
  221.     TQ3UL_KeyHandler     theKeyHandler )
  222. {
  223. #if defined( DEBUGGING ) && DEBUGGING
  224.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  225. #endif
  226.     gWindowArray[windowID].theKeyHandler = theKeyHandler ;
  227. }
  228.  
  229.  
  230.  
  231. /*------------------------------------------------------------------------------
  232.  * 
  233.  */
  234.  
  235. void Q3UL_RegisterMouseDownHandler( 
  236.     TQ3UL_WindowRef         windowID, 
  237.     TQ3UL_MouseDownHandler     theMouseDownHandler )
  238. {
  239. #if defined( DEBUGGING ) && DEBUGGING
  240.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  241. #endif
  242.     gWindowArray[windowID].theMouseDownHandler = theMouseDownHandler ;
  243. }
  244.  
  245.  
  246.  
  247. /*------------------------------------------------------------------------------
  248.  * 
  249.  */
  250.  
  251. void Q3UL_RegisterMouseUpHandler( 
  252.     TQ3UL_WindowRef         windowID, 
  253.     TQ3UL_MouseUpHandler     theMouseUpHandler )
  254. {
  255. #if defined( DEBUGGING ) && DEBUGGING
  256.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  257. #endif
  258.     gWindowArray[windowID].theMouseUpHandler = theMouseUpHandler ;
  259.  
  260. }
  261.  
  262.  
  263.  
  264. /*------------------------------------------------------------------------------
  265.  * 
  266.  */
  267.  
  268. void Q3UL_RegisterMouseDragHandler( 
  269.     TQ3UL_WindowRef         windowID, 
  270.     TQ3UL_MouseDragHandler     theMouseDragHandler )
  271. {
  272. #if defined( DEBUGGING ) && DEBUGGING
  273.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  274. #endif
  275.     gWindowArray[windowID].theMouseDragHandler = theMouseDragHandler ;
  276.  
  277. }
  278.  
  279.  
  280.  
  281. /*------------------------------------------------------------------------------
  282.  * 
  283.  */
  284.  
  285. void Q3UL_RegisterRedraw( 
  286.     TQ3UL_WindowRef         windowID, 
  287.     TQ3UL_RedrawHandler     theRedrawHandler )
  288. {
  289. #if defined( DEBUGGING ) && DEBUGGING
  290.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  291. #endif
  292.     gWindowArray[windowID].theRedrawHandler = theRedrawHandler ;
  293.  
  294. }
  295.  
  296.  
  297.  
  298. /*------------------------------------------------------------------------------
  299.  * 
  300.  */
  301.  
  302. void Q3UL_RegisterIdle( 
  303.     TQ3UL_WindowRef         windowID, 
  304.     TQ3UL_IdleHandler         theIdleHandler )
  305. {
  306. #if defined( DEBUGGING ) && DEBUGGING
  307.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  308. #endif
  309.     gWindowArray[windowID].theIdleHandler = theIdleHandler ;
  310. }
  311.  
  312.  
  313.  
  314. /*------------------------------------------------------------------------------
  315.  * 
  316.  */
  317.  
  318. TQ3UL_WindowRef Q3UL_NewWindow(
  319.     short width, 
  320.     short height )
  321. {
  322.     WindowPtr        theWindow ;
  323.     static    short    staggerPos = 50 ;
  324.     int                i ;
  325.  
  326.     /* skanky hack to offset the window, not that you 
  327.      * could create something that is offscreen, so really
  328.      * should check to see if the values need to wrap.
  329.      */
  330.     Rect            boundsRect  ;
  331.     
  332.     boundsRect.top = boundsRect.left = staggerPos ;
  333.     boundsRect.bottom = staggerPos+height ;
  334.     boundsRect.right = staggerPos+height ;
  335.  
  336.     /* find a place for the window in the array */
  337.     while( i < MAXNUMWINDOWS )
  338.     {
  339.         if( gWindowArray[i].window == NULL ) 
  340.             break ;
  341.     }
  342.     
  343.     if( i >= MAXNUMWINDOWS )
  344.     {
  345.         /* can create a window, the array is full */
  346.         SysBeep(10) ;
  347.         return -1 ;
  348.     } 
  349.     
  350.     /* create a new window of width x height */
  351.     theWindow = NewCWindow ( NULL, 
  352.                              &boundsRect, 
  353.                              "\pUntitled", 
  354.                              false, 
  355.                              zoomDocProc, 
  356.                              (WindowPtr)-1, 
  357.                              true, 
  358.                              0L ) ;
  359.     
  360.     if( theWindow != NULL )
  361.         gWindowArray[i].window = theWindow ;         
  362.         
  363.     /*
  364.      * set up a quickDraw 3D view for this window 
  365.      */
  366.      
  367.     gWindowArray[i].theView = Q3UL_NewView( i ) ;
  368.                      
  369.     ShowWindow(theWindow) ;
  370.     
  371.     return i ;
  372.     
  373. }
  374.  
  375.  
  376.  
  377. /*------------------------------------------------------------------------------
  378.  * Dispose of a utility library window, make sure you dispose of associated
  379.  * storage before calling this.
  380.  */
  381.  
  382. void Q3UL_DestroyWindow( 
  383.     TQ3UL_WindowRef windowID )
  384. {
  385. #if defined( DEBUGGING ) && DEBUGGING
  386.     assert( windowID >= 0 && windowID < MAXNUMWINDOWS ) ;
  387. #endif
  388.     /*
  389.      * first get rid of the view we created for this object
  390.      */
  391.     Q3Object_Dispose( gWindowArray[windowID].theView ) ;
  392.     
  393.     /*
  394.      * get rid of the window itself
  395.      */
  396.     DisposeWindow( gWindowArray[windowID].window ) ;
  397.  
  398.     /* reset the fields to nil */
  399.     iZeroWinElement( windowID ) ;
  400.  
  401. }
  402.  
  403.  
  404.  
  405.  
  406. /*------------------------------------------------------------------------------
  407.  * 
  408.  */
  409.  
  410. void Q3UL_ResizeFrontWindow( 
  411.     short x, 
  412.     short y )
  413. {
  414.  
  415. }
  416.  
  417.  
  418.  
  419. /*------------------------------------------------------------------------------
  420.  * 
  421.  */
  422.  
  423. void Q3UL_ZoomFrontWindow()
  424. {
  425.  
  426. }
  427.  
  428.  
  429.  
  430. /*------------------------------------------------------------------------------
  431.  * 
  432.  */
  433.  
  434. void Q3UL_MoveFrontWindow( 
  435.     short top, 
  436.     short left )
  437. {
  438.     MoveWindow ( FrontWindow(), left, top, true ) ;    
  439. }
  440.  
  441. /*------------------------------------------------------------------------------
  442.  * 
  443.  */
  444.  
  445. void Q3UL_RedrawWindow(
  446.     TQ3UL_WindowRef windowID )
  447. {
  448.     GrafPtr        savedPort ;
  449.     
  450.     GetPort( &savedPort ) ;
  451.     SetPort( gWindowArray[windowID].window ) ;
  452.     InvalRect( &gWindowArray[windowID].window->portRect ) ;    
  453.     SetPort( savedPort ) ;
  454. }
  455.  
  456. /*------------------------------------------------------------------------------
  457.  * 
  458.  */
  459.  
  460. void Q3UL_MainEventLoop(void)
  461. {
  462.     EventRecord     event;
  463.     WindowPtr       window;
  464.     short           thePart;
  465.     Rect            screenRect, updateRect;
  466.     Point            aPoint = {100, 100};
  467.     TQ3UL_WindowRef    windowID ;
  468.     
  469.  
  470.     while( !gQuitFlag )
  471.     {
  472.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  473.         {
  474.  
  475.             switch (event.what) {
  476.                 case mouseDown:
  477.                 
  478.                     thePart = FindWindow( event.where, &window );
  479.                     windowID = iWindowPtrToIdx( window ) ;
  480.                     
  481.                     switch( thePart ) {
  482.                         case inMenuBar: 
  483.                             break;
  484.                         
  485.                         case inDrag:
  486.                             /* drag the window by the title bar */
  487.                             screenRect = (**GetGrayRgn()).rgnBBox;
  488.                             DragWindow( FrontWindow(), event.where, &screenRect );
  489.                             break ;
  490.                     
  491.                         case inContent:
  492.                         
  493.                             /* content region: if the window is not the front
  494.                              * window make it so 
  495.                              */
  496.                             if (window != FrontWindow())
  497.                                 SelectWindow( window );
  498.                             
  499.                             /* if we have a mouse hanfler, call it, passing in the window 
  500.                              * reference, and the x, and y position 
  501.                              */
  502.                             if(gWindowArray[windowID].theMouseDownHandler != NULL )
  503.                                 (*gWindowArray[windowID].theMouseDownHandler)( 
  504.                                         windowID,
  505.                                         event.where.h,
  506.                                         event.where.v );
  507.                             
  508.                             break ;
  509.                     
  510.                         case inGoAway:
  511.                             if (TrackGoAway( window, event.where )) {
  512.                                 /* check to see if there is a close handler for this window */
  513.                                 if( gWindowArray[windowID].theCloseHandler)
  514.                                 {
  515.                                     /* yes: then call it */
  516.                                     (*gWindowArray[windowID].theCloseHandler)(windowID) ;
  517.                                 }
  518.                                 else
  519.                                 {
  520.                                     /* jusr dispose of the window */
  521.                                     DisposeWindow ( window );
  522.                                 }
  523.  
  524.                             }
  525.                             break ;
  526.                             
  527.                         default:
  528.                             break ;
  529.                     }
  530.                     break ;
  531.                             
  532.                         
  533.                 case updateEvt:
  534.                 
  535.                     window = (WindowPtr)event.message;
  536.                     windowID = iWindowPtrToIdx( window ) ;
  537.                     updateRect = (**(window->visRgn)).rgnBBox;
  538.                     SetPort( window ) ;
  539.                     BeginUpdate( window );
  540.                     
  541.                     /* check to see if there is a redraw handler for this window */
  542.                     if( gWindowArray[windowID].theRedrawHandler)
  543.                     {
  544.                         /* yes: then call it */
  545.                         (*gWindowArray[windowID].theRedrawHandler)(windowID,gWindowArray[windowID].theView) ;
  546.                     }
  547.  
  548.                     EndUpdate( window );
  549.                     break ;
  550.                     
  551.                 case keyDown:
  552.                 case autoKey:
  553.                     /* check to see if there is a redraw handler for this window */
  554.                     if( gWindowArray[windowID].theKeyHandler)
  555.                     {
  556.                         /* yes: then call it */
  557.                         (*gWindowArray[windowID].theKeyHandler)
  558.                             (windowID,
  559.                             event.where.h,
  560.                             event.where.v,
  561.                             (event.message & charCodeMask) ) ;
  562.                     }
  563.  
  564.                     break;
  565.                     
  566.                 case diskEvt:
  567.                     if ( HiWrd(event.message) != noErr ) 
  568.                         (void) DIBadMount(aPoint, event.message);
  569.                     break;
  570.                     
  571.                 case osEvt:
  572.                 case activateEvt:
  573.                     break;
  574.  
  575.  
  576.             }
  577.         }
  578.         else {
  579.             /* check to see if there is a idle handler for this window */
  580.             if( gWindowArray[windowID].theIdleHandler)
  581.             {
  582.                 /* yes: then call it */
  583.                 (*gWindowArray[windowID].theIdleHandler)(windowID) ;
  584.             }
  585.         }
  586.     }
  587. }
  588.  
  589.