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