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.

1981 lines
73KB

  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. typedef void (*AppFocusChangeCallback)();
  18. extern AppFocusChangeCallback appFocusChangeCallback;
  19. typedef bool (*CheckEventBlockedByModalComps) (NSEvent*);
  20. extern CheckEventBlockedByModalComps isEventBlockedByModalComps;
  21. //==============================================================================
  22. #if ! (defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7)
  23. } // (juce namespace)
  24. @interface NSEvent (JuceDeviceDelta)
  25. - (CGFloat) scrollingDeltaX;
  26. - (CGFloat) scrollingDeltaY;
  27. - (BOOL) hasPreciseScrollingDeltas;
  28. - (BOOL) isDirectionInvertedFromDevice;
  29. @end
  30. namespace juce {
  31. #endif
  32. //==============================================================================
  33. class NSViewComponentPeer : public ComponentPeer
  34. {
  35. public:
  36. NSViewComponentPeer (Component& comp, const int windowStyleFlags, NSView* viewToAttachTo)
  37. : ComponentPeer (comp, windowStyleFlags),
  38. window (nil),
  39. view (nil),
  40. isSharedWindow (viewToAttachTo != nil),
  41. fullScreen (false),
  42. insideDrawRect (false),
  43. #if USE_COREGRAPHICS_RENDERING
  44. usingCoreGraphics (true),
  45. #else
  46. usingCoreGraphics (false),
  47. #endif
  48. isZooming (false),
  49. textWasInserted (false),
  50. notificationCenter (nil)
  51. {
  52. appFocusChangeCallback = appFocusChanged;
  53. isEventBlockedByModalComps = checkEventBlockedByModalComps;
  54. NSRect r = NSMakeRect (0, 0, (CGFloat) component.getWidth(), (CGFloat) component.getHeight());
  55. view = [createViewInstance() initWithFrame: r];
  56. setOwner (view, this);
  57. [view registerForDraggedTypes: getSupportedDragTypes()];
  58. notificationCenter = [NSNotificationCenter defaultCenter];
  59. [notificationCenter addObserver: view
  60. selector: @selector (frameChanged:)
  61. name: NSViewFrameDidChangeNotification
  62. object: view];
  63. if (! isSharedWindow)
  64. {
  65. [notificationCenter addObserver: view
  66. selector: @selector (frameChanged:)
  67. name: NSWindowDidMoveNotification
  68. object: window];
  69. [notificationCenter addObserver: view
  70. selector: @selector (frameChanged:)
  71. name: NSWindowDidMiniaturizeNotification
  72. object: window];
  73. [notificationCenter addObserver: view
  74. selector: @selector (frameChanged:)
  75. name: NSWindowDidDeminiaturizeNotification
  76. object: window];
  77. }
  78. [view setPostsFrameChangedNotifications: YES];
  79. if (isSharedWindow)
  80. {
  81. window = [viewToAttachTo window];
  82. [viewToAttachTo addSubview: view];
  83. }
  84. else
  85. {
  86. r.origin.x = (CGFloat) component.getX();
  87. r.origin.y = (CGFloat) component.getY();
  88. r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - (r.origin.y + r.size.height);
  89. window = [createWindowInstance() initWithContentRect: r
  90. styleMask: getNSWindowStyleMask (windowStyleFlags)
  91. backing: NSBackingStoreBuffered
  92. defer: YES];
  93. setOwner (window, this);
  94. [window orderOut: nil];
  95. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  96. [window setDelegate: (id<NSWindowDelegate>) window];
  97. #else
  98. [window setDelegate: window];
  99. #endif
  100. [window setOpaque: component.isOpaque()];
  101. [window setHasShadow: ((windowStyleFlags & windowHasDropShadow) != 0)];
  102. if (component.isAlwaysOnTop())
  103. [window setLevel: NSFloatingWindowLevel];
  104. [window setContentView: view];
  105. [window setAutodisplay: YES];
  106. [window setAcceptsMouseMovedEvents: YES];
  107. // We'll both retain and also release this on closing because plugin hosts can unexpectedly
  108. // close the window for us, and also tend to get cause trouble if setReleasedWhenClosed is NO.
  109. [window setReleasedWhenClosed: YES];
  110. [window retain];
  111. [window setExcludedFromWindowsMenu: (windowStyleFlags & windowIsTemporary) != 0];
  112. [window setIgnoresMouseEvents: (windowStyleFlags & windowIgnoresMouseClicks) != 0];
  113. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  114. if ((windowStyleFlags & (windowHasMaximiseButton | windowHasTitleBar)) == (windowHasMaximiseButton | windowHasTitleBar))
  115. [window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
  116. if ([window respondsToSelector: @selector (setRestorable:)])
  117. [window setRestorable: NO];
  118. #endif
  119. }
  120. const float alpha = component.getAlpha();
  121. if (alpha < 1.0f)
  122. setAlpha (alpha);
  123. setTitle (component.getName());
  124. }
  125. ~NSViewComponentPeer()
  126. {
  127. [notificationCenter removeObserver: view];
  128. setOwner (view, nullptr);
  129. if ([view superview] != nil)
  130. [view removeFromSuperview];
  131. [view release];
  132. if (! isSharedWindow)
  133. {
  134. setOwner (window, nullptr);
  135. [window close];
  136. [window release];
  137. }
  138. }
  139. //==============================================================================
  140. void* getNativeHandle() const { return view; }
  141. void setVisible (bool shouldBeVisible)
  142. {
  143. if (isSharedWindow)
  144. {
  145. [view setHidden: ! shouldBeVisible];
  146. }
  147. else
  148. {
  149. if (shouldBeVisible)
  150. {
  151. [window orderFront: nil];
  152. handleBroughtToFront();
  153. }
  154. else
  155. {
  156. [window orderOut: nil];
  157. }
  158. }
  159. }
  160. void setTitle (const String& title)
  161. {
  162. JUCE_AUTORELEASEPOOL
  163. {
  164. if (! isSharedWindow)
  165. [window setTitle: juceStringToNS (title)];
  166. }
  167. }
  168. bool setDocumentEditedStatus (bool edited)
  169. {
  170. if (! hasNativeTitleBar())
  171. return false;
  172. [window setDocumentEdited: edited];
  173. return true;
  174. }
  175. void setRepresentedFile (const File& file)
  176. {
  177. if (! isSharedWindow)
  178. [window setRepresentedFilename: juceStringToNS (file != File::nonexistent
  179. ? file.getFullPathName()
  180. : String::empty)];
  181. }
  182. void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen)
  183. {
  184. fullScreen = isNowFullScreen;
  185. NSRect r = NSMakeRect ((CGFloat) newBounds.getX(), (CGFloat) newBounds.getY(),
  186. (CGFloat) jmax (0, newBounds.getWidth()), (CGFloat) jmax (0, newBounds.getHeight()));
  187. if (isSharedWindow)
  188. {
  189. r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);
  190. if ([view frame].size.width != r.size.width
  191. || [view frame].size.height != r.size.height)
  192. {
  193. [view setNeedsDisplay: true];
  194. }
  195. [view setFrame: r];
  196. }
  197. else
  198. {
  199. r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - (r.origin.y + r.size.height);
  200. [window setFrame: [window frameRectForContentRect: r]
  201. display: true];
  202. }
  203. }
  204. Rectangle<int> getBounds (const bool global) const
  205. {
  206. NSRect r = [view frame];
  207. NSWindow* viewWindow = [view window];
  208. if (global && viewWindow != nil)
  209. {
  210. r = [[view superview] convertRect: r toView: nil];
  211. #if defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
  212. r = [viewWindow convertRectToScreen: r];
  213. #else
  214. r.origin = [viewWindow convertBaseToScreen: r.origin];
  215. #endif
  216. r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - r.origin.y - r.size.height;
  217. }
  218. else
  219. {
  220. r.origin.y = [[view superview] frame].size.height - r.origin.y - r.size.height;
  221. }
  222. return Rectangle<int> (convertToRectInt (r));
  223. }
  224. Rectangle<int> getBounds() const
  225. {
  226. return getBounds (! isSharedWindow);
  227. }
  228. Point<int> localToGlobal (const Point<int>& relativePosition)
  229. {
  230. return relativePosition + getBounds (true).getPosition();
  231. }
  232. Point<int> globalToLocal (const Point<int>& screenPosition)
  233. {
  234. return screenPosition - getBounds (true).getPosition();
  235. }
  236. void setAlpha (float newAlpha)
  237. {
  238. if (! isSharedWindow)
  239. {
  240. [window setAlphaValue: (CGFloat) newAlpha];
  241. }
  242. else
  243. {
  244. #if defined (MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  245. [view setAlphaValue: (CGFloat) newAlpha];
  246. #else
  247. if ([view respondsToSelector: @selector (setAlphaValue:)])
  248. {
  249. // PITA dynamic invocation for 10.4 builds..
  250. NSInvocation* inv = [NSInvocation invocationWithMethodSignature: [view methodSignatureForSelector: @selector (setAlphaValue:)]];
  251. [inv setSelector: @selector (setAlphaValue:)];
  252. [inv setTarget: view];
  253. CGFloat cgNewAlpha = (CGFloat) newAlpha;
  254. [inv setArgument: &cgNewAlpha atIndex: 2];
  255. [inv invoke];
  256. }
  257. #endif
  258. }
  259. }
  260. void setMinimised (bool shouldBeMinimised)
  261. {
  262. if (! isSharedWindow)
  263. {
  264. if (shouldBeMinimised)
  265. [window miniaturize: nil];
  266. else
  267. [window deminiaturize: nil];
  268. }
  269. }
  270. bool isMinimised() const
  271. {
  272. return [window isMiniaturized];
  273. }
  274. void setFullScreen (bool shouldBeFullScreen)
  275. {
  276. if (! isSharedWindow)
  277. {
  278. Rectangle<int> r (lastNonFullscreenBounds);
  279. if (isMinimised())
  280. setMinimised (false);
  281. if (fullScreen != shouldBeFullScreen)
  282. {
  283. if (shouldBeFullScreen && hasNativeTitleBar())
  284. {
  285. fullScreen = true;
  286. [window performZoom: nil];
  287. }
  288. else
  289. {
  290. if (shouldBeFullScreen)
  291. r = component.getParentMonitorArea();
  292. // (can't call the component's setBounds method because that'll reset our fullscreen flag)
  293. if (r != component.getBounds() && ! r.isEmpty())
  294. setBounds (r, shouldBeFullScreen);
  295. }
  296. }
  297. }
  298. }
  299. bool isFullScreen() const
  300. {
  301. return fullScreen;
  302. }
  303. bool contains (const Point<int>& position, bool trueIfInAChildWindow) const
  304. {
  305. if (! (isPositiveAndBelow (position.getX(), component.getWidth())
  306. && isPositiveAndBelow (position.getY(), component.getHeight())))
  307. return false;
  308. NSRect frameRect = [view frame];
  309. NSView* v = [view hitTest: NSMakePoint (frameRect.origin.x + position.getX(),
  310. frameRect.origin.y + frameRect.size.height - position.getY())];
  311. return trueIfInAChildWindow ? (v != nil)
  312. : (v == view);
  313. }
  314. BorderSize<int> getFrameSize() const
  315. {
  316. BorderSize<int> b;
  317. if (! isSharedWindow)
  318. {
  319. NSRect v = [view convertRect: [view frame] toView: nil];
  320. NSRect w = [window frame];
  321. b.setTop ((int) (w.size.height - (v.origin.y + v.size.height)));
  322. b.setBottom ((int) v.origin.y);
  323. b.setLeft ((int) v.origin.x);
  324. b.setRight ((int) (w.size.width - (v.origin.x + v.size.width)));
  325. }
  326. return b;
  327. }
  328. void updateFullscreenStatus()
  329. {
  330. if (hasNativeTitleBar())
  331. {
  332. const Rectangle<int> screen (getFrameSize().subtractedFrom (component.getParentMonitorArea()));
  333. fullScreen = component.getScreenBounds().expanded (2, 2).contains (screen);
  334. }
  335. }
  336. bool hasNativeTitleBar() const
  337. {
  338. return (getStyleFlags() & windowHasTitleBar) != 0;
  339. }
  340. bool setAlwaysOnTop (bool alwaysOnTop)
  341. {
  342. if (! isSharedWindow)
  343. [window setLevel: alwaysOnTop ? NSFloatingWindowLevel
  344. : NSNormalWindowLevel];
  345. return true;
  346. }
  347. void toFront (bool makeActiveWindow)
  348. {
  349. if (isSharedWindow)
  350. [[view superview] addSubview: view
  351. positioned: NSWindowAbove
  352. relativeTo: nil];
  353. if (window != nil && component.isVisible())
  354. {
  355. ++insideToFrontCall;
  356. if (makeActiveWindow)
  357. [window makeKeyAndOrderFront: nil];
  358. else
  359. [window orderFront: nil];
  360. if (insideToFrontCall <= 1)
  361. {
  362. Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
  363. handleBroughtToFront();
  364. }
  365. --insideToFrontCall;
  366. }
  367. }
  368. void toBehind (ComponentPeer* other)
  369. {
  370. NSViewComponentPeer* const otherPeer = dynamic_cast <NSViewComponentPeer*> (other);
  371. jassert (otherPeer != nullptr); // wrong type of window?
  372. if (otherPeer != nullptr)
  373. {
  374. if (isSharedWindow)
  375. {
  376. [[view superview] addSubview: view
  377. positioned: NSWindowBelow
  378. relativeTo: otherPeer->view];
  379. }
  380. else
  381. {
  382. [window orderWindow: NSWindowBelow
  383. relativeTo: [otherPeer->window windowNumber]];
  384. }
  385. }
  386. }
  387. void setIcon (const Image&)
  388. {
  389. // to do..
  390. }
  391. StringArray getAvailableRenderingEngines()
  392. {
  393. StringArray s (ComponentPeer::getAvailableRenderingEngines());
  394. #if USE_COREGRAPHICS_RENDERING
  395. s.add ("CoreGraphics Renderer");
  396. #endif
  397. return s;
  398. }
  399. int getCurrentRenderingEngine() const
  400. {
  401. return usingCoreGraphics ? 1 : 0;
  402. }
  403. void setCurrentRenderingEngine (int index)
  404. {
  405. #if USE_COREGRAPHICS_RENDERING
  406. if (usingCoreGraphics != (index > 0))
  407. {
  408. usingCoreGraphics = index > 0;
  409. [view setNeedsDisplay: true];
  410. }
  411. #endif
  412. }
  413. void redirectMouseDown (NSEvent* ev)
  414. {
  415. if (! Process::isForegroundProcess())
  416. Process::makeForegroundProcess();
  417. currentModifiers = currentModifiers.withFlags (getModifierForButtonNumber ([ev buttonNumber]));
  418. sendMouseEvent (ev);
  419. }
  420. void redirectMouseUp (NSEvent* ev)
  421. {
  422. currentModifiers = currentModifiers.withoutFlags (getModifierForButtonNumber ([ev buttonNumber]));
  423. sendMouseEvent (ev);
  424. showArrowCursorIfNeeded();
  425. }
  426. void redirectMouseDrag (NSEvent* ev)
  427. {
  428. currentModifiers = currentModifiers.withFlags (getModifierForButtonNumber ([ev buttonNumber]));
  429. sendMouseEvent (ev);
  430. }
  431. void redirectMouseMove (NSEvent* ev)
  432. {
  433. currentModifiers = currentModifiers.withoutMouseButtons();
  434. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  435. if ([NSWindow respondsToSelector: @selector (windowNumberAtPoint:belowWindowWithWindowNumber:)]
  436. && [NSWindow windowNumberAtPoint: [[ev window] convertBaseToScreen: [ev locationInWindow]]
  437. belowWindowWithWindowNumber: 0] != [window windowNumber])
  438. {
  439. // moved into another window which overlaps this one, so trigger an exit
  440. handleMouseEvent (0, Point<int> (-1, -1), currentModifiers, getMouseTime (ev));
  441. }
  442. else
  443. #endif
  444. {
  445. sendMouseEvent (ev);
  446. }
  447. showArrowCursorIfNeeded();
  448. }
  449. void redirectMouseEnter (NSEvent* ev)
  450. {
  451. Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
  452. currentModifiers = currentModifiers.withoutMouseButtons();
  453. sendMouseEvent (ev);
  454. }
  455. void redirectMouseExit (NSEvent* ev)
  456. {
  457. currentModifiers = currentModifiers.withoutMouseButtons();
  458. sendMouseEvent (ev);
  459. }
  460. static float checkDeviceDeltaReturnValue (float v) noexcept
  461. {
  462. // (deviceDeltaX can fail and return NaN, so need to sanity-check the result)
  463. v *= 0.5f / 256.0f;
  464. return (v > -1000.0f && v < 1000.0f) ? v : 0.0f;
  465. }
  466. void redirectMouseWheel (NSEvent* ev)
  467. {
  468. updateModifiers (ev);
  469. MouseWheelDetails wheel;
  470. wheel.deltaX = 0;
  471. wheel.deltaY = 0;
  472. wheel.isReversed = false;
  473. wheel.isSmooth = false;
  474. #if ! JUCE_PPC
  475. @try
  476. {
  477. #if defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
  478. if ([ev respondsToSelector: @selector (isDirectionInvertedFromDevice)])
  479. wheel.isReversed = [ev isDirectionInvertedFromDevice];
  480. if ([ev respondsToSelector: @selector (hasPreciseScrollingDeltas)])
  481. {
  482. if ([ev hasPreciseScrollingDeltas])
  483. {
  484. const float scale = 0.5f / 256.0f;
  485. wheel.deltaX = [ev scrollingDeltaX] * scale;
  486. wheel.deltaY = [ev scrollingDeltaY] * scale;
  487. wheel.isSmooth = true;
  488. }
  489. }
  490. else
  491. #endif
  492. if ([ev respondsToSelector: @selector (deviceDeltaX)])
  493. {
  494. wheel.deltaX = checkDeviceDeltaReturnValue ((float) objc_msgSend_fpret (ev, @selector (deviceDeltaX)));
  495. wheel.deltaY = checkDeviceDeltaReturnValue ((float) objc_msgSend_fpret (ev, @selector (deviceDeltaY)));
  496. }
  497. }
  498. @catch (...)
  499. {}
  500. #endif
  501. if (wheel.deltaX == 0 && wheel.deltaY == 0)
  502. {
  503. const float scale = 10.0f / 256.0f;
  504. wheel.deltaX = [ev deltaX] * scale;
  505. wheel.deltaY = [ev deltaY] * scale;
  506. }
  507. handleMouseWheel (0, getMousePos (ev, view), getMouseTime (ev), wheel);
  508. }
  509. void redirectMagnify (NSEvent* ev)
  510. {
  511. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  512. const float invScale = 1.0f - [ev magnification];
  513. if (invScale != 0.0f)
  514. handleMagnifyGesture (0, getMousePos (ev, view), getMouseTime (ev), 1.0f / invScale);
  515. #endif
  516. (void) ev;
  517. }
  518. void sendMouseEvent (NSEvent* ev)
  519. {
  520. updateModifiers (ev);
  521. handleMouseEvent (0, getMousePos (ev, view), currentModifiers, getMouseTime (ev));
  522. }
  523. bool handleKeyEvent (NSEvent* ev, bool isKeyDown)
  524. {
  525. const String unicode (nsStringToJuce ([ev characters]));
  526. const int keyCode = getKeyCodeFromEvent (ev);
  527. //DBG ("unicode: " + unicode + " " + String::toHexString ((int) unicode[0]));
  528. //String unmodified (nsStringToJuce ([ev charactersIgnoringModifiers]));
  529. //DBG ("unmodified: " + unmodified + " " + String::toHexString ((int) unmodified[0]));
  530. if (keyCode != 0 || unicode.isNotEmpty())
  531. {
  532. if (isKeyDown)
  533. {
  534. bool used = false;
  535. for (String::CharPointerType u (unicode.getCharPointer()); ! u.isEmpty();)
  536. {
  537. juce_wchar textCharacter = u.getAndAdvance();
  538. switch (keyCode)
  539. {
  540. case NSLeftArrowFunctionKey:
  541. case NSRightArrowFunctionKey:
  542. case NSUpArrowFunctionKey:
  543. case NSDownArrowFunctionKey:
  544. case NSPageUpFunctionKey:
  545. case NSPageDownFunctionKey:
  546. case NSEndFunctionKey:
  547. case NSHomeFunctionKey:
  548. case NSDeleteFunctionKey:
  549. textCharacter = 0;
  550. break; // (these all seem to generate unwanted garbage unicode strings)
  551. default:
  552. if (([ev modifierFlags] & NSCommandKeyMask) != 0)
  553. textCharacter = 0;
  554. break;
  555. }
  556. used = handleKeyUpOrDown (true) || used;
  557. used = handleKeyPress (keyCode, textCharacter) || used;
  558. }
  559. return used;
  560. }
  561. if (handleKeyUpOrDown (false))
  562. return true;
  563. }
  564. return false;
  565. }
  566. bool redirectKeyDown (NSEvent* ev)
  567. {
  568. // (need to retain this in case a modal loop runs in handleKeyEvent and
  569. // our event object gets lost)
  570. const NSObjectRetainer<NSEvent> r (ev);
  571. updateKeysDown (ev, true);
  572. bool used = handleKeyEvent (ev, true);
  573. if (([ev modifierFlags] & NSCommandKeyMask) != 0)
  574. {
  575. // for command keys, the key-up event is thrown away, so simulate one..
  576. updateKeysDown (ev, false);
  577. used = (isValidPeer (this) && handleKeyEvent (ev, false)) || used;
  578. }
  579. // (If we're running modally, don't allow unused keystrokes to be passed
  580. // along to other blocked views..)
  581. if (Component::getCurrentlyModalComponent() != nullptr)
  582. used = true;
  583. return used;
  584. }
  585. bool redirectKeyUp (NSEvent* ev)
  586. {
  587. updateKeysDown (ev, false);
  588. return handleKeyEvent (ev, false)
  589. || Component::getCurrentlyModalComponent() != nullptr;
  590. }
  591. void redirectModKeyChange (NSEvent* ev)
  592. {
  593. // (need to retain this in case a modal loop runs and our event object gets lost)
  594. const NSObjectRetainer<NSEvent> r (ev);
  595. keysCurrentlyDown.clear();
  596. handleKeyUpOrDown (true);
  597. updateModifiers (ev);
  598. handleModifierKeysChange();
  599. }
  600. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  601. bool redirectPerformKeyEquivalent (NSEvent* ev)
  602. {
  603. if ([ev type] == NSKeyDown) return redirectKeyDown (ev);
  604. if ([ev type] == NSKeyUp) return redirectKeyUp (ev);
  605. return false;
  606. }
  607. #endif
  608. bool isOpaque()
  609. {
  610. return component.isOpaque();
  611. }
  612. void drawRect (NSRect r)
  613. {
  614. if (r.size.width < 1.0f || r.size.height < 1.0f)
  615. return;
  616. CGContextRef cg = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
  617. if (! component.isOpaque())
  618. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  619. float displayScale = 1.0f;
  620. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  621. NSScreen* screen = [[view window] screen];
  622. if ([screen respondsToSelector: @selector (backingScaleFactor)])
  623. displayScale = screen.backingScaleFactor;
  624. #endif
  625. #if USE_COREGRAPHICS_RENDERING
  626. if (usingCoreGraphics)
  627. {
  628. CoreGraphicsContext context (cg, (float) [view frame].size.height, displayScale);
  629. insideDrawRect = true;
  630. handlePaint (context);
  631. insideDrawRect = false;
  632. }
  633. else
  634. #endif
  635. {
  636. const Point<int> offset (-roundToInt (r.origin.x),
  637. -roundToInt ([view frame].size.height - (r.origin.y + r.size.height)));
  638. const int clipW = (int) (r.size.width + 0.5f);
  639. const int clipH = (int) (r.size.height + 0.5f);
  640. RectangleList clip;
  641. getClipRects (clip, offset, clipW, clipH);
  642. if (! clip.isEmpty())
  643. {
  644. Image temp (component.isOpaque() ? Image::RGB : Image::ARGB,
  645. roundToInt (clipW * displayScale),
  646. roundToInt (clipH * displayScale),
  647. ! component.isOpaque());
  648. {
  649. const int intScale = roundToInt (displayScale);
  650. if (intScale != 1)
  651. clip.scaleAll (intScale);
  652. ScopedPointer<LowLevelGraphicsContext> context (component.getLookAndFeel()
  653. .createGraphicsContext (temp, offset * intScale, clip));
  654. if (intScale != 1)
  655. context->addTransform (AffineTransform::scale (displayScale));
  656. insideDrawRect = true;
  657. handlePaint (*context);
  658. insideDrawRect = false;
  659. }
  660. CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
  661. CGImageRef image = juce_createCoreGraphicsImage (temp, colourSpace, false);
  662. CGColorSpaceRelease (colourSpace);
  663. CGContextDrawImage (cg, CGRectMake (r.origin.x, r.origin.y, clipW, clipH), image);
  664. CGImageRelease (image);
  665. }
  666. }
  667. }
  668. bool sendModalInputAttemptIfBlocked()
  669. {
  670. Component* const modal = Component::getCurrentlyModalComponent();
  671. if (modal != nullptr
  672. && insideToFrontCall == 0
  673. && (! getComponent().isParentOf (modal))
  674. && getComponent().isCurrentlyBlockedByAnotherModalComponent())
  675. {
  676. modal->inputAttemptWhenModal();
  677. return true;
  678. }
  679. return false;
  680. }
  681. bool canBecomeKeyWindow()
  682. {
  683. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  684. }
  685. void becomeKeyWindow()
  686. {
  687. handleBroughtToFront();
  688. grabFocus();
  689. }
  690. bool windowShouldClose()
  691. {
  692. if (! isValidPeer (this))
  693. return YES;
  694. handleUserClosingWindow();
  695. return NO;
  696. }
  697. void redirectMovedOrResized()
  698. {
  699. updateFullscreenStatus();
  700. handleMovedOrResized();
  701. }
  702. void viewMovedToWindow()
  703. {
  704. if (isSharedWindow)
  705. window = [view window];
  706. }
  707. void liveResizingStart()
  708. {
  709. if (constrainer != nullptr)
  710. constrainer->resizeStart();
  711. }
  712. void liveResizingEnd()
  713. {
  714. if (constrainer != nullptr)
  715. constrainer->resizeEnd();
  716. }
  717. NSRect constrainRect (NSRect r)
  718. {
  719. if (constrainer != nullptr
  720. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  721. && ([window styleMask] & NSFullScreenWindowMask) == 0
  722. #endif
  723. )
  724. {
  725. NSRect current = [window frame];
  726. current.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - current.origin.y - current.size.height;
  727. r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - r.origin.y - r.size.height;
  728. Rectangle<int> pos (convertToRectInt (r));
  729. Rectangle<int> original (convertToRectInt (current));
  730. const Rectangle<int> screenBounds (Desktop::getInstance().getDisplays().getTotalBounds (true));
  731. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
  732. if ([window inLiveResize])
  733. #else
  734. if ([window respondsToSelector: @selector (inLiveResize)]
  735. && [window performSelector: @selector (inLiveResize)])
  736. #endif
  737. {
  738. constrainer->checkBounds (pos, original, screenBounds,
  739. false, false, true, true);
  740. }
  741. else
  742. {
  743. constrainer->checkBounds (pos, original, screenBounds,
  744. pos.getY() != original.getY() && pos.getBottom() == original.getBottom(),
  745. pos.getX() != original.getX() && pos.getRight() == original.getRight(),
  746. pos.getY() == original.getY() && pos.getBottom() != original.getBottom(),
  747. pos.getX() == original.getX() && pos.getRight() != original.getRight());
  748. }
  749. r.origin.x = pos.getX();
  750. r.origin.y = [[[NSScreen screens] objectAtIndex: 0] frame].size.height - r.size.height - pos.getY();
  751. r.size.width = pos.getWidth();
  752. r.size.height = pos.getHeight();
  753. }
  754. return r;
  755. }
  756. static void showArrowCursorIfNeeded()
  757. {
  758. Desktop& desktop = Desktop::getInstance();
  759. MouseInputSource& mouse = desktop.getMainMouseSource();
  760. if (mouse.getComponentUnderMouse() == nullptr
  761. && desktop.findComponentAt (mouse.getScreenPosition()) == nullptr)
  762. {
  763. [[NSCursor arrowCursor] set];
  764. }
  765. }
  766. static void updateModifiers (NSEvent* e)
  767. {
  768. updateModifiers ([e modifierFlags]);
  769. }
  770. static void updateModifiers (const NSUInteger flags)
  771. {
  772. int m = 0;
  773. if ((flags & NSShiftKeyMask) != 0) m |= ModifierKeys::shiftModifier;
  774. if ((flags & NSControlKeyMask) != 0) m |= ModifierKeys::ctrlModifier;
  775. if ((flags & NSAlternateKeyMask) != 0) m |= ModifierKeys::altModifier;
  776. if ((flags & NSCommandKeyMask) != 0) m |= ModifierKeys::commandModifier;
  777. currentModifiers = currentModifiers.withOnlyMouseButtons().withFlags (m);
  778. }
  779. static void updateKeysDown (NSEvent* ev, bool isKeyDown)
  780. {
  781. updateModifiers (ev);
  782. int keyCode = getKeyCodeFromEvent (ev);
  783. if (keyCode != 0)
  784. {
  785. if (isKeyDown)
  786. keysCurrentlyDown.addIfNotAlreadyThere (keyCode);
  787. else
  788. keysCurrentlyDown.removeFirstMatchingValue (keyCode);
  789. }
  790. }
  791. static int getKeyCodeFromEvent (NSEvent* ev)
  792. {
  793. const String unmodified (nsStringToJuce ([ev charactersIgnoringModifiers]));
  794. int keyCode = unmodified[0];
  795. if (keyCode == 0x19) // (backwards-tab)
  796. keyCode = '\t';
  797. else if (keyCode == 0x03) // (enter)
  798. keyCode = '\r';
  799. else
  800. keyCode = (int) CharacterFunctions::toUpperCase ((juce_wchar) keyCode);
  801. if (([ev modifierFlags] & NSNumericPadKeyMask) != 0)
  802. {
  803. const int numPadConversions[] = { '0', KeyPress::numberPad0, '1', KeyPress::numberPad1,
  804. '2', KeyPress::numberPad2, '3', KeyPress::numberPad3,
  805. '4', KeyPress::numberPad4, '5', KeyPress::numberPad5,
  806. '6', KeyPress::numberPad6, '7', KeyPress::numberPad7,
  807. '8', KeyPress::numberPad8, '9', KeyPress::numberPad9,
  808. '+', KeyPress::numberPadAdd, '-', KeyPress::numberPadSubtract,
  809. '*', KeyPress::numberPadMultiply, '/', KeyPress::numberPadDivide,
  810. '.', KeyPress::numberPadDecimalPoint, '=', KeyPress::numberPadEquals };
  811. for (int i = 0; i < numElementsInArray (numPadConversions); i += 2)
  812. if (keyCode == numPadConversions [i])
  813. keyCode = numPadConversions [i + 1];
  814. }
  815. return keyCode;
  816. }
  817. static int64 getMouseTime (NSEvent* e)
  818. {
  819. return (Time::currentTimeMillis() - Time::getMillisecondCounter())
  820. + (int64) ([e timestamp] * 1000.0);
  821. }
  822. static Point<int> getMousePos (NSEvent* e, NSView* view)
  823. {
  824. NSPoint p = [view convertPoint: [e locationInWindow] fromView: nil];
  825. return Point<int> ((int) p.x, (int) ([view frame].size.height - p.y));
  826. }
  827. static int getModifierForButtonNumber (const NSInteger num)
  828. {
  829. return num == 0 ? ModifierKeys::leftButtonModifier
  830. : (num == 1 ? ModifierKeys::rightButtonModifier
  831. : (num == 2 ? ModifierKeys::middleButtonModifier : 0));
  832. }
  833. static unsigned int getNSWindowStyleMask (const int flags) noexcept
  834. {
  835. unsigned int style = (flags & windowHasTitleBar) != 0 ? NSTitledWindowMask
  836. : NSBorderlessWindowMask;
  837. if ((flags & windowHasMinimiseButton) != 0) style |= NSMiniaturizableWindowMask;
  838. if ((flags & windowHasCloseButton) != 0) style |= NSClosableWindowMask;
  839. if ((flags & windowIsResizable) != 0) style |= NSResizableWindowMask;
  840. return style;
  841. }
  842. static NSArray* getSupportedDragTypes()
  843. {
  844. return [NSArray arrayWithObjects: NSFilenamesPboardType, NSFilesPromisePboardType, NSStringPboardType, nil];
  845. }
  846. BOOL sendDragCallback (const int type, id <NSDraggingInfo> sender)
  847. {
  848. NSPasteboard* pasteboard = [sender draggingPasteboard];
  849. NSString* contentType = [pasteboard availableTypeFromArray: getSupportedDragTypes()];
  850. if (contentType == nil)
  851. return false;
  852. NSPoint p = [view convertPoint: [sender draggingLocation] fromView: nil];
  853. ComponentPeer::DragInfo dragInfo;
  854. dragInfo.position.setXY ((int) p.x, (int) ([view frame].size.height - p.y));
  855. if (contentType == NSStringPboardType)
  856. dragInfo.text = nsStringToJuce ([pasteboard stringForType: NSStringPboardType]);
  857. else
  858. dragInfo.files = getDroppedFiles (pasteboard, contentType);
  859. if (! dragInfo.isEmpty())
  860. {
  861. switch (type)
  862. {
  863. case 0: return handleDragMove (dragInfo);
  864. case 1: return handleDragExit (dragInfo);
  865. case 2: return handleDragDrop (dragInfo);
  866. default: jassertfalse; break;
  867. }
  868. }
  869. return false;
  870. }
  871. StringArray getDroppedFiles (NSPasteboard* pasteboard, NSString* contentType)
  872. {
  873. StringArray files;
  874. NSString* iTunesPasteboardType = nsStringLiteral ("CorePasteboardFlavorType 0x6974756E"); // 'itun'
  875. if (contentType == NSFilesPromisePboardType
  876. && [[pasteboard types] containsObject: iTunesPasteboardType])
  877. {
  878. id list = [pasteboard propertyListForType: iTunesPasteboardType];
  879. if ([list isKindOfClass: [NSDictionary class]])
  880. {
  881. NSDictionary* iTunesDictionary = (NSDictionary*) list;
  882. NSArray* tracks = [iTunesDictionary valueForKey: nsStringLiteral ("Tracks")];
  883. NSEnumerator* enumerator = [tracks objectEnumerator];
  884. NSDictionary* track;
  885. while ((track = [enumerator nextObject]) != nil)
  886. {
  887. NSURL* url = [NSURL URLWithString: [track valueForKey: nsStringLiteral ("Location")]];
  888. if ([url isFileURL])
  889. files.add (nsStringToJuce ([url path]));
  890. }
  891. }
  892. }
  893. else
  894. {
  895. id list = [pasteboard propertyListForType: NSFilenamesPboardType];
  896. if ([list isKindOfClass: [NSArray class]])
  897. {
  898. NSArray* items = (NSArray*) [pasteboard propertyListForType: NSFilenamesPboardType];
  899. for (unsigned int i = 0; i < [items count]; ++i)
  900. files.add (nsStringToJuce ((NSString*) [items objectAtIndex: i]));
  901. }
  902. }
  903. return files;
  904. }
  905. //==============================================================================
  906. void viewFocusGain()
  907. {
  908. if (currentlyFocusedPeer != this)
  909. {
  910. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  911. currentlyFocusedPeer->handleFocusLoss();
  912. currentlyFocusedPeer = this;
  913. handleFocusGain();
  914. }
  915. }
  916. void viewFocusLoss()
  917. {
  918. if (currentlyFocusedPeer == this)
  919. {
  920. currentlyFocusedPeer = nullptr;
  921. handleFocusLoss();
  922. }
  923. }
  924. bool isFocused() const
  925. {
  926. return isSharedWindow ? this == currentlyFocusedPeer
  927. : [window isKeyWindow];
  928. }
  929. void grabFocus()
  930. {
  931. if (window != nil)
  932. {
  933. [window makeKeyWindow];
  934. [window makeFirstResponder: view];
  935. viewFocusGain();
  936. }
  937. }
  938. void textInputRequired (const Point<int>&) {}
  939. //==============================================================================
  940. void repaint (const Rectangle<int>& area)
  941. {
  942. if (insideDrawRect)
  943. {
  944. class AsyncRepaintMessage : public CallbackMessage
  945. {
  946. public:
  947. AsyncRepaintMessage (NSViewComponentPeer* const p, const Rectangle<int>& r)
  948. : peer (p), rect (r)
  949. {}
  950. void messageCallback()
  951. {
  952. if (ComponentPeer::isValidPeer (peer))
  953. peer->repaint (rect);
  954. }
  955. private:
  956. NSViewComponentPeer* const peer;
  957. const Rectangle<int> rect;
  958. };
  959. (new AsyncRepaintMessage (this, area))->post();
  960. }
  961. else
  962. {
  963. [view setNeedsDisplayInRect: NSMakeRect ((CGFloat) area.getX(), [view frame].size.height - (CGFloat) area.getBottom(),
  964. (CGFloat) area.getWidth(), (CGFloat) area.getHeight())];
  965. }
  966. }
  967. void performAnyPendingRepaintsNow()
  968. {
  969. [view displayIfNeeded];
  970. }
  971. //==============================================================================
  972. NSWindow* window;
  973. NSView* view;
  974. bool isSharedWindow, fullScreen, insideDrawRect;
  975. bool usingCoreGraphics, isZooming, textWasInserted;
  976. String stringBeingComposed;
  977. NSNotificationCenter* notificationCenter;
  978. static ModifierKeys currentModifiers;
  979. static ComponentPeer* currentlyFocusedPeer;
  980. static Array<int> keysCurrentlyDown;
  981. static int insideToFrontCall;
  982. private:
  983. static NSView* createViewInstance();
  984. static NSWindow* createWindowInstance();
  985. static void setOwner (id viewOrWindow, NSViewComponentPeer* newOwner)
  986. {
  987. object_setInstanceVariable (viewOrWindow, "owner", newOwner);
  988. }
  989. void getClipRects (RectangleList& clip, const Point<int> offset, const int clipW, const int clipH)
  990. {
  991. const NSRect* rects = nullptr;
  992. NSInteger numRects = 0;
  993. [view getRectsBeingDrawn: &rects count: &numRects];
  994. const Rectangle<int> clipBounds (clipW, clipH);
  995. const CGFloat viewH = [view frame].size.height;
  996. for (int i = 0; i < numRects; ++i)
  997. clip.addWithoutMerging (clipBounds.getIntersection (Rectangle<int> (roundToInt (rects[i].origin.x) + offset.x,
  998. roundToInt (viewH - (rects[i].origin.y + rects[i].size.height)) + offset.y,
  999. roundToInt (rects[i].size.width),
  1000. roundToInt (rects[i].size.height))));
  1001. }
  1002. static void appFocusChanged()
  1003. {
  1004. keysCurrentlyDown.clear();
  1005. if (isValidPeer (currentlyFocusedPeer))
  1006. {
  1007. if (Process::isForegroundProcess())
  1008. {
  1009. currentlyFocusedPeer->handleFocusGain();
  1010. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  1011. }
  1012. else
  1013. {
  1014. currentlyFocusedPeer->handleFocusLoss();
  1015. }
  1016. }
  1017. }
  1018. static bool checkEventBlockedByModalComps (NSEvent* e)
  1019. {
  1020. if (Component::getNumCurrentlyModalComponents() == 0)
  1021. return false;
  1022. NSWindow* const w = [e window];
  1023. if (w == nil || [w worksWhenModal])
  1024. return false;
  1025. bool isKey = false, isInputAttempt = false;
  1026. switch ([e type])
  1027. {
  1028. case NSKeyDown:
  1029. case NSKeyUp:
  1030. isKey = isInputAttempt = true;
  1031. break;
  1032. case NSLeftMouseDown:
  1033. case NSRightMouseDown:
  1034. case NSOtherMouseDown:
  1035. isInputAttempt = true;
  1036. break;
  1037. case NSLeftMouseDragged:
  1038. case NSRightMouseDragged:
  1039. case NSLeftMouseUp:
  1040. case NSRightMouseUp:
  1041. case NSOtherMouseUp:
  1042. case NSOtherMouseDragged:
  1043. if (Desktop::getInstance().getDraggingMouseSource(0) != nullptr)
  1044. return false;
  1045. break;
  1046. case NSMouseMoved:
  1047. case NSMouseEntered:
  1048. case NSMouseExited:
  1049. case NSCursorUpdate:
  1050. case NSScrollWheel:
  1051. case NSTabletPoint:
  1052. case NSTabletProximity:
  1053. break;
  1054. default:
  1055. return false;
  1056. }
  1057. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  1058. {
  1059. ComponentPeer* const peer = ComponentPeer::getPeer (i);
  1060. NSView* const compView = (NSView*) peer->getNativeHandle();
  1061. if ([compView window] == w)
  1062. {
  1063. if (isKey)
  1064. {
  1065. if (compView == [w firstResponder])
  1066. return false;
  1067. }
  1068. else
  1069. {
  1070. NSViewComponentPeer* nsViewPeer = dynamic_cast<NSViewComponentPeer*> (peer);
  1071. if ((nsViewPeer == nullptr || ! nsViewPeer->isSharedWindow)
  1072. ? NSPointInRect ([e locationInWindow], NSMakeRect (0, 0, [w frame].size.width, [w frame].size.height))
  1073. : NSPointInRect ([compView convertPoint: [e locationInWindow] fromView: nil], [compView bounds]))
  1074. return false;
  1075. }
  1076. }
  1077. }
  1078. if (isInputAttempt)
  1079. {
  1080. if (! [NSApp isActive])
  1081. [NSApp activateIgnoringOtherApps: YES];
  1082. if (Component* const modal = Component::getCurrentlyModalComponent())
  1083. modal->inputAttemptWhenModal();
  1084. }
  1085. return true;
  1086. }
  1087. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NSViewComponentPeer)
  1088. };
  1089. int NSViewComponentPeer::insideToFrontCall = 0;
  1090. //==============================================================================
  1091. struct JuceNSViewClass : public ObjCClass <NSView>
  1092. {
  1093. JuceNSViewClass() : ObjCClass <NSView> ("JUCEView_")
  1094. {
  1095. addIvar<NSViewComponentPeer*> ("owner");
  1096. addMethod (@selector (isOpaque), isOpaque, "c@:");
  1097. addMethod (@selector (drawRect:), drawRect, "v@:", @encode (NSRect));
  1098. addMethod (@selector (mouseDown:), mouseDown, "v@:@");
  1099. addMethod (@selector (asyncMouseDown:), asyncMouseDown, "v@:@");
  1100. addMethod (@selector (mouseUp:), mouseUp, "v@:@");
  1101. addMethod (@selector (asyncMouseUp:), asyncMouseUp, "v@:@");
  1102. addMethod (@selector (mouseDragged:), mouseDragged, "v@:@");
  1103. addMethod (@selector (mouseMoved:), mouseMoved, "v@:@");
  1104. addMethod (@selector (mouseEntered:), mouseEntered, "v@:@");
  1105. addMethod (@selector (mouseExited:), mouseExited, "v@:@");
  1106. addMethod (@selector (rightMouseDown:), mouseDown, "v@:@");
  1107. addMethod (@selector (rightMouseDragged:), mouseDragged, "v@:@");
  1108. addMethod (@selector (rightMouseUp:), mouseUp, "v@:@");
  1109. addMethod (@selector (otherMouseDown:), mouseDown, "v@:@");
  1110. addMethod (@selector (otherMouseDragged:), mouseDragged, "v@:@");
  1111. addMethod (@selector (otherMouseUp:), mouseUp, "v@:@");
  1112. addMethod (@selector (scrollWheel:), scrollWheel, "v@:@");
  1113. addMethod (@selector (magnifyWithEvent:), magnify, "v@:@");
  1114. addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse, "v@:@");
  1115. addMethod (@selector (frameChanged:), frameChanged, "v@:@");
  1116. addMethod (@selector (viewDidMoveToWindow), viewDidMoveToWindow, "v@:");
  1117. addMethod (@selector (keyDown:), keyDown, "v@:@");
  1118. addMethod (@selector (keyUp:), keyUp, "v@:@");
  1119. addMethod (@selector (insertText:), insertText, "v@:@");
  1120. addMethod (@selector (doCommandBySelector:), doCommandBySelector, "v@::");
  1121. addMethod (@selector (setMarkedText:selectedRange:), setMarkedText, "v@:@", @encode (NSRange));
  1122. addMethod (@selector (unmarkText), unmarkText, "v@:");
  1123. addMethod (@selector (hasMarkedText), hasMarkedText, "c@:");
  1124. addMethod (@selector (conversationIdentifier), conversationIdentifier, "l@:");
  1125. addMethod (@selector (attributedSubstringFromRange:), attributedSubstringFromRange, "@@:", @encode (NSRange));
  1126. addMethod (@selector (markedRange), markedRange, @encode (NSRange), "@:");
  1127. addMethod (@selector (selectedRange), selectedRange, @encode (NSRange), "@:");
  1128. addMethod (@selector (firstRectForCharacterRange:), firstRectForCharacterRange, @encode (NSRect), "@:", @encode (NSRange));
  1129. addMethod (@selector (validAttributesForMarkedText), validAttributesForMarkedText, "@@:");
  1130. addMethod (@selector (flagsChanged:), flagsChanged, "v@:@");
  1131. addMethod (@selector (becomeFirstResponder), becomeFirstResponder, "c@:");
  1132. addMethod (@selector (resignFirstResponder), resignFirstResponder, "c@:");
  1133. addMethod (@selector (acceptsFirstResponder), acceptsFirstResponder, "c@:");
  1134. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  1135. addMethod (@selector (performKeyEquivalent:), performKeyEquivalent, "c@:@");
  1136. #endif
  1137. addMethod (@selector (draggingEntered:), draggingEntered, @encode (NSDragOperation), "@:@");
  1138. addMethod (@selector (draggingUpdated:), draggingUpdated, @encode (NSDragOperation), "@:@");
  1139. addMethod (@selector (draggingEnded:), draggingEnded, "v@:@");
  1140. addMethod (@selector (draggingExited:), draggingExited, "v@:@");
  1141. addMethod (@selector (prepareForDragOperation:), prepareForDragOperation, "c@:@");
  1142. addMethod (@selector (performDragOperation:), performDragOperation, "c@:@");
  1143. addMethod (@selector (concludeDragOperation:), concludeDragOperation, "v@:@");
  1144. addProtocol (@protocol (NSTextInput));
  1145. registerClass();
  1146. }
  1147. private:
  1148. static NSViewComponentPeer* getOwner (id self)
  1149. {
  1150. return getIvar<NSViewComponentPeer*> (self, "owner");
  1151. }
  1152. static void mouseDown (id self, SEL s, NSEvent* ev)
  1153. {
  1154. if (JUCEApplication::isStandaloneApp())
  1155. asyncMouseDown (self, s, ev);
  1156. else
  1157. // In some host situations, the host will stop modal loops from working
  1158. // correctly if they're called from a mouse event, so we'll trigger
  1159. // the event asynchronously..
  1160. [self performSelectorOnMainThread: @selector (asyncMouseDown:)
  1161. withObject: ev
  1162. waitUntilDone: NO];
  1163. }
  1164. static void mouseUp (id self, SEL s, NSEvent* ev)
  1165. {
  1166. if (JUCEApplication::isStandaloneApp())
  1167. asyncMouseUp (self, s, ev);
  1168. else
  1169. // In some host situations, the host will stop modal loops from working
  1170. // correctly if they're called from a mouse event, so we'll trigger
  1171. // the event asynchronously..
  1172. [self performSelectorOnMainThread: @selector (asyncMouseUp:)
  1173. withObject: ev
  1174. waitUntilDone: NO];
  1175. }
  1176. static void asyncMouseDown (id self, SEL, NSEvent* ev) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMouseDown (ev); }
  1177. static void asyncMouseUp (id self, SEL, NSEvent* ev) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMouseUp (ev); }
  1178. static void mouseDragged (id self, SEL, NSEvent* ev) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMouseDrag (ev); }
  1179. static void mouseMoved (id self, SEL, NSEvent* ev) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMouseMove (ev); }
  1180. static void mouseEntered (id self, SEL, NSEvent* ev) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMouseEnter (ev); }
  1181. static void mouseExited (id self, SEL, NSEvent* ev) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMouseExit (ev); }
  1182. static void scrollWheel (id self, SEL, NSEvent* ev) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMouseWheel (ev); }
  1183. static void magnify (id self, SEL, NSEvent* ev) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMagnify (ev); }
  1184. static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
  1185. static void drawRect (id self, SEL, NSRect r) { if (NSViewComponentPeer* const p = getOwner (self)) p->drawRect (r); }
  1186. static void frameChanged (id self, SEL, NSNotification*) { if (NSViewComponentPeer* const p = getOwner (self)) p->redirectMovedOrResized(); }
  1187. static void viewDidMoveToWindow (id self, SEL) { if (NSViewComponentPeer* const p = getOwner (self)) p->viewMovedToWindow(); }
  1188. static BOOL isOpaque (id self, SEL)
  1189. {
  1190. NSViewComponentPeer* const owner = getOwner (self);
  1191. return owner == nullptr || owner->isOpaque();
  1192. }
  1193. //==============================================================================
  1194. static void keyDown (id self, SEL, NSEvent* ev)
  1195. {
  1196. if (NSViewComponentPeer* const owner = getOwner (self))
  1197. {
  1198. TextInputTarget* const target = owner->findCurrentTextInputTarget();
  1199. owner->textWasInserted = false;
  1200. if (target != nullptr)
  1201. [(NSView*) self interpretKeyEvents: [NSArray arrayWithObject: ev]];
  1202. else
  1203. owner->stringBeingComposed = String::empty;
  1204. if ((! owner->textWasInserted) && (owner == nullptr || ! owner->redirectKeyDown (ev)))
  1205. {
  1206. objc_super s = { self, [NSView class] };
  1207. objc_msgSendSuper (&s, @selector (keyDown:), ev);
  1208. }
  1209. }
  1210. }
  1211. static void keyUp (id self, SEL, NSEvent* ev)
  1212. {
  1213. NSViewComponentPeer* const owner = getOwner (self);
  1214. if (owner == nullptr || ! owner->redirectKeyUp (ev))
  1215. {
  1216. objc_super s = { self, [NSView class] };
  1217. objc_msgSendSuper (&s, @selector (keyUp:), ev);
  1218. }
  1219. }
  1220. //==============================================================================
  1221. static void insertText (id self, SEL, id aString)
  1222. {
  1223. // This commits multi-byte text when return is pressed, or after every keypress for western keyboards
  1224. if (NSViewComponentPeer* const owner = getOwner (self))
  1225. {
  1226. NSString* newText = [aString isKindOfClass: [NSAttributedString class]] ? [aString string] : aString;
  1227. if ([newText length] > 0)
  1228. {
  1229. if (TextInputTarget* const target = owner->findCurrentTextInputTarget())
  1230. {
  1231. target->insertTextAtCaret (nsStringToJuce (newText));
  1232. owner->textWasInserted = true;
  1233. }
  1234. }
  1235. owner->stringBeingComposed = String::empty;
  1236. }
  1237. }
  1238. static void doCommandBySelector (id, SEL, SEL) {}
  1239. static void setMarkedText (id self, SEL, id aString, NSRange)
  1240. {
  1241. if (NSViewComponentPeer* const owner = getOwner (self))
  1242. {
  1243. owner->stringBeingComposed = nsStringToJuce ([aString isKindOfClass: [NSAttributedString class]]
  1244. ? [aString string] : aString);
  1245. if (TextInputTarget* const target = owner->findCurrentTextInputTarget())
  1246. {
  1247. const Range<int> currentHighlight (target->getHighlightedRegion());
  1248. target->insertTextAtCaret (owner->stringBeingComposed);
  1249. target->setHighlightedRegion (currentHighlight.withLength (owner->stringBeingComposed.length()));
  1250. owner->textWasInserted = true;
  1251. }
  1252. }
  1253. }
  1254. static void unmarkText (id self, SEL)
  1255. {
  1256. if (NSViewComponentPeer* const owner = getOwner (self))
  1257. {
  1258. if (owner->stringBeingComposed.isNotEmpty())
  1259. {
  1260. if (TextInputTarget* const target = owner->findCurrentTextInputTarget())
  1261. {
  1262. target->insertTextAtCaret (owner->stringBeingComposed);
  1263. owner->textWasInserted = true;
  1264. }
  1265. owner->stringBeingComposed = String::empty;
  1266. }
  1267. }
  1268. }
  1269. static BOOL hasMarkedText (id self, SEL)
  1270. {
  1271. NSViewComponentPeer* const owner = getOwner (self);
  1272. return owner != nullptr && owner->stringBeingComposed.isNotEmpty();
  1273. }
  1274. static long conversationIdentifier (id self, SEL)
  1275. {
  1276. return (long) (pointer_sized_int) self;
  1277. }
  1278. static NSAttributedString* attributedSubstringFromRange (id self, SEL, NSRange theRange)
  1279. {
  1280. if (NSViewComponentPeer* const owner = getOwner (self))
  1281. {
  1282. if (TextInputTarget* const target = owner->findCurrentTextInputTarget())
  1283. {
  1284. const Range<int> r ((int) theRange.location,
  1285. (int) (theRange.location + theRange.length));
  1286. return [[[NSAttributedString alloc] initWithString: juceStringToNS (target->getTextInRange (r))] autorelease];
  1287. }
  1288. }
  1289. return nil;
  1290. }
  1291. static NSRange markedRange (id self, SEL)
  1292. {
  1293. if (NSViewComponentPeer* const owner = getOwner (self))
  1294. if (owner->stringBeingComposed.isNotEmpty())
  1295. return NSMakeRange (0, (NSUInteger) owner->stringBeingComposed.length());
  1296. return NSMakeRange (NSNotFound, 0);
  1297. }
  1298. static NSRange selectedRange (id self, SEL)
  1299. {
  1300. if (NSViewComponentPeer* const owner = getOwner (self))
  1301. {
  1302. if (TextInputTarget* const target = owner->findCurrentTextInputTarget())
  1303. {
  1304. const Range<int> highlight (target->getHighlightedRegion());
  1305. if (! highlight.isEmpty())
  1306. return NSMakeRange ((NSUInteger) highlight.getStart(),
  1307. (NSUInteger) highlight.getLength());
  1308. }
  1309. }
  1310. return NSMakeRange (NSNotFound, 0);
  1311. }
  1312. static NSRect firstRectForCharacterRange (id self, SEL, NSRange)
  1313. {
  1314. if (NSViewComponentPeer* const owner = getOwner (self))
  1315. {
  1316. if (Component* const comp = dynamic_cast <Component*> (owner->findCurrentTextInputTarget()))
  1317. {
  1318. const Rectangle<int> bounds (comp->getScreenBounds());
  1319. return NSMakeRect (bounds.getX(),
  1320. [[[NSScreen screens] objectAtIndex: 0] frame].size.height - bounds.getY(),
  1321. bounds.getWidth(),
  1322. bounds.getHeight());
  1323. }
  1324. }
  1325. return NSZeroRect;
  1326. }
  1327. static NSUInteger characterIndexForPoint (id, SEL, NSPoint) { return NSNotFound; }
  1328. static NSArray* validAttributesForMarkedText (id, SEL) { return [NSArray array]; }
  1329. //==============================================================================
  1330. static void flagsChanged (id self, SEL, NSEvent* ev)
  1331. {
  1332. if (NSViewComponentPeer* const owner = getOwner (self))
  1333. owner->redirectModKeyChange (ev);
  1334. }
  1335. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  1336. static BOOL performKeyEquivalent (id self, SEL, NSEvent* ev)
  1337. {
  1338. if (NSViewComponentPeer* const owner = getOwner (self))
  1339. if (owner->redirectPerformKeyEquivalent (ev))
  1340. return true;
  1341. objc_super s = { self, [NSView class] };
  1342. return objc_msgSendSuper (&s, @selector (performKeyEquivalent:), ev) != nil;
  1343. }
  1344. #endif
  1345. static BOOL becomeFirstResponder (id self, SEL)
  1346. {
  1347. if (NSViewComponentPeer* const owner = getOwner (self))
  1348. owner->viewFocusGain();
  1349. return YES;
  1350. }
  1351. static BOOL resignFirstResponder (id self, SEL)
  1352. {
  1353. if (NSViewComponentPeer* const owner = getOwner (self))
  1354. owner->viewFocusLoss();
  1355. return YES;
  1356. }
  1357. static BOOL acceptsFirstResponder (id self, SEL)
  1358. {
  1359. NSViewComponentPeer* const owner = getOwner (self);
  1360. return owner != nullptr && owner->canBecomeKeyWindow();
  1361. }
  1362. //==============================================================================
  1363. static NSDragOperation draggingEntered (id self, SEL s, id <NSDraggingInfo> sender)
  1364. {
  1365. return draggingUpdated (self, s, sender);
  1366. }
  1367. static NSDragOperation draggingUpdated (id self, SEL, id <NSDraggingInfo> sender)
  1368. {
  1369. if (NSViewComponentPeer* const owner = getOwner (self))
  1370. if (owner->sendDragCallback (0, sender))
  1371. return NSDragOperationCopy | NSDragOperationMove | NSDragOperationGeneric;
  1372. return NSDragOperationNone;
  1373. }
  1374. static void draggingEnded (id self, SEL s, id <NSDraggingInfo> sender)
  1375. {
  1376. draggingExited (self, s, sender);
  1377. }
  1378. static void draggingExited (id self, SEL, id <NSDraggingInfo> sender)
  1379. {
  1380. if (NSViewComponentPeer* const owner = getOwner (self))
  1381. owner->sendDragCallback (1, sender);
  1382. }
  1383. static BOOL prepareForDragOperation (id, SEL, id <NSDraggingInfo>)
  1384. {
  1385. return YES;
  1386. }
  1387. static BOOL performDragOperation (id self, SEL, id <NSDraggingInfo> sender)
  1388. {
  1389. NSViewComponentPeer* const owner = getOwner (self);
  1390. return owner != nullptr && owner->sendDragCallback (2, sender);
  1391. }
  1392. static void concludeDragOperation (id, SEL, id <NSDraggingInfo>) {}
  1393. };
  1394. //==============================================================================
  1395. struct JuceNSWindowClass : public ObjCClass <NSWindow>
  1396. {
  1397. JuceNSWindowClass() : ObjCClass <NSWindow> ("JUCEWindow_")
  1398. {
  1399. addIvar<NSViewComponentPeer*> ("owner");
  1400. addMethod (@selector (canBecomeKeyWindow), canBecomeKeyWindow, "c@:");
  1401. addMethod (@selector (becomeKeyWindow), becomeKeyWindow, "v@:");
  1402. addMethod (@selector (windowShouldClose:), windowShouldClose, "c@:@");
  1403. addMethod (@selector (constrainFrameRect:toScreen:), constrainFrameRect, @encode (NSRect), "@:", @encode (NSRect*), "@");
  1404. addMethod (@selector (windowWillResize:toSize:), windowWillResize, @encode (NSSize), "@:@", @encode (NSSize));
  1405. addMethod (@selector (zoom:), zoom, "v@:@");
  1406. addMethod (@selector (windowWillMove:), windowWillMove, "v@:@");
  1407. addMethod (@selector (windowWillStartLiveResize:), windowWillStartLiveResize, "v@:@");
  1408. addMethod (@selector (windowDidEndLiveResize:), windowDidEndLiveResize, "v@:@");
  1409. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  1410. addProtocol (@protocol (NSWindowDelegate));
  1411. #endif
  1412. registerClass();
  1413. }
  1414. private:
  1415. static NSViewComponentPeer* getOwner (id self)
  1416. {
  1417. return getIvar<NSViewComponentPeer*> (self, "owner");
  1418. }
  1419. //==============================================================================
  1420. static BOOL canBecomeKeyWindow (id self, SEL)
  1421. {
  1422. NSViewComponentPeer* const owner = getOwner (self);
  1423. return owner != nullptr
  1424. && owner->canBecomeKeyWindow()
  1425. && ! owner->sendModalInputAttemptIfBlocked();
  1426. }
  1427. static void becomeKeyWindow (id self, SEL)
  1428. {
  1429. sendSuperclassMessage (self, @selector (becomeKeyWindow));
  1430. if (NSViewComponentPeer* const owner = getOwner (self))
  1431. owner->becomeKeyWindow();
  1432. }
  1433. static BOOL windowShouldClose (id self, SEL, id /*window*/)
  1434. {
  1435. NSViewComponentPeer* const owner = getOwner (self);
  1436. return owner == nullptr || owner->windowShouldClose();
  1437. }
  1438. static NSRect constrainFrameRect (id self, SEL, NSRect frameRect, NSScreen*)
  1439. {
  1440. if (NSViewComponentPeer* const owner = getOwner (self))
  1441. frameRect = owner->constrainRect (frameRect);
  1442. return frameRect;
  1443. }
  1444. static NSSize windowWillResize (id self, SEL, NSWindow*, NSSize proposedFrameSize)
  1445. {
  1446. NSViewComponentPeer* const owner = getOwner (self);
  1447. if (owner == nullptr || owner->isZooming)
  1448. return proposedFrameSize;
  1449. NSRect frameRect = [(NSWindow*) self frame];
  1450. frameRect.origin.y -= proposedFrameSize.height - frameRect.size.height;
  1451. frameRect.size = proposedFrameSize;
  1452. frameRect = owner->constrainRect (frameRect);
  1453. if (owner->hasNativeTitleBar())
  1454. owner->sendModalInputAttemptIfBlocked();
  1455. return frameRect.size;
  1456. }
  1457. static void zoom (id self, SEL, id sender)
  1458. {
  1459. if (NSViewComponentPeer* const owner = getOwner (self))
  1460. {
  1461. owner->isZooming = true;
  1462. objc_super s = { self, [NSWindow class] };
  1463. objc_msgSendSuper (&s, @selector (zoom:), sender);
  1464. owner->isZooming = false;
  1465. owner->redirectMovedOrResized();
  1466. }
  1467. }
  1468. static void windowWillMove (id self, SEL, NSNotification*)
  1469. {
  1470. if (NSViewComponentPeer* const owner = getOwner (self))
  1471. if (owner->hasNativeTitleBar())
  1472. owner->sendModalInputAttemptIfBlocked();
  1473. }
  1474. static void windowWillStartLiveResize (id self, SEL, NSNotification*)
  1475. {
  1476. if (NSViewComponentPeer* const owner = getOwner (self))
  1477. owner->liveResizingStart();
  1478. }
  1479. static void windowDidEndLiveResize (id self, SEL, NSNotification*)
  1480. {
  1481. if (NSViewComponentPeer* const owner = getOwner (self))
  1482. owner->liveResizingEnd();
  1483. }
  1484. };
  1485. NSView* NSViewComponentPeer::createViewInstance()
  1486. {
  1487. static JuceNSViewClass cls;
  1488. return cls.createInstance();
  1489. }
  1490. NSWindow* NSViewComponentPeer::createWindowInstance()
  1491. {
  1492. static JuceNSWindowClass cls;
  1493. return cls.createInstance();
  1494. }
  1495. //==============================================================================
  1496. ModifierKeys NSViewComponentPeer::currentModifiers;
  1497. ComponentPeer* NSViewComponentPeer::currentlyFocusedPeer = nullptr;
  1498. Array<int> NSViewComponentPeer::keysCurrentlyDown;
  1499. //==============================================================================
  1500. bool KeyPress::isKeyCurrentlyDown (const int keyCode)
  1501. {
  1502. if (NSViewComponentPeer::keysCurrentlyDown.contains (keyCode))
  1503. return true;
  1504. if (keyCode >= 'A' && keyCode <= 'Z'
  1505. && NSViewComponentPeer::keysCurrentlyDown.contains ((int) CharacterFunctions::toLowerCase ((juce_wchar) keyCode)))
  1506. return true;
  1507. if (keyCode >= 'a' && keyCode <= 'z'
  1508. && NSViewComponentPeer::keysCurrentlyDown.contains ((int) CharacterFunctions::toUpperCase ((juce_wchar) keyCode)))
  1509. return true;
  1510. return false;
  1511. }
  1512. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  1513. {
  1514. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  1515. if ([NSEvent respondsToSelector: @selector (modifierFlags)])
  1516. NSViewComponentPeer::updateModifiers ((NSUInteger) [NSEvent modifierFlags]);
  1517. #endif
  1518. return NSViewComponentPeer::currentModifiers;
  1519. }
  1520. void ModifierKeys::updateCurrentModifiers() noexcept
  1521. {
  1522. currentModifiers = NSViewComponentPeer::currentModifiers;
  1523. }
  1524. //==============================================================================
  1525. bool Desktop::addMouseInputSource()
  1526. {
  1527. if (mouseSources.size() == 0)
  1528. {
  1529. mouseSources.add (new MouseInputSource (0, true));
  1530. return true;
  1531. }
  1532. return false;
  1533. }
  1534. //==============================================================================
  1535. void Desktop::setKioskComponent (Component* kioskComp, bool enableOrDisable, bool allowMenusAndBars)
  1536. {
  1537. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
  1538. NSViewComponentPeer* const peer = dynamic_cast<NSViewComponentPeer*> (kioskComp->getPeer());
  1539. jassert (peer != nullptr); // (this should have been checked by the caller)
  1540. #if defined (MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
  1541. if (peer->hasNativeTitleBar()
  1542. && [peer->window respondsToSelector: @selector (toggleFullScreen:)])
  1543. {
  1544. [peer->window performSelector: @selector (toggleFullScreen:)
  1545. withObject: [NSNumber numberWithBool: (BOOL) enableOrDisable]];
  1546. }
  1547. else
  1548. #endif
  1549. {
  1550. if (enableOrDisable)
  1551. {
  1552. if (peer->hasNativeTitleBar())
  1553. [peer->window setStyleMask: NSBorderlessWindowMask];
  1554. [NSApp setPresentationOptions: (allowMenusAndBars ? (NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)
  1555. : (NSApplicationPresentationHideDock | NSApplicationPresentationHideMenuBar))];
  1556. kioskComp->setBounds (Desktop::getInstance().getDisplays().getMainDisplay().totalArea);
  1557. peer->becomeKeyWindow();
  1558. }
  1559. else
  1560. {
  1561. if (peer->hasNativeTitleBar())
  1562. {
  1563. [peer->window setStyleMask: (NSViewComponentPeer::getNSWindowStyleMask (peer->getStyleFlags()))];
  1564. peer->setTitle (peer->getComponent().getName()); // required to force the OS to update the title
  1565. }
  1566. [NSApp setPresentationOptions: NSApplicationPresentationDefault];
  1567. }
  1568. }
  1569. #elif JUCE_SUPPORT_CARBON
  1570. (void) kioskComp; (void) enableOrDisable; (void) allowMenusAndBars;
  1571. if (enableOrDisable)
  1572. {
  1573. SetSystemUIMode (kUIModeAllSuppressed, allowMenusAndBars ? kUIOptionAutoShowMenuBar : 0);
  1574. kioskComp->setBounds (Desktop::getInstance().getDisplays().getMainDisplay().totalArea);
  1575. }
  1576. else
  1577. {
  1578. SetSystemUIMode (kUIModeNormal, 0);
  1579. }
  1580. #else
  1581. (void) kioskComp; (void) enableOrDisable; (void) allowMenusAndBars;
  1582. // If you're targeting OSes earlier than 10.6 and want to use this feature,
  1583. // you'll need to enable JUCE_SUPPORT_CARBON.
  1584. jassertfalse;
  1585. #endif
  1586. }
  1587. //==============================================================================
  1588. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  1589. {
  1590. return new NSViewComponentPeer (*this, styleFlags, (NSView*) windowToAttachTo);
  1591. }
  1592. //==============================================================================
  1593. const int KeyPress::spaceKey = ' ';
  1594. const int KeyPress::returnKey = 0x0d;
  1595. const int KeyPress::escapeKey = 0x1b;
  1596. const int KeyPress::backspaceKey = 0x7f;
  1597. const int KeyPress::leftKey = NSLeftArrowFunctionKey;
  1598. const int KeyPress::rightKey = NSRightArrowFunctionKey;
  1599. const int KeyPress::upKey = NSUpArrowFunctionKey;
  1600. const int KeyPress::downKey = NSDownArrowFunctionKey;
  1601. const int KeyPress::pageUpKey = NSPageUpFunctionKey;
  1602. const int KeyPress::pageDownKey = NSPageDownFunctionKey;
  1603. const int KeyPress::endKey = NSEndFunctionKey;
  1604. const int KeyPress::homeKey = NSHomeFunctionKey;
  1605. const int KeyPress::deleteKey = NSDeleteFunctionKey;
  1606. const int KeyPress::insertKey = -1;
  1607. const int KeyPress::tabKey = 9;
  1608. const int KeyPress::F1Key = NSF1FunctionKey;
  1609. const int KeyPress::F2Key = NSF2FunctionKey;
  1610. const int KeyPress::F3Key = NSF3FunctionKey;
  1611. const int KeyPress::F4Key = NSF4FunctionKey;
  1612. const int KeyPress::F5Key = NSF5FunctionKey;
  1613. const int KeyPress::F6Key = NSF6FunctionKey;
  1614. const int KeyPress::F7Key = NSF7FunctionKey;
  1615. const int KeyPress::F8Key = NSF8FunctionKey;
  1616. const int KeyPress::F9Key = NSF9FunctionKey;
  1617. const int KeyPress::F10Key = NSF10FunctionKey;
  1618. const int KeyPress::F11Key = NSF1FunctionKey;
  1619. const int KeyPress::F12Key = NSF12FunctionKey;
  1620. const int KeyPress::F13Key = NSF13FunctionKey;
  1621. const int KeyPress::F14Key = NSF14FunctionKey;
  1622. const int KeyPress::F15Key = NSF15FunctionKey;
  1623. const int KeyPress::F16Key = NSF16FunctionKey;
  1624. const int KeyPress::numberPad0 = 0x30020;
  1625. const int KeyPress::numberPad1 = 0x30021;
  1626. const int KeyPress::numberPad2 = 0x30022;
  1627. const int KeyPress::numberPad3 = 0x30023;
  1628. const int KeyPress::numberPad4 = 0x30024;
  1629. const int KeyPress::numberPad5 = 0x30025;
  1630. const int KeyPress::numberPad6 = 0x30026;
  1631. const int KeyPress::numberPad7 = 0x30027;
  1632. const int KeyPress::numberPad8 = 0x30028;
  1633. const int KeyPress::numberPad9 = 0x30029;
  1634. const int KeyPress::numberPadAdd = 0x3002a;
  1635. const int KeyPress::numberPadSubtract = 0x3002b;
  1636. const int KeyPress::numberPadMultiply = 0x3002c;
  1637. const int KeyPress::numberPadDivide = 0x3002d;
  1638. const int KeyPress::numberPadSeparator = 0x3002e;
  1639. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  1640. const int KeyPress::numberPadEquals = 0x30030;
  1641. const int KeyPress::numberPadDelete = 0x30031;
  1642. const int KeyPress::playKey = 0x30000;
  1643. const int KeyPress::stopKey = 0x30001;
  1644. const int KeyPress::fastForwardKey = 0x30002;
  1645. const int KeyPress::rewindKey = 0x30003;