//-------------------------------------------------------------------------------------------------//
void OSXCarbonWindow::destroy(void)
{
    if(!mCreated)
        return;
    
	if(mIsFullScreen)
    {
        // Handle fullscreen destruction
		destroyCGLFullscreen();
    }
    else
    {
        // Handle windowed destruction
        
        // Destroy the Ogre context
        delete mContext;
        
        if(!mIsExternal)
        {
            // Remove the window from the Window listener
            WindowEventUtilities::_removeRenderWindow( this );
            
            // Remove our event handler
            if(mEventHandlerRef)
                RemoveEventHandler(mEventHandlerRef);        
        }
    }

	mActive = false;
    mClosed = true;
    mCreated = false;
}
    //-------------------------------------------------------------------------------------------------//
    void OSXCarbonWindow::destroy(void)
    {
        if(!mCreated)
            return;

        if(mIsFullScreen)
        {
            // Handle fullscreen destruction
            destroyCGLFullscreen();
        }
        else
        {
            // Handle windowed destruction
            
            // Destroy the Ogre context
            if(mCGLContext)
            {
                OGRE_DELETE mCGLContext;
                mCGLContext = NULL;
            }
            
            if(mCarbonContext)
            {
                OGRE_DELETE mCarbonContext;
                mCarbonContext = NULL;
            }

            if(mContext)
            {
                // mContext is a reference to either the AGL or CGL context, already deleted.
                // Just clear the variable
                mContext = NULL;
            }

            if(!mIsExternal)
            {
                // Remove the window from the Window listener
                WindowEventUtilities::_removeRenderWindow( this );
                
                // Remove our event handler
                if(mEventHandlerRef)
                    RemoveEventHandler(mEventHandlerRef);        
            }

            if(mAGLContext)
                aglDestroyContext(mAGLContext);
            
            if(mWindow)
                DisposeWindow(mWindow);
        }

        mActive = false;
        mClosed = true;
        mCreated = false;
    }
    void OSXCarbonWindow::setFullscreen(bool fullScreen, unsigned int width, unsigned int height)
    {
        if (mIsFullScreen != fullScreen || width != mWidth || height != mHeight)
        {
            // Set the full screen flag
            mIsFullScreen = fullScreen;

            createAGLContext(mFSAA, mColourDepth);

            if (mIsFullScreen)
            {
                GLRenderSystem *rs = static_cast<GLRenderSystem*>(Root::getSingleton().getRenderSystem());

                CGLContextObj share = NULL;
                aglGetCGLContext(mAGLContext, (void**)&share);

                // Create the CGL context object if it doesn't already exist, sharing the AGL context.
                if(!mCGLContext)
                {
                    void *cglPixFormat;
                    aglGetCGLPixelFormat(mAGLPixelFormat, (void **)&cglPixFormat);
                    mCGLContext = OGRE_NEW OSXCGLContext(mCGLContextObj, (CGLPixelFormatObj) cglPixFormat);
                }

                // Create the context, keeping the current colour depth and FSAA settings
                createCGLFullscreen(width, height, getColourDepth(), getFSAA(), share);
                rs->_switchContext(mContext);

                // Hide the Carbon window
                HideWindow(mWindow);

                // And tell the rendersystem to stop rendering to it too
                WindowEventUtilities::_removeRenderWindow(this);
            }
            else
            {
                // Create a new AGL context and pixel format if necessary
                createAGLContext(mFSAA, mColourDepth);

                // Create a window if we haven't already, existence check is done within the functions
                if(!mWindow)
                {
                    if(mIsExternal)
                        createWindowFromExternal(mView);
                    else
                        createNewWindow(width, height, mWindowTitle);
                }

                // Destroy the current CGL context, we will create a new one when/if we go back to full screen
                destroyCGLFullscreen();

                // Set the drawable, and current context
                // If you do this last, there is a moment before the rendering window pops-up
                #if defined(MAC_OS_X_VERSION_10_4) && MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
                    aglSetDrawable(mAGLContext, GetWindowPort(mWindow));
                #else
                    aglSetWindowRef(mAGLContext, mWindow);
                #endif
                aglSetCurrentContext(mAGLContext);

                if(!mCarbonContext)
                {
                    mCarbonContext = OGRE_NEW OSXCarbonContext(mAGLContext, mAGLPixelFormat);
                }
                
                GLRenderSystem *rs = static_cast<GLRenderSystem*>(Root::getSingleton().getRenderSystem());
                mContext = mCarbonContext;
                rs->_switchContext(mContext);

                WindowEventUtilities::_addRenderWindow(this);

                ShowWindow(mWindow);
                SelectWindow(mWindow);
                RepositionWindow(mWindow, NULL, kWindowCenterOnMainScreen);
            }
            mWidth = width;
            mHeight = height;
        }
    }