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.

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