From a23dee8a62e2887fbcda56aac9116c851080b0d0 Mon Sep 17 00:00:00 2001
From: Phil Rosenberg <p.d.rosenberg@gmail.com>
Date: Mon, 19 Dec 2016 11:13:36 +0000
Subject: [PATCH 1/4] Revert "wxwidgets binding: modification of workaround fix
 for delayed OnCreate event"

This reverts commit 786ff045d1b28db848222e66e47b6349d4aabb52.

This workaround works in this specific case, but we need to do something
more general.
---
 bindings/wxwidgets/wxPLplotwindow.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bindings/wxwidgets/wxPLplotwindow.h b/bindings/wxwidgets/wxPLplotwindow.h
index 70d5b66..9214828 100644
--- a/bindings/wxwidgets/wxPLplotwindow.h
+++ b/bindings/wxwidgets/wxPLplotwindow.h
@@ -122,7 +122,7 @@ bool wxPLplotwindow<WXWINDOW>::Show(bool show)
 {
   wxLogDebug("wxPLplotwindow::Show");
   CreateStream();
-  return WXWINDOW::Show(show);
+  WXWINDOW::Show(show);
 
 }
 
-- 
2.8.3


From 22ef2dc57a8e0509d8a7a20529a48fcccb53631a Mon Sep 17 00:00:00 2001
From: Phil Rosenberg <p.d.rosenberg@gmail.com>
Date: Mon, 19 Dec 2016 11:14:54 +0000
Subject: [PATCH 2/4] Revert "wxwidgets binding: workaround fix for delayed
 OnCreate event"

This reverts commit e5b7485290a3d5d65b0be1b8037e56441b71d891.

This fix works for the specific example, but we need something more
general.
---
 bindings/wxwidgets/wxPLplotwindow.h | 86 ++++++++++++++-----------------------
 1 file changed, 33 insertions(+), 53 deletions(-)

diff --git a/bindings/wxwidgets/wxPLplotwindow.h b/bindings/wxwidgets/wxPLplotwindow.h
index 9214828..8571fe6 100644
--- a/bindings/wxwidgets/wxPLplotwindow.h
+++ b/bindings/wxwidgets/wxPLplotwindow.h
@@ -45,8 +45,6 @@ public:
     wxPLplotstream* GetStream()  { return m_created ? &m_stream : NULL; }                //!< Get pointer to wxPLplotstream of this widget.
     void setUseGraphicsContext( bool useGraphicsContext );
     void setCanvasColour( const wxColour &colour );
-    void CreateStream( void );
-    virtual bool Show(bool show = true);
 
 protected:
     virtual void OnPaint( wxPaintEvent& event );         //!< Paint event
@@ -114,56 +112,6 @@ wxPLplotwindow<WXWINDOW>::~wxPLplotwindow( void )
         delete m_gcDc;
 }
 
-//Implemented to handle cases (some linux builds with wxWidgets 3.0/3.1) 
-//where the OnCreate function is not called before a plot was made
-//with an already created stream 
-template<class WXWINDOW>
-bool wxPLplotwindow<WXWINDOW>::Show(bool show)
-{
-  wxLogDebug("wxPLplotwindow::Show");
-  CreateStream();
-  WXWINDOW::Show(show);
-
-}
-
-template<class WXWINDOW>
-void wxPLplotwindow<WXWINDOW>::CreateStream( void )
-{
-    wxLogDebug("wxPLplotwindow::CreateStream");
-    if ( !m_created )
-    {
-        //set the client size if requested
-        if ( m_initialSize != wxDefaultSize )
-            WXWINDOW::SetClientSize( m_initialSize );
-        //create the stream
-        int width  = WXWINDOW::GetClientSize().GetWidth();
-        int height = WXWINDOW::GetClientSize().GetHeight();
-        m_bitmap.Create( width, height );
-        if ( m_memoryDc )
-            delete m_memoryDc;
-        m_memoryDc = new wxMemoryDC;
-        m_memoryDc->SelectObject( m_bitmap );
-        wxDC * drawDc = m_memoryDc;
-#ifdef wxUSE_GRAPHICS_CONTEXT
-        if ( m_useGraphicsContext )
-        {
-            if ( m_gcDc )
-                delete m_gcDc;
-            m_gcDc = new wxGCDC( *m_memoryDc );
-            drawDc = m_gcDc;
-        }
-#endif
-        if ( !m_stream.IsValid() )
-            m_stream.Create( drawDc, width, height, wxPLPLOT_DRAW_TEXT );
-        else
-            m_stream.SetDC( drawDc );
-        drawDc->SetBackground( wxBrush( m_canvasColour ) );
-        drawDc->Clear();
-
-        m_created = true;
-        RenewPlot();
-    }
-}
 
 //! In the OnPaint Method we check if the Windows was resized (will be moved to OnSize() sometimes
 //  later), we also implement our own double buffering here (since the PLplot wxWidgets driver draws
@@ -239,7 +187,39 @@ template<class WXWINDOW>
 void wxPLplotwindow<WXWINDOW>::OnCreate( wxWindowCreateEvent &event )
 {
     wxLogDebug("wxPLplotwindow::OnCreate");
-    CreateStream();
+    if ( !m_created )
+    {
+        //set the client size if requested
+        if ( m_initialSize != wxDefaultSize )
+            WXWINDOW::SetClientSize( m_initialSize );
+        //create the stream
+        int width  = WXWINDOW::GetClientSize().GetWidth();
+        int height = WXWINDOW::GetClientSize().GetHeight();
+        m_bitmap.Create( width, height );
+        if ( m_memoryDc )
+            delete m_memoryDc;
+        m_memoryDc = new wxMemoryDC;
+        m_memoryDc->SelectObject( m_bitmap );
+        wxDC * drawDc = m_memoryDc;
+#ifdef wxUSE_GRAPHICS_CONTEXT
+        if ( m_useGraphicsContext )
+        {
+            if ( m_gcDc )
+                delete m_gcDc;
+            m_gcDc = new wxGCDC( *m_memoryDc );
+            drawDc = m_gcDc;
+        }
+#endif
+        if ( !m_stream.IsValid() )
+            m_stream.Create( drawDc, width, height, wxPLPLOT_DRAW_TEXT );
+        else
+            m_stream.SetDC( drawDc );
+        drawDc->SetBackground( wxBrush( m_canvasColour ) );
+        drawDc->Clear();
+
+        m_created = true;
+        RenewPlot();
+    }
 }
 
 //Capture Mouse events and pass the
-- 
2.8.3


From ebb19d30b5e63671780d02d66c50a6212e797f91 Mon Sep 17 00:00:00 2001
From: Phil Rosenberg <p.d.rosenberg@gmail.com>
Date: Mon, 19 Dec 2016 13:44:02 +0000
Subject: [PATCH 3/4] Added a function to check if the window is ready to
 accept plot commands.

This function calls GetStream() to get a pointer to the wxPLplotstream
and checks if this pointer is NULL as a method for checking if
everything is ready to accept plot commands. This method is then used
internally in a few locations to check if things are ready in OnMouse
and OnPaint. Performing these checks implies that it is possible for a
paint or mouse event to be generated before a create event. I'm not sure
if this is the case, but the wxWidgets docs are a bit vague so there is
no harm in using the checks.
---
 bindings/wxwidgets/wxPLplotwindow.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/bindings/wxwidgets/wxPLplotwindow.h b/bindings/wxwidgets/wxPLplotwindow.h
index 8571fe6..2fa1123 100644
--- a/bindings/wxwidgets/wxPLplotwindow.h
+++ b/bindings/wxwidgets/wxPLplotwindow.h
@@ -45,6 +45,7 @@ public:
     wxPLplotstream* GetStream()  { return m_created ? &m_stream : NULL; }                //!< Get pointer to wxPLplotstream of this widget.
     void setUseGraphicsContext( bool useGraphicsContext );
     void setCanvasColour( const wxColour &colour );
+    bool IsReady() { return GetStream() != NULL; }
 
 protected:
     virtual void OnPaint( wxPaintEvent& event );         //!< Paint event
@@ -153,10 +154,12 @@ void wxPLplotwindow<WXWINDOW>::OnPaint( wxPaintEvent &WXUNUSED( event ) )
             drawDc = m_gcDc;
         }
 #endif
-        m_stream.SetDC( drawDc );
+        if( IsReady() )
+            m_stream.SetDC( drawDc );
         drawDc->SetBackground( wxBrush( m_canvasColour ) );
         drawDc->Clear();
-        m_stream.SetSize( width, height );
+        if( IsReady() )
+            m_stream.SetSize( width, height );
     }
 
     paintDc.Blit( 0, 0, width, height, m_memoryDc, 0, 0 );
@@ -289,7 +292,8 @@ void wxPLplotwindow<WXWINDOW>::OnMouse( wxMouseEvent &event )
         graphicsIn.state |= PL_MASK_SCROLL;
     //Note I can't find a way to catch the windows key
 
-    m_stream.translatecursor( &graphicsIn );
+    if( IsReady() )
+        m_stream.translatecursor( &graphicsIn );
     this->OnLocate( graphicsIn );
 }
 
@@ -353,7 +357,7 @@ void wxPLplotwindow<WXWINDOW>::setUseGraphicsContext( bool useGraphicsContext )
     drawDc = &m_memoryDc;
     m_useGraphicsContext = false;
 #endif
-    if ( m_created )
+    if ( IsReady() )
     {
         m_stream.SetDC( drawDc );
         RenewPlot();
-- 
2.8.3


From f918e908031b374070e1289273735294550def82 Mon Sep 17 00:00:00 2001
From: Phil Rosenberg <p.d.rosenberg@gmail.com>
Date: Mon, 19 Dec 2016 13:50:04 +0000
Subject: [PATCH 4/4] Fix bug with plotting in wxPLplotDemo

On most systems it seems that when wxWindow::Create() is called that
triggers a create event, but within the Create() function the event loop
is checked and the create event is processed immediately and OnCreate()
is called setting up the wxPLplotstream. This means we can begin using
the wxPLplotstream immediately after the call to Create().

However on some systems the create event is not processed immediately.
This has been observed on some Linux systems but apparently
identical systems seem to have different behaviour. The cause of this is
not clear.

To deal with this we now check the wxPLplotwindow is ready and wait for
it if not, yielding to allow the create event to be processed. Another
option might be to catch idle events and each time check the window is
ready then do the plotting at this point. However this is a more complex
proceedure and it would partly obfuscate what should be a simple
example.
---
 examples/c++/wxPLplotDemo.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/examples/c++/wxPLplotDemo.cpp b/examples/c++/wxPLplotDemo.cpp
index c0ec049..e80ddfd 100644
--- a/examples/c++/wxPLplotDemo.cpp
+++ b/examples/c++/wxPLplotDemo.cpp
@@ -155,12 +155,12 @@ bool MyApp::OnInit()
 template< class WXWINDOW >
 void Plot( wxPLplotwindow<WXWINDOW> *plotwindow )
 {
-    wxPLplotstream* pls = plotwindow->GetStream();
-    if (pls == NULL)
+	while (!plotwindow->IsReady())
     {
-     wxLogDebug("pls NULL");
-     return;
+		wxLogDebug("Plot() Yielding");
+		wxGetApp().Yield();
     }
+    wxPLplotstream* pls = plotwindow->GetStream();
     wxLogDebug("Plot()");
     assert(pls);
 
-- 
2.8.3

