00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "docviewprec.h"
00024
00025 #ifdef __BORLANDC__
00026 #pragma hdrstop
00027 #endif
00028
00029
00030
00031 #ifndef WX_PRECOMP
00032 #include "wx/wx.h"
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #endif
00046
00047
00048 #ifdef __WXGTK__
00049 #include "wx/mdi.h"
00050 #endif
00051
00052 #if wxUSE_PRINTING_ARCHITECTURE
00053 #include "wx/prntbase.h"
00054 #include "wx/printdlg.h"
00055 #endif
00056
00057 #include "wx/msgdlg.h"
00058 #include "wx/choicdlg.h"
00059 #include "wx/confbase.h"
00060 #include "wx/file.h"
00061 #include "wx/cmdproc.h"
00062 #include "wx/log.h"
00063 #include "wx/tokenzr.h"
00064 #include <wx/xrc/xmlres.h>
00065
00066 #include <stdio.h>
00067 #include <string.h>
00068
00069 #include "wx/docview.h"
00070
00071
00072
00073
00074
00075 #include "wx/docview/docviewref.h"
00076 #include "wx/docview/doccom.h"
00077
00078 #if wxUSE_STD_IOSTREAM
00079 #include "wx/ioswrap.h"
00080 #if wxUSE_IOSTREAMH
00081 #include <fstream.h>
00082 #else
00083 #include <fstream>
00084 #endif
00085 #else
00086 #include "wx/wfstream.h"
00087 #endif
00088
00089
00090
00091 #include "wx/general/smrtptr.inl"
00092
00093
00094
00095
00096
00097
00098
00099 #ifdef _MSC_VER
00100 #pragma warning(disable: 4660)
00101 #endif
00102
00103
00104 template class A2DDOCVIEWDLLEXP a2dSmrtPtr<a2dView>;
00105 template class A2DDOCVIEWDLLEXP a2dSmrtPtr<a2dDocument>;
00106 template class A2DDOCVIEWDLLEXP a2dSmrtPtr<a2dIOHandler>;
00107 template class A2DDOCVIEWDLLEXP a2dlist<class a2dSmrtPtr<class a2dDocument> >;
00108 template class A2DDOCVIEWDLLEXP a2dSmrtPtrList<a2dDocument>;
00109 template class A2DDOCVIEWDLLEXP a2dlist<class a2dSmrtPtr<class a2dView> >;
00110 template class A2DDOCVIEWDLLEXP a2dSmrtPtrList<a2dView>;
00111 template class A2DDOCVIEWDLLEXP a2dlist<class a2dSmrtPtr<class a2dDocumentTemplate> >;
00112 template class A2DDOCVIEWDLLEXP a2dSmrtPtrList<a2dDocumentTemplate>;
00113 template class A2DDOCVIEWDLLEXP a2dlist<class a2dSmrtPtr<class a2dViewTemplate> >;
00114 template class A2DDOCVIEWDLLEXP a2dSmrtPtrList<a2dViewTemplate>;
00115
00116
00117 #ifdef _MSC_VER
00118 #pragma warning(default: 4660)
00119 #endif
00120
00121
00122
00123
00124
00125 IMPLEMENT_CLASS(a2dMenuIdItem, wxMenuItem)
00126 IMPLEMENT_ABSTRACT_CLASS(a2dDocument, wxEvtHandler)
00127 IMPLEMENT_ABSTRACT_CLASS(a2dView, wxEvtHandler)
00128 IMPLEMENT_CLASS(a2dDocumentTemplate, wxObject)
00129 IMPLEMENT_CLASS(a2dDocumentTemplateAuto, a2dDocumentTemplate)
00130 IMPLEMENT_CLASS(a2dViewTemplate, wxObject)
00131
00132 IMPLEMENT_ABSTRACT_CLASS(a2dViewConnector,wxEvtHandler)
00133 IMPLEMENT_ABSTRACT_CLASS(a2dFrameViewConnector,a2dViewConnector)
00134
00135 IMPLEMENT_CLASS(a2dCloseViewEvent, wxEvent)
00136 IMPLEMENT_CLASS(a2dDocumentEvent, wxEvent)
00137 IMPLEMENT_CLASS(a2dViewEvent, wxEvent)
00138 IMPLEMENT_CLASS(a2dTemplateEvent, wxEvent)
00139
00140 IMPLEMENT_CLASS(a2dCloseDocumentEvent, wxCloseEvent)
00141
00142 DEFINE_EVENT_TYPE(wxEVT_DISCONNECT_ALLVIEWS)
00143 DEFINE_EVENT_TYPE(wxEVT_SAVE_DOCUMENT)
00144 DEFINE_EVENT_TYPE(wxEVT_SAVEAS_DOCUMENT)
00145 DEFINE_EVENT_TYPE(wxEVT_EXPORT_DOCUMENT)
00146 DEFINE_EVENT_TYPE(wxEVT_IMPORT_DOCUMENT)
00147 DEFINE_EVENT_TYPE(wxEVT_CLOSE_DOCUMENT)
00148 DEFINE_EVENT_TYPE(wxEVT_OPEN_DOCUMENT)
00149 DEFINE_EVENT_TYPE(wxEVT_NEW_DOCUMENT)
00150 DEFINE_EVENT_TYPE(wxEVT_CREATE_DOCUMENT)
00151 DEFINE_EVENT_TYPE(wxEVT_ADD_VIEW)
00152 DEFINE_EVENT_TYPE(wxEVT_REMOVE_VIEW)
00153 DEFINE_EVENT_TYPE(wxEVT_UPDATE_VIEWS)
00154 DEFINE_EVENT_TYPE(wxEVT_ENABLE_VIEWS)
00155 DEFINE_EVENT_TYPE(wxEVT_DISCONNECT_VIEW)
00156
00157 DEFINE_EVENT_TYPE(wxEVT_REPORT_VIEWS)
00158 DEFINE_EVENT_TYPE(wxEVT_CHANGEDFILENAME_DOCUMENT)
00159 DEFINE_EVENT_TYPE(wxEVT_CHANGEDTITLE_DOCUMENT)
00160 DEFINE_EVENT_TYPE(wxEVT_CHANGEDMODIFY_DOCUMENT)
00161 DEFINE_EVENT_TYPE(wxEVT_POST_LOAD_DOCUMENT)
00162
00163 DEFINE_EVENT_TYPE(wxEVT_CLOSE_VIEW)
00164 DEFINE_EVENT_TYPE(wxEVT_CREATE_VIEW)
00165 DEFINE_EVENT_TYPE(wxEVT_SET_DOCUMENT)
00166 DEFINE_EVENT_TYPE(wxEVT_ACTIVATE_VIEW)
00167 DEFINE_EVENT_TYPE(wxEVT_ACTIVATE_VIEW_SENT_FROM_CHILD)
00168 DEFINE_EVENT_TYPE(wxEVT_ENABLE_VIEW)
00169 DEFINE_EVENT_TYPE(wxEVT_CHANGEDFILENAME_VIEW)
00170 DEFINE_EVENT_TYPE(wxEVT_CHANGEDTITLE_VIEW)
00171
00172 DEFINE_EVENT_TYPE( wxEVT_PRE_ADD_DOCUMENT )
00173 DEFINE_EVENT_TYPE( wxEVT_POST_CREATE_DOCUMENT )
00174 DEFINE_EVENT_TYPE( wxEVT_POST_CREATE_VIEW )
00175
00176 #if wxUSE_PRINTING_ARCHITECTURE
00177 IMPLEMENT_DYNAMIC_CLASS(a2dDocumentPrintout, wxPrintout)
00178 #endif
00179
00180
00181
00182
00183
00184 static inline wxString FindExtension(const wxChar *path);
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 static wxString FindExtension(const wxChar *path)
00199 {
00200 wxString ext;
00201 wxFileName::SplitPath( path, NULL, NULL, &ext);
00202
00203 #ifdef __WXMSW__
00204
00205
00206 return ext;
00207 #else
00208 return ext;
00209 #endif
00210 }
00211
00212
00213
00214
00215
00216 const a2dMenuIdItem a2dMenuIdItem::sm_noCmdMenuId = a2dMenuIdItem( wxT("NoCommandId") );
00217 bool a2dMenuIdItem::m_bitmapinitialized = false;
00218
00219 a2dMenuIdItemMap& a2dMenuIdItem::GetHashMap()
00220 {
00221 static a2dMemoryCriticalSectionHelper helper;
00222 static a2dMenuIdItemMap ms_Name2Id;
00223 return ms_Name2Id;
00224 }
00225
00226 a2dMenuIdItem::a2dMenuIdItem( const wxString& menuIdName, const wxString& text, const wxString& help, wxItemKind kind )
00227 {
00228 m_isEnabled = true;
00229 m_isChecked = false;
00230 m_id = wxXmlResource::GetXRCID( menuIdName );
00231 m_kind = kind;
00232 if (m_id == wxID_ANY)
00233 m_id = wxNewId();
00234 if (m_id == wxID_SEPARATOR)
00235 m_kind = wxITEM_SEPARATOR;
00236
00237 SetText(text);
00238 SetHelp(help);
00239
00240 m_name = menuIdName;
00241 if ( GetHashMap().find( m_name ) == GetHashMap().end())
00242 {
00243 GetHashMap()[ m_name ] = this;
00244 }
00245 else
00246 wxFAIL_MSG( wxT( "duplicate id" ) );
00247 }
00248
00249 void a2dMenuIdItem::InitializeBitmaps()
00250 {
00251 a2dMenuIdItemMap::iterator iter;
00252 for( iter = GetHashMap().begin(); iter != GetHashMap().end(); ++iter )
00253 {
00254 a2dMenuIdItem& item = *(iter->second);
00255 wxString art2d = a2dGeneralGlobals->GetWxArt2DArtVar();
00256 art2d += wxT("resources/") ;
00257
00258 wxImage image;
00259 if ( wxFileExists( art2d + item.m_name + wxT(".ico") ) )
00260 {
00261 if ( image.LoadFile( art2d + item.m_name + wxT(".ico"), wxBITMAP_TYPE_ICO ) )
00262 {
00263 item.m_bmpUnchecked = wxBitmap( image );
00264 wxASSERT_MSG( item.m_bmpUnchecked.Ok(),
00265 _T("Bitmap for a2dMenuIdItem wrong") );
00266
00267 }
00268 }
00269 if ( wxFileExists( art2d + item.m_name + wxT(".bmp") ) )
00270 {
00271 if ( image.LoadFile( art2d + item.m_name + wxT(".bmp"), wxBITMAP_TYPE_BMP ) )
00272 {
00273 item.m_bmpUnchecked = wxBitmap( image );
00274 wxASSERT_MSG( item.m_bmpUnchecked.Ok(),
00275 _T("Bitmap for a2dMenuIdItem wrong") );
00276
00277 }
00278 }
00279 if ( wxFileExists( art2d + item.m_name + wxT("_Checked.ico") ) )
00280 {
00281 if ( image.LoadFile( art2d + item.m_name + wxT("_Checked.ico"), wxBITMAP_TYPE_ICO ) )
00282 {
00283 item.m_bmpChecked = wxBitmap( image );
00284 wxASSERT_MSG( item.m_bmpChecked.Ok(),
00285 _T("Bitmap for a2dMenuIdItem wrong") );
00286 }
00287 }
00288 if ( wxFileExists( art2d + item.m_name + wxT("_Checked.bmp") ) )
00289 {
00290 if ( image.LoadFile( art2d + item.m_name + wxT("_Checked.bmp"), wxBITMAP_TYPE_BMP ) )
00291 {
00292 item.m_bmpChecked = wxBitmap( image );
00293 wxASSERT_MSG( item.m_bmpChecked.Ok(),
00294 _T("Bitmap for a2dMenuIdItem wrong") );
00295 }
00296 }
00297 }
00298 m_bitmapinitialized = true;
00299 }
00300
00301 const a2dMenuIdItem& a2dMenuIdItem::GetItemByName( const wxString& menuIdName )
00302 {
00303 a2dMenuIdItemMap::iterator iterCommand = GetHashMap().find(menuIdName);
00304 return iterCommand != GetHashMap().end() ? *iterCommand->second : a2dMenuIdItem::sm_noCmdMenuId;
00305 }
00306
00307 #if wxUSE_ACCEL
00308
00309 wxAcceleratorEntry *a2dMenuIdItem::GetAccel() const
00310 {
00311 return wxAcceleratorEntry::Create(GetText());
00312 }
00313
00314 void a2dMenuIdItem::SetAccel(wxAcceleratorEntry *accel)
00315 {
00316 wxString text = m_text.BeforeFirst(wxT('\t'));
00317 if ( accel )
00318 {
00319 text += wxT('\t');
00320 text += accel->ToString();
00321 }
00322
00323 SetText(text);
00324 }
00325
00326 #endif // wxUSE_ACCEL
00327
00328 void a2dMenuIdItem::SetText(const wxString& str)
00329 {
00330 m_text = str;
00331 }
00332
00333 void a2dMenuIdItem::SetHelp(const wxString& str)
00334 {
00335 m_help = str;
00336 }
00337
00338 wxString a2dMenuIdItem::GetLabelText(const wxString& label)
00339 {
00340 return wxStripMenuCodes(label);
00341 }
00342
00343
00344
00345
00346
00347 const a2dTemplateFlagMask a2dTemplateFlag::NON = 0x00000000;
00348 const a2dTemplateFlagMask a2dTemplateFlag::VISIBLE = 0x00000001;
00349 const a2dTemplateFlagMask a2dTemplateFlag::MARK = 0x00000002;
00350 const a2dTemplateFlagMask a2dTemplateFlag::LOAD = 0x00000004;
00351 const a2dTemplateFlagMask a2dTemplateFlag::SAVE = 0x00000008;
00352 const a2dTemplateFlagMask a2dTemplateFlag::IMPORTING = 0x00000010;
00353 const a2dTemplateFlagMask a2dTemplateFlag::EXPORTING = 0x00000020;
00354 const a2dTemplateFlagMask a2dTemplateFlag::ALL = 0x00000040;
00355 const a2dTemplateFlagMask a2dTemplateFlag::DEFAULT = a2dTemplateFlag::VISIBLE |
00356 a2dTemplateFlag::LOAD | a2dTemplateFlag::SAVE | a2dTemplateFlag::IMPORTING | a2dTemplateFlag::EXPORTING;
00357 const a2dTemplateFlagMask a2dTemplateFlag::LOAD_IMPORT = a2dTemplateFlag::VISIBLE |
00358 a2dTemplateFlag::LOAD | a2dTemplateFlag::IMPORTING;
00359 const a2dTemplateFlagMask a2dTemplateFlag::SAVE_EXPORT = a2dTemplateFlag::VISIBLE |
00360 a2dTemplateFlag::SAVE | a2dTemplateFlag::EXPORTING;
00361
00362 a2dTemplateFlag::a2dTemplateFlag( a2dTemplateFlagMask newmask )
00363 {
00364 SetFlags( newmask );
00365 }
00366
00367 bool a2dTemplateFlag::CheckMask( a2dTemplateFlagMask mask)
00368 {
00369 if ( mask == a2dTemplateFlag::NON )
00370 return true;
00371 if ( mask == a2dTemplateFlag::ALL )
00372 return true;
00373
00374
00375 if ( 0 < (mask & a2dTemplateFlag::VISIBLE) && !m_visible ) return false;
00376 if ( 0 < (mask & a2dTemplateFlag::MARK) && !m_mark ) return false;
00377 if ( 0 < (mask & a2dTemplateFlag::LOAD) && !m_load ) return false;
00378 if ( 0 < (mask & a2dTemplateFlag::SAVE) && !m_save ) return false;
00379 if ( 0 < (mask & a2dTemplateFlag::IMPORTING) && !m_import ) return false;
00380 if ( 0 < (mask & a2dTemplateFlag::EXPORTING) && !m_export ) return false;
00381
00382 return true;
00383 }
00384
00385 void a2dTemplateFlag::SetFlags( a2dTemplateFlagMask which )
00386 {
00387 if ( which == a2dTemplateFlag::ALL )
00388 {
00389 m_visible = true;
00390 m_mark = true;
00391 m_load = true;
00392 m_save = true;
00393 m_import = true;
00394 m_export = true;
00395
00396 return;
00397 }
00398
00399 m_visible = 0 < (which & a2dTemplateFlag::VISIBLE);
00400 m_mark = 0 < (which & a2dTemplateFlag::MARK);
00401 m_load = 0 < (which & a2dTemplateFlag::LOAD);
00402 m_save = 0 < (which & a2dTemplateFlag::SAVE);
00403 m_import = 0 < (which & a2dTemplateFlag::IMPORTING);
00404 m_export = 0 < (which & a2dTemplateFlag::EXPORTING);
00405
00406 }
00407
00408 a2dTemplateFlagMask a2dTemplateFlag::GetFlags() const
00409 {
00410 a2dTemplateFlagMask oflags = a2dTemplateFlag::NON;
00411
00412 if (m_visible) oflags = oflags | a2dTemplateFlag::VISIBLE;
00413 if (m_mark) oflags = oflags | a2dTemplateFlag::MARK;
00414 if (m_load) oflags = oflags | a2dTemplateFlag::LOAD;
00415 if (m_save) oflags = oflags | a2dTemplateFlag::SAVE;
00416 if (m_import) oflags = oflags | a2dTemplateFlag::IMPORTING;
00417 if (m_export) oflags = oflags | a2dTemplateFlag::EXPORTING;
00418
00419 return oflags;
00420 }
00421
00422 bool a2dTemplateFlag::GetFlag( a2dTemplateFlagMask which ) const
00423 {
00424 switch ( which )
00425 {
00426 case a2dTemplateFlag::VISIBLE: return m_visible;
00427 case a2dTemplateFlag::MARK: return m_mark;
00428 case a2dTemplateFlag::LOAD: return m_load;
00429 case a2dTemplateFlag::SAVE: return m_save;
00430 case a2dTemplateFlag::IMPORTING: return m_import;
00431 case a2dTemplateFlag::EXPORTING: return m_export;
00432 default:
00433 wxFAIL_MSG( wxT("This Flag not implemented") );
00434 return false;
00435 }
00436 return false;
00437 }
00438
00439
00440
00441
00442
00443 A2D_BEGIN_EVENT_TABLE( a2dDocument, a2dEvtHandler )
00444 A2D_EVT_CLOSE_VIEW( a2dDocument::OnCloseView )
00445 A2D_EVT_ADD_VIEW( a2dDocument::OnAddView )
00446 A2D_EVT_REMOVE_VIEW( a2dDocument::OnRemoveView )
00447 A2D_EVT_CLOSE_DOCUMENT( a2dDocument::OnCloseDocument )
00448 A2D_EVT_OPEN_DOCUMENT( a2dDocument::OnOpenDocument )
00449 A2D_EVT_SAVE_DOCUMENT( a2dDocument::OnSaveDocument )
00450 A2D_EVT_SAVEAS_DOCUMENT( a2dDocument::OnSaveAsDocument )
00451 A2D_EVT_EXPORT_DOCUMENT( a2dDocument::OnExportDocument )
00452 A2D_EVT_IMPORT_DOCUMENT( a2dDocument::OnImportDocument )
00453 A2D_EVT_NEW_DOCUMENT( a2dDocument::OnNewDocument )
00454 A2D_EVT_CREATE_DOCUMENT( a2dDocument::OnCreateDocument )
00455 A2D_END_EVENT_TABLE()
00456
00457 a2dDocument::a2dDocument( a2dDocument *parent)
00458 {
00459 m_askSaveWhenOwned = true;
00460
00461 m_documentModified = false;
00462 m_documentParent = parent;
00463 m_commandProcessor = (a2dCommandProcessor*) NULL;
00464
00465 m_documentTitle = wxT("");
00466
00467 m_documentTypeName = wxT("");
00468
00469 m_savedYet = false;
00470 m_isClosed = false;
00471 m_id = wxNewId();
00472 m_lastError = a2dError_NoError;
00473 }
00474
00475 a2dDocument::a2dDocument( const a2dDocument &other)
00476 :a2dEvtHandler()
00477 {
00478 m_askSaveWhenOwned = other.m_askSaveWhenOwned;
00479
00480 m_documentFile = other.m_documentFile;
00481 m_documentTitle = other.m_documentTitle;
00482 m_documentTypeName = other.m_documentTypeName;
00483 m_documentModified = other.m_documentModified;
00484 m_documentParent = other.m_documentParent;
00485 m_commandProcessor = other.m_commandProcessor;
00486 m_savedYet = other.m_savedYet;
00487 m_isClosed = other.m_isClosed;
00488 m_id = wxNewId();
00489 m_lastError = a2dError_NoError;
00490 }
00491
00492 void a2dDocument::DeleteContents()
00493 {
00494 return;
00495 }
00496
00497 a2dDocument::~a2dDocument()
00498 {
00499 DisConnectAllViews();
00500 DeleteContents();
00501 }
00502
00503 a2dObject* a2dDocument::Clone( a2dObject::CloneOptions options ) const
00504 {
00505 return new a2dDocument( *this );
00506 }
00507
00508 a2dObject* a2dDocument::SmrtPtrOwn()
00509 {
00510 m_refcount++;
00511 return this;
00512 }
00513
00514 bool a2dDocument::SmrtPtrRelease()
00515 {
00516 m_refcount--;
00517
00518 if ( m_refcount <= 0 )
00519 {
00520 SaveModifiedDialog();
00521 }
00522
00523 wxASSERT_MSG( m_refcount >= 0, wxT("a2dDocument::Release Negative reference count") );
00524 if ( m_refcount <= 0 )
00525 {
00526 delete this;
00527 return true;
00528 }
00529 return false;
00530 }
00531
00532 void a2dDocument::OnCloseView( a2dCloseViewEvent& event )
00533 {
00534
00535
00536 if ( m_refcount == 1 || m_askSaveWhenOwned )
00537 {
00538 if ( SaveModifiedDialog() != wxCANCEL )
00539 {
00540
00541
00542 }
00543 else if ( event.CanVeto() )
00544 event.Veto();
00545 }
00546 }
00547
00548 void a2dDocument::OnRemoveView( a2dDocumentEvent& event )
00549 {
00550
00551
00552 if ( m_refcount == 1 || m_askSaveWhenOwned )
00553 {
00554 if ( SaveModifiedDialog() != wxCANCEL )
00555 {
00556
00557
00558 }
00559 else
00560 event.Veto();
00561 }
00562 }
00563
00564 void a2dDocument::OnAddView( a2dDocumentEvent& WXUNUSED(event))
00565 {
00566 }
00567
00568 void a2dDocument::DisConnectAllViews()
00569 {
00570 a2dDocumentEvent eventremoveall( wxEVT_DISCONNECT_ALLVIEWS, m_documentFile );
00571 eventremoveall.SetEventObject(this);
00572
00573 ProcessEvent( eventremoveall );
00574
00575 DistributeEvent( eventremoveall );
00576 }
00577
00578 bool a2dDocument::Close( bool force )
00579 {
00580
00581 if ( m_isClosed )
00582 return true;
00583
00584 m_isClosed = true;
00585
00586 a2dCloseDocumentEvent event( m_id );
00587 event.SetEventObject(this);
00588 event.SetCanVeto(!force);
00589
00590
00591
00592 if ( ProcessEvent(event) && !event.GetVeto() )
00593 {
00594 return true;
00595 }
00596 m_isClosed = false;
00597 return false;
00598 }
00599
00600 void a2dDocument::OnCloseDocument( a2dCloseDocumentEvent& event )
00601 {
00602 if ( SaveModifiedDialog( event.CanVeto() ) != wxCANCEL )
00603 {
00604
00605
00606
00607
00608 a2dREFOBJECTPTR_KEEPALIVE;
00609
00610 DisConnectAllViews();
00611 DeleteContents();
00612
00613 GetCommandProcessor()->ClearCommands();
00614 Modify(false);
00615 }
00616 else if ( event.CanVeto() )
00617 event.Veto();
00618 else
00619 wxFAIL_MSG( wxT("You can not Cancel this Close Document event") );
00620 }
00621
00622 void a2dDocument::OnNewDocument( a2dDocumentEvent& WXUNUSED(event) )
00623 {
00624 m_isClosed = false;
00625 Modify(false);
00626 SetDocumentSaved(false);
00627
00628 wxString name = wxT("");
00629 a2dDocviewGlobals->GetDocviewCommandProcessor()->MakeDefaultName(name);
00630
00631 SetTitle(name);
00632
00633
00634 SetFilename(name, true);
00635 }
00636
00637 bool a2dDocument::Save()
00638 {
00639 a2dDocumentEvent event( wxEVT_SAVE_DOCUMENT, m_documentFile );
00640 event.SetFileName( m_documentFile );
00641 event.SetEventObject(this);
00642
00643 if ( ProcessEvent(event) && event.IsAllowed() )
00644 {
00645 return true;
00646 }
00647 return false;
00648 }
00649
00650 bool a2dDocument::SaveAs( const wxFileName& fileTosaveTo, a2dDocumentFlagMask flags )
00651 {
00652 wxString filename = fileTosaveTo.GetFullPath();
00653 a2dDocumentEvent event( wxEVT_SAVEAS_DOCUMENT, filename.IsEmpty()? m_documentFile: fileTosaveTo, flags );
00654
00655 event.SetEventObject(this);
00656
00657 if ( ProcessEvent(event) && event.IsAllowed() )
00658 {
00659 return true;
00660 }
00661 return false;
00662 }
00663
00664 void a2dDocument::OnSaveAsDocument( a2dDocumentEvent& event )
00665 {
00666 a2dDocumentTemplate* docTemplate = GetDocumentTemplate();
00667 if (! docTemplate )
00668 {
00669 a2dDocviewGlobals->ReportError( a2dError_NoDocTemplateRef );
00670 event.Veto();
00671 event.SetError( a2dError_NoDocTemplateRef );
00672 return;
00673 }
00674
00675 wxString fullname = event.GetFileName().GetFullPath();
00676
00677 if ( !( event.GetFlags() & a2dREFDOC_SILENT ) )
00678 {
00679 fullname = wxFileSelector(_("Save as"),
00680 docTemplate->GetDirectory(),
00681 fullname,
00682 docTemplate->GetDefaultExtension(),
00683 docTemplate->GetFileFilter(),
00684 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
00685 GetAssociatedWindow());
00686
00687 if (fullname.IsEmpty())
00688 {
00689 event.Veto();
00690 return;
00691 }
00692 }
00693
00694 wxString ext;
00695 wxFileName::SplitPath( fullname, NULL, NULL, &ext);
00696
00697 if (ext.IsEmpty() || ext == wxT(""))
00698 {
00699 fullname += wxT(".");
00700 fullname += docTemplate->GetDefaultExtension();
00701 }
00702
00703 #if wxUSE_STD_IOSTREAM
00704 a2dDocumentFileOutputStream store( fullname.mb_str(), wxSTD ios_base::out | wxSTD ios_base::binary );
00705 if (store.fail() || store.bad())
00706 {
00707 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for saving"), fullname.c_str() );
00708
00709 event.Veto();
00710 event.SetError( a2dError_FileCouldNotOpen );
00711 return;
00712 }
00713 #else
00714 wxFileOutputStream storeUnbuf( fullname );
00715 if ( storeUnbuf.GetLastError() != wxSTREAM_NO_ERROR )
00716 {
00717 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for saving"), fullname.c_str() );
00718
00719 event.Veto();
00720 event.SetError( a2dError_FileCouldNotOpen );
00721 return;
00722 }
00723 wxBufferedOutputStream store( storeUnbuf );
00724 #endif
00725
00726 wxBusyCursor wait;
00727 m_lastError = a2dError_NoError;
00728
00729 SaveObject( store, docTemplate->GetDocumentIOHandlerForSave( fullname, this ) );
00730 if ( m_lastError != a2dError_NoError )
00731 {
00732 a2dDocviewGlobals->ReportErrorF( a2dError_SaveFile, _("Sorry, could not save document to file %s"), fullname.c_str() );
00733
00734 event.Veto();
00735 event.SetError( m_lastError );
00736 return;
00737 }
00738 Modify(false);
00739
00740
00741 SetFilename(fullname, true);
00742
00743 a2dDocviewGlobals->GetDocviewCommandProcessor()->AddFileToHistory(fullname);
00744
00745 SetDocumentSaved(true);
00746 }
00747
00748 void a2dDocument::OnSaveDocument( a2dDocumentEvent& event )
00749 {
00750 wxString filetosave = event.GetFileName().GetFullPath();
00751
00752 if (!IsModified() && m_savedYet)
00753 {
00754 event.Veto();
00755 return;
00756 }
00757
00758 if ( filetosave.IsEmpty() || !m_savedYet )
00759 {
00760 if ( !SaveAs() )
00761 event.Veto();
00762 return;
00763 }
00764
00765 #if wxUSE_STD_IOSTREAM
00766 a2dDocumentFileOutputStream store( filetosave.mb_str(), wxSTD ios_base::out | wxSTD ios_base::binary );
00767 if (store.fail() || store.bad())
00768 {
00769 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for saving."), filetosave.c_str() );
00770
00771 event.Veto();
00772 event.SetError( a2dError_FileCouldNotOpen );
00773 return;
00774 }
00775 #else
00776 wxFileOutputStream storeUnbuf( filetosave );
00777 if ( storeUnbuf.GetLastError() != wxSTREAM_NO_ERROR )
00778 {
00779 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for saving."), filetosave.c_str() );
00780
00781 event.Veto();
00782 event.SetError( a2dError_FileCouldNotOpen );
00783 return;
00784 }
00785 wxBufferedOutputStream store( storeUnbuf );
00786 #endif
00787
00788 if (! GetDocumentTemplate() )
00789 {
00790 a2dDocviewGlobals->ReportError( a2dError_NoDocTemplateRef );
00791 event.Veto();
00792 event.SetError( a2dError_NoDocTemplateRef );
00793 return;
00794 }
00795
00796 wxBusyCursor wait;
00797 m_lastError = a2dError_NoError;
00798
00799 SaveObject(store, m_documentTemplate->GetDocumentIOHandlerForSave( filetosave, this ) );
00800 if ( m_lastError != a2dError_NoError )
00801 {
00802 a2dDocviewGlobals->ReportErrorF( a2dError_SaveFile, _("Sorry, could not save document to file %s"), filetosave.c_str() );
00803
00804 event.Veto();
00805 event.SetError( m_lastError );
00806 return;
00807 }
00808 Modify(false);
00809 SetFilename(filetosave);
00810 SetDocumentSaved(true);
00811 }
00812
00813 bool a2dDocument::Export( a2dDocumentTemplate* doctemplate, const wxFileName& fileTosaveTo, a2dDocumentFlagMask flags )
00814 {
00815 wxString filename = fileTosaveTo.GetFullPath();
00816 a2dDocumentEvent event( wxEVT_EXPORT_DOCUMENT, doctemplate, filename.IsEmpty()? m_documentFile: fileTosaveTo, flags );
00817 event.SetEventObject(this);
00818
00819 if ( ProcessEvent(event) && event.IsAllowed() )
00820 {
00821 return true;
00822 }
00823 return false;
00824 }
00825
00826 void a2dDocument::OnExportDocument( a2dDocumentEvent& event )
00827 {
00828 static a2dDocumentTemplate *lastSelectedDocTemplate = NULL;
00829 a2dDocumentTemplate *docTemplate = event.GetDocumentTemplate();
00830 wxString path = event.GetFileName().GetFullPath();
00831 if (!docTemplate)
00832 {
00833 wxASSERT_MSG( GetDocumentTemplate(), wxT("document has no a2dDocumentTemplate set") );
00834
00835
00836
00837 a2dDocumentTemplateList onlyThisDocTemplates;
00838 const a2dDocumentTemplateList& allDocTemplates = a2dDocviewGlobals->GetDocviewCommandProcessor()->GetDocTemplates();
00839 for( a2dDocumentTemplateList::const_iterator iter = allDocTemplates.begin(); iter != allDocTemplates.end(); ++iter )
00840 {
00841 a2dDocumentTemplateList::value_type temp = *iter;
00842 if ( temp->GetDocumentTypeName() == GetDocumentTemplate()->GetDocumentTypeName() &&
00843 ( !temp->GetDocumentIOHandlerStrOut() || temp->GetDocumentIOHandlerStrOut()->CanSave( this ) )
00844 )
00845 onlyThisDocTemplates.push_back( temp );
00846 }
00847 wxArrayString selectedPaths;
00848 a2dDocumentTemplate *fileTemplate = (a2dDocumentTemplate *) NULL;
00849 a2dError result = a2dDocviewGlobals->GetDocviewCommandProcessor()->SelectDocumentPath( onlyThisDocTemplates,
00850 path,
00851 a2dREFDOC_NON,
00852 &selectedPaths,
00853 &docTemplate,
00854 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
00855 a2dTemplateFlag::VISIBLE | a2dTemplateFlag::EXPORTING,
00856 lastSelectedDocTemplate);
00857 if( result == a2dError_NoError )
00858 {
00859 path = selectedPaths.Item(0);
00860 lastSelectedDocTemplate = docTemplate;
00861 }
00862 else if( result == a2dError_Canceled )
00863 {
00864 event.Veto();
00865 return;
00866 }
00867 else
00868 {
00869 a2dDocviewGlobals->ReportErrorF( a2dError_IOHandler, _("suitable template I/O handler for loading not available in document templates.") );
00870 event.Veto();
00871 event.SetError( a2dError_IOHandler );
00872 return;
00873 }
00874 }
00875 else if ( !( event.GetFlags() & a2dREFDOC_SILENT ) )
00876 {
00877 path = wxFileSelector(_("Save as"),
00878 docTemplate->GetDirectory(),
00879 path,
00880 docTemplate->GetDefaultExtension(),
00881 docTemplate->GetFileFilter(),
00882 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
00883 GetAssociatedWindow());
00884 }
00885 if (path.IsEmpty())
00886 {
00887 event.Veto();
00888 return;
00889 }
00890
00891 wxBusyCursor wait;
00892
00893 wxString fileName(path);
00894 wxString name, ext;
00895 wxFileName::SplitPath( fileName, & path, & name, & ext);
00896
00897 if (ext.IsEmpty() || ext == wxT(""))
00898 {
00899 fileName += wxT(".");
00900 fileName += docTemplate->GetDefaultExtension();
00901 }
00902
00903 if( !docTemplate )
00904 {
00905 a2dDocviewGlobals->ReportErrorF( a2dError_NoDocTemplateRef, _("Sorry, no document template for %s export."), fileName.c_str() );
00906
00907 event.Veto();
00908 event.SetError( a2dError_NoDocTemplateRef );
00909 return;
00910 }
00911
00912 #if wxUSE_STD_IOSTREAM
00913 a2dDocumentFileOutputStream store( fileName.mb_str(), wxSTD ios_base::out | wxSTD ios_base::binary );
00914 if (store.fail() || store.bad())
00915 {
00916 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for export."), fileName.c_str() );
00917
00918 event.Veto();
00919 event.SetError( a2dError_FileCouldNotOpen );
00920 return;
00921 }
00922 #else
00923 wxFileOutputStream storeUnbuf( fileName );
00924 if (storeUnbuf.GetLastError() != wxSTREAM_NO_ERROR)
00925 {
00926 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for export."), fileName.c_str() );
00927
00928 event.Veto();
00929 event.SetError( a2dError_FileCouldNotOpen );
00930 return;
00931 }
00932 wxBufferedOutputStream store( storeUnbuf );
00933 #endif
00934 m_lastError = a2dError_NoError;
00935
00936 SaveObject(store, docTemplate->GetDocumentIOHandlerForSave( fileName, this ) );
00937 if ( m_lastError != a2dError_NoError )
00938 {
00939 a2dDocviewGlobals->ReportErrorF( a2dError_ExportFile, _("Sorry, could not export document to file %s"), fileName.c_str() );
00940
00941 event.Veto();
00942 event.SetError( m_lastError );
00943 return;
00944 }
00945 }
00946
00947 bool a2dDocument::Import( a2dDocumentTemplate* doctemplate, const wxFileName& fileToImport, a2dDocumentFlagMask flags )
00948 {
00949 wxString filename = fileToImport.GetFullPath();
00950 a2dDocumentEvent event( wxEVT_IMPORT_DOCUMENT, doctemplate, filename, flags );
00951 event.SetEventObject(this);
00952
00953 if ( ProcessEvent(event) && event.IsAllowed() )
00954 {
00955 return true;
00956 }
00957 return false;
00958 }
00959
00960 void a2dDocument::OnImportDocument( a2dDocumentEvent& event )
00961 {
00962 static a2dDocumentTemplate *lastSelectedDocTemplate = NULL;
00963 a2dDocumentTemplate *docTemplate = event.GetDocumentTemplate();
00964 wxString path = event.GetFileName().GetFullPath();
00965
00966 wxFileName file = event.GetFileName();
00967
00968 if (!docTemplate)
00969 {
00970 wxASSERT_MSG( GetDocumentTemplate(), wxT("document has no a2dDocumentTemplate set") );
00971
00972
00973
00974 a2dDocumentTemplateList onlyThisDocTemplates;
00975 const a2dDocumentTemplateList& allDocTemplates = a2dDocviewGlobals->GetDocviewCommandProcessor()->GetDocTemplates();
00976 for( a2dDocumentTemplateList::const_iterator iter = allDocTemplates.begin(); iter != allDocTemplates.end(); ++iter )
00977 {
00978 a2dDocumentTemplateList::value_type temp = *iter;
00979 if ( temp->GetDocumentTypeName() == GetDocumentTemplate()->GetDocumentTypeName() )
00980 onlyThisDocTemplates.push_back( temp );
00981 }
00982 wxArrayString selectedPaths;
00983 a2dError result = a2dDocviewGlobals->GetDocviewCommandProcessor()->SelectDocumentPath( onlyThisDocTemplates,
00984 path,
00985 a2dREFDOC_NON,
00986 &selectedPaths,
00987 &docTemplate,
00988 wxFD_OPEN,
00989 a2dTemplateFlag::VISIBLE | a2dTemplateFlag::IMPORTING,
00990 lastSelectedDocTemplate);
00991 if( result == a2dError_NoError )
00992 {
00993 path = selectedPaths.Item(0);
00994 lastSelectedDocTemplate = docTemplate;
00995 }
00996 else if( result == a2dError_Canceled )
00997 {
00998 event.Veto();
00999 event.SetError( a2dError_Canceled );
01000 return;
01001 }
01002 else
01003 {
01004 a2dDocviewGlobals->ReportErrorF( a2dError_IOHandler, _("suitable template I/O handler for export not available in document templates.") );
01005 event.Veto();
01006 event.SetError( result );
01007 return;
01008 }
01009 }
01010 else if ( !( event.GetFlags() & a2dREFDOC_SILENT ) )
01011 {
01012 path = wxFileSelector(_("Import"),
01013 docTemplate->GetDirectory(),
01014 path,
01015 docTemplate->GetDefaultExtension(),
01016 docTemplate->GetFileFilter(),
01017 wxFD_OPEN,
01018 GetAssociatedWindow());
01019 }
01020
01021 if (path.IsEmpty())
01022 {
01023 event.Veto();
01024 event.SetError( a2dError_CouldNotEvaluatePath );
01025 return;
01026 }
01027
01028 wxString fileName(path);
01029 wxString name, ext;
01030 wxFileName::SplitPath( fileName, & path, & name, & ext);
01031
01032 if (ext.IsEmpty() || ext == wxT(""))
01033 {
01034 fileName += wxT(".");
01035 fileName += docTemplate->GetDefaultExtension();
01036 }
01037
01038 wxBusyCursor wait;
01039
01040 #if wxUSE_STD_IOSTREAM
01041 a2dDocumentFileInputStream store( fileName.mb_str(), wxSTD ios_base::in | wxSTD ios_base::binary );
01042 if (store.fail() || store.bad())
01043 {
01044 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for import"), fileName.c_str() );
01045 event.Veto();
01046 event.SetError( a2dError_FileCouldNotOpen );
01047 return;
01048 }
01049 #else
01050 wxFileInputStream storeUnbuf( fileName );
01051 if ( !storeUnbuf.Ok() )
01052 {
01053 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for import"), fileName.c_str() );
01054 event.Veto();
01055 event.SetError( a2dError_FileCouldNotOpen );
01056 return;
01057 }
01058 wxBufferedInputStream store( storeUnbuf );
01059 #endif
01060
01061 a2dIOHandlerStrIn* handler = docTemplate->GetDocumentIOHandlerForLoad( store, this );
01062
01063 m_lastError = a2dError_NoError;
01064
01065 #if wxUSE_STD_IOSTREAM
01066 LoadObject( store, handler );
01067 if ( m_lastError != a2dError_NoError || (!store && !store.eof()) )
01068 #else
01069 int res = LoadObject( store, handler ).GetLastError();
01070 if ( m_lastError != a2dError_NoError || ((res != wxSTREAM_NO_ERROR) && (res != wxSTREAM_EOF)) )
01071 #endif
01072 {
01073 a2dDocviewGlobals->ReportErrorF( a2dError_ExportFile, _("template I/O handler not capable of importing this format from file %s"), fileName.c_str() );
01074 event.Veto();
01075 event.SetError( m_lastError );
01076 }
01077 UpdateAllViews();
01078 }
01079
01080 void a2dDocument::OnOpenDocument( a2dDocumentEvent& event )
01081 {
01082 wxFileName file = event.GetFileName();
01083
01084 if ( SaveModifiedDialog() == wxCANCEL )
01085 {
01086 event.Veto();
01087 event.SetError( a2dError_Canceled );
01088 return;
01089 }
01090
01091 #if wxUSE_STD_IOSTREAM
01092 a2dDocumentFileInputStream store( file.GetFullPath().mb_str(), wxSTD ios_base::in | wxSTD ios_base::binary );
01093 if (store.fail() || store.bad())
01094 {
01095 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for loading"), file.GetFullPath().c_str() );
01096 event.Veto();
01097 event.SetError( a2dError_FileCouldNotOpen );
01098
01099 return;
01100 }
01101 #else
01102 wxFileInputStream storeUnbuf( file.GetFullPath() );
01103 if ( !storeUnbuf.Ok() )
01104 {
01105 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for loading"), file.GetFullPath().c_str() );
01106 event.Veto();
01107 event.SetError( a2dError_FileCouldNotOpen );
01108 return;
01109 }
01110 wxBufferedInputStream store( storeUnbuf );
01111 #endif
01112 if (! m_documentTemplate->GetAutoZeroPtrList() )
01113 {
01114 a2dDocviewGlobals->ReportError( a2dError_NoDocTemplateRef );
01115 event.Veto();
01116 event.SetError( a2dError_NoDocTemplateRef );
01117 return;
01118 }
01119
01120 a2dIOHandlerStrIn* handler = m_documentTemplate->GetDocumentIOHandlerForLoad( store, this );
01121
01122 wxBusyCursor wait;
01123
01124 m_lastError = a2dError_NoError;
01125
01126 #if wxUSE_STD_IOSTREAM
01127 LoadObject( store, handler );
01128 if ( m_lastError != a2dError_NoError || (!store && !store.eof()) )
01129 #else
01130
01131 int res = LoadObject( store, handler ).GetLastError();
01132 if ( m_lastError != a2dError_NoError || ((res != wxSTREAM_NO_ERROR) && (res != wxSTREAM_EOF)) )
01133 #endif
01134 {
01135 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not Load file %s"), file.GetFullPath().c_str() );
01136 event.Veto();
01137 event.SetError( m_lastError );
01138 }
01139 SetFilename(file, true);
01140 Modify(false);
01141 m_savedYet = true;
01142 m_isClosed = false;
01143
01144 UpdateAllViews();
01145 }
01146
01147 a2dDocumentInputStream& a2dDocument::LoadObject( a2dDocumentInputStream& stream, a2dIOHandlerStrIn* handler )
01148 {
01149 #if wxUSE_STD_IOSTREAM
01150 int isOK = (stream.fail() || stream.bad()) == 0;
01151 #else
01152 int isOK = stream.IsOk ();
01153 #endif
01154
01155 bool oke = false;
01156 if (isOK)
01157 {
01158 if ( handler )
01159 {
01160 if ( handler->CanLoad( stream, this, m_documentTemplate ? m_documentTemplate->GetDocumentClassInfo(): NULL ) )
01161 {
01162 if ( !handler->Load(stream, this) )
01163 {
01164 a2dDocviewGlobals->ReportError( a2dError_CouldNotLoad );
01165 m_lastError = a2dError_CouldNotLoad;
01166 }
01167 else
01168 {
01169 oke = true;
01170 a2dDocumentEvent event( wxEVT_POST_LOAD_DOCUMENT );
01171 event.SetEventObject( this );
01172 DistributeEvent( event );
01173 }
01174 }
01175 else
01176 {
01177 a2dDocviewGlobals->ReportError( a2dError_IOHandler );
01178 m_lastError = a2dError_IOHandler;
01179 }
01180 }
01181 else
01182 {
01183 a2dDocviewGlobals->ReportErrorF( a2dError_IOHandler, _("I/O handler must be != NULL, maybe template I/O handler not available in document template.") );
01184 m_lastError = a2dError_IOHandler;
01185 }
01186 }
01187 else
01188 {
01189 a2dDocviewGlobals->ReportError( a2dError_ImportObject );
01190 m_lastError = a2dError_ImportObject;
01191 }
01192
01193 return stream;
01194 }
01195
01196 #if wxART2D_USE_CVGIO
01197 void a2dDocument::Load( wxObject* WXUNUSED(parent), a2dIOHandlerXmlSerIn& parser )
01198 {
01199 parser.Next();
01200 parser.Require( START_TAG, wxT("cvg") );
01201 parser.Next();
01202 if ( parser.GetName() == wxT("title") )
01203 {
01204 m_documentTitle = parser.GetContent();
01205 parser.Next();
01206 parser.Require( END_TAG, wxT("title") );
01207 parser.Next();
01208 }
01209
01210 parser.Require( END_TAG, wxT("cvg") );
01211 }
01212
01213 void a2dDocument::Save( wxObject* WXUNUSED(parent), a2dIOHandlerXmlSerOut &out, a2dObjectList* WXUNUSED(towrite) )
01214 {
01215 out.WriteStartDocument( wxT("1.0"), wxT("UTF-8") , true );
01216
01217 out.WriteStartElementAttributes( wxT("cvg") );
01218 out.WriteAttribute( wxT("classname"), GetClassInfo()->GetClassName() );
01219 out.WriteEndAttributes();
01220
01221 out.WriteElement( wxT("title"), GetPrintableName() );
01222
01223 out.WriteEndElement();
01224 }
01225
01226
01227 void a2dDocument::Save( a2dIOHandlerXmlSerOut &out, wxObject* WXUNUSED(start) )
01228 {
01229 out.WriteStartDocument( wxT("1.0"), wxT("UTF-8") , true );
01230
01231 out.WriteStartElementAttributes( wxT("cvg") );
01232 out.WriteAttribute( wxT("classname"), GetClassInfo()->GetClassName() );
01233 out.WriteEndAttributes();
01234
01235 out.WriteElement( wxT("title"), GetPrintableName() );
01236
01237 out.WriteEndElement();
01238 }
01239
01240 #endif //wxART2D_USE_CVGIO
01241
01242 a2dDocumentOutputStream& a2dDocument::SaveObject(a2dDocumentOutputStream& stream, a2dIOHandlerStrOut* handler )
01243 {
01244 #if wxUSE_STD_IOSTREAM
01245 int isOK = (stream.fail() || stream.bad()) == 0;
01246 #else
01247 int isOK = stream.IsOk ();
01248 #endif
01249
01250 if (isOK)
01251 {
01252 if ( handler )
01253 {
01254 if ( handler->CanSave( this ) )
01255 {
01256 if ( !handler->Save(stream, this) )
01257 {
01258 a2dDocviewGlobals->ReportError( a2dError_IOHandler );
01259 m_lastError = a2dError_IOHandler;
01260 }
01261 }
01262 else
01263 {
01264 a2dDocviewGlobals->ReportErrorF( a2dError_IOHandler, _("I/O handler cannot save document" ));
01265 m_lastError = a2dError_IOHandler;
01266 }
01267 }
01268 else
01269 {
01270 a2dDocviewGlobals->ReportErrorF( a2dError_IOHandler, _("I/O handler must be != NULL, maybe not available in template") );
01271 m_lastError = a2dError_IOHandler;
01272 }
01273 }
01274 else
01275 {
01276 a2dDocviewGlobals->ReportError( a2dError_SaveObject );
01277 m_lastError = a2dError_SaveObject;
01278 }
01279
01280 return stream;
01281 }
01282
01283 bool a2dDocument::Revert()
01284 {
01285 return false;
01286 }
01287
01288
01289
01290 wxString a2dDocument::GetPrintableName() const
01291 {
01292 if (m_documentTitle != wxT(""))
01293 {
01294 return m_documentTitle;
01295 }
01296 else if (m_documentFile.GetFullPath() != wxT(""))
01297 {
01298 return m_documentFile.GetFullPath();
01299 }
01300 else
01301 {
01302 return _("unnamed");
01303 }
01304 }
01305
01306 wxWindow* a2dDocument::GetAssociatedWindow() const
01307 {
01308 return wxTheApp->GetTopWindow();
01309 }
01310
01311
01312 void a2dDocument::CreateCommandProcessor()
01313 {
01314 m_commandProcessor = new a2dCommandProcessor;
01315 }
01316
01317 int a2dDocument::SaveModifiedDialog( bool canCancel )
01318 {
01319 if ( IsModified() )
01320 {
01321 wxString title = GetPrintableName();
01322
01323 wxString msgTitle;
01324 if (wxTheApp->GetAppName() != wxT(""))
01325 msgTitle = wxTheApp->GetAppName();
01326 else
01327 msgTitle = _("Warning");
01328
01329 wxString prompt;
01330 prompt.Printf(_("Do you want to save changes to document %s?"),
01331 (const wxChar *)title);
01332
01333 int res = wxMessageBox(prompt, msgTitle,
01334 canCancel ? wxYES_NO|wxCANCEL|wxICON_QUESTION : wxYES_NO|wxICON_QUESTION,
01335 GetAssociatedWindow());
01336
01337 if (res == wxNO)
01338 {
01339 Modify(false);
01340 return wxNO;
01341 }
01342 else if (res == wxYES)
01343 {
01344 Save();
01345 return wxYES;
01346 }
01347 else if (res == wxCANCEL)
01348 return wxCANCEL;
01349 }
01350 return wxNO;
01351 }
01352
01353 bool a2dDocument::Draw(wxDC& WXUNUSED(context))
01354 {
01355 return true;
01356 }
01357
01358 void a2dDocument::OnCreateDocument( a2dDocumentEvent& WXUNUSED(event))
01359 {
01360 }
01361
01362
01363 void a2dDocument::Modify(bool mod)
01364 {
01365 if (m_documentModified != mod)
01366 {
01367 m_documentModified = mod;
01368
01369 a2dDocumentEvent event( wxEVT_CHANGEDMODIFY_DOCUMENT );
01370 event.SetEventObject( this );
01371 DistributeEvent( event );
01372 }
01373 }
01374
01375 void a2dDocument::ReportViews( a2dViewList* allviews )
01376 {
01377 a2dDocumentEvent event( wxEVT_REPORT_VIEWS, allviews );
01378 event.SetEventObject( this );
01379 DistributeEvent( event );
01380 }
01381
01382 void a2dDocument::UpdateAllViews(a2dView *sender, unsigned int hint, wxObject *objecthint )
01383 {
01384 a2dDocumentEvent event( wxEVT_UPDATE_VIEWS, sender );
01385 event.SetEventObject(this);
01386 event.SetUpdateHint( hint );
01387 event.SetUpdateObject( objecthint );
01388 DistributeEvent( event );
01389 }
01390
01391 void a2dDocument::EnableAllViews( bool enable, a2dView *sender )
01392 {
01393 a2dDocumentEvent event( wxEVT_ENABLE_VIEWS, sender, enable );
01394 event.SetEventObject(this);
01395 DistributeEvent( event );
01396 }
01397
01398 void a2dDocument::SetFilename( const wxFileName& filename, bool notifyViews)
01399 {
01400
01401 {
01402 m_documentFile = filename;
01403 if ( notifyViews )
01404 {
01405 a2dDocumentEvent event( wxEVT_CHANGEDFILENAME_DOCUMENT,m_documentFile,(a2dDocumentFlagMask) a2dREFDOC_NON, m_id );
01406 event.SetEventObject(this);
01407 DistributeEvent( event );
01408 }
01409 }
01410 }
01411
01412 void a2dDocument::SetTitle(const wxString& title, bool notifyViews)
01413 {
01414
01415 {
01416 m_documentTitle = title;
01417 if ( notifyViews )
01418 {
01419 a2dDocumentEvent event( wxEVT_CHANGEDTITLE_DOCUMENT, m_documentFile, (a2dDocumentFlagMask) a2dREFDOC_NON, m_id );
01420 event.SetEventObject(this);
01421 DistributeEvent( event );
01422 }
01423 }
01424 }
01425
01426
01427
01428
01429
01430 A2D_BEGIN_EVENT_TABLE( a2dView, a2dEvtHandler )
01431 A2D_EVT_CLOSE_VIEW( a2dView::OnCloseView )
01432 A2D_EVT_CHANGEDFILENAME_DOCUMENT( a2dView::OnChangeFilename )
01433 A2D_EVT_CHANGEDTITLE_DOCUMENT( a2dView::OnChangeTitle )
01434 A2D_EVT_ENABLE_VIEW( a2dView::OnEnable )
01435 A2D_EVT_UPDATE_VIEWS( a2dView::OnUpdate )
01436 A2D_EVT_ENABLE_VIEWS( a2dView::OnEnableViews )
01437 A2D_EVT_REPORT_VIEWS( a2dView::OnReport )
01438 A2D_EVT_DISCONNECT_ALLVIEWS( a2dView::OnDisConnectView )
01439 A2D_END_EVENT_TABLE()
01440
01441 a2dView::a2dView()
01442 {
01443 m_display = NULL;
01444
01445 m_id = wxNewId();
01446
01447 m_isClosed = false;
01448
01449 m_viewDocument = (a2dDocument*) NULL;
01450
01451 m_viewTypeName = wxT("");
01452
01453 m_active = false;
01454
01455 m_DoDistributeActivateEvent = true;
01456
01457 m_viewEnabled = true;
01458
01459
01460 m_keepalive = this;
01461
01462
01463
01464 a2dDocviewGlobals->GetEventDistributer()->Register( this );
01465 }
01466
01467 a2dView::a2dView( const a2dView& other)
01468 :a2dEvtHandler()
01469 {
01470 m_isClosed = false;
01471
01472 m_display = other.m_display;
01473 m_viewDocument = other.m_viewDocument;
01474 m_viewTypeName = other.m_viewTypeName;
01475 m_viewTemplate = other.m_viewTemplate;
01476 m_viewEnabled = other.m_viewEnabled;
01477 m_active = other.m_active;
01478 m_DoDistributeActivateEvent = other.m_DoDistributeActivateEvent;
01479
01480 m_id = wxNewId();
01481 }
01482
01483 a2dView::~a2dView()
01484 {
01485 wxASSERT_MSG( m_refcount == 0, wxT("deleting a2dView while referenced") );
01486 a2dDocviewGlobals->GetEventDistributer()->Unregister( this );
01487 a2dDocviewGlobals->GetDocviewCommandProcessor()->CheckCurrentLastView(this);
01488 }
01489
01490 a2dObject* a2dView::SmrtPtrOwn()
01491 {
01492 m_refcount++;
01493 return this;
01494 }
01495
01496 bool a2dView::SmrtPtrRelease()
01497 {
01498 m_refcount--;
01499
01500 wxASSERT_MSG( m_refcount >= 0, wxT("a2dView::Release Negative reference count") );
01501 if ( m_refcount <= 0 )
01502 {
01503 delete this;
01504 return true;
01505 }
01506 return false;
01507 }
01508
01509 void a2dView::SetDisplayWindow( wxWindow* display )
01510 {
01511 if ( GetEvtHandlerEnabled() )
01512 {
01513 if ( m_display != display )
01514 Activate( false );
01515 m_display = display;
01516 if ( m_display )
01517 Activate(true);
01518 }
01519 }
01520
01521 bool a2dView::Enable(bool enable)
01522 {
01523 if ( enable != m_viewEnabled )
01524 {
01525 bool restorestate = GetEvtHandlerEnabled();
01526 if ( m_display )
01527 {
01528
01529
01530 a2dViewEvent event( wxEVT_ENABLE_VIEW, enable, m_id );
01531 event.SetEventObject( this );
01532
01533 SetEvtHandlerEnabled(false);
01534 event.ResumePropagation( wxEVENT_PROPAGATE_MAX );
01535 m_display->GetEventHandler()->ProcessEvent( event );
01536 event.StopPropagation();
01537 }
01538
01539
01540 SetEvtHandlerEnabled(true);
01541 if ( enable )
01542 {
01543
01544 a2dViewEvent Event( wxEVT_ENABLE_VIEW, enable, m_id );
01545 Event.SetEventObject( this );
01546 GetEventHandler()->ProcessEvent( Event );
01547 }
01548
01549 m_viewEnabled = enable;
01550
01551 SetEvtHandlerEnabled(restorestate);
01552 return true;
01553 }
01554 else
01555 {
01556 return false;
01557 }
01558 }
01559
01560 void a2dView::SetDocument(a2dDocument *doc)
01561 {
01562 if( m_viewDocument != doc )
01563 {
01564 bool viewRemoved = false;
01565 if ( m_viewDocument )
01566 {
01567
01568 viewRemoved = true;
01569 a2dDocumentEvent eventremove( wxEVT_REMOVE_VIEW, this, true, m_id );
01570 eventremove.SetEventObject(this);
01571 m_viewDocument->ProcessEvent( eventremove );
01572 DistributeEvent( eventremove );
01573 }
01574
01575 m_viewDocument = doc;
01576
01577 if ( doc )
01578 {
01579 a2dDocumentEvent eventadd( wxEVT_ADD_VIEW, this, viewRemoved , m_id );
01580 eventadd.SetEventObject(this);
01581 m_viewDocument->ProcessEvent( eventadd );
01582 DistributeEvent( eventadd );
01583 }
01584
01585 a2dViewEvent viewevent( wxEVT_SET_DOCUMENT, (a2dDocumentFlagMask) a2dREFDOC_NON, m_id );
01586 viewevent.SetEventObject(this);
01587 GetEventHandler()->ProcessEvent( viewevent );
01588 }
01589 }
01590
01591 void a2dView::Activate( bool activate )
01592 {
01593
01594
01595
01596 if(!activate && !m_active)
01597 {
01598
01599
01600
01601 return;
01602 }
01603
01604 a2dViewEvent activateEvent( wxEVT_ACTIVATE_VIEW, activate, m_id );
01605 activateEvent.SetEventObject(this);
01606
01607 m_active = activate;
01608
01609 bool oldEnableState = GetEvtHandlerEnabled();
01610 SetEvtHandlerEnabled(false);
01611
01612 if (m_display)
01613 {
01614 a2dViewEvent activateEvent( wxEVT_ACTIVATE_VIEW_SENT_FROM_CHILD, activate, m_id );
01615 activateEvent.SetEventObject(this);
01616 activateEvent.ResumePropagation( wxEVENT_PROPAGATE_MAX );
01617 m_display->GetEventHandler()->ProcessEvent( activateEvent );
01618 activateEvent.StopPropagation();
01619 }
01620
01621
01622 if( m_DoDistributeActivateEvent )
01623 DistributeEvent( activateEvent );
01624
01625 SetEvtHandlerEnabled(oldEnableState);
01626
01627
01628 GetEventHandler()->ProcessEvent( activateEvent );
01629 }
01630
01631
01632 bool a2dView::ProcessEvent(wxEvent& event)
01633 {
01634 if ( (GetEvtHandlerEnabled() && m_viewEnabled) ||
01635 (event.GetEventType() == wxEVT_ENABLE_VIEW) ||
01636 (event.GetEventType() == wxEVT_ENABLE_VIEWS)
01637 )
01638 {
01639
01640 a2dREFOBJECTPTR_KEEPALIVE;
01641
01642 bool processed = false;
01643
01644 if ( event.GetEventType() == wxEVT_ACTIVATE )
01645 {
01646 Activate( ((wxActivateEvent&) event).GetActive() );
01647
01648
01649
01650
01651 return true;
01652 }
01653 else if ( event.GetEventType() == wxEVT_SET_FOCUS )
01654 {
01655 processed = GetEventHandler()->a2dEvtHandler::ProcessEvent(event);
01656
01657
01658
01659
01660
01661 return processed;
01662 }
01663 else if ( event.GetEventType() == wxEVT_KILL_FOCUS )
01664 {
01665
01666
01667
01668
01669 processed = GetEventHandler()->a2dEvtHandler::ProcessEvent(event);
01670
01671 return processed;
01672 }
01673 else if ( event.GetEventType() == wxEVT_CLOSE_WINDOW )
01674 {
01675 wxCloseEvent& closeevent = (wxCloseEvent&) event;
01676
01677
01678
01679 if ( !GetDocument() )
01680 {
01681 processed = Close( !closeevent.CanVeto() );
01682 return processed;
01683 }
01684
01685
01686 processed = Close( !closeevent.CanVeto() );
01687 return processed;
01688 }
01689 else if ( event.GetEventType() == wxEVT_IDLE )
01690 {
01691
01692 if ( GetDocument() && GetDocument()->ProcessEvent(event) )
01693 return true;
01694 }
01695
01696
01697 event.Skip();
01698 processed = GetEventHandler()->a2dEvtHandler::ProcessEvent(event);
01699 return processed;
01700 }
01701 return false;
01702 }
01703
01704 void a2dView::OnCloseView( a2dCloseViewEvent& event )
01705 {
01706 if ( this != event.GetEventObject() )
01707 return;
01708 }
01709
01710 void a2dView::OnDisConnectView( a2dDocumentEvent& event )
01711 {
01712
01713 if ( m_isClosed )
01714 return;
01715
01716 if ( m_viewDocument != event.GetEventObject() )
01717 return;
01718
01719 bool processed = false;
01720
01721
01722
01723 if ( m_viewTemplate && m_viewTemplate->GetViewConnector() )
01724 {
01725 a2dTemplateEvent event( wxEVT_DISCONNECT_VIEW, this, a2dREFDOC_NON, m_id );
01726 event.SetEventObject( this );
01727 processed = m_viewTemplate->GetViewConnector()->ProcessEvent( event );
01728 }
01729
01730
01731
01732 if ( !processed )
01733 {
01734 Close( true );
01735 }
01736 }
01737
01738 void a2dView::SetClosed()
01739 {
01740 m_isClosed = true;
01741 m_viewDocument = 0;
01742
01743 m_keepalive = 0;
01744 }
01745
01746 bool a2dView::Close( bool force )
01747 {
01748
01749 if ( m_isClosed )
01750 return true;
01751
01752
01753 wxASSERT( m_keepalive == this );
01754
01755 m_isClosed = true;
01756
01757 a2dCloseViewEvent event( m_id );
01758 event.SetEventObject(this);
01759 event.SetCanVeto(!force);
01760
01761
01762 if ( m_viewDocument )
01763 m_viewDocument->ProcessEvent(event);
01764
01765
01766 if ( !event.GetVeto() )
01767 {
01768
01769 if ( m_viewTemplate.Get() && m_viewTemplate->GetViewConnector() )
01770 m_viewTemplate->GetViewConnector()->ProcessEvent( event );
01771 else
01772 {
01773 if (m_display )
01774 {
01775 bool oldEnableState = GetEvtHandlerEnabled();
01776 SetEvtHandlerEnabled(false);
01777 event.ResumePropagation( wxEVENT_PROPAGATE_MAX );
01778
01779 m_display->GetEventHandler()->ProcessEvent( event );
01780 event.StopPropagation();
01781 SetEvtHandlerEnabled(oldEnableState);
01782 }
01783 }
01784 }
01785
01786 if ( !event.GetVeto() )
01787 {
01788
01789
01790
01791
01792 m_display = NULL;
01793
01794
01795 GetEventHandler()->ProcessEvent(event);
01796 if ( !event.GetVeto() )
01797 {
01798 Activate( false );
01799 if (m_viewDocument )
01800 {
01801 a2dDocumentEvent eventremove( wxEVT_REMOVE_VIEW, this, true, m_id );
01802 eventremove.SetEventObject(this);
01803 m_viewDocument->ProcessEvent( eventremove );
01804 DistributeEvent( eventremove );
01805 }
01806 m_viewDocument = 0;
01807
01808
01809 m_keepalive = 0;
01810 return true;
01811 }
01812 }
01813
01814 m_isClosed = false;
01815
01816
01817 return false;
01818 }
01819
01820 void a2dView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info))
01821 {
01822 OnDraw(dc);
01823 }
01824
01825
01826 void a2dView::Update( unsigned int hint, wxObject* hintObject )
01827 {
01828 a2dDocumentEvent event( wxEVT_UPDATE_VIEWS, this );
01829 event.SetEventObject(m_viewDocument);
01830
01831 event.SetUpdateHint( hint );
01832 event.SetUpdateObject( hintObject );
01833 GetEventHandler()->ProcessEvent( event );
01834 }
01835
01836 void a2dView::OnUpdate( a2dDocumentEvent& event )
01837 {
01838 if ( m_viewDocument == event.GetEventObject() && GetEvtHandlerEnabled() && m_display )
01839 m_display->Refresh();
01840 }
01841
01842 void a2dView::OnReport( a2dDocumentEvent& event )
01843 {
01844 if ( m_viewDocument == event.GetEventObject() && !m_isClosed )
01845 event.GetReportList()->push_back( this );
01846 }
01847
01848 void a2dView::OnEnable( a2dViewEvent& event )
01849 {
01850 if ( this != event.GetEventObject() )
01851 return;
01852
01853 if ( event.GetEnable() )
01854 Update();
01855 }
01856
01857 void a2dView::OnEnableViews( a2dDocumentEvent& event )
01858 {
01859 if ( m_viewDocument == event.GetEventObject() &&
01860 ( event.GetView() != this || event.GetView() == NULL )
01861 )
01862 Enable( event.GetEnable() );
01863 }
01864
01865 void a2dView::OnChangeFilename( a2dDocumentEvent& event )
01866 {
01867 if ( m_viewDocument != event.GetEventObject() )
01868 return;
01869
01870 bool oldEnableState = GetEvtHandlerEnabled();
01871 SetEvtHandlerEnabled(false);
01872
01873 if ( m_display )
01874 {
01875 a2dViewEvent eventView( wxEVT_CHANGEDFILENAME_VIEW, (a2dDocumentFlagMask) a2dREFDOC_NON, m_id );
01876 eventView.SetEventObject(this);
01877 eventView.ResumePropagation( wxEVENT_PROPAGATE_MAX );
01878 m_display->GetEventHandler()->ProcessEvent( eventView );
01879 eventView.StopPropagation();
01880 }
01881
01882 SetEvtHandlerEnabled(oldEnableState);
01883 }
01884
01885 void a2dView::OnChangeTitle( a2dDocumentEvent& event )
01886 {
01887 if ( m_viewDocument != event.GetEventObject() )
01888 return;
01889
01890 bool oldEnableState = GetEvtHandlerEnabled();
01891 SetEvtHandlerEnabled(false);
01892
01893 if ( m_display )
01894 {
01895 a2dViewEvent eventView( wxEVT_CHANGEDTITLE_VIEW, (a2dDocumentFlagMask) a2dREFDOC_NON, m_id );
01896 eventView.SetEventObject(this);
01897 eventView.ResumePropagation( wxEVENT_PROPAGATE_MAX );
01898 m_display->GetEventHandler()->ProcessEvent( eventView );
01899 eventView.StopPropagation();
01900 }
01901
01902 SetEvtHandlerEnabled(oldEnableState);
01903 }
01904
01905 #if wxUSE_PRINTING_ARCHITECTURE
01906 wxPrintout *a2dView::OnCreatePrintout( a2dCommand* WXUNUSED(typeOfPrint) )
01907 {
01908 return new a2dDocumentPrintout(this);
01909 }
01910 #endif // wxUSE_PRINTING_ARCHITECTURE
01911
01912 void a2dView::OnSetFocus( wxFocusEvent &event )
01913 {
01914 event.Skip( true );
01915 }
01916
01917 void a2dView::OnKillFocus( wxFocusEvent &event )
01918 {
01919 event.Skip( true );
01920 }
01921
01922
01923
01924
01925 a2dDocumentTemplate::a2dDocumentTemplate(
01926 const wxString& descr,
01927 const wxString& filter,
01928 const wxString& dir,
01929 const wxString& ext,
01930 const wxString& docTypeName,
01931 wxClassInfo* docClassInfo,
01932 a2dViewConnector* connector,
01933 long flags,
01934 a2dIOHandlerStrIn* handlerIn,
01935 a2dIOHandlerStrOut* handlerOut )
01936 {
01937 m_description = descr;
01938 m_directory = dir;
01939 m_defaultExt = ext;
01940 m_fileFilter = filter;
01941 m_flags = flags;
01942 m_docTypeName = docTypeName;
01943 m_connector = connector;
01944
01945 m_docClassInfo = docClassInfo;
01946 m_documentCreate = NULL;
01947
01948 m_ioHandlerStrIn = handlerIn;
01949 m_ioHandlerStrOut = handlerOut;
01950 #if wxUSE_PRINTING_ARCHITECTURE
01951 m_pageSetupData = new wxPageSetupData(*a2dDocviewGlobals->GetDocviewCommandProcessor()->GetPageSetupData());
01952 #endif
01953 }
01954
01955 a2dDocumentTemplate::~a2dDocumentTemplate()
01956 {
01957 #if wxUSE_PRINTING_ARCHITECTURE
01958 if(m_pageSetupData)
01959 delete m_pageSetupData;
01960 #endif
01961 }
01962
01963 a2dObject* a2dDocumentTemplate::SmrtPtrOwn()
01964 {
01965 m_refcount++;
01966 return this;
01967 }
01968
01969 bool a2dDocumentTemplate::SmrtPtrRelease()
01970 {
01971 m_refcount--;
01972
01973 wxASSERT_MSG( m_refcount >= 0, wxT("a2dDocumentTemplate::Release Negative reference count") );
01974 if ( m_refcount <= 0 )
01975 {
01976 delete this;
01977 return true;
01978 }
01979 return false;
01980 }
01981
01982 a2dObject* a2dDocumentTemplate::Clone( CloneOptions WXUNUSED(options) ) const
01983 {
01984 wxLogMessage( _("Not implemented") );
01985 return NULL;
01986 }
01987
01988 a2dIOHandlerStrIn* a2dDocumentTemplate::GetDocumentIOHandlerForLoad( a2dDocumentInputStream& stream, a2dDocument* document )
01989 {
01990 if ( m_ioHandlerStrIn && m_ioHandlerStrIn->CanLoad( stream, document, m_docClassInfo ) )
01991 return m_ioHandlerStrIn;
01992 return NULL;
01993 }
01994
01995 a2dIOHandlerStrOut* a2dDocumentTemplate::GetDocumentIOHandlerForSave( const wxString& filename, a2dDocument* document )
01996 {
01997 if ( m_ioHandlerStrOut && m_ioHandlerStrOut->CanSave( document ) )
01998 return m_ioHandlerStrOut;
01999 return NULL;
02000 }
02001
02002
02003 #if wxART2D_USE_CVGIO
02004 void a2dDocumentTemplate::DoSave( wxObject* WXUNUSED(parent), a2dIOHandlerXmlSerOut &WXUNUSED(out), a2dXmlSer_flag WXUNUSED(xmlparts), a2dObjectList* WXUNUSED(towrite) )
02005 {
02006 wxLogMessage( _("Not implemented") );
02007 }
02008
02009 void a2dDocumentTemplate::DoLoad( wxObject* WXUNUSED(parent), a2dIOHandlerXmlSerIn& WXUNUSED(parser), a2dXmlSer_flag WXUNUSED(xmlparts) )
02010 {
02011 wxLogMessage( _("Not implemented") );
02012 }
02013
02014 #endif //wxART2D_USE_CVGIO
02015
02016
02017 a2dDocument* a2dDocumentTemplate::CreateDocument( const wxFileName& path, a2dDocumentFlagMask flags )
02018 {
02019 if (!m_docClassInfo)
02020 return (a2dDocument *) NULL;
02021
02022 a2dDocument* doc;
02023 if ( m_documentCreate )
02024 doc = (a2dDocument *) m_documentCreate->Clone( clone_deep );
02025 else
02026 doc = (a2dDocument *) m_docClassInfo->CreateObject();
02027
02028 if (doc)
02029 {
02030
02031 doc->SetFilename(path, false);
02032 doc->SetDocumentTemplate(this);
02033 if ( !doc->GetCommandProcessor() )
02034 doc->CreateCommandProcessor();
02035 doc->SetDocumentTypeName( m_docTypeName );
02036
02037 a2dDocumentEvent createdDocument( wxEVT_CREATE_DOCUMENT, path, flags );
02038 createdDocument.SetEventObject(this);
02039 doc->ProcessEvent( createdDocument );
02040
02041 return doc;
02042 }
02043 return (a2dDocument *) NULL;
02044 }
02045
02046 a2dDocument* a2dDocumentTemplate::SentPreAddCreatedDocumentEvent( a2dDocument* newDoc, a2dDocumentFlagMask flags )
02047 {
02048 bool ret = false;
02049
02050
02051
02052 a2dTemplateEvent preAddCreatedDocumentEvent( newDoc, wxEVT_PRE_ADD_DOCUMENT, flags );
02053 preAddCreatedDocumentEvent.SetEventObject(this);
02054 if ( m_connector )
02055 ret = m_connector->ProcessEvent( preAddCreatedDocumentEvent );
02056 else
02057 ret = DistributeEvent( preAddCreatedDocumentEvent );
02058
02059 return preAddCreatedDocumentEvent.GetDocument();
02060 }
02061
02062 bool a2dDocumentTemplate::SentPostCreateDocumentEvent( a2dDocument* newDoc, a2dDocumentFlagMask flags )
02063 {
02064 bool ret = false;
02065
02066
02067
02068 a2dTemplateEvent postCreatedDocumentEvent( newDoc, wxEVT_POST_CREATE_DOCUMENT, flags );
02069 postCreatedDocumentEvent.SetEventObject(this);
02070 if ( m_connector )
02071 ret = m_connector->ProcessEvent( postCreatedDocumentEvent );
02072 else
02073 ret = DistributeEvent( postCreatedDocumentEvent );
02074
02075 return ret;
02076 }
02077
02078 bool a2dDocumentTemplate::FileMatchesTemplate(const wxString& path)
02079 {
02080
02081 if ( FindExtension(path) != wxT("") )
02082 if ( GetDefaultExtension().IsSameAs( FindExtension(path) ) )
02083 return true;
02084
02085
02086 #if wxUSE_STD_IOSTREAM
02087 a2dDocumentFileInputStream store( path.mb_str(), wxSTD ios_base::in | wxSTD ios_base::binary );
02088 if (store.fail() || store.bad())
02089 {
02090 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for testing format"), path.c_str() );
02091 return false;
02092 }
02093 #else
02094 wxFileInputStream storeUnbuf( path );
02095 if ( !storeUnbuf.Ok() )
02096 {
02097 a2dDocviewGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open file %s for testing format"), path.c_str() );
02098 return false;
02099 }
02100 wxBufferedInputStream store( storeUnbuf );
02101 #endif
02102
02103 a2dIOHandlerStrIn* handler = GetDocumentIOHandlerForLoad( store, NULL );
02104 if ( handler )
02105 return true;
02106
02107 wxStringTokenizer parser (GetFileFilter(), wxT(";"));
02108 wxString anything = wxT ("*");
02109 while (parser.HasMoreTokens())
02110 {
02111 wxString filter = parser.GetNextToken();
02112 wxString filterExt = FindExtension (filter);
02113 if ( filter.IsSameAs (anything) ||
02114 filterExt.IsSameAs (anything) ||
02115 filterExt.IsSameAs (FindExtension (path)) )
02116 return true;
02117 }
02118 return false;
02119 }
02120
02121
02122
02123
02124
02125 a2dDocumentTemplateAuto::a2dDocumentTemplateAuto(
02126 const wxString& descr,
02127 const wxString& filter,
02128 const wxString& dir,
02129 const wxString& ext,
02130 const wxString& docTypeName,
02131 wxClassInfo* docClassInfo,
02132 a2dViewConnector* connector,
02133 long flags ):
02134 a2dDocumentTemplate( descr, filter, dir, ext, docTypeName, docClassInfo, connector, flags, NULL )
02135 {
02136 }
02137
02138 a2dDocumentTemplateAuto::~a2dDocumentTemplateAuto()
02139 {
02140 }
02141
02142 a2dIOHandlerStrIn* a2dDocumentTemplateAuto::GetDocumentIOHandlerForLoad( a2dDocumentInputStream& stream, a2dDocument* document )
02143 {
02144
02145 const a2dDocumentTemplateList& allDocTemplates = a2dDocviewGlobals->GetDocviewCommandProcessor()->GetDocTemplates();
02146 for( a2dDocumentTemplateList::const_iterator iter = allDocTemplates.begin(); iter != allDocTemplates.end(); ++iter )
02147 {
02148 a2dDocumentTemplateList::value_type temp = *iter;
02149 if ( temp != this && temp->GetDocumentTypeName() == GetDocumentTypeName() &&
02150 temp->GetDocumentIOHandlerStrIn() &&
02151 temp->GetDocumentIOHandlerStrIn()->CanLoad( stream, document, temp->GetDocumentClassInfo() ) )
02152 return temp->GetDocumentIOHandlerStrIn();
02153 }
02154 return NULL;
02155 }
02156
02157 a2dIOHandlerStrOut* a2dDocumentTemplateAuto::GetDocumentIOHandlerForSave( const wxString& filename, a2dDocument* document )
02158 {
02159
02160 const a2dDocumentTemplateList& allDocTemplates = a2dDocviewGlobals->GetDocviewCommandProcessor()->GetDocTemplates();
02161 for( a2dDocumentTemplateList::const_iterator iter = allDocTemplates.begin(); iter != allDocTemplates.end(); ++iter )
02162 {
02163 a2dDocumentTemplateList::value_type temp = *iter;
02164 if ( temp != this && temp->GetDocumentTypeName() == GetDocumentTypeName() && temp->GetDocumentIOHandlerStrOut() && temp->GetDocumentIOHandlerStrOut()->CanSave( document ) )
02165 {
02166 if ( filename.IsEmpty() )
02167 return temp->GetDocumentIOHandlerStrOut();
02168 else
02169 {
02170 wxString path, name, ext;
02171 wxFileName::SplitPath( filename, & path, & name, & ext);
02172 if ( ext == temp->GetDefaultExtension() )
02173 return temp->GetDocumentIOHandlerStrOut();
02174 }
02175 }
02176 }
02177 return NULL;
02178 }
02179
02180
02181
02182
02183 a2dViewTemplate::a2dViewTemplate(
02184 const wxString& descr,
02185 const wxString& docTypeName,
02186 const wxString& viewTypeName,
02187 wxClassInfo* viewClassInfo,
02188 a2dViewConnector* connector,
02189 long flags,
02190 const wxSize& size
02191 )
02192 {
02193 m_description = descr;
02194 m_flags = flags;
02195 m_docTypeName = docTypeName;
02196 m_viewTypeName = viewTypeName;
02197 m_connector = connector;
02198
02199 m_viewClassInfo = viewClassInfo;
02200
02201 m_initialSize = size;
02202 #if wxUSE_PRINTING_ARCHITECTURE
02203 m_pageSetupData = new wxPageSetupData(*a2dDocviewGlobals->GetDocviewCommandProcessor()->GetPageSetupData());
02204 #endif
02205 }
02206
02207 a2dViewTemplate::~a2dViewTemplate()
02208 {
02209 #if wxUSE_PRINTING_ARCHITECTURE
02210 if(m_pageSetupData)
02211 delete m_pageSetupData;
02212 #endif
02213 }
02214
02215 a2dObject* a2dViewTemplate::SmrtPtrOwn()
02216 {
02217 m_refcount++;
02218 return this;
02219 }
02220
02221 bool a2dViewTemplate::SmrtPtrRelease()
02222 {
02223 m_refcount--;
02224
02225 wxASSERT_MSG( m_refcount >= 0, wxT("a2dViewTemplate::Release Negative reference count") );
02226 if ( m_refcount <= 0 )
02227 {
02228 delete this;
02229 return true;
02230 }
02231 return false;
02232 }
02233
02234 a2dObject* a2dViewTemplate::Clone( CloneOptions WXUNUSED(options) ) const
02235 {
02236 wxLogMessage( _("Not implemented") );
02237 return NULL;
02238 }
02239
02240 #if wxART2D_USE_CVGIO
02241 void a2dViewTemplate::DoSave( wxObject* WXUNUSED(parent), a2dIOHandlerXmlSerOut &WXUNUSED(out), a2dXmlSer_flag WXUNUSED(xmlparts), a2dObjectList* WXUNUSED(towrite) )
02242 {
02243 wxLogMessage( _("Not implemented") ) ;
02244 }
02245
02246 void a2dViewTemplate::DoLoad( wxObject* WXUNUSED(parent), a2dIOHandlerXmlSerIn& WXUNUSED(parser), a2dXmlSer_flag WXUNUSED(xmlparts) )
02247 {
02248 wxLogMessage( _("Not implemented") );
02249 }
02250 #endif //wxART2D_USE_CVGIO
02251
02252 a2dView *a2dViewTemplate::CreateView( a2dDocument *doc, a2dDocumentFlagMask flags )
02253 {
02254 if (!m_viewClassInfo)
02255 return (a2dView *) NULL;
02256
02257 a2dView *view = (a2dView *)m_viewClassInfo->CreateObject();
02258
02259 if ( view )
02260 {
02261 view->SetDocument(doc);
02262 view->SetViewTypeName( m_viewTypeName );
02263 view->SetViewTemplate( this );
02264
02265 a2dViewEvent createdView( wxEVT_CREATE_VIEW, flags, view->GetId() );
02266 createdView.SetEventObject( this );
02267
02268
02269 view->ProcessEvent( createdView );
02270
02271 a2dTemplateEvent postCreatedViewEvent( wxEVT_POST_CREATE_VIEW, view, flags );
02272 postCreatedViewEvent.SetEventObject(this);
02273 m_connector->ProcessEvent( postCreatedViewEvent );
02274 view->Activate( true );
02275 return view;
02276 }
02277
02278 return (a2dView *) NULL;
02279 }
02280
02281
02282
02283
02284
02285 A2D_BEGIN_EVENT_TABLE( a2dViewConnector, a2dEvtHandler )
02286 A2D_EVT_POST_CREATE_DOCUMENT( a2dViewConnector::OnPostCreateDocument )
02287 A2D_EVT_DISCONNECT_VIEW( a2dViewConnector::OnDisConnectView )
02288 A2D_EVT_CLOSE_VIEW( a2dViewConnector::OnCloseView )
02289 A2D_END_EVENT_TABLE()
02290
02291 a2dViewConnector::a2dViewConnector()
02292 {
02293 m_initialPos = wxPoint( 0,0 );
02294 m_initialSize = wxSize( 300, 400 );
02295 m_initialStyle = wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE;
02296
02297 m_OnlyDisconnect = false;
02298 }
02299
02300 a2dViewConnector::~a2dViewConnector()
02301 {
02302 wxASSERT_MSG( m_refcount == 0, wxT("deleting a2dViewConnector while referenced") );
02303 }
02304
02305 a2dViewConnector::a2dViewConnector( const a2dViewConnector &other )
02306 {
02307 m_initialPos = other.m_initialPos;
02308 m_initialSize = other.m_initialSize;
02309 m_initialStyle = other.m_initialStyle;
02310 m_OnlyDisconnect = other.m_OnlyDisconnect;
02311 }
02312
02313 void a2dViewConnector::AssociateViewTemplate( a2dViewTemplate *temp )
02314 {
02315 if ( m_viewTemplates.end() == m_viewTemplates.Find(temp))
02316 {
02317 m_viewTemplates.push_back(temp);
02318 }
02319 }
02320
02321 void a2dViewConnector::DisassociateViewTemplate( a2dViewTemplate *temp )
02322 {
02323 m_viewTemplates.ReleaseObject(temp);
02324 }
02325
02326 void a2dViewConnector::DisassociateViewTemplates()
02327 {
02328 m_viewTemplates.clear();
02329 }
02330
02331 void a2dViewConnector::OnPostCreateDocument( a2dTemplateEvent& event )
02332 {
02333
02334 a2dViewTemplate* viewtempl;
02335
02336 viewtempl = a2dDocviewGlobals->GetDocviewCommandProcessor()->SelectViewType( event.GetDocument(),
02337 m_viewTemplates.size()?m_viewTemplates:a2dDocviewGlobals->GetDocviewCommandProcessor()->GetViewTemplates(),
02338 wxT(""),
02339 true, event.GetTemplateMaskFlags() );
02340
02341 if ( !viewtempl )
02342 {
02343 wxLogMessage( _("No view available for this document in a2dViewConnector") );
02344 return;
02345 }
02346 else
02347 {
02348 a2dView* view = viewtempl->CreateView( event.GetDocument(), event.GetFlags() );
02349 event.SetView( view );
02350 if ( view )
02351 return;
02352 }
02353 wxLogMessage(_("a2dViewConnector no view was created") );
02354 }
02355
02356 a2dView* a2dViewConnector::CreateView( a2dDocument* doc, const wxString& viewTypeName,
02357 a2dDocumentFlagMask flags,
02358 a2dTemplateFlagMask viewTemplateFlags )
02359 {
02360
02361 a2dViewTemplate* viewtempl;
02362
02363 viewtempl = a2dDocviewGlobals->GetDocviewCommandProcessor()->SelectViewType( doc,
02364 m_viewTemplates.size()?m_viewTemplates:
02365 a2dDocviewGlobals->GetDocviewCommandProcessor()->GetViewTemplates(), viewTypeName,
02366 true, viewTemplateFlags );
02367
02368 a2dView* createdView;
02369
02370 if ( !viewtempl )
02371 {
02372 wxLogMessage( _("No view available for this document in a2dViewConnector") );
02373 return NULL;
02374 }
02375 else
02376 {
02377 createdView = viewtempl->CreateView( doc, flags );
02378 if ( createdView )
02379 return createdView;
02380 }
02381 wxLogMessage(_("a2dViewConnector no view was created") );
02382 return NULL;
02383 }
02384
02385 void a2dViewConnector::OnDisConnectView( a2dTemplateEvent& event )
02386 {
02387 a2dSmrtPtr<a2dView> theViewToDisconnect = (a2dView*) event.GetEventObject();
02388
02389 if ( !m_OnlyDisconnect )
02390
02391 theViewToDisconnect->Close( true );
02392 else
02393 {
02394
02395 theViewToDisconnect->Activate( false );
02396 if ( theViewToDisconnect->GetDocument() )
02397 {
02398 a2dDocumentEvent eventremove( wxEVT_REMOVE_VIEW, theViewToDisconnect, true, theViewToDisconnect->GetId() );
02399 eventremove.SetEventObject(theViewToDisconnect);
02400 theViewToDisconnect->GetDocument()->ProcessEvent( eventremove );
02401 DistributeEvent( eventremove );
02402 }
02403 theViewToDisconnect->SetDocument( NULL );
02404 }
02405 }
02406
02407 void a2dViewConnector::OnCloseView( a2dCloseViewEvent& event )
02408 {
02409 a2dSmrtPtr<a2dView> theViewToClose = (a2dView*) event.GetEventObject();
02410
02411 if ( theViewToClose->GetDisplayWindow() )
02412 {
02413 bool oldEnableState = theViewToClose->GetEvtHandlerEnabled();
02414 theViewToClose->SetEvtHandlerEnabled(false);
02415
02416 event.ResumePropagation( wxEVENT_PROPAGATE_MAX );
02417
02418 theViewToClose->GetDisplayWindow()->GetEventHandler()->ProcessEvent( event );
02419 event.StopPropagation();
02420
02421 theViewToClose->SetEvtHandlerEnabled(oldEnableState);
02422 }
02423 }
02424
02425
02426
02427
02428
02429 a2dFrameViewConnector::a2dFrameViewConnector(): a2dViewConnector()
02430 {
02431 m_docframe = (a2dDocumentFrame*) NULL;
02432 m_createChildframe = false;
02433 }
02434
02435 a2dFrameViewConnector::a2dFrameViewConnector( const a2dFrameViewConnector &other ): a2dViewConnector( other )
02436 {
02437 m_docframe = other.m_docframe;
02438 m_createChildframe = other.m_createChildframe;
02439 }
02440
02441 void a2dFrameViewConnector::Init( wxFrame* frame, bool createChildframe )
02442 {
02443 m_docframe = frame;
02444 m_createChildframe = createChildframe;
02445 }
02446
02447 void a2dFrameViewConnector::OnPostCreateView( a2dTemplateEvent& event )
02448 {
02449 a2dView* newview = event.GetView();
02450
02451
02452 if ( m_createChildframe )
02453 {
02454 a2dDocumentFrame* viewFrame = new a2dDocumentFrame( false,
02455 m_docframe, newview, -1, newview->GetViewTypeName(),
02456 GetInitialPosition(),
02457 GetInitialSize(),
02458 GetInitialStyle()
02459 );
02460 viewFrame->Show(true);
02461 }
02462 else
02463 {
02464 a2dView* oldview = ((a2dDocumentFrame*) m_docframe)->GetView();
02465 if ( oldview )
02466 {
02467 oldview->SetDisplayWindow( NULL );
02468 }
02469 ((a2dDocumentFrame*) m_docframe)->SetView( newview );
02470 newview->SetDisplayWindow( m_docframe );
02471 }
02472 newview->Update();
02473 }
02474
02475
02476
02477
02478
02479 IMPLEMENT_DYNAMIC_CLASS( a2dScrolledWindowViewConnector, a2dViewConnector )
02480
02481
02482
02483
02484
02485 IMPLEMENT_DYNAMIC_CLASS( a2dWindowViewConnector, a2dViewConnector )
02486
02487
02488
02489
02490 IMPLEMENT_CLASS(a2dDocumentViewScrolledWindow, a2dViewWindow<wxScrolledWindow> )
02491
02492 BEGIN_EVENT_TABLE( a2dDocumentViewScrolledWindow, a2dViewWindow<wxScrolledWindow> )
02493 EVT_CLOSE_VIEW( a2dDocumentViewScrolledWindow::OnCloseView )
02494 EVT_PAINT( a2dDocumentViewScrolledWindow::OnPaint )
02495 END_EVENT_TABLE()
02496
02497 a2dDocumentViewScrolledWindow::a2dDocumentViewScrolledWindow( a2dView* view, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const long style, const wxString& name ):
02498 a2dViewWindow<wxScrolledWindow>( view, parent, id, pos, size, style, name )
02499 {
02500 }
02501
02502 a2dDocumentViewScrolledWindow::a2dDocumentViewScrolledWindow( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const long style, const wxString& name ):
02503 a2dViewWindow<wxScrolledWindow>( parent, id, pos, size, style, name )
02504 {
02505 }
02506
02507 a2dDocumentViewScrolledWindow::~a2dDocumentViewScrolledWindow()
02508 {
02509 }
02510
02511
02512
02513
02514
02515 IMPLEMENT_CLASS(a2dDocumentViewWindow, a2dViewWindow<wxWindow> )
02516
02517 BEGIN_EVENT_TABLE( a2dDocumentViewWindow, a2dViewWindow<wxWindow> )
02518 EVT_CLOSE_VIEW( a2dViewWindow<wxWindow>::OnCloseView )
02519 EVT_PAINT( a2dViewWindow<wxWindow>::OnPaint )
02520 END_EVENT_TABLE()
02521
02522 a2dDocumentViewWindow::a2dDocumentViewWindow( a2dView* view, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const long style, const wxString& name ):
02523 a2dViewWindow<wxWindow>( view, parent, id, pos, size, style, name )
02524 {
02525 }
02526
02527 a2dDocumentViewWindow::a2dDocumentViewWindow( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const long style, const wxString& name ):
02528 a2dViewWindow<wxWindow>( parent, id, pos, size, style, name )
02529 {
02530 }
02531
02532 a2dDocumentViewWindow::~a2dDocumentViewWindow()
02533 {
02534 }
02535
02536
02537
02538
02539
02540
02541 #if wxUSE_PRINTING_ARCHITECTURE
02542
02543 a2dDocumentPrintout::a2dDocumentPrintout(a2dView *view, const wxString& title)
02544 : wxPrintout(title)
02545 {
02546 m_printoutView = view;
02547 }
02548
02549 bool a2dDocumentPrintout::OnPrintPage(int WXUNUSED(page))
02550 {
02551 wxDC *dc = GetDC();
02552
02553
02554 int ppiScreenX, ppiScreenY;
02555 GetPPIScreen(&ppiScreenX, &ppiScreenY);
02556 int ppiPrinterX, ppiPrinterY;
02557 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
02558
02559
02560
02561
02562
02563
02564 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
02565
02566
02567
02568 int pageWidth, pageHeight;
02569 int w, h;
02570 dc->GetSize(&w, &h);
02571 GetPageSizePixels(&pageWidth, &pageHeight);
02572
02573
02574
02575 float overallScale = scale * (float)(w/(float)pageWidth);
02576 dc->SetUserScale(overallScale, overallScale);
02577
02578 if (m_printoutView)
02579 {
02580 m_printoutView->OnPrint(dc, NULL);
02581 }
02582 return true;
02583 }
02584
02585 bool a2dDocumentPrintout::HasPage(int pageNum)
02586 {
02587 return (pageNum == 1);
02588 }
02589
02590 bool a2dDocumentPrintout::OnBeginDocument(int startPage, int endPage)
02591 {
02592 if (!wxPrintout::OnBeginDocument(startPage, endPage))
02593 return false;
02594
02595 return true;
02596 }
02597
02598 void a2dDocumentPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
02599 {
02600 *minPage = 1;
02601 *maxPage = 1;
02602 *selPageFrom = 1;
02603 *selPageTo = 1;
02604 }
02605
02606 #endif // wxUSE_PRINTING_ARCHITECTURE
02607
02608
02609
02610
02611
02612
02613 IMPLEMENT_CLASS(a2dDocumentFrame, wxFrame)
02614
02615 BEGIN_EVENT_TABLE(a2dDocumentFrame, wxFrame)
02616 EVT_MENU(wxID_EXIT, a2dDocumentFrame::OnExit)
02617 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, a2dDocumentFrame::OnMRUFile)
02618 EVT_CHANGEDFILENAME_VIEW( a2dDocumentFrame::OnChangeFilename )
02619 EVT_CLOSE_VIEW( a2dDocumentFrame::OnCloseView )
02620 EVT_CLOSE( a2dDocumentFrame::OnCloseWindow)
02621 EVT_PAINT( a2dDocumentFrame::OnPaint )
02622 EVT_ACTIVATE_VIEW_SENT_FROM_CHILD( a2dDocumentFrame::OnActivateViewSentFromChild )
02623 END_EVENT_TABLE()
02624
02625 a2dDocumentFrame::a2dDocumentFrame()
02626 {
02627 m_destroyOnCloseView = true;
02628 m_isParentFrame = true;
02629 m_view = NULL;
02630 }
02631
02632 a2dDocumentFrame::a2dDocumentFrame( bool parentFrame,
02633 wxWindow* frame,
02634 a2dView* view,
02635 wxWindowID id,
02636 const wxString& title,
02637 const wxPoint& pos,
02638 const wxSize& size,
02639 long style,
02640 const wxString& name)
02641 : wxFrame(frame, id, title, pos, size, style, name)
02642 {
02643 m_destroyOnCloseView = true;
02644 m_isParentFrame = parentFrame;
02645 m_view = view;
02646 if ( m_view )
02647 {
02648 m_view->SetDisplayWindow( this );
02649 }
02650 }
02651
02652 a2dDocumentFrame::a2dDocumentFrame(
02653 wxWindow* frame,
02654 wxWindowID id,
02655 const wxString& title,
02656 const wxPoint& pos,
02657 const wxSize& size,
02658 long style,
02659 const wxString& name)
02660 : wxFrame(frame, id, title, pos, size, style, name)
02661 {
02662 m_destroyOnCloseView = true;
02663 m_isParentFrame = true;
02664 m_view = NULL;
02665 if ( m_view )
02666 {
02667 m_view->SetDisplayWindow( this );
02668 }
02669 }
02670
02671 bool a2dDocumentFrame::Create(
02672 bool parentFrame,
02673 wxWindow* frame,
02674 a2dView* view,
02675 wxWindowID id,
02676 const wxString& title,
02677 const wxPoint& pos,
02678 const wxSize& size,
02679 long style,
02680 const wxString& name)
02681 {
02682 bool res = wxFrame::Create(frame, id, title, pos, size, style, name);
02683 m_destroyOnCloseView = true;
02684 m_isParentFrame = parentFrame;
02685 m_view = view;
02686 if ( m_view )
02687 {
02688 m_view->SetDisplayWindow( this );
02689 }
02690 return res;
02691 }
02692
02693
02694 a2dDocumentFrame::~a2dDocumentFrame()
02695 {
02696 }
02697
02698 void a2dDocumentFrame::SetView( a2dView* view )
02699 {
02700 m_view = view;
02701 }
02702
02703
02704
02705
02706 void a2dDocumentFrame::OnCloseView( a2dCloseViewEvent& event )
02707 {
02708 wxASSERT_MSG( m_view || event.GetEventObject(), wxT("not view of frame closed") );
02709
02710
02711
02712
02713
02714 if ( m_destroyOnCloseView )
02715 Destroy();
02716
02717 if ( m_view )
02718 {
02719 m_view = NULL;
02720
02721 }
02722
02723 if ( m_isParentFrame && GetParent() )
02724 GetParent()->Refresh();
02725 else
02726 Refresh();
02727 }
02728
02729 void a2dDocumentFrame::OnChangeFilename( a2dViewEvent& event )
02730 {
02731 a2dDocument* doc = ((a2dView*)event.GetEventObject())->GetDocument();
02732
02733 if ( doc )
02734 {
02735 wxString title = doc->GetPrintableName();
02736
02737 SetTitle(title);
02738 }
02739 }
02740
02741 bool a2dDocumentFrame::ProcessEvent(wxEvent& event)
02742 {
02743 static wxEvent *ActiveEvent = NULL;
02744
02745
02746 if (ActiveEvent == &event)
02747 return false;
02748
02749 ActiveEvent = &event;
02750
02751 bool ret = false;
02752 if ( event.GetEventType() == wxEVT_CLOSE_WINDOW )
02753 {
02754
02755
02756
02757
02758
02759
02760
02761
02762
02763
02764
02765
02766
02767 wxCloseEvent& closeevent = (wxCloseEvent&) event;
02768
02769 if ( m_isParentFrame )
02770 {
02771
02772
02773 if ( wxEvtHandler::ProcessEvent(event) && !closeevent.GetVeto() )
02774 {
02775
02776
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786
02787 if ( wxTheApp->GetTopWindow() == this )
02788 {
02789 if ( !a2dDocviewGlobals->GetDocviewCommandProcessor()->Exit( !closeevent.CanVeto()) )
02790 closeevent.Veto();
02791 }
02792 else
02793 {
02794 if ( !a2dDocviewGlobals->GetDocviewCommandProcessor()->Clear( !closeevent.CanVeto()) )
02795 closeevent.Veto();
02796 }
02797 }
02798 else
02799 closeevent.Veto();
02800 ret = true;
02801 }
02802 else
02803 {
02804
02805 if ( wxEvtHandler::ProcessEvent(event) && !closeevent.GetVeto() )
02806 {
02807 closeevent.SetCanVeto( false );
02808 if ( m_view )
02809 m_view->GetEventHandler()->ProcessEvent(closeevent);
02810 m_view = NULL;
02811 }
02812 else
02813 closeevent.Veto();
02814 ret = true;
02815 }
02816 }
02817 else if ( event.GetEventType() == wxEVT_ACTIVATE )
02818 {
02819
02820
02821
02822
02823 if ( m_view && !m_view->IsClosed() )
02824 m_view->GetEventHandler()->ProcessEvent(event);
02825
02826 ret = wxEvtHandler::ProcessEvent(event);
02827 }
02828 else if ( event.GetEventType() == wxEVT_IDLE )
02829 {
02830
02831
02832 if ( m_view && !m_view->IsClosed() )
02833 m_view->GetEventHandler()->ProcessEvent(event);
02834
02835 ret = wxEvtHandler::ProcessEvent(event);
02836 }
02837 else
02838 {
02839
02840
02841
02842
02843
02844
02845 if ( !wxEvtHandler::ProcessEvent(event) )
02846 {
02847
02848 if ( a2dDocviewGlobals->GetDocviewCommandProcessor() )
02849 {
02850 ret = a2dDocviewGlobals->GetDocviewCommandProcessor()->ProcessEvent(event);
02851 }
02852 else
02853 ret = false;
02854 }
02855 else
02856 ret = true;
02857 }
02858 ActiveEvent = NULL;
02859 return ret;
02860 }
02861
02862 void a2dDocumentFrame::OnCloseWindow( wxCloseEvent &event )
02863 {
02864 if ( event.CanVeto() )
02865 {
02866 if ( !GetIsParentFrame() )
02867 {
02868 if ( !m_view || m_view->Close( !event.CanVeto() ) )
02869 {
02870 Destroy();
02871 m_view = NULL;
02872 }
02873 else
02874 event.Veto( true );
02875 }
02876 else
02877 {
02878 if ( !a2dDocviewGlobals->GetDocviewCommandProcessor()->Clear( !event.CanVeto()) )
02879 event.Veto( true );
02880 }
02881 }
02882 else
02883 Destroy();
02884 }
02885
02886 void a2dDocumentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
02887 {
02888 if (m_isParentFrame)
02889 {
02890 Close( true );
02891 }
02892 }
02893
02894 void a2dDocumentFrame::OnMRUFile(wxCommandEvent& event)
02895 {
02896 if (m_isParentFrame)
02897 {
02898 int n = event.GetId() - wxID_FILE1;
02899 a2dFileHistoryItem* fileHistItem = a2dDocviewGlobals->GetDocviewCommandProcessor()->GetHistoryFileItem(n);
02900 if ( fileHistItem )
02901 {
02902
02903 if ( wxFile::Exists( fileHistItem->m_filename.GetFullPath() ) )
02904 {
02905
02906 a2dError returncode = a2dDocviewGlobals->GetDocviewCommandProcessor()->CreateDocuments( fileHistItem->m_filename.GetFullPath(), a2dREFDOC_SILENT, fileHistItem->m_docTemplate );
02907 if ( returncode == a2dError_NoDocTemplateRef )
02908 {
02909 a2dDocviewGlobals->ReportError( a2dError_CouldNotCreateDocument, _("No templates to create document, removed from history, a2dDocumentCommandProcessor::OnMRUFile"));
02910 a2dDocviewGlobals->GetDocviewCommandProcessor()->RemoveFileFromHistory(n);
02911 }
02912 }
02913 else
02914 {
02915 a2dDocviewGlobals->ReportErrorF( a2dError_FileHistory, _("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
02916 fileHistItem->m_filename.GetFullPath().c_str());
02917
02918
02919
02920 a2dDocviewGlobals->GetDocviewCommandProcessor()->RemoveFileFromHistory(n);
02921 }
02922 }
02923 }
02924 }
02925
02926 void a2dDocumentFrame::OnPaint( wxPaintEvent& WXUNUSED(event) )
02927 {
02928 wxPaintDC dc( this );
02929 PrepareDC(dc);
02930
02931 if ( m_view && m_view->GetDisplayWindow() == this )
02932 OnDraw(dc);
02933 }
02934
02935
02936 void a2dDocumentFrame::OnDraw(wxDC& dc)
02937 {
02938 if ( m_view && m_view->GetDisplayWindow() == this )
02939 m_view->OnDraw(& dc);
02940 }
02941
02942 void a2dDocumentFrame::OnActivateViewSentFromChild( a2dViewEvent& viewevent )
02943 {
02944 if ( viewevent.GetActive() )
02945 {
02946 m_view = (a2dView*) viewevent.GetEventObject();
02947 }
02948 }
02949
02950 void a2dDocumentFrame::OnCmdMenuId(wxCommandEvent &event)
02951 {
02952 event.Skip();
02953 }
02954
02955 void a2dDocumentFrame::AddCmdToToolbar( const a2dMenuIdItem& cmdId )
02956 {
02957 if ( ! GetToolBar() )
02958 return;
02959 Connect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentFrame::OnCmdMenuId ) );
02960
02961 wxASSERT_MSG( a2dMenuIdItem::GetInitialized(), _T("call a2dMenuIdItem::GetInitialized() before using AddCmdToToolbar") );
02962
02963 wxString error = _T("No Bitmap for a2dToolCmd found for:") + cmdId.GetIdName();
02964 wxASSERT_MSG( cmdId.GetBitmap(false).Ok(), error );
02965 GetToolBar()->AddTool( cmdId.GetId(), cmdId.GetLabel(), cmdId.GetBitmap(false), cmdId.GetHelp() );
02966 }
02967
02968 void a2dDocumentFrame::ConnectCmdId( const a2dMenuIdItem& cmdId )
02969 {
02970 Connect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentFrame::OnCmdMenuId ) );
02971 }
02972
02973 void a2dDocumentFrame::AddCmdMenu( wxMenu* parentMenu, wxMenuItem* item )
02974 {
02975 Connect( item->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentFrame::OnCmdMenuId ) );
02976 parentMenu->Append( item );
02977 }
02978
02979 void a2dDocumentFrame::AddCmdMenu( wxMenu* parentMenu, const a2dMenuIdItem& cmdId )
02980 {
02981 Connect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentFrame::OnCmdMenuId ) );
02982 parentMenu->Append( cmdId.GetId(), cmdId.GetLabel(), cmdId.GetHelp(), cmdId.IsCheckable() );
02983 }
02984
02985 void a2dDocumentFrame::RemoveCmdMenu( wxMenu* parentMenu, const a2dMenuIdItem& cmdId )
02986 {
02987 Disconnect( cmdId.GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( a2dDocumentFrame::OnCmdMenuId ) );
02988 parentMenu->Delete( cmdId.GetId() );
02989 }
02990
02991 void a2dDocumentFrame::OnExecuteCommand(wxCommandEvent &event)
02992 {
02993 a2dCommandStore* command = (a2dCommandStore*) event.m_callbackUserData;
02994 a2dDocviewGlobals->GetDocviewCommandProcessor()->Submit( command->m_smrtP->TClone() );
02995 }
02996
02997