00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "a2dprec.h"
00012
00013 #ifdef __BORLANDC__
00014 #pragma hdrstop
00015 #endif
00016
00017 #ifndef WX_PRECOMP
00018 #include "wx/wx.h"
00019 #endif
00020
00021 #include "wx/canvas/candoc.h"
00022 #include "wx/canvas/cansim.h"
00023 #include "wx/canvas/tools.h"
00024 #include "wx/artbase/dcdrawer.h"
00025
00026 #include <wx/wfstream.h>
00027
00028
00029
00030
00031
00032
00033 #define WIDTHSCROLLBAR 20
00034
00035
00036
00037
00038
00039
00040
00041 IMPLEMENT_CLASS(a2dCanvasSim,a2dDocumentViewScrolledWindow)
00042
00043 BEGIN_EVENT_TABLE(a2dCanvasSim,a2dDocumentViewScrolledWindow)
00044 EVT_PAINT( a2dCanvasSim::OnPaint )
00045 EVT_ERASE_BACKGROUND( a2dCanvasSim::OnEraseBackground )
00046 EVT_SIZE( a2dCanvasSim::OnSize )
00047 END_EVENT_TABLE()
00048
00049
00050 a2dCanvasSim::a2dCanvasSim( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style ):
00051 a2dDocumentViewScrolledWindow( parent, id, pos, size, style )
00052 {
00053 m_border = 0;
00054 m_delta = 100;
00055
00056 int dvx = size.GetWidth();
00057 int dvy = size.GetHeight();
00058
00059 if ( size == wxDefaultSize )
00060 {
00061 dvx = 1000;
00062 dvy = 1000;
00063 }
00064 else
00065 GetClientSize(&dvx,&dvy);
00066
00067 a2dCanvasView* view = new a2dCanvasView( dvx, dvy );
00068 a2dDrawer2D* drawer2d = new a2dMemDcDrawer( dvx, dvy );
00069 view->SetDisplayWindow( this );
00070 view->SetDrawer2D( drawer2d );
00071 SetView( view );
00072
00073 m_frozen = false;
00074
00075
00076 m_doc = new a2dCanvasDocument();
00077 m_view->SetDocument( m_doc );
00078 }
00079
00080
00081 void a2dCanvasSim::ClearBackground()
00082 {
00083 SetBackgroundColour( GetCanvasView()->GetBackgroundFill().GetColour() );
00084
00085 wxWindow::ClearBackground();
00086 }
00087
00088 void a2dCanvasSim::Refresh( bool eraseBackground, const wxRect* rect )
00089 {
00090 if ( m_view && !rect )
00091 {
00092 m_view->Update( a2dCANVIEW_UPDATE_ALL );
00093 }
00094 wxWindow::Refresh( eraseBackground, rect );
00095 }
00096
00097 a2dCanvasSim::~a2dCanvasSim()
00098 {
00099 if ( !m_view )
00100 return;
00101 GetCanvasView()->Close();
00102 }
00103
00104 a2dCanvasDocument* a2dCanvasSim::GetCanvasDocument()
00105 {
00106 if (!m_view)
00107 return NULL;
00108 return GetCanvasView()->GetCanvasDocument();
00109 }
00110
00111 a2dCanvasObject* a2dCanvasSim::SetShowObject(const wxString& name)
00112 {
00113 if ( !GetCanvasView() )
00114 return false;
00115 return GetCanvasView()->SetShowObject(name);
00116 }
00117
00118 bool a2dCanvasSim::SetShowObject(a2dCanvasObject* obj)
00119 {
00120 if ( !GetCanvasView() )
00121 return false;
00122 return GetCanvasView()->SetShowObject(obj);
00123 }
00124
00125 void a2dCanvasSim::SetBackgroundFill( const a2dFill& backgroundfill )
00126 {
00127 if ( !GetCanvasView() )
00128 return;
00129 GetCanvasView()->SetBackgroundFill( backgroundfill );
00130
00131 m_view->Update( a2dCANVIEW_UPDATE_ALL );
00132 }
00133
00134 bool a2dCanvasSim::GetYaxis() const
00135 {
00136 if ( !GetCanvasView() )
00137 return true;
00138 return GetDrawer2D()->GetYaxis();
00139 }
00140
00141 void a2dCanvasSim::Freeze()
00142 {
00143 wxWindow::Freeze();
00144 m_frozen = true;
00145 }
00146
00147 void a2dCanvasSim::Thaw()
00148 {
00149 wxWindow::Thaw();
00150 m_frozen = false;
00151 }
00152
00153 void a2dCanvasSim::OnPaint(wxPaintEvent& WXUNUSED(event) )
00154 {
00155 wxPaintDC dc( this );
00156
00157
00158
00159
00160 if ( !GetCanvasView() || m_view->IsClosed() )
00161 {
00162 return;
00163 }
00164
00165 if (GetCanvasView()->IsFrozen()) return;
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 bool eventHandlerEnabledState = GetCanvasView()->GetEvtHandlerEnabled();
00180 if ( !eventHandlerEnabledState )
00181 GetCanvasView()->SetEvtHandlerEnabled(true);
00182
00183
00184
00185
00186 GetCanvasView()->Update( a2dCANVIEW_UPDATE_AREAS );
00187
00188
00189 GetCanvasView()->SetEvtHandlerEnabled(eventHandlerEnabledState);
00190
00191
00192 GetDrawer2D()->BeginDraw();
00193
00194
00195
00196 wxRegionIterator it( GetUpdateRegion() );
00197 while (it)
00198 {
00199 int x = it.GetX();
00200 int y = it.GetY();
00201
00202 int w = it.GetWidth();
00203 int h = it.GetHeight();
00204
00205 int xx;
00206 int yy;
00207 CalcUnscrolledPosition( x, y, &xx, &yy);
00208 GetDrawer2D()->BlitBuffer( xx, yy, w, h );
00209
00210 it++;
00211 }
00212 GetDrawer2D()->EndDraw();
00213 }
00214
00215 void a2dCanvasSim::SetMouseEvents(bool onoff)
00216 {
00217 if ( !m_view )
00218 return;
00219
00220 GetCanvasView()->SetMouseEvents( onoff );
00221 }
00222
00223 void a2dCanvasSim::OnEraseBackground(wxEraseEvent& WXUNUSED(event) )
00224 {
00225 }
00226
00227 a2dCanvasObject* a2dCanvasSim::IsHitWorld(
00228 double x, double y,
00229 int layer,
00230 a2dHitOption option
00231 )
00232 {
00233 if ( !m_view )
00234 return NULL;
00235 return GetCanvasView()->IsHitWorld( x, y, layer, option );
00236 }
00237
00238 bool a2dCanvasSim::WriteSVG(const wxString& filename, double Width, double Height, wxString unit)
00239 {
00240 if ( !m_view || !GetDrawer2D() )
00241 return false;
00242
00243
00244 return GetCanvasView()->GetCanvasDocument()->WriteSVG( GetCanvasView(), filename, GetCanvasView()->GetShowObject(), Width, Height, unit);
00245 }
00246
00247 void a2dCanvasSim::SetMappingWidthHeight( double vx1, double vy1, double width, double height )
00248 {
00249 int dxn,dyn;
00250 GetVirtualSize(&dxn,&dyn);
00251
00252 if ( dxn == 0 ) dxn = 1000;
00253 if ( dyn == 0 ) dyn = 1000;
00254
00255 double xupp=width/dxn;
00256 double yupp=height/dyn;
00257
00258 if (yupp==0 || xupp==0)
00259 {
00260 yupp=1;xupp=1;
00261 }
00262
00263 if (yupp > xupp)
00264 SetMappingUpp(vx1,vy1,yupp,yupp);
00265 else
00266 SetMappingUpp(vx1,vy1,xupp,xupp);
00267 }
00268
00269
00270
00271 void a2dCanvasSim::SetMappingUpp( double vx1, double vy1, double xpp, double ypp)
00272 {
00273 if ( !m_view || !GetDrawer2D() )
00274 return;
00275
00276 int dxn,dyn;
00277 GetVirtualSize(&dxn,&dyn);
00278
00279 if ( dxn == 0 ) dxn = 1000;
00280 if ( dyn == 0 ) dyn = 1000;
00281
00282 GetDrawer2D()->SetMappingDeviceRect( 0, 0, dxn, dyn );
00283 GetDrawer2D()->SetMappingUpp( vx1, vy1, xpp, ypp );
00284 }
00285
00286 void a2dCanvasSim::SetYaxis(bool up)
00287 {
00288 if ( !m_view || !GetDrawer2D() )
00289 return;
00290
00291 GetDrawer2D()->SetYaxis( up);
00292
00293 m_view->Update( a2dCANVIEW_UPDATE_ALL );
00294 }
00295
00296 bool a2dCanvasSim::GetMouseEvents()
00297 {
00298 if ( !m_view || !GetDrawer2D() )
00299 return false;
00300
00301 return GetCanvasView()->GetMouseEvents();
00302 }
00303
00304
00305 void a2dCanvasSim::OnSize(wxSizeEvent& WXUNUSED(event) )
00306 {
00307 if ( !m_view )
00308 return;
00309
00310 if (!GetDrawer2D())
00311 return;
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 int oldw = GetDrawer2D()->GetBuffer().GetWidth();
00322 int oldh = GetDrawer2D()->GetBuffer().GetHeight();
00323
00324 int w,h;
00325
00326 GetVirtualSize( &w, &h );
00327
00328
00329
00330 w = w+WIDTHSCROLLBAR+m_delta;
00331 h = h+WIDTHSCROLLBAR+m_delta;
00332
00333 if (abs(oldw-w) > m_delta || abs(oldh-h) > m_delta)
00334 {
00335
00336
00337 m_view->Update( a2dCANVIEW_UPDATE_AREAS | a2dCANVIEW_UPDATE_BLIT );
00338
00339 GetCanvasView()->SetBufferSize( w, h );
00340
00341
00342
00343
00344 if (GetDrawer2D()->GetYaxis())
00345 {
00346 if (oldw < w)
00347 GetCanvasView()->AddPendingUpdateArea(oldw,0, w-oldw, h);
00348 if (oldh < h)
00349 GetCanvasView()->AddPendingUpdateArea(0,oldh,w, h-oldh);
00350 }
00351 else
00352 {
00353 if (oldw < w)
00354 GetCanvasView()->AddPendingUpdateArea(oldw,0, w-oldw, h);
00355 if (oldh < h)
00356 GetCanvasView()->AddPendingUpdateArea(0,oldh,w, h-oldh);
00357 }
00358 }
00359 m_view->Update( a2dCANVIEW_UPDATE_ALL | a2dCANVIEW_UPDATE_BLIT );
00360 }
00361
00362 void a2dCanvasSim::SetMappingShowAll( bool centre )
00363 {
00364 if ( !m_view || !GetDrawer2D() )
00365 return;
00366
00367 if (!GetShowObject() || !GetCanvasDocument())
00368 return;
00369
00370 m_view->Update( a2dCANVIEW_UPDATE_OLDNEW );
00371 a2dBoundingBox untr = GetShowObject()->GetBbox();
00372 a2dAffineMatrix cworld = GetShowObject()->GetTransformMatrix();
00373 cworld.Invert();
00374 untr.MapBbox(cworld);
00375
00376 double w = untr.GetWidth();
00377 double h = untr.GetHeight();
00378
00379 if ( w == 0 )
00380 w=1000;
00381 if ( h== 0 )
00382 h=1000;
00383
00384 double uppx, uppy;
00385 int clientw, clienth;
00386 GetVirtualSize( &clientw, &clienth );
00387
00388
00389 if ( clientw - m_border > 0 )
00390 uppx = w/(clientw - m_border);
00391 else
00392 uppx = w/clientw;
00393
00394
00395 if ( clienth - m_border > 0 )
00396 uppy = h/(clienth - m_border);
00397 else
00398 uppy = h/clienth;
00399
00400
00401 if ( uppy > uppx )
00402 uppx = uppy;
00403
00404 GetDrawer2D()->StartRefreshDisplayDisable();
00405 GetDrawer2D()->SetMappingDeviceRect( 0, 0, clientw, clienth);
00406
00407 if ( !centre )
00408 {
00409 SetMappingWidthHeight( untr.GetMinX(),
00410 untr.GetMinY(),
00411 w,
00412 h
00413 );
00414 }
00415 else
00416 {
00417 double middlexworld = untr.GetMinX()+w/2.0;
00418 double middleyworld = untr.GetMinY()+h/2.0;
00419 GetDrawer2D()->SetMappingUpp( middlexworld - clientw/2.0*uppx, middleyworld - clienth/2.0*uppx, uppx, uppx );
00420 }
00421
00422
00423 int clientwNew, clienthNew;
00424 GetVirtualSize( &clientwNew, &clienthNew );
00425 if ( clientw != clientwNew || clienth != clienthNew )
00426 {
00427
00428 if ( clientw - m_border > 0 )
00429 uppx = w/(clientw - m_border);
00430 else
00431 uppx = w/clientw;
00432
00433
00434 if ( clienth - m_border > 0 )
00435 uppy = h/(clienth - m_border);
00436 else
00437 uppy = h/clienth;
00438
00439
00440 if ( uppy > uppx )
00441 uppx = uppy;
00442
00443 GetDrawer2D()->SetMappingDeviceRect( 0, 0, clientwNew, clienthNew );
00444 if ( !centre )
00445 {
00446 SetMappingWidthHeight( untr.GetMinX(),
00447 untr.GetMinY(),
00448 w,
00449 h
00450 );
00451 }
00452 else
00453 {
00454 double middlexworld = untr.GetMinX()+w/2.0;
00455 double middleyworld = untr.GetMinY()+h/2.0;
00456 GetDrawer2D()->SetMappingUpp( middlexworld - clientwNew/2.0*uppx, middleyworld - clienthNew/2.0*uppx, uppx, uppx );
00457 }
00458 }
00459
00460 m_view->Update( a2dCANVIEW_UPDATE_ALL );
00461 GetDrawer2D()->EndRefreshDisplayDisable();
00462 Refresh();
00463 }