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.

206 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_MAC
  18. //==============================================================================
  19. namespace MouseCursorHelpers
  20. {
  21. NSImage* createNSImage (const Image&, float scaleFactor = 1.f);
  22. NSImage* createNSImage (const Image& image, float scaleFactor)
  23. {
  24. JUCE_AUTORELEASEPOOL
  25. {
  26. NSImage* im = [[NSImage alloc] init];
  27. const NSSize requiredSize = NSMakeSize (image.getWidth() / scaleFactor, image.getHeight() / scaleFactor);
  28. [im setSize: requiredSize];
  29. CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
  30. CGImageRef imageRef = juce_createCoreGraphicsImage (image, colourSpace, true);
  31. CGColorSpaceRelease (colourSpace);
  32. NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
  33. [imageRep setSize: requiredSize];
  34. [im addRepresentation: imageRep];
  35. [imageRep release];
  36. CGImageRelease (imageRef);
  37. return im;
  38. }
  39. }
  40. static NSCursor* fromNSImage (NSImage* im, NSPoint hotspot)
  41. {
  42. NSCursor* c = [[NSCursor alloc] initWithImage: im
  43. hotSpot: hotspot];
  44. [im release];
  45. return c;
  46. }
  47. static void* fromHIServices (const char* filename)
  48. {
  49. JUCE_AUTORELEASEPOOL
  50. {
  51. const String cursorPath (String ("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/"
  52. "HIServices.framework/Versions/A/Resources/cursors/")
  53. + filename);
  54. NSImage* originalImage = [[NSImage alloc] initByReferencingFile: juceStringToNS (cursorPath + "/cursor.pdf")];
  55. NSSize originalSize = [originalImage size];
  56. NSImage* resultImage = [[NSImage alloc] initWithSize: originalSize];
  57. for (int scale = 1; scale <= 4; ++scale)
  58. {
  59. NSAffineTransform* scaleTransform = [NSAffineTransform transform];
  60. [scaleTransform scaleBy: (float) scale];
  61. if (CGImageRef rasterCGImage = [originalImage CGImageForProposedRect: nil
  62. context: nil
  63. hints: [NSDictionary dictionaryWithObjectsAndKeys:
  64. NSImageHintCTM, scaleTransform, nil]])
  65. {
  66. NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: rasterCGImage];
  67. [imageRep setSize: originalSize];
  68. [resultImage addRepresentation: imageRep];
  69. [imageRep release];
  70. }
  71. else
  72. {
  73. return nil;
  74. }
  75. }
  76. NSDictionary* info = [NSDictionary dictionaryWithContentsOfFile: juceStringToNS (cursorPath + "/info.plist")];
  77. const float hotspotX = (float) [[info valueForKey: nsStringLiteral ("hotx")] doubleValue];
  78. const float hotspotY = (float) [[info valueForKey: nsStringLiteral ("hoty")] doubleValue];
  79. return fromNSImage (resultImage, NSMakePoint (hotspotX, hotspotY));
  80. }
  81. }
  82. }
  83. void* CustomMouseCursorInfo::create() const
  84. {
  85. return MouseCursorHelpers::fromNSImage (MouseCursorHelpers::createNSImage (image, scaleFactor),
  86. NSMakePoint (hotspot.x, hotspot.y));
  87. }
  88. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  89. {
  90. JUCE_AUTORELEASEPOOL
  91. {
  92. NSCursor* c = nil;
  93. switch (type)
  94. {
  95. case NormalCursor:
  96. case ParentCursor: c = [NSCursor arrowCursor]; break;
  97. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 8, 8, true), 0, 0).create();
  98. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  99. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  100. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  101. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  102. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  103. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  104. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  105. case CopyingCursor:
  106. {
  107. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
  108. if (void* m = MouseCursorHelpers::fromHIServices ("copy"))
  109. return m;
  110. #endif
  111. c = [NSCursor dragCopyCursor]; // added in 10.6
  112. break;
  113. }
  114. case UpDownResizeCursor:
  115. case TopEdgeResizeCursor:
  116. case BottomEdgeResizeCursor:
  117. if (void* m = MouseCursorHelpers::fromHIServices ("resizenorthsouth"))
  118. return m;
  119. c = [NSCursor resizeUpDownCursor];
  120. break;
  121. case LeftRightResizeCursor:
  122. if (void* m = MouseCursorHelpers::fromHIServices ("resizeeastwest"))
  123. return m;
  124. c = [NSCursor resizeLeftRightCursor];
  125. break;
  126. case TopLeftCornerResizeCursor:
  127. case BottomRightCornerResizeCursor:
  128. return MouseCursorHelpers::fromHIServices ("resizenorthwestsoutheast");
  129. case TopRightCornerResizeCursor:
  130. case BottomLeftCornerResizeCursor:
  131. return MouseCursorHelpers::fromHIServices ("resizenortheastsouthwest");
  132. case UpDownLeftRightResizeCursor:
  133. return MouseCursorHelpers::fromHIServices ("move");
  134. default:
  135. jassertfalse;
  136. break;
  137. }
  138. [c retain];
  139. return c;
  140. }
  141. }
  142. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  143. {
  144. [((NSCursor*) cursorHandle) release];
  145. }
  146. void MouseCursor::showInAllWindows() const
  147. {
  148. showInWindow (nullptr);
  149. }
  150. void MouseCursor::showInWindow (ComponentPeer*) const
  151. {
  152. NSCursor* c = (NSCursor*) getHandle();
  153. if (c == nil)
  154. c = [NSCursor arrowCursor];
  155. [c set];
  156. }
  157. #else
  158. void* CustomMouseCursorInfo::create() const { return nullptr; }
  159. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType) { return nullptr; }
  160. void MouseCursor::deleteMouseCursor (void*, bool) {}
  161. void MouseCursor::showInAllWindows() const {}
  162. void MouseCursor::showInWindow (ComponentPeer*) const {}
  163. #endif