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.

163 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 void* fromWebKitFile (const char* filename, float hx, float hy)
  40. {
  41. FileInputStream fileStream (File ("/System/Library/Frameworks/WebKit.framework/Frameworks/WebCore.framework/Resources").getChildFile (filename));
  42. BufferedInputStream buf (fileStream, 4096);
  43. PNGImageFormat pngFormat;
  44. Image im (pngFormat.decodeImage (buf));
  45. if (im.isValid())
  46. return CustomMouseCursorInfo (im, (int) (hx * im.getWidth()),
  47. (int) (hy * im.getHeight())).create();
  48. return nullptr;
  49. }
  50. }
  51. void* CustomMouseCursorInfo::create() const
  52. {
  53. NSImage* im = MouseCursorHelpers::createNSImage (image);
  54. NSCursor* c = [[NSCursor alloc] initWithImage: im
  55. hotSpot: NSMakePoint (hotspot.x, hotspot.y)];
  56. [im release];
  57. return c;
  58. }
  59. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  60. {
  61. JUCE_AUTORELEASEPOOL
  62. {
  63. NSCursor* c = nil;
  64. switch (type)
  65. {
  66. case NormalCursor:
  67. case ParentCursor: c = [NSCursor arrowCursor]; break;
  68. case NoCursor: return CustomMouseCursorInfo (Image (Image::ARGB, 8, 8, true), 0, 0).create();
  69. case DraggingHandCursor: c = [NSCursor openHandCursor]; break;
  70. case WaitCursor: c = [NSCursor arrowCursor]; break; // avoid this on the mac, let the OS provide the beachball
  71. case IBeamCursor: c = [NSCursor IBeamCursor]; break;
  72. case PointingHandCursor: c = [NSCursor pointingHandCursor]; break;
  73. case LeftRightResizeCursor: c = [NSCursor resizeLeftRightCursor]; break;
  74. case LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  75. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  76. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  77. case CopyingCursor:
  78. {
  79. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
  80. if (void* m = MouseCursorHelpers::fromWebKitFile ("copyCursor.png", 0, 0))
  81. return m;
  82. #endif
  83. c = [NSCursor dragCopyCursor]; // added in 10.6
  84. break;
  85. }
  86. case UpDownResizeCursor:
  87. case TopEdgeResizeCursor:
  88. case BottomEdgeResizeCursor:
  89. return MouseCursorHelpers::fromWebKitFile ("northSouthResizeCursor.png", 0.5f, 0.5f);
  90. case TopLeftCornerResizeCursor:
  91. case BottomRightCornerResizeCursor:
  92. return MouseCursorHelpers::fromWebKitFile ("northWestSouthEastResizeCursor.png", 0.5f, 0.5f);
  93. case TopRightCornerResizeCursor:
  94. case BottomLeftCornerResizeCursor:
  95. return MouseCursorHelpers::fromWebKitFile ("northEastSouthWestResizeCursor.png", 0.5f, 0.5f);
  96. case UpDownLeftRightResizeCursor:
  97. return MouseCursorHelpers::fromWebKitFile ("moveCursor.png", 0.5f, 0.5f);
  98. default:
  99. jassertfalse;
  100. break;
  101. }
  102. [c retain];
  103. return c;
  104. }
  105. }
  106. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  107. {
  108. [((NSCursor*) cursorHandle) release];
  109. }
  110. void MouseCursor::showInAllWindows() const
  111. {
  112. showInWindow (nullptr);
  113. }
  114. void MouseCursor::showInWindow (ComponentPeer*) const
  115. {
  116. NSCursor* c = (NSCursor*) getHandle();
  117. if (c == nil)
  118. c = [NSCursor arrowCursor];
  119. [c set];
  120. }
  121. #else
  122. void* CustomMouseCursorInfo::create() const { return nullptr; }
  123. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType) { return nullptr; }
  124. void MouseCursor::deleteMouseCursor (void*, bool) {}
  125. void MouseCursor::showInAllWindows() const {}
  126. void MouseCursor::showInWindow (ComponentPeer*) const {}
  127. #endif