<commit_before>#pragma once

#include <osgViewer/Viewer>
#include <osgViewer/GraphicsWindow>
#include "wx/glcanvas.h"


class LocalGLContext;
class wxGLCanvas;

#ifndef __WXMAC__
class LocalGLContext : public wxGLContext
{
public:
    LocalGLContext(wxGLCanvas *win, const wxGLContext* other=NULL /* for sharing display lists */ )
	: wxGLContext(win, other)
	{
	}

	void ReleaseContext(const wxGLCanvas& win)
	{
#if defined(__WXMSW__)
		wglMakeCurrent((HDC) win.GetHDC(), NULL);
#endif
	}
};
#endif

class GraphicsWindowWX : public osgViewer::GraphicsWindow
{
public:
	GraphicsWindowWX(wxGLCanvas* pCanvas)
	{
		m_pCanvas = pCanvas;
#ifndef __WXMAC__
		m_pGLContext = NULL;
#endif
		m_bValid = true;
		m_bIsRealized = false;
	}

	~GraphicsWindowWX()
	{
#ifndef __WXMAC__
		delete m_pGLContext;
#endif
	}

	void CloseOsgContext()
	{
		getEventQueue()->closeWindow(0);
		// Force handling of event before the idle loop can call frame();
		dynamic_cast<osgViewer::View*>(getCameras().front()->getView())->getViewerBase()->eventTraversal();
	}

    bool makeCurrentImplementation()
	{
#ifdef __WXMAC__
		m_pCanvas->SetCurrent();
#else
		m_pCanvas->SetCurrent(*m_pGLContext);
#endif
		return true;
	}

    void swapBuffersImplementation()
	{
		m_pCanvas->SwapBuffers();
	}

    virtual bool releaseContextImplementation()
	{
#ifndef __WXMAC__
		m_pGLContext->ReleaseContext(*m_pCanvas);
#endif
		return true;
	}
	
    virtual void closeImplementation()
	{
		m_bValid = false;
	}


    virtual bool realizeImplementation()
	{
#ifndef __WXMAC__
		m_pGLContext = new LocalGLContext(m_pCanvas);
#endif
		wxPoint pos = m_pCanvas->GetPosition();
		wxSize  size = m_pCanvas->GetSize();

		// Set up traits to match the canvas
		_traits = new GraphicsContext::Traits;
		_traits->x = pos.x;
		_traits->y = pos.y;
		_traits->width = size.x;
		_traits->height = size.y;

		// Set up a new context ID - I don't think we make use of this at the moment
		setState( new osg::State );
		getState()->setGraphicsContext(this);
		getState()->setContextID( osg::GraphicsContext::createNewContextID() );
		m_bIsRealized = true;
		return true;
	}


	virtual bool valid() const<commit_msg>Moved the code that sets up the state set up into the constructor so that it works on OSG 3.0<commit_after>#pragma once

#include <osgViewer/Viewer>
#include <osgViewer/GraphicsWindow>
#include "wx/glcanvas.h"


class LocalGLContext;
class wxGLCanvas;

#ifndef __WXMAC__
class LocalGLContext : public wxGLContext
{
public:
    LocalGLContext(wxGLCanvas *win, const wxGLContext* other=NULL /* for sharing display lists */ )
	: wxGLContext(win, other)
	{
	}

	void ReleaseContext(const wxGLCanvas& win)
	{
#if defined(__WXMSW__)
		wglMakeCurrent((HDC) win.GetHDC(), NULL);
#endif
	}
};
#endif

class GraphicsWindowWX : public osgViewer::GraphicsWindow
{
public:
	GraphicsWindowWX(wxGLCanvas* pCanvas)
	{
		m_pCanvas = pCanvas;
#ifndef __WXMAC__
		m_pGLContext = NULL;
#endif
		m_bValid = true;
		m_bIsRealized = false;

		wxPoint pos = m_pCanvas->GetPosition();
		wxSize  size = m_pCanvas->GetSize();

		// Set up traits to match the canvas
		_traits = new GraphicsContext::Traits;
		_traits->x = pos.x;
		_traits->y = pos.y;
		_traits->width = size.x;
		_traits->height = size.y;

		// Set up a new context ID - I don't think we make use of this at the moment
		setState( new osg::State );
		getState()->setGraphicsContext(this);
        if (_traits.valid() && _traits->sharedContext)
        {
			// I left this code in just in case we want shared contexts in the future
			// they would need to be passed in and set up in the traits object above
            getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
            incrementContextIDUsageCount( getState()->getContextID() );
        }
        else
            getState()->setContextID( osg::GraphicsContext::createNewContextID() );
	}

	~GraphicsWindowWX()
	{
#ifndef __WXMAC__
		delete m_pGLContext;
#endif
	}

	void CloseOsgContext()
	{
		getEventQueue()->closeWindow(0);
		// Force handling of event before the idle loop can call frame();
		dynamic_cast<osgViewer::View*>(getCameras().front()->getView())->getViewerBase()->eventTraversal();
	}

    bool makeCurrentImplementation()
	{
#ifdef __WXMAC__
		m_pCanvas->SetCurrent();
#else
		m_pCanvas->SetCurrent(*m_pGLContext);
#endif
		return true;
	}

    void swapBuffersImplementation()
	{
		m_pCanvas->SwapBuffers();
	}

    virtual bool releaseContextImplementation()
	{
#ifndef __WXMAC__
		m_pGLContext->ReleaseContext(*m_pCanvas);
#endif
		return true;
	}
	
    virtual void closeImplementation()
	{
		m_bValid = false;
	}


    virtual bool realizeImplementation()
	{
#ifndef __WXMAC__
		m_pGLContext = new LocalGLContext(m_pCanvas);
#endif
		m_bIsRealized = true;
		return true;
	}


	virtual bool valid() const
{'fraction_non_alphanumeric': 0.11581569115815692, 'fraction_numerical': 0.0008302200083022001, 'mean_word_length': 4.226681127982647, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5431319', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1649, 'n_words': 398}