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.

208 lines
8.0KB

  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. [originalImage release];
  77. NSDictionary* info = [NSDictionary dictionaryWithContentsOfFile: juceStringToNS (cursorPath + "/info.plist")];
  78. const float hotspotX = (float) [[info valueForKey: nsStringLiteral ("hotx")] doubleValue];
  79. const float hotspotY = (float) [[info valueForKey: nsStringLiteral ("hoty")] doubleValue];
  80. return fromNSImage (resultImage, NSMakePoint (hotspotX, hotspotY));
  81. }
  82. }
  83. }
  84. void* CustomMouseCursorInfo::create() const
  85. {
  86. return MouseCursorHelpers::fromNSImage (MouseCursorHelpers::createNSImage (image, scaleFactor),
  87. NSMakePoint (hotspot.x, hotspot.y));
  88. }
  89. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  90. {
  91. JUCE_AUTORELEASEPOOL
  92. {
  93. NSCursor* c = nil;
  94. switch (type)
  95. {
  96. case NormalCursor:
  97. case ParentCursor: c = [NSCursor arrowCursor]; break;
  98. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 8, 8, true), 0, 0).create();
  99. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  100. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  101. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  102. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  103. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  104. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  105. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  106. case CopyingCursor:
  107. {
  108. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
  109. if (void* m = MouseCursorHelpers::fromHIServices ("copy"))
  110. return m;
  111. #endif
  112. c = [NSCursor dragCopyCursor]; // added in 10.6
  113. break;
  114. }
  115. case UpDownResizeCursor:
  116. case TopEdgeResizeCursor:
  117. case BottomEdgeResizeCursor:
  118. if (void* m = MouseCursorHelpers::fromHIServices ("resizenorthsouth"))
  119. return m;
  120. c = [NSCursor resizeUpDownCursor];
  121. break;
  122. case LeftRightResizeCursor:
  123. if (void* m = MouseCursorHelpers::fromHIServices ("resizeeastwest"))
  124. return m;
  125. c = [NSCursor resizeLeftRightCursor];
  126. break;
  127. case TopLeftCornerResizeCursor:
  128. case BottomRightCornerResizeCursor:
  129. return MouseCursorHelpers::fromHIServices ("resizenorthwestsoutheast");
  130. case TopRightCornerResizeCursor:
  131. case BottomLeftCornerResizeCursor:
  132. return MouseCursorHelpers::fromHIServices ("resizenortheastsouthwest");
  133. case UpDownLeftRightResizeCursor:
  134. return MouseCursorHelpers::fromHIServices ("move");
  135. default:
  136. jassertfalse;
  137. break;
  138. }
  139. [c retain];
  140. return c;
  141. }
  142. }
  143. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  144. {
  145. [((NSCursor*) cursorHandle) release];
  146. }
  147. void MouseCursor::showInAllWindows() const
  148. {
  149. showInWindow (nullptr);
  150. }
  151. void MouseCursor::showInWindow (ComponentPeer*) const
  152. {
  153. NSCursor* c = (NSCursor*) getHandle();
  154. if (c == nil)
  155. c = [NSCursor arrowCursor];
  156. [c set];
  157. }
  158. #else
  159. void* CustomMouseCursorInfo::create() const { return nullptr; }
  160. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType) { return nullptr; }
  161. void MouseCursor::deleteMouseCursor (void*, bool) {}
  162. void MouseCursor::showInAllWindows() const {}
  163. void MouseCursor::showInWindow (ComponentPeer*) const {}
  164. #endif