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.

1982 lines
73KB

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