documentmanager.h
来自「这是VCF框架的代码」· C头文件 代码 · 共 2,046 行 · 第 1/5 页
H
2,046 行
action = new Action(); action->setName("EditPreferences"); action->Performed += new GenericEventHandler< AppClass >( app_, &DocumentManagerImpl<AppClass,DocInterfacePolicy>::onPreferences, "onPreferences" ); action->Update += new EventHandlerInstance<AppClass,ActionEvent>( app_, &DocumentManagerImpl<AppClass,DocInterfacePolicy>::onUpdatePreferences, "onUpdatePreferences" ); addAction( DocumentManager::atEditPreferences, action );}template < typename AppClass, typename DocInterfacePolicy >bool DocumentManagerImpl<AppClass,DocInterfacePolicy>::saveFile( Document* doc ){ bool result = false; if ( NULL == doc ) { return false; } if ( doc->isModified() ) { FilePath fp = doc->getFileName(); FilePath tmpName; if ( File::exists( fp ) ) { //save back up first File backup( fp ); String tmp = fp.getPathName(true); tmp += "~" + fp.getBaseName() + ".tmp"; tmpName = tmp; backup.copyTo( tmpName ); } result = saveFileAs( doc, fp ); if ( result ) { if ( (!doc->getKeepsBackUpFile()) && File::exists( tmpName ) ) { File backup( tmpName ); backup.remove(); } } } return result;}template < typename AppClass, typename DocInterfacePolicy >bool DocumentManagerImpl<AppClass,DocInterfacePolicy>::saveFileAs( Document* doc, const String& newFileName ){ bool result = false; if ( NULL == doc ) { return false; } /** * allow the base behaviour to be bypassed if necessary. *@see DocManagerEvent */ DocManagerEvent event( doc, DocumentManager::dmSaveDocument ); SaveFile.fireEvent( &event ); if ( !event.allowFileOperation() ) { return event.getFileOperationStatus(); } String fileType; String fileTypes; DocumentInfo* info = getDocumentInfo( doc ); if ( NULL != info ) { fileType = info->mimetype; fileTypes = info->fileTypes; } // prompts the user with a dialog if the destination filename is empty // or if its directory does not exists. bool prompt = false; FilePath fp = newFileName; if ( newFileName.empty() ) { fp = doc->getFileName(); prompt = true; } String currentDir = fp.getPathName(true); if ( !prompt && !File::exists( currentDir ) ) { prompt = true; } if ( prompt ) { String basename = fp.getBaseName(true); if ( currentDir.empty() ) { currentDir = System::getCurrentWorkingDirectory(); } else { if ( !File::exists( currentDir ) ) { // we look for the first existing parent directory String appDir = currentDir; std::vector<String> pathComponents = fp.getPathComponents(); std::vector<String>::reverse_iterator it = pathComponents.rbegin(); bool found = false; while ( it != pathComponents.rend() ) { String s = (*it); size_t length = (*it).length();// + FilePath::getDirectorySeparator().length(); appDir.erase( appDir.length()-length, length ); if ( File::exists( appDir ) ) { found = true; break; } it ++; } if ( found ) { currentDir = appDir; } else { currentDir = System::getCurrentWorkingDirectory(); } } } CommonFileSaveDialog saveDialog( doc->getWindow(), currentDir ); saveDialog.setFileName( currentDir + basename ); prepareSaveDialog( &saveDialog, doc ); saveDialog.addFilter( ".* files", "*.*" ); if ( saveDialog.execute() ) { saveDialogFinished( &saveDialog ); fp = saveDialog.getFileName(); // adds the filter's extension if specified but missing from the filename String selectedFilter = saveDialog.getSelectedFilter(); selectedFilter.erase( 0, 1 ); if ( ( !selectedFilter.empty() ) && ( selectedFilter != L".*" ) && ( selectedFilter != fp.getExtension() ) ) { fp = fp + selectedFilter; } } else { return false; } } if ( fileType.empty() ) { fileType = fp.getExtension(); } //store off the current name String oldName = doc->getName(); //set the name to the new file doc->setFileName( fp ); try { result = doc->saveAsType( fp, fileType ); //reset it back to the old name, we'll change it later doc->setFileName( oldName ); } catch ( BasicException& e) { Dialog::showMessage( "Error saving '" + doc->getName() + "'\nError: " + e.getMessage() ); result = false; } if ( result ) { doc->setModified(false); // it already is, we just make sure. doc->setFileName( fp ); // notifies the UI that the document has changed name ModelEvent e( doc, Document::deSaved ); doc->ModelChanged.fireEvent( &e ); } return result;}template < typename AppClass, typename DocInterfacePolicy >void DocumentManagerImpl<AppClass,DocInterfacePolicy>::openFile(){ Document* doc = DocInterfacePolicy::getCurrentDocument(); /** * allow the base behaviour to be bypassed if necessary. *@see DocManagerEvent */ DocManagerEvent event( NULL, DocumentManager::dmOpenDocument ); OpenFile.fireEvent( &event ); if ( !event.allowFileOperation() ) { return; } String currentDir = System::getCurrentWorkingDirectory(); CommonFileOpenDialog openDialog( Frame::getActiveFrame(), currentDir ); prepareOpenDialog( &openDialog ); if ( openDialog.execute() ) { Document* doc = openFromFileName( openDialog.getFileName() ); openDialogFinished( &openDialog ); }}template < typename AppClass, typename DocInterfacePolicy >void DocumentManagerImpl<AppClass,DocInterfacePolicy>::closeCurrentDocument(){ //JC - I got rid of this because I beleive it is no longer //neccessary /*closingDocument_ = true; Document* currentDoc = DocInterfacePolicy::getCurrentDocument(); Component* owner = currentDoc->getOwner(); if ( NULL != owner ) { owner->removeComponent( currentDoc ); } documentClosedOK_ = false; // closes the current document window ( and so the window // associated to the just deleted document ). DocInterfacePolicy::closeDocument(); // remove the current document form the list of opened documents // and frees it. removeDocument( currentDoc ); closingDocument_ = false; removeUndoRedoStackForDocument( currentDoc ); currentDoc->free(); documentClosedOK_ = false; */ DocInterfacePolicy::closeDocument();}template < typename AppClass, typename DocInterfacePolicy >void DocumentManagerImpl<AppClass,DocInterfacePolicy>::reloadDocument( Document* document, const bool& keepPosition ){ // returns if the user doesn't want to save the changes. if ( !saveDocument( document ) ) { return; }; String fileName = document->getFileName(); //String mimetype = app->getMimeTypeFromFileExtension( fileName ); //String mimetype = getDocumentMimeType( document ); String mimeType; String fileTypes; DocumentInfo* info = getDocumentInfo( document ); if ( NULL != info ) { mimeType = info->mimetype; fileTypes = info->fileTypes; } //VCF_ASSERT( !mimetype.empty() ); document->openFromType( fileName, mimeType ); // reset the undo/redo stack getUndoRedoStack( document).clearCommands();}template < typename AppClass, typename DocInterfacePolicy >Window* DocumentManagerImpl<AppClass,DocInterfacePolicy>::getWindowForNewDocument( Document* document, const DocumentInfo& info ){ Window* result; if ( DocInterfacePolicy::usesMainWindow() ) { // in a SDI policy the new window for the document is the main window if ( NULL == app_->getMainWindow() ) { if ( info.window.empty() ) { result = new Window(); } else { Object* windowObj = ClassRegistry::createNewInstance( info.window ); result = dynamic_cast<Window*>(windowObj); } app_->setMainWindow( result ); } else { result = app_->getMainWindow(); } } else { // in a MDI policy the new window for the document is a new window if ( info.window.empty() ) { result = new Window(); } else { Object* windowObj = ClassRegistry::createNewInstance( info.window ); result = dynamic_cast<Window*>(windowObj); } } return result;}template < typename AppClass, typename DocInterfacePolicy >void DocumentManagerImpl<AppClass,DocInterfacePolicy>::initializeWindowMenus( Window* window, Document* document, const DocumentInfo& info ){ /* if the window has no menu yet, we need to have one */ if ( NULL == window->getMenuBar() ) { MenuBar* menu = new MenuBar(); window->setMenuBar(menu); window->addComponent( menu ); } DocInterfacePolicy::mergeWindowMenus( MenuManager::getMainMenu(), window->getMenuBar() );}template < typename AppClass, typename DocInterfacePolicy >void DocumentManagerImpl<AppClass,DocInterfacePolicy>::attachUI( const DocumentInfo& info, Document* document ){ Window* window = NULL; EventHandler* docEv = app_->getEventHandler("onDocModified"); window = getWindowForNewDocument( document, info ); initializeWindowMenus( window, document, info ); document->setWindow( window ); window->addComponent( document ); if ( NULL == docEv ) { docEv = new ModelEventHandler< AppClass >( app_, &DocumentManagerImpl<AppClass,DocInterfacePolicy>::onDocModified, "onDocModified" ); } document->addModelHandler( docEv ); /** create a view from the DocInfo if necessary */ View* view = NULL; if ( !info.view.empty() ) { if ( info.window != info.view ) { Object* viewObject = NULL; viewObject = ClassRegistry::createNewInstance( info.view ); view = dynamic_cast<View*>(viewObject); } else { view = window; } } else { view = window; } if ( NULL != view ) { document->addView( view ); if ( view != window ) { // sets a child view for the document inside the window setNewView( info, view, window, document ); } } //need to provide a common place to //init everything once all the "connections" are in place DocManagerEvent event( document, DocumentManager::dmDocumentInitialized ); DocumentInitialized.fireEvent( &event ); // let the policy to update its data to the new document DocInterfacePolicy::afterNewDocument( document ); // makes sure the Frame has an handler to // to catch if the document's window is to be closing EventHandler* newEv = app_->getEventHandler("onDocWindowClosing"); if ( NULL == newEv ) { newEv = new FrameEventHandler< AppClass >( app_, &DocumentManagerImpl<AppClass,DocInterfacePolicy>::onDocWindowClosing, "onDocWindowClosing" ); } window->FrameClosing += newEv; // makes sure the window has an handler to catch if the document's window has been activated newEv = app_->getEventHandler("onDocWindowActive"); if ( NULL == newEv ) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?