The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. // (This file gets included by juce_mac_NativeCode.mm, rather than being
  24. // compiled on its own).
  25. #ifdef JUCE_INCLUDED_FILE
  26. //==============================================================================
  27. static const String nsStringToJuce (NSString* s)
  28. {
  29. return String::fromUTF8 ((uint8*) [s UTF8String]);
  30. }
  31. static NSString* juceStringToNS (const String& s)
  32. {
  33. return [NSString stringWithUTF8String: (const char*) s.toUTF8()];
  34. }
  35. //==============================================================================
  36. static const String convertUTF16ToString (const UniChar* utf16)
  37. {
  38. String s;
  39. while (*utf16 != 0)
  40. s += (juce_wchar) *utf16++;
  41. return s;
  42. }
  43. const String PlatformUtilities::cfStringToJuceString (CFStringRef cfString)
  44. {
  45. String result;
  46. if (cfString != 0)
  47. {
  48. #if JUCE_STRINGS_ARE_UNICODE
  49. CFRange range = { 0, CFStringGetLength (cfString) };
  50. UniChar* const u = (UniChar*) juce_malloc (sizeof (UniChar) * (range.length + 1));
  51. CFStringGetCharacters (cfString, range, u);
  52. u[range.length] = 0;
  53. result = convertUTF16ToString (u);
  54. juce_free (u);
  55. #else
  56. const int len = CFStringGetLength (cfString);
  57. char* buffer = (char*) juce_malloc (len + 1);
  58. CFStringGetCString (cfString, buffer, len + 1, CFStringGetSystemEncoding());
  59. result = buffer;
  60. juce_free (buffer);
  61. #endif
  62. }
  63. return result;
  64. }
  65. CFStringRef PlatformUtilities::juceStringToCFString (const String& s)
  66. {
  67. #if JUCE_STRINGS_ARE_UNICODE
  68. const int len = s.length();
  69. const juce_wchar* t = (const juce_wchar*) s;
  70. UniChar* temp = (UniChar*) juce_malloc (sizeof (UniChar) * len + 4);
  71. for (int i = 0; i <= len; ++i)
  72. temp[i] = t[i];
  73. CFStringRef result = CFStringCreateWithCharacters (kCFAllocatorDefault, temp, len);
  74. juce_free (temp);
  75. return result;
  76. #else
  77. return CFStringCreateWithCString (kCFAllocatorDefault,
  78. (const char*) s,
  79. CFStringGetSystemEncoding());
  80. #endif
  81. }
  82. const String PlatformUtilities::convertToPrecomposedUnicode (const String& s)
  83. {
  84. UnicodeMapping map;
  85. map.unicodeEncoding = CreateTextEncoding (kTextEncodingUnicodeDefault,
  86. kUnicodeNoSubset,
  87. kTextEncodingDefaultFormat);
  88. map.otherEncoding = CreateTextEncoding (kTextEncodingUnicodeDefault,
  89. kUnicodeCanonicalCompVariant,
  90. kTextEncodingDefaultFormat);
  91. map.mappingVersion = kUnicodeUseLatestMapping;
  92. UnicodeToTextInfo conversionInfo = 0;
  93. String result;
  94. if (CreateUnicodeToTextInfo (&map, &conversionInfo) == noErr)
  95. {
  96. const int len = s.length();
  97. UniChar* const tempIn = (UniChar*) juce_calloc (sizeof (UniChar) * len + 4);
  98. UniChar* const tempOut = (UniChar*) juce_calloc (sizeof (UniChar) * len + 4);
  99. for (int i = 0; i <= len; ++i)
  100. tempIn[i] = s[i];
  101. ByteCount bytesRead = 0;
  102. ByteCount outputBufferSize = 0;
  103. if (ConvertFromUnicodeToText (conversionInfo,
  104. len * sizeof (UniChar), tempIn,
  105. kUnicodeDefaultDirectionMask,
  106. 0, 0, 0, 0,
  107. len * sizeof (UniChar), &bytesRead,
  108. &outputBufferSize, tempOut) == noErr)
  109. {
  110. result.preallocateStorage (bytesRead / sizeof (UniChar) + 2);
  111. tchar* t = const_cast <tchar*> ((const tchar*) result);
  112. unsigned int i;
  113. for (i = 0; i < bytesRead / sizeof (UniChar); ++i)
  114. t[i] = (tchar) tempOut[i];
  115. t[i] = 0;
  116. }
  117. juce_free (tempIn);
  118. juce_free (tempOut);
  119. DisposeUnicodeToTextInfo (&conversionInfo);
  120. }
  121. return result;
  122. }
  123. //==============================================================================
  124. #if ! JUCE_ONLY_BUILD_CORE_LIBRARY
  125. void SystemClipboard::copyTextToClipboard (const String& text) throw()
  126. {
  127. [[NSPasteboard generalPasteboard] declareTypes: [NSArray arrayWithObject: NSStringPboardType]
  128. owner: nil];
  129. [[NSPasteboard generalPasteboard] setString: juceStringToNS (text)
  130. forType: NSStringPboardType];
  131. }
  132. const String SystemClipboard::getTextFromClipboard() throw()
  133. {
  134. NSString* text = [[NSPasteboard generalPasteboard] stringForType: NSStringPboardType];
  135. return text == 0 ? String::empty
  136. : nsStringToJuce (text);
  137. }
  138. #endif
  139. #endif