diff options
Diffstat (limited to 'win32/ScintillaWin.cxx')
| -rw-r--r-- | win32/ScintillaWin.cxx | 59 | 
1 files changed, 59 insertions, 0 deletions
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);  		}  | 
