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.

976 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.autoresizesSubviews = NO;
  391. window.transform = CGAffineTransformIdentity;
  392. window.frame = r;
  393. window.opaque = component.isOpaque();
  394. window.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0];
  395. [((JuceUIWindow*) window) setOwner: this];
  396. if (component.isAlwaysOnTop())
  397. window.windowLevel = UIWindowLevelAlert;
  398. view.frame = CGRectMake (0, 0, r.size.width, r.size.height);
  399. window.rootViewController = controller;
  400. [window addSubview: view];
  401. window.hidden = view.hidden;
  402. }
  403. setTitle (component.getName());
  404. Desktop::getInstance().addFocusChangeListener (this);
  405. }
  406. UIViewComponentPeer::~UIViewComponentPeer()
  407. {
  408. Desktop::getInstance().removeFocusChangeListener (this);
  409. view->owner = nullptr;
  410. [view removeFromSuperview];
  411. [view release];
  412. [controller release];
  413. if (! isSharedWindow)
  414. {
  415. [((JuceUIWindow*) window) setOwner: nil];
  416. [window release];
  417. }
  418. }
  419. //==============================================================================
  420. void UIViewComponentPeer::setVisible (bool shouldBeVisible)
  421. {
  422. view.hidden = ! shouldBeVisible;
  423. if (! isSharedWindow)
  424. window.hidden = ! shouldBeVisible;
  425. }
  426. void UIViewComponentPeer::setTitle (const String&)
  427. {
  428. // xxx is this possible?
  429. }
  430. void UIViewComponentPeer::setBounds (const Rectangle<int>& newBounds, const bool isNowFullScreen)
  431. {
  432. fullScreen = isNowFullScreen;
  433. if (isSharedWindow)
  434. {
  435. CGRect r = convertToCGRect (newBounds);
  436. if (view.frame.size.width != r.size.width || view.frame.size.height != r.size.height)
  437. [view setNeedsDisplay];
  438. view.frame = r;
  439. }
  440. else
  441. {
  442. window.frame = convertToCGRect (rotatedScreenPosToReal (newBounds));
  443. view.frame = CGRectMake (0, 0, (CGFloat) newBounds.getWidth(), (CGFloat) newBounds.getHeight());
  444. handleMovedOrResized();
  445. }
  446. }
  447. Rectangle<int> UIViewComponentPeer::getBounds (const bool global) const
  448. {
  449. CGRect r = view.frame;
  450. if (global && view.window != nil)
  451. {
  452. r = [view convertRect: r toView: view.window];
  453. r = [view.window convertRect: r toWindow: nil];
  454. return realScreenPosToRotated (convertToRectInt (r));
  455. }
  456. return convertToRectInt (r);
  457. }
  458. Point<int> UIViewComponentPeer::localToGlobal (Point<int> relativePosition)
  459. {
  460. return relativePosition + getBounds (true).getPosition();
  461. }
  462. Point<int> UIViewComponentPeer::globalToLocal (Point<int> screenPosition)
  463. {
  464. return screenPosition - getBounds (true).getPosition();
  465. }
  466. void UIViewComponentPeer::setAlpha (float newAlpha)
  467. {
  468. [view.window setAlpha: (CGFloat) newAlpha];
  469. }
  470. void UIViewComponentPeer::setFullScreen (bool shouldBeFullScreen)
  471. {
  472. if (! isSharedWindow)
  473. {
  474. Rectangle<int> r (shouldBeFullScreen ? Desktop::getInstance().getDisplays().getMainDisplay().userArea
  475. : lastNonFullscreenBounds);
  476. if ((! shouldBeFullScreen) && r.isEmpty())
  477. r = getBounds();
  478. // (can't call the component's setBounds method because that'll reset our fullscreen flag)
  479. if (! r.isEmpty())
  480. setBounds (r, shouldBeFullScreen);
  481. component.repaint();
  482. }
  483. }
  484. void UIViewComponentPeer::updateTransformAndScreenBounds()
  485. {
  486. Desktop& desktop = Desktop::getInstance();
  487. const Rectangle<int> oldArea (component.getBounds());
  488. const Rectangle<int> oldDesktop (desktop.getDisplays().getMainDisplay().userArea);
  489. const_cast <Desktop::Displays&> (desktop.getDisplays()).refresh();
  490. window.transform = Orientations::getCGTransformFor (desktop.getCurrentOrientation());
  491. view.transform = CGAffineTransformIdentity;
  492. if (fullScreen)
  493. {
  494. fullScreen = false;
  495. setFullScreen (true);
  496. }
  497. else if (! isSharedWindow)
  498. {
  499. // this will re-centre the window, but leave its size unchanged
  500. const float centreRelX = oldArea.getCentreX() / (float) oldDesktop.getWidth();
  501. const float centreRelY = oldArea.getCentreY() / (float) oldDesktop.getHeight();
  502. const Rectangle<int> newDesktop (desktop.getDisplays().getMainDisplay().userArea);
  503. const int x = ((int) (newDesktop.getWidth() * centreRelX)) - (oldArea.getWidth() / 2);
  504. const int y = ((int) (newDesktop.getHeight() * centreRelY)) - (oldArea.getHeight() / 2);
  505. setBounds (oldArea.withPosition (x, y), false);
  506. }
  507. [view setNeedsDisplay];
  508. }
  509. bool UIViewComponentPeer::contains (Point<int> localPos, bool trueIfInAChildWindow) const
  510. {
  511. if (! component.getLocalBounds().contains (localPos))
  512. return false;
  513. UIView* v = [view hitTest: convertToCGPoint (localPos)
  514. withEvent: nil];
  515. if (trueIfInAChildWindow)
  516. return v != nil;
  517. return v == view;
  518. }
  519. bool UIViewComponentPeer::setAlwaysOnTop (bool alwaysOnTop)
  520. {
  521. if (! isSharedWindow)
  522. window.windowLevel = alwaysOnTop ? UIWindowLevelAlert : UIWindowLevelNormal;
  523. return true;
  524. }
  525. void UIViewComponentPeer::toFront (bool makeActiveWindow)
  526. {
  527. if (isSharedWindow)
  528. [[view superview] bringSubviewToFront: view];
  529. if (makeActiveWindow && window != nil && component.isVisible())
  530. [window makeKeyAndVisible];
  531. }
  532. void UIViewComponentPeer::toBehind (ComponentPeer* other)
  533. {
  534. UIViewComponentPeer* const otherPeer = dynamic_cast <UIViewComponentPeer*> (other);
  535. jassert (otherPeer != nullptr); // wrong type of window?
  536. if (otherPeer != nullptr)
  537. {
  538. if (isSharedWindow)
  539. {
  540. [[view superview] insertSubview: view belowSubview: otherPeer->view];
  541. }
  542. else
  543. {
  544. // don't know how to do this
  545. }
  546. }
  547. }
  548. void UIViewComponentPeer::setIcon (const Image& /*newIcon*/)
  549. {
  550. // to do..
  551. }
  552. //==============================================================================
  553. void UIViewComponentPeer::handleTouches (UIEvent* event, const bool isDown, const bool isUp, bool isCancel)
  554. {
  555. NSArray* touches = [[event touchesForView: view] allObjects];
  556. for (unsigned int i = 0; i < [touches count]; ++i)
  557. {
  558. UITouch* touch = [touches objectAtIndex: i];
  559. if ([touch phase] == UITouchPhaseStationary)
  560. continue;
  561. CGPoint p = [touch locationInView: view];
  562. const Point<int> pos ((int) p.x, (int) p.y);
  563. juce_lastMousePos = pos + getBounds (true).getPosition();
  564. const int64 time = getMouseTime (event);
  565. const int touchIndex = currentTouches.getIndexOfTouch (touch);
  566. ModifierKeys modsToSend (currentModifiers);
  567. if (isDown)
  568. {
  569. if ([touch phase] != UITouchPhaseBegan)
  570. continue;
  571. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  572. modsToSend = currentModifiers;
  573. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  574. handleMouseEvent (touchIndex, pos, modsToSend.withoutMouseButtons(), time);
  575. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  576. return;
  577. }
  578. else if (isUp)
  579. {
  580. if (! ([touch phase] == UITouchPhaseEnded || [touch phase] == UITouchPhaseCancelled))
  581. continue;
  582. modsToSend = modsToSend.withoutMouseButtons();
  583. currentTouches.clearTouch (touchIndex);
  584. if (! currentTouches.areAnyTouchesActive())
  585. isCancel = true;
  586. }
  587. if (isCancel)
  588. {
  589. currentTouches.clearTouch (touchIndex);
  590. modsToSend = currentModifiers = currentModifiers.withoutMouseButtons();
  591. }
  592. handleMouseEvent (touchIndex, pos, modsToSend, time);
  593. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  594. return;
  595. if (isUp || isCancel)
  596. {
  597. handleMouseEvent (touchIndex, Point<int> (-1, -1), modsToSend, time);
  598. if (! isValidPeer (this))
  599. return;
  600. }
  601. }
  602. }
  603. //==============================================================================
  604. static UIViewComponentPeer* currentlyFocusedPeer = nullptr;
  605. void UIViewComponentPeer::viewFocusGain()
  606. {
  607. if (currentlyFocusedPeer != this)
  608. {
  609. if (ComponentPeer::isValidPeer (currentlyFocusedPeer))
  610. currentlyFocusedPeer->handleFocusLoss();
  611. currentlyFocusedPeer = this;
  612. handleFocusGain();
  613. }
  614. }
  615. void UIViewComponentPeer::viewFocusLoss()
  616. {
  617. if (currentlyFocusedPeer == this)
  618. {
  619. currentlyFocusedPeer = nullptr;
  620. handleFocusLoss();
  621. }
  622. }
  623. bool UIViewComponentPeer::isFocused() const
  624. {
  625. return isSharedWindow ? this == currentlyFocusedPeer
  626. : (window != nil && [window isKeyWindow]);
  627. }
  628. void UIViewComponentPeer::grabFocus()
  629. {
  630. if (window != nil)
  631. {
  632. [window makeKeyWindow];
  633. viewFocusGain();
  634. }
  635. }
  636. void UIViewComponentPeer::textInputRequired (const Point<int>&)
  637. {
  638. }
  639. void UIViewComponentPeer::updateHiddenTextContent (TextInputTarget* target)
  640. {
  641. view->hiddenTextView.text = juceStringToNS (target->getTextInRange (Range<int> (0, target->getHighlightedRegion().getStart())));
  642. view->hiddenTextView.selectedRange = NSMakeRange (target->getHighlightedRegion().getStart(), 0);
  643. }
  644. BOOL UIViewComponentPeer::textViewReplaceCharacters (Range<int> range, const String& text)
  645. {
  646. if (TextInputTarget* const target = findCurrentTextInputTarget())
  647. {
  648. const Range<int> currentSelection (target->getHighlightedRegion());
  649. if (range.getLength() == 1 && text.isEmpty()) // (detect backspace)
  650. if (currentSelection.isEmpty())
  651. target->setHighlightedRegion (currentSelection.withStart (currentSelection.getStart() - 1));
  652. if (text == "\r" || text == "\n" || text == "\r\n")
  653. handleKeyPress (KeyPress::returnKey, text[0]);
  654. else
  655. target->insertTextAtCaret (text);
  656. updateHiddenTextContent (target);
  657. }
  658. return NO;
  659. }
  660. void UIViewComponentPeer::globalFocusChanged (Component*)
  661. {
  662. if (TextInputTarget* const target = findCurrentTextInputTarget())
  663. {
  664. Component* comp = dynamic_cast<Component*> (target);
  665. Point<int> pos (component.getLocalPoint (comp, Point<int>()));
  666. view->hiddenTextView.frame = CGRectMake (pos.x, pos.y, 0, 0);
  667. updateHiddenTextContent (target);
  668. [view->hiddenTextView becomeFirstResponder];
  669. }
  670. else
  671. {
  672. [view->hiddenTextView resignFirstResponder];
  673. }
  674. }
  675. //==============================================================================
  676. void UIViewComponentPeer::drawRect (CGRect r)
  677. {
  678. if (r.size.width < 1.0f || r.size.height < 1.0f)
  679. return;
  680. CGContextRef cg = UIGraphicsGetCurrentContext();
  681. if (! component.isOpaque())
  682. CGContextClearRect (cg, CGContextGetClipBoundingBox (cg));
  683. CGContextConcatCTM (cg, CGAffineTransformMake (1, 0, 0, -1, 0, getComponent().getHeight()));
  684. CoreGraphicsContext g (cg, getComponent().getHeight(), [UIScreen mainScreen].scale);
  685. insideDrawRect = true;
  686. handlePaint (g);
  687. insideDrawRect = false;
  688. }
  689. bool UIViewComponentPeer::canBecomeKeyWindow()
  690. {
  691. return (getStyleFlags() & juce::ComponentPeer::windowIgnoresKeyPresses) == 0;
  692. }
  693. //==============================================================================
  694. void Desktop::setKioskComponent (Component* kioskModeComp, bool enableOrDisable, bool /*allowMenusAndBars*/)
  695. {
  696. [[UIApplication sharedApplication] setStatusBarHidden: enableOrDisable
  697. withAnimation: UIStatusBarAnimationSlide];
  698. displays->refresh();
  699. if (ComponentPeer* const peer = kioskModeComp->getPeer())
  700. peer->setFullScreen (enableOrDisable);
  701. }
  702. //==============================================================================
  703. void UIViewComponentPeer::repaint (const Rectangle<int>& area)
  704. {
  705. if (insideDrawRect || ! MessageManager::getInstance()->isThisTheMessageThread())
  706. (new AsyncRepaintMessage (this, area))->post();
  707. else
  708. [view setNeedsDisplayInRect: convertToCGRect (area)];
  709. }
  710. void UIViewComponentPeer::performAnyPendingRepaintsNow()
  711. {
  712. }
  713. ComponentPeer* Component::createNewPeer (int styleFlags, void* windowToAttachTo)
  714. {
  715. return new UIViewComponentPeer (*this, styleFlags, (UIView*) windowToAttachTo);
  716. }
  717. //==============================================================================
  718. const int KeyPress::spaceKey = ' ';
  719. const int KeyPress::returnKey = 0x0d;
  720. const int KeyPress::escapeKey = 0x1b;
  721. const int KeyPress::backspaceKey = 0x7f;
  722. const int KeyPress::leftKey = 0x1000;
  723. const int KeyPress::rightKey = 0x1001;
  724. const int KeyPress::upKey = 0x1002;
  725. const int KeyPress::downKey = 0x1003;
  726. const int KeyPress::pageUpKey = 0x1004;
  727. const int KeyPress::pageDownKey = 0x1005;
  728. const int KeyPress::endKey = 0x1006;
  729. const int KeyPress::homeKey = 0x1007;
  730. const int KeyPress::deleteKey = 0x1008;
  731. const int KeyPress::insertKey = -1;
  732. const int KeyPress::tabKey = 9;
  733. const int KeyPress::F1Key = 0x2001;
  734. const int KeyPress::F2Key = 0x2002;
  735. const int KeyPress::F3Key = 0x2003;
  736. const int KeyPress::F4Key = 0x2004;
  737. const int KeyPress::F5Key = 0x2005;
  738. const int KeyPress::F6Key = 0x2006;
  739. const int KeyPress::F7Key = 0x2007;
  740. const int KeyPress::F8Key = 0x2008;
  741. const int KeyPress::F9Key = 0x2009;
  742. const int KeyPress::F10Key = 0x200a;
  743. const int KeyPress::F11Key = 0x200b;
  744. const int KeyPress::F12Key = 0x200c;
  745. const int KeyPress::F13Key = 0x200d;
  746. const int KeyPress::F14Key = 0x200e;
  747. const int KeyPress::F15Key = 0x200f;
  748. const int KeyPress::F16Key = 0x2010;
  749. const int KeyPress::numberPad0 = 0x30020;
  750. const int KeyPress::numberPad1 = 0x30021;
  751. const int KeyPress::numberPad2 = 0x30022;
  752. const int KeyPress::numberPad3 = 0x30023;
  753. const int KeyPress::numberPad4 = 0x30024;
  754. const int KeyPress::numberPad5 = 0x30025;
  755. const int KeyPress::numberPad6 = 0x30026;
  756. const int KeyPress::numberPad7 = 0x30027;
  757. const int KeyPress::numberPad8 = 0x30028;
  758. const int KeyPress::numberPad9 = 0x30029;
  759. const int KeyPress::numberPadAdd = 0x3002a;
  760. const int KeyPress::numberPadSubtract = 0x3002b;
  761. const int KeyPress::numberPadMultiply = 0x3002c;
  762. const int KeyPress::numberPadDivide = 0x3002d;
  763. const int KeyPress::numberPadSeparator = 0x3002e;
  764. const int KeyPress::numberPadDecimalPoint = 0x3002f;
  765. const int KeyPress::numberPadEquals = 0x30030;
  766. const int KeyPress::numberPadDelete = 0x30031;
  767. const int KeyPress::playKey = 0x30000;
  768. const int KeyPress::stopKey = 0x30001;
  769. const int KeyPress::fastForwardKey = 0x30002;
  770. const int KeyPress::rewindKey = 0x30003;