Menus should deallocate better now - John Fay
[freeglut] / src / freeglut_main.c
index f9c78ef..bdfe139 100644 (file)
@@ -106,6 +106,8 @@ static void fghReshapeWindowByHandle
     ( HWND handle, int width, int height )
 #endif
 {
+  SFG_Window *current_window = fgStructure.Window ;
+
     /*
      * Find the window that received the reshape event
      */
@@ -142,6 +144,12 @@ static void fghReshapeWindowByHandle
      * we resize the window.
      */
     window->State.Redisplay = TRUE ;
+
+    /*
+     * If this is a menu, restore the active window
+     */
+    if ( window->IsMenu )
+      fgSetWindow ( current_window ) ;
 }
 
 /*
@@ -157,6 +165,8 @@ static void fghcbDisplayWindow( SFG_Window *window, SFG_Enumerator *enumerator )
         (window->State.Redisplay == TRUE) &&
         (window->State.Visible == TRUE) )
     {
+        SFG_Window *current_window = fgStructure.Window ;
+
         /*
          * OKi, this is the case: have the window set as the current one
          */
@@ -171,6 +181,8 @@ static void fghcbDisplayWindow( SFG_Window *window, SFG_Enumerator *enumerator )
          * And execute the display callback immediately after
          */
         window->Callbacks.Display();
+
+        fgSetWindow ( current_window ) ;
     }
 
 #elif TARGET_HOST_WIN32
@@ -180,6 +192,8 @@ static void fghcbDisplayWindow( SFG_Window *window, SFG_Enumerator *enumerator )
      */
     if( window->State.NeedToResize )
     {
+        SFG_Window *current_window = fgStructure.Window ;
+
         fgSetWindow( window );
 
         fghReshapeWindowByHandle( 
@@ -192,6 +206,8 @@ static void fghcbDisplayWindow( SFG_Window *window, SFG_Enumerator *enumerator )
          * Never ever do that again:
          */
         window->State.NeedToResize = FALSE;
+
+        fgSetWindow ( current_window ) ;
     }
 
     /*
@@ -301,9 +317,9 @@ static void fghCheckTimers( void )
     /*
      * For every timer that is waiting for triggering
      */
-    for( timer = fgState.Timers.First; timer; timer = next )
+    for( timer = (SFG_Timer *)fgState.Timers.First; timer; timer = (SFG_Timer *)next )
     {
-       next = timer->Node.Next;
+             next = (SFG_Timer *)timer->Node.Next;
 
         /*
          * Check for the timeout:
@@ -313,7 +329,7 @@ static void fghCheckTimers( void )
             /*
              * Add the timer to the timed out timers list
              */
-           fgListRemove( &fgState.Timers, &timer->Node );
+                 fgListRemove( &fgState.Timers, &timer->Node );
             fgListAppend( &timedOut, &timer->Node );
         }
     }
@@ -322,7 +338,7 @@ static void fghCheckTimers( void )
      * Now feel free to execute all the hooked and timed out timer callbacks
      * And delete the timed out timers...
      */
-    while ( (timer = timedOut.First) )
+    while ( (timer = (SFG_Timer *)timedOut.First) )
     {
         if( timer->Callback != NULL )
             timer->Callback( timer->ID );
@@ -394,13 +410,13 @@ static void fgCleanUpGlutsMess( void )
 
   if ( fgStructure.Windows.First != NULL ) 
   {
-    SFG_Window *win = fgStructure.Windows.First ;
+    SFG_Window *win = (SFG_Window *)fgStructure.Windows.First ;
     glEnd();
     glFinish();
     glFlush();
     while ( win != NULL )
     {
-      SFG_Window *temp_win = win->Node.Next ;
+      SFG_Window *temp_win = (SFG_Window *)win->Node.Next ;
       fgDestroyWindow ( win, FALSE ) ;
       win = temp_win ;
     }
@@ -617,6 +633,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
         if( window->Callbacks.Entry != NULL )
         {
           /*
+           * Set the current window
+           */
+          fgSetWindow ( window ) ;
+
+          /*
            * Yeah. Notify the window about having the mouse cursor over
            */
           window->Callbacks.Entry( GLUT_ENTERED );
@@ -637,6 +658,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
         if( window->Callbacks.Entry != NULL )
         {
           /*
+           * Set the current window
+           */
+          fgSetWindow ( window ) ;
+
+          /*
            * Yeah. Notify the window about having the mouse cursor over
            */
           window->Callbacks.Entry( GLUT_LEFT );
@@ -652,9 +678,22 @@ void FGAPIENTRY glutMainLoopEvent( void )
         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 ;
+
+            /*
+             * Since the window is a menu, make the parent window current
+             */
+            fgSetWindow ( window->ActiveMenu->ParentWindow ) ;
+
+            break;
+        }
 
         /*
          * What kind of a movement was it?
@@ -670,6 +709,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
           if( window->Callbacks.Motion != NULL )
           {
             /*
+             * Set the current window
+             */
+            fgSetWindow ( window ) ;
+
+            /*
              * Yup. Have it executed immediately
              */
             window->Callbacks.Motion( event.xmotion.x, event.xmotion.y );
@@ -683,6 +727,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
           if( window->Callbacks.Passive != NULL )
           {
             /*
+             * Set the current window
+             */
+            fgSetWindow ( window ) ;
+
+            /*
              * That's right, and there is a passive callback, too.
              */
             window->Callbacks.Passive( event.xmotion.x, event.xmotion.y );
@@ -744,12 +793,13 @@ void FGAPIENTRY glutMainLoopEvent( void )
             /* Save the current window and menu and set the current window to the window whose menu this is */
             SFG_Window *save_window = fgStructure.Window ;
             SFG_Menu *save_menu = fgStructure.Menu ;
+            SFG_Window *parent_window = window->ActiveMenu->ParentWindow ;
             fgSetWindow ( window ) ;
             fgStructure.Menu = window->ActiveMenu ;
 
             /* Execute the menu callback */
             fgExecuteMenuCallback ( window->ActiveMenu ) ;
-            fgDeactivateMenu ( window ) ;
+            fgDeactivateMenu ( parent_window ) ;
 
             /* Restore the current window and menu */
             fgSetWindow ( save_window ) ;
@@ -757,7 +807,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 +829,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
           window->State.Redisplay = TRUE ;
 
           /*
+           * Set the current window
+           */
+          fgSetWindow( window );
+
+          /*
            * Activate the appropriate menu structure...
            */
           fgActivateMenu( window, button );
@@ -795,7 +850,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * Set the current window
          */
-        fgSetWindow( window );
+        fgSetWindow ( window );
 
         /*
          * Remember the current modifiers state
@@ -807,12 +862,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 +877,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * Trash the modifiers state
          */
-        window->State.Modifiers = 0xffffffff;
+        fgStructure.Window->State.Modifiers = 0xffffffff;
       }
       break;
 
@@ -864,11 +919,6 @@ void FGAPIENTRY glutMainLoopEvent( void )
           len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode), &keySym, &composeStatus );
 
           /*
-           * Get ready to calling the keyboard/special callbacks
-           */
-          fgSetWindow( window );
-
-          /*
            * GLUT API tells us to have two separate callbacks...
            */
           if( len > 0 )
@@ -879,6 +929,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
             if( keyboard_cb != NULL )
             {
               /*
+               * Set the current window
+               */
+              fgSetWindow( window );
+
+              /*
                * Remember the current modifiers state
                */
               modifiers = 0;
@@ -953,6 +1008,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
             if( (special_cb != NULL) && (special != -1) )
             {
               /*
+               * Set the current window
+               */
+              fgSetWindow( window );
+
+              /*
                * Remember the current modifiers state
                */
               modifiers = 0;
@@ -1061,7 +1121,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
 void FGAPIENTRY glutMainLoop( void )
 {
 #if TARGET_HOST_WIN32
-  SFG_Window *window = fgStructure.Windows.First ;
+  SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
 #endif
 
   /*
@@ -1081,9 +1141,23 @@ void FGAPIENTRY glutMainLoop( void )
   while ( window != NULL )
   {
     if ( window->Callbacks.Visibility != NULL )
+    {
+      SFG_Window *current_window = fgStructure.Window ;
+
+      /*
+       * Set the current window
+       */
+      fgSetWindow( window );
+
       window->Callbacks.Visibility ( window->State.Visible ) ;
 
-    window = window->Node.Next ;
+      /*
+       * Restore the current window
+       */
+      fgSetWindow( current_window );
+    }
+
+    window = (SFG_Window *)window->Node.Next ;
   }
 #endif
 
@@ -1170,17 +1244,48 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         window->Window.Device = GetDC( hWnd );
 
         /*
-         * Setup the pixel format of our window
-         */
-        fgSetupPixelFormat( window, FALSE, PFD_MAIN_PLANE );
-
-        /*
          * Create or get the OpenGL rendering context now
          */
-        if ( fgState.UseCurrentContext == TRUE )
-          window->Window.Context = wglGetCurrentContext();
-        else
+        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 ;
+
+          /*
+           * 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
@@ -1297,7 +1402,7 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
             /* 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 )
+            for ( iter = (SFG_Window *)fgStructure.Windows.First; iter; iter = (SFG_Window *)iter->Node.Next )
             {
               if ( ( iter->Window.Context == window->Window.Context ) && ( iter != window ) )
                 used = TRUE ;
@@ -1334,13 +1439,18 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         /*
          * Fallback if there's an active menu hooked to this window
          */
-        if( window->ActiveMenu != NULL )
+        if ( window->ActiveMenu != NULL )
         {
             /*
              * Let's make the window redraw as a result of the mouse motion.
              */
             window->State.Redisplay = TRUE ;
 
+            /*
+             * Since the window is a menu, make the parent window current
+             */
+            fgSetWindow ( window->ActiveMenu->ParentWindow ) ;
+
             break;
         }
 
@@ -1465,7 +1575,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 ) ;
@@ -1473,13 +1583,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 ;
         }
@@ -1495,6 +1605,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 );
@@ -1511,12 +1626,12 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         /*
          * Set the current window
          */
-        fgSetWindow( 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 );
@@ -1534,7 +1649,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;
 
@@ -1551,11 +1666,6 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
             break;
 
         /*
-         * Set the current window
-         */
-        fgSetWindow( window );
-
-        /*
          * Remember the current modifiers state. This is done here in order 
          * to make sure the VK_DELETE keyboard callback is executed properly.
          */
@@ -1603,7 +1713,10 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
                  * The delete key should be treated as an ASCII keypress:
                  */
                 if( window->Callbacks.Keyboard != NULL )
+                {
+                    fgSetWindow( window );
                     window->Callbacks.Keyboard( 127, window->State.MouseX, window->State.MouseY );
+                }
         }
 
         /*
@@ -1612,6 +1725,11 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         if( (keypress != -1) && (window->Callbacks.Special != NULL) )
         {
             /*
+             * Set the current window
+             */
+            fgSetWindow( window );
+
+            /*
              * Have the special callback executed:
              */
             window->Callbacks.Special( keypress, window->State.MouseX, window->State.MouseY );
@@ -1631,11 +1749,6 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         POINT mouse_pos ;
 
         /*
-         * Set the current window
-         */
-        fgSetWindow( window );
-
-        /*
          * Remember the current modifiers state. This is done here in order 
          * to make sure the VK_DELETE keyboard callback is executed properly.
          */
@@ -1682,7 +1795,10 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
              * The delete key should be treated as an ASCII keypress:
              */
             if( window->Callbacks.KeyboardUp != NULL )
+            {
+                fgSetWindow ( window ) ;
                 window->Callbacks.KeyboardUp( 127, window->State.MouseX, window->State.MouseY );
+            }
 
             break ;
           default:
@@ -1699,7 +1815,14 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
                 wParam=code[ 0 ];
 
               if( window->Callbacks.KeyboardUp != NULL )
+              {
+                /*
+                 * Set the current window
+                 */
+                fgSetWindow( window );
+
                 window->Callbacks.KeyboardUp( (char)wParam, window->State.MouseX, window->State.MouseY );
+              }
             }
         }
 
@@ -1709,6 +1832,11 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         if( (keypress != -1) && (window->Callbacks.SpecialUp != NULL) )
         {
             /*
+             * Set the current window
+             */
+            fgSetWindow( window );
+
+            /*
              * Have the special callback executed:
              */
             window->Callbacks.SpecialUp( keypress, window->State.MouseX, window->State.MouseY );
@@ -1736,6 +1864,11 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         if( window->Callbacks.Keyboard != NULL )
         {
             /*
+             * Set the current window
+             */
+            fgSetWindow( window );
+
+            /*
              * Remember the current modifiers state
              */
             window->State.Modifiers = 
@@ -1758,7 +1891,14 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
 
     case WM_CAPTURECHANGED :  /* User has finished resizing the window, force a redraw */
       if ( window->Callbacks.Display )
+      {
+        /*
+         * Set the current window
+         */
+        fgSetWindow( window );
+
         window->Callbacks.Display () ;
+      }
 
 /*      lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ) ; */
       break ;