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.

210 lines
8.0KB

  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. #if JUCE_MAC
  20. //==============================================================================
  21. namespace MouseCursorHelpers
  22. {
  23. NSImage* createNSImage (const Image&, float scaleFactor = 1.f);
  24. NSImage* createNSImage (const Image& image, float scaleFactor)
  25. {
  26. JUCE_AUTORELEASEPOOL
  27. {
  28. NSImage* im = [[NSImage alloc] init];
  29. const NSSize requiredSize = NSMakeSize (image.getWidth() / scaleFactor, image.getHeight() / scaleFactor);
  30. [im setSize: requiredSize];
  31. CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
  32. CGImageRef imageRef = juce_createCoreGraphicsImage (image, colourSpace, true);
  33. CGColorSpaceRelease (colourSpace);
  34. NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
  35. [imageRep setSize: requiredSize];
  36. [im addRepresentation: imageRep];
  37. [imageRep release];
  38. CGImageRelease (imageRef);
  39. return im;
  40. }
  41. }
  42. static NSCursor* fromNSImage (NSImage* im, NSPoint hotspot)
  43. {
  44. NSCursor* c = [[NSCursor alloc] initWithImage: im
  45. hotSpot: hotspot];
  46. [im release];
  47. return c;
  48. }
  49. static void* fromHIServices (const char* filename)
  50. {
  51. JUCE_AUTORELEASEPOOL
  52. {
  53. const String cursorPath (String ("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/"
  54. "HIServices.framework/Versions/A/Resources/cursors/")
  55. + filename);
  56. NSImage* originalImage = [[NSImage alloc] initByReferencingFile: juceStringToNS (cursorPath + "/cursor.pdf")];
  57. NSSize originalSize = [originalImage size];
  58. NSImage* resultImage = [[NSImage alloc] initWithSize: originalSize];
  59. for (int scale = 1; scale <= 4; ++scale)
  60. {
  61. NSAffineTransform* scaleTransform = [NSAffineTransform transform];
  62. [scaleTransform scaleBy: (float) scale];
  63. if (CGImageRef rasterCGImage = [originalImage CGImageForProposedRect: nil
  64. context: nil
  65. hints: [NSDictionary dictionaryWithObjectsAndKeys:
  66. NSImageHintCTM, scaleTransform, nil]])
  67. {
  68. NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: rasterCGImage];
  69. [imageRep setSize: originalSize];
  70. [resultImage addRepresentation: imageRep];
  71. [imageRep release];
  72. }
  73. else
  74. {
  75. return nil;
  76. }
  77. }
  78. [originalImage release];
  79. NSDictionary* info = [NSDictionary dictionaryWithContentsOfFile: juceStringToNS (cursorPath + "/info.plist")];
  80. const float hotspotX = (float) [[info valueForKey: nsStringLiteral ("hotx")] doubleValue];
  81. const float hotspotY = (float) [[info valueForKey: nsStringLiteral ("hoty")] doubleValue];
  82. return fromNSImage (resultImage, NSMakePoint (hotspotX, hotspotY));
  83. }
  84. }
  85. }
  86. void* CustomMouseCursorInfo::create() const
  87. {
  88. return MouseCursorHelpers::fromNSImage (MouseCursorHelpers::createNSImage (image, scaleFactor),
  89. NSMakePoint (hotspot.x, hotspot.y));
  90. }
  91. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  92. {
  93. JUCE_AUTORELEASEPOOL
  94. {
  95. NSCursor* c = nil;
  96. switch (type)
  97. {
  98. case NormalCursor:
  99. case ParentCursor: c = [NSCursor arrowCursor]; break;
  100. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 8, 8, true), 0, 0).create();
  101. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  102. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  103. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  104. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  105. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  106. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  107. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  108. case CopyingCursor:
  109. {
  110. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
  111. if (void* m = MouseCursorHelpers::fromHIServices ("copy"))
  112. return m;
  113. #endif
  114. c = [NSCursor dragCopyCursor]; // added in 10.6
  115. break;
  116. }
  117. case UpDownResizeCursor:
  118. case TopEdgeResizeCursor:
  119. case BottomEdgeResizeCursor:
  120. if (void* m = MouseCursorHelpers::fromHIServices ("resizenorthsouth"))
  121. return m;
  122. c = [NSCursor resizeUpDownCursor];
  123. break;
  124. case LeftRightResizeCursor:
  125. if (void* m = MouseCursorHelpers::fromHIServices ("resizeeastwest"))
  126. return m;
  127. c = [NSCursor resizeLeftRightCursor];
  128. break;
  129. case TopLeftCornerResizeCursor:
  130. case BottomRightCornerResizeCursor:
  131. return MouseCursorHelpers::fromHIServices ("resizenorthwestsoutheast");
  132. case TopRightCornerResizeCursor:
  133. case BottomLeftCornerResizeCursor:
  134. return MouseCursorHelpers::fromHIServices ("resizenortheastsouthwest");
  135. case UpDownLeftRightResizeCursor:
  136. return MouseCursorHelpers::fromHIServices ("move");
  137. default:
  138. jassertfalse;
  139. break;
  140. }
  141. [c retain];
  142. return c;
  143. }
  144. }
  145. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  146. {
  147. [((NSCursor*) cursorHandle) release];
  148. }
  149. void MouseCursor::showInAllWindows() const
  150. {
  151. showInWindow (nullptr);
  152. }
  153. void MouseCursor::showInWindow (ComponentPeer*) const
  154. {
  155. NSCursor* c = (NSCursor*) getHandle();
  156. if (c == nil)
  157. c = [NSCursor arrowCursor];
  158. [c set];
  159. }
  160. #else
  161. void* CustomMouseCursorInfo::create() const { return nullptr; }
  162. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType) { return nullptr; }
  163. void MouseCursor::deleteMouseCursor (void*, bool) {}
  164. void MouseCursor::showInAllWindows() const {}
  165. void MouseCursor::showInWindow (ComponentPeer*) const {}
  166. #endif