PLplot  5.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
wxPLplotstream.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 // wxwidgets headers
21 #include "wx/wx.h"
22 
23 // plplot headers
24 #include "plplotP.h"
25 
26 #include "wxPLplotstream.h"
27 
29 // Here we set the driver (wxwidgets :), and tell plplot in which dc to
30 // plot to and the size of the canvas. We also check and set several
31 // device style options.
32 //
33 wxPLplotstream::wxPLplotstream( wxDC *dc, int width, int height, int style ) : plstream()
34 {
35  Create( dc, width, height, style );
36 }
37 
38 
39 wxPLplotstream::wxPLplotstream() : plstream()
40 {
41 }
42 
43 
44 void wxPLplotstream::Create( wxDC *dc, int width, int height, int style )
45 {
46  const size_t bufferSize = 256;
47 
48  m_dc = dc;
49  m_width = width;
50  m_height = height;
51  m_style = style;
52  m_image = NULL;
53 
54  sdev( "wxwidgets" );
55  spage( 0.0, 0.0, m_width, m_height, 0, 0 );
56 
57  // use freetype, antialized canvas?
58  char drvopt[bufferSize], buffer[bufferSize];
59  drvopt[0] = '\0';
60 #ifdef WX_TEMP_PL_HAVE_FREETYPE_IS_ON
61  sprintf( buffer, "freetype=%d,smooth=%d,",
62  m_style & wxPLPLOT_FREETYPE ? 1 : 0,
63  m_style & wxPLPLOT_SMOOTH_TEXT ? 1 : 0 );
64  strcat( drvopt, buffer );
65 #endif
66 
67  int backend;
68  if ( m_style & wxPLPLOT_BACKEND_GC )
69  backend = 2;
70  else if ( m_style & wxPLPLOT_BACKEND_AGG )
71  backend = 1;
72  else
73  backend = 0;
74 
75  sprintf( buffer, "hrshsym=%d,text=%d,backend=%d",
76  m_style & wxPLPLOT_USE_HERSHEY_SYMBOLS ? 1 : 0,
77  m_style & wxPLPLOT_DRAW_TEXT ? 1 : 0,
78  backend );
79  strncat( drvopt, buffer, bufferSize - strlen( drvopt ) );
80 
81  setopt( "-drvopt", drvopt );
82 
83  init();
84 
85  cmd( PLESC_GETBACKEND, &m_backend );
86  m_backend = 1 << ( m_backend + 2 );
87 
88  if ( m_backend == wxPLPLOT_BACKEND_AGG )
89  {
90  m_image = new wxImage( m_width, m_height );
91  cmd( PLESC_DEVINIT, (void *) m_image );
92  }
93  else
94  cmd( PLESC_DEVINIT, (void *) m_dc );
95 }
96 
97 
98 wxPLplotstream::~wxPLplotstream()
99 {
100  if ( m_image )
101  delete m_image;
102 }
103 
104 
106 // code processed before every call of a plplot functions, since set_stream()
107 // is called before every plplot function. Not used in the moment.
108 //
109 void wxPLplotstream::set_stream()
110 {
112 }
113 
114 
116 // to set the new size. You need to call RenewPlot afterwards.
117 //
118 void wxPLplotstream::SetSize( int width, int height )
119 {
120  // For the AGG backend it is important to set first the new image buffer
121  // and tell the driver the new size if the buffer size increases and
122  // the other way round if the buffer size decreases. There is no impact
123  // for the other backends. This is kind of hacky, but I have no better
124  // idea in the moment
125  if ( width * height > m_width * m_height )
126  {
127  if ( m_image )
128  {
129  delete m_image;
130  m_image = new wxImage( width, height );
131  cmd( PLESC_DEVINIT, (void *) m_image );
132  }
133  wxSize size( width, height );
134  cmd( PLESC_RESIZE, (void *) &size );
135  }
136  else
137  {
138  wxSize size( width, height );
139  cmd( PLESC_RESIZE, (void *) &size );
140  if ( m_image )
141  {
142  delete m_image;
143  m_image = new wxImage( width, height );
144  cmd( PLESC_DEVINIT, (void *) m_image );
145  }
146  }
147 
148  m_width = width;
149  m_height = height;
150 }
151 
152 
154 //
155 void wxPLplotstream::RenewPlot()
156 {
157  replot();
158  Update();
159 }
160 
161 
162 // After calling plot commands it is not sure, that the dc
163 // gets updated properly, therefore you need to call this function.
164 //
165 void wxPLplotstream::Update()
166 {
167  if ( m_image )
168  {
169  wxMemoryDC MemoryDC;
170  wxBitmap bitmap( *m_image, -1 );
171  MemoryDC.SelectObject( bitmap );
172  m_dc->Blit( 0, 0, m_width, m_height, &MemoryDC, 0, 0 );
173  MemoryDC.SelectObject( wxNullBitmap );
174  }
175 }