Removed some remaining glib depandancy.
[freeglut] / src / freeglut_main.c
index 4df16bf..fafb503 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 );
@@ -383,67 +399,6 @@ void fgWarning( const char *fmt, ... )
     va_end( ap );
 }
 
-/*
- * Clean up on exit
- */
-static void fgCleanUpGlutsMess( void ) 
-{
-  int i;
-
-  i = 0;
-
-  if ( fgStructure.Windows.First != NULL ) 
-  {
-    SFG_Window *win = fgStructure.Windows.First ;
-    glEnd();
-    glFinish();
-    glFlush();
-    while ( win != NULL )
-    {
-      SFG_Window *temp_win = win->Node.Next ;
-      fgDestroyWindow ( win, FALSE ) ;
-      win = temp_win ;
-    }
-  }
-
-#if 0
-  /* these are pointers to external handles */
-
-  __glutWindowListSize    = 0;
-  __glutStaleWindowList   = NULL;
-  __glutWindowList        = NULL;
-  __glutCurrentWindow     = NULL;
-
-  /* make sure we no longer have a GL context */
-
-  if ( wglGetCurrentContext() != NULL ) 
-  {
-    wglDeleteContext( wglGetCurrentContext() );
-  }
-
-  hInstance = GetModuleHandle(NULL);
-  UnregisterClass( classname, hInstance );
-
-  /* clean up allocated timer memory */
-
-  tList = __glutTimerList;
-  i = 0;
-
-  while ( __glutTimerList ) 
-  {
-    i++;
-    tList = __glutTimerList;
-    
-    if ( __glutTimerList )
-      __glutTimerList = __glutTimerList->next;
-
-    if ( tList )
-      free( tList );
-  }
-#endif
-}
-
-
 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
 
 /*
@@ -617,6 +572,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 +597,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 );
@@ -659,7 +624,12 @@ void FGAPIENTRY glutMainLoopEvent( void )
             /*
              * Let's make the window redraw as a result of the mouse motion.
              */
-            if ( window->IsMenu ) window->State.Redisplay = TRUE ;
+            window->State.Redisplay = TRUE ;
+
+            /*
+             * Since the window is a menu, make the parent window current
+             */
+            fgSetWindow ( window->ActiveMenu->ParentWindow ) ;
 
             break;
         }
@@ -680,7 +650,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
             /*
              * Set the current window
              */
-            fgStructure.Window = window ;
+            fgSetWindow ( window ) ;
 
             /*
              * Yup. Have it executed immediately
@@ -698,7 +668,7 @@ void FGAPIENTRY glutMainLoopEvent( void )
             /*
              * Set the current window
              */
-            fgStructure.Window = window ;
+            fgSetWindow ( window ) ;
 
             /*
              * That's right, and there is a passive callback, too.
@@ -762,12 +732,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->ActiveMenu->ParentWindow ) ;
+            fgDeactivateMenu ( parent_window ) ;
 
             /* Restore the current window and menu */
             fgSetWindow ( save_window ) ;
@@ -778,6 +749,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
             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 ;
+
           break ;
         }
 
@@ -787,6 +763,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
         if ( ( window->Menu[ button ] != NULL ) && ( pressed == TRUE ) )
         {
           /*
+           * Let's make the window redraw as a result of the mouse click.
+           */
+          window->State.Redisplay = TRUE ;
+
+          /*
            * Set the current window
            */
           fgSetWindow( window );
@@ -802,13 +783,13 @@ void FGAPIENTRY glutMainLoopEvent( void )
         /*
          * Check if there is a mouse callback hooked to the window
          */
-        if( fgStructure.Window->Callbacks.Mouse == NULL )
+        if( window->Callbacks.Mouse == NULL )
           break;
 
         /*
          * Set the current window
          */
-        fgSetWindow( window );
+        fgSetWindow ( window );
 
         /*
          * Remember the current modifiers state
@@ -877,11 +858,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 )
@@ -892,6 +868,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
             if( keyboard_cb != NULL )
             {
               /*
+               * Set the current window
+               */
+              fgSetWindow( window );
+
+              /*
                * Remember the current modifiers state
                */
               modifiers = 0;
@@ -966,6 +947,11 @@ void FGAPIENTRY glutMainLoopEvent( void )
             if( (special_cb != NULL) && (special != -1) )
             {
               /*
+               * Set the current window
+               */
+              fgSetWindow( window );
+
+              /*
                * Remember the current modifiers state
                */
               modifiers = 0;
@@ -1004,9 +990,8 @@ void FGAPIENTRY glutMainLoopEvent( void )
 
     /*
      * No messages in the queue, which means we are idling...
-     * Don't call this if there is a menu active
      */
-    if( ( fgState.IdleCallback != NULL ) && ( fgState.ActiveMenus == 0 ) )
+    if( fgState.IdleCallback != NULL )
         fgState.IdleCallback();
 
     /*
@@ -1051,9 +1036,8 @@ void FGAPIENTRY glutMainLoopEvent( void )
 
     /*
      * No messages in the queue, which means we are idling...
-     * Don't call this if there is a menu active
      */
-    if( ( fgState.IdleCallback != NULL ) && ( fgState.ActiveMenus == 0 ) )
+    if( fgState.IdleCallback != NULL )
       fgState.IdleCallback();
 
     /*
@@ -1076,7 +1060,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
 
   /*
@@ -1096,9 +1080,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
 
@@ -1121,23 +1119,21 @@ void FGAPIENTRY glutMainLoop( void )
       fgState.ExecState = GLUT_EXEC_STATE_STOP ;
   }
 
+  {
+    fgExecutionState execState = fgState.ActionOnWindowClose;
 
-  /*
-   * If we got here by the user closing a window or by the application closing down, there may still be windows open.
-   */
-  fgCleanUpGlutsMess () ;
-
-  /*
-   * Check whether we return to the calling program or simply exit
-   */
-  if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
-    exit ( 0 ) ;
+    /*
+     * When this loop terminates, destroy the display, state and structure
+     * of a freeglut session, so that another glutInit() call can happen
+     */
+    fgDeinitialize();
 
-  /*
-   * When this loop terminates, destroy the display, state and structure
-   * of a freeglut session, so that another glutInit() call can happen
-   */
-  fgDeinitialize();
+    /*
+     * Check whether we return to the calling program or simply exit
+     */
+    if ( execState == GLUT_ACTION_EXIT )
+      exit ( 0 ) ;
+  }
 }
 
 /*
@@ -1343,7 +1339,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 ;
@@ -1385,7 +1381,12 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
             /*
              * Let's make the window redraw as a result of the mouse motion.
              */
-            if ( window->IsMenu ) window->State.Redisplay = TRUE ;
+            window->State.Redisplay = TRUE ;
+
+            /*
+             * Since the window is a menu, make the parent window current
+             */
+            fgSetWindow ( window->ActiveMenu->ParentWindow ) ;
 
             break;
         }
@@ -1506,12 +1507,13 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
             /* 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->ActiveMenu->ParentWindow ) ;
+            fgDeactivateMenu ( parent_window ) ;
 
             /* Restore the current window and menu */
             fgSetWindow ( save_window ) ;
@@ -1522,6 +1524,11 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
             if ( pressed == TRUE ) fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
           }
 
+          /*
+           * Let's make the window redraw as a result of the mouse click and menu activity.
+           */
+          if ( ! window->IsMenu ) window->State.Redisplay = TRUE ;
+
           break ;
         }
 
@@ -1531,6 +1538,11 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         if ( ( window->Menu[ button ] != NULL ) && ( pressed == TRUE ) )
         {
             /*
+             * Let's make the window redraw as a result of the mouse click.
+             */
+            window->State.Redisplay = TRUE ;
+
+            /*
              * Set the current window
              */
             fgSetWindow( window );
@@ -1546,13 +1558,13 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         /*
          * Check if there is a mouse callback hooked to the window
          */
-        if( fgStructure.Window->Callbacks.Mouse == NULL )
+        if( window->Callbacks.Mouse == NULL )
             break;
 
         /*
          * Set the current window
          */
-        fgSetWindow( window );
+        fgSetWindow ( window );
 
         /*
          * Remember the current modifiers state.
@@ -1565,7 +1577,7 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPara
         /*
          * Finally execute the mouse callback
          */
-        fgStructure.Window->Callbacks.Mouse(
+        window->Callbacks.Mouse(
             button,
             pressed == TRUE ? GLUT_DOWN : GLUT_UP,
             window->State.MouseX,
@@ -1592,11 +1604,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.
          */
@@ -1644,7 +1651,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 );
+                }
         }
 
         /*
@@ -1653,6 +1663,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 );
@@ -1672,11 +1687,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.
          */
@@ -1723,7 +1733,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:
@@ -1740,7 +1753,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 );
+              }
             }
         }
 
@@ -1750,6 +1770,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 );
@@ -1777,6 +1802,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 = 
@@ -1799,7 +1829,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 ;