00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "docviewprec.h"
00014
00015 #ifdef __BORLANDC__
00016 #pragma hdrstop
00017 #endif
00018
00019 #ifndef WX_PRECOMP
00020 #include "wx/wx.h"
00021 #endif
00022
00023
00024
00025
00026
00027 #ifndef WX_PRECOMP
00028 #include "wx/string.h"
00029 #include "wx/utils.h"
00030 #include "wx/app.h"
00031 #include "wx/dc.h"
00032 #include "wx/dialog.h"
00033 #include "wx/menu.h"
00034 #include "wx/list.h"
00035 #include "wx/filedlg.h"
00036 #include "wx/intl.h"
00037 #endif
00038
00039
00040 #ifdef __WXGTK__
00041 #include "wx/mdi.h"
00042 #endif
00043
00044 #if wxUSE_PRINTING_ARCHITECTURE
00045 #include "wx/prntbase.h"
00046 #include "wx/printdlg.h"
00047 #endif
00048
00049 #include "wx/msgdlg.h"
00050 #include "wx/choicdlg.h"
00051 #include "wx/confbase.h"
00052 #include "wx/file.h"
00053 #include "wx/cmdproc.h"
00054 #include "wx/log.h"
00055 #include "wx/textdlg.h"
00056
00057 #include <wx/tokenzr.h>
00058 #include <wx/regex.h>
00059
00060 #if wxUSE_PRINTING_ARCHITECTURE
00061 #include <wx/paper.h>
00062 #endif
00063
00064 #include <stdio.h>
00065 #include <string.h>
00066
00067 #include "wx/docview.h"
00068 #include "wx/docview/docviewref.h"
00069 #include "wx/general/gen.h"
00070 #include "wx/docview/doccom.h"
00071 #include "wx/docview/doccomimpl.h"
00072 #include "wx/docview/cparser.h"
00073
00074
00075
00076
00077
00078
00079
00080 #if (defined(__WXMSW__) && defined(WXUSINGDLL) )
00081 template class A2DDOCVIEWDLLEXP a2dSmrtPtrList< a2dCommandLanguageWrapper >;
00082 #endif
00083
00084
00085
00086
00087
00088 DEFINE_EVENT_TYPE(wxEVT_ADD_DOCUMENT)
00089 DEFINE_EVENT_TYPE(wxEVT_REMOVE_DOCUMENT)
00090 DEFINE_EVENT_TYPE(wxEVT_CHANGED_DOCUMENT)
00091 DEFINE_EVENT_TYPE(wxEVT_RECORD)
00092 DEFINE_EVENT_TYPE(wxEVT_CANNOT_OPEN_DOCUMENT)
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 a2dError a2dFileSelectorEx( const wxString& message = wxFileSelectorPromptStr,
00111 const wxString& default_path = wxEmptyString,
00112 const wxString& default_filename = wxEmptyString,
00113 int *indexDefaultExtension = NULL,
00114 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
00115 int flags = 0,
00116 wxWindow *parent = NULL,
00117 wxArrayString *returnPaths = NULL,
00118 int x = wxDefaultCoord, int y = wxDefaultCoord )
00119 {
00120 wxASSERT_MSG( returnPaths, wxT("must have return path wxArrayString") );
00121 returnPaths->Clear();
00122
00123 wxFileDialog fileDialog(parent,
00124 message,
00125 default_path,
00126 default_filename,
00127 wildcard,
00128 flags, wxPoint(x, y));
00129
00130
00131 if (indexDefaultExtension && *indexDefaultExtension > 0)
00132 fileDialog.SetFilterIndex(*indexDefaultExtension);
00133
00134 if ( fileDialog.ShowModal() == wxID_OK )
00135 {
00136 if ( indexDefaultExtension )
00137 *indexDefaultExtension = fileDialog.GetFilterIndex();
00138
00139 if(flags & wxFD_MULTIPLE)
00140 fileDialog.GetPaths(*returnPaths);
00141 else
00142 returnPaths->Add( fileDialog.GetPath() );
00143 return a2dError_NoError;
00144 }
00145 return a2dError_Canceled;
00146 }
00147
00148
00149
00150
00151
00152 IMPLEMENT_DYNAMIC_CLASS(a2dDocumentCommandProcessor, a2dCommandProcessor )
00153
00154
00155 const a2dMenuIdItem CmdMenu_Exit( wxT("CmdMenu_Exit"), wxT("&Exit\tAlt-X"), wxT("Exit application") );
00156 const a2dMenuIdItem CmdMenu_FileClose( wxT("CmdMenu_FileClose"), wxT("&Close"), wxT("Close current file") );
00157 const a2dMenuIdItem CmdMenu_FileOpen( wxT("CmdMenu_FileOpen"), wxT("&Open"), wxT("Open existing file") );
00158 const a2dMenuIdItem CmdMenu_FileNew( wxT("CmdMenu_FileNew"), wxT("&New"), wxT("Create new file") );
00159 const a2dMenuIdItem CmdMenu_FileSave( wxT("CmdMenu_FileSave"), wxT("Save"), wxT("Save file") );
00160 const a2dMenuIdItem CmdMenu_FileSaveAs( wxT("CmdMenu_FileSaveAs"), wxT("Save &As"), wxT("Save file with different Name") );
00161 const a2dMenuIdItem CmdMenu_FileImport( wxT("CmdMenu_FileImport"), wxT("&Import"), wxT("Import file into current document") );
00162 const a2dMenuIdItem CmdMenu_FileExport( wxT("CmdMenu_FileExport"), wxT("&Export"), wxT("Export file to formatX") );
00163 const a2dMenuIdItem CmdMenu_FileRevert( wxT("CmdMenu_FileRevert"), wxT("&Revert"), wxT("reload file") );
00164 const a2dMenuIdItem CmdMenu_Print( wxT("CmdMenu_Print"), wxT("Print"), wxT("&Print") );
00165 const a2dMenuIdItem CmdMenu_Preview( wxT("CmdMenu_Preview"), wxT("Preview"), wxT("Preview") );
00166 const a2dMenuIdItem CmdMenu_PrintView( wxT("CmdMenu_PrintView"), wxT("Print View"), wxT("Print View") );
00167 const a2dMenuIdItem CmdMenu_PreviewView( wxT("CmdMenu_PreviewView"), wxT("Preview View"), wxT("Preview Print View") );
00168 const a2dMenuIdItem CmdMenu_PrintDocument( wxT("CmdMenu_PrintDocument"), wxT("Print Document"), wxT("Print Document") );
00169 const a2dMenuIdItem CmdMenu_PreviewDocument( wxT("CmdMenu_PreviewDocument"), wxT("Preview Document"), wxT("Preview Print Document") );
00170 const a2dMenuIdItem CmdMenu_PrintSetup( wxT("CmdMenu_PrintSetup"), wxT("Print Setup"), wxT("Setup Print") );
00171 const a2dMenuIdItem CmdMenu_EmptyDocument( wxT("CmdMenu_EmptyDocument"), wxT("Empty Document"), wxT("Remove contents of document") );
00172
00173
00174
00175
00176
00177
00178
00179 const a2dCommandId a2dCommand_Exit::Id( wxT("Exit") );
00180 const a2dCommandId a2dCommand_FileClose::Id( wxT("FileClose") );
00181 const a2dCommandId a2dCommand_FileOpen::Id( wxT("FileOpen") );
00182 const a2dCommandId a2dCommand_FileNew::Id( wxT("FileNew") );
00183 const a2dCommandId a2dCommand_FileSave::Id( wxT("FileSave") );
00184 const a2dCommandId a2dCommand_FileSaveAs::Id( wxT("FileSaveAs") );
00185 const a2dCommandId a2dCommand_FileImport::Id( wxT("FileImport") );
00186 const a2dCommandId a2dCommand_FileExport::Id( wxT("FileExport") );
00187 const a2dCommandId a2dCommand_FileRevert::Id( wxT("FileRevert") );
00188 const a2dCommandId a2dCommand_Print::Id( wxT("Print") );
00189 const a2dCommandId a2dCommand_CreateDocument::Id( wxT("CreateDocument") );
00190 const a2dCommandId a2dCommand_CloseDocuments::Id( wxT("CloseDocuments") );
00191 const a2dCommandId a2dCommand_Clear::Id( wxT("Clear") );
00192 const a2dCommandId a2dCommand_Ask::Id( wxT("Ask") );
00193 const a2dCommandId a2dCommand_AskFile::Id( wxT("AskFile") );
00194 const a2dCommandId a2dCommand_ShowVar::Id( wxT("ShowVar") );
00195 const a2dCommandId a2dCommand_SetActivateView::Id( wxT("SetActivateView") );
00196
00197
00198 A2D_BEGIN_EVENT_TABLE( a2dDocumentCommandProcessor, a2dCommandProcessor )
00199 A2D_EVT_MENU( CmdMenu_Exit.GetId(), a2dDocumentCommandProcessor::OnMenu )
00200 A2D_EVT_MENU( CmdMenu_FileClose.GetId(), a2dDocumentCommandProcessor::OnMenu )
00201 A2D_EVT_MENU( CmdMenu_FileOpen.GetId(), a2dDocumentCommandProcessor::OnMenu )
00202 A2D_EVT_MENU( CmdMenu_FileNew.GetId(), a2dDocumentCommandProcessor::OnMenu )
00203 A2D_EVT_MENU( CmdMenu_FileSave.GetId(), a2dDocumentCommandProcessor::OnMenu )
00204 A2D_EVT_MENU( CmdMenu_FileSaveAs.GetId(), a2dDocumentCommandProcessor::OnMenu )
00205 A2D_EVT_MENU( CmdMenu_FileImport.GetId(), a2dDocumentCommandProcessor::OnMenu )
00206 A2D_EVT_MENU( CmdMenu_FileExport.GetId(), a2dDocumentCommandProcessor::OnMenu )
00207 A2D_EVT_MENU( CmdMenu_FileRevert.GetId(), a2dDocumentCommandProcessor::OnMenu )
00208 A2D_EVT_MENU( CmdMenu_Print.GetId(), a2dDocumentCommandProcessor::OnMenu )
00209 A2D_EVT_MENU( CmdMenu_Preview.GetId(), a2dDocumentCommandProcessor::OnMenu )
00210 A2D_EVT_MENU( CmdMenu_PrintView.GetId(), a2dDocumentCommandProcessor::OnMenu )
00211 A2D_EVT_MENU( CmdMenu_PreviewView.GetId(), a2dDocumentCommandProcessor::OnMenu )
00212 A2D_EVT_MENU( CmdMenu_PrintDocument.GetId(), a2dDocumentCommandProcessor::OnMenu )
00213 A2D_EVT_MENU( CmdMenu_PreviewDocument.GetId(), a2dDocumentCommandProcessor::OnMenu )
00214 A2D_EVT_MENU( CmdMenu_PrintSetup.GetId(), a2dDocumentCommandProcessor::OnMenu )
00215 A2D_EVT_MENU( CmdMenu_EmptyDocument.GetId(), a2dDocumentCommandProcessor::OnMenu )
00216 A2D_EVT_UPDATE_UI( CmdMenu_FileOpen.GetId(), a2dDocumentCommandProcessor::OnUpdateFileOpen)
00217 A2D_EVT_UPDATE_UI( CmdMenu_FileClose.GetId(), a2dDocumentCommandProcessor::OnUpdateFileClose)
00218 A2D_EVT_UPDATE_UI( CmdMenu_FileRevert.GetId(), a2dDocumentCommandProcessor::OnUpdateFileRevert)
00219 A2D_EVT_UPDATE_UI( CmdMenu_FileNew.GetId(), a2dDocumentCommandProcessor::OnUpdateFileNew)
00220 A2D_EVT_UPDATE_UI( CmdMenu_FileSave.GetId(), a2dDocumentCommandProcessor::OnUpdateFileSave)
00221 A2D_EVT_UPDATE_UI( CmdMenu_FileSaveAs.GetId(), a2dDocumentCommandProcessor::OnUpdateFileSaveAs)
00222 A2D_EVT_UPDATE_UI(wxID_UNDO, a2dDocumentCommandProcessor::OnUpdateUndo)
00223 A2D_EVT_UPDATE_UI(wxID_REDO, a2dDocumentCommandProcessor::OnUpdateRedo)
00224
00225 #if wxUSE_PRINTING_ARCHITECTURE
00226 A2D_EVT_UPDATE_UI( CmdMenu_Print.GetId(), a2dDocumentCommandProcessor::OnUpdatePrint)
00227 A2D_EVT_UPDATE_UI( CmdMenu_Preview.GetId(), a2dDocumentCommandProcessor::OnUpdatePreview)
00228 #endif
00229
00230 A2D_EVT_ACTIVATE_VIEW( a2dDocumentCommandProcessor::OnActivateView )
00231 A2D_EVT_REMOVE_VIEW( a2dDocumentCommandProcessor::OnRemoveView )
00232 A2D_EVT_ADD_VIEW( a2dDocumentCommandProcessor::OnAddView )
00233
00234 A2D_EVT_CANNOT_OPEN_DOCUMENT( a2dDocumentCommandProcessor::OnCannotOpenDocument )
00235
00236 A2D_END_EVENT_TABLE()
00237
00238 a2dDocumentCommandProcessor::a2dDocumentCommandProcessor(long flags, bool WXUNUSED(initialize), int maxCommands )
00239 : a2dCommandProcessor( maxCommands )
00240 #if defined(_DEBUG) && defined (SMART_POINTER_DEBUG)
00241 , m_initCurrentSmartPointerOwner(this)
00242 #endif
00243 {
00244 m_defaultDocumentNameCounter = 1;
00245 m_flags = flags;
00246 m_lastView = m_currentView = (a2dView*) NULL;
00247 m_maxDocsOpen = 10000;
00248 m_fileHistory = (a2dFileHistory *) NULL;
00249 #if wxUSE_PRINTING_ARCHITECTURE
00250 m_pageSetupData = new wxPageSetupData;
00251 #endif
00252 Initialize();
00253 m_currentDocument = NULL;
00254 m_lastView = m_currentView = NULL;
00255 m_busyExit = false;
00256
00257 }
00258
00259 a2dDocumentCommandProcessor::a2dDocumentCommandProcessor( a2dDocumentCommandProcessor* other )
00260 : a2dCommandProcessor( other->m_maxNoCommands )
00261 #if defined(_DEBUG) && defined (SMART_POINTER_DEBUG)
00262 , m_initCurrentSmartPointerOwner(this)
00263 #endif
00264 {
00265 m_defaultDocumentNameCounter = 1;
00266 m_flags = other->m_flags;
00267 m_lastView = m_currentView = (a2dView*) NULL;
00268 m_maxDocsOpen = 10000;
00269
00270 #if wxUSE_PRINTING_ARCHITECTURE
00271 m_pageSetupData = new wxPageSetupData(*other->m_pageSetupData);
00272 #endif
00273 a2dCommandProcessor::Initialize();
00274
00275 m_fileHistory = new a2dFileHistory;
00276 size_t j;
00277 for ( j = 0; j < other->m_fileHistory->GetCount(); j++ )
00278 {
00279 m_fileHistory->AddFileToHistory( other->GetHistoryFile(j) );
00280 }
00281
00282 m_currentDocument = NULL;
00283 m_lastView = m_currentView = NULL;
00284
00285 m_docTemplates = other->m_docTemplates;
00286 m_viewTemplates = other->m_viewTemplates;
00287 m_busyExit = false;
00288 }
00289
00290 a2dDocumentCommandProcessor::~a2dDocumentCommandProcessor()
00291 {
00292 m_busyExit = true;
00293 Clear();
00294 if (m_fileHistory)
00295 delete m_fileHistory;
00296 #if wxUSE_PRINTING_ARCHITECTURE
00297 if(m_pageSetupData)
00298 delete m_pageSetupData;
00299 #endif
00300 }
00301
00302 void a2dDocumentCommandProcessor::OnMenu( wxCommandEvent& event )
00303 {
00304 if ( event.GetId() == CmdMenu_Exit.GetId() )
00305 {
00306 Exit();
00307 }
00308 else if ( event.GetId() == CmdMenu_FileClose.GetId() )
00309 {
00310 FileClose();
00311 }
00312 else if ( event.GetId() == CmdMenu_FileOpen.GetId() )
00313 {
00314 a2dDocumentPtr doc = NULL;
00315 a2dError result = FileOpen( doc, wxFileName( wxT("") ) );
00316 if ( !doc )
00317 {
00318 if ( result == a2dError_Canceled )
00319 {
00320 }
00321 }
00322 }
00323 else if ( event.GetId() == CmdMenu_FileNew.GetId() )
00324 {
00325 a2dDocumentPtr doc;
00326 a2dError result = FileNew( doc );
00327 }
00328 else if ( event.GetId() == CmdMenu_FileSave.GetId() )
00329 {
00330 FileSave();
00331 }
00332 else if ( event.GetId() == CmdMenu_FileSaveAs.GetId() )
00333 {
00334 FileSaveAs();
00335 }
00336 else if ( event.GetId() == CmdMenu_FileImport.GetId() )
00337 {
00338 FileImport();
00339 }
00340 else if ( event.GetId() == CmdMenu_FileExport.GetId() )
00341 {
00342 FileExport();
00343 }
00344 else if ( event.GetId() == CmdMenu_FileRevert.GetId() )
00345 {
00346 FileRevert();
00347 }
00348 else if ( event.GetId() == CmdMenu_Print.GetId() )
00349 {
00350 Print( new a2dCommand_Print( a2dCommand_Print::Args().what( a2dCommand_Print::PrintView ) ));
00351 }
00352 else if ( event.GetId() == CmdMenu_PrintView.GetId() )
00353 {
00354 Print( new a2dCommand_Print( a2dCommand_Print::Args().what( a2dCommand_Print::PrintView ) ));
00355 }
00356 else if ( event.GetId() == CmdMenu_PreviewView.GetId() )
00357 {
00358 Preview( new a2dCommand_Print( a2dCommand_Print::Args().what( a2dCommand_Print::PreviewView ) ));
00359 }
00360 else if ( event.GetId() == CmdMenu_PrintDocument.GetId() )
00361 {
00362 Print( new a2dCommand_Print( a2dCommand_Print::Args().what( a2dCommand_Print::PrintDocument ) ));
00363 }
00364 else if ( event.GetId() == CmdMenu_PreviewDocument.GetId() )
00365 {
00366 Preview( new a2dCommand_Print( a2dCommand_Print::Args().what( a2dCommand_Print::PreviewDocument ) ));
00367 }
00368 else if ( event.GetId() == CmdMenu_PrintSetup.GetId() )
00369 {
00370 Print( new a2dCommand_Print( a2dCommand_Print::Args().what( a2dCommand_Print::PrintSetup ) ));
00371 }
00372 else if ( event.GetId() == CmdMenu_EmptyDocument.GetId() )
00373 {
00374 if ( !m_currentDocument )
00375 return;
00376
00377 if ( !m_currentDocument->GetCommandProcessor() )
00378 return;
00379
00380 m_currentDocument->DeleteContents();
00381 m_currentDocument->GetCommandProcessor()->Initialize();
00382 }
00383 a2dDocviewGlobals->SendToLogTarget();
00384 a2dDocviewGlobals->ResetErrors();
00385 }
00386
00387 void a2dDocumentCommandProcessor::OnExit()
00388 {
00389 }
00390
00391 void a2dDocumentCommandProcessor::Initialize()
00392 {
00393 a2dCommandProcessor::Initialize();
00394
00395 if (m_fileHistory)
00396 delete m_fileHistory;
00397 m_fileHistory = OnCreateFileHistory();
00398 }
00399
00400 bool a2dDocumentCommandProcessor::SubmitToDocument( a2dCommand *command, bool storeIt )
00401 {
00402 if ( !m_currentDocument )
00403 return false;
00404
00405 if ( !m_currentDocument->GetCommandProcessor() )
00406 return false;
00407
00408 return m_currentDocument->GetCommandProcessor()->Submit( command, storeIt );
00409 }
00410
00411 bool a2dDocumentCommandProcessor::CloseDocuments(bool force)
00412 {
00413 m_currentView = NULL;
00414 m_currentDocument = NULL;
00415
00416 bool all = true;
00417
00418 a2dDocumentList::iterator iter = m_docs.begin();
00419 while( iter != m_docs.end() )
00420 {
00421
00422
00423
00424
00425
00426
00427 a2dDECLARE_LOCAL_ITEM(a2dDocumentList::value_type, doc, *iter);
00428
00429 iter++;
00430 if ( doc->Close( force ) || force )
00431 {
00432
00433
00434 doc->DisConnectAllViews();
00435
00436
00437
00438
00439
00440 RemoveDocument( doc );
00441 }
00442 else
00443 all = false;
00444
00445 }
00446 return all;
00447 }
00448
00449 bool a2dDocumentCommandProcessor::Clear(bool force)
00450 {
00451 if (!CloseDocuments(force))
00452 return false;
00453
00454 m_docTemplates.clear();
00455 m_viewTemplates.clear();
00456 return true;
00457 }
00458
00459 bool a2dDocumentCommandProcessor::Exit(bool force)
00460 {
00461 if ( m_busyExit )
00462 return true;
00463
00464 m_busyExit = true;
00465
00466 if ( Clear( force ) )
00467 {
00468
00469 OnExit();
00470
00471
00472
00473 wxFrame *pf = (wxFrame*) wxTheApp->GetTopWindow();
00474 if ( wxTopLevelWindows.Find( pf ) && !wxPendingDelete.Member( pf ) )
00475 {
00476 pf->Close(true);
00477 }
00478 return true;
00479 }
00480 return false;
00481 }
00482
00483 a2dFileHistory *a2dDocumentCommandProcessor::OnCreateFileHistory()
00484 {
00485 return new a2dFileHistory;
00486 }
00487
00488 bool a2dDocumentCommandProcessor::FileClose( bool force )
00489 {
00490 a2dDocument *doc = GetCurrentDocument();
00491 if (!doc)
00492 return false;
00493
00494 if (doc->Close( force ))
00495 {
00496 RemoveDocument( doc );
00497 return true;
00498 }
00499 return false;
00500 }
00501
00502 a2dError a2dDocumentCommandProcessor::FileNew( a2dDocumentPtr& doc, a2dTemplateFlagMask docTemplateFlags )
00503 {
00504 doc = NULL;
00505 a2dError result;
00506 result = CreateDocuments(wxT(""), a2dREFDOC_NEW | a2dREFDOC_INIT, NULL, wxFD_OPEN, docTemplateFlags );
00507 if ( result == a2dError_NoError )
00508 doc = m_docs.back();
00509 else if ( result == a2dError_Canceled )
00510 doc = NULL;
00511 else
00512 {
00513 a2dDocviewGlobals->ReportError( result, _("could not create document, a2dDocumentCommandProcessor::FileNew"));
00514 doc = NULL;
00515 }
00516
00517 return result;
00518 }
00519
00520 a2dError a2dDocumentCommandProcessor::FileOpen( a2dDocumentPtr& doc, const wxFileName& file, a2dTemplateFlagMask docTemplateFlags )
00521 {
00522 a2dError result ;
00523 wxString filename = file.GetFullPath();
00524
00525 doc = NULL;
00526 if ( filename.IsEmpty() )
00527 {
00528 result = CreateDocuments(wxT(""), a2dREFDOC_NON, NULL, wxFD_OPEN, docTemplateFlags );
00529 if ( result == a2dError_NoError )
00530 doc = m_docs.back();
00531 else if ( result == a2dError_Canceled )
00532 doc = NULL;
00533 else if ( result == a2dError_NoDocTemplateRef )
00534 {
00535 a2dDocviewGlobals->ReportError( a2dError_CouldNotCreateDocument, _("No templates, could not create document, a2dDocumentCommandProcessor::FileOpen"));
00536 doc = NULL;
00537 }
00538 else
00539 {
00540 a2dDocviewGlobals->ReportError( result, _("could not create document, a2dDocumentCommandProcessor::FileOpen"));
00541 doc = NULL;
00542 }
00543 }
00544 else
00545 {
00546 a2dPathList path;
00547 path.Add( wxT(".") );
00548 if ( !path.ExpandPath( filename ) )
00549 {
00550 a2dDocviewGlobals->ReportErrorF( a2dError_CouldNotEvaluatePath, _("Could not expand %s resulted in %s"), file.GetFullPath().c_str(),filename.c_str() );
00551 result = a2dError_CouldNotEvaluatePath;
00552 return result;
00553 }
00554 result = CreateDocuments( filename, a2dREFDOC_SILENT, NULL, wxFD_OPEN, docTemplateFlags );
00555 if ( result == a2dError_NoError )
00556 doc = m_docs.back();
00557 else if ( result == a2dError_Canceled )
00558 doc = NULL;
00559 else if ( result == a2dError_NoDocTemplateRef )
00560 {
00561 a2dDocviewGlobals->ReportError( a2dError_CouldNotCreateDocument, _("No templates, could not create document, a2dDocumentCommandProcessor::FileOpen"));
00562 doc = NULL;
00563 }
00564 else
00565 {
00566 a2dDocviewGlobals->ReportError( result, _("could not create document, a2dDocumentCommandProcessor::FileOpen"));
00567 doc = NULL;
00568 }
00569 }
00570
00571
00572 return result;
00573 }
00574
00575 a2dError a2dDocumentCommandProcessor::FilesOpen( const wxString& openPath, int dialogFlags, a2dTemplateFlagMask mask )
00576 {
00577 a2dError result;
00578 wxString expandedPath = openPath;
00579 a2dPathList path;
00580 path.Add( wxT(".") );
00581 if ( !path.ExpandPath( expandedPath ) )
00582 {
00583 a2dDocviewGlobals->ReportErrorF( a2dError_CouldNotEvaluatePath, _("Could not expand %s resulted in %s"), openPath.c_str(),expandedPath.c_str() );
00584 result = a2dError_CouldNotEvaluatePath;
00585 return result;
00586 }
00587 result = CreateDocuments( openPath, a2dREFDOC_NON, NULL, dialogFlags, mask );
00588 if ( result == a2dError_NoError )
00589 {
00590 }
00591 else if ( result == a2dError_Canceled )
00592 {
00593 }
00594 else
00595 {
00596 a2dDocviewGlobals->ReportError( result, _("could not create document, a2dDocumentCommandProcessor::FilesOpen"));
00597 }
00598
00599 return result;
00600 }
00601
00602 bool a2dDocumentCommandProcessor::FileRevert()
00603 {
00604 a2dDocument *doc = GetCurrentDocument();
00605 if (!doc)
00606 return false;
00607 doc->Revert();
00608 return true;
00609 }
00610
00611 bool a2dDocumentCommandProcessor::FileSave()
00612 {
00613 a2dDocument *doc = GetCurrentDocument();
00614 if (!doc)
00615 return false;
00616 doc->Save();
00617 return true;
00618 }
00619
00620 bool a2dDocumentCommandProcessor::FileSaveAs( const wxFileName& file, a2dDocumentFlagMask flags )
00621 {
00622 a2dDocument *doc = GetCurrentDocument();
00623 if (!doc)
00624 return false;
00625
00626 a2dPathList path;
00627 path.Add( wxT(".") );
00628 wxString foundfile = file.GetFullPath();
00629 if ( !path.ExpandPath( foundfile ) )
00630 {
00631 a2dDocviewGlobals->ReportErrorF( a2dError_CouldNotEvaluatePath, _("Could not expand %s resulted in %s"), file.GetFullPath().c_str(),foundfile.c_str() );
00632 return false;
00633 }
00634
00635 return doc->SaveAs( foundfile, flags );
00636 }
00637
00638 bool a2dDocumentCommandProcessor::FileExport( const wxFileName& file, const wxString& description, a2dDocumentFlagMask flags )
00639 {
00640 a2dDocument *doc = GetCurrentDocument();
00641 if (!doc)
00642 return false;
00643
00644 a2dDocumentTemplate* found = NULL;
00645 size_t n = 0;
00646 const_forEachIn( a2dDocumentTemplateList, &m_docTemplates )
00647 {
00648 a2dDocumentTemplateList::value_type temp = *iter;
00649 if ( temp->CheckMask( a2dTemplateFlag::EXPORTING ) )
00650 {
00651 if ( (description.IsEmpty() && !file.GetExt().IsEmpty() && file.GetExt() == temp->GetDefaultExtension() ) ||
00652 temp->GetDescription() == description )
00653 found = temp;
00654 n++;
00655 }
00656 }
00657
00658 a2dPathList path;
00659 path.Add( wxT(".") );
00660 wxString foundfile = file.GetFullPath();
00661 if ( !path.ExpandPath( foundfile ) )
00662 {
00663 a2dDocviewGlobals->ReportErrorF( a2dError_CouldNotEvaluatePath, _("Could not expand %s resulted in %s"), file.GetFullPath().c_str(),foundfile.c_str() );
00664 return false;
00665 }
00666
00667
00668 return doc->Export( found, foundfile, flags );
00669 }
00670
00671 bool a2dDocumentCommandProcessor::FileImport( const wxFileName& file, const wxString& description, a2dDocumentFlagMask flags )
00672 {
00673 a2dDocument *doc = GetCurrentDocument();
00674 if (!doc)
00675 return false;
00676
00677 a2dDocumentTemplate* found = NULL;
00678 size_t n = 0;
00679 const_forEachIn( a2dDocumentTemplateList, &m_docTemplates )
00680 {
00681 a2dDocumentTemplateList::value_type temp = *iter;
00682 if ( temp->CheckMask( a2dTemplateFlag::IMPORTING ) )
00683 {
00684 if ( (description.IsEmpty() && !file.GetExt().IsEmpty() && file.GetExt() == temp->GetDefaultExtension() ) ||
00685 temp->GetDescription() == description )
00686 found = temp;
00687 n++;
00688 }
00689 }
00690
00691 a2dPathList path;
00692 path.Add( wxT(".") );
00693 wxString foundfile = file.GetFullPath();
00694 if ( !path.ExpandPath( foundfile ) )
00695 {
00696 a2dDocviewGlobals->ReportErrorF( a2dError_CouldNotEvaluatePath, _("Could not expand %s resulted in %s"), file.GetFullPath().c_str(),foundfile.c_str() );
00697 return false;
00698 }
00699
00700
00701 return doc->Import( found, foundfile, flags );
00702 }
00703
00704
00705 bool a2dDocumentCommandProcessor::Print( a2dCommand* command )
00706 {
00707 #if wxUSE_PRINTING_ARCHITECTURE
00708 a2dView *view = GetCurrentView();
00709 if (!view)
00710 return false;
00711
00712 wxPageSetupData* aPageSetupData = NULL;
00713 if (view && view->GetViewTemplate())
00714 aPageSetupData = view->GetViewTemplate()->GetPageSetupData();
00715 if(!aPageSetupData)
00716 {
00717 a2dDocument *aDoc = GetCurrentDocument();
00718 if (aDoc && aDoc->GetDocumentTemplate())
00719 aPageSetupData = aDoc->GetDocumentTemplate()->GetPageSetupData();
00720 }
00721 if(!aPageSetupData)
00722 aPageSetupData = GetPageSetupData();
00723
00724 wxPrintDialogData printDialogData( aPageSetupData->GetPrintData());
00725
00726 wxPrintout *printout = view->OnCreatePrintout( command );
00727 if (printout)
00728 {
00729 wxPrinter printer(&printDialogData);
00730 if(printer.Print( view->GetDisplayWindow(), printout, true))
00731 aPageSetupData->SetPrintData(printer.GetPrintDialogData().GetPrintData());
00732
00733 delete printout;
00734 }
00735 #endif // wxUSE_PRINTING_ARCHITECTURE
00736 return true;
00737 }
00738
00739 bool a2dDocumentCommandProcessor::Preview( a2dCommand* command )
00740 {
00741 #if wxUSE_PRINTING_ARCHITECTURE
00742 a2dView *view = GetCurrentView();
00743 if (!view)
00744 return false;
00745 wxPageSetupData* aPageSetupData = NULL;
00746 if (view && view->GetViewTemplate())
00747 aPageSetupData = view->GetViewTemplate()->GetPageSetupData();
00748 if(!aPageSetupData)
00749 {
00750 a2dDocument *aDoc = GetCurrentDocument();
00751 if (aDoc && aDoc->GetDocumentTemplate())
00752 aPageSetupData = aDoc->GetDocumentTemplate()->GetPageSetupData();
00753 }
00754 if(!aPageSetupData)
00755 aPageSetupData = GetPageSetupData();
00756
00757 wxPrintDialogData printDialogData(aPageSetupData->GetPrintData());
00758
00759 wxPrintout *printout = view->OnCreatePrintout( command );
00760 if (printout)
00761 {
00762
00763 wxPrintPreviewBase *preview = (wxPrintPreviewBase *) NULL;
00764 preview = new wxPrintPreview(printout, view->OnCreatePrintout( command ), &printDialogData);
00765 if ( !preview->Ok() )
00766 {
00767 delete preview;
00768 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
00769 return false;
00770 }
00771
00772 wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"),
00773 wxPoint(100, 100), wxSize(600, 650));
00774 frame->Centre(wxBOTH);
00775 frame->Initialize();
00776 frame->Show(true);
00777 }
00778 #endif // wxUSE_PRINTING_ARCHITECTURE
00779 return true;
00780 }
00781
00782 bool a2dDocumentCommandProcessor::PrintSetup( a2dCommand* command )
00783 {
00784 #if wxUSE_PRINTING_ARCHITECTURE
00785 wxPageSetupData* aPageSetupData = NULL;
00786 a2dView *view = GetCurrentView();
00787 if (view && view->GetViewTemplate())
00788 aPageSetupData = view->GetViewTemplate()->GetPageSetupData();
00789 if(!aPageSetupData)
00790 {
00791 a2dDocument *aDoc = GetCurrentDocument();
00792 if (aDoc && aDoc->GetDocumentTemplate())
00793 aPageSetupData = aDoc->GetDocumentTemplate()->GetPageSetupData();
00794 }
00795 if(!aPageSetupData)
00796 aPageSetupData = GetPageSetupData();
00797
00798 wxPageSetupDialog pageSetupDialog((wxFrame *)wxTheApp->GetTopWindow(), aPageSetupData);
00799 pageSetupDialog.ShowModal();
00800 *aPageSetupData = pageSetupDialog.GetPageSetupData();
00801 #endif // wxUSE_PRINTING_ARCHITECTURE
00802 return true;
00803 }
00804
00805
00806 a2dView *a2dDocumentCommandProcessor::GetCurrentView() const
00807 {
00808 return m_currentView;
00809 }
00810
00811 a2dView *a2dDocumentCommandProcessor::GetLastView() const
00812 {
00813 return m_lastView;
00814 }
00815
00816 void a2dDocumentCommandProcessor::OnCannotOpenDocument( a2dCommandEvent& event )
00817 {
00818 a2dDocument* newDoc = event.GetDocument();
00819 newDoc->DisConnectAllViews();
00820 newDoc->DeleteContents();
00821 a2dDocviewGlobals->ReportError( a2dError_FileCouldNotOpen, _("File could not be opened , a2dDocumentCommandProcessor::OnCannotOpenDocument") );
00822 }
00823
00824 a2dError a2dDocumentCommandProcessor::CreateDocuments(const wxString& path, a2dDocumentFlagMask documentflags,
00825 a2dDocumentTemplate* wantedDocTemplate,
00826 int dialogflags,
00827 a2dTemplateFlagMask docTemplateFlags
00828 )
00829 {
00830
00831 static a2dDocumentTemplate* lastSelectedDocTemplate = NULL;
00832 size_t n = 0;
00833 if ( !wantedDocTemplate )
00834 {
00835
00836 const_forEachIn( a2dDocumentTemplateList, &m_docTemplates )
00837 {
00838 a2dDocumentTemplateList::value_type temp = *iter;
00839 if ( temp->CheckMask( docTemplateFlags ) )
00840 {
00841 if (n == 0)
00842 wantedDocTemplate = temp;
00843 n ++;
00844 }
00845 }
00846
00847 if (n == 0)
00848 {
00849 return a2dError_NoDocTemplateRef;
00850 }
00851 }
00852 else
00853 n = 1;
00854
00855
00856
00857
00858
00859
00860
00861
00862 if ( m_docs.size() >= m_maxDocsOpen)
00863 {
00864 a2dDECLARE_LOCAL_ITEM(a2dDocumentList::value_type, doc, m_docs.front());
00865
00866 if ( doc->Close( false ) )
00867 {
00868 doc->DisConnectAllViews();
00869 RemoveDocument( doc );
00870 }
00871 else
00872 {
00873 return a2dError_ToManyOpen;
00874 }
00875 }
00876
00877
00878 if ( documentflags & a2dREFDOC_NEW )
00879 {
00880 a2dDocumentTemplate *fileTemplate = (a2dDocumentTemplate *) NULL;
00881 if (n == 1)
00882 fileTemplate = wantedDocTemplate;
00883 else
00884 fileTemplate = SelectDocumentType( false, docTemplateFlags );
00885
00886 if (fileTemplate)
00887 {
00888 a2dDocumentList openedDocList;
00889 a2dSmrtPtr< a2dDocument > newDoc = fileTemplate->CreateDocument(path, documentflags);
00890
00891
00892
00893 if (newDoc)
00894 {
00895 a2dDocumentEvent event( wxEVT_NEW_DOCUMENT );
00896 event.SetEventObject(newDoc);
00897 newDoc->ProcessEvent( event );
00898
00899 a2dDocument* ret = fileTemplate->SentPreAddCreatedDocumentEvent( newDoc, documentflags );
00900
00901
00902
00903 if ( ret == newDoc && !newDoc->IsClosed() )
00904 {
00905
00906 AddDocument( newDoc );
00907 fileTemplate->SentPostCreateDocumentEvent( newDoc, documentflags );
00908
00909 openedDocList.push_back(newDoc);
00910
00911
00912
00913
00914
00915
00916 newDoc->SetFilename( newDoc->GetFilename(), true );
00917 newDoc->SetTitle( newDoc->GetTitle(), true );
00918 }
00919 }
00920 else
00921 {
00922 return a2dError_CouldNotCreateDocument;
00923 }
00924
00925 a2dDocumentList::iterator iter = openedDocList.begin();
00926 for( ; iter != openedDocList.end(); iter++ )
00927 {
00928 a2dDocument *aDoc = *iter;
00929
00930
00931 aDoc->UpdateAllViews();
00932 }
00933 return a2dError_NoError;
00934
00935 }
00936 else
00937 {
00938 return a2dError_NoDocTemplateRef;
00939 }
00940 }
00941
00942
00943 a2dDocumentTemplate *fileTemplate = (a2dDocumentTemplate *) NULL;
00944
00945 wxArrayString selectedPaths;
00946
00947 if (documentflags & a2dREFDOC_SILENT)
00948 {
00949 if ( n == 1 )
00950 {
00951 fileTemplate = wantedDocTemplate;
00952 selectedPaths.Add( path );
00953 }
00954 else
00955 {
00956
00957 fileTemplate = FindTemplateForPath(m_docTemplates, path );
00958 if( fileTemplate )
00959 selectedPaths.Add( path );
00960 else
00961 {
00962 return a2dError_NoDocTemplateRef;
00963 }
00964 }
00965 }
00966 else
00967 {
00968 if ( n == 1 )
00969 {
00970 a2dDocumentTemplateList docTemplates;
00971 docTemplates.push_back( wantedDocTemplate );
00972 fileTemplate = wantedDocTemplate;
00973
00974 if( a2dError_NoError == SelectDocumentPath(docTemplates, path, documentflags, &selectedPaths, &fileTemplate, dialogflags, docTemplateFlags, lastSelectedDocTemplate ) )
00975 lastSelectedDocTemplate = fileTemplate;
00976 else
00977 return a2dError_Canceled;
00978 }
00979 else
00980 {
00981
00982 if( a2dError_NoError == SelectDocumentPath(m_docTemplates, path, documentflags, &selectedPaths, &fileTemplate, dialogflags, docTemplateFlags, lastSelectedDocTemplate ) )
00983 lastSelectedDocTemplate = fileTemplate;
00984 else
00985 return a2dError_Canceled;
00986 }
00987 }
00988
00989
00990 if (fileTemplate)
00991 {
00992 fileTemplate->SetDirectory( m_lastDirectory );
00993
00994
00995 a2dDocumentList openedDocList;
00996 a2dSmrtPtr< a2dDocument > newDoc;
00997 for( size_t i=0; i<selectedPaths.GetCount(); i++ )
00998 {
00999 newDoc = fileTemplate->CreateDocument(selectedPaths[i], documentflags);
01000
01001
01002
01003 if (newDoc)
01004 {
01005 a2dDocumentEvent event( wxEVT_OPEN_DOCUMENT );
01006 event.SetFileName( selectedPaths.Item(i) );
01007 event.SetEventObject(newDoc);
01008 if ( !newDoc->ProcessEvent( event ) || !event.IsAllowed() )
01009 {
01010 a2dCommandEvent event( wxEVT_CANNOT_OPEN_DOCUMENT, newDoc );
01011 event.SetEventObject(this);
01012 a2dDocviewGlobals->GetEventDistributer()->ProcessEvent( event );
01013 return a2dError_FileCouldNotOpen;
01014 }
01015
01016 a2dDocument* ret = fileTemplate->SentPreAddCreatedDocumentEvent( newDoc, documentflags );
01017
01018
01019
01020 if ( ret == newDoc && !newDoc->IsClosed() )
01021 {
01022
01023 AddDocument( newDoc );
01024 AddFileToHistory( selectedPaths.Item(i), fileTemplate );
01025
01026 fileTemplate->SentPostCreateDocumentEvent( newDoc, documentflags );
01027
01028 openedDocList.push_back(newDoc);
01029
01030
01031
01032
01033
01034
01035 newDoc->SetFilename( newDoc->GetFilename(), true );
01036 newDoc->SetTitle( newDoc->GetTitle(), true );
01037 }
01038 }
01039 else
01040 {
01041 return a2dError_CouldNotCreateDocument;
01042 }
01043 }
01044 a2dDocumentList::iterator iter = openedDocList.begin();
01045 for( ; iter != openedDocList.end(); iter++ )
01046 {
01047 a2dDocument *aDoc = *iter;
01048
01049 aDoc->UpdateAllViews();
01050 }
01051
01052 return a2dError_NoError;
01053 }
01054
01055 return a2dError_NoDocTemplateRef;
01056 }
01057
01058 a2dView* a2dDocumentCommandProcessor::AddDocumentCreateView(
01059 a2dDocument* newDoc, const wxString& viewTypeName,
01060 a2dDocumentFlagMask documentflags,
01061 a2dTemplateFlagMask documentTemplateFlags,
01062 a2dTemplateFlagMask viewTemplateFlags )
01063 {
01064 AddCreatedDocument( newDoc, false, false, documentflags, documentTemplateFlags );
01065 return CreateView( newDoc, viewTypeName, documentflags, viewTemplateFlags );
01066 }
01067
01068 void a2dDocumentCommandProcessor::AddCreatedDocument( a2dDocument* newDoc,
01069 bool sentPreAddCreatedDocumentEvent, bool sentPostCreateDocumentEvent,
01070 a2dDocumentFlagMask documentflags,
01071 a2dTemplateFlagMask docTemplateFlags )
01072 {
01073
01074 a2dDocumentTemplate* foundtemplate = 0;
01075
01076
01077 if ( !newDoc->GetDocumentTemplate() )
01078 {
01079 const_forEachIn( a2dDocumentTemplateList, &m_docTemplates )
01080 {
01081 a2dDocumentTemplateList::value_type temp = *iter;
01082 if ( temp->CheckMask( docTemplateFlags ) )
01083 {
01084 if (temp->GetDocumentTypeName() == newDoc->GetDocumentTypeName())
01085 {
01086 foundtemplate = temp;
01087 break;
01088 }
01089 }
01090 }
01091 }
01092
01093 wxASSERT_MSG( foundtemplate,
01094 _("a2dDocumentCommandProcessor::AddCreatedDocument Could not find template for document type name") );
01095
01096 wxASSERT_MSG( foundtemplate->GetDocumentTypeName() == newDoc->GetDocumentTypeName(),
01097 _("a2dDocumentCommandProcessor::AddCreatedDocument template DocumentTypeName different from document") );
01098
01099 if ( !foundtemplate )
01100 return;
01101
01102
01103
01104
01105
01106
01107
01108
01109 if ( m_docs.size() >= m_maxDocsOpen)
01110 {
01111 a2dDocument *doc = GetDocuments().front();
01112
01113 if ( doc->Close( false ) )
01114 {
01115 doc->DisConnectAllViews();
01116 RemoveDocument( doc );
01117 }
01118 else
01119 {
01120 return;
01121 }
01122 }
01123
01124
01125
01126 newDoc->SetFilename( wxFileName(wxT("")) );
01127 newDoc->CreateCommandProcessor();
01128 newDoc->SetDocumentTypeName( foundtemplate->GetDocumentTypeName() );
01129 newDoc->SetDocumentTemplate( foundtemplate );
01130 a2dDocumentEvent event( wxEVT_NEW_DOCUMENT );
01131 event.SetEventObject(newDoc);
01132 newDoc->ProcessEvent( event );
01133
01134 if ( sentPreAddCreatedDocumentEvent )
01135 {
01136 foundtemplate->SentPreAddCreatedDocumentEvent( newDoc, documentflags );
01137 }
01138
01139
01140 AddDocument( newDoc );
01141
01142 if ( sentPostCreateDocumentEvent )
01143 {
01144 foundtemplate->SentPostCreateDocumentEvent( newDoc, documentflags );
01145
01146
01147
01148 newDoc->UpdateAllViews();
01149 }
01150 }
01151
01152 a2dView *a2dDocumentCommandProcessor::CreateView( a2dDocument *doc, const wxString& viewTypeName,
01153 a2dDocumentFlagMask flags, a2dTemplateFlagMask viewTemplateFlags )
01154 {
01155
01156 a2dViewTemplate *temp = SelectViewType( doc, m_viewTemplates, viewTypeName, false, viewTemplateFlags );
01157 if (temp)
01158 {
01159 return temp->CreateView(doc, flags );
01160 }
01161
01162 wxASSERT_MSG( viewTypeName.IsEmpty(), _("a2dDocumentCommandProcessor::CreateView could not find template of given type") );
01163
01164 return (a2dView *) NULL;
01165 }
01166
01167
01168 void a2dDocumentCommandProcessor::ReleaseTemplate(a2dDocumentTemplate* temp, long flags )
01169 {
01170 a2dSmrtPtrList<a2dDocumentTemplate>::itSmart iter;
01171 iter = m_docTemplates.Find(temp);
01172 if ( m_docTemplates.end() != iter )
01173 {
01174 m_docTemplates.erase( iter );
01175 }
01176 }
01177
01178
01179 bool a2dDocumentCommandProcessor::FlushDoc(a2dDocument *WXUNUSED(doc))
01180 {
01181 return false;
01182 }
01183
01184 a2dDocument* a2dDocumentCommandProcessor::GetCurrentDocument() const
01185 {
01186 if ( m_currentDocument )
01187 return m_currentDocument;
01188 else
01189 return (a2dDocument *) NULL;
01190 }
01191
01192 a2dCommandProcessor* a2dDocumentCommandProcessor::GetCurrentDocumentCommandProcessor() const
01193 {
01194 if ( !m_currentDocument )
01195 return false;
01196
01197 if ( !m_currentDocument->GetCommandProcessor() )
01198 return false;
01199
01200 return m_currentDocument->GetCommandProcessor();
01201 }
01202
01203
01204 bool a2dDocumentCommandProcessor::MakeDefaultName(wxString& name)
01205 {
01206 name.Printf(_T("unnamed%d"), m_defaultDocumentNameCounter);
01207 m_defaultDocumentNameCounter++;
01208
01209 return true;
01210 }
01211
01212
01213
01214 wxString a2dDocumentCommandProcessor::MakeFrameTitle(a2dDocument* doc, const wxString& modifiedIndicator)
01215 {
01216 wxString appName = wxTheApp->GetAppName();
01217 wxString title;
01218 if (!doc)
01219 title = appName;
01220 else
01221 {
01222 wxString docName = doc->GetPrintableName();
01223
01224 if (doc->IsModified())
01225 docName = docName + modifiedIndicator;
01226
01227 title = docName + wxString(wxT(" - ")) + appName;
01228 }
01229 return title;
01230 }
01231
01232
01233
01234 a2dDocumentTemplate *a2dDocumentCommandProcessor::MatchTemplate( const wxString& path )
01235 {
01236 return FindTemplateForPath(m_docTemplates, path );
01237 }
01238
01239
01240 void a2dDocumentCommandProcessor::AddFileToHistory( const wxFileName& file, a2dDocumentTemplate* docTemplate, a2dViewTemplate* viewTemplate )
01241 {
01242 if (m_fileHistory)
01243 m_fileHistory->AddFileToHistory( file, docTemplate, viewTemplate );
01244 }
01245
01246 void a2dDocumentCommandProcessor::RemoveFileFromHistory(size_t i)
01247 {
01248 if (m_fileHistory)
01249 m_fileHistory->RemoveFileFromHistory(i);
01250 }
01251
01252 wxString a2dDocumentCommandProcessor::GetHistoryFile(size_t i) const
01253 {
01254 wxString histFile;
01255
01256 if ( m_fileHistory )
01257 {
01258 a2dFileHistoryItem* item = m_fileHistory->GetHistoryFileItem( i );
01259 if ( item )
01260 histFile = item->m_filename.GetFullPath();
01261 }
01262 return histFile;
01263 }
01264
01265 a2dFileHistoryItem* a2dDocumentCommandProcessor::GetHistoryFileItem(size_t i) const
01266 {
01267 if (m_fileHistory)
01268 return m_fileHistory->GetHistoryFileItem(i);
01269 return NULL;
01270 }
01271
01272 void a2dDocumentCommandProcessor::FileHistoryUseMenu(wxMenu *menu)
01273 {
01274 if (m_fileHistory)
01275 m_fileHistory->UseMenu(menu);
01276 }
01277
01278 void a2dDocumentCommandProcessor::FileHistoryRemoveMenu(wxMenu *menu)
01279 {
01280 if (m_fileHistory)
01281 m_fileHistory->RemoveMenu(menu);
01282 }
01283
01284 #if wxUSE_CONFIG
01285 void a2dDocumentCommandProcessor::FileHistoryLoad(wxConfigBase& config)
01286 {
01287 if (m_fileHistory)
01288 m_fileHistory->Load(config);
01289 }
01290
01291 void a2dDocumentCommandProcessor::FileHistorySave(wxConfigBase& config)
01292 {
01293 if (m_fileHistory)
01294 m_fileHistory->Save(config);
01295 }
01296 #endif
01297
01298 void a2dDocumentCommandProcessor::FileHistoryAddFilesToMenu(wxMenu* menu)
01299 {
01300 if (m_fileHistory)
01301 m_fileHistory->AddFilesToMenu(menu);
01302 }
01303
01304 void a2dDocumentCommandProcessor::FileHistoryAddFilesToMenu()
01305 {
01306 if (m_fileHistory)
01307 m_fileHistory->AddFilesToMenu();
01308 }
01309
01310 size_t a2dDocumentCommandProcessor::GetHistoryFilesCount() const
01311 {
01312 if (m_fileHistory)
01313 return m_fileHistory->GetCount();
01314 else
01315 return 0;
01316 }
01317
01318
01319
01320
01321 a2dDocumentTemplate *a2dDocumentCommandProcessor::FindTemplateForPath( const a2dDocumentTemplateList& docTemplates, const wxString& path, a2dTemplateFlagMask mask )
01322 {
01323 a2dDocumentTemplate *theTemplate = (a2dDocumentTemplate *) NULL;
01324
01325
01326 const_forEachIn( a2dDocumentTemplateList, &docTemplates )
01327 {
01328 a2dDocumentTemplateList::value_type temp = *iter;
01329 if ( temp->FileMatchesTemplate(path) && temp->CheckMask( mask ) )
01330 {
01331 theTemplate = temp;
01332 break;
01333 }
01334 }
01335 return theTemplate;
01336 }
01337
01338
01339
01340
01341 wxWindow* wxFindSuitableParent()
01342 {
01343 wxWindow* parent = wxTheApp->GetTopWindow();
01344
01345 wxWindow* focusWindow = wxWindow::FindFocus();
01346 if (focusWindow)
01347 {
01348 while (focusWindow &&
01349 !focusWindow->IsKindOf(CLASSINFO(wxDialog)) &&
01350 !focusWindow->IsKindOf(CLASSINFO(wxFrame)))
01351
01352 focusWindow = focusWindow->GetParent();
01353
01354 if (focusWindow)
01355 parent = focusWindow;
01356 }
01357 return parent;
01358 }
01359
01360
01361 a2dError a2dDocumentCommandProcessor::SelectDocumentPath( const a2dDocumentTemplateList& docTemplates,
01362 const wxString& path,
01363 a2dDocumentFlagMask WXUNUSED(flags),
01364 wxArrayString* selectedPaths,
01365 a2dDocumentTemplate** chosenTemplate,
01366 int dialogflags,
01367 a2dTemplateFlagMask docTemplateFlags,
01368 const a2dDocumentTemplate* preferedTemplate )
01369 {
01370 wxString descrBuf;
01371 int FilterIndex = -1;
01372
01373
01374 #if defined(__WXMSW__) || defined(__WXGTK__)
01375
01376
01377 int filterCount = 0;
01378 const_forEachIn( a2dDocumentTemplateList, &docTemplates )
01379 {
01380 a2dDocumentTemplateList::value_type temp = *iter;
01381 if ( temp->CheckMask( docTemplateFlags ) )
01382 {
01383 if(temp == preferedTemplate)
01384 FilterIndex = filterCount;
01385
01386 filterCount++;
01387
01388 if ( !descrBuf.IsEmpty() )
01389 descrBuf << wxT('|');
01390
01391 descrBuf << temp->GetDescription()
01392 << wxT(" (") << temp->GetFileFilter() << wxT(") |")
01393 << temp->GetFileFilter();
01394 }
01395 }
01396 #else
01397 descrBuf << wxT("*.*");
01398 #endif
01399
01400
01401 if ( !path.IsEmpty() )
01402 m_lastDirectory = path;
01403 else
01404 {
01405
01406 if ( preferedTemplate )
01407 m_lastDirectory = preferedTemplate->GetDirectory();
01408
01409 }
01410
01411 wxWindow* parent = wxFindSuitableParent();
01412
01413 *chosenTemplate = (a2dDocumentTemplate *)NULL;
01414
01415 if ( a2dError_Canceled == a2dFileSelectorEx(_("Select a file"), m_lastDirectory, wxT(""),&FilterIndex,
01416 descrBuf, dialogflags, parent, selectedPaths) )
01417 return a2dError_Canceled;
01418
01419 if ( selectedPaths->GetCount() )
01420 {
01421 if ( dialogflags & wxFD_OPEN )
01422 {
01423 for( size_t i=0; i < selectedPaths->GetCount(); i++)
01424 if( !wxFileExists(selectedPaths->Item(i)) )
01425 {
01426 wxString msgTitle;
01427 if (!wxTheApp->GetAppName().IsEmpty())
01428 msgTitle = wxTheApp->GetAppName();
01429 else
01430 msgTitle = wxString(_("File error"));
01431
01432 wxString buf;
01433 buf.Printf( _("Sorry, could not open the file: %s\n"), selectedPaths->Item(i).c_str() );
01434 wxMessageBox( buf , msgTitle, wxOK | wxICON_EXCLAMATION );
01435 return a2dError_FileCouldNotOpen;
01436 }
01437 }
01438 m_lastDirectory = wxPathOnly( selectedPaths->Item(0) );
01439
01440
01441
01442 if ( FilterIndex != -1 )
01443 {
01444 int n = 0;
01445 const_forEachIn( a2dDocumentTemplateList, &docTemplates )
01446 {
01447 a2dDocumentTemplateList::value_type temp = *iter;
01448
01449 if ( temp->CheckMask( docTemplateFlags ) )
01450 {
01451 if ( n == FilterIndex )
01452 {
01453 *chosenTemplate = temp;
01454 break;
01455 }
01456
01457 n++;
01458 }
01459 }
01460 }
01461
01462 if ( dialogflags & wxFD_OPEN && !*chosenTemplate )
01463 *chosenTemplate = FindTemplateForPath( docTemplates, path );
01464
01465 return a2dError_NoError;
01466 }
01467 return a2dError_NoSelection;
01468 }
01469
01470 a2dDocumentTemplate* a2dDocumentCommandProcessor::SelectDocumentType( bool sort, a2dTemplateFlagMask docTemplateFlags )
01471 {
01472 wxArrayString strings(sort);
01473 a2dDocumentTemplate **templates = new a2dDocumentTemplate *[m_docTemplates.size()];
01474 int n = 0;
01475
01476 const_forEachIn( a2dDocumentTemplateList, &m_docTemplates )
01477 {
01478 a2dDocumentTemplateList::value_type temp = *iter;
01479 if ( temp->CheckMask( docTemplateFlags ) )
01480 {
01481 int j;
01482 bool want = true;
01483 for (j = 0; j < n; j++)
01484 {
01485
01486 if ( temp->GetDocumentTypeName() == templates[j]->GetDocumentTypeName() )
01487 want = false;
01488 }
01489
01490 if ( want )
01491 {
01492 strings.Add(temp->GetDescription());
01493
01494 templates[n] = temp;
01495 n ++;
01496 }
01497 }
01498 }
01499
01500 if (sort)
01501 {
01502
01503
01504 int i;
01505 n = strings.Count();
01506 for (i = 0; i < n; i++)
01507 {
01508 const_forEachIn( a2dDocumentTemplateList, &m_docTemplates )
01509 {
01510 a2dDocumentTemplateList::value_type temp = *iter;
01511 if (strings[i] == temp->GetDescription())
01512 templates[i] = temp;
01513 }
01514 }
01515 }
01516
01517 a2dDocumentTemplate *theTemplate;
01518
01519 switch ( n )
01520 {
01521 case 0:
01522
01523 theTemplate = NULL;
01524 break;
01525
01526 case 1:
01527
01528 theTemplate = templates[0];
01529 break;
01530
01531 default:
01532
01533 theTemplate = (a2dDocumentTemplate *)wxGetSingleChoiceData
01534 (
01535 _("Select a document template"),
01536 _("Templates"),
01537 strings,
01538 (void **)templates,
01539 wxFindSuitableParent()
01540 );
01541 }
01542
01543 delete[] templates;
01544
01545 return theTemplate;
01546 }
01547
01548 a2dViewTemplate* a2dDocumentCommandProcessor::SelectViewType( a2dDocument* doc,
01549 const a2dViewTemplateList& list,
01550 const wxString& viewTypeName,
01551 bool sort,
01552 a2dTemplateFlagMask mask )
01553 {
01554 if ( !viewTypeName.IsEmpty() )
01555 {
01556 const_forEachIn( a2dViewTemplateList, &list )
01557 {
01558 a2dViewTemplateList::value_type temp = *iter;
01559 if ( temp->GetViewTypeName() == viewTypeName )
01560 {
01561 return temp;
01562 }
01563 }
01564 return (a2dViewTemplate *)NULL;
01565 }
01566
01567 wxArrayString strings;
01568 a2dViewTemplate **templates = new a2dViewTemplate *[list.size()];
01569
01570 int n = 0;
01571
01572
01573 const_forEachIn( a2dViewTemplateList, &list )
01574 {
01575 a2dViewTemplateList::value_type temp = *iter;
01576 if ( !temp->GetViewTypeName().empty() && temp->CheckMask( mask ) &&
01577 temp->GetDocumentTypeName() == doc->GetDocumentTypeName() )
01578 {
01579 strings.Add(temp->GetViewTypeName());
01580 templates[n] = temp;
01581 n ++;
01582 }
01583 }
01584
01585 if (sort)
01586 {
01587 strings.Sort();
01588
01589
01590
01591 int i;
01592 n = strings.Count();
01593 for (i = 0; i < n; i++)
01594 {
01595 const_forEachIn( a2dViewTemplateList, &list )
01596 {
01597 a2dViewTemplateList::value_type temp = *iter;
01598 if (strings[i] == temp->GetViewTypeName())
01599 {
01600 templates[i] = temp;
01601 }
01602 }
01603 }
01604 }
01605
01606 a2dViewTemplate *theTemplate;
01607
01608 switch ( n )
01609 {
01610 case 0:
01611
01612 theTemplate = (a2dViewTemplate *)NULL;
01613 break;
01614
01615 case 1:
01616
01617 theTemplate = templates[0];
01618 break;
01619
01620 default:
01621
01622 theTemplate = (a2dViewTemplate *)wxGetSingleChoiceData
01623 (
01624 wxString::Format(_("Select a document view for the file %s"), doc->GetFilename().GetFullName().c_str()),
01625 _("Views"),
01626 strings,
01627 (void **)templates,
01628 wxFindSuitableParent()
01629 );
01630
01631 }
01632
01633 delete[] templates;
01634 return theTemplate;
01635 }
01636
01637 void a2dDocumentCommandProcessor::AssociateDocTemplate( a2dDocumentTemplate *temp )
01638 {
01639 if ( m_docTemplates.end() == m_docTemplates.Find(temp))
01640 {
01641 m_docTemplates.push_back(temp);
01642 }
01643 }
01644
01645 void a2dDocumentCommandProcessor::DisassociateDocTemplate( a2dDocumentTemplate *temp )
01646 {
01647 m_docTemplates.ReleaseObject(temp);
01648 }
01649
01650 void a2dDocumentCommandProcessor::AssociateViewTemplate( a2dViewTemplate *temp )
01651 {
01652 if ( m_viewTemplates.end() == m_viewTemplates.Find(temp))
01653 {
01654 m_viewTemplates.push_back(temp);
01655 }
01656 }
01657
01658 void a2dDocumentCommandProcessor::DisassociateViewTemplate( a2dViewTemplate *temp )
01659 {
01660 m_viewTemplates.ReleaseObject(temp);
01661 }
01662
01663
01664 void a2dDocumentCommandProcessor::AddDocument(a2dDocument *doc)
01665 {
01666 if ( m_docs.end() == m_docs.Find(doc))
01667 {
01668 m_docs.push_back( a2dDocumentPtr( doc ) );
01669 SetCurrentDocument( doc );
01670 a2dCommandEvent event( wxEVT_ADD_DOCUMENT, doc );
01671 event.SetEventObject(this);
01672 a2dDocviewGlobals->GetEventDistributer()->ProcessEvent( event );
01673 }
01674 else
01675 SetCurrentDocument( doc );
01676 }
01677
01678 void a2dDocumentCommandProcessor::RemoveDocument(a2dDocument *doc)
01679 {
01680 if ( m_currentDocument == doc )
01681 SetCurrentDocument( (a2dDocument*) NULL );
01682
01683 if ( m_docs.ReleaseObject(doc) )
01684 {
01685 a2dCommandEvent event( wxEVT_REMOVE_DOCUMENT, doc );
01686 event.SetEventObject(this);
01687 a2dDocviewGlobals->GetEventDistributer()->ProcessEvent( event );
01688 }
01689 }
01690
01691
01692
01693 void a2dDocumentCommandProcessor::SetActiveView( a2dView* view )
01694 {
01695
01696
01697 static bool recur = false;
01698 if ( recur )
01699 return;
01700 recur = true;
01701
01702
01703 if ( m_currentView && !m_currentView->IsClosed() && m_currentView != view && m_currentView->GetActive() )
01704 {
01705 m_currentView->Activate( false );
01706 }
01707
01708 m_currentView = view;
01709
01710 if ( m_currentView )
01711 {
01712 SetCurrentDocument( m_currentView->GetDocument() );
01713 m_lastView = m_currentView;
01714 }
01715
01716 recur = false;
01717 }
01718
01719 void a2dDocumentCommandProcessor::CheckCurrentLastView( a2dView* view )
01720 {
01721 if(!view)
01722 {
01723 m_currentView = NULL;
01724 m_lastView = NULL;
01725 }
01726 else
01727 {
01728 if(m_currentView == view)
01729 m_currentView = NULL;
01730 if(m_lastView == view)
01731 m_lastView = NULL;
01732 }
01733 }
01734
01735 void a2dDocumentCommandProcessor::SetCurrentDocument( a2dDocument* document )
01736 {
01737 bool changed = m_currentDocument != document;
01738
01739
01740 m_currentDocument = document;
01741
01742
01743
01744 if ( !document || changed )
01745 {
01746 if ( changed )
01747 {
01748 a2dCommandEvent event( wxEVT_CHANGED_DOCUMENT, m_currentDocument );
01749 event.SetEventObject(this);
01750 a2dDocviewGlobals->GetEventDistributer()->ProcessEvent( event );
01751 }
01752 }
01753 }
01754
01755 void a2dDocumentCommandProcessor::OnUndo(wxCommandEvent& WXUNUSED(event))
01756 {
01757 Undo();
01758 }
01759
01760 void a2dDocumentCommandProcessor::OnRedo(wxCommandEvent& WXUNUSED(event))
01761 {
01762 Redo();
01763 }
01764
01765
01766
01767 void a2dDocumentCommandProcessor::OnUpdateFileOpen(wxUpdateUIEvent& event)
01768 {
01769 event.Enable( true );
01770 }
01771
01772 void a2dDocumentCommandProcessor::OnUpdateFileClose(wxUpdateUIEvent& event)
01773 {
01774 a2dDocument *doc = GetCurrentDocument();
01775 event.Enable( (doc != (a2dDocument*) NULL) );
01776 }
01777
01778 void a2dDocumentCommandProcessor::OnUpdateFileRevert(wxUpdateUIEvent& event)
01779 {
01780 a2dDocument *doc = GetCurrentDocument();
01781 event.Enable( (doc != (a2dDocument*) NULL) );
01782 }
01783
01784 void a2dDocumentCommandProcessor::OnUpdateFileNew(wxUpdateUIEvent& event)
01785 {
01786 event.Enable( true );
01787 }
01788
01789 void a2dDocumentCommandProcessor::OnUpdateFileSave(wxUpdateUIEvent& event)
01790 {
01791 a2dDocument *doc = GetCurrentDocument();
01792 event.Enable( doc && doc->IsModified() );
01793 }
01794
01795 void a2dDocumentCommandProcessor::OnUpdateFileSaveAs(wxUpdateUIEvent& event)
01796 {
01797 a2dDocument *doc = GetCurrentDocument();
01798 event.Enable( (doc != (a2dDocument*) NULL) );
01799 }
01800
01801 void a2dDocumentCommandProcessor::OnUpdateUndo(wxUpdateUIEvent& event)
01802 {
01803 a2dDocument *doc = GetCurrentDocument();
01804 event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanUndo()) );
01805 }
01806
01807 void a2dDocumentCommandProcessor::OnUpdateRedo(wxUpdateUIEvent& event)
01808 {
01809 a2dDocument *doc = GetCurrentDocument();
01810 event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanRedo()) );
01811 }
01812
01813 void a2dDocumentCommandProcessor::OnUpdatePrint(wxUpdateUIEvent& event)
01814 {
01815 a2dDocument *doc = GetCurrentDocument();
01816 event.Enable( (doc != (a2dDocument*) NULL) );
01817 }
01818
01819 void a2dDocumentCommandProcessor::OnUpdatePreview(wxUpdateUIEvent& event)
01820 {
01821 a2dDocument *doc = GetCurrentDocument();
01822 event.Enable( (doc != (a2dDocument*) NULL) );
01823 }
01824
01825
01826
01827 void a2dDocumentCommandProcessor::OnActivateView( a2dViewEvent& viewevent )
01828 {
01829 a2dView* view = (a2dView*) viewevent.GetEventObject();
01830 SetActiveView( viewevent.GetActive() ? view : NULL );
01831 }
01832
01833
01834 void a2dDocumentCommandProcessor::OnAddView( a2dDocumentEvent& docevent )
01835 {
01836 a2dView* view = (a2dView*) docevent.GetEventObject();
01837 if(docevent.GetEnable())
01838 SetActiveView( view );
01839 }
01840
01841 void a2dDocumentCommandProcessor::OnRemoveView( a2dDocumentEvent& viewevent )
01842 {
01843 a2dView* view = (a2dView*) viewevent.GetEventObject();
01844
01845
01846 if ( view == m_lastView )
01847 m_lastView = NULL;
01848 if ( view == m_currentView )
01849 m_currentView = NULL;
01850 }
01851
01852 bool a2dDocumentCommandProcessor::ProcessEvent(wxEvent& event)
01853 {
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883 return a2dCommandProcessor::ProcessEvent(event);
01884 }
01885
01886
01887
01888
01889
01890
01891 a2dDocviewGlobal* a2dDocviewGlobals = NULL;
01892
01893 a2dDocviewGlobal::a2dDocviewGlobal()
01894 {
01895 m_docviewCommandProcessor = new a2dDocumentCommandProcessor();
01896 m_docviewEventDistributer->Register( m_docviewCommandProcessor );
01897
01898 m_showlog = true;
01899 }
01900
01901 void a2dDocviewGlobal::SetDocviewCommandProcessor( a2dDocumentCommandProcessor* docviewCommandProcessor )
01902 {
01903 if ( m_docviewCommandProcessor )
01904 m_docviewEventDistributer->Unregister( m_docviewCommandProcessor );
01905
01906 docviewCommandProcessor->CheckCurrentLastView( NULL );
01907 m_docviewCommandProcessor = docviewCommandProcessor;
01908 m_docviewEventDistributer->Register( m_docviewCommandProcessor );
01909 }
01910
01911 a2dDocviewGlobal::~a2dDocviewGlobal()
01912 {
01913 }
01914
01915 void a2dDocviewGlobal::RecordF( wxObject* sender, const wxChar* Format, ... )
01916 {
01917 va_list ap;
01918
01919 wxString recordstring;
01920 va_start( ap, Format );
01921
01922
01923
01924 recordstring.PrintfV( Format, ap );
01925 va_end( ap );
01926
01927 a2dCommandEvent event( recordstring );
01928 event.SetEventObject( sender );
01929
01930 GetEventDistributer()->ProcessEvent( event );
01931 }
01932
01933 void a2dDocviewGlobal::RecordF( const wxChar* Format, ... )
01934 {
01935 va_list ap;
01936
01937 wxString recordstring;
01938 va_start( ap, Format );
01939
01940
01941
01942 recordstring.PrintfV( Format, ap );
01943 va_end( ap );
01944
01945 a2dCommandEvent event( recordstring );
01946 event.SetEventObject( this );
01947
01948 GetEventDistributer()->ProcessEvent( event );
01949 }
01950
01951
01952
01953
01954
01955 IMPLEMENT_DYNAMIC_CLASS(a2dFileHistory, wxObject)
01956
01957
01958
01959
01960
01961 static const wxChar *s_MRUEntryFormat = wxT("&%lu %s");
01962
01963
01964 static inline wxChar* MYcopystring(const wxString& s)
01965 {
01966 wxChar* copy = new wxChar[s.length() + 1];
01967 return wxStrcpy(copy, s.c_str());
01968 }
01969
01970 static inline wxChar* MYcopystring(const wxChar* s)
01971 {
01972 wxChar* copy = new wxChar[wxStrlen(s) + 1];
01973 return wxStrcpy(copy, s);
01974 }
01975
01976 a2dFileHistory::a2dFileHistory(size_t maxFiles, wxWindowID idBase)
01977 {
01978 m_fileMaxFiles = maxFiles;
01979 m_idBase = idBase;
01980 }
01981
01982 a2dFileHistory::~a2dFileHistory()
01983 {
01984 }
01985
01986
01987 void a2dFileHistory::AddFileToHistory( const wxFileName& file, a2dDocumentTemplate* docTemplate, a2dViewTemplate* viewTemplate )
01988 {
01989 a2dFileHistoryItemList::iterator iter = m_fileHistoryList.begin();
01990 size_t i = 0;
01991 for( ; iter != m_fileHistoryList.end(); iter++ )
01992 {
01993 a2dFileHistoryItemPtr fileitem = *iter;
01994 if ( fileitem->m_filename == file )
01995 {
01996
01997 RemoveFileFromHistory (i);
01998
01999 AddFileToHistory( file, docTemplate, viewTemplate );
02000 return;
02001 }
02002 i++;
02003 }
02004
02005
02006 if ( m_fileMaxFiles == m_fileHistoryList.size() )
02007 {
02008 RemoveFileFromHistory ( m_fileHistoryList.size() - 1);
02009 AddFileToHistory( file, docTemplate, viewTemplate );
02010 return;
02011 }
02012
02013
02014
02015 if ( m_fileHistoryList.size() < m_fileMaxFiles)
02016 {
02017 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
02018 while (node)
02019 {
02020 wxMenu* menu = (wxMenu*) node->GetData();
02021 if ( m_fileHistoryList.empty() && menu->GetMenuItemCount() )
02022 {
02023 menu->AppendSeparator();
02024 }
02025
02026 menu->Append( m_idBase + m_fileHistoryList.size(), _("[EMPTY]"));
02027 node = node->GetNext();
02028 }
02029 }
02030 m_fileHistoryList.push_front( new a2dFileHistoryItem( file, docTemplate, viewTemplate ) );
02031
02032
02033 iter = m_fileHistoryList.begin();
02034 wxString pathCurrent = (*iter)->m_filename.GetPath();
02035
02036
02037 i = 0;
02038 for( ; iter != m_fileHistoryList.end(); iter++ )
02039 {
02040 a2dFileHistoryItemPtr fileitem = *iter;
02041
02042
02043
02044 wxString pathInMenu, path;
02045 path = fileitem->m_filename.GetPath();
02046 if ( path == pathCurrent )
02047 pathInMenu = fileitem->m_filename.GetFullName();
02048 else
02049 {
02050
02051 pathInMenu = fileitem->m_filename.GetFullPath();
02052 }
02053
02054
02055 pathInMenu.Replace(_T("&"), _T("&&"));
02056 wxString buf;
02057 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str() );
02058 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
02059 while (node)
02060 {
02061 wxMenu* menu = (wxMenu*) node->GetData();
02062 menu->SetLabel(m_idBase + i, buf);
02063 node = node->GetNext();
02064 }
02065 i++;
02066 }
02067 }
02068
02069 void a2dFileHistory::RemoveFileFromHistory(size_t i)
02070 {
02071 wxCHECK_RET( i < m_fileHistoryList.size(),
02072 wxT("invalid index in a2dFileHistory::RemoveFileFromHistory") );
02073
02074
02075 a2dFileHistoryItemList::iterator iter = m_fileHistoryList.begin();
02076 size_t j = 0;
02077 for( ; iter != m_fileHistoryList.end(); iter++ )
02078 {
02079 a2dFileHistoryItemPtr fileitem = *iter;
02080 if ( j == i )
02081 {
02082 m_fileHistoryList.erase( iter );
02083 break;
02084 }
02085 j++;
02086 }
02087
02088 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
02089 while ( node )
02090 {
02091 wxMenu* menu = (wxMenu*) node->GetData();
02092
02093
02094 wxString buf;
02095 a2dFileHistoryItemList::iterator iter = m_fileHistoryList.begin();
02096 size_t j = 0;
02097 for( ; iter != m_fileHistoryList.end(); iter++ )
02098 {
02099 a2dFileHistoryItemPtr fileitem = *iter;
02100 buf.Printf(s_MRUEntryFormat, j + 1, fileitem->m_filename.GetFullPath().c_str() );
02101 menu->SetLabel(m_idBase + j, buf);
02102 j++;
02103 }
02104
02105 node = node->GetNext();
02106
02107
02108 wxWindowID lastItemId = m_idBase + wx_truncate_cast(wxWindowID, m_fileHistoryList.size());
02109 if (menu->FindItem(lastItemId))
02110 {
02111 menu->Delete(lastItemId);
02112 }
02113
02114
02115 if ( m_fileHistoryList.empty() )
02116 {
02117 wxMenuItemList::compatibility_iterator nodeLast = menu->GetMenuItems().GetLast();
02118 if ( nodeLast )
02119 {
02120 wxMenuItem *menuItem = nodeLast->GetData();
02121 if ( menuItem->IsSeparator() )
02122 {
02123 menu->Delete(menuItem);
02124 }
02125
02126 }
02127
02128 }
02129 }
02130 }
02131
02132 wxString a2dFileHistory::GetHistoryFile(size_t i) const
02133 {
02134 wxString s;
02135 if ( i < m_fileHistoryList.size() )
02136 {
02137 a2dFileHistoryItemList::const_iterator iter = m_fileHistoryList.item( i );
02138 a2dFileHistoryItemPtr fileitem = *iter;
02139 s = fileitem->m_filename.GetFullPath();
02140 }
02141 else
02142 wxFAIL_MSG( wxT("bad index in a2dFileHistory::GetHistoryFile") );
02143 return s;
02144 }
02145
02146 a2dFileHistoryItem* a2dFileHistory::GetHistoryFileItem(size_t i) const
02147 {
02148 wxString s;
02149 if ( i < m_fileHistoryList.size() )
02150 {
02151 a2dFileHistoryItemList::const_iterator iter = m_fileHistoryList.item( i );
02152 return *iter;
02153 }
02154 else
02155 wxFAIL_MSG( wxT("bad index in a2dFileHistory::GetHistoryFile") );
02156 return NULL;
02157 }
02158
02159 void a2dFileHistory::UseMenu(wxMenu *menu)
02160 {
02161 if (!m_fileMenus.Member(menu))
02162 m_fileMenus.Append(menu);
02163 }
02164
02165 void a2dFileHistory::RemoveMenu(wxMenu *menu)
02166 {
02167 m_fileMenus.DeleteObject(menu);
02168 }
02169
02170 #if wxUSE_CONFIG
02171 void a2dFileHistory::Load(wxConfigBase& config)
02172 {
02173 wxString buf;
02174 buf.Printf(wxT("file%d"), 1 );
02175 wxString historyFile;
02176 while (( m_fileHistoryList.size() < m_fileMaxFiles) && config.Read(buf, &historyFile) && (!historyFile.empty()))
02177 {
02178 m_fileHistoryList.push_back( new a2dFileHistoryItem( wxFileName( historyFile ), NULL, NULL ) );
02179 buf.Printf(wxT("file%d"), (int)m_fileHistoryList.size()+1);
02180 historyFile = wxEmptyString;
02181 }
02182 AddFilesToMenu();
02183 }
02184
02185 void a2dFileHistory::Save(wxConfigBase& config)
02186 {
02187 a2dFileHistoryItemList::iterator iter = m_fileHistoryList.begin();
02188 size_t j = 0;
02189 for( ; iter != m_fileHistoryList.end(); iter++ )
02190 {
02191 a2dFileHistoryItemPtr fileitem = *iter;
02192 wxString buf;
02193 buf.Printf(wxT("file%d"), (int)j+1);
02194 if ( j < m_fileHistoryList.size() )
02195 config.Write( buf, fileitem->m_filename.GetFullPath() );
02196 else
02197 config.Write(buf, wxEmptyString);
02198 j++;
02199 }
02200 }
02201 #endif // wxUSE_CONFIG
02202
02203 void a2dFileHistory::AddFilesToMenu()
02204 {
02205 if ( !m_fileHistoryList.empty() )
02206 {
02207 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
02208 while (node)
02209 {
02210 wxMenu* menu = (wxMenu*) node->GetData();
02211 if (menu->GetMenuItemCount())
02212 {
02213 menu->AppendSeparator();
02214 }
02215
02216 a2dFileHistoryItemList::iterator iter = m_fileHistoryList.begin();
02217 size_t i = 0;
02218 for( ; iter != m_fileHistoryList.end(); iter++ )
02219 {
02220 a2dFileHistoryItemPtr fileitem = *iter;
02221 wxString buf;
02222 buf.Printf(s_MRUEntryFormat, i+1, fileitem->m_filename.GetFullPath().c_str() );
02223 menu->Append(m_idBase+i, buf);
02224 i++;
02225 }
02226 node = node->GetNext();
02227 }
02228 }
02229 }
02230
02231 void a2dFileHistory::AddFilesToMenu(wxMenu* menu)
02232 {
02233 if ( !m_fileHistoryList.empty() )
02234 {
02235 if (menu->GetMenuItemCount())
02236 {
02237 menu->AppendSeparator();
02238 }
02239
02240 a2dFileHistoryItemList::iterator iter = m_fileHistoryList.begin();
02241 size_t i = 0;
02242 for( ; iter != m_fileHistoryList.end(); iter++ )
02243 {
02244 a2dFileHistoryItemPtr fileitem = *iter;
02245 wxString buf;
02246 buf.Printf(s_MRUEntryFormat, i+1, fileitem->m_filename.GetFullPath().c_str() );
02247 menu->Append(m_idBase+i, buf);
02248 i++;
02249 }
02250 }
02251 }
02252
02253
02254
02255
02256
02257 IMPLEMENT_DYNAMIC_CLASS(a2dDocviewModule, wxModule)
02258
02259 bool a2dDocviewModule::OnInit()
02260 {
02261 #if wxUSE_PRINTING_ARCHITECTURE
02262 if(wxThePrintPaperDatabase == NULL)
02263 {
02264 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
02265 wxThePrintPaperDatabase->CreateDatabase();
02266 m_wxThePrintPaperDatabase = wxThePrintPaperDatabase;
02267 }
02268 #endif
02269 a2dDocviewGlobals = new a2dDocviewGlobal();
02270 wxNullRefObjectList = new a2dObjectList();
02271
02272 return true;
02273 }
02274
02275 void a2dDocviewModule::OnExit()
02276 {
02277 delete wxNullRefObjectList;
02278 delete a2dDocviewGlobals;
02279 a2dDocviewGlobals = NULL;
02280 #if wxUSE_PRINTING_ARCHITECTURE
02281 if(m_wxThePrintPaperDatabase && m_wxThePrintPaperDatabase != wxThePrintPaperDatabase)
02282 delete m_wxThePrintPaperDatabase;
02283 m_wxThePrintPaperDatabase = NULL;
02284 #endif
02285 }
02286
02287
02288