src/freeglut_main.c:816 and :1571, set the current window (John F. Fay)
[freeglut] / src / freeglut_main.c
index 1c11f0d..50dd5e4 100644 (file)
@@ -476,7 +476,6 @@ void FGAPIENTRY glutMainLoopEvent( void )
      * Grab the next event to be processed...
      */
     XNextEvent( fgDisplay.Display, &event );
-    window = fgWindowByHandle ( event.xany.window ) ;
 
     /*
      * Check the event's type
@@ -496,7 +495,6 @@ void FGAPIENTRY glutMainLoopEvent( void )
       /*
        * Call the window closure callback, remove from the structure, etc.
        */
-      fgStructure.Window = window ;
 /*      fgAddToWindowDestroyList ( window, FALSE ); */
 
       break;
@@ -507,7 +505,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
        */
       if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
       {
-        fgStructure.Window = window ;
+        GETWINDOW( xclient ); 
 
         /*
          * Call the XWindows functions to close the window
@@ -532,7 +530,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
        * We are too dumb to process partial exposes...
        */
       if( event.xexpose.count == 0 )
-          fghRedrawWindowByHandle( window->Window.Handle );
+          fghRedrawWindowByHandle( event.xexpose.window );
       break;
 
     case ConfigureNotify:
@@ -559,6 +557,8 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * The window's visiblity might have changed
          */
+        GETWINDOW( xvisibility ); 
+
         /*
          * Break now if no window status callback has been hooked to that window
          */
@@ -609,7 +609,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * Mouse is over one of our windows
          */
-        GETMOUSE( xcrossing );
+        GETWINDOW( xcrossing ); GETMOUSE( xcrossing );
 
         /*
          * Is there an entry callback hooked to the window?
@@ -629,7 +629,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * Mouse is no longer over one of our windows
          */
-        GETMOUSE( xcrossing );
+        GETWINDOW( xcrossing ); GETMOUSE( xcrossing );
 
         /*
          * Is there an entry callback hooked to the window?
@@ -649,12 +649,20 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * The mouse cursor was moved...
          */
-        GETMOUSE( xmotion );
+        GETWINDOW( xmotion ); GETMOUSE( xmotion );
 
         /*
-         * Set the current window
+         * Fallback if there's an active menu hooked to this window
          */
-        fgStructure.Window = window ;
+        if( window->ActiveMenu != NULL )
+        {
+            /*
+             * Let's make the window redraw as a result of the mouse motion.
+             */
+            window->State.Redisplay = TRUE ;
+
+            break;
+        }
 
         /*
          * What kind of a movement was it?
@@ -670,6 +678,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
           if( window->Callbacks.Motion != NULL )
           {
             /*
+             * Set the current window
+             */
+            fgStructure.Window = window ;
+
+            /*
              * Yup. Have it executed immediately
              */
             window->Callbacks.Motion( event.xmotion.x, event.xmotion.y );
@@ -683,6 +696,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
           if( window->Callbacks.Passive != NULL )
           {
             /*
+             * Set the current window
+             */
+            fgStructure.Window = window ;
+
+            /*
              * That's right, and there is a passive callback, too.
              */
             window->Callbacks.Passive( event.xmotion.x, event.xmotion.y );
@@ -703,7 +721,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
          * A mouse button has been pressed or released. Traditionally,
          * break if the window was found within the freeglut structures.
          */
-        GETMOUSE( xbutton );
+        GETWINDOW( xbutton ); GETMOUSE( xbutton );
 
         /*
          * GLUT API assumes that you can't have more than three mouse buttons, so:
@@ -749,7 +767,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
 
             /* Execute the menu callback */
             fgExecuteMenuCallback ( window->ActiveMenu ) ;
-            fgDeactivateMenu ( window ) ;
+            fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
 
             /* Restore the current window and menu */
             fgSetWindow ( save_window ) ;
@@ -757,7 +775,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
           }
           else  /* Outside the menu, deactivate the menu if it's a downclick */
           {
-            if ( pressed == TRUE ) fgDeactivateMenu ( window ) ;
+            if ( pressed == TRUE ) fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
           }
 
           /*
@@ -779,6 +797,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
           window->State.Redisplay = TRUE ;
 
           /*
+           * Set the current window
+           */
+          fgSetWindow( window );
+
+          /*
            * Activate the appropriate menu structure...
            */
           fgActivateMenu( window, button );
@@ -789,13 +812,13 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * Check if there is a mouse callback hooked to the window
          */
-        if( window->Callbacks.Mouse == NULL )
+        if( fgStructure.Window->Callbacks.Mouse == NULL )
           break;
-
         /*
          * Set the current window
          */
-        fgSetWindow( window );
+        fgSetWindow ( window );
+
 
         /*
          * Remember the current modifiers state
@@ -807,12 +830,12 @@ void FGAPIENTRY glutMainLoopEvent( void )
           modifiers |= GLUT_ACTIVE_CTRL;
         if (event.xbutton.state & Mod1Mask)
           modifiers |= GLUT_ACTIVE_ALT;
-        window->State.Modifiers = modifiers;
+        fgStructure.Window->State.Modifiers = modifiers;
 
         /*
          * Finally execute the mouse callback
          */
-        window->Callbacks.Mouse(
+        fgStructure.Window->Callbacks.Mouse(
             button,
             event.type == ButtonPress ? GLUT_DOWN : GLUT_UP,
             event.xbutton.x,
@@ -822,7 +845,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * Trash the modifiers state
          */
-        window->State.Modifiers = 0xffffffff;
+        fgStructure.Window->State.Modifiers = 0xffffffff;
       }
       break;
 
@@ -835,7 +858,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * A key has been pressed, find the window that had the focus:
          */
-        GETMOUSE( xkey );
+        GETWINDOW( xkey ); GETMOUSE( xkey );
 
         if( event.type == KeyPress )
         {
@@ -1048,6 +1071,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
     fghDisplayAll();
   }
 #endif
+
+    /* 
+     * If an event caused a window to be closed, do the actual closing here
+     */
+    fgCloseWindows () ;
 }
 
 /*
@@ -1095,11 +1123,6 @@ void FGAPIENTRY glutMainLoop( void )
     glutMainLoopEvent () ;
 
     /*
-     * If an event caused a window to be closed, do the actual closing here
-     */
-    fgCloseWindows () ;
-
-    /*
      * If there are no more windows open, stop execution
      */
     if ( fgStructure.Windows.First == NULL )
@@ -1170,14 +1193,48 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         window->Window.Device = GetDC( hWnd );
 
         /*
-         * Setup the pixel format of our window
+         * Create or get the OpenGL rendering context now
          */
-        fgSetupPixelFormat( window, FALSE, PFD_MAIN_PLANE );
+        if ( fgState.BuildingAMenu )
+        {
+          /*
+           * Setup the pixel format of our window
+           */
+          unsigned int current_DisplayMode = fgState.DisplayMode ;
+          fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ;
+          fgSetupPixelFormat( window, FALSE, PFD_MAIN_PLANE );
+          fgState.DisplayMode = current_DisplayMode ;
 
-        /*
-         * Create the OpenGL rendering context now
-         */
-        window->Window.Context = wglCreateContext( window->Window.Device );
+          /*
+           * If there isn't already an OpenGL rendering context for menu windows, make one
+           */
+          if ( !fgStructure.MenuContext )
+          {
+            fgStructure.MenuContext = (SFG_MenuContext *)malloc ( sizeof(SFG_MenuContext) ) ;
+            fgStructure.MenuContext->Context = wglCreateContext( window->Window.Device );
+          }
+          else
+            wglMakeCurrent ( window->Window.Device, fgStructure.MenuContext->Context ) ;
+
+/*          window->Window.Context = wglGetCurrentContext () ;   */
+          window->Window.Context = wglCreateContext( window->Window.Device );
+        }
+        else
+        {
+          /*
+           * Setup the pixel format of our window
+           */
+          fgSetupPixelFormat( window, FALSE, PFD_MAIN_PLANE );
+
+          if ( fgState.UseCurrentContext == TRUE )
+          {
+            window->Window.Context = wglGetCurrentContext();
+            if ( ! window->Window.Context )
+              window->Window.Context = wglCreateContext( window->Window.Device );
+          }
+          else
+            window->Window.Context = wglCreateContext( window->Window.Device );
+        }
 
         /*
          * Still, we'll be needing to explicitly resize the window
@@ -1287,8 +1344,20 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
          */
         if( fgStructure.Window == window )
         {
+          int used = FALSE ;
+          SFG_Window *iter ;
+
             wglMakeCurrent( NULL, NULL );
-            wglDeleteContext( window->Window.Context );
+            /* Step through the list of windows.  If the rendering context is notbeing used
+             * by another window, then we delete it.
+             */
+            for ( iter = fgStructure.Windows.First; iter; iter = iter->Node.Next )
+            {
+              if ( ( iter->Window.Context == window->Window.Context ) && ( iter != window ) )
+                used = TRUE ;
+            }
+
+            if ( used == FALSE ) wglDeleteContext( window->Window.Context );
         }
 
         /*
@@ -1450,7 +1519,7 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
 
             /* Execute the menu callback */
             fgExecuteMenuCallback ( window->ActiveMenu ) ;
-            fgDeactivateMenu ( window ) ;
+            fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
 
             /* Restore the current window and menu */
             fgSetWindow ( save_window ) ;
@@ -1458,13 +1527,13 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
           }
           else  /* Outside the menu, deactivate the menu if it's a downclick */
           {
-            if ( pressed == TRUE ) fgDeactivateMenu ( window ) ;
+            if ( pressed == TRUE ) fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
           }
 
           /*
            * Let's make the window redraw as a result of the mouse click and menu activity.
            */
-          window->State.Redisplay = TRUE ;
+          if ( ! window->IsMenu ) window->State.Redisplay = TRUE ;
 
           break ;
         }
@@ -1480,6 +1549,11 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
             window->State.Redisplay = TRUE ;
 
             /*
+             * Set the current window
+             */
+            fgSetWindow( window );
+
+            /*
              * Activate the appropriate menu structure...
              */
             fgActivateMenu( window, button );
@@ -1490,26 +1564,26 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         /*
          * Check if there is a mouse callback hooked to the window
          */
-        if( window->Callbacks.Mouse == NULL )
+        if( fgStructure.Window->Callbacks.Mouse == NULL )
             break;
 
         /*
-         * Set the current window
-         */
-        fgSetWindow( window );
-
-        /*
          * Remember the current modifiers state.
          */
-        window->State.Modifiers = 
+        fgStructure.Window->State.Modifiers = 
             ( ( (GetKeyState( VK_LSHIFT   ) < 0 ) || ( GetKeyState( VK_RSHIFT   ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
             ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL  : 0 ) |
             ( ( (GetKeyState( VK_LMENU    ) < 0 ) || ( GetKeyState( VK_RMENU    ) < 0 )) ? GLUT_ACTIVE_ALT   : 0 );
 
         /*
+         * Set the current window
+         */
+        fgSetWindow ( window );
+
+        /*
          * Finally execute the mouse callback
          */
-        window->Callbacks.Mouse(
+        fgStructure.Window->Callbacks.Mouse(
             button,
             pressed == TRUE ? GLUT_DOWN : GLUT_UP,
             window->State.MouseX,
@@ -1519,7 +1593,7 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         /*
          * Trash the modifiers state
          */
-        window->State.Modifiers = 0xffffffff;
+        fgStructure.Window->State.Modifiers = 0xffffffff;
     }
     break;