void Notepad_plus::fileNew()
{
    BufferID newBufID = MainFileManager->newEmptyDocument();
	
    loadBufferIntoView(newBufID, currentView(), true);	//true, because we want multiple new files if possible
    activateBuffer(newBufID, currentView());
}
BufferID Notepad_plus::openNormalFile(NotepadFile notepadFile, bool isReadOnly)
{
	_pluginsManager.notify(BeforeFileLoadNoticiation(getMainWindowHandle()));

	BufferID buffer = MainFileManager->loadFile(notepadFile.getLongFileName(), NULL, notepadFile.getEncoding());

	if (buffer != BUFFER_INVALID) {
		Buffer * buf = MainFileManager->getBufferByID(buffer);

		// if file is read only, we set the view read only
		if (isReadOnly)
			buf->setUserReadOnly(true);

		loadBufferIntoView(buffer, currentView());
		updateTray();
		_linkTriggered = true;
		_isDocModifing = false;

		_pluginsManager.notify(FileJustBeenOpenedNotification(getMainWindowHandle(), buffer));
		if (_pFileSwitcherPanel)
			_pFileSwitcherPanel->newItem((int)buf, currentView());
	}
	else {
		generic_string msg = TEXT("Can not open file \"");
		msg += notepadFile.getLongFileName();
		msg += TEXT("\".");
		::MessageBox(getMainWindowHandle(), msg.c_str(), TEXT("ERROR"), MB_OK);

		_pluginsManager.notify(LoadingFileFailedNotification(getMainWindowHandle()));
	}
	return buffer;
}
Exemplo n.º 3
0
// return true if all the session files are loaded
// return false if one or more sessions files fail to load (and session is modify to remove invalid files)
bool Notepad_plus::loadSession(Session* session)
{
	// JOCE: This function is so retarded my brain hurts. This is basically twice the same
	// chunk of code, with the only distinction being which edit view the methods are called for.
	// Need to be factored. Badly.

	bool allSessionFilesLoaded = true;
	BufferID lastOpened = BUFFER_INVALID;
	size_t i = 0;
	showView(MAIN_VIEW);
	switchEditViewTo(MAIN_VIEW);	//open files in main
	for ( ; i < session->nbMainFiles() ; )
	{
		const TCHAR *pFn = session->_mainViewFiles[i]._fileName.c_str();
		if (isFileSession(pFn)) {
			std::vector<sessionFileInfo>::iterator posIt = session->_mainViewFiles.begin() + i;
			session->_mainViewFiles.erase(posIt);
			continue;	//skip session files, not supporting recursive sessions
		}
		if (PathFileExists(pFn)) {
			lastOpened = doOpen(pFn, false, session->_mainViewFiles[i]._encoding);
		} else {
			lastOpened = BUFFER_INVALID;
		}
		if (lastOpened != BUFFER_INVALID)
		{
			showView(MAIN_VIEW);
			const TCHAR *pLn = session->_mainViewFiles[i]._langName.c_str();
			int id = getLangFromMenuName(pLn);
			LangType typeToSet = L_TEXT;
			if (id != 0 && lstrcmp(pLn, TEXT("User Defined")) != 0)
				typeToSet = menuID2LangType(id);
			if (typeToSet == L_EXTERNAL )
				typeToSet = (LangType)(id - IDM_LANG_EXTERNAL + L_EXTERNAL);

			Buffer * buf = MainFileManager->getBufferByID(lastOpened);
			buf->setPosition(session->_mainViewFiles[i], _mainEditView);
			buf->setLangType(typeToSet, pLn);
			if (session->_mainViewFiles[i]._encoding != -1)
				buf->setEncoding(session->_mainViewFiles[i]._encoding);

			//Force in the document so we can add the markers
			//Don't use default methods because of performance
			Document prevDoc = _mainEditView->execute(SCI_GETDOCPOINTER);
			_mainEditView->execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
			for (size_t j = 0 ; j < session->_mainViewFiles[i].marks.size() ; j++)
			{
				_mainEditView->execute(SCI_MARKERADD, session->_mainViewFiles[i].marks[j], MARK_BOOKMARK);
			}
			_mainEditView->execute(SCI_SETDOCPOINTER, 0, prevDoc);
			i++;
		}
		else
		{
			std::vector<sessionFileInfo>::iterator posIt = session->_mainViewFiles.begin() + i;
			session->_mainViewFiles.erase(posIt);
			allSessionFilesLoaded = false;
		}
	}

	size_t k = 0;
	showView(SUB_VIEW);
	switchEditViewTo(SUB_VIEW);	//open files in sub
	for ( ; k < session->nbSubFiles() ; )
	{
		const TCHAR *pFn = session->_subViewFiles[k]._fileName.c_str();
		if (isFileSession(pFn)) {
			std::vector<sessionFileInfo>::iterator posIt = session->_subViewFiles.begin() + k;
			session->_subViewFiles.erase(posIt);
			continue;	//skip session files, not supporting recursive sessions
		}
		if (PathFileExists(pFn)) {
			lastOpened = doOpen(pFn, false, session->_subViewFiles[k]._encoding);
			//check if already open in main. If so, clone
			if (_mainDocTab->getIndexByBuffer(lastOpened) != -1) {
				loadBufferIntoView(lastOpened, SUB_VIEW);
			}
		} else {
			lastOpened = BUFFER_INVALID;
		}
		if (lastOpened != BUFFER_INVALID)
		{
			showView(SUB_VIEW);
			if (canHideView(MAIN_VIEW))
				hideView(MAIN_VIEW);
			const TCHAR *pLn = session->_subViewFiles[k]._langName.c_str();
			int id = getLangFromMenuName(pLn);
			LangType typeToSet = L_TEXT;
			if (id != 0)
				typeToSet = menuID2LangType(id);
			if (typeToSet == L_EXTERNAL )
				typeToSet = (LangType)(id - IDM_LANG_EXTERNAL + L_EXTERNAL);

			Buffer * buf = MainFileManager->getBufferByID(lastOpened);
			buf->setPosition(session->_subViewFiles[k], _subEditView);
			if (typeToSet == L_USER) {
				if (!lstrcmp(pLn, TEXT("User Defined"))) {
					pLn = TEXT("");	//default user defined
				}
			}
			buf->setLangType(typeToSet, pLn);
			buf->setEncoding(session->_subViewFiles[k]._encoding);

			//Force in the document so we can add the markers
			//Don't use default methods because of performance
			Document prevDoc = _subEditView->execute(SCI_GETDOCPOINTER);
			_subEditView->execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
			for (size_t j = 0 ; j < session->_subViewFiles[k].marks.size() ; j++)
			{
				_subEditView->execute(SCI_MARKERADD, session->_subViewFiles[k].marks[j], MARK_BOOKMARK);
			}
			_subEditView->execute(SCI_SETDOCPOINTER, 0, prevDoc);

			k++;
		}
		else
		{
			std::vector<sessionFileInfo>::iterator posIt = session->_subViewFiles.begin() + k;
			session->_subViewFiles.erase(posIt);
			allSessionFilesLoaded = false;
		}
	}

	_mainEditView->restoreCurrentPos();
	_subEditView->restoreCurrentPos();

	if (session->_activeMainIndex < (size_t)_mainDocTab->nbItem())//session->nbMainFiles())
		activateBuffer(_mainDocTab->getBufferByIndex(session->_activeMainIndex), MAIN_VIEW);

	if (session->_activeSubIndex < (size_t)_subDocTab->nbItem())//session->nbSubFiles())
		activateBuffer(_subDocTab->getBufferByIndex(session->_activeSubIndex), SUB_VIEW);

	if ((session->nbSubFiles() > 0) && (session->_activeView == MAIN_VIEW || session->_activeView == SUB_VIEW))
		switchEditViewTo(session->_activeView);
	else
		switchEditViewTo(MAIN_VIEW);

	if (canHideView(otherView()))
		hideView(otherView());
	else if (canHideView(currentView()))
		hideView(currentView());
	return allSessionFilesLoaded;
}
Exemplo n.º 4
0
BufferID Notepad_plus::doOpen(const TCHAR *fileName, bool isReadOnly, int encoding)
{
	TCHAR longFileName[MAX_PATH];

	::GetFullPathName(fileName, MAX_PATH, longFileName, NULL);
	::GetLongPathName(longFileName, longFileName, MAX_PATH);

	_lastRecentFileList->remove(longFileName);

	const TCHAR * fileName2Find;
	generic_string gs_fileName = fileName;
	size_t res = gs_fileName.find_first_of(UNTITLED_STR);

	if (res != generic_string::npos && res == 0)
	{
		fileName2Find = fileName;
	}
	else
	{
		fileName2Find = longFileName;
	}

	BufferID test = MainFileManager->getBufferFromName(fileName2Find);
	if (test != BUFFER_INVALID)
	{
		//switchToFile(test);
		//Dont switch, not responsibility of doOpen, but of caller
		if (_pTrayIco)
		{
			if (_pTrayIco->isInTray())
			{
				::ShowWindow(_pPublicInterface->getHSelf(), SW_SHOW);
				if (!_pPublicInterface->isPrelaunch())
					_pTrayIco->doTrayIcon(TRAYICON_REMOVE);
				::SendMessage(_pPublicInterface->getHSelf(), WM_SIZE, 0, 0);
			}
		}
		return test;
	}

	if (isFileSession(longFileName) && PathFileExists(longFileName))
	{
		fileLoadSession(longFileName);
		return BUFFER_INVALID;
	}



	if (!PathFileExists(longFileName))
	{
		TCHAR str2display[MAX_PATH*2];
		generic_string longFileDir(longFileName);
		PathRemoveFileSpec(longFileDir);

		if (PathFileExists(longFileDir.c_str()))
		{
			wsprintf(str2display, TEXT("%s doesn't exist. Create it?"), longFileName);

			if (::MessageBox(_pPublicInterface->getHSelf(), str2display, TEXT("Create new file"), MB_YESNO) == IDYES)
			{
				bool res = MainFileManager->createEmptyFile(longFileName);
				if (!res)
				{
					wsprintf(str2display, TEXT("Cannot create the file \"%s\""), longFileName);
					::MessageBox(_pPublicInterface->getHSelf(), str2display, TEXT("Create new file"), MB_OK);
					return BUFFER_INVALID;
				}
			}
			else
			{
				return BUFFER_INVALID;
			}
		}
		else
		{
			return BUFFER_INVALID;
		}
	}

	// Notify plugins that current file is about to load
	// Plugins can should use this notification to filter SCN_MODIFIED
	SCNotification scnN;
	scnN.nmhdr.code = NPPN_FILEBEFORELOAD;
	scnN.nmhdr.hwndFrom = _pPublicInterface->getHSelf();
	scnN.nmhdr.idFrom = NULL;
	_pluginsManager->notify(&scnN);

	if (encoding == -1)
	{
		encoding = getHtmlXmlEncoding(longFileName);
	}

	BufferID buffer = MainFileManager->loadFile(longFileName, NULL, encoding);
	if (buffer != BUFFER_INVALID)
	{
		_isFileOpening = true;

		Buffer * buf = MainFileManager->getBufferByID(buffer);
		// if file is read only, we set the view read only
		if (isReadOnly)
			buf->setUserReadOnly(true);

		// Notify plugins that current file is about to open
		scnN.nmhdr.code = NPPN_FILEBEFOREOPEN;
		scnN.nmhdr.idFrom = (uptr_t)buffer;
		_pluginsManager->notify(&scnN);


		loadBufferIntoView(buffer, currentView());

		if (_pTrayIco)
		{
			if (_pTrayIco->isInTray())
			{
				::ShowWindow(_pPublicInterface->getHSelf(), SW_SHOW);
				if (!_pPublicInterface->isPrelaunch())
					_pTrayIco->doTrayIcon(TRAYICON_REMOVE);
				::SendMessage(_pPublicInterface->getHSelf(), WM_SIZE, 0, 0);
			}
		}
		PathRemoveFileSpec(longFileName);
		_linkTriggered = true;
		_isDocModifing = false;

		_isFileOpening = false;

		// Notify plugins that current file is just opened
		scnN.nmhdr.code = NPPN_FILEOPENED;
		_pluginsManager->notify(&scnN);

		return buffer;
	}
	else
	{
		if (::PathIsDirectory(fileName))
		{
			std::vector<generic_string> fileNames;
			std::vector<generic_string> patterns;
			patterns.push_back(TEXT("*.*"));

			generic_string fileNameStr = fileName;
			if (fileName[lstrlen(fileName) - 1] != '\\')
				fileNameStr += TEXT("\\");

			getMatchedFileNames(fileNameStr.c_str(), patterns, fileNames, true, false);
			for (size_t i = 0 ; i < fileNames.size() ; i++)
			{
				doOpen(fileNames[i].c_str());
			}
		}
		else
		{
			generic_string msg = TEXT("Cannot open file \"");
			msg += longFileName;
			msg += TEXT("\".");
			::MessageBox(_pPublicInterface->getHSelf(), msg.c_str(), TEXT("ERROR"), MB_OK);
			_isFileOpening = false;

			scnN.nmhdr.code = NPPN_FILELOADFAILED;
			_pluginsManager->notify(&scnN);
		}
		return BUFFER_INVALID;
	}
}
// return true if all the session files are loaded
// return false if one or more sessions files fail to load (and session is modify to remove invalid files)
bool Notepad_plus::loadSession(Session & session)
{
	NppParameters *pNppParam = NppParameters::getInstance();
	bool allSessionFilesLoaded = true;
	BufferID lastOpened = BUFFER_INVALID;
	size_t i = 0;
	showView(MAIN_VIEW);
	switchEditViewTo(MAIN_VIEW);	//open files in main
	for ( ; i < session.nbMainFiles() ; )
	{
		const TCHAR *pFn = session._mainViewFiles[i]._fileName.c_str();
		if (NotepadFile(pFn, -1).isFileSession())
		{
			vector<sessionFileInfo>::iterator posIt = session._mainViewFiles.begin() + i;
			session._mainViewFiles.erase(posIt);
			continue;	//skip session files, not supporting recursive sessions
		}

		bool isWow64Off = false;
		if (!PathFileExists(pFn))
		{
			pNppParam->safeWow64EnableWow64FsRedirection(FALSE);
			isWow64Off = true;
		}
		if (PathFileExists(pFn)) 
		{
			lastOpened = doOpen(pFn, false, session._mainViewFiles[i]._encoding);
		}
		else
		{
			lastOpened = BUFFER_INVALID;
		}
		if (isWow64Off)
		{
			pNppParam->safeWow64EnableWow64FsRedirection(TRUE);
			isWow64Off = false;
		}

		if (lastOpened != BUFFER_INVALID)
		{
			showView(MAIN_VIEW);
			const TCHAR *pLn = session._mainViewFiles[i]._langName.c_str();
			int id = getLangFromMenuName(pLn);
			LangType typeToSet = L_TEXT;
			if (id != 0 && id != IDM_LANG_USER)
				typeToSet = menuID2LangType(id);
			if (typeToSet == L_EXTERNAL )
				typeToSet = (LangType)(id - IDM_LANG_EXTERNAL + L_EXTERNAL);

			Buffer * buf = MainFileManager->getBufferByID(lastOpened);
			buf->setPosition(session._mainViewFiles[i], &_mainEditView);
			buf->setLangType(typeToSet, pLn);
			if (session._mainViewFiles[i]._encoding != -1)
				buf->setEncoding(session._mainViewFiles[i]._encoding);

			//Force in the document so we can add the markers
			//Dont use default methods because of performance
			Document prevDoc = _mainEditView.execute(SCI_GETDOCPOINTER);
			_mainEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
			for (size_t j = 0 ; j < session._mainViewFiles[i].marks.size() ; j++) 
			{
				_mainEditView.execute(SCI_MARKERADD, session._mainViewFiles[i].marks[j], MARK_BOOKMARK);
			}
			_mainEditView.execute(SCI_SETDOCPOINTER, 0, prevDoc);
			i++;
		}
		else
		{
			vector<sessionFileInfo>::iterator posIt = session._mainViewFiles.begin() + i;
			session._mainViewFiles.erase(posIt);
			allSessionFilesLoaded = false;
		}
	}

	size_t k = 0;
	showView(SUB_VIEW);
	switchEditViewTo(SUB_VIEW);	//open files in sub
	for ( ; k < session.nbSubFiles() ; )
	{
		const TCHAR *pFn = session._subViewFiles[k]._fileName.c_str();
		if (NotepadFile(pFn, -1).isFileSession()) {
			vector<sessionFileInfo>::iterator posIt = session._subViewFiles.begin() + k;
			session._subViewFiles.erase(posIt);
			continue;	//skip session files, not supporting recursive sessions
		}

		bool isWow64Off = false;
		if (!PathFileExists(pFn))
		{
			pNppParam->safeWow64EnableWow64FsRedirection(FALSE);
			isWow64Off = true;
		}
		if (PathFileExists(pFn)) 
		{
			lastOpened = doOpen(pFn, false, session._subViewFiles[k]._encoding);
			//check if already open in main. If so, clone
			if (_mainDocTab.getIndexByBuffer(lastOpened) != -1) {
				loadBufferIntoView(lastOpened, SUB_VIEW);
			}
		}
		else 
		{
			lastOpened = BUFFER_INVALID;
		}
		if (isWow64Off)
		{
			pNppParam->safeWow64EnableWow64FsRedirection(TRUE);
			isWow64Off = false;
		}

		if (lastOpened != BUFFER_INVALID)
		{
			showView(SUB_VIEW);
			if (canHideView(MAIN_VIEW))
				hideView(MAIN_VIEW);
			const TCHAR *pLn = session._subViewFiles[k]._langName.c_str();
			int id = getLangFromMenuName(pLn);
			LangType typeToSet = L_TEXT;
			if (id != 0)
				typeToSet = menuID2LangType(id);
			if (typeToSet == L_EXTERNAL )
				typeToSet = (LangType)(id - IDM_LANG_EXTERNAL + L_EXTERNAL);

			Buffer * buf = MainFileManager->getBufferByID(lastOpened);
			buf->setPosition(session._subViewFiles[k], &_subEditView);
			if (typeToSet == L_USER) {
				if (!lstrcmp(pLn, TEXT("User Defined"))) {
					pLn = TEXT("");	//default user defined
				}
			}
			buf->setLangType(typeToSet, pLn);
			buf->setEncoding(session._subViewFiles[k]._encoding);
			
			//Force in the document so we can add the markers
			//Dont use default methods because of performance
			Document prevDoc = _subEditView.execute(SCI_GETDOCPOINTER);
			_subEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
			for (size_t j = 0 ; j < session._subViewFiles[k].marks.size() ; j++) 
			{
				_subEditView.execute(SCI_MARKERADD, session._subViewFiles[k].marks[j], MARK_BOOKMARK);
			}
			_subEditView.execute(SCI_SETDOCPOINTER, 0, prevDoc);

			k++;
		}
		else
		{
			vector<sessionFileInfo>::iterator posIt = session._subViewFiles.begin() + k;
			session._subViewFiles.erase(posIt);
			allSessionFilesLoaded = false;
		}
	}

	_mainEditView.restoreCurrentPos();
	_subEditView.restoreCurrentPos();

	if (session._activeMainIndex < (size_t)_mainDocTab.nbItem())//session.nbMainFiles())
		activateBuffer(_mainDocTab.getBufferByIndex(session._activeMainIndex), MAIN_VIEW);

	if (session._activeSubIndex < (size_t)_subDocTab.nbItem())//session.nbSubFiles())
		activateBuffer(_subDocTab.getBufferByIndex(session._activeSubIndex), SUB_VIEW);

	if ((session.nbSubFiles() > 0) && (session._activeView == MAIN_VIEW || session._activeView == SUB_VIEW))
		switchEditViewTo(session._activeView);
	else
		switchEditViewTo(MAIN_VIEW);

	if (canHideView(otherView()))
		hideView(otherView());
	else if (canHideView(currentView()))
		hideView(currentView());
	return allSessionFilesLoaded;
}