Hi Alan

Success

The solution is to override the Show() function of the window, that is called in the demo.
So no need to modify the demo at all with a custom function call.


in wxPLplotwindow.h I just added this function

template<class WXWINDOW>
bool wxPLplotwindow<WXWINDOW>::Show(bool show)
{
  wxLogDebug("wxPLplotwindow::Show");
  CreateStream();
  WXWINDOW::Show(show);

}

where
CreateStream();
is a new internal function of the class that contains the code that is now in OnCreate()

the 2 files are attached
they contain a couple of debug messages

this is the sequence I get

11:13:52: Debug: wxPLplotwindow
11:13:52: Debug: frame->Create
11:13:52: Debug: wxPLplotwindow::Show
11:13:52: Debug: wxPLplotwindow::CreateStream
11:13:52: Debug: wxPLplotwindow::RenewPlot
11:13:52: Debug: Plot()
11:13:52: Debug: wxPLplotwindow::RenewPlot
11:13:52: Debug: wxPLplotwindow::RenewPlot
11:13:52: Debug: wxPLplotwindow::OnCreate
11:13:52: Debug: wxPLplotwindow::CreateStream
11:13:52: Debug: wxPLplotwindow::OnErase


as you can see the stream is NOT NULL when we get at
11:13:52: Debug: Plot()

-Pedro



On 2016-12-15 10:22, Pedro Vicente wrote:
Hi Alan

I installed the latest from the debian site, here

https://www.debian.org/distrib/

it's not surprising that I got the same results in ubuntu because I
used the same package

sudo apt-get install libwxgtk3.0-dev



On 2016-12-15 04:32, Alan W. Irwin wrote:
On 2016-12-15 01:11-0500 Pedro Vicente wrote:

Hi Alan

I just installed the latest debian on VirtualBox, and I get the same
errors.
the results are attached, same thing that I did for ubuntu.

Hi Pedro:

Just for the record, what version of Debian?

Currently Jessie = stable (a largely frozen release from two years
ago), Stretch = testing (a rolling release not as stable as stable),
and Sid = unstable (another rolling release not as stable as
testing).
Stretch has turned or will turn soon into a frozen distribution
similar to Jessie but two years more modern and be designated stable
likely sometime in 2017. Jessie will be redesignated as oldstable at
that Debian release epoch, and Sid is always going to be unstable.
:-)

So if you installed Jessie, I would be officially amazed you cannot
replicate my good results there.  But if Stretch or Sid, then those
are quite different, and all bets are off.

it seems that something on your debian is preventing this error,
but you should be able to verify this by just installing a new
debian (or any linux , it seems) on VirtualBox.

Half my computer memory fried itself a year or so ago so my computer
(already 9 years old but with ASUS motherboard, Intel CPU's, new
disks, and new power supply still doing pretty well) is a little
short
of memory. Thus, I don't plan to look at VirtualBox at the moment,
but
when (if?) one of my motherboard, CPU's, or remaining RAM die so that
I really do have to replace all three, I intend to buy a huge amount
of memory for the new system so I can play with VirtualBox with ease.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and
Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state
implementation for stellar interiors (freeeos.sf.net); the Time
Ephemerides project (timeephem.sf.net); PLplot scientific plotting
software package (plplot.sf.net); the libLASi project
(unifont.org/lasi); the Loads of Linux Links project (loll.sf.net);
and the Linux Brochure Project (lbproject.sf.net).
__________________________

Linux-powered Science
__________________________

--
Pedro Vicente
pedro.vice...@space-research.org
http://www.space-research.org/


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Plplot-devel mailing list
Plplot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/plplot-devel

--
Pedro Vicente
pedro.vice...@space-research.org
http://www.space-research.org/
// Copyright (C) 2005  Werner Smekal
//
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#ifdef __WXMAC__
        #include <Carbon/Carbon.h>
extern "C" { void CPSEnableForegroundOperation( ProcessSerialNumber* psn ); }
#endif

#include "wxPLplotwindow.h"
#include <cmath>

#define MAX( a, b )    ( ( a ) < ( b ) ? ( b ) : ( a ) )
#define MIN( a, b )    ( ( a ) < ( b ) ? ( a ) : ( b ) )

// Application icon as XPM
// This free icon was taken from http://2pt3.com/news/twotone-icons-for-free/
static const char            *graph[] = {
// columns rows colors chars-per-pixel
    "16 16 4 2",
    "   c black",
    ".  c #BA1825",
    "X  c gray100",
    "UX c None",
// pixels
    "UX. . . . . . . . . . . . . . UX",
    ". . . . . . . . . . . . . . . . ",
    ". . . . . . . . . . . . . . . . ",
    ". . . . . . . . . . . X X . . . ",
    ". . . . . . . . . . . X X . . . ",
    ". . . . . . . . . . . X X . . . ",
    ". . . . . X X . . . . X X . . . ",
    ". . . . . X X . . . . X X . . . ",
    ". . . . . X X . X X . X X . . . ",
    ". . . . . X X . X X . X X . . . ",
    ". . . . . X X . X X . X X . . . ",
    ". . . . . X X . X X . X X . . . ",
    ". . . X X X X X X X X X X . . . ",
    ". . . . . . . . . . . . . . . . ",
    ". . . . . . . . . . . . . . . . ",
    "UX. . . . . . . . . . . . . . UX"
};

class wxPlDemoFrame : public wxPLplotwindow<wxFrame>
{
private:
    virtual void OnLocate( const PLGraphicsIn &graphicsIn );
};

void wxPlDemoFrame::OnLocate( const PLGraphicsIn &graphicsIn )
{
    if ( graphicsIn.button == 0 )
        return;         //Do nothing for motion, only respond to clicks

    wxString message;

    if ( ( graphicsIn.state & PL_MASK_SHIFT ) != 0 )
        message << "Shift-";
    if ( ( graphicsIn.state & PL_MASK_CAPS ) != 0 )
        message << "Caps Lock-";
    if ( ( graphicsIn.state & PL_MASK_CONTROL ) != 0 )
        message << "Ctrl-";
    if ( ( graphicsIn.state & PL_MASK_ALT ) != 0 )
        message << "Alt-";
    if ( ( graphicsIn.state & PL_MASK_NUM ) != 0 )
        message << "Num Lock-";
    if ( ( graphicsIn.state & PL_MASK_ALTGR ) != 0 )
        message << "Alt Gr-";
    if ( ( graphicsIn.state & PL_MASK_WIN ) != 0 )
        message << "Win-";
    if ( ( graphicsIn.state & PL_MASK_SCROLL ) != 0 )
        message << "Scroll Lock-";

    if ( graphicsIn.button == 1 )
        message << "Left click.\n";
    else if ( graphicsIn.button == 2 )
        message << "Middle click.\n";
    else if ( graphicsIn.button == 3 )
        message << "Right click.\n";
    message << "Pixels: x = " << graphicsIn.pX << " y = " << graphicsIn.pY << 
".\n";
    if ( graphicsIn.subwindow >= 0 )
    {
        message << "World: x = " << graphicsIn.wX << " y = " << graphicsIn.wY 
<< ".\n";
        message << "Window = " << graphicsIn.subwindow << ".\n";
    }
    else
    {
        message << "Point is not in a Window.\n";
    }
    wxMessageBox( message, "Mouse capture demo" );
}

template< class WXWINDOW >
void Plot( wxPLplotwindow<WXWINDOW> *plotwindow );

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

IMPLEMENT_APP( MyApp )

//! This method is called right at the beginning and opens a frame for us.
//
bool MyApp::OnInit()
{
#ifdef __WXMAC__
    // this hack enables to have a GUI on Mac OSX even if the
    // program was called from the command line (and isn't a bundle)
    ProcessSerialNumber psn;

    GetCurrentProcess( &psn );
    CPSEnableForegroundOperation( &psn );
    SetFrontProcess( &psn );
#endif

    wxPLplotwindow<wxFrame> *frame = new wxPlDemoFrame();
    frame->Create( NULL, wxID_ANY, wxT( "wxPLplotDemo" ) );
    wxLogDebug("frame->Create");
    frame->SetIcon( wxIcon( graph ) );
    frame->Show();
    Plot( frame );

    return true;
}

template< class WXWINDOW >
void Plot( wxPLplotwindow<WXWINDOW> *plotwindow )
{
    wxPLplotstream* pls = plotwindow->GetStream();
    
    if (pls == NULL)
    {
     wxLogDebug("pls NULL");
     return;
    }
    wxLogDebug("Plot()");
    assert(pls);

    const size_t  np = 500;
    PLFLT         x[np], y[np];
    PLFLT         xmin, xmax;
    PLFLT         ymin = 1e30, ymax = 1e-30;

    xmin = -2.0;
    xmax = 10.0;
    for ( size_t i = 0; i < np; i++ )
    {
        x[i] = ( xmax - xmin ) * i / np + xmin;
        y[i] = 1.0;
        if ( x[i] != 0.0 )
            y[i] = sin( x[i] ) / x[i];
        ymin = MIN( ymin, y[i] );
        ymax = MAX( ymax, y[i] );
    }

    pls->adv( 0 );
    pls->col0( 1 );
    pls->env( xmin, xmax, ymin, ymax, 0, 0 );
    pls->col0( 2 );
    pls->lab( "x", "y", "sin(x)/x" );

    pls->col0( 3 );
    pls->width( 2 );
    pls->line( np, x, y );

    plotwindow->RenewPlot();
}

//
//class MyPlotwindow : public wxPLplotwindow
//{
//public:
//    MyPlotwindow( wxFrame* frame, wxWindow* parent, wxWindowID id = -1, const 
wxPoint& pos = wxDefaultPosition,
//                  const wxSize& size = wxDefaultSize, long style = 0,
//                  int pl_style = wxPLPLOT_NONE );
//
//    void OnChar( wxKeyEvent& event );
//
//private:
//    wxFrame* mframe;
//};
//
//
//// Define a new frame type: this is going to be our main frame
//class MyFrame : public wxFrame
//{
//public:
//    MyFrame( const wxString& title );
//    void Plot();
//
//private:
//    void OnQuit( wxCommandEvent& event );
//    void OnAbout( wxCommandEvent& event );
//    void OnBackgroundColor( wxCommandEvent& event );
//
//private:
//    MyPlotwindow* plotwindow;
//    bool        bgcolor;
//    int         m_backend;
//
//    DECLARE_EVENT_TABLE()
//};
//
//
//--------------------------------------------------------------------------
//// constants
//--------------------------------------------------------------------------
//enum { wxPLplotDemo_Quit    = wxID_EXIT, wxPLplotDemo_About = wxID_ABOUT,
//       wxPLplotDemo_BGColor = 10000 };
//
//--------------------------------------------------------------------------
//// event tables and other macros for wxWidgets
//--------------------------------------------------------------------------
//BEGIN_EVENT_TABLE( MyFrame, wxFrame )
//EVT_MENU( wxPLplotDemo_Quit, MyFrame::OnQuit )
//EVT_MENU( wxPLplotDemo_About, MyFrame::OnAbout )
//EVT_MENU( wxPLplotDemo_BGColor, MyFrame::OnBackgroundColor )
//END_EVENT_TABLE()
//
//
//--------------------------------------------------------------------------
//// implementation
//--------------------------------------------------------------------------
//
//
//
//MyPlotwindow::MyPlotwindow( wxFrame* frame, wxWindow* parent, wxWindowID id, 
const wxPoint& pos,
//                            const wxSize& size, long style, int pl_style ) :
//    wxPLplotwindow( parent, id, pos, size, style, pl_style )
//{
//    mframe = frame;
//}
//
//
//void MyPlotwindow::OnChar( wxKeyEvent& event )
//{
//    int keycode = event.GetKeyCode();
//
//    if ( keycode == WXK_RETURN ||
//         keycode == WXK_SPACE ||
//         keycode == WXK_RIGHT ||
//         keycode == WXK_ESCAPE )
//        mframe->Close( true );
//    else
//        event.Skip();
//}
//
//
////! Constructor of our custom frame, where the Menu is created and a
////  a wxPLplotwindow is inserted into the frame. We plot some easy functions
////  just to show how it works. wxPLplotwindow takes care of all the setup
////  for the use of PLplot library.
////
//MyFrame::MyFrame( const wxString& title ) : wxFrame( NULL, wxID_ANY, title )
//{
//    bgcolor = false;
//
//    // add menu
//    wxMenu *fileMenu = new wxMenu;
//    fileMenu->Append( wxPLplotDemo_BGColor, _T( "&Change background 
color...\tAlt-C" ), _T( "Change background color" ) );
//    fileMenu->Append( wxPLplotDemo_About, _T( "&About...\tF1" ), _T( "Show 
about dialog" ) );
//    fileMenu->Append( wxPLplotDemo_Quit, _T( "E&xit\tAlt-X" ), _T( "Quit this 
program" ) );
//
//    wxMenuBar *menuBar = new wxMenuBar();
//    menuBar->Append( fileMenu, _T( "&File" ) );
//    SetMenuBar( menuBar );
//    SetIcon( wxIcon( graph ) );
//
//    // add the wxPLplot
//    wxPanel   * panel = new wxPanel( this );
//    wxBoxSizer* box   = new wxBoxSizer( wxVERTICAL );
//    plotwindow = new MyPlotwindow( this, panel, -1, wxDefaultPosition, 
wxDefaultSize, wxWANTS_CHARS,
//#if wxUSE_GRAPHICS_CONTEXT
//        wxPLPLOT_BACKEND_GC | wxPLPLOT_DRAW_TEXT );
//#else
//        wxPLPLOT_BACKEND_AGG | wxPLPLOT_DRAW_TEXT );
//#endif
//    plotwindow->Connect( wxEVT_CHAR, wxKeyEventHandler( MyPlotwindow::OnChar 
) );
//    box->Add( plotwindow, 1, wxALL | wxEXPAND, 0 );
//    panel->SetSizer( box );
//    SetSize( 640, 500 );      // set frame size
//    SetSizeHints( 220, 150 ); // set minimum frame size
//
//    wxString m_title = title;
//    switch ( plotwindow->getBackend() )
//    {
//    case wxPLPLOT_BACKEND_DC:
//        m_title += wxT( " (basic)" );
//        break;
//    case wxPLPLOT_BACKEND_GC:
//        m_title += wxT( " (wxGC)" );
//        break;
//    case wxPLPLOT_BACKEND_AGG:
//        m_title += wxT( " (AGG)" );
//        break;
//    default:
//        break;
//    }
//    SetTitle( m_title );
//
//    Plot();
//}
//
//void MyFrame::Plot()
//{
//    wxPLplotstream* pls = plotwindow->GetStream();
//
//    const size_t  np = 500;
//    PLFLT         x[np], y[np];
//    PLFLT         xmin, xmax;
//    PLFLT         ymin = 1e30, ymax = 1e-30;
//
//    xmin = -2.0;
//    xmax = 10.0;
//    for ( size_t i = 0; i < np; i++ )
//    {
//        x[i] = ( xmax - xmin ) * i / np + xmin;
//        y[i] = 1.0;
//        if ( x[i] != 0.0 )
//            y[i] = sin( x[i] ) / x[i];
//        ymin = MIN( ymin, y[i] );
//        ymax = MAX( ymax, y[i] );
//    }
//
//    pls->adv( 0 );
//    if ( bgcolor )
//    {
//        pls->scol0( 0, 255, 255, 255 );
//        pls->scol0( 15, 0, 0, 0 );
//    }
//    else
//    {
//        pls->scol0( 15, 255, 255, 255 );
//        pls->scol0( 0, 0, 0, 0 );
//    }
//    pls->col0( 1 );
//    pls->env( xmin, xmax, ymin, ymax, 0, 0 );
//    pls->col0( 2 );
//    pls->lab( "x", "y", "sin(x)/x" );
//
//    pls->col0( 3 );
//    pls->width( 2 );
//    pls->line( np, x, y );
//
//    plotwindow->RenewPlot();
//}
//
//
//void MyFrame::OnQuit( wxCommandEvent& WXUNUSED( event ) )
//{
//    Close( true );
//}
//
//
//void MyFrame::OnBackgroundColor( wxCommandEvent& WXUNUSED( event ) )
//{
//    bgcolor = !bgcolor;
//    Plot();
//}
//
//
////! Show information if Menu entry About was choosen.
////
//void MyFrame::OnAbout( wxCommandEvent& WXUNUSED( event ) )
//{
//    wxMessageBox( _T( "This is the About dialog of the wxPLplot demo.\n" ), 
_T( "About wxPLplot" ),
//        wxOK | wxICON_INFORMATION, this );
//}
// Copyright (C) 2015  Phil Rosenberg
// Copyright (C) 2005  Werner Smekal
//
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//

#if !defined ( WXPLPLOTWINDOW_H__INCLUDED_ )
#define WXPLPLOTWINDOW_H__INCLUDED_

#include "plplot.h"
#include "wxPLplotstream.h"
#include <wx/window.h>
#include <wx/dcmemory.h>
#include <wx/dcclient.h>
#include <wx/dcgraph.h>
#include <wx/dcbuffer.h>

// A plplot wxWindow template. To create an actual plplot wxWindow use
// the type of wxWindow you wish to inherit from at the template parameter
// For example to create a plplot wxFrame create a wxPLplotwindow<wxFrame>
// then call the base wxWindow's Create method to initialise.
template <class WXWINDOW>
class wxPLplotwindow : public WXWINDOW
{
public:
    wxPLplotwindow( bool useGraphicsContext = true, wxSize clientSize = 
wxDefaultSize ); //!< Constructor.
    virtual ~wxPLplotwindow( void );                                            
         //!< Destructor.

    void RenewPlot( void );                                                     
         //!< Redo plot.
    bool SavePlot( const wxString& driver, const wxString& filename );          
         //!< Save plot using a different driver.
    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
    virtual void OnSize( wxSizeEvent & event );          //!< Size event
    virtual void OnErase( wxEraseEvent &event );         //!< Background erase 
event
    virtual void OnCreate( wxWindowCreateEvent &event ); //!< Window created 
event
    void OnMouse( wxMouseEvent &event );                 //!< Mouse events
    wxPLplotstream m_stream;                             //!< The 
wxPLplotstream which belongs to this plot widget
    bool           m_created;                            //!< Flag to indicate 
the window has been Created

private:
    bool     m_useGraphicsContext;                       //!< Flag to indicate 
whether we should use a wxGCDC
    wxBitmap m_bitmap;
    // The memory dc and wrapping gc dc for drawing. Note we
    //use pointers and reallocate them whenever the bitmap is
    //resized because reusing either of these causes problems
    //for rendering on a wxGCDC - at least on Windows.
    wxMemoryDC *m_memoryDc;
    wxSize     m_initialSize;
#ifdef wxUSE_GRAPHICS_CONTEXT
    wxGCDC     *m_gcDc;
#endif
    wxColour   m_canvasColour;
    virtual void OnLocate( const PLGraphicsIn &graphicsIn ){}
};


//! Constructor initialises variables, creates the wxStream and connects
// methods with events. The WXWINDOW default constructor is used.
//
template<class WXWINDOW>
wxPLplotwindow<WXWINDOW>::wxPLplotwindow( bool useGraphicsContext, wxSize 
clientSize )
    : m_created( false ), m_initialSize( clientSize )

{
    wxLogDebug("wxPLplotwindow");
    m_memoryDc = NULL;
#ifdef wxUSE_GRAPHICS_CONTEXT
    m_gcDc = NULL;
#endif
    setUseGraphicsContext( useGraphicsContext );
    m_canvasColour = *wxBLACK;

    //We use connect instead of Bind for compatiblity with wxWidgets 2.8
    //but should move to bind in the future.
    WXWINDOW::Connect( wxEVT_SIZE, wxSizeEventHandler( 
wxPLplotwindow<WXWINDOW>::OnSize ) );
    WXWINDOW::Connect( wxEVT_PAINT, wxPaintEventHandler( 
wxPLplotwindow<WXWINDOW>::OnPaint ) );
    WXWINDOW::Connect( wxEVT_ERASE_BACKGROUND, wxEraseEventHandler( 
wxPLplotwindow<WXWINDOW>::OnErase ) );
    WXWINDOW::Connect( wxEVT_CREATE, wxWindowCreateEventHandler( 
wxPLplotwindow<WXWINDOW>::OnCreate ) );
    WXWINDOW::Connect( wxEVT_MOTION, wxMouseEventHandler( 
wxPLplotwindow<WXWINDOW>::OnMouse ) );
    WXWINDOW::Connect( wxEVT_LEFT_UP, wxMouseEventHandler( 
wxPLplotwindow<WXWINDOW>::OnMouse ) );
    WXWINDOW::Connect( wxEVT_MIDDLE_UP, wxMouseEventHandler( 
wxPLplotwindow<WXWINDOW>::OnMouse ) );
    WXWINDOW::Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( 
wxPLplotwindow<WXWINDOW>::OnMouse ) );
}


//! Destructor - delete the dc and gcdc if needed.
//
template<class WXWINDOW>
wxPLplotwindow<WXWINDOW>::~wxPLplotwindow( void )
{
    if ( m_memoryDc )
        delete m_memoryDc;
    if ( m_gcDc )
        delete m_gcDc;
}


template<class WXWINDOW>
bool wxPLplotwindow<WXWINDOW>::Show(bool show)
{
  wxLogDebug("wxPLplotwindow::Show");
  CreateStream();
  WXWINDOW::Show(show);

}

//! 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
//  into a wxMemoryDC)
//
template<class WXWINDOW>
void wxPLplotwindow<WXWINDOW>::OnPaint( wxPaintEvent &WXUNUSED( event ) )
{
    //Really this should be in the constructor, but it caused a segfault
    //on at least one system (CentOS with intel compiler and wxWidgets 2.8.12).
    //Moving it here after WXWINDOW::Create has been called stops this and
    //the call does nothing if the style is the same as previous calls so
    //should be safe to call here.
    //WXWINDOW::SetBackgroundStyle( wxBG_STYLE_CUSTOM );


    //wxAutoBufferedPaintDC dc( (WXWINDOW*)this );
    int       width  = WXWINDOW::GetClientSize().GetWidth();
    int       height = WXWINDOW::GetClientSize().GetHeight();

    wxPaintDC paintDc( this );

    //resize the plot if needed
    bool needResize = width != m_bitmap.GetWidth() || height != 
m_bitmap.GetHeight();
    if ( needResize )
    {
        m_bitmap.Create( width, height, 32 );
        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
        m_stream.SetDC( drawDc );
        drawDc->SetBackground( wxBrush( m_canvasColour ) );
        drawDc->Clear();
        m_stream.SetSize( width, height );
    }

    paintDc.Blit( 0, 0, width, height, m_memoryDc, 0, 0 );
}

//! This is called when the plot is resized
//
template<class WXWINDOW>
void wxPLplotwindow<WXWINDOW>::OnSize( wxSizeEvent& WXUNUSED( event ) )
{
    //Invalidate the whole window so it is all redrawn, otherwise only
    //newly exposed parts of the window get redrawn
    RenewPlot();
}

//! This is called before each paint event
//
template<class WXWINDOW>
void wxPLplotwindow<WXWINDOW>::OnErase( wxEraseEvent& WXUNUSED( event ) )
{
    wxLogDebug("wxPLplotwindow::OnErase");
    //Do nothing. This stops screen flicker.
}

//! This is called when the widow is created i.e. after WXWINDOW::Create
//  has been called. We note that this has been called to avoid attempting
//  to redraw a plot on a window that hasn't been created yet.
template<class WXWINDOW>
void wxPLplotwindow<WXWINDOW>::OnCreate( wxWindowCreateEvent &event )
{
    wxLogDebug("wxPLplotwindow::OnCreate");
    CreateStream();
    
}

//Capture Mouse events and pass the
template<class WXWINDOW>
void wxPLplotwindow<WXWINDOW>::OnMouse( wxMouseEvent &event )
{
    PLGraphicsIn graphicsIn;
    wxPoint      cursorPosition = event.GetPosition();
    wxSize       clientSize     = WXWINDOW::GetClientSize();

    graphicsIn.pX        = cursorPosition.x;
    graphicsIn.pY        = cursorPosition.y;
    graphicsIn.dX        = PLFLT( cursorPosition.x + 0.5 ) / PLFLT( 
clientSize.GetWidth() );
    graphicsIn.dY        = 1.0 - PLFLT( cursorPosition.y + 0.5 ) / PLFLT( 
clientSize.GetHeight() );
    graphicsIn.keysym    = 0x20;
    graphicsIn.state     = 0;
    graphicsIn.subwindow = -1;
    graphicsIn.type      = 0;
    graphicsIn.string[0] = '\0';
    if ( event.LeftUp() )
    {
        graphicsIn.button = 1;
        graphicsIn.state |= PL_MASK_BUTTON1;
    }
    else if ( event.MiddleUp() )
    {
        graphicsIn.button = 2;
        graphicsIn.state |= PL_MASK_BUTTON2;
    }
    else if ( event.RightUp() )
    {
        graphicsIn.button = 3;
        graphicsIn.state |= PL_MASK_BUTTON3;
    }
    else if ( event.Aux1Up() )
    {
        graphicsIn.button = 4;
        graphicsIn.state |= PL_MASK_BUTTON4;
    }
    else if ( event.Aux2Up() )
    {
        graphicsIn.button = 5;
        graphicsIn.state |= PL_MASK_BUTTON5;
    }
    else
    {
        //If we get here we have just captured motion
        //not a click
        graphicsIn.button = 0;
        graphicsIn.state  = 0;
        graphicsIn.keysym = 0;
    }

    if ( wxGetKeyState( WXK_SHIFT ) )
        graphicsIn.state |= PL_MASK_SHIFT;
    if ( wxGetKeyState( WXK_CAPITAL ) )
        graphicsIn.state |= PL_MASK_CAPS;
    if ( wxGetKeyState( WXK_ALT ) && wxGetKeyState( WXK_CONTROL ) )
        graphicsIn.state |= PL_MASK_ALTGR;
    else if ( wxGetKeyState( WXK_CONTROL ) )
        graphicsIn.state |= PL_MASK_CONTROL;
    else if ( wxGetKeyState( WXK_ALT ) )
        graphicsIn.state |= PL_MASK_ALT;
    if ( wxGetKeyState( WXK_NUMLOCK ) )
        graphicsIn.state |= PL_MASK_NUM;
    if ( wxGetKeyState( WXK_SCROLL ) )
        graphicsIn.state |= PL_MASK_SCROLL;
    //Note I can't find a way to catch the windows key

    m_stream.translatecursor( &graphicsIn );
    this->OnLocate( graphicsIn );
}

//! Redo the whole plot, only if the window has been Created
//
template<class WXWINDOW>
void wxPLplotwindow<WXWINDOW>::RenewPlot( void )
{
    wxLogDebug("wxPLplotwindow::RenewPlot");
    if ( m_created )
    {
        WXWINDOW::Refresh();
    }
}

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();
    }
}


//! Save plot.
//
template<class WXWINDOW>
bool wxPLplotwindow<WXWINDOW>::SavePlot( const wxString& devname, const 
wxString& filename )
{
    int  pls, pls_save;
    FILE *sfile;

    if ( ( sfile = fopen( filename.mb_str(), "wb+" ) ) == NULL )
    {
        return false;
    }

    plgstrm( &pls );
    plmkstrm( &pls_save );
    if ( pls_save < 0 )
    {
        fclose( sfile );
        return false;
    }
    plsdev( devname.mb_str() );
    plsfile( sfile );

    plspage( 0., 0., 800, 600, 0, 0 );
    plcpstrm( pls, 0 );
    pladv( 0 );
    plreplot();
    plend1();
    plsstrm( pls );

    return true;
}

//! Set whether we wish to use wxGCDC instead of a wxDC
template<class WXWINDOW>
void wxPLplotwindow<WXWINDOW>::setUseGraphicsContext( bool useGraphicsContext )
{
    wxDC *drawDc;
#ifdef wxUSE_GRAPHICS_CONTEXT
    if ( useGraphicsContext != m_useGraphicsContext )
    {
        m_useGraphicsContext = useGraphicsContext;
        drawDc = m_useGraphicsContext ? (wxDC *) m_gcDc : (wxDC *) m_memoryDc;
    }
#else
    drawDc = &m_memoryDc;
    m_useGraphicsContext = false;
#endif
    if ( m_created )
    {
        m_stream.SetDC( drawDc );
        RenewPlot();
    }
}

template<class WXWINDOW>
void wxPLplotwindow<WXWINDOW>::setCanvasColour( const wxColour &colour )
{
    m_canvasColour = colour;
    RenewPlot();
}

#endif // !defined( WXPLPLOTWINDOW_H__INCLUDED_ )
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Plplot-devel mailing list
Plplot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/plplot-devel

Reply via email to