Audio plugin host https://kx.studio/carla
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.

962 lines
31KB

  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. class UIViewComponentPeer;
  18. namespace Orientations
  19. {
  20. static Desktop::DisplayOrientation convertToJuce (UIInterfaceOrientation orientation)
  21. {
  22. switch (orientation)
  23. {
  24. case UIInterfaceOrientationPortrait: return Desktop::upright;
  25. case UIInterfaceOrientationPortraitUpsideDown: return Desktop::upsideDown;
  26. case UIInterfaceOrientationLandscapeLeft: return Desktop::rotatedClockwise;
  27. case UIInterfaceOrientationLandscapeRight: return Desktop::rotatedAntiClockwise;
  28. default: jassertfalse; // unknown orientation!
  29. }
  30. return Desktop::upright;
  31. }
  32. static CGAffineTransform getCGTransformFor (const Desktop::DisplayOrientation orientation) noexcept
  33. {
  34. switch (orientation)
  35. {
  36. case Desktop::upsideDown: return CGAffineTransformMake (-1, 0, 0, -1, 0, 0);
  37. case Desktop::rotatedClockwise: return CGAffineTransformMake (0, -1, 1, 0, 0, 0);
  38. case Desktop::rotatedAntiClockwise: return CGAffineTransformMake (0, 1, -1, 0, 0, 0);
  39. default: break;
  40. }
  41. return CGAffineTransformIdentity;
  42. }
  43. static NSUInteger getSupportedOrientations()
  44. {
  45. NSUInteger allowed = 0;
  46. Desktop& d = Desktop::getInstance();
  47. if (d.isOrientationEnabled (Desktop::upright)) allowed |= UIInterfaceOrientationMaskPortrait;
  48. if (d.isOrientationEnabled (Desktop::upsideDown)) allowed |= UIInterfaceOrientationMaskPortraitUpsideDown;
  49. if (d.isOrientationEnabled (Desktop::rotatedClockwise)) allowed |= UIInterfaceOrientationMaskLandscapeLeft;
  50. if (d.isOrientationEnabled (Desktop::rotatedAntiClockwise)) allowed |= UIInterfaceOrientationMaskLandscapeRight;
  51. return allowed;
  52. }
  53. }
  54. //==============================================================================
  55. } // (juce namespace)
  56. using namespace juce;
  57. @interface JuceUIView : UIView <UITextViewDelegate>
  58. {
  59. @public
  60. UIViewComponentPeer* owner;
  61. UITextView* hiddenTextView;
  62. }
  63. - (JuceUIView*) initWithOwner: (UIViewComponentPeer*) owner withFrame: (CGRect) frame;
  64. - (void) dealloc;
  65. - (void) drawRect: (CGRect) r;
  66. - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event;
  67. - (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event;
  68. - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event;
  69. - (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event;
  70. - (BOOL) becomeFirstResponder;
  71. - (BOOL) resignFirstResponder;
  72. - (BOOL) canBecomeFirstResponder;
  73. - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text;
  74. @end
  75. //==============================================================================
  76. @interface JuceUIViewController : UIViewController
  77. {
  78. }
  79. - (NSUInteger) supportedInterfaceOrientations;
  80. - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation;
  81. - (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration;
  82. - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation;
  83. @end
  84. //==============================================================================
  85. @interface JuceUIWindow : UIWindow
  86. {
  87. @private
  88. UIViewComponentPeer* owner;
  89. bool isZooming;
  90. }
  91. - (void) setOwner: (UIViewComponentPeer*) owner;
  92. - (void) becomeKeyWindow;
  93. @end
  94. //==============================================================================
  95. //==============================================================================
  96. namespace juce
  97. {
  98. class UIViewComponentPeer : public ComponentPeer,
  99. public FocusChangeListener
  100. {
  101. public:
  102. UIViewComponentPeer (Component&, int windowStyleFlags, UIView* viewToAttachTo);
  103. ~UIViewComponentPeer();
  104. //==============================================================================
  105. void* getNativeHandle() const override { return view; }
  106. void setVisible (bool shouldBeVisible) override;
  107. void setTitle (const String& title) override;
  108. void setBounds (const Rectangle<int>&, bool isNowFullScreen) override;
  109. Rectangle<int> getBounds() const override { return getBounds (! isSharedWindow); }
  110. Rectangle<int> getBounds (bool global) const;
  111. Point<int> localToGlobal (Point<int> relativePosition) override;
  112. Point<int> globalToLocal (Point<int> screenPosition) override;
  113. void setAlpha (float newAlpha) override;
  114. void setMinimised (bool) override {}
  115. bool isMinimised() const override { return false; }
  116. void setFullScreen (bool shouldBeFullScreen) override;
  117. bool isFullScreen() const override { return fullScreen; }
  118. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override;
  119. BorderSize<int> getFrameSize() const override { return BorderSize<int>(); }
  120. bool setAlwaysOnTop (bool alwaysOnTop) override;
  121. void toFront (bool makeActiveWindow) override;
  122. void toBehind (ComponentPeer* other) override;
  123. void setIcon (const Image& newIcon) override;
  124. StringArray getAvailableRenderingEngines() override { return StringArray ("CoreGraphics Renderer"); }
  125. void drawRect (CGRect);
  126. bool canBecomeKeyWindow();
  127. //==============================================================================
  128. void viewFocusGain();
  129. void viewFocusLoss();
  130. bool isFocused() const override;
  131. void grabFocus() override;
  132. void textInputRequired (const Point<int>&) override;
  133. BOOL textViewReplaceCharacters (Range<int>, const String&);
  134. void updateHiddenTextContent (TextInputTarget*);
  135. void globalFocusChanged (Component*) override;
  136. void updateTransformAndScreenBounds();
  137. void handleTouches (UIEvent*, bool isDown, bool isUp, bool isCancel);
  138. //==============================================================================
  139. void repaint (const Rectangle<int>& area) override;
  140. void performAnyPendingRepaintsNow() override;
  141. //==============================================================================
  142. UIWindow* window;
  143. JuceUIView* view;
  144. JuceUIViewController* controller;
  145. bool isSharedWindow, fullScreen, insideDrawRect;
  146. static ModifierKeys currentModifiers;
  147. static int64 getMouseTime (UIEvent* e)
  148. {
  149. return (Time::currentTimeMillis() - Time::getMillisecondCounter())
  150. + (int64) ([e timestamp] * 1000.0);
  151. }
  152. static Rectangle<int> rotatedScreenPosToReal (const Rectangle<int>& r)
  153. {
  154. const Rectangle<int> screen (convertToRectInt ([UIScreen mainScreen].bounds));
  155. switch ([[UIApplication sharedApplication] statusBarOrientation])
  156. {
  157. case UIInterfaceOrientationPortrait:
  158. return r;
  159. case UIInterfaceOrientationPortraitUpsideDown:
  160. return Rectangle<int> (screen.getWidth() - r.getRight(), screen.getHeight() - r.getBottom(),
  161. r.getWidth(), r.getHeight());
  162. case UIInterfaceOrientationLandscapeLeft:
  163. return Rectangle<int> (r.getY(), screen.getHeight() - r.getRight(),
  164. r.getHeight(), r.getWidth());
  165. case UIInterfaceOrientationLandscapeRight:
  166. return Rectangle<int> (screen.getWidth() - r.getBottom(), r.getX(),
  167. r.getHeight(), r.getWidth());
  168. default: jassertfalse; // unknown orientation!
  169. }
  170. return r;
  171. }
  172. static Rectangle<int> realScreenPosToRotated (const Rectangle<int>& r)
  173. {
  174. const Rectangle<int> screen (convertToRectInt ([UIScreen mainScreen].bounds));
  175. switch ([[UIApplication sharedApplication] statusBarOrientation])
  176. {
  177. case UIInterfaceOrientationPortrait:
  178. return r;
  179. case UIInterfaceOrientationPortraitUpsideDown:
  180. return Rectangle<int> (screen.getWidth() - r.getRight(), screen.getHeight() - r.getBottom(),
  181. r.getWidth(), r.getHeight());
  182. case UIInterfaceOrientationLandscapeLeft:
  183. return Rectangle<int> (screen.getHeight() - r.getBottom(), r.getX(),
  184. r.getHeight(), r.getWidth());
  185. case UIInterfaceOrientationLandscapeRight:
  186. return Rectangle<int> (r.getY(), screen.getWidth() - r.getRight(),
  187. r.getHeight(), r.getWidth());
  188. default: jassertfalse; // unknown orientation!
  189. }
  190. return r;
  191. }
  192. MultiTouchMapper<UITouch*> currentTouches;
  193. private:
  194. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIViewComponentPeer)
  195. class AsyncRepaintMessage : public CallbackMessage
  196. {
  197. public:
  198. UIViewComponentPeer* const peer;
  199. const Rectangle<int> rect;
  200. AsyncRepaintMessage (UIViewComponentPeer* const p, const Rectangle<int>& r)
  201. : peer (p), rect (r)
  202. {
  203. }
  204. void messageCallback() override
  205. {
  206. if (ComponentPeer::isValidPeer (peer))
  207. peer->repaint (rect);
  208. }
  209. };
  210. };
  211. } // (juce namespace)
  212. //==============================================================================
  213. //==============================================================================
  214. @implementation JuceUIViewController
  215. - (NSUInteger) supportedInterfaceOrientations
  216. {
  217. return Orientations::getSupportedOrientations();
  218. }
  219. - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
  220. {
  221. return Desktop::getInstance().isOrientationEnabled (Orientations::convertToJuce (interfaceOrientation));
  222. }
  223. - (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
  224. duration: (NSTimeInterval) duration
  225. {
  226. [UIView setAnimationsEnabled: NO]; // disable this because it goes the wrong way and looks like crap.
  227. }
  228. - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
  229. {
  230. JuceUIView* juceView = (JuceUIView*) [self view];
  231. jassert (juceView != nil && juceView->owner != nullptr);
  232. juceView->owner->updateTransformAndScreenBounds();
  233. [UIView setAnimationsEnabled: YES];
  234. }
  235. - (void) viewDidLoad
  236. {
  237. JuceUIView* juceView = (JuceUIView*) [self view];
  238. jassert (juceView != nil && juceView->owner != nullptr);
  239. juceView->owner->updateTransformAndScreenBounds();
  240. }
  241. - (void) viewWillAppear: (BOOL) animated
  242. {
  243. [self viewDidLoad];
  244. }
  245. - (void) viewDidAppear: (BOOL) animated
  246. {
  247. [self viewDidLoad];
  248. }
  249. @end
  250. @implementation JuceUIView
  251. - (JuceUIView*) initWithOwner: (UIViewComponentPeer*) peer
  252. withFrame: (CGRect) frame
  253. {
  254. [super initWithFrame: frame];
  255. owner = peer;
  256. hiddenTextView = [[UITextView alloc] initWithFrame: CGRectZero];
  257. [self addSubview: hiddenTextView];
  258. hiddenTextView.delegate = self;
  259. hiddenTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  260. hiddenTextView.autocorrectionType = UITextAutocorrectionTypeNo;
  261. return self;
  262. }
  263. - (void) dealloc
  264. {
  265. [hiddenTextView removeFromSuperview];
  266. [hiddenTextView release];
  267. [super dealloc];
  268. }
  269. //==============================================================================
  270. - (void) drawRect: (CGRect) r
  271. {
  272. if (owner != nullptr)
  273. owner->drawRect (r);
  274. }
  275. //==============================================================================
  276. - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
  277. {
  278. if (owner != nullptr)
  279. owner->handleTouches (event, true, false, false);
  280. }
  281. - (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event
  282. {
  283. if (owner != nullptr)
  284. owner->handleTouches (event, false, false, false);
  285. }
  286. - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
  287. {
  288. if (owner != nullptr)
  289. owner->handleTouches (event, false, true, false);
  290. }
  291. - (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event
  292. {
  293. if (owner != nullptr)
  294. owner->handleTouches (event, false, true, true);
  295. [self touchesEnded: touches withEvent: event];
  296. }
  297. //==============================================================================
  298. - (BOOL) becomeFirstResponder
  299. {
  300. if (owner != nullptr)
  301. owner->viewFocusGain();
  302. return true;
  303. }
  304. - (BOOL) resignFirstResponder
  305. {
  306. if (owner != nullptr)
  307. owner->viewFocusLoss();
  308. return true;
  309. }
  310. - (BOOL) canBecomeFirstResponder
  311. {
  312. return owner != nullptr && owner->canBecomeKeyWindow();
  313. }
  314. - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text
  315. {
  316. return owner->textViewReplaceCharacters (Range<int> (range.location, range.location + range.length),
  317. nsStringToJuce (text));
  318. }
  319. @end
  320. //==============================================================================
  321. @implementation JuceUIWindow
  322. - (void) setOwner: (UIViewComponentPeer*) peer
  323. {
  324. owner = peer;
  325. isZooming = false;
  326. }
  327. - (void) becomeKeyWindow
  328. {
  329. [super becomeKeyWindow];
  330. if (owner != nullptr)
  331. owner->grabFocus();
  332. }
  333. @end
  334. //==============================================================================
  335. //==============================================================================
  336. namespace juce
  337. {
  338. bool KeyPress::isKeyCurrentlyDown (const int keyCode)
  339. {
  340. return false;
  341. }
  342. ModifierKeys UIViewComponentPeer::currentModifiers;
  343. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  344. {
  345. return UIViewComponentPeer::currentModifiers;
  346. }
  347. void ModifierKeys::updateCurrentModifiers() noexcept
  348. {
  349. currentModifiers = UIViewComponentPeer::currentModifiers;
  350. }
  351. Point<int> juce_lastMousePos;
  352. //==============================================================================
  353. UIViewComponentPeer::UIViewComponentPeer (Component& comp, const int windowStyleFlags, UIView* viewToAttachTo)
  354. : ComponentPeer (comp, windowStyleFlags),
  355. window (nil),
  356. view (nil),
  357. controller (nil),
  358. isSharedWindow (viewToAttachTo != nil),
  359. fullScreen (false),
  360. insideDrawRect (false)
  361. {
  362. CGRect r = convertToCGRect (component.getBounds());
  363. view = [[JuceUIView alloc] initWithOwner: this withFrame: r];
  364. view.multipleTouchEnabled = YES;
  365. view.hidden = ! component.isVisible();
  366. view.opaque = component.isOpaque();
  367. view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
  368. view.transform = CGAffineTransformIdentity;
  369. if (isSharedWindow)
  370. {
  371. window = [viewToAttachTo window];
  372. [viewToAttachTo addSubview: view];
  373. }
  374. else
  375. {
  376. controller = [[JuceUIViewController alloc] init];
  377. controller.view = view;
  378. r = convertToCGRect (rotatedScreenPosToReal (component.getBounds()));
  379. r.origin.y = [UIScreen mainScreen].bounds.size.height - (r.origin.y + r.size.height);
  380. window = [[JuceUIWindow alloc] init];
  381. window.autoresizesSubviews = NO;
  382. window.transform = CGAffineTransformIdentity;
  383. window.frame = r;
  384. window.opaque = component.isOpaque();
  385. window.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
  386. [((JuceUIWindow*) window) setOwner: this];
  387. if (component.isAlwaysOnTop())
  388. window.windowLevel = UIWindowLevelAlert;
  389. view.frame = CGRectMake (0, 0, r.size.width, r.size.height);
  390. window.rootViewController = controller;
  391. [window addSubview: view];
  392. window.hidden = view.hidden;
  393. }
  394. setTitle (component.getName());
  395. Desktop::getInstance().addFocusChangeListener (this);
  396. }
  397. UIViewComponentPeer::~UIViewComponentPeer()
  398. {
  399. Desktop::getInstance().removeFocusChangeListener (this);
  400. view->owner = nullptr;
  401. [view removeFromSuperview];
  402. [view release];
  403. [controller release];
  404. if (! isSharedWindow)
  405. {
  406. [((JuceUIWindow*) window) setOwner: nil];
  407. [window release];
  408. }
  409. }
  410. //==============================================================================
  411. void UIViewComponentPeer::setVisible (bool shouldBeVisible)
  412. {
  413. view.hidden = ! shouldBeVisible;
  414. if (! isSharedWindow)
  415. window.hidden = ! shouldBeVisible;
  416. }
  417. void UIViewComponentPeer::setTitle (const String& title)
  418. {
  419. // xxx is this possible?
  420. }
  421. void UIViewComponentPeer::setBounds (const Rectangle<int>& newBounds, const bool isNowFullScreen)
  422. {
  423. fullScreen = isNowFullScreen;
  424. if (isSharedWindow)
  425. {
  426. CGRect r = convertToCGRect (newBounds);
  427. if (view.frame.size.width != r.size.width || view.frame.size.height != r.size.height)
  428. [view setNeedsDisplay];
  429. view.frame = r;
  430. }
  431. else
  432. {
  433. window.frame = convertToCGRect (rotatedScreenPosToReal (newBounds));
  434. view.frame = CGRectMake (0, 0, (CGFloat) newBounds.getWidth(), (CGFloat) newBounds.getHeight());
  435. handleMovedOrResized();
  436. }
  437. }
  438. Rectangle<int> UIViewComponentPeer::getBounds (const bool global) const
  439. {
  440. CGRect r = view.frame;
  441. if (global && view.window != nil)
  442. {
  443. r = [view convertRect: r toView: view.window];
  444. r = [view.window convertRect: r toWindow: nil];
  445. return realScreenPosToRotated (convertToRectInt (r));
  446. }
  447. return convertToRectInt (r);
  448. }
  449. Point<int> UIViewComponentPeer::localToGlobal (Point<int> relativePosition)
  450. {
  451. return relativePosition + getBounds (true).getPosition();
  452. }
  453. Point<int> UIViewComponentPeer::globalToLocal (Point<int> screenPosition)
  454. {
  455. return screenPosition - getBounds (true).getPosition();
  456. }
  457. void UIViewComponentPeer::setAlpha (float newAlpha)
  458. {
  459. [view.window setAlpha: (CGFloat) newAlpha];
  460. }
  461. void UIViewComponentPeer::setFullScreen (bool shouldBeFullScreen)
  462. {
  463. if (! isSharedWindow)
  464. {
  465. Rectangle<int> r (shouldBeFullScreen ? Desktop::getInstance().getDisplays().getMainDisplay().userArea
  466. : lastNonFullscreenBounds);
  467. if ((! shouldBeFullScreen) && r.isEmpty())
  468. r = getBounds();
  469. // (can't call the component's setBounds method because that'll reset our fullscreen flag)
  470. if (! r.isEmpty())
  471. setBounds (r, shouldBeFullScreen);
  472. component.repaint();
  473. }
  474. }
  475. void UIViewComponentPeer::updateTransformAndScreenBounds()
  476. {
  477. Desktop& desktop = Desktop::getInstance();
  478. const Rectangle<int> oldArea (component.getBounds());
  479. const Rectangle<int> oldDesktop (desktop.getDisplays().getMainDisplay().userArea);
  480. const_cast <Desktop::Displays&> (desktop.getDisplays()).refresh();
  481. window.transform = Orientations::getCGTransformFor (desktop.getCurrentOrientation());
  482. view.transform = CGAffineTransformIdentity;
  483. if (fullScreen)
  484. {
  485. fullScreen = false;
  486. setFullScreen (true);
  487. }
  488. else if (! isSharedWindow)
  489. {
  490. // this will re-centre the window, but leave its size unchanged
  491. const float centreRelX = oldArea.getCentreX() / (float) oldDesktop.getWidth();
  492. const float centreRelY = oldArea.getCentreY() / (float) oldDesktop.getHeight();
  493. const Rectangle<int> newDesktop (desktop.getDisplays().getMainDisplay().userArea);
  494. const int x = ((int) (newDesktop.getWidth() * centreRelX)) - (oldArea.getWidth() / 2);
  495. const int y = ((int) (newDesktop.getHeight() * centreRelY)) - (oldArea.getHeight() / 2);
  496. setBounds (oldArea.withPosition (x, y), false);
  497. }
  498. [view setNeedsDisplay];
  499. }
  500. bool UIViewComponentPeer::contains (Point<int> localPos, bool trueIfInAChildWindow) const
  501. {
  502. if (! component.getLocalBounds().contains (localPos))
  503. return false;
  504. UIView* v = [view hitTest: convertToCGPoint (localPos)
  505. withEvent: nil];
  506. if (trueIfInAChildWindow)
  507. return v != nil;
  508. return v == view;
  509. }
  510. bool UIViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
  511. {
  512. if (! isSharedWindow)
  513. window.windowLevel = alwaysOnTop ? UIWindowLevelAlert : UIWindowLevelNormal;
  514. return true;
  515. }
  516. void UIViewComponentPeer::toFront (bool makeActiveWindow)
  517. {
  518. if (isSharedWindow)
  519. [[view superview] bringSubviewToFront: view];
  520. if (window != nil && component.isVisible())
  521. [window makeKeyAndVisible];
  522. }
  523. void UIViewComponentPeer::toBehind (ComponentPeer* other)
  524. {
  525. UIViewComponentPeer* const otherPeer = dynamic_cast <UIViewComponentPeer*> (other);
  526. jassert (otherPeer != nullptr); // wrong type of window?
  527. if (otherPeer != nullptr)
  528. {
  529. if (isSharedWindow)
  530. {
  531. [[view superview] insertSubview: view belowSubview: otherPeer->view];
  532. }
  533. else
  534. {
  535. // don't know how to do this
  536. }
  537. }
  538. }
  539. void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
  540. {
  541. // to do..
  542. }
  543. //==============================================================================
  544. void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
  545. {
  546. NSArray* touches = [[event touchesForView: view] allObjects];
  547. for (unsigned int i = 0; i < [touches count]; ++i)
  548. {
  549. UITouch* touch = [touches objectAtIndex: i];
  550. if ([touch phase] == UITouchPhaseStationary)
  551. continue;
  552. CGPoint p = [touch locationInView: view];
  553. const Point<int> pos ((int) p.x, (int) p.y);
  554. juce_lastMousePos = pos + getBounds (true).getPosition();
  555. const int64 time = getMouseTime (event);
  556. const int touchIndex = currentTouches.getIndexOfTouch (touch);
  557. ModifierKeys modsToSend (currentModifiers);
  558. if (isDown)
  559. {
  560. if ([touch phase] != UITouchPhaseBegan)
  561. continue;
  562. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  563. modsToSend = currentModifiers;
  564. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  565. handleMouseEvent (touchIndex, pos, modsToSend.withoutMouseButtons(), time);
  566. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  567. return;
  568. }
  569. else if (isUp)
  570. {
  571. if (! ([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled))
  572. continue;
  573. modsToSend = modsToSend.withoutMouseButtons();
  574. currentTouches.clearTouch (touchIndex);
  575. if (! currentTouches.areAnyTouchesActive())
  576. isCancel = true;
  577. }
  578. if (isCancel)
  579. {
  580. currentTouches.clearTouch (touchIndex);
  581. modsToSend = currentModifiers = currentModifiers.withoutMouseButtons();
  582. }
  583. handleMouseEvent (touchIndex, pos, modsToSend, time);
  584. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  585. return;
  586. if (isUp || isCancel)
  587. {
  588. handleMouseEvent (touchIndex, Point<int> (-1, -1), modsToSend, time);
  589. if (! isValidPeer (this))
  590. return;
  591. }
  592. }
  593. }
  594. //==============================================================================
  595. static UIViewComponentPeer* currentlyFocusedPeer = nullptr;
  596. void UIViewComponentPeer::viewFocusGain()
  597. {
  598. if (currentlyFocusedPeer != this)
  599. {
  600. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  601. currentlyFocusedPeer->handleFocusLoss();
  602. currentlyFocusedPeer = this;
  603. handleFocusGain();
  604. }
  605. }
  606. void UIViewComponentPeer::viewFocusLoss()
  607. {
  608. if (currentlyFocusedPeer == this)
  609. {
  610. currentlyFocusedPeer = nullptr;
  611. handleFocusLoss();
  612. }
  613. }
  614. bool UIViewComponentPeer::isFocused() const
  615. {
  616. return isSharedWindow ? this == currentlyFocusedPeer
  617. : (window != nil && [window isKeyWindow]);
  618. }
  619. void UIViewComponentPeer::grabFocus()
  620. {
  621. if (window != nil)
  622. {
  623. [window makeKeyWindow];
  624. viewFocusGain();
  625. }
  626. }
  627. void UIViewComponentPeer::textInputRequired (const Point<int>&)
  628. {
  629. }
  630. void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
  631. {
  632. view->hiddenTextView.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
  633. view->hiddenTextView.selectedRange = NSMakeRange (target->getHighlightedRegion().getStart(), 0);
  634. }
  635. BOOL UIViewComponentPeer::textViewReplaceCharacters (Range<int> range, const String& text)
  636. {
  637. if (TextInputTarget* const target = findCurrentTextInputTarget())
  638. {
  639. const Range<int> currentSelection (target->getHighlightedRegion());
  640. if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
  641. if (currentSelection.isEmpty())
  642. target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));
  643. if (text == "\r" || text == "\n" || text == "\r\n")
  644. handleKeyPress (KeyPress::returnKey, text[0]);
  645. else
  646. target->insertTextAtCaret (text);
  647. updateHiddenTextContent (target);
  648. }
  649. return NO;
  650. }
  651. void UIViewComponentPeer::globalFocusChanged (Component*)
  652. {
  653. if (TextInputTarget* const target = findCurrentTextInputTarget())
  654. {
  655. Component* comp = dynamic_cast<Component*> (target);
  656. Point<int> pos (component.getLocalPoint (comp, Point<int>()));
  657. view->hiddenTextView.frame = CGRectMake (pos.x, pos.y, 0, 0);
  658. updateHiddenTextContent (target);
  659. [view->hiddenTextView becomeFirstResponder];
  660. }
  661. else
  662. {
  663. [view->hiddenTextView resignFirstResponder];
  664. }
  665. }
  666. //==============================================================================
  667. void UIViewComponentPeer::drawRect (CGRect r)
  668. {
  669. if (r.size.width < 1.0f || r.size.height < 1.0f)
  670. return;
  671. CGContextRef cg = UIGraphicsGetCurrentContext();
  672. if (! component.isOpaque())
  673. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  674. CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, getComponent().getHeight()));
  675. CoreGraphicsContext g (cg, getComponent().getHeight(), [UIScreen mainScreen].scale);
  676. insideDrawRect = true;
  677. handlePaint (g);
  678. insideDrawRect = false;
  679. }
  680. bool UIViewComponentPeer::canBecomeKeyWindow()
  681. {
  682. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  683. }
  684. //==============================================================================
  685. void Desktop::setKioskComponent (Component* kioskModeComponent, bool enableOrDisable, bool allowMenusAndBars)
  686. {
  687. [[UIApplication sharedApplication] setStatusBarHidden: enableOrDisable
  688. withAnimation: UIStatusBarAnimationSlide];
  689. displays->refresh();
  690. if (ComponentPeer* const peer = kioskModeComponent->getPeer())
  691. peer->setFullScreen (enableOrDisable);
  692. }
  693. //==============================================================================
  694. void UIViewComponentPeer::repaint (const Rectangle<int>& area)
  695. {
  696. if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
  697. (new AsyncRepaintMessage (this, area))->post();
  698. else
  699. [view setNeedsDisplayInRect: convertToCGRect (area)];
  700. }
  701. void UIViewComponentPeer::performAnyPendingRepaintsNow()
  702. {
  703. }
  704. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  705. {
  706. return new UIViewComponentPeer (*this, styleFlags, (UIView*) windowToAttachTo);
  707. }
  708. //==============================================================================
  709. const int KeyPress::spaceKey = ' ';
  710. const int KeyPress::returnKey = 0x0d;
  711. const int KeyPress::escapeKey = 0x1b;
  712. const int KeyPress::backspaceKey = 0x7f;
  713. const int KeyPress::leftKey = 0x1000;
  714. const int KeyPress::rightKey = 0x1001;
  715. const int KeyPress::upKey = 0x1002;
  716. const int KeyPress::downKey = 0x1003;
  717. const int KeyPress::pageUpKey = 0x1004;
  718. const int KeyPress::pageDownKey = 0x1005;
  719. const int KeyPress::endKey = 0x1006;
  720. const int KeyPress::homeKey = 0x1007;
  721. const int KeyPress::deleteKey = 0x1008;
  722. const int KeyPress::insertKey = -1;
  723. const int KeyPress::tabKey = 9;
  724. const int KeyPress::F1Key = 0x2001;
  725. const int KeyPress::F2Key = 0x2002;
  726. const int KeyPress::F3Key = 0x2003;
  727. const int KeyPress::F4Key = 0x2004;
  728. const int KeyPress::F5Key = 0x2005;
  729. const int KeyPress::F6Key = 0x2006;
  730. const int KeyPress::F7Key = 0x2007;
  731. const int KeyPress::F8Key = 0x2008;
  732. const int KeyPress::F9Key = 0x2009;
  733. const int KeyPress::F10Key = 0x200a;
  734. const int KeyPress::F11Key = 0x200b;
  735. const int KeyPress::F12Key = 0x200c;
  736. const int KeyPress::F13Key = 0x200d;
  737. const int KeyPress::F14Key = 0x200e;
  738. const int KeyPress::F15Key = 0x200f;
  739. const int KeyPress::F16Key = 0x2010;
  740. const int KeyPress::numberPad0 = 0x30020;
  741. const int KeyPress::numberPad1 = 0x30021;
  742. const int KeyPress::numberPad2 = 0x30022;
  743. const int KeyPress::numberPad3 = 0x30023;
  744. const int KeyPress::numberPad4 = 0x30024;
  745. const int KeyPress::numberPad5 = 0x30025;
  746. const int KeyPress::numberPad6 = 0x30026;
  747. const int KeyPress::numberPad7 = 0x30027;
  748. const int KeyPress::numberPad8 = 0x30028;
  749. const int KeyPress::numberPad9 = 0x30029;
  750. const int KeyPress::numberPadAdd = 0x3002a;
  751. const int KeyPress::numberPadSubtract = 0x3002b;
  752. const int KeyPress::numberPadMultiply = 0x3002c;
  753. const int KeyPress::numberPadDivide = 0x3002d;
  754. const int KeyPress::numberPadSeparator = 0x3002e;
  755. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  756. const int KeyPress::numberPadEquals = 0x30030;
  757. const int KeyPress::numberPadDelete = 0x30031;
  758. const int KeyPress::playKey = 0x30000;
  759. const int KeyPress::stopKey = 0x30001;
  760. const int KeyPress::fastForwardKey = 0x30002;
  761. const int KeyPress::rewindKey = 0x30003;