PLplot  5.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
wxPLplotwindow.cpp
Go to the documentation of this file.
1 // Copyright (C) 2005 Werner Smekal
2 //
3 // This file is part of PLplot.
4 //
5 // PLplot is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // PLplot is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public License
16 // along with PLplot; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //
19 
20 #include <wx/window.h>
21 #include <wx/dcclient.h>
22 
23 //#include "plplotP.h"
24 #include "wxPLplotwindow.h"
25 #include "wxPLplotstream.h"
26 
27 
28 BEGIN_EVENT_TABLE( wxPLplotwindow, wxWindow )
29 EVT_SIZE( wxPLplotwindow::OnSize )
30 EVT_PAINT( wxPLplotwindow::OnPaint )
31 EVT_ERASE_BACKGROUND( wxPLplotwindow::OnErase )
32 END_EVENT_TABLE()
33 
34 
36 //
37 wxPLplotwindow::wxPLplotwindow( wxWindow* parent, wxWindowID id, const wxPoint& pos,
38  const wxSize& size, long style, int pl_style ) :
39  wxWindow( parent, id, pos, size, style | wxFULL_REPAINT_ON_RESIZE )
40 {
41  // create MemoryDC and set size - if size not set (-1, -1) than
42  // set size to (640,400)
43  MemPlotDC = new wxMemoryDC;
44  if ( size.GetWidth() < 0 || size.GetHeight() < 0 )
45  {
46  m_width = 640;
47  m_height = 400;
48  }
49  else
50  {
51  m_width = size.GetWidth();
52  m_height = size.GetHeight();
53  }
54  bitmapWidth = m_width;
55  bitmapHeight = m_height;
56 
57  MemPlotDCBitmap = new wxBitmap( bitmapWidth, bitmapHeight, -1 );
58  MemPlotDC->SelectObject( *MemPlotDCBitmap );
59 
60  m_stream = new wxPLplotstream( (wxDC *) MemPlotDC, m_width, m_height, pl_style );
61 
62  m_stream->cmd( PLESC_GETBACKEND, &m_backend );
63  m_backend = 1 << ( m_backend + 2 );
64 
65  // tell wxWidgets to leave the background painting to this control
66  SetBackgroundStyle( wxBG_STYLE_CUSTOM );
67 }
68 
69 
71 //
73 {
74  MemPlotDC->SelectObject( wxNullBitmap );
75 
76  if ( MemPlotDCBitmap )
77  delete MemPlotDCBitmap;
78 
79  if ( m_stream )
80  delete m_stream;
81 
82  if ( MemPlotDC )
83  delete MemPlotDC;
84 }
85 
86 
88 // later), we also implement our own double buffering here (since the PLplot wxWidgets driver draws
89 // into a wxMemoryDC)
90 //
91 void wxPLplotwindow::OnPaint( wxPaintEvent &WXUNUSED( event ) )
92 {
93  wxPaintDC dc( this );
94  dc.Blit( 0, 0, m_width, m_height, MemPlotDC, 0, 0 );
95 }
96 
97 
98 void wxPLplotwindow::OnSize( wxSizeEvent& WXUNUSED( event ) )
99 {
100  int width, height;
101  GetClientSize( &width, &height );
102 
103  // Check if we window was resized
104  if ( ( m_width != width ) || ( m_height != height ) )
105  {
106  if ( ( width > bitmapWidth ) || ( height > bitmapHeight ) )
107  {
108  bitmapWidth = bitmapWidth > width ? bitmapWidth : width;
109  bitmapHeight = bitmapHeight > height ? bitmapHeight : height;
110 
111  MemPlotDC->SelectObject( wxNullBitmap );
112  if ( MemPlotDCBitmap )
113  delete MemPlotDCBitmap;
114  MemPlotDCBitmap = new wxBitmap( bitmapWidth, bitmapHeight, -1 );
115  MemPlotDC->SelectObject( *MemPlotDCBitmap );
116  }
117 
118  m_stream->SetSize( width, height );
119  m_stream->RenewPlot();
120 
121  m_width = width;
122  m_height = height;
123  }
124  else
125  {
126  m_stream->Update();
127  Refresh( false );
128  }
129 }
130 
131 
133 // is responsible that the background is not erased in order to prevent flickering.
134 //
135 void wxPLplotwindow::OnErase( wxEraseEvent &WXUNUSED( event ) )
136 {
137 }
138 
139 
141 //
143 {
144  if ( m_stream )
145  {
146  m_stream->RenewPlot();
147  Refresh( false );
148  }
149 }
150 
151 
153 //
154 bool wxPLplotwindow::SavePlot( const wxString& devname, const wxString& filename )
155 {
156  int pls, pls_save;
157  FILE *sfile;
158 
159  if ( ( sfile = fopen( filename.mb_str(), "wb+" ) ) == NULL )
160  {
161  return false;
162  }
163 
164  plgstrm( &pls );
165  plmkstrm( &pls_save );
166  if ( pls_save < 0 )
167  {
168  fclose( sfile );
169  return false;
170  }
171  plsdev( devname.mb_str() );
172  plsfile( sfile );
173 
174  plspage( 0., 0., 800, 600, 0, 0 );
175  plcpstrm( pls, 0 );
176  pladv( 0 );
177  plreplot();
178  plend1();
179  plsstrm( pls );
180 
181  return true;
182 }