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.

155 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #if JUCE_MAC
  19. //==============================================================================
  20. namespace MouseCursorHelpers
  21. {
  22. static NSImage* createNSImage (const Image& image)
  23. {
  24. JUCE_AUTORELEASEPOOL
  25. NSImage* im = [[NSImage alloc] init];
  26. [im setSize: NSMakeSize (image.getWidth(), image.getHeight())];
  27. [im lockFocus];
  28. CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
  29. CGImageRef imageRef = juce_createCoreGraphicsImage (image, false, colourSpace, false);
  30. CGColorSpaceRelease (colourSpace);
  31. CGContextRef cg = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
  32. CGContextDrawImage (cg, CGRectMake (0, 0, image.getWidth(), image.getHeight()), imageRef);
  33. CGImageRelease (imageRef);
  34. [im unlockFocus];
  35. return im;
  36. }
  37. static void* createFromImage (const Image& image, float hotspotX, float hotspotY)
  38. {
  39. NSImage* im = createNSImage (image);
  40. NSCursor* c = [[NSCursor alloc] initWithImage: im
  41. hotSpot: NSMakePoint (hotspotX, hotspotY)];
  42. [im release];
  43. return c;
  44. }
  45. static void* fromWebKitFile (const char* filename, float hx, float hy)
  46. {
  47. FileInputStream fileStream (String ("/System/Library/Frameworks/WebKit.framework/Frameworks/WebCore.framework/Resources/") + filename);
  48. BufferedInputStream buf (fileStream, 4096);
  49. PNGImageFormat pngFormat;
  50. Image im (pngFormat.decodeImage (buf));
  51. if (im.isValid())
  52. return createFromImage (im, hx * im.getWidth(), hy * im.getHeight());
  53. jassertfalse;
  54. return nullptr;
  55. }
  56. }
  57. void* MouseCursor::createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY)
  58. {
  59. return MouseCursorHelpers::createFromImage (image, (float) hotspotX, (float) hotspotY);
  60. }
  61. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type)
  62. {
  63. JUCE_AUTORELEASEPOOL
  64. NSCursor* c = nil;
  65. switch (type)
  66. {
  67. case NormalCursor: c = [NSCursor arrowCursor]; break;
  68. case NoCursor: return createMouseCursorFromImage (Image (Image::ARGB, 8, 8, true), 0, 0);
  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: return MouseCursorHelpers::fromWebKitFile ("copyCursor.png", 0, 0);
  78. case UpDownResizeCursor:
  79. case TopEdgeResizeCursor:
  80. case BottomEdgeResizeCursor:
  81. return MouseCursorHelpers::fromWebKitFile ("northSouthResizeCursor.png", 0.5f, 0.5f);
  82. case TopLeftCornerResizeCursor:
  83. case BottomRightCornerResizeCursor:
  84. return MouseCursorHelpers::fromWebKitFile ("northWestSouthEastResizeCursor.png", 0.5f, 0.5f);
  85. case TopRightCornerResizeCursor:
  86. case BottomLeftCornerResizeCursor:
  87. return MouseCursorHelpers::fromWebKitFile ("northEastSouthWestResizeCursor.png", 0.5f, 0.5f);
  88. case UpDownLeftRightResizeCursor:
  89. return MouseCursorHelpers::fromWebKitFile ("moveCursor.png", 0.5f, 0.5f);
  90. default:
  91. jassertfalse;
  92. break;
  93. }
  94. [c retain];
  95. return c;
  96. }
  97. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool /*isStandard*/)
  98. {
  99. [((NSCursor*) cursorHandle) release];
  100. }
  101. void MouseCursor::showInAllWindows() const
  102. {
  103. showInWindow (nullptr);
  104. }
  105. void MouseCursor::showInWindow (ComponentPeer*) const
  106. {
  107. NSCursor* c = (NSCursor*) getHandle();
  108. if (c == nil)
  109. c = [NSCursor arrowCursor];
  110. [c set];
  111. }
  112. #else
  113. void* MouseCursor::createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY) { return nullptr; }
  114. void* MouseCursor::createStandardMouseCursor (MouseCursor::StandardCursorType type) { return nullptr; }
  115. void MouseCursor::deleteMouseCursor (void* const cursorHandle, const bool isStandard) {}
  116. void MouseCursor::showInAllWindows() const {}
  117. void MouseCursor::showInWindow (ComponentPeer*) const {}
  118. #endif