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.

182 lines
6.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. namespace MouseCursorHelpers
  23. {
  24. static NSCursor* fromNSImage (NSImage* im, NSPoint hotspot)
  25. {
  26. NSCursor* c = [[NSCursor alloc] initWithImage: im
  27. hotSpot: hotspot];
  28. [im release];
  29. return c;
  30. }
  31. static void* fromHIServices (const char* filename)
  32. {
  33. JUCE_AUTORELEASEPOOL
  34. {
  35. auto cursorPath = String ("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/"
  36. "HIServices.framework/Versions/A/Resources/cursors/")
  37. + filename;
  38. NSImage* originalImage = [[NSImage alloc] initByReferencingFile: juceStringToNS (cursorPath + "/cursor.pdf")];
  39. NSSize originalSize = [originalImage size];
  40. NSImage* resultImage = [[NSImage alloc] initWithSize: originalSize];
  41. for (int scale = 1; scale <= 4; ++scale)
  42. {
  43. NSAffineTransform* scaleTransform = [NSAffineTransform transform];
  44. [scaleTransform scaleBy: (float) scale];
  45. if (CGImageRef rasterCGImage = [originalImage CGImageForProposedRect: nil
  46. context: nil
  47. hints: [NSDictionary dictionaryWithObjectsAndKeys:
  48. NSImageHintCTM, scaleTransform, nil]])
  49. {
  50. NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: rasterCGImage];
  51. [imageRep setSize: originalSize];
  52. [resultImage addRepresentation: imageRep];
  53. [imageRep release];
  54. }
  55. else
  56. {
  57. return nil;
  58. }
  59. }
  60. [originalImage release];
  61. NSDictionary* info = [NSDictionary dictionaryWithContentsOfFile: juceStringToNS (cursorPath + "/info.plist")];
  62. auto hotspotX = (float) [[info valueForKey: nsStringLiteral ("hotx")] doubleValue];
  63. auto hotspotY = (float) [[info valueForKey: nsStringLiteral ("hoty")] doubleValue];
  64. return fromNSImage (resultImage, NSMakePoint (hotspotX, hotspotY));
  65. }
  66. }
  67. }
  68. void* CustomMouseCursorInfo::create() const
  69. {
  70. return MouseCursorHelpers::fromNSImage (imageToNSImage (image, scaleFactor),
  71. NSMakePoint (hotspot.x, hotspot.y));
  72. }
  73. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  74. {
  75. JUCE_AUTORELEASEPOOL
  76. {
  77. NSCursor* c = nil;
  78. switch (type)
  79. {
  80. case NormalCursor:
  81. case ParentCursor: c = [NSCursor arrowCursor]; break;
  82. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 8, 8, true), {}).create();
  83. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  84. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  85. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  86. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  87. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  88. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  89. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  90. case CopyingCursor:
  91. {
  92. c = [NSCursor dragCopyCursor];
  93. break;
  94. }
  95. case UpDownResizeCursor:
  96. case TopEdgeResizeCursor:
  97. case BottomEdgeResizeCursor:
  98. if (void* m = MouseCursorHelpers::fromHIServices ("resizenorthsouth"))
  99. return m;
  100. c = [NSCursor resizeUpDownCursor];
  101. break;
  102. case LeftRightResizeCursor:
  103. if (void* m = MouseCursorHelpers::fromHIServices ("resizeeastwest"))
  104. return m;
  105. c = [NSCursor resizeLeftRightCursor];
  106. break;
  107. case TopLeftCornerResizeCursor:
  108. case BottomRightCornerResizeCursor:
  109. return MouseCursorHelpers::fromHIServices ("resizenorthwestsoutheast");
  110. case TopRightCornerResizeCursor:
  111. case BottomLeftCornerResizeCursor:
  112. return MouseCursorHelpers::fromHIServices ("resizenortheastsouthwest");
  113. case UpDownLeftRightResizeCursor:
  114. return MouseCursorHelpers::fromHIServices ("move");
  115. case NumStandardCursorTypes:
  116. default:
  117. jassertfalse;
  118. break;
  119. }
  120. [c retain];
  121. return c;
  122. }
  123. }
  124. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  125. {
  126. [((NSCursor*) cursorHandle) release];
  127. }
  128. void MouseCursor::showInWindow (ComponentPeer*) const
  129. {
  130. auto c = (NSCursor*) getHandle();
  131. if (c == nil)
  132. c = [NSCursor arrowCursor];
  133. [c set];
  134. }
  135. #else
  136. void* CustomMouseCursorInfo::create() const { return nullptr; }
  137. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType) { return nullptr; }
  138. void MouseCursor::deleteMouseCursor (void*, bool) {}
  139. void MouseCursor::showInWindow (ComponentPeer*) const {}
  140. #endif
  141. } // namespace juce