diff options
author | Neil <nyamatongwe@gmail.com> | 2015-05-20 10:57:02 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2015-05-20 10:57:02 +1000 |
commit | 172c81f3d02a240b5b4b8d7215bcfecfbf5fa9f3 (patch) | |
tree | 1f72857679732287be84b5ede6cba31dac83310f | |
parent | d9bed61b8ebe3d289d7a9c468d8cb39d1ebd0bdd (diff) | |
download | scintilla-mirror-172c81f3d02a240b5b4b8d7215bcfecfbf5fa9f3.tar.gz |
SCI_TARGETASUTF8 and SCI_ENCODEDFROMUTF8 implemented on Win32.
From johnsonj.
-rw-r--r-- | doc/ScintillaDoc.html | 4 | ||||
-rw-r--r-- | doc/ScintillaHistory.html | 4 | ||||
-rw-r--r-- | win32/ScintillaWin.cxx | 59 |
3 files changed, 65 insertions, 2 deletions
diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html index da63297bf..3a51cfb61 100644 --- a/doc/ScintillaDoc.html +++ b/doc/ScintillaDoc.html @@ -587,14 +587,14 @@ struct Sci_TextRange { }; </pre> - <h3 id="EncodedAccess">Specific to GTK+ and Cocoa only: Access to encoded text</h3> + <h3 id="EncodedAccess">Specific to GTK+, Cocoa and Windows only: Access to encoded text</h3> <p><b id="SCI_TARGETASUTF8">SCI_TARGETASUTF8(<unused>, char *s)</b><br /> This method retrieves the value of the target encoded as UTF-8 which is the default encoding of GTK+ so is useful for retrieving text for use in other parts of the user interface, such as find and replace dialogs. The length of the encoded text in bytes is returned. Cocoa uses UTF-16 which is easily converted from UTF-8 so this method can be used to perform the - more complex work of transcoding from the various of encodings supported. + more complex work of transcoding from the various encodings supported. </p> <p><b id="SCI_ENCODEDFROMUTF8">SCI_ENCODEDFROMUTF8(const char *utf8, char *encoded)</b><br /> diff --git a/doc/ScintillaHistory.html b/doc/ScintillaHistory.html index 2b776d254..10ec216f9 100644 --- a/doc/ScintillaHistory.html +++ b/doc/ScintillaHistory.html @@ -494,6 +494,10 @@ On Qt, use fractional positioning calls and avoid rounding to ensure consistency. </li> <li> + SCI_TARGETASUTF8 and SCI_ENCODEDFROMUTF8 implemented on + Win32 as well as GTK+ and Cocoa. + </li> + <li> C++ lexer fixes empty backquoted string. <a href="http://sourceforge.net/p/scintilla/bugs/1711/">Bug #1711</a>. </li> diff --git a/win32/ScintillaWin.cxx b/win32/ScintillaWin.cxx index 666dc75e3..176faed1b 100644 --- a/win32/ScintillaWin.cxx +++ b/win32/ScintillaWin.cxx @@ -244,6 +244,8 @@ class ScintillaWin : virtual bool DragThreshold(Point ptStart, Point ptNow); virtual void StartDrag(); + int TargetAsUTF8(char *text); + int EncodedFromUTF8(char *utf8, char *encoded) const; sptr_t WndPaint(uptr_t wParam); sptr_t HandleCompositionWindowed(uptr_t wParam, sptr_t lParam); @@ -652,6 +654,56 @@ static bool BoundsContains(PRectangle rcBounds, HRGN hRgnBounds, PRectangle rcCh return contains; } +// Returns the target converted to UTF8. +// Return the length in bytes. +int ScintillaWin::TargetAsUTF8(char *text) { + int targetLength = targetEnd - targetStart; + if (IsUnicodeMode()) { + if (text) { + pdoc->GetCharRange(text, targetStart, targetLength); + } + } else { + // Need to convert + std::string s = RangeText(targetStart, targetEnd); + int charsLen = ::MultiByteToWideChar(CodePageOfDocument(), 0, &s[0], targetLength, NULL, 0); + std::wstring characters(charsLen, '\0'); + ::MultiByteToWideChar(CodePageOfDocument(), 0, &s[0], targetLength, &characters[0], charsLen); + + int utf8Len = ::WideCharToMultiByte(CP_UTF8, 0, &characters[0], charsLen, NULL, 0, 0, 0); + if (text) { + ::WideCharToMultiByte(CP_UTF8, 0, &characters[0], charsLen, text, utf8Len, 0, 0); + text[utf8Len] = '\0'; + } + return utf8Len; + } + return targetLength; +} + +// Translates a nul terminated UTF8 string into the document encoding. +// Return the length of the result in bytes. +int ScintillaWin::EncodedFromUTF8(char *utf8, char *encoded) const { + int inputLength = (lengthForEncode >= 0) ? lengthForEncode : static_cast<int>(strlen(utf8)); + if (IsUnicodeMode()) { + if (encoded) { + memcpy(encoded, utf8, inputLength); + } + return inputLength; + } else { + // Need to convert + int charsLen = ::MultiByteToWideChar(CP_UTF8, 0, utf8, inputLength, NULL, 0); + std::wstring characters(charsLen, '\0'); + ::MultiByteToWideChar(CP_UTF8, 0, utf8, inputLength, &characters[0], charsLen); + + int encodedLen = ::WideCharToMultiByte(CodePageOfDocument(), + 0, &characters[0], charsLen, NULL, 0, 0, 0); + if (encoded) { + ::WideCharToMultiByte(CodePageOfDocument(), 0, &characters[0], charsLen, encoded, encodedLen, 0, 0); + encoded[encodedLen] = '\0'; + } + return encodedLen; + } +} + LRESULT ScintillaWin::WndPaint(uptr_t wParam) { //ElapsedTime et; @@ -1634,6 +1686,13 @@ sptr_t ScintillaWin::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam break; #endif + case SCI_TARGETASUTF8: + return TargetAsUTF8(reinterpret_cast<char*>(lParam)); + + case SCI_ENCODEDFROMUTF8: + return EncodedFromUTF8(reinterpret_cast<char*>(wParam), + reinterpret_cast<char*>(lParam)); + default: return ScintillaBase::WndProc(iMessage, wParam, lParam); } |