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