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.

977 lines
32KB

  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. (void) toInterfaceOrientation;
  227. (void) duration;
  228. [UIView setAnimationsEnabled: NO]; // disable this because it goes the wrong way and looks like crap.
  229. }
  230. - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
  231. {
  232. (void) fromInterfaceOrientation;
  233. JuceUIView* juceView = (JuceUIView*) [self view];
  234. jassert (juceView != nil && juceView->owner != nullptr);
  235. juceView->owner->updateTransformAndScreenBounds();
  236. [UIView setAnimationsEnabled: YES];
  237. }
  238. - (void) viewDidLoad
  239. {
  240. JuceUIView* juceView = (JuceUIView*) [self view];
  241. jassert (juceView != nil && juceView->owner != nullptr);
  242. juceView->owner->updateTransformAndScreenBounds();
  243. }
  244. - (void) viewWillAppear: (BOOL) animated
  245. {
  246. (void) animated;
  247. [self viewDidLoad];
  248. }
  249. - (void) viewDidAppear: (BOOL) animated
  250. {
  251. (void) animated;
  252. [self viewDidLoad];
  253. }
  254. @end
  255. @implementation JuceUIView
  256. - (JuceUIView*) initWithOwner: (UIViewComponentPeer*) peer
  257. withFrame: (CGRect) frame
  258. {
  259. [super initWithFrame: frame];
  260. owner = peer;
  261. hiddenTextView = [[UITextView alloc] initWithFrame: CGRectZero];
  262. [self addSubview: hiddenTextView];
  263. hiddenTextView.delegate = self;
  264. hiddenTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
  265. hiddenTextView.autocorrectionType = UITextAutocorrectionTypeNo;
  266. return self;
  267. }
  268. - (void) dealloc
  269. {
  270. [hiddenTextView removeFromSuperview];
  271. [hiddenTextView release];
  272. [super dealloc];
  273. }
  274. //==============================================================================
  275. - (void) drawRect: (CGRect) r
  276. {
  277. if (owner != nullptr)
  278. owner->drawRect (r);
  279. }
  280. //==============================================================================
  281. - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
  282. {
  283. (void) touches;
  284. if (owner != nullptr)
  285. owner->handleTouches (event, true, false, false);
  286. }
  287. - (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event
  288. {
  289. (void) touches;
  290. if (owner != nullptr)
  291. owner->handleTouches (event, false, false, false);
  292. }
  293. - (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
  294. {
  295. (void) touches;
  296. if (owner != nullptr)
  297. owner->handleTouches (event, false, true, false);
  298. }
  299. - (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event
  300. {
  301. if (owner != nullptr)
  302. owner->handleTouches (event, false, true, true);
  303. [self touchesEnded: touches withEvent: event];
  304. }
  305. //==============================================================================
  306. - (BOOL) becomeFirstResponder
  307. {
  308. if (owner != nullptr)
  309. owner->viewFocusGain();
  310. return true;
  311. }
  312. - (BOOL) resignFirstResponder
  313. {
  314. if (owner != nullptr)
  315. owner->viewFocusLoss();
  316. return true;
  317. }
  318. - (BOOL) canBecomeFirstResponder
  319. {
  320. return owner != nullptr && owner->canBecomeKeyWindow();
  321. }
  322. - (BOOL) textView: (UITextView*) textView shouldChangeTextInRange: (NSRange) range replacementText: (NSString*) text
  323. {
  324. (void) textView;
  325. return owner->textViewReplaceCharacters (Range<int> ((int) range.location, (int) (range.location + range.length)),
  326. nsStringToJuce (text));
  327. }
  328. @end
  329. //==============================================================================
  330. @implementation JuceUIWindow
  331. - (void) setOwner: (UIViewComponentPeer*) peer
  332. {
  333. owner = peer;
  334. isZooming = false;
  335. }
  336. - (void) becomeKeyWindow
  337. {
  338. [super becomeKeyWindow];
  339. if (owner != nullptr)
  340. owner->grabFocus();
  341. }
  342. @end
  343. //==============================================================================
  344. //==============================================================================
  345. namespace juce
  346. {
  347. bool KeyPress::isKeyCurrentlyDown (int)
  348. {
  349. return false;
  350. }
  351. ModifierKeys UIViewComponentPeer::currentModifiers;
  352. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  353. {
  354. return UIViewComponentPeer::currentModifiers;
  355. }
  356. void ModifierKeys::updateCurrentModifiers() noexcept
  357. {
  358. currentModifiers = UIViewComponentPeer::currentModifiers;
  359. }
  360. Point<int> juce_lastMousePos;
  361. //==============================================================================
  362. UIViewComponentPeer::UIViewComponentPeer (Component& comp, const int windowStyleFlags, UIView* viewToAttachTo)
  363. : ComponentPeer (comp, windowStyleFlags),
  364. window (nil),
  365. view (nil),
  366. controller (nil),
  367. isSharedWindow (viewToAttachTo != nil),
  368. fullScreen (false),
  369. insideDrawRect (false)
  370. {
  371. CGRect r = convertToCGRect (component.getBounds());
  372. view = [[JuceUIView alloc] initWithOwner: this withFrame: r];
  373. view.multipleTouchEnabled = YES;
  374. view.hidden = ! component.isVisible();
  375. view.opaque = component.isOpaque();
  376. view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
  377. view.transform = CGAffineTransformIdentity;
  378. if (isSharedWindow)
  379. {
  380. window = [viewToAttachTo window];
  381. [viewToAttachTo addSubview: view];
  382. }
  383. else
  384. {
  385. controller = [[JuceUIViewController alloc] init];
  386. controller.view = view;
  387. r = convertToCGRect (rotatedScreenPosToReal (component.getBounds()));
  388. r.origin.y = [UIScreen mainScreen].bounds.size.height - (r.origin.y + r.size.height);
  389. window = [[JuceUIWindow alloc] init];
  390. window.hidden = true;
  391. window.autoresizesSubviews = NO;
  392. window.transform = CGAffineTransformIdentity;
  393. window.frame = r;
  394. window.opaque = component.isOpaque();
  395. window.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
  396. [((JuceUIWindow*) window) setOwner: this];
  397. if (component.isAlwaysOnTop())
  398. window.windowLevel = UIWindowLevelAlert;
  399. view.frame = CGRectMake (0, 0, r.size.width, r.size.height);
  400. window.rootViewController = controller;
  401. [window addSubview: view];
  402. window.hidden = view.hidden;
  403. }
  404. setTitle (component.getName());
  405. Desktop::getInstance().addFocusChangeListener (this);
  406. }
  407. UIViewComponentPeer::~UIViewComponentPeer()
  408. {
  409. Desktop::getInstance().removeFocusChangeListener (this);
  410. view->owner = nullptr;
  411. [view removeFromSuperview];
  412. [view release];
  413. [controller release];
  414. if (! isSharedWindow)
  415. {
  416. [((JuceUIWindow*) window) setOwner: nil];
  417. [window release];
  418. }
  419. }
  420. //==============================================================================
  421. void UIViewComponentPeer::setVisible (bool shouldBeVisible)
  422. {
  423. view.hidden = ! shouldBeVisible;
  424. if (! isSharedWindow)
  425. window.hidden = ! shouldBeVisible;
  426. }
  427. void UIViewComponentPeer::setTitle (const String&)
  428. {
  429. // xxx is this possible?
  430. }
  431. void UIViewComponentPeer::setBounds (const Rectangle<int>& newBounds, const bool isNowFullScreen)
  432. {
  433. fullScreen = isNowFullScreen;
  434. if (isSharedWindow)
  435. {
  436. CGRect r = convertToCGRect (newBounds);
  437. if (view.frame.size.width != r.size.width || view.frame.size.height != r.size.height)
  438. [view setNeedsDisplay];
  439. view.frame = r;
  440. }
  441. else
  442. {
  443. window.frame = convertToCGRect (rotatedScreenPosToReal (newBounds));
  444. view.frame = CGRectMake (0, 0, (CGFloat) newBounds.getWidth(), (CGFloat) newBounds.getHeight());
  445. handleMovedOrResized();
  446. }
  447. }
  448. Rectangle<int> UIViewComponentPeer::getBounds (const bool global) const
  449. {
  450. CGRect r = view.frame;
  451. if (global && view.window != nil)
  452. {
  453. r = [view convertRect: r toView: view.window];
  454. r = [view.window convertRect: r toWindow: nil];
  455. return realScreenPosToRotated (convertToRectInt (r));
  456. }
  457. return convertToRectInt (r);
  458. }
  459. Point<int> UIViewComponentPeer::localToGlobal (Point<int> relativePosition)
  460. {
  461. return relativePosition + getBounds (true).getPosition();
  462. }
  463. Point<int> UIViewComponentPeer::globalToLocal (Point<int> screenPosition)
  464. {
  465. return screenPosition - getBounds (true).getPosition();
  466. }
  467. void UIViewComponentPeer::setAlpha (float newAlpha)
  468. {
  469. [view.window setAlpha: (CGFloat) newAlpha];
  470. }
  471. void UIViewComponentPeer::setFullScreen (bool shouldBeFullScreen)
  472. {
  473. if (! isSharedWindow)
  474. {
  475. Rectangle<int> r (shouldBeFullScreen ? Desktop::getInstance().getDisplays().getMainDisplay().userArea
  476. : lastNonFullscreenBounds);
  477. if ((! shouldBeFullScreen) && r.isEmpty())
  478. r = getBounds();
  479. // (can't call the component's setBounds method because that'll reset our fullscreen flag)
  480. if (! r.isEmpty())
  481. setBounds (r, shouldBeFullScreen);
  482. component.repaint();
  483. }
  484. }
  485. void UIViewComponentPeer::updateTransformAndScreenBounds()
  486. {
  487. Desktop& desktop = Desktop::getInstance();
  488. const Rectangle<int> oldArea (component.getBounds());
  489. const Rectangle<int> oldDesktop (desktop.getDisplays().getMainDisplay().userArea);
  490. const_cast <Desktop::Displays&> (desktop.getDisplays()).refresh();
  491. window.transform = Orientations::getCGTransformFor (desktop.getCurrentOrientation());
  492. view.transform = CGAffineTransformIdentity;
  493. if (fullScreen)
  494. {
  495. fullScreen = false;
  496. setFullScreen (true);
  497. }
  498. else if (! isSharedWindow)
  499. {
  500. // this will re-centre the window, but leave its size unchanged
  501. const float centreRelX = oldArea.getCentreX() / (float) oldDesktop.getWidth();
  502. const float centreRelY = oldArea.getCentreY() / (float) oldDesktop.getHeight();
  503. const Rectangle<int> newDesktop (desktop.getDisplays().getMainDisplay().userArea);
  504. const int x = ((int) (newDesktop.getWidth() * centreRelX)) - (oldArea.getWidth() / 2);
  505. const int y = ((int) (newDesktop.getHeight() * centreRelY)) - (oldArea.getHeight() / 2);
  506. setBounds (oldArea.withPosition (x, y), false);
  507. }
  508. [view setNeedsDisplay];
  509. }
  510. bool UIViewComponentPeer::contains (Point<int> localPos, bool trueIfInAChildWindow) const
  511. {
  512. if (! component.getLocalBounds().contains (localPos))
  513. return false;
  514. UIView* v = [view hitTest: convertToCGPoint (localPos)
  515. withEvent: nil];
  516. if (trueIfInAChildWindow)
  517. return v != nil;
  518. return v == view;
  519. }
  520. bool UIViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
  521. {
  522. if (! isSharedWindow)
  523. window.windowLevel = alwaysOnTop ? UIWindowLevelAlert : UIWindowLevelNormal;
  524. return true;
  525. }
  526. void UIViewComponentPeer::toFront (bool makeActiveWindow)
  527. {
  528. if (isSharedWindow)
  529. [[view superview] bringSubviewToFront: view];
  530. if (makeActiveWindow && window != nil && component.isVisible())
  531. [window makeKeyAndVisible];
  532. }
  533. void UIViewComponentPeer::toBehind (ComponentPeer* other)
  534. {
  535. UIViewComponentPeer* const otherPeer = dynamic_cast <UIViewComponentPeer*> (other);
  536. jassert (otherPeer != nullptr); // wrong type of window?
  537. if (otherPeer != nullptr)
  538. {
  539. if (isSharedWindow)
  540. {
  541. [[view superview] insertSubview: view belowSubview: otherPeer->view];
  542. }
  543. else
  544. {
  545. // don't know how to do this
  546. }
  547. }
  548. }
  549. void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
  550. {
  551. // to do..
  552. }
  553. //==============================================================================
  554. void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
  555. {
  556. NSArray* touches = [[event touchesForView: view] allObjects];
  557. for (unsigned int i = 0; i < [touches count]; ++i)
  558. {
  559. UITouch* touch = [touches objectAtIndex: i];
  560. if ([touch phase] == UITouchPhaseStationary)
  561. continue;
  562. CGPoint p = [touch locationInView: view];
  563. const Point<int> pos ((int) p.x, (int) p.y);
  564. juce_lastMousePos = pos + getBounds (true).getPosition();
  565. const int64 time = getMouseTime (event);
  566. const int touchIndex = currentTouches.getIndexOfTouch (touch);
  567. ModifierKeys modsToSend (currentModifiers);
  568. if (isDown)
  569. {
  570. if ([touch phase] != UITouchPhaseBegan)
  571. continue;
  572. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  573. modsToSend = currentModifiers;
  574. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  575. handleMouseEvent (touchIndex, pos, modsToSend.withoutMouseButtons(), time);
  576. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  577. return;
  578. }
  579. else if (isUp)
  580. {
  581. if (! ([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled))
  582. continue;
  583. modsToSend = modsToSend.withoutMouseButtons();
  584. currentTouches.clearTouch (touchIndex);
  585. if (! currentTouches.areAnyTouchesActive())
  586. isCancel = true;
  587. }
  588. if (isCancel)
  589. {
  590. currentTouches.clearTouch (touchIndex);
  591. modsToSend = currentModifiers = currentModifiers.withoutMouseButtons();
  592. }
  593. handleMouseEvent (touchIndex, pos, modsToSend, time);
  594. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  595. return;
  596. if (isUp || isCancel)
  597. {
  598. handleMouseEvent (touchIndex, Point<int> (-1, -1), modsToSend, time);
  599. if (! isValidPeer (this))
  600. return;
  601. }
  602. }
  603. }
  604. //==============================================================================
  605. static UIViewComponentPeer* currentlyFocusedPeer = nullptr;
  606. void UIViewComponentPeer::viewFocusGain()
  607. {
  608. if (currentlyFocusedPeer != this)
  609. {
  610. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  611. currentlyFocusedPeer->handleFocusLoss();
  612. currentlyFocusedPeer = this;
  613. handleFocusGain();
  614. }
  615. }
  616. void UIViewComponentPeer::viewFocusLoss()
  617. {
  618. if (currentlyFocusedPeer == this)
  619. {
  620. currentlyFocusedPeer = nullptr;
  621. handleFocusLoss();
  622. }
  623. }
  624. bool UIViewComponentPeer::isFocused() const
  625. {
  626. return isSharedWindow ? this == currentlyFocusedPeer
  627. : (window != nil && [window isKeyWindow]);
  628. }
  629. void UIViewComponentPeer::grabFocus()
  630. {
  631. if (window != nil)
  632. {
  633. [window makeKeyWindow];
  634. viewFocusGain();
  635. }
  636. }
  637. void UIViewComponentPeer::textInputRequired (const Point<int>&)
  638. {
  639. }
  640. void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
  641. {
  642. view->hiddenTextView.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
  643. view->hiddenTextView.selectedRange = NSMakeRange (target->getHighlightedRegion().getStart(), 0);
  644. }
  645. BOOL UIViewComponentPeer::textViewReplaceCharacters (Range<int> range, const String& text)
  646. {
  647. if (TextInputTarget* const target = findCurrentTextInputTarget())
  648. {
  649. const Range<int> currentSelection (target->getHighlightedRegion());
  650. if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
  651. if (currentSelection.isEmpty())
  652. target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));
  653. if (text == "\r" || text == "\n" || text == "\r\n")
  654. handleKeyPress (KeyPress::returnKey, text[0]);
  655. else
  656. target->insertTextAtCaret (text);
  657. updateHiddenTextContent (target);
  658. }
  659. return NO;
  660. }
  661. void UIViewComponentPeer::globalFocusChanged (Component*)
  662. {
  663. if (TextInputTarget* const target = findCurrentTextInputTarget())
  664. {
  665. Component* comp = dynamic_cast<Component*> (target);
  666. Point<int> pos (component.getLocalPoint (comp, Point<int>()));
  667. view->hiddenTextView.frame = CGRectMake (pos.x, pos.y, 0, 0);
  668. updateHiddenTextContent (target);
  669. [view->hiddenTextView becomeFirstResponder];
  670. }
  671. else
  672. {
  673. [view->hiddenTextView resignFirstResponder];
  674. }
  675. }
  676. //==============================================================================
  677. void UIViewComponentPeer::drawRect (CGRect r)
  678. {
  679. if (r.size.width < 1.0f || r.size.height < 1.0f)
  680. return;
  681. CGContextRef cg = UIGraphicsGetCurrentContext();
  682. if (! component.isOpaque())
  683. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  684. CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, getComponent().getHeight()));
  685. CoreGraphicsContext g (cg, getComponent().getHeight(), [UIScreen mainScreen].scale);
  686. insideDrawRect = true;
  687. handlePaint (g);
  688. insideDrawRect = false;
  689. }
  690. bool UIViewComponentPeer::canBecomeKeyWindow()
  691. {
  692. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  693. }
  694. //==============================================================================
  695. void Desktop::setKioskComponent (Component* kioskModeComp, bool enableOrDisable, bool /*allowMenusAndBars*/)
  696. {
  697. [[UIApplication sharedApplication] setStatusBarHidden: enableOrDisable
  698. withAnimation: UIStatusBarAnimationSlide];
  699. displays->refresh();
  700. if (ComponentPeer* const peer = kioskModeComp->getPeer())
  701. peer->setFullScreen (enableOrDisable);
  702. }
  703. //==============================================================================
  704. void UIViewComponentPeer::repaint (const Rectangle<int>& area)
  705. {
  706. if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
  707. (new AsyncRepaintMessage (this, area))->post();
  708. else
  709. [view setNeedsDisplayInRect: convertToCGRect (area)];
  710. }
  711. void UIViewComponentPeer::performAnyPendingRepaintsNow()
  712. {
  713. }
  714. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  715. {
  716. return new UIViewComponentPeer (*this, styleFlags, (UIView*) windowToAttachTo);
  717. }
  718. //==============================================================================
  719. const int KeyPress::spaceKey = ' ';
  720. const int KeyPress::returnKey = 0x0d;
  721. const int KeyPress::escapeKey = 0x1b;
  722. const int KeyPress::backspaceKey = 0x7f;
  723. const int KeyPress::leftKey = 0x1000;
  724. const int KeyPress::rightKey = 0x1001;
  725. const int KeyPress::upKey = 0x1002;
  726. const int KeyPress::downKey = 0x1003;
  727. const int KeyPress::pageUpKey = 0x1004;
  728. const int KeyPress::pageDownKey = 0x1005;
  729. const int KeyPress::endKey = 0x1006;
  730. const int KeyPress::homeKey = 0x1007;
  731. const int KeyPress::deleteKey = 0x1008;
  732. const int KeyPress::insertKey = -1;
  733. const int KeyPress::tabKey = 9;
  734. const int KeyPress::F1Key = 0x2001;
  735. const int KeyPress::F2Key = 0x2002;
  736. const int KeyPress::F3Key = 0x2003;
  737. const int KeyPress::F4Key = 0x2004;
  738. const int KeyPress::F5Key = 0x2005;
  739. const int KeyPress::F6Key = 0x2006;
  740. const int KeyPress::F7Key = 0x2007;
  741. const int KeyPress::F8Key = 0x2008;
  742. const int KeyPress::F9Key = 0x2009;
  743. const int KeyPress::F10Key = 0x200a;
  744. const int KeyPress::F11Key = 0x200b;
  745. const int KeyPress::F12Key = 0x200c;
  746. const int KeyPress::F13Key = 0x200d;
  747. const int KeyPress::F14Key = 0x200e;
  748. const int KeyPress::F15Key = 0x200f;
  749. const int KeyPress::F16Key = 0x2010;
  750. const int KeyPress::numberPad0 = 0x30020;
  751. const int KeyPress::numberPad1 = 0x30021;
  752. const int KeyPress::numberPad2 = 0x30022;
  753. const int KeyPress::numberPad3 = 0x30023;
  754. const int KeyPress::numberPad4 = 0x30024;
  755. const int KeyPress::numberPad5 = 0x30025;
  756. const int KeyPress::numberPad6 = 0x30026;
  757. const int KeyPress::numberPad7 = 0x30027;
  758. const int KeyPress::numberPad8 = 0x30028;
  759. const int KeyPress::numberPad9 = 0x30029;
  760. const int KeyPress::numberPadAdd = 0x3002a;
  761. const int KeyPress::numberPadSubtract = 0x3002b;
  762. const int KeyPress::numberPadMultiply = 0x3002c;
  763. const int KeyPress::numberPadDivide = 0x3002d;
  764. const int KeyPress::numberPadSeparator = 0x3002e;
  765. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  766. const int KeyPress::numberPadEquals = 0x30030;
  767. const int KeyPress::numberPadDelete = 0x30031;
  768. const int KeyPress::playKey = 0x30000;
  769. const int KeyPress::stopKey = 0x30001;
  770. const int KeyPress::fastForwardKey = 0x30002;
  771. const int KeyPress::rewindKey = 0x30003;