PLplot  5.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
wxwidgets.h
Go to the documentation of this file.
1 // Copyright (C) 2008 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 #ifndef __WXWIDGETS_H__
21 #define __WXWIDGETS_H__
22 
23 // some special wxWidgets headers
24 #include <wx/spinctrl.h>
25 
26 // freetype headers and macros
27 #ifdef PL_HAVE_FREETYPE
28  #include "plfreetype.h"
29 #endif
30 
31 #ifndef max_number_of_grey_levels_used_in_text_smoothing
32  #define max_number_of_grey_levels_used_in_text_smoothing 64
33 #endif
34 
35 #define NDEV 100 // Max number of output device types in menu
36 
37 //--------------------------------------------------------------------------
38 // Physical dimension constants used by the driver
39 
40 // Virtual coordinate scaling parameter, used to do calculations at
41 // higher resolution. Chosen so that maximum plP_setphy coordinate
42 // is 32000 which is close to but still less than the maximum possible
43 // which is 32767.
44 //
45 // The trick here is to do everything in device coordinates on the driver
46 // side, but report/receive everything in virtual coordinates to/from the
47 // PLplot core.
48 //
49 #define VSCALE ( 40. )
50 
51 // pixels per inch
52 #define DEVICE_PIXELS_PER_IN ( 80. )
53 #define VIRTUAL_PIXELS_PER_IN ( DEVICE_PIXELS_PER_IN * VSCALE )
54 
55 
56 // mm per inch
57 #define MM_PER_IN ( 25.4 )
58 
59 // pixels per mm
60 #define DEVICE_PIXELS_PER_MM ( DEVICE_PIXELS_PER_IN / MM_PER_IN )
61 #define VIRTUAL_PIXELS_PER_MM ( VIRTUAL_PIXELS_PER_IN / MM_PER_IN )
62 
63 // Default dimensions of the canvas (in inches)
64 #define CANVAS_WIDTH ( 10.0 )
65 #define CANVAS_HEIGHT ( 7.5 )
66 
67 // size of plot in pixels on screen if not given
68 #define PLOT_WIDTH ( 800 )
69 #define PLOT_HEIGHT ( 600 )
70 
71 // These need to be distinguished since the handling is slightly different.
72 #define LOCATE_INVOKED_VIA_API 1
73 #define LOCATE_INVOKED_VIA_DRIVER 2
74 
75 // Available backends
76 #define wxBACKEND_DC 0
77 #define wxBACKEND_AGG 1
78 #define wxBACKEND_GC 2
79 
80 class wxPLplotFrame;
81 
82 // base device class
84 {
85 public: // methods
86  wxPLDevBase( int bcknd );
87  virtual ~wxPLDevBase( void );
88 
89  // virtual functions which need to implemented
90  virtual void DrawLine( short x1a, short y1a, short x2a, short y2a ) = 0;
91  virtual void DrawPolyline( short *xa, short *ya, PLINT npts ) = 0;
92  virtual void ClearBackground( PLINT bgr, PLINT bgg, PLINT bgb, PLINT x1 = -1, PLINT y1 = -1, PLINT x2 = -1, PLINT y2 = -1 ) = 0;
93  virtual void FillPolygon( PLStream *pls ) = 0;
94  virtual void BlitRectangle( wxDC* dc, int vX, int vY, int vW, int vH ) = 0;
95  void AddtoClipRegion( int x1, int y1, int x2, int y2 );
96  virtual void CreateCanvas() = 0;
97  virtual void SetWidth( PLStream *pls ) = 0;
98  virtual void SetColor0( PLStream *pls ) = 0;
99  virtual void SetColor1( PLStream *pls ) = 0;
100  virtual void SetExternalBuffer( void* buffer ) = 0;
101 #ifdef PL_HAVE_FREETYPE
102  virtual void PutPixel( short x, short y, PLINT color ) = 0;
103  virtual void PutPixel( short x, short y ) = 0;
104  virtual PLINT GetPixel( short x, short y ) = 0;
105 #endif // PL_HAVE_FREETYPE
106  virtual void ProcessString( PLStream* pls, EscText* args ) = 0;
107  virtual void PSDrawText( PLUNICODE* ucs4, int ucs4Len, bool drawText );
108  virtual void PSDrawTextToDC( char* utf8_string, bool drawText ) = 0;
109  virtual void PSSetFont( PLUNICODE fci ) = 0;
110 
111 public: // variables
112  const int backend;
113  bool ready;
114  bool ownGUI;
115  bool showGUI;
116  bool waiting;
117  bool resizing;
118  bool exit;
119  int comcount;
120 
128 
133 
136 
137  bool plstate_width; // Flags indicating change of state before
138  bool plstate_color0; // device is fully initialized
139  bool plstate_color1; // taken from gcw driver
140 
141  PLGraphicsIn gin; // Graphics input structure
142  int locate_mode; // Set while in locate mode
143  bool draw_xhair; // Set during xhair draws
144 
145  // clipping region
149 
150  // variables for antializing
151  int freetype;
153 
154  const char ** devDesc; // Descriptive names for file-oriented devices. Malloc'ed.
155  const char ** devName; // Keyword names of file-oriented devices. Malloc'ed.
156  int ndev;
157  wxBitmapType bitmapType;
158 
159  // font variables
160  static const int max_string_length = 500;
161  wxFont * m_font;
163  double fontSize;
164  double fontScale;
167  //the distance between the superscript top and subscript base from the baseline
169  double lineSpacing;
170  double yOffset;
175 };
176 
177 
178 class wxPLDevDC : public wxPLDevBase
179 {
180 public: // methods
181  wxPLDevDC( void );
182  ~wxPLDevDC( void );
183 
184  void DrawLine( short x1a, short y1a, short x2a, short y2a );
185  void DrawPolyline( short *xa, short *ya, PLINT npts );
186  void ClearBackground( PLINT bgr, PLINT bgg, PLINT bgb, PLINT x1 = -1, PLINT y1 = -1, PLINT x2 = -1, PLINT y2 = -1 );
187  void FillPolygon( PLStream *pls );
188  void BlitRectangle( wxDC* dc, int vX, int vY, int vW, int vH );
189  void CreateCanvas();
190  void SetWidth( PLStream *pls );
191  void SetColor0( PLStream *pls );
192  void SetColor1( PLStream *pls );
193  void SetExternalBuffer( void* buffer );
194 #ifdef PL_HAVE_FREETYPE
195  void PutPixel( short x, short y, PLINT color );
196  void PutPixel( short x, short y );
197  PLINT GetPixel( short x, short y );
198 #endif // PL_HAVE_FREETYPE
199  void ProcessString( PLStream* pls, EscText* args );
200  void PSDrawTextToDC( char* utf8_string, bool drawText );
201  void PSSetFont( PLUNICODE fci );
202 
203 private: // variables
204  wxBitmap* m_bitmap;
205  wxDC * m_dc;
206 };
207 
208 
209 #ifdef HAVE_AGG
210 // antigrain headers
211 #include "agg2/agg_basics.h"
212 #include "agg2/agg_rendering_buffer.h"
213 #include "agg2/agg_rasterizer_scanline_aa.h"
214 #include "agg2/agg_scanline_u.h"
215 #include "agg2/agg_scanline_bin.h"
216 #include "agg2/agg_conv_stroke.h"
217 #include "agg2/agg_pixfmt_rgb.h"
218 #include "agg2/agg_renderer_base.h"
219 #include "agg2/agg_renderer_scanline.h"
220 #include "agg2/agg_renderer_primitives.h"
221 #include "agg2/agg_path_storage.h"
222 #include "agg2/agg_conv_curve.h"
223 #include "agg2/agg_conv_contour.h"
224 #include "agg2/agg_font_freetype.h"
225 
226 class wxPLDevAGG : public wxPLDevBase
227 {
228  // type definitions and enums
229  typedef agg::pixfmt_rgb24 pixFormat;
230  typedef agg::renderer_base<pixFormat> rendererBase;
231  typedef agg::renderer_scanline_aa_solid<rendererBase> rendererSolid;
232  typedef agg::font_engine_freetype_int32 fontEngineType;
233  typedef agg::font_cache_manager<fontEngineType> fontManagerType;
234  typedef agg::font_cache_manager<fontEngineType>::gray8_adaptor_type fontRasterizer;
235  typedef agg::font_cache_manager<fontEngineType>::gray8_scanline_type fontScanline;
236  typedef agg::conv_curve<agg::path_storage> convCurve;
237  typedef agg::conv_stroke<convCurve> convStroke;
238  typedef agg::conv_transform<convCurve> pathTransform;
239  typedef agg::conv_transform<convStroke> strokeTransform;
240 
241  enum drawPathFlag { Fill, Stroke, FillAndStroke };
242 
243 public: // methods
244  wxPLDevAGG( void );
245  ~wxPLDevAGG( void );
246 
247  void DrawLine( short x1a, short y1a, short x2a, short y2a );
248  void DrawPolyline( short *xa, short *ya, PLINT npts );
249  void ClearBackground( PLINT bgr, PLINT bgg, PLINT bgb, PLINT x1 = -1, PLINT y1 = -1, PLINT x2 = -1, PLINT y2 = -1 );
250  void FillPolygon( PLStream *pls );
251  void BlitRectangle( wxDC* dc, int vX, int vY, int vW, int vH );
252  void CreateCanvas();
253  void SetWidth( PLStream *pls );
254  void SetColor0( PLStream *pls );
255  void SetColor1( PLStream *pls );
256  void SetExternalBuffer( void* buffer );
257 #ifdef PL_HAVE_FREETYPE
258  void PutPixel( short x, short y, PLINT color );
259  void PutPixel( short x, short y );
260  PLINT GetPixel( short x, short y );
261 #endif // PL_HAVE_FREETYPE
262  void ProcessString( PLStream* pls, EscText* args );
263  void PSDrawTextToDC( char* utf8_string, bool drawText );
264  void PSSetFont( PLUNICODE fci );
265  void drawPath( drawPathFlag flag );
266  void AGGAddtoClipRegion( short x1, short y1, short x2, short y2 );
267 
268 private: // variables
269  wxDC * mDC;
270  wxImage * mBuffer;
271  agg::rendering_buffer mRenderingBuffer;
272  fontEngineType mFontEngine;
273  fontManagerType mFontManager;
274  agg::conv_curve<fontManagerType::path_adaptor_type> mCurves;
275  agg::conv_contour<agg::conv_curve<fontManagerType::path_adaptor_type> > mContour;
276 
277  pixFormat mPixFormat;
278  rendererBase mRendererBase;
279  rendererSolid mRendererSolid;
280 
281  agg::scanline_u8 mScanLine;
282  agg::rasterizer_scanline_aa<> mRasterizer;
283  agg::path_storage mPath;
284  agg::trans_affine mTransform;
285 
286  convCurve mConvCurve;
287  convStroke mConvStroke;
288  pathTransform mPathTransform;
289  strokeTransform mStrokeTransform;
290 
291  double mStrokeWidth;
292  wxUint8 mStrokeOpacity;
293  unsigned char mColorRedStroke;
294  unsigned char mColorGreenStroke;
295  unsigned char mColorBlueStroke;
296  unsigned char mColorRedFill;
297  unsigned char mColorGreenFill;
298  unsigned char mColorBlueFill;
299 
300  double textWidth, textHeight;
301  wxString fontdir;
302 };
303 #endif
304 
305 
306 #if wxUSE_GRAPHICS_CONTEXT
307 #include <wx/graphics.h>
308 
309 class wxPLDevGC : public wxPLDevBase
310 {
311 public: // methods
312  wxPLDevGC( void );
313  ~wxPLDevGC( void );
314 
315  void DrawLine( short x1a, short y1a, short x2a, short y2a );
316  void DrawPolyline( short *xa, short *ya, PLINT npts );
317  void ClearBackground( PLINT bgr, PLINT bgg, PLINT bgb, PLINT x1 = -1, PLINT y1 = -1, PLINT x2 = -1, PLINT y2 = -1 );
318  void FillPolygon( PLStream *pls );
319  void BlitRectangle( wxDC* dc, int vX, int vY, int vW, int vH );
320  void CreateCanvas();
321  void SetWidth( PLStream *pls );
322  void SetColor0( PLStream *pls );
323  void SetColor1( PLStream *pls );
324  void SetExternalBuffer( void* buffer );
325 #ifdef PL_HAVE_FREETYPE
326  void PutPixel( short x, short y, PLINT color );
327  void PutPixel( short x, short y );
328  PLINT GetPixel( short x, short y );
329 #endif // PL_HAVE_FREETYPE
330  void ProcessString( PLStream* pls, EscText* args );
331  void PSDrawTextToDC( char* utf8_string, bool drawText );
332  void PSSetFont( PLUNICODE fci );
333 
334 private: // variables
335  wxBitmap * m_bitmap;
336  wxDC * m_dc;
337  wxGraphicsContext* m_context;
338 
339  // text colors
340  unsigned char textRed, textGreen, textBlue;
341 
342  unsigned char mStrokeOpacity;
343  unsigned char mColorRedStroke;
344  unsigned char mColorGreenStroke;
345  unsigned char mColorBlueStroke;
346  unsigned char mColorRedFill;
347  unsigned char mColorGreenFill;
348  unsigned char mColorBlueFill;
349 };
350 #endif
351 
352 
353 struct dev_entry
354 {
355  wxString dev_name;
356  wxString dev_menu_short;
357  wxString dev_menu_long;
358  wxString dev_file_app;
360 };
361 
362 
363 
364 // after how many commands the window should be refreshed
365 #define MAX_COMCOUNT 10000
366 
367 // wxwidgets application definition (if needed)
368 WX_DEFINE_ARRAY( wxPLplotFrame *, wxArrayOfwxPLplotFrame );
369 class wxPLplotApp : public wxApp
370 {
371 public:
372  virtual bool OnInit();
373  // virtual int OnExit();
374  void SetExitFlag( bool flag = true ) { exit = flag; };
375  bool GetExitFlag( void ) { return exit; };
376  void SetAdvanceFlag( bool flag = true ) { advance = flag; };
377  bool GetAdvanceFlag( void ) { return advance; };
378  void SetRefreshFlag( bool flag = true );
379  void AddFrame( wxPLplotFrame* frame ) { FrameArray.Add( frame ); };
380  void RemoveFrame( wxPLplotFrame* frame ) { FrameArray.Remove( frame ); };
381  size_t FrameCount( void ) { return FrameArray.GetCount(); }
382  void OnIdle( wxIdleEvent& event );
383 
384 private:
385  bool exit;
386  bool advance;
387  wxArrayOfwxPLplotFrame FrameArray;
388 
389  DECLARE_EVENT_TABLE()
390 };
391 
392 // definition of the actual window/frame shown
393 class wxPLplotWindow : public wxWindow
394 {
395 public:
396  wxPLplotWindow( wxWindow* parent, PLStream *pls );
397 
398  void SetRefreshFlag( bool flag = true ) { refresh = flag; };
399  bool GetRefreshFlag( void ) { return refresh; };
400  void SetOrientation( int rot );
401 
402 private:
403  void OnPaint( wxPaintEvent& event );
404  void OnChar( wxKeyEvent& event );
405  void OnIdle( wxIdleEvent& event );
406  void OnErase( wxEraseEvent & WXUNUSED( event ) );
407  void OnSize( wxSizeEvent & WXUNUSED( event ) );
408  void OnMaximize( wxMaximizeEvent & WXUNUSED( event ) );
409  void OnMouse( wxMouseEvent& event );
410  void Locate( void );
411  void DrawCrosshair();
412 
414  wxPLDevBase* m_dev; // windows needs to know this structure
415  bool refresh;
418 
419  DECLARE_EVENT_TABLE()
420 };
421 
422 
423 // declaration of the actual window/frame shown
424 class wxPLplotFrame : public wxFrame
425 {
426 public:
427  wxPLplotFrame( const wxString& title, PLStream* pls );
428  void OnMenu( wxCommandEvent& event );
429  void OnClose( wxCloseEvent& event );
430  bool SavePlot( const char* filename, const char* devname, int width, int height );
431  void SetRefreshFlag( bool flag = true ) { m_window->SetRefreshFlag( flag ); };
432  bool GetRefreshFlag( void ) { return m_window->GetRefreshFlag(); };
433 
434 private:
435  wxPanel * m_panel;
437  wxPLDevBase * m_dev; // frame needs to know this structure
438 
439  DECLARE_EVENT_TABLE()
440 };
441 
442 // menu ids
445 
446 
447 // Pixel size dialog
448 class wxGetSizeDialog : public wxDialog
449 {
450 public:
451  // constructors and destructors
452  wxGetSizeDialog( wxWindow *parent, wxWindowID id, const wxString &title,
453  const wxPoint& pos = wxDefaultPosition,
454  const wxSize& size = wxDefaultSize,
455  long style = wxDEFAULT_DIALOG_STYLE,
456  int width = 800, int height = 600 );
457 
458  int getWidth() { return spinControlWidth->GetValue(); }
459  int getHeight() { return spinControlHeight->GetValue(); }
460 
461 private:
462  wxSpinCtrl* spinControlWidth;
463  wxSpinCtrl* spinControlHeight;
464 
465 private:
466  DECLARE_EVENT_TABLE()
467 };
468 
469 
470 // Use this macro if you want to define your own main() or WinMain() function
471 // and call wxEntry() from there.
472 #define IMPLEMENT_PLAPP_NO_MAIN( appname ) \
473  wxAppConsole * wxPLCreateApp() \
474  { \
475  wxAppConsole::CheckBuildOptions( WX_BUILD_OPTIONS_SIGNATURE, \
476  "your program" ); \
477  return new appname; \
478  } \
479  wxAppInitializer \
480  wxAppInitializer( (wxAppInitializerFunction) ( wxApp::GetInitializerFunction() == NULL ? wxPLCreateApp : wxApp::GetInitializerFunction() ) ); \
481  DECLARE_PLAPP( appname ) \
482  appname & wxPLGetApp() { return *wx_static_cast( appname *, wxApp::GetInstance() ); }
483 
484 #define DECLARE_PLAPP( appname ) extern appname &wxPLGetApp();
485 
486 // workaround against warnings for unused variables
487 static inline void Use( void * )
488 {
489 }
490 #define WX_SUPPRESS_UNUSED_WARN( x ) Use( &x )
491 
492 
493 //--------------------------------------------------------------------------
494 // Declarations for the device.
495 //--------------------------------------------------------------------------
496 
497 void plD_init_wxwidgets( PLStream * );
498 void plD_init_wxpng( PLStream * );
499 void plD_line_wxwidgets( PLStream *, short, short, short, short );
500 void plD_polyline_wxwidgets( PLStream *, short *, short *, PLINT );
501 void plD_eop_wxwidgets( PLStream * );
502 void plD_bop_wxwidgets( PLStream * );
503 void plD_tidy_wxwidgets( PLStream * );
505 void plD_esc_wxwidgets( PLStream *, PLINT, void * );
506 
507 void wx_set_dc( PLStream* pls, wxDC* dc );
508 void wx_set_buffer( PLStream* pls, wxImage* buffer );
509 void wx_set_size( PLStream* pls, int width, int height );
510 int plD_errorexithandler_wxwidgets( char *errormessage );
511 void plD_erroraborthandler_wxwidgets( char *errormessage );
512 
513 //--------------------------------------------------------------------------
514 // Debug functions
515 //--------------------------------------------------------------------------
516 
517 // define if you want debug output
518 // #define _DEBUG //
519 // #define _DEBUG_VERBOSE //
520 void Log_Verbose( const char *fmt, ... );
521 void Log_Debug( const char *fmt, ... );
522 
523 
524 //--------------------------------------------------------------------------
525 // Font style and weight lookup tables
526 //--------------------------------------------------------------------------
527 const wxFontFamily fontFamilyLookup[5] = {
528  wxFONTFAMILY_SWISS, // sans-serif
529  wxFONTFAMILY_ROMAN, // serif
530  wxFONTFAMILY_TELETYPE, // monospace
531  wxFONTFAMILY_SCRIPT, // script
532  wxFONTFAMILY_SWISS // symbol
533 };
534 
535 const int fontStyleLookup[3] = {
536  wxFONTFLAG_DEFAULT, // upright
537  wxFONTFLAG_ITALIC, // italic
538  wxFONTFLAG_SLANT // oblique
539 };
540 
541 const int fontWeightLookup[2] = {
542  wxFONTFLAG_DEFAULT, // medium
543  wxFONTFLAG_BOLD // bold
544 };
545 
546 #endif // __WXWIDGETS_H__