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.

184 lines
5.9KB

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