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.

215 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. namespace juce
  20. {
  21. #if JUCE_MAC
  22. //==============================================================================
  23. namespace MouseCursorHelpers
  24. {
  25. NSImage* createNSImage (const Image&, float scaleFactor = 1.0f);
  26. NSImage* createNSImage (const Image& image, float scaleFactor)
  27. {
  28. JUCE_AUTORELEASEPOOL
  29. {
  30. NSImage* im = [[NSImage alloc] init];
  31. auto requiredSize = NSMakeSize (image.getWidth() / scaleFactor, image.getHeight() / scaleFactor);
  32. [im setSize: requiredSize];
  33. CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
  34. CGImageRef imageRef = juce_createCoreGraphicsImage (image, colourSpace, true);
  35. CGColorSpaceRelease (colourSpace);
  36. NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
  37. [imageRep setSize: requiredSize];
  38. [im addRepresentation: imageRep];
  39. [imageRep release];
  40. CGImageRelease (imageRef);
  41. return im;
  42. }
  43. }
  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 void* 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. }
  88. void* CustomMouseCursorInfo::create() const
  89. {
  90. return MouseCursorHelpers::fromNSImage (MouseCursorHelpers::createNSImage (image, scaleFactor),
  91. NSMakePoint (hotspot.x, hotspot.y));
  92. }
  93. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  94. {
  95. JUCE_AUTORELEASEPOOL
  96. {
  97. NSCursor* c = nil;
  98. switch (type)
  99. {
  100. case NormalCursor:
  101. case ParentCursor: c = [NSCursor arrowCursor]; break;
  102. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 8, 8, true), {}).create();
  103. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  104. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  105. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  106. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  107. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  108. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  109. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  110. case CopyingCursor:
  111. {
  112. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
  113. if (void* m = MouseCursorHelpers::fromHIServices ("copy"))
  114. return m;
  115. #endif
  116. c = [NSCursor dragCopyCursor]; // added in 10.6
  117. break;
  118. }
  119. case UpDownResizeCursor:
  120. case TopEdgeResizeCursor:
  121. case BottomEdgeResizeCursor:
  122. if (void* m = MouseCursorHelpers::fromHIServices ("resizenorthsouth"))
  123. return m;
  124. c = [NSCursor resizeUpDownCursor];
  125. break;
  126. case LeftRightResizeCursor:
  127. if (void* m = MouseCursorHelpers::fromHIServices ("resizeeastwest"))
  128. return m;
  129. c = [NSCursor resizeLeftRightCursor];
  130. break;
  131. case TopLeftCornerResizeCursor:
  132. case BottomRightCornerResizeCursor:
  133. return MouseCursorHelpers::fromHIServices ("resizenorthwestsoutheast");
  134. case TopRightCornerResizeCursor:
  135. case BottomLeftCornerResizeCursor:
  136. return MouseCursorHelpers::fromHIServices ("resizenortheastsouthwest");
  137. case UpDownLeftRightResizeCursor:
  138. return MouseCursorHelpers::fromHIServices ("move");
  139. default:
  140. jassertfalse;
  141. break;
  142. }
  143. [c retain];
  144. return c;
  145. }
  146. }
  147. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  148. {
  149. [((NSCursor*) cursorHandle) release];
  150. }
  151. void MouseCursor::showInAllWindows() const
  152. {
  153. showInWindow (nullptr);
  154. }
  155. void MouseCursor::showInWindow (ComponentPeer*) const
  156. {
  157. auto c = (NSCursor*) getHandle();
  158. if (c == nil)
  159. c = [NSCursor arrowCursor];
  160. [c set];
  161. }
  162. #else
  163. void* CustomMouseCursorInfo::create() const { return nullptr; }
  164. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType) { return nullptr; }
  165. void MouseCursor::deleteMouseCursor (void*, bool) {}
  166. void MouseCursor::showInAllWindows() const {}
  167. void MouseCursor::showInWindow (ComponentPeer*) const {}
  168. #endif
  169. } // namespace juce