Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PythonScript/src/MenuManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ void MenuManager::updatePreviousScript(const char *filename)
m_previousRunFilename = filename;

tstring tdisplayName(_T("Run Previous Script ("));
tdisplayName.append(WcharMbcsConverter::char2tchar(displayName).get());
tdisplayName.append(WcharMbcsConverter::char2acp(displayName).get());
tdisplayName.append(_T(")"));

if (!m_runLastScriptShortcut.empty())
Expand Down
19 changes: 12 additions & 7 deletions PythonScript/src/PythonConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void PythonConsole::initPython(PythonHandler *pythonHandler)
void PythonConsole::pythonShowDialog()
{
assert(mp_consoleDlg);
GILRelease release;
if (mp_consoleDlg)
{
// Post the message to ourselves (on the right thread) to create the window
Expand All @@ -144,6 +145,7 @@ void PythonConsole::showDialog()
assert(mp_consoleDlg);
if (mp_consoleDlg)
{
GILRelease release;
mp_consoleDlg->doDialog();
}
}
Expand All @@ -153,6 +155,7 @@ void PythonConsole::hideDialog()
assert(mp_consoleDlg);
if (mp_consoleDlg)
{
GILRelease release;
mp_consoleDlg->hide();
}
}
Expand All @@ -162,6 +165,7 @@ void PythonConsole::message(const char *msg)
assert(mp_consoleDlg);
if (mp_consoleDlg)
{
GILRelease release;
mp_consoleDlg->writeText(strlen(msg), msg);
}
}
Expand All @@ -171,6 +175,7 @@ void PythonConsole::clear()
assert(mp_consoleDlg);
if (mp_consoleDlg)
{
GILRelease release;
mp_consoleDlg->clearText();
}
}
Expand Down Expand Up @@ -210,14 +215,15 @@ void PythonConsole::writeError(boost::python::object text)
if (PyUnicode_Check(text.ptr()))
{
boost::python::object utf8String(boost::python::handle<PyObject>(PyUnicode_AsUTF8String(text.ptr())));

std::string textToWrite((const char *)boost::python::extract<const char *>(utf8String));
std::string textToWrite((const char *)boost::python::extract<const char *>(utf8String));
GILRelease release;
mp_consoleDlg->writeError(textToWrite.size(), textToWrite.c_str());
}
else
{
std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()));
std::shared_ptr<wchar_t> wacpString = WcharMbcsConverter::char2acp(boost::python::extract<const char *>(text.attr("__str__")()));
std::string textToWrite(WcharMbcsConverter::tchar2char(wacpString.get()).get());
//std::string textToWrite((const char *)boost::python::extract<const char *>(text.attr("__str__")()), _len(text));
GILRelease release;
mp_consoleDlg->writeError(textToWrite.size(),textToWrite.c_str());
}
Expand Down Expand Up @@ -272,9 +278,8 @@ void PythonConsole::queueComplete()

void PythonConsole::consume(std::shared_ptr<std::string> statement)
{
GILLock gilLock;

bool continuePrompt = false;
bool continuePrompt = false;
GILLock gilLock;
try
{
boost::python::object oldStdout = m_sys.attr("stdout");
Expand Down Expand Up @@ -327,7 +332,7 @@ void export_console()

void PythonConsole::openFile(const char *filename, idx_t lineNo)
{
std::shared_ptr<TCHAR> tFilename = WcharMbcsConverter::char2tchar(filename);
std::shared_ptr<TCHAR> tFilename = WcharMbcsConverter::char2wchar(filename);
if (!SendMessage(m_hNotepad, NPPM_SWITCHTOFILE, 0, reinterpret_cast<LPARAM>(tFilename.get())))
{
SendMessage(m_hNotepad, NPPM_DOOPEN, 0, reinterpret_cast<LPARAM>(tFilename.get()));
Expand Down
36 changes: 16 additions & 20 deletions PythonScript/src/PythonScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ static void runScript(idx_t number)
MenuManager::s_menuItemClicked = true;
}

runScript(ConfigFile::getInstance()->getMenuScript(number).c_str(), false);
std::shared_ptr<wchar_t> wscript = WcharMbcsConverter::char2tchar(ConfigFile::getInstance()->getMenuScript(number).c_str());
std::shared_ptr<char> script = WcharMbcsConverter::acp2char(wscript.get());
runScript(script.get(), false);
}


Expand Down Expand Up @@ -539,9 +541,9 @@ static void runScript(const char *filename, bool synchronous, HANDLE completedEv
&& ((keyState[VK_SHIFT] & 0x80) == 0)
&& ((keyState[VK_MENU] & 0x80) == 0))
{
if (!SendMessage(nppData._nppHandle, NPPM_SWITCHTOFILE, 0, reinterpret_cast<LPARAM>(WcharMbcsConverter::char2tchar(filename).get())))
if (!SendMessage(nppData._nppHandle, NPPM_SWITCHTOFILE, 0, reinterpret_cast<LPARAM>(WcharMbcsConverter::char2acp(filename).get())))
{
SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast<LPARAM>(WcharMbcsConverter::char2tchar(filename).get()));
SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast<LPARAM>(WcharMbcsConverter::char2acp(filename).get()));
}
}
else
Expand Down Expand Up @@ -621,43 +623,37 @@ static void ensurePathExists(const tstring& path)

static void newScript()
{

OPENFILENAMEA ofn;
memset(&ofn, 0, sizeof(OPENFILENAMEA));
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(OPENFILENAME));

ofn.lStructSize = sizeof(OPENFILENAMEA);
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = nppData._nppHandle;
ensurePathExists(ConfigFile::getInstance()->getUserScriptsDir());
std::shared_ptr<char> userScriptsDir = WcharMbcsConverter::tchar2char(ConfigFile::getInstance()->getUserScriptsDir().c_str());
ofn.lpstrInitialDir = userScriptsDir.get();
//ofn.lpstrFileTitle = "Choose filename for new script";
ofn.lpstrFile = new char[MAX_PATH];

ofn.lpstrInitialDir = ConfigFile::getInstance()->getUserScriptsDir().c_str();
ofn.lpstrFile = new TCHAR[MAX_PATH];
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "py";
ofn.lpstrDefExt = L"py";
//lint -e840 Use of nul character in a string literal
// This is how it's meant to be used.
ofn.lpstrFilter = "Python Source Files (*.py)\0*.py\0All Files (*.*)\0*.*\0";
ofn.lpstrFilter = L"Python Source Files (*.py)\0*.py\0All Files (*.*)\0*.*\0";
//lint +e840
ofn.nFilterIndex = 1;

ofn.Flags = OFN_OVERWRITEPROMPT;


if (GetSaveFileNameA(&ofn))
if (GetSaveFileName(&ofn))
{

HANDLE hFile = CreateFileA(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hFile = CreateFile(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
CloseHandle(hFile);
SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast<LPARAM>(WcharMbcsConverter::char2tchar(ofn.lpstrFile).get()));
SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast<LPARAM>(ofn.lpstrFile));
intptr_t bufferID = SendMessage(nppData._nppHandle, NPPM_GETCURRENTBUFFERID, 0, 0);
SendMessage(nppData._nppHandle, NPPM_SETBUFFERLANGTYPE, L_PYTHON, bufferID);
}


delete [] ofn.lpstrFile;


}


Expand Down
43 changes: 43 additions & 0 deletions PythonScript/src/WcharMbcsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,49 @@ std::shared_ptr<char> WcharMbcsConverter::wchar2char(const wchar_t* wcStr)
return multiByteStr;
}

std::shared_ptr<wchar_t> WcharMbcsConverter::char2acp(const char* mbStr)
{

std::shared_ptr<wchar_t> wideCharStr;

size_t len = (size_t)MultiByteToWideChar(CP_ACP, 0, mbStr, -1, NULL, 0);


if (len > 0)
{
wideCharStr.reset(new wchar_t[len]);
MultiByteToWideChar(CP_ACP, 0, mbStr, -1, wideCharStr.get(), (int)len);
}
else
{
wideCharStr.reset(new wchar_t[1]);
wideCharStr.get()[0] = 0;
}

return wideCharStr;
}

std::shared_ptr<char> WcharMbcsConverter::acp2char(const wchar_t* wcStr)
{

std::shared_ptr<char> multiByteStr;

size_t len = (size_t)WideCharToMultiByte(CP_ACP, 0, wcStr, -1, NULL, 0, NULL, NULL);

if (len > 0)
{
multiByteStr.reset(new char[len]);
WideCharToMultiByte(CP_ACP, 0, wcStr, -1, multiByteStr.get(), (int)len, NULL, NULL);
}
else
{
multiByteStr.reset(new char[1]);
multiByteStr.get()[0] = 0;
}

return multiByteStr;
}

//static boost::std::shared_ptr<const TCHAR> char2tchar(const char* mbStr);
//static boost::std::shared_ptr<const char> tchar2char(const TCHAR* tStr);
std::shared_ptr<TCHAR> WcharMbcsConverter::char2tchar(const char* mbStr)
Expand Down
2 changes: 2 additions & 0 deletions PythonScript/src/WcharMbcsConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class WcharMbcsConverter {

static std::shared_ptr<wchar_t> char2wchar(const char* mbStr);
static std::shared_ptr<char> wchar2char(const wchar_t* wcStr);
static std::shared_ptr<wchar_t> char2acp(const char* mbStr);
static std::shared_ptr<char> acp2char(const wchar_t* wcStr);

static std::shared_ptr<TCHAR> char2tchar(const char* mbStr);
static std::shared_ptr<char> tchar2char(const TCHAR* tStr);
Expand Down