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.

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