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.

193 lines
7.1KB

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