diff --git a/DlgConsole.cpp b/DlgConsole.cpp index 250aec8..2f5140b 100755 --- a/DlgConsole.cpp +++ b/DlgConsole.cpp @@ -508,6 +508,17 @@ static void OnClose(HWND hWnd) EndDialog(hWnd, 0); } +///////////////////////////////////////////////////////////////////////////// +// Convert a wide Unicode string to an UTF8 string + +static std::string utf8_encode(const std::wstring &wstr) +{ + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); + std::string strTo(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); + return strTo; +} + ///////////////////////////////////////////////////////////////////////////// // Insert the snippet (selected from the context menu item) into the text // in the Scintilla window. Double clicking get redirected to here as well. @@ -547,7 +558,9 @@ static void OnSnippetInsert(HWND hWnd) } // Insert the first part of the snippet - SendMsg(SCI_REPLACESEL, 0, (LPARAM) snip.GetBeforeSelection()); + std::wstring wstr = snip.WGetBeforeSelection(); + std::string strTo = utf8_encode(wstr); + SendMsg(SCI_REPLACESEL, 0, (LPARAM) strTo.c_str()); int beforeSel = (int) SendMsg(SCI_GETCURRENTPOS); @@ -566,7 +579,11 @@ static void OnSnippetInsert(HWND hWnd) { size_t len = wcslen(snip.WGetAfterSelection()); if (len > 0) - SendMsg(SCI_ADDTEXT, len, (LPARAM) snip.GetAfterSelection()); + { + std::wstring wstr = snip.WGetAfterSelection(); + std::string strTo = utf8_encode(wstr); + SendMsg(SCI_REPLACESEL, 0, (LPARAM) strTo.c_str()); + } } // Restore the cursor position and selection diff --git a/Snippets.cpp b/Snippets.cpp index 97d66ef..3d24836 100755 --- a/Snippets.cpp +++ b/Snippets.cpp @@ -149,11 +149,12 @@ void Snippet::SetBeforeSelection(LPCSTR txt) WSetBeforeSelection(NULL); return; } - size_t size = sizeof(WCHAR) * (len + 3); + // Convert from UTF-8 to WCHAR and store + size_t size = MultiByteToWideChar(CP_UTF8, 0, txt, (int) len, NULL, 0); WCHAR* wBuffer = (WCHAR*) malloc(size); ZeroMemory(wBuffer, size); - Ansi2Unicode(wBuffer, txt, len); + MultiByteToWideChar(CP_UTF8, 0, txt, (int) len, wBuffer, size); WSetBeforeSelection(wBuffer); free(wBuffer); }