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.

174 lines
6.3KB

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