aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNeil Hodgson <nyamatongwe@gmail.com>2015-02-16 11:13:24 +1100
committerNeil Hodgson <nyamatongwe@gmail.com>2015-02-16 11:13:24 +1100
commit04477a781d31e2c4e3b10cfe09d7c025fd897421 (patch)
tree0cf04b2c878275d6d35a425f3efc34ad5db831e4
parent4e705edb0910461ebe675eb57d8fd08cc94495ec (diff)
downloadscintilla-mirror-04477a781d31e2c4e3b10cfe09d7c025fd897421.tar.gz
Implement SCI_TARGETASUTF8 and SCI_ENCODEDFROMUTF8 on Cocoa.
-rw-r--r--cocoa/ScintillaCocoa.h2
-rw-r--r--cocoa/ScintillaCocoa.mm89
-rw-r--r--doc/ScintillaDoc.html4
3 files changed, 94 insertions, 1 deletions
diff --git a/cocoa/ScintillaCocoa.h b/cocoa/ScintillaCocoa.h
index 384282270..45d04e564 100644
--- a/cocoa/ScintillaCocoa.h
+++ b/cocoa/ScintillaCocoa.h
@@ -105,6 +105,8 @@ private:
bool GetPasteboardData(NSPasteboard* board, SelectionText* selectedText);
void SetPasteboardData(NSPasteboard* board, const SelectionText& selectedText);
+ int TargetAsUTF8(char *text);
+ int EncodedFromUTF8(char *utf8, char *encoded) const;
int scrollSpeed;
int scrollTicks;
diff --git a/cocoa/ScintillaCocoa.mm b/cocoa/ScintillaCocoa.mm
index 45ce0cc86..f248f3bb0 100644
--- a/cocoa/ScintillaCocoa.mm
+++ b/cocoa/ScintillaCocoa.mm
@@ -526,6 +526,26 @@ static char *EncodedBytes(CFStringRef cfsRef, CFStringEncoding encoding) {
//--------------------------------------------------------------------------------------------------
/**
+ * Convert a core foundation string into a std::string in a particular encoding
+ */
+
+static std::string EncodedBytesString(CFStringRef cfsRef, CFStringEncoding encoding) {
+ const CFRange rangeAll = {0, CFStringGetLength(cfsRef)};
+ CFIndex usedLen = 0;
+ CFStringGetBytes(cfsRef, rangeAll, encoding, '?', false,
+ NULL, 0, &usedLen);
+
+ std::string buffer(usedLen, '\0');
+ if (usedLen > 0) {
+ CFStringGetBytes(cfsRef, rangeAll, encoding, '?', false,
+ reinterpret_cast<UInt8 *>(&buffer[0]), usedLen, NULL);
+ }
+ return buffer;
+}
+
+//--------------------------------------------------------------------------------------------------
+
+/**
* Case folders.
*/
@@ -825,6 +845,13 @@ sptr_t ScintillaCocoa::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lPar
case SCI_GETDIRECTPOINTER:
return reinterpret_cast<sptr_t>(this);
+ case SCI_TARGETASUTF8:
+ return TargetAsUTF8(reinterpret_cast<char*>(lParam));
+
+ case SCI_ENCODEDFROMUTF8:
+ return EncodedFromUTF8(reinterpret_cast<char*>(wParam),
+ reinterpret_cast<char*>(lParam));
+
case SCI_SETIMEINTERACTION:
// Only inline IME supported on Cocoa
break;
@@ -1543,6 +1570,68 @@ bool ScintillaCocoa::GetPasteboardData(NSPasteboard* board, SelectionText* selec
//--------------------------------------------------------------------------------------------------
+// Returns the target converted to UTF8.
+// Return the length in bytes.
+int ScintillaCocoa::TargetAsUTF8(char *text)
+{
+ const int targetLength = targetEnd - targetStart;
+ if (IsUnicodeMode())
+ {
+ if (text)
+ pdoc->GetCharRange(text, targetStart, targetLength);
+ }
+ else
+ {
+ // Need to convert
+ const CFStringEncoding encoding = EncodingFromCharacterSet(IsUnicodeMode(),
+ vs.styles[STYLE_DEFAULT].characterSet);
+ const std::string s = RangeText(targetStart, targetEnd);
+ CFStringRef cfsVal = CFStringCreateWithBytes(kCFAllocatorDefault,
+ reinterpret_cast<const UInt8 *>(s.c_str()),
+ s.length(), encoding, false);
+
+ const std::string tmputf = EncodedBytesString(cfsVal, kCFStringEncodingUTF8);
+
+ if (text)
+ memcpy(text, tmputf.c_str(), tmputf.length());
+ CFRelease(cfsVal);
+ return tmputf.length();
+ }
+ return targetLength;
+}
+
+//--------------------------------------------------------------------------------------------------
+
+// Translates a UTF8 string into the document encoding.
+// Return the length of the result in bytes.
+int ScintillaCocoa::EncodedFromUTF8(char *utf8, char *encoded) const
+{
+ const int inputLength = (lengthForEncode >= 0) ? lengthForEncode : strlen(utf8);
+ if (IsUnicodeMode())
+ {
+ if (encoded)
+ memcpy(encoded, utf8, inputLength);
+ return inputLength;
+ }
+ else
+ {
+ // Need to convert
+ const CFStringEncoding encoding = EncodingFromCharacterSet(IsUnicodeMode(),
+ vs.styles[STYLE_DEFAULT].characterSet);
+
+ CFStringRef cfsVal = CFStringCreateWithBytes(kCFAllocatorDefault,
+ reinterpret_cast<const UInt8 *>(utf8),
+ inputLength, kCFStringEncodingUTF8, false);
+ const std::string sEncoded = EncodedBytesString(cfsVal, encoding);
+ if (encoded)
+ memcpy(encoded, sEncoded.c_str(), sEncoded.length());
+ CFRelease(cfsVal);
+ return sEncoded.length();
+ }
+}
+
+//--------------------------------------------------------------------------------------------------
+
void ScintillaCocoa::SetMouseCapture(bool on)
{
capturedMouse = on;
diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html
index 46039ed54..1749fa1f9 100644
--- a/doc/ScintillaDoc.html
+++ b/doc/ScintillaDoc.html
@@ -585,12 +585,14 @@ struct Sci_TextRange {
};
</pre>
- <h3 id="EncodedAccess">GTK+-specific: Access to encoded text</h3>
+ <h3 id="EncodedAccess">Specific to GTK+ and Cocoa only: Access to encoded text</h3>
<p><b id="SCI_TARGETASUTF8">SCI_TARGETASUTF8(&lt;unused&gt;, 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.
</p>
<p><b id="SCI_ENCODEDFROMUTF8">SCI_ENCODEDFROMUTF8(const char *utf8, char *encoded)</b><br />