NotepadFile NotepadFile::getDirectoryPart()
{
	TCHAR str2display[MAX_PATH*2];
	generic_string longFileDir(_longFileName);
	PathRemoveFileSpec(longFileDir);
	return NotepadFile(longFileDir.c_str(), -1);
}
Example #2
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;
	}
}