The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1040 lines
33KB

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