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.

207 lines
7.8KB

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