PLplot  5.10.0
wxwidgets.h
Go to the documentation of this file.
00001 // Copyright (C) 2008  Werner Smekal
00002 //
00003 // This file is part of PLplot.
00004 //
00005 // PLplot is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU Library General Public License as published
00007 // by the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // PLplot is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Library General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Library General Public License
00016 // along with PLplot; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00018 //
00019 
00020 #ifndef __WXWIDGETS_H__
00021 #define __WXWIDGETS_H__
00022 
00023 // some special wxWidgets headers
00024 #include <wx/spinctrl.h>
00025 
00026 // freetype headers and macros
00027 #ifdef PL_HAVE_FREETYPE
00028   #include "plfreetype.h"
00029 #endif
00030 
00031 #ifndef max_number_of_grey_levels_used_in_text_smoothing
00032   #define max_number_of_grey_levels_used_in_text_smoothing    64
00033 #endif
00034 
00035 #define NDEV                                                  100 // Max number of output device types in menu
00036 
00037 //--------------------------------------------------------------------------
00038 // Physical dimension constants used by the driver
00039 
00040 // Virtual coordinate scaling parameter, used to do calculations at
00041 // higher resolution.  Chosen so that maximum plP_setphy coordinate
00042 // is 32000 which is close to but still less than the maximum possible
00043 // which is 32767.
00044 //
00045 // The trick here is to do everything in device coordinates on the driver
00046 // side, but report/receive everything in virtual coordinates to/from the
00047 // PLplot core.
00048 //
00049 #define VSCALE                   ( 40. )
00050 
00051 // pixels per inch
00052 #define DEVICE_PIXELS_PER_IN     ( 80. )
00053 #define VIRTUAL_PIXELS_PER_IN    ( DEVICE_PIXELS_PER_IN * VSCALE )
00054 
00055 
00056 // mm per inch
00057 #define MM_PER_IN                    ( 25.4 )
00058 
00059 // pixels per mm
00060 #define DEVICE_PIXELS_PER_MM         ( DEVICE_PIXELS_PER_IN / MM_PER_IN )
00061 #define VIRTUAL_PIXELS_PER_MM        ( VIRTUAL_PIXELS_PER_IN / MM_PER_IN )
00062 
00063 // Default dimensions of the canvas (in inches)
00064 #define CANVAS_WIDTH                 ( 10.0 )
00065 #define CANVAS_HEIGHT                ( 7.5 )
00066 
00067 // size of plot in pixels on screen if not given
00068 #define PLOT_WIDTH                   ( 800 )
00069 #define PLOT_HEIGHT                  ( 600 )
00070 
00071 // These need to be distinguished since the handling is slightly different.
00072 #define LOCATE_INVOKED_VIA_API       1
00073 #define LOCATE_INVOKED_VIA_DRIVER    2
00074 
00075 // Available backends
00076 #define wxBACKEND_DC                 0
00077 #define wxBACKEND_AGG                1
00078 #define wxBACKEND_GC                 2
00079 
00080 class wxPLplotFrame;
00081 
00082 // base device class
00083 class wxPLDevBase
00084 {
00085 public: // methods
00086     wxPLDevBase( int bcknd );
00087     virtual ~wxPLDevBase( void );
00088 
00089     // virtual functions which need to implemented
00090     virtual void DrawLine( short x1a, short y1a, short x2a, short y2a ) = 0;
00091     virtual void DrawPolyline( short *xa, short *ya, PLINT npts )       = 0;
00092     virtual void ClearBackground( PLINT bgr, PLINT bgg, PLINT bgb, PLINT x1 = -1, PLINT y1 = -1, PLINT x2 = -1, PLINT y2 = -1 ) = 0;
00093     virtual void FillPolygon( PLStream *pls ) = 0;
00094     virtual void BlitRectangle( wxDC* dc, int vX, int vY, int vW, int vH ) = 0;
00095     void AddtoClipRegion( int x1, int y1, int x2, int y2 );
00096     virtual void CreateCanvas()                    = 0;
00097     virtual void SetWidth( PLStream *pls )         = 0;
00098     virtual void SetColor0( PLStream *pls )        = 0;
00099     virtual void SetColor1( PLStream *pls )        = 0;
00100     virtual void SetExternalBuffer( void* buffer ) = 0;
00101 #ifdef PL_HAVE_FREETYPE
00102     virtual void PutPixel( short x, short y, PLINT color ) = 0;
00103     virtual void PutPixel( short x, short y )  = 0;
00104     virtual PLINT GetPixel( short x, short y ) = 0;
00105 #endif // PL_HAVE_FREETYPE
00106     virtual void ProcessString( PLStream* pls, EscText* args ) = 0;
00107     virtual void PSDrawText( PLUNICODE* ucs4, int ucs4Len, bool drawText );
00108     virtual void PSDrawTextToDC( char* utf8_string, bool drawText ) = 0;
00109     virtual void PSSetFont( PLUNICODE fci ) = 0;
00110 
00111 public: // variables
00112     const int    backend;
00113     bool         ready;
00114     bool         ownGUI;
00115     bool         showGUI;
00116     bool         waiting;
00117     bool         resizing;
00118     bool         exit;
00119     int          comcount;
00120 
00121     wxPLplotFrame* m_frame;
00122     PLINT        xpos;
00123     PLINT        ypos;
00124     PLINT        width;
00125     PLINT        height;
00126     PLINT        bm_width;
00127     PLINT        bm_height;
00128 
00129     PLINT        xmin;
00130     PLINT        xmax;
00131     PLINT        ymin;
00132     PLINT        ymax;
00133 
00134     PLFLT        scalex;
00135     PLFLT        scaley;
00136 
00137     bool         plstate_width;         // Flags indicating change of state before
00138     bool         plstate_color0;        // device is fully initialized
00139     bool         plstate_color1;        // taken from gcw driver
00140 
00141     PLGraphicsIn gin;                   // Graphics input structure
00142     int          locate_mode;           // Set while in locate mode
00143     bool         draw_xhair;            // Set during xhair draws
00144 
00145     // clipping region
00146     int  clipminx, clipmaxx;
00147     int  clipminy, clipmaxy;
00148     bool newclipregion;
00149 
00150     // variables for antializing
00151     int          freetype;
00152     int          smooth_text;
00153 
00154     const char   ** devDesc;    // Descriptive names for file-oriented devices.  Malloc'ed.
00155     const char   ** devName;    // Keyword names of file-oriented devices. Malloc'ed.
00156     int          ndev;
00157     wxBitmapType bitmapType;
00158 
00159     // font variables
00160     static const int max_string_length = 500;
00161     wxFont           * m_font;
00162     bool             underlined;
00163     double           fontSize;
00164     double           fontScale;
00165     wxCoord          textWidth, textHeight, textDescent, textLeading;
00166     PLUNICODE        fci;
00167     //the distance between the superscript top and subscript base from the baseline
00168     wxCoord          superscriptHeight, subscriptDepth;
00169     double           lineSpacing;
00170     double           yOffset;
00171     PLINT            posX, posY;
00172     PLFLT            rotation, cos_rot, sin_rot;
00173     PLFLT            shear, cos_shear, sin_shear;
00174     PLFLT            stride;
00175 };
00176 
00177 
00178 class wxPLDevDC : public wxPLDevBase
00179 {
00180 public: // methods
00181     wxPLDevDC( void );
00182     ~wxPLDevDC( void );
00183 
00184     void DrawLine( short x1a, short y1a, short x2a, short y2a );
00185     void DrawPolyline( short *xa, short *ya, PLINT npts );
00186     void ClearBackground( PLINT bgr, PLINT bgg, PLINT bgb, PLINT x1 = -1, PLINT y1 = -1, PLINT x2 = -1, PLINT y2 = -1 );
00187     void FillPolygon( PLStream *pls );
00188     void BlitRectangle( wxDC* dc, int vX, int vY, int vW, int vH );
00189     void CreateCanvas();
00190     void SetWidth( PLStream *pls );
00191     void SetColor0( PLStream *pls );
00192     void SetColor1( PLStream *pls );
00193     void SetExternalBuffer( void* buffer );
00194 #ifdef PL_HAVE_FREETYPE
00195     void PutPixel( short x, short y, PLINT color );
00196     void PutPixel( short x, short y );
00197     PLINT GetPixel( short x, short y );
00198 #endif // PL_HAVE_FREETYPE
00199     void ProcessString( PLStream* pls, EscText* args );
00200     void PSDrawTextToDC( char* utf8_string, bool drawText );
00201     void PSSetFont( PLUNICODE fci );
00202 
00203 private: // variables
00204     wxBitmap* m_bitmap;
00205     wxDC    * m_dc;
00206 };
00207 
00208 
00209 #ifdef HAVE_AGG
00210 // antigrain headers
00211 #include "agg2/agg_basics.h"
00212 #include "agg2/agg_rendering_buffer.h"
00213 #include "agg2/agg_rasterizer_scanline_aa.h"
00214 #include "agg2/agg_scanline_u.h"
00215 #include "agg2/agg_scanline_bin.h"
00216 #include "agg2/agg_conv_stroke.h"
00217 #include "agg2/agg_pixfmt_rgb.h"
00218 #include "agg2/agg_renderer_base.h"
00219 #include "agg2/agg_renderer_scanline.h"
00220 #include "agg2/agg_renderer_primitives.h"
00221 #include "agg2/agg_path_storage.h"
00222 #include "agg2/agg_conv_curve.h"
00223 #include "agg2/agg_conv_contour.h"
00224 #include "agg2/agg_font_freetype.h"
00225 
00226 class wxPLDevAGG : public wxPLDevBase
00227 {
00228     // type definitions and enums
00229     typedef agg::pixfmt_rgb24                                              pixFormat;
00230     typedef agg::renderer_base<pixFormat>                                  rendererBase;
00231     typedef agg::renderer_scanline_aa_solid<rendererBase>                  rendererSolid;
00232     typedef agg::font_engine_freetype_int32                                fontEngineType;
00233     typedef agg::font_cache_manager<fontEngineType>                        fontManagerType;
00234     typedef agg::font_cache_manager<fontEngineType>::gray8_adaptor_type    fontRasterizer;
00235     typedef agg::font_cache_manager<fontEngineType>::gray8_scanline_type   fontScanline;
00236     typedef agg::conv_curve<agg::path_storage>                             convCurve;
00237     typedef agg::conv_stroke<convCurve>                                    convStroke;
00238     typedef agg::conv_transform<convCurve>                                 pathTransform;
00239     typedef agg::conv_transform<convStroke>                                strokeTransform;
00240 
00241     enum drawPathFlag { Fill, Stroke, FillAndStroke };
00242 
00243 public: // methods
00244     wxPLDevAGG( void );
00245     ~wxPLDevAGG( void );
00246 
00247     void DrawLine( short x1a, short y1a, short x2a, short y2a );
00248     void DrawPolyline( short *xa, short *ya, PLINT npts );
00249     void ClearBackground( PLINT bgr, PLINT bgg, PLINT bgb, PLINT x1 = -1, PLINT y1 = -1, PLINT x2 = -1, PLINT y2 = -1 );
00250     void FillPolygon( PLStream *pls );
00251     void BlitRectangle( wxDC* dc, int vX, int vY, int vW, int vH );
00252     void CreateCanvas();
00253     void SetWidth( PLStream *pls );
00254     void SetColor0( PLStream *pls );
00255     void SetColor1( PLStream *pls );
00256     void SetExternalBuffer( void* buffer );
00257 #ifdef PL_HAVE_FREETYPE
00258     void PutPixel( short x, short y, PLINT color );
00259     void PutPixel( short x, short y );
00260     PLINT GetPixel( short x, short y );
00261 #endif // PL_HAVE_FREETYPE
00262     void ProcessString( PLStream* pls, EscText* args );
00263     void PSDrawTextToDC( char* utf8_string, bool drawText );
00264     void PSSetFont( PLUNICODE fci );
00265     void drawPath( drawPathFlag flag );
00266     void AGGAddtoClipRegion( short x1, short y1, short x2, short y2 );
00267 
00268 private: // variables
00269     wxDC * mDC;
00270     wxImage               * mBuffer;
00271     agg::rendering_buffer mRenderingBuffer;
00272     fontEngineType        mFontEngine;
00273     fontManagerType       mFontManager;
00274     agg::conv_curve<fontManagerType::path_adaptor_type> mCurves;
00275     agg::conv_contour<agg::conv_curve<fontManagerType::path_adaptor_type> > mContour;
00276 
00277     pixFormat         mPixFormat;
00278     rendererBase      mRendererBase;
00279     rendererSolid     mRendererSolid;
00280 
00281     agg::scanline_u8  mScanLine;
00282     agg::rasterizer_scanline_aa<>                                           mRasterizer;
00283     agg::path_storage mPath;
00284     agg::trans_affine mTransform;
00285 
00286     convCurve         mConvCurve;
00287     convStroke        mConvStroke;
00288     pathTransform     mPathTransform;
00289     strokeTransform   mStrokeTransform;
00290 
00291     double            mStrokeWidth;
00292     wxUint8           mStrokeOpacity;
00293     unsigned char     mColorRedStroke;
00294     unsigned char     mColorGreenStroke;
00295     unsigned char     mColorBlueStroke;
00296     unsigned char     mColorRedFill;
00297     unsigned char     mColorGreenFill;
00298     unsigned char     mColorBlueFill;
00299 
00300     double            textWidth, textHeight;
00301     wxString          fontdir;
00302 };
00303 #endif
00304 
00305 
00306 #if wxUSE_GRAPHICS_CONTEXT
00307 #include <wx/graphics.h>
00308 
00309 class wxPLDevGC : public wxPLDevBase
00310 {
00311 public: // methods
00312     wxPLDevGC( void );
00313     ~wxPLDevGC( void );
00314 
00315     void DrawLine( short x1a, short y1a, short x2a, short y2a );
00316     void DrawPolyline( short *xa, short *ya, PLINT npts );
00317     void ClearBackground( PLINT bgr, PLINT bgg, PLINT bgb, PLINT x1 = -1, PLINT y1 = -1, PLINT x2 = -1, PLINT y2 = -1 );
00318     void FillPolygon( PLStream *pls );
00319     void BlitRectangle( wxDC* dc, int vX, int vY, int vW, int vH );
00320     void CreateCanvas();
00321     void SetWidth( PLStream *pls );
00322     void SetColor0( PLStream *pls );
00323     void SetColor1( PLStream *pls );
00324     void SetExternalBuffer( void* buffer );
00325 #ifdef PL_HAVE_FREETYPE
00326     void PutPixel( short x, short y, PLINT color );
00327     void PutPixel( short x, short y );
00328     PLINT GetPixel( short x, short y );
00329 #endif // PL_HAVE_FREETYPE
00330     void ProcessString( PLStream* pls, EscText* args );
00331     void PSDrawTextToDC( char* utf8_string, bool drawText );
00332     void PSSetFont( PLUNICODE fci );
00333 
00334 private: // variables
00335     wxBitmap         * m_bitmap;
00336     wxDC             * m_dc;
00337     wxGraphicsContext* m_context;
00338 
00339     // text colors
00340     unsigned char textRed, textGreen, textBlue;
00341 
00342     unsigned char mStrokeOpacity;
00343     unsigned char mColorRedStroke;
00344     unsigned char mColorGreenStroke;
00345     unsigned char mColorBlueStroke;
00346     unsigned char mColorRedFill;
00347     unsigned char mColorGreenFill;
00348     unsigned char mColorBlueFill;
00349 };
00350 #endif
00351 
00352 
00353 struct dev_entry
00354 {
00355     wxString dev_name;
00356     wxString dev_menu_short;
00357     wxString dev_menu_long;
00358     wxString dev_file_app;
00359     bool     pixelDevice;
00360 };
00361 
00362 
00363 
00364 // after how many commands the window should be refreshed
00365 #define MAX_COMCOUNT    10000
00366 
00367 // wxwidgets application definition (if needed)
00368 WX_DEFINE_ARRAY( wxPLplotFrame *, wxArrayOfwxPLplotFrame );
00369 class wxPLplotApp : public wxApp
00370 {
00371 public:
00372     virtual bool OnInit();
00373     // virtual int OnExit();
00374     void SetExitFlag( bool flag = true ) { exit = flag; };
00375     bool GetExitFlag( void ) { return exit; };
00376     void SetAdvanceFlag( bool flag = true ) { advance = flag; };
00377     bool GetAdvanceFlag( void ) { return advance; };
00378     void SetRefreshFlag( bool flag = true );
00379     void AddFrame( wxPLplotFrame* frame ) { FrameArray.Add( frame ); };
00380     void RemoveFrame( wxPLplotFrame* frame ) { FrameArray.Remove( frame ); };
00381     size_t FrameCount( void ) { return FrameArray.GetCount(); }
00382     void OnIdle( wxIdleEvent& event );
00383 
00384 private:
00385     bool exit;
00386     bool advance;
00387     wxArrayOfwxPLplotFrame FrameArray;
00388 
00389     DECLARE_EVENT_TABLE()
00390 };
00391 
00392 // definition of the actual window/frame shown
00393 class wxPLplotWindow : public wxWindow
00394 {
00395 public:
00396     wxPLplotWindow( wxWindow* parent, PLStream *pls );
00397 
00398     void SetRefreshFlag( bool flag = true ) { refresh = flag; };
00399     bool GetRefreshFlag( void ) { return refresh; };
00400     void SetOrientation( int rot );
00401 
00402 private:
00403     void OnPaint( wxPaintEvent& event );
00404     void OnChar( wxKeyEvent& event );
00405     void OnIdle( wxIdleEvent& event );
00406     void OnErase( wxEraseEvent & WXUNUSED( event ) );
00407     void OnSize( wxSizeEvent & WXUNUSED( event ) );
00408     void OnMaximize( wxMaximizeEvent & WXUNUSED( event ) );
00409     void OnMouse( wxMouseEvent& event );
00410     void Locate( void );
00411     void DrawCrosshair();
00412 
00413     PLStream   * m_pls;
00414     wxPLDevBase* m_dev; // windows needs to know this structure
00415     bool       refresh;
00416     bool       xhair_drawn;
00417     int        mouse_x, mouse_y, old_mouse_x, old_mouse_y;
00418 
00419     DECLARE_EVENT_TABLE()
00420 };
00421 
00422 
00423 // declaration of the actual window/frame shown
00424 class wxPLplotFrame : public wxFrame
00425 {
00426 public:
00427     wxPLplotFrame( const wxString& title, PLStream* pls );
00428     void OnMenu( wxCommandEvent& event );
00429     void OnClose( wxCloseEvent& event );
00430     bool SavePlot( const char* filename, const char* devname, int width, int height );
00431     void SetRefreshFlag( bool flag = true ) { m_window->SetRefreshFlag( flag ); };
00432     bool GetRefreshFlag( void ) { return m_window->GetRefreshFlag(); };
00433 
00434 private:
00435     wxPanel       * m_panel;
00436     wxPLplotWindow* m_window;
00437     wxPLDevBase   * m_dev; // frame needs to know this structure
00438 
00439     DECLARE_EVENT_TABLE()
00440 };
00441 
00442 // menu ids
00443 enum { wxPL_Save = 10000, wxPL_Next = 10100, wxPL_Locate, wxPL_Orientation_0, wxPL_Orientation_90,
00444        wxPL_Orientation_180, wxPL_Orientation_270 };
00445 
00446 
00447 // Pixel size dialog
00448 class wxGetSizeDialog : public wxDialog
00449 {
00450 public:
00451     // constructors and destructors
00452     wxGetSizeDialog( wxWindow *parent, wxWindowID id, const wxString &title,
00453                      const wxPoint& pos = wxDefaultPosition,
00454                      const wxSize& size = wxDefaultSize,
00455                      long style = wxDEFAULT_DIALOG_STYLE,
00456                      int width = 800, int height = 600 );
00457 
00458     int getWidth()  { return spinControlWidth->GetValue(); }
00459     int getHeight()  { return spinControlHeight->GetValue(); }
00460 
00461 private:
00462     wxSpinCtrl* spinControlWidth;
00463     wxSpinCtrl* spinControlHeight;
00464 
00465 private:
00466     DECLARE_EVENT_TABLE()
00467 };
00468 
00469 
00470 // Use this macro if you want to define your own main() or WinMain() function
00471 // and call wxEntry() from there.
00472 #define IMPLEMENT_PLAPP_NO_MAIN( appname )                                                                                                        \
00473     wxAppConsole * wxPLCreateApp()                                                                                                                \
00474     {                                                                                                                                             \
00475         wxAppConsole::CheckBuildOptions( WX_BUILD_OPTIONS_SIGNATURE,                                                                              \
00476             "your program" );                                                                                                                     \
00477         return new appname;                                                                                                                       \
00478     }                                                                                                                                             \
00479     wxAppInitializer                                                                                                                              \
00480     wxAppInitializer( (wxAppInitializerFunction) ( wxApp::GetInitializerFunction() == NULL ? wxPLCreateApp : wxApp::GetInitializerFunction() ) ); \
00481     DECLARE_PLAPP( appname )                                                                                                                      \
00482     appname & wxPLGetApp() { return *wx_static_cast( appname *, wxApp::GetInstance() ); }
00483 
00484 #define DECLARE_PLAPP( appname )    extern appname &wxPLGetApp();
00485 
00486 // workaround against warnings for unused variables
00487 static inline void Use( void * )
00488 {
00489 }
00490 #define WX_SUPPRESS_UNUSED_WARN( x )    Use( &x )
00491 
00492 
00493 //--------------------------------------------------------------------------
00494 //  Declarations for the device.
00495 //--------------------------------------------------------------------------
00496 
00497 void plD_init_wxwidgets( PLStream * );
00498 void plD_init_wxpng( PLStream * );
00499 void plD_line_wxwidgets( PLStream *, short, short, short, short );
00500 void plD_polyline_wxwidgets( PLStream *, short *, short *, PLINT );
00501 void plD_eop_wxwidgets( PLStream * );
00502 void plD_bop_wxwidgets( PLStream * );
00503 void plD_tidy_wxwidgets( PLStream * );
00504 void plD_state_wxwidgets( PLStream *, PLINT );
00505 void plD_esc_wxwidgets( PLStream *, PLINT, void * );
00506 
00507 void wx_set_dc( PLStream* pls, wxDC* dc );
00508 void wx_set_buffer( PLStream* pls, wxImage* buffer );
00509 void wx_set_size( PLStream* pls, int width, int height );
00510 int plD_errorexithandler_wxwidgets( char *errormessage );
00511 void plD_erroraborthandler_wxwidgets( char *errormessage );
00512 
00513 //--------------------------------------------------------------------------
00514 //  Debug functions
00515 //--------------------------------------------------------------------------
00516 
00517 // define if you want debug output
00518 // #define _DEBUG //
00519 // #define _DEBUG_VERBOSE //
00520 void Log_Verbose( const char *fmt, ... );
00521 void Log_Debug( const char *fmt, ... );
00522 
00523 
00524 //--------------------------------------------------------------------------
00525 // Font style and weight lookup tables
00526 //--------------------------------------------------------------------------
00527 const wxFontFamily fontFamilyLookup[5] = {
00528     wxFONTFAMILY_SWISS,      // sans-serif
00529     wxFONTFAMILY_ROMAN,      // serif
00530     wxFONTFAMILY_TELETYPE,   // monospace
00531     wxFONTFAMILY_SCRIPT,     // script
00532     wxFONTFAMILY_SWISS       // symbol
00533 };
00534 
00535 const int          fontStyleLookup[3] = {
00536     wxFONTFLAG_DEFAULT,      // upright
00537     wxFONTFLAG_ITALIC,       // italic
00538     wxFONTFLAG_SLANT         // oblique
00539 };
00540 
00541 const int          fontWeightLookup[2] = {
00542     wxFONTFLAG_DEFAULT,     // medium
00543     wxFONTFLAG_BOLD         // bold
00544 };
00545 
00546 #endif // __WXWIDGETS_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines