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.

198 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. #if JUCE_MAC
  21. //==============================================================================
  22. class MouseCursor::PlatformSpecificHandle
  23. {
  24. public:
  25. PlatformSpecificHandle (const MouseCursor::StandardCursorType type)
  26. : cursorHandle (createCursor (type)) {}
  27. PlatformSpecificHandle (const detail::CustomMouseCursorInfo& info)
  28. : cursorHandle (createCursor (info)) {}
  29. ~PlatformSpecificHandle()
  30. {
  31. [cursorHandle release];
  32. }
  33. static void showInWindow (PlatformSpecificHandle* handle, ComponentPeer*)
  34. {
  35. auto c = [&]
  36. {
  37. if (handle == nullptr || handle->cursorHandle == nullptr)
  38. return [NSCursor arrowCursor];
  39. return handle->cursorHandle;
  40. }();
  41. [c set];
  42. }
  43. private:
  44. static NSCursor* fromNSImage (NSImage* im, NSPoint hotspot)
  45. {
  46. NSCursor* c = [[NSCursor alloc] initWithImage: im
  47. hotSpot: hotspot];
  48. [im release];
  49. return c;
  50. }
  51. static NSCursor* fromHIServices (const char* filename)
  52. {
  53. JUCE_AUTORELEASEPOOL
  54. {
  55. auto cursorPath = String ("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/"
  56. "HIServices.framework/Versions/A/Resources/cursors/")
  57. + filename;
  58. NSImage* originalImage = [[NSImage alloc] initByReferencingFile: juceStringToNS (cursorPath + "/cursor.pdf")];
  59. NSSize originalSize = [originalImage size];
  60. NSImage* resultImage = [[NSImage alloc] initWithSize: originalSize];
  61. for (int scale = 1; scale <= 4; ++scale)
  62. {
  63. NSAffineTransform* scaleTransform = [NSAffineTransform transform];
  64. [scaleTransform scaleBy: (float) scale];
  65. if (CGImageRef rasterCGImage = [originalImage CGImageForProposedRect: nil
  66. context: nil
  67. hints: [NSDictionary dictionaryWithObjectsAndKeys:
  68. NSImageHintCTM, scaleTransform, nil]])
  69. {
  70. NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: rasterCGImage];
  71. [imageRep setSize: originalSize];
  72. [resultImage addRepresentation: imageRep];
  73. [imageRep release];
  74. }
  75. else
  76. {
  77. return nil;
  78. }
  79. }
  80. [originalImage release];
  81. NSDictionary* info = [NSDictionary dictionaryWithContentsOfFile: juceStringToNS (cursorPath + "/info.plist")];
  82. auto hotspotX = (float) [[info valueForKey: nsStringLiteral ("hotx")] doubleValue];
  83. auto hotspotY = (float) [[info valueForKey: nsStringLiteral ("hoty")] doubleValue];
  84. return fromNSImage (resultImage, NSMakePoint (hotspotX, hotspotY));
  85. }
  86. }
  87. static NSCursor* createCursor (const detail::CustomMouseCursorInfo& info)
  88. {
  89. return fromNSImage (imageToNSImage (info.image),
  90. NSMakePoint (info.hotspot.x, info.hotspot.y));
  91. }
  92. static NSCursor* createCursor (const MouseCursor::StandardCursorType type)
  93. {
  94. JUCE_AUTORELEASEPOOL
  95. {
  96. NSCursor* c = nil;
  97. switch (type)
  98. {
  99. case NormalCursor:
  100. case ParentCursor: c = [NSCursor arrowCursor]; break;
  101. case NoCursor: return createCursor ({ ScaledImage (Image (Image::ARGB, 8, 8, true)), {} });
  102. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  103. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  104. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  105. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  106. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  107. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  108. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  109. case CopyingCursor:
  110. {
  111. c = [NSCursor dragCopyCursor];
  112. break;
  113. }
  114. case UpDownResizeCursor:
  115. case TopEdgeResizeCursor:
  116. case BottomEdgeResizeCursor:
  117. if (NSCursor* m = fromHIServices ("resizenorthsouth"))
  118. return m;
  119. c = [NSCursor resizeUpDownCursor];
  120. break;
  121. case LeftRightResizeCursor:
  122. if (NSCursor* m = fromHIServices ("resizeeastwest"))
  123. return m;
  124. c = [NSCursor resizeLeftRightCursor];
  125. break;
  126. case TopLeftCornerResizeCursor:
  127. case BottomRightCornerResizeCursor:
  128. return fromHIServices ("resizenorthwestsoutheast");
  129. case TopRightCornerResizeCursor:
  130. case BottomLeftCornerResizeCursor:
  131. return fromHIServices ("resizenortheastsouthwest");
  132. case UpDownLeftRightResizeCursor:
  133. return fromHIServices ("move");
  134. case NumStandardCursorTypes:
  135. default:
  136. jassertfalse;
  137. break;
  138. }
  139. [c retain];
  140. return c;
  141. }
  142. }
  143. NSCursor* cursorHandle;
  144. };
  145. #else
  146. class MouseCursor::PlatformSpecificHandle
  147. {
  148. public:
  149. PlatformSpecificHandle (const MouseCursor::StandardCursorType) {}
  150. PlatformSpecificHandle (const detail::CustomMouseCursorInfo&) {}
  151. static void showInWindow (PlatformSpecificHandle*, ComponentPeer*) {}
  152. };
  153. #endif
  154. } // namespace juce