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) 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. c = [NSCursor dragCopyCursor];
  94. break;
  95. }
  96. case UpDownResizeCursor:
  97. case TopEdgeResizeCursor:
  98. case BottomEdgeResizeCursor:
  99. if (void* m = MouseCursorHelpers::fromHIServices ("resizenorthsouth"))
  100. return m;
  101. c = [NSCursor resizeUpDownCursor];
  102. break;
  103. case LeftRightResizeCursor:
  104. if (void* m = MouseCursorHelpers::fromHIServices ("resizeeastwest"))
  105. return m;
  106. c = [NSCursor resizeLeftRightCursor];
  107. break;
  108. case TopLeftCornerResizeCursor:
  109. case BottomRightCornerResizeCursor:
  110. return MouseCursorHelpers::fromHIServices ("resizenorthwestsoutheast");
  111. case TopRightCornerResizeCursor:
  112. case BottomLeftCornerResizeCursor:
  113. return MouseCursorHelpers::fromHIServices ("resizenortheastsouthwest");
  114. case UpDownLeftRightResizeCursor:
  115. return MouseCursorHelpers::fromHIServices ("move");
  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