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.

169 lines
6.2KB

  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 LeftEdgeResizeCursor: c = [NSCursor resizeLeftCursor]; break;
  74. case RightEdgeResizeCursor: c = [NSCursor resizeRightCursor]; break;
  75. case CrosshairCursor: c = [NSCursor crosshairCursor]; break;
  76. case CopyingCursor:
  77. {
  78. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
  79. if (void* m = MouseCursorHelpers::fromWebKitFile ("copyCursor.png", 0, 0))
  80. return m;
  81. #endif
  82. c = [NSCursor dragCopyCursor]; // added in 10.6
  83. break;
  84. }
  85. case UpDownResizeCursor:
  86. case TopEdgeResizeCursor:
  87. case BottomEdgeResizeCursor:
  88. return MouseCursorHelpers::fromWebKitFile ("northSouthResizeCursor.png", 0.5f, 0.5f);
  89. case LeftRightResizeCursor:
  90. if (void* m = MouseCursorHelpers::fromWebKitFile ("eastWestResizeCursor.png", 0.5f, 0.5f))
  91. return m;
  92. c = [NSCursor resizeLeftRightCursor];
  93. break;
  94. case TopLeftCornerResizeCursor:
  95. case BottomRightCornerResizeCursor:
  96. return MouseCursorHelpers::fromWebKitFile ("northWestSouthEastResizeCursor.png", 0.5f, 0.5f);
  97. case TopRightCornerResizeCursor:
  98. case BottomLeftCornerResizeCursor:
  99. return MouseCursorHelpers::fromWebKitFile ("northEastSouthWestResizeCursor.png", 0.5f, 0.5f);
  100. case UpDownLeftRightResizeCursor:
  101. return MouseCursorHelpers::fromWebKitFile ("moveCursor.png", 0.5f, 0.5f);
  102. default:
  103. jassertfalse;
  104. break;
  105. }
  106. [c retain];
  107. return c;
  108. }
  109. }
  110. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  111. {
  112. [((NSCursor*) cursorHandle) release];
  113. }
  114. void MouseCursor::showInAllWindows() const
  115. {
  116. showInWindow (nullptr);
  117. }
  118. void MouseCursor::showInWindow (ComponentPeer*) const
  119. {
  120. NSCursor* c = (NSCursor*) getHandle();
  121. if (c == nil)
  122. c = [NSCursor arrowCursor];
  123. [c set];
  124. }
  125. #else
  126. void* CustomMouseCursorInfo::create() const { return nullptr; }
  127. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType) { return nullptr; }
  128. void MouseCursor::deleteMouseCursor (void*, bool) {}
  129. void MouseCursor::showInAllWindows() const {}
  130. void MouseCursor::showInWindow (ComponentPeer*) const {}
  131. #endif